diff --git a/REST/functions.php b/REST/functions.php
index 32a76197..46b9b01d 100644
--- a/REST/functions.php
+++ b/REST/functions.php
@@ -1,10 +1,11 @@
retrieve($record);
$pl = $inv->getPositionList();
- $response = array (
+ $response = array(
'document_no' => $inv->document_no,
'register_date' => $inv->register_date,
'payment_date' => $inv->payment_date,
@@ -17,11 +18,13 @@ function sendInvoice($record) {
);
echo json_encode($response);
}
-function sendProduct($record) {
+
+function sendProduct($record)
+{
require_once('modules/EcmProducts/EcmProduct.php');
$prod = new EcmProduct();
$prod->retrieve($record);
- $response = array (
+ $response = array(
'name' => $prod->name,
'code' => $prod->code,
'ean' => $prod->ean,
@@ -32,27 +35,32 @@ function sendProduct($record) {
);
echo json_encode($response);
}
-function copySaleFromTwinpol($record) {
+
+function copySaleFromTwinpol($record)
+{
$db = $GLOBALS['db'];
// check if sale exists
$exists = $db->fetchByAssoc($db->query("SELECT id, document_no FROM ecmsales WHERE edi_zs_id='$record'"));
if (isset($exists)) {
echo "ZS istnieje: ";
- echo ''.$exists['document_no'].'';
+ echo '' . $exists['document_no'] . '';
die();
}
- $res = makeCUrlRequest("https://crm.twinpol.com/REST/index.php?key=d68dac4c-f784-4e1b-8267-9ffcfa0eda4c&action=getSale&record=".$record);
+ $res = makeCUrlRequest("https://crm.twinpol.com/REST/index.php?key=d68dac4c-f784-4e1b-8267-9ffcfa0eda4c&action=getSale&record=" . $record);
$sale = json_decode($res);
$sale->position_list = json_decode($sale->position_list);
- echo "Numer dokumentu TwinPol: ".$sale->document_no."
";
+ echo "Numer dokumentu TwinPol: " . $sale->document_no . "
";
$gotAllProducts = true; // hope :)
$newPositionList = array();
- $total_netto = 0; $total_brutto = 0; $total_discount = 0; $vats = array();
- foreach ( $sale->position_list as $product ) {
- echo 'Produkt: '.$product->product_code;
+ $total_netto = 0;
+ $total_brutto = 0;
+ $total_discount = 0;
+ $vats = array();
+ foreach ($sale->position_list as $product) {
+ echo 'Produkt: ' . $product->product_code;
$p = getProduct(trim($product->product_code));
if (!$p) {
echo ' (produkt nie istnieje w bazie E5)
';
@@ -67,7 +75,7 @@ function copySaleFromTwinpol($record) {
AND deleted=0 LIMIT 0,1"));
if (isset($pricebookPrice)) {
$price_start = $pricebookPrice['price'];
- echo " Cena: ".$price_start." (Amazon_2023)
";
+ echo " Cena: " . $price_start . " (Amazon_2023)
";
} else {
// invoice price?
$fvPrice = $db->fetchByAssoc($db->query("
@@ -82,7 +90,7 @@ function copySaleFromTwinpol($record) {
ORDER BY i.register_date DESC
LIMIT 0,1
"));
-
+
if (!isset($fvPrice)) {
$price_start = 0;
echo " Brak ceny
";
@@ -94,9 +102,9 @@ function copySaleFromTwinpol($record) {
$vat_id = $fvPrice['ecmvat_id'];
$vat_name = $fvPrice['ecmvat_name'];
$vat_value = $fvPrice['ecmvat_value'];
- echo " Cena: ".$price_start." (".$fvPrice['document_no'].")
";
+ echo " Cena: " . $price_start . " (" . $fvPrice['document_no'] . ")
";
}
- }
+ }
$prod = array();
@@ -106,13 +114,13 @@ function copySaleFromTwinpol($record) {
$prod['quantity'] = $product->quantity;
$prod['discount'] = $product->discount;
$prod['price_start'] = $price_start;
- $prod['price_netto'] = round($price_start - ($price_start * ($product->discount / 100)),2);
+ $prod['price_netto'] = round($price_start - ($price_start * ($product->discount / 100)), 2);
$prod['ecmvat_id'] = $vat_id;
$prod['ecmvat_value'] = $vat_value;
$prod['ecmvat_name'] = $vat_name;
- $prod['price_brutto'] = round($prod['price_netto']+($prod['price_netto']*($prod['ecmvat_value']/100)),2);
+ $prod['price_brutto'] = round($prod['price_netto'] + ($prod['price_netto'] * ($prod['ecmvat_value'] / 100)), 2);
$prod['total_netto'] = round($prod['price_netto'] * $prod['quantity'], 2);
- $prod['total_brutto'] = round($prod['total_netto']+($prod['total_netto']*($prod['ecmvat_value']/100)),2);
+ $prod['total_brutto'] = round($prod['total_netto'] + ($prod['total_netto'] * ($prod['ecmvat_value'] / 100)), 2);
$total_netto += $prod['total_netto'];
$total_brutto += $prod['total_brutto'];
@@ -121,7 +129,7 @@ function copySaleFromTwinpol($record) {
if (isset($vats[$prod['ecmvat_value']])) {
$vats[$prod['ecmvat_value']] += $prod['total_netto'];
} else {
- $vats[$prod['ecmvat_value']] = $prod['total_netto'];
+ $vats[$prod['ecmvat_value']] = $prod['total_netto'];
}
$prod['date_entered'] = date('Y-m-d H:i:s');
@@ -143,8 +151,8 @@ function copySaleFromTwinpol($record) {
}
}
$vats_summary = '';
- foreach ($vats as $k=>$v ) {
- $vats_summary .= $k.'%:'.$v.',';
+ foreach ($vats as $k => $v) {
+ $vats_summary .= $k . '%:' . $v . ',';
}
if (!$gotAllProducts) {
@@ -189,13 +197,13 @@ function copySaleFromTwinpol($record) {
$new->edi_zs_id = $sale->id;
$new->edi_zs_document_no = $sale->document_no;
$new->pdf_text =
-"Dostawa
-".$sale->shipping_address_name."
-".$sale->shipping_address_street."
-".$sale->shipping_address_postalcode." ".$sale->shipping_address_city."
-".$sale->shipping_address_country."
-NIP: ".$sale->shipping_nip."
-ILN: ".$sale->shipping_iln;
+ "Dostawa
+" . $sale->shipping_address_name . "
+" . $sale->shipping_address_street . "
+" . $sale->shipping_address_postalcode . " " . $sale->shipping_address_city . "
+" . $sale->shipping_address_country . "
+NIP: " . $sale->shipping_nip . "
+ILN: " . $sale->shipping_iln;
$newId = $new->save(true);
@@ -204,12 +212,13 @@ ILN: ".$sale->shipping_iln;
$document_no = str_replace(" ", "%20", $res['document_no']);
makeCUrlRequest("https://crm.twinpol.com/REST/index.php?key=d68dac4c-f784-4e1b-8267-9ffcfa0eda4c&action=updateSaleE5Number&record=$record&e5_record=$newId&e5_document_no=$document_no");
- echo 'Utworzono ZS.';
+ echo 'Utworzono ZS.';
die();
}
// local helpers
-function getProduct($code) {
+function getProduct($code)
+{
$db = $GLOBALS['db'];
$res = $db->fetchByAssoc($db->query("SELECT * FROM ecmproducts WHERE code='$code' AND deleted=0"));
if (!$res) {
@@ -222,9 +231,10 @@ function getProduct($code) {
}
}
-function makeCUrlRequest($url) {
+function makeCUrlRequest($url)
+{
$curl = curl_init();
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET' );
+ curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
@@ -233,7 +243,8 @@ function makeCUrlRequest($url) {
return curl_exec($curl);
}
-function brecho($msg) {
+function brecho($msg)
+{
echo '
';
var_dump($msg);
echo '
';
@@ -247,7 +258,7 @@ function createCSVReports()
$jobs = [
[
- 'sql' => "
+ 'sql' => "
SELECT
i.document_no,
i.register_date,
@@ -286,7 +297,7 @@ ORDER BY i.register_date DESC;
'filename' => 'invoices_2024.csv',
], // invoices 2024
[
- 'sql' => "
+ 'sql' => "
SELECT
i.document_no,
i.register_date,
@@ -325,7 +336,7 @@ ORDER BY i.register_date DESC;
'filename' => 'invoices_2025.csv',
], // invoices 2025
[
- 'sql' => "
+ 'sql' => "
SELECT
i.document_no,
i.register_date,
@@ -371,7 +382,7 @@ ORDER BY i.register_date DESC;
'filename' => 'correct_invoices_2024.csv',
], // correct invoices 2024
[
- 'sql' => "
+ 'sql' => "
SELECT
i.document_no,
i.register_date,
@@ -417,7 +428,7 @@ ORDER BY i.register_date DESC;
'filename' => 'correct_invoices_2025.csv',
], // correct invoices 2025
[
- 'sql' => "
+ 'sql' => "
SELECT
i.document_no,
i.register_date,
@@ -455,7 +466,7 @@ ORDER BY i.register_date DESC;
'filename' => 'rw_2025.csv',
], // rw 2025
[
- 'sql' => "
+ 'sql' => "
SELECT
i.document_no,
i.register_date,
@@ -493,24 +504,61 @@ ORDER BY i.register_date DESC;
'filename' => 'rw_2024.csv',
], // rw 2024
[
- 'sql' => "
- SELECT
- ss.product_code,
- ss.product_name,
- COALESCE(NULLIF(ss.quantity, ''), 0) AS quantity,
- s.name
- FROM ecmstockstates AS ss
- JOIN ecmstocks AS s ON ss.stock_id = s.id
- ORDER BY quantity + 0 DESC;",
+ 'sql' => "
+SELECT
+ ss.product_code,
+ ss.product_name,
+ ss.product_id,
+ COALESCE(NULLIF(ss.quantity, ''), 0) AS quantity,
+ s.name,
+ COALESCE(si.ordered_quantity, 0) AS ordered_quantity
+FROM ecmstockstates AS ss
+JOIN ecmstocks AS s ON ss.stock_id = s.id
+LEFT JOIN (
+ SELECT
+ i.ecmproduct_id,
+ SUM(i.quantity) AS ordered_quantity
+ FROM ecmsaleitems AS i
+ JOIN ecmsales AS es ON es.id = i.ecmsale_id
+ WHERE es.status IN ('s10','s20','s30')
+ GROUP BY i.ecmproduct_id
+) AS si ON si.ecmproduct_id = ss.product_id
+ORDER BY quantity + 0 DESC;",
'filename' => 'stocks.csv',
- ], // stocks
+ ],// stocks
+ [
+ 'sql' => "
+ SELECT
+ i.code AS product_code,
+ i.name AS product_name,
+ i.quantity,
+ i.price_netto,
+ es.document_no,
+ CASE es.status
+ WHEN 's10' THEN 'Planowany'
+ WHEN 's20' THEN 'Oczekujący'
+ WHEN 's30' THEN 'Zaakceptowany'
+ ELSE 'Nieznane'
+ END AS status,
+ es.register_date,
+ es.delivery_date,
+ es.send_date
+FROM ecmsaleitems AS i
+ JOIN ecmsales AS es ON es.id = i.ecmsale_id
+WHERE es.status IN ('s10','s20','s30')
+ AND es.deleted = '0'
+ AND i.deleted = '0'
+ORDER BY es.register_date DESC, i.position;
+ ",
+ 'filename' => 'sales.csv',
+ ], //sales
];
$report = [];
foreach ($jobs as $job) {
- $sql = $job['sql'];
+ $sql = $job['sql'];
$filename = $job['filename'];
- $headers = isset($job['headers']) ? $job['headers'] : null;
+ $headers = isset($job['headers']) ? $job['headers'] : null;
$res = $db->query($sql);
$fullpath = rtrim($exportDir, "/") . "/" . $filename;
@@ -518,9 +566,9 @@ ORDER BY i.register_date DESC;
$result = exportToCSVFile($res, $fullpath, $headers, ';', true);
if ($result['ok']) {
- $report[] = "OK → {$result['path']} (wiersze: {$result['rows']})".PHP_EOL;
+ $report[] = "OK → {$result['path']} (wiersze: {$result['rows']})" . PHP_EOL;
} else {
- $report[] = "ERR → {$result['path']} ({$result['error']})".PHP_EOL;;
+ $report[] = "ERR → {$result['path']} ({$result['error']})" . PHP_EOL;;
}
}
@@ -528,6 +576,7 @@ ORDER BY i.register_date DESC;
exit;
}
}
+
function exportToCSVFile($res, $fullpath, array $headers = null, $delimiter = ';', $withBom = true)
{
$db = $GLOBALS['db'];
@@ -535,16 +584,16 @@ function exportToCSVFile($res, $fullpath, array $headers = null, $delimiter = ';
$dir = dirname($fullpath);
if (!is_dir($dir)) {
if (!@mkdir($dir, 0775, true)) {
- return ['ok'=>false, 'path'=>$fullpath, 'rows'=>0, 'error'=>"Nie mogę utworzyć katalogu: $dir"];
+ return ['ok' => false, 'path' => $fullpath, 'rows' => 0, 'error' => "Nie mogę utworzyć katalogu: $dir"];
}
}
if (!is_writable($dir)) {
- return ['ok'=>false, 'path'=>$fullpath, 'rows'=>0, 'error'=>"Katalog nie jest zapisywalny: $dir"];
+ return ['ok' => false, 'path' => $fullpath, 'rows' => 0, 'error' => "Katalog nie jest zapisywalny: $dir"];
}
$fp = @fopen($fullpath, 'w');
if ($fp === false) {
- return ['ok'=>false, 'path'=>$fullpath, 'rows'=>0, 'error'=>"Nie mogę otworzyć pliku do zapisu: $fullpath"];
+ return ['ok' => false, 'path' => $fullpath, 'rows' => 0, 'error' => "Nie mogę otworzyć pliku do zapisu: $fullpath"];
}
// BOM dla Excel PL
@@ -561,7 +610,7 @@ function exportToCSVFile($res, $fullpath, array $headers = null, $delimiter = ';
fputcsv($fp, $headers, $delimiter);
}
fclose($fp);
- return ['ok'=>true, 'path'=>$fullpath, 'rows'=>0, 'error'=>null];
+ return ['ok' => true, 'path' => $fullpath, 'rows' => 0, 'error' => null];
}
// dynamiczne nagłówki, jeśli nie podano
@@ -574,19 +623,23 @@ function exportToCSVFile($res, $fullpath, array $headers = null, $delimiter = ';
// zapisz pierwszy wiersz w kolejności nagłówków
$line = [];
- foreach ($headers as $h) { $line[] = isset($first[$h]) ? $first[$h] : ''; }
+ foreach ($headers as $h) {
+ $line[] = isset($first[$h]) ? $first[$h] : '';
+ }
fputcsv($fp, $line, $delimiter);
$count = 1;
// pozostałe wiersze
while ($row = $db->fetchByAssoc($res)) {
$line = [];
- foreach ($headers as $h) { $line[] = isset($row[$h]) ? $row[$h] : ''; }
+ foreach ($headers as $h) {
+ $line[] = isset($row[$h]) ? $row[$h] : '';
+ }
fputcsv($fp, $line, $delimiter);
$count++;
}
fclose($fp);
$chmod_ok = @chmod($fullpath, 0664);
- return ['ok'=>true, 'path'=>$fullpath, 'rows'=>$count, 'chmod'=>$chmod_ok, 'error'=>null];
+ return ['ok' => true, 'path' => $fullpath, 'rows' => $count, 'chmod' => $chmod_ok, 'error' => null];
}
\ No newline at end of file