Update ai exports
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
function sendInvoice($record) {
|
function sendInvoice($record)
|
||||||
|
{
|
||||||
require_once('modules/EcmInvoiceOuts/EcmInvoiceOut.php');
|
require_once('modules/EcmInvoiceOuts/EcmInvoiceOut.php');
|
||||||
$inv = new EcmInvoiceOut();
|
$inv = new EcmInvoiceOut();
|
||||||
$inv->retrieve($record);
|
$inv->retrieve($record);
|
||||||
$pl = $inv->getPositionList();
|
$pl = $inv->getPositionList();
|
||||||
$response = array (
|
$response = array(
|
||||||
'document_no' => $inv->document_no,
|
'document_no' => $inv->document_no,
|
||||||
'register_date' => $inv->register_date,
|
'register_date' => $inv->register_date,
|
||||||
'payment_date' => $inv->payment_date,
|
'payment_date' => $inv->payment_date,
|
||||||
@@ -17,11 +18,13 @@ function sendInvoice($record) {
|
|||||||
);
|
);
|
||||||
echo json_encode($response);
|
echo json_encode($response);
|
||||||
}
|
}
|
||||||
function sendProduct($record) {
|
|
||||||
|
function sendProduct($record)
|
||||||
|
{
|
||||||
require_once('modules/EcmProducts/EcmProduct.php');
|
require_once('modules/EcmProducts/EcmProduct.php');
|
||||||
$prod = new EcmProduct();
|
$prod = new EcmProduct();
|
||||||
$prod->retrieve($record);
|
$prod->retrieve($record);
|
||||||
$response = array (
|
$response = array(
|
||||||
'name' => $prod->name,
|
'name' => $prod->name,
|
||||||
'code' => $prod->code,
|
'code' => $prod->code,
|
||||||
'ean' => $prod->ean,
|
'ean' => $prod->ean,
|
||||||
@@ -32,27 +35,32 @@ function sendProduct($record) {
|
|||||||
);
|
);
|
||||||
echo json_encode($response);
|
echo json_encode($response);
|
||||||
}
|
}
|
||||||
function copySaleFromTwinpol($record) {
|
|
||||||
|
function copySaleFromTwinpol($record)
|
||||||
|
{
|
||||||
$db = $GLOBALS['db'];
|
$db = $GLOBALS['db'];
|
||||||
|
|
||||||
// check if sale exists
|
// check if sale exists
|
||||||
$exists = $db->fetchByAssoc($db->query("SELECT id, document_no FROM ecmsales WHERE edi_zs_id='$record'"));
|
$exists = $db->fetchByAssoc($db->query("SELECT id, document_no FROM ecmsales WHERE edi_zs_id='$record'"));
|
||||||
if (isset($exists)) {
|
if (isset($exists)) {
|
||||||
echo "ZS istnieje: ";
|
echo "ZS istnieje: ";
|
||||||
echo '<a target="new" href="https://crm.e5.pl/index.php?module=EcmSales&action=DetailView&record='.$exists['id'].'">'.$exists['document_no'].'</a>';
|
echo '<a target="new" href="https://crm.e5.pl/index.php?module=EcmSales&action=DetailView&record=' . $exists['id'] . '">' . $exists['document_no'] . '</a>';
|
||||||
die();
|
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 = json_decode($res);
|
||||||
$sale->position_list = json_decode($sale->position_list);
|
$sale->position_list = json_decode($sale->position_list);
|
||||||
echo "Numer dokumentu TwinPol: <b>".$sale->document_no."</b><br>";
|
echo "Numer dokumentu TwinPol: <b>" . $sale->document_no . "</b><br>";
|
||||||
|
|
||||||
$gotAllProducts = true; // hope :)
|
$gotAllProducts = true; // hope :)
|
||||||
$newPositionList = array();
|
$newPositionList = array();
|
||||||
$total_netto = 0; $total_brutto = 0; $total_discount = 0; $vats = array();
|
$total_netto = 0;
|
||||||
foreach ( $sale->position_list as $product ) {
|
$total_brutto = 0;
|
||||||
echo 'Produkt: '.$product->product_code;
|
$total_discount = 0;
|
||||||
|
$vats = array();
|
||||||
|
foreach ($sale->position_list as $product) {
|
||||||
|
echo 'Produkt: ' . $product->product_code;
|
||||||
$p = getProduct(trim($product->product_code));
|
$p = getProduct(trim($product->product_code));
|
||||||
if (!$p) {
|
if (!$p) {
|
||||||
echo ' (produkt nie istnieje w bazie E5)<br>';
|
echo ' (produkt nie istnieje w bazie E5)<br>';
|
||||||
@@ -67,7 +75,7 @@ function copySaleFromTwinpol($record) {
|
|||||||
AND deleted=0 LIMIT 0,1"));
|
AND deleted=0 LIMIT 0,1"));
|
||||||
if (isset($pricebookPrice)) {
|
if (isset($pricebookPrice)) {
|
||||||
$price_start = $pricebookPrice['price'];
|
$price_start = $pricebookPrice['price'];
|
||||||
echo " Cena: ".$price_start." (<a href=\"https://crm.e5.pl/index.php?module=EcmPriceBooks&action=DetailView&record=3e78ac33-7c46-1b94-0a67-653a17c06f9e\" target=\"new\">Amazon_2023</a>)<br>";
|
echo " Cena: " . $price_start . " (<a href=\"https://crm.e5.pl/index.php?module=EcmPriceBooks&action=DetailView&record=3e78ac33-7c46-1b94-0a67-653a17c06f9e\" target=\"new\">Amazon_2023</a>)<br>";
|
||||||
} else {
|
} else {
|
||||||
// invoice price?
|
// invoice price?
|
||||||
$fvPrice = $db->fetchByAssoc($db->query("
|
$fvPrice = $db->fetchByAssoc($db->query("
|
||||||
@@ -94,7 +102,7 @@ function copySaleFromTwinpol($record) {
|
|||||||
$vat_id = $fvPrice['ecmvat_id'];
|
$vat_id = $fvPrice['ecmvat_id'];
|
||||||
$vat_name = $fvPrice['ecmvat_name'];
|
$vat_name = $fvPrice['ecmvat_name'];
|
||||||
$vat_value = $fvPrice['ecmvat_value'];
|
$vat_value = $fvPrice['ecmvat_value'];
|
||||||
echo " Cena: ".$price_start." (<a href=\"https://crm.e5.pl/index.php?module=EcmInvoiceOuts&action=DetailView&record=".$fvPrice['id']."\" target=\"new\">".$fvPrice['document_no']."</a>)<br>";
|
echo " Cena: " . $price_start . " (<a href=\"https://crm.e5.pl/index.php?module=EcmInvoiceOuts&action=DetailView&record=" . $fvPrice['id'] . "\" target=\"new\">" . $fvPrice['document_no'] . "</a>)<br>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,13 +114,13 @@ function copySaleFromTwinpol($record) {
|
|||||||
$prod['quantity'] = $product->quantity;
|
$prod['quantity'] = $product->quantity;
|
||||||
$prod['discount'] = $product->discount;
|
$prod['discount'] = $product->discount;
|
||||||
$prod['price_start'] = $price_start;
|
$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_id'] = $vat_id;
|
||||||
$prod['ecmvat_value'] = $vat_value;
|
$prod['ecmvat_value'] = $vat_value;
|
||||||
$prod['ecmvat_name'] = $vat_name;
|
$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_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_netto += $prod['total_netto'];
|
||||||
$total_brutto += $prod['total_brutto'];
|
$total_brutto += $prod['total_brutto'];
|
||||||
@@ -143,8 +151,8 @@ function copySaleFromTwinpol($record) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
$vats_summary = '';
|
$vats_summary = '';
|
||||||
foreach ($vats as $k=>$v ) {
|
foreach ($vats as $k => $v) {
|
||||||
$vats_summary .= $k.'%:'.$v.',';
|
$vats_summary .= $k . '%:' . $v . ',';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$gotAllProducts) {
|
if (!$gotAllProducts) {
|
||||||
@@ -189,13 +197,13 @@ function copySaleFromTwinpol($record) {
|
|||||||
$new->edi_zs_id = $sale->id;
|
$new->edi_zs_id = $sale->id;
|
||||||
$new->edi_zs_document_no = $sale->document_no;
|
$new->edi_zs_document_no = $sale->document_no;
|
||||||
$new->pdf_text =
|
$new->pdf_text =
|
||||||
"Dostawa
|
"Dostawa
|
||||||
".$sale->shipping_address_name."
|
" . $sale->shipping_address_name . "
|
||||||
".$sale->shipping_address_street."
|
" . $sale->shipping_address_street . "
|
||||||
".$sale->shipping_address_postalcode." ".$sale->shipping_address_city."
|
" . $sale->shipping_address_postalcode . " " . $sale->shipping_address_city . "
|
||||||
".$sale->shipping_address_country."
|
" . $sale->shipping_address_country . "
|
||||||
NIP: ".$sale->shipping_nip."
|
NIP: " . $sale->shipping_nip . "
|
||||||
ILN: ".$sale->shipping_iln;
|
ILN: " . $sale->shipping_iln;
|
||||||
|
|
||||||
$newId = $new->save(true);
|
$newId = $new->save(true);
|
||||||
|
|
||||||
@@ -204,12 +212,13 @@ ILN: ".$sale->shipping_iln;
|
|||||||
$document_no = str_replace(" ", "%20", $res['document_no']);
|
$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");
|
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 '<a target="new" href="https://crm.e5.pl/index.php?module=EcmSales&action=DetailView&record='.$newId.'">Utworzono ZS.</a>';
|
echo '<a target="new" href="https://crm.e5.pl/index.php?module=EcmSales&action=DetailView&record=' . $newId . '">Utworzono ZS.</a>';
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
// local helpers
|
// local helpers
|
||||||
function getProduct($code) {
|
function getProduct($code)
|
||||||
|
{
|
||||||
$db = $GLOBALS['db'];
|
$db = $GLOBALS['db'];
|
||||||
$res = $db->fetchByAssoc($db->query("SELECT * FROM ecmproducts WHERE code='$code' AND deleted=0"));
|
$res = $db->fetchByAssoc($db->query("SELECT * FROM ecmproducts WHERE code='$code' AND deleted=0"));
|
||||||
if (!$res) {
|
if (!$res) {
|
||||||
@@ -222,9 +231,10 @@ function getProduct($code) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeCUrlRequest($url) {
|
function makeCUrlRequest($url)
|
||||||
|
{
|
||||||
$curl = curl_init();
|
$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_VERBOSE, 1);
|
||||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
|
||||||
@@ -233,7 +243,8 @@ function makeCUrlRequest($url) {
|
|||||||
return curl_exec($curl);
|
return curl_exec($curl);
|
||||||
}
|
}
|
||||||
|
|
||||||
function brecho($msg) {
|
function brecho($msg)
|
||||||
|
{
|
||||||
echo '<br><br>';
|
echo '<br><br>';
|
||||||
var_dump($msg);
|
var_dump($msg);
|
||||||
echo '<br><br>';
|
echo '<br><br>';
|
||||||
@@ -494,16 +505,53 @@ ORDER BY i.register_date DESC;
|
|||||||
], // rw 2024
|
], // rw 2024
|
||||||
[
|
[
|
||||||
'sql' => "
|
'sql' => "
|
||||||
SELECT
|
SELECT
|
||||||
ss.product_code,
|
ss.product_code,
|
||||||
ss.product_name,
|
ss.product_name,
|
||||||
|
ss.product_id,
|
||||||
COALESCE(NULLIF(ss.quantity, ''), 0) AS quantity,
|
COALESCE(NULLIF(ss.quantity, ''), 0) AS quantity,
|
||||||
s.name
|
s.name,
|
||||||
FROM ecmstockstates AS ss
|
COALESCE(si.ordered_quantity, 0) AS ordered_quantity
|
||||||
JOIN ecmstocks AS s ON ss.stock_id = s.id
|
FROM ecmstockstates AS ss
|
||||||
ORDER BY quantity + 0 DESC;",
|
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',
|
'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 = [];
|
$report = [];
|
||||||
@@ -518,9 +566,9 @@ ORDER BY i.register_date DESC;
|
|||||||
$result = exportToCSVFile($res, $fullpath, $headers, ';', true);
|
$result = exportToCSVFile($res, $fullpath, $headers, ';', true);
|
||||||
|
|
||||||
if ($result['ok']) {
|
if ($result['ok']) {
|
||||||
$report[] = "OK → {$result['path']} (wiersze: {$result['rows']})".PHP_EOL;
|
$report[] = "OK → {$result['path']} (wiersze: {$result['rows']})" . PHP_EOL;
|
||||||
} else {
|
} 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;
|
exit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function exportToCSVFile($res, $fullpath, array $headers = null, $delimiter = ';', $withBom = true)
|
function exportToCSVFile($res, $fullpath, array $headers = null, $delimiter = ';', $withBom = true)
|
||||||
{
|
{
|
||||||
$db = $GLOBALS['db'];
|
$db = $GLOBALS['db'];
|
||||||
@@ -535,16 +584,16 @@ function exportToCSVFile($res, $fullpath, array $headers = null, $delimiter = ';
|
|||||||
$dir = dirname($fullpath);
|
$dir = dirname($fullpath);
|
||||||
if (!is_dir($dir)) {
|
if (!is_dir($dir)) {
|
||||||
if (!@mkdir($dir, 0775, true)) {
|
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)) {
|
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');
|
$fp = @fopen($fullpath, 'w');
|
||||||
if ($fp === false) {
|
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
|
// BOM dla Excel PL
|
||||||
@@ -561,7 +610,7 @@ function exportToCSVFile($res, $fullpath, array $headers = null, $delimiter = ';
|
|||||||
fputcsv($fp, $headers, $delimiter);
|
fputcsv($fp, $headers, $delimiter);
|
||||||
}
|
}
|
||||||
fclose($fp);
|
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
|
// 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
|
// zapisz pierwszy wiersz w kolejności nagłówków
|
||||||
$line = [];
|
$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);
|
fputcsv($fp, $line, $delimiter);
|
||||||
$count = 1;
|
$count = 1;
|
||||||
|
|
||||||
// pozostałe wiersze
|
// pozostałe wiersze
|
||||||
while ($row = $db->fetchByAssoc($res)) {
|
while ($row = $db->fetchByAssoc($res)) {
|
||||||
$line = [];
|
$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);
|
fputcsv($fp, $line, $delimiter);
|
||||||
$count++;
|
$count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose($fp);
|
fclose($fp);
|
||||||
$chmod_ok = @chmod($fullpath, 0664);
|
$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];
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user