diff --git a/index.php b/index.php index 16d040ec..3c597033 100755 --- a/index.php +++ b/index.php @@ -27,7 +27,7 @@ if ($SHOW_ERRORS) { * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more * details. - * + * You should have received a copy of the GNU Affero General Public License along with * this program; if not, see http://www.gnu.org/licenses or write to the Free * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA diff --git a/info.php b/info.php index ec65ae7b..61ace196 100644 --- a/info.php +++ b/info.php @@ -1,2 +1,2 @@ query("SELECT * FROM config WHERE category='apilo'"); + $config = []; + while ($row = $db->fetchByAssoc($dbRes)) { + $config[$row['name']] = $row['value']; + } + return $config; +} +function brecho($msg) +{ +echo '
';
+    var_dump($msg);
+    echo PHP_EOL;
+    echo '

'; +} \ No newline at end of file diff --git a/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/checkApiloProducts.php b/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/checkApiloProducts.php new file mode 100644 index 00000000..bb45c103 --- /dev/null +++ b/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/checkApiloProducts.php @@ -0,0 +1,57 @@ +products as $product) { + $p = $db->fetchByAssoc($db->query(sprintf("SELECT * FROM ecmproducts WHERE code = '%s' AND deleted = 0", trim($db->quote($product->sku))))); + if (!$p) { + echo 'Brak produktu: '.$product->sku.'
'; + } +} +echo '---------------------


'; +brecho($products); + +function loadApiloConfiguration() +{ + global $db; + $dbRes = $db->query("SELECT * FROM config WHERE category='apilo'"); + $config = []; + while ($row = $db->fetchByAssoc($dbRes)) { + $config[$row['name']] = $row['value']; + } + return $config; +} +function loadApiloProducts($token) { + $url = "https://twinpol.apilo.com/rest/api/warehouse/product/"; + $params = [ + 'limit' => 300, + 'offset' => 0 + ]; + $url .= '?' . http_build_query($params); + $headers = [ + 'Authorization: Bearer ' . $token, + 'Content-Type: application/json', + 'Accept: application/json' + ]; + $curl = curl_init($url); + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_TIMEOUT, 30); + $response = curl_exec($curl); + $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); + curl_close($curl); + if ($httpCode !== 200) { + return false; + } + return json_decode($response); +} +function brecho($msg) +{ + echo '
';
+    var_dump($msg);
+    echo PHP_EOL;
+    echo '

'; +} \ No newline at end of file diff --git a/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/ecommerceInvoicesListView.js b/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/ecommerceInvoicesListView.js index e6c2cf2e..8543d164 100644 --- a/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/ecommerceInvoicesListView.js +++ b/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/ecommerceInvoicesListView.js @@ -106,7 +106,9 @@ function blockUI($msg) { message: $msg }); } - function copyToClipboard(text) { navigator.clipboard.writeText(text); +} +function openDetails(id) { + window.open("index.php?module=EcmInvoiceOuts&action=ecommerce&apilo_details="+id, "_blank"); } \ No newline at end of file diff --git a/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/ecommerceInvoicesListView.php b/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/ecommerceInvoicesListView.php index b3ad160d..49fb4eb8 100644 --- a/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/ecommerceInvoicesListView.php +++ b/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/ecommerceInvoicesListView.php @@ -50,37 +50,122 @@ function show() echo $smarty->display(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/ecommerceInvoicesListView.tpl'); } -function getInvoices($source, $date, $type) -{ +function getInvoices($source, $date, $type) { $db = $GLOBALS['db']; $date .= '%'; - $query = "SELECT i.*, wz.document_no AS wz_document_no, wz.id AS wz_id FROM ecommerce_invoices AS i - LEFT JOIN ecmstockdocouts AS wz on wz.id = i.ecmstockdocout_id - "; - $query .= " WHERE i.register_date LIKE '$date'"; + + $query = "SELECT + i.id, + i.document_no, + i.type, + i.register_date, + i.sell_date, + i.origin, + i.order_no, + i.parent_name, + i.parent_nip, + i.parent_address_city, + i.parent_address_postalcode, + i.parent_address_street, + i.parent_address_country, + i.parent_address_country_code, + i.currency, + i.total_netto, + i.total_brutto, + i.total_vat, + i.url, + i.corrected_invoice_id, + i.ecmstockdocout_id, + i.ecmstockdoccorrect_id, + i.ecmsale_id, + i.series_id, + wz.document_no AS wz_document_no, + wz.id AS wz_id, + p.id as product_id, + p.code as product_code, + p.name as product_name, + ip.quantity, + ip.price_netto, + ip.price_brutto, + ip.price_vat, + ip.vat_value, + ip.code as ecommerce_code + FROM ecommerce_invoices AS i + LEFT JOIN ecmstockdocouts AS wz ON wz.id = i.ecmstockdocout_id + LEFT JOIN ecommerce_invoices_products AS ip ON i.id = ip.invoice_id + LEFT JOIN ecmproducts AS p ON ip.ecmproduct_id = p.id + WHERE i.register_date LIKE '$date'"; + if ($source != '') { if ($source == 'baselinker') { $query .= " AND i.origin IN ('allegro', 'shop')"; + } else if ($source == 'apilo') { + $query .= " AND i.origin LIKE 'apilo%'"; } else { - $query .= " AND i.origin = '$source'"; + $query .= " AND i.origin = '" . $db->quote($source) . "'"; } } + if ($type != '') { - $query .= " AND i.type='$type'"; + $query .= " AND i.type = '" . $db->quote($type) . "'"; } + $query .= " ORDER BY i.register_date"; $result = $db->query($query); $invoices = array(); + while ($row = $db->fetchByAssoc($result)) { - // format date in row register_date from mysql format into d.m.Y - $row['register_date'] = date('d.m.Y', strtotime($row['register_date'])); - $row['sell_date'] = date('d.m.Y', strtotime($row['sell_date'])); - $row['products'] = getInvoicProducts($row['id']); - $row['sum_by_products'] = getInvoiceSumByProducts($row['id']); - $invoices[] = $row; + if (!isset($invoices[$row['id']])) { + $invoices[$row['id']] = array( + 'id' => $row['id'], + 'document_no' => $row['document_no'], + 'type' => $row['type'], + 'register_date' => date('d.m.Y', strtotime($row['register_date'])), + 'sell_date' => date('d.m.Y', strtotime($row['sell_date'])), + 'origin' => $row['origin'], + 'order_no' => $row['order_no'], + 'parent_name' => $row['parent_name'], + 'parent_nip' => $row['parent_nip'], + 'parent_address_city' => $row['parent_address_city'], + 'parent_address_postalcode' => $row['parent_address_postalcode'], + 'parent_address_street' => $row['parent_address_street'], + 'parent_address_country' => $row['parent_address_country'], + 'parent_address_country_code' => $row['parent_address_country_code'], + 'currency' => $row['currency'], + 'total_netto' => $row['total_netto'], + 'total_brutto' => $row['total_brutto'], + 'total_vat' => $row['total_vat'], + 'url' => $row['url'], + 'corrected_invoice_id' => $row['corrected_invoice_id'], + 'ecmstockdocout_id' => $row['ecmstockdocout_id'], + 'ecmstockdoccorrect_id' => $row['ecmstockdoccorrect_id'], + 'ecmsale_id' => $row['ecmsale_id'], + 'series_id' => $row['series_id'], + 'wz_document_no' => $row['wz_document_no'], + 'wz_id' => $row['wz_id'], + 'products' => array(), + 'sum_by_products' => 0 + ); + } + + if ($row['product_id']) { + $invoices[$row['id']]['products'][] = array( + 'id' => $row['product_id'], + 'code' => $row['product_code'], + 'name' => $row['product_name'], + 'price_netto' => $row['price_netto'], + 'price_brutto' => $row['price_brutto'], + 'quantity' => $row['quantity'], + 'ecommerce_code' => $row['ecommerce_code'], + 'vat_value' => str_replace(".00", "", $row['vat_value']) + ); + + $invoices[$row['id']]['sum_by_products'] += ($row['price_netto'] * $row['quantity']); + } } - return $invoices; + + return array_values($invoices); } function getInvoiceSumByProducts($invoiceId) { $db = $GLOBALS['db']; @@ -118,6 +203,7 @@ function getSources() { while ($row = $db->fetchByAssoc($res)) { $sources[]= $row['origin']; } + $sources[] = 'apilo'; $sources[] = 'baselinker'; return $sources; } diff --git a/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/ecommerceInvoicesListView.tpl b/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/ecommerceInvoicesListView.tpl index f57ae819..a340d20e 100644 --- a/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/ecommerceInvoicesListView.tpl +++ b/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/ecommerceInvoicesListView.tpl @@ -149,7 +149,12 @@ {$ROW.currency} - {$ROW.total_netto} + {$ROW.total_netto}
+ {$ROW.sum_by_products} + {math equation="abs(x-y)" x=$ROW.total_netto y=$ROW.sum_by_products assign="price_difference"} + {if $price_difference > 0.02} + !!! + {/if} {$ROW.total_brutto} diff --git a/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/importApiloInvoices.php b/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/importApiloInvoices.php index d9ba41d9..fdbd22de 100644 --- a/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/importApiloInvoices.php +++ b/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/importApiloInvoices.php @@ -1,10 +1,12 @@ query("SELECT COUNT(id) as last_id FROM ecommerce_invoices WHERE origin = 'apilo'"); + $dbRes = $db->query("SELECT COUNT(id) as last_id FROM ecommerce_invoices WHERE origin LIKE 'apilo%'"); $offset = intval($db->fetchByAssoc($dbRes)['last_id']); $invoices = loadApiloInvoices($apilo_config['token'], $offset); @@ -12,16 +14,18 @@ function importApiloInvoices() if (isset($invoices->error)) { if (refreshApiloToken($apilo_config['refresh_token'], $apilo_config['client_id'], $apilo_config['client_secret']) == true) { $apilo_config = loadApiloConfiguration(); - $invoices = loadApiloInvoices($apilo_config['access_token'], $offset); + $invoices = loadApiloInvoices($apilo_config['token'], $offset); } else { - die('Nie udało się odświeżyć tokena apilo'); + return false; } } - brecho(count($invoices->documents)); - $platforms = loadApiloPlatformsList($apilo_config['token']); + if (!$platforms) { + return false; + } + if (isset($invoices->documents) && count($invoices->documents) > 0) { foreach ($invoices->documents as $invoice) { addapiloInvoice($invoice, $platforms, $apilo_config['token']); @@ -29,7 +33,6 @@ function importApiloInvoices() } return true; } - function loadApiloConfiguration() { global $db; @@ -101,17 +104,14 @@ function refreshApiloToken($refreshToken, $clientId, $clientSecret) $tokenData = json_decode($response, true); if (isset($tokenData['access_token'])) { - // Zapisujemy nowy token w bazie global $db; $db->query("UPDATE config SET value='" . $tokenData['access_token'] . "' WHERE category='apilo' AND name='access_token'"); if (isset($tokenData['refresh_token'])) { $db->query("UPDATE config SET value='" . $tokenData['refresh_token'] . "' WHERE category='apilo' AND name='refresh_token'"); } - return true; } - return false; } function addApiloInvoice($invoice, $platforms, $token) @@ -119,6 +119,9 @@ function addApiloInvoice($invoice, $platforms, $token) $db = $GLOBALS['db']; $platformId = loadApiloOrderPlatformId($token, $invoice->orderId); + if (!$platformId) { + return false; + } $platformDescription = 'ERROR'; foreach ($platforms as $platform) { if ($platform->id == $platformId) { @@ -210,8 +213,7 @@ function addApiloInvoice($invoice, $platforms, $token) $db->query($query); if ($db->last_error) { - error_log("Błąd dodawania faktury apilo: " . $db->last_error); - return; + return false; } if (isset($invoice->documentItems) && is_array($invoice->documentItems)) { @@ -227,7 +229,7 @@ function addApiloInvoiceProduct($invoiceId, $item) $productId = ''; if (isset($item->sku) && !empty($item->sku)) { - $productResult = $db->query(sprintf("SELECT id FROM ecmproducts WHERE code = '%s'", $db->quote($item->sku))); + $productResult = $db->query(sprintf("SELECT id FROM ecmproducts WHERE code = '%s'", trim($db->quote($item->sku)))); $productRow = $db->fetchByAssoc($productResult); if ($productRow) { $productId = $productRow['id']; @@ -272,7 +274,7 @@ function addApiloInvoiceProduct($invoiceId, $item) $db->query($query); if ($db->last_error) { - error_log("Błąd dodawania pozycji faktury apilo: " . $db->last_error); + return false; } } function loadApiloPlatformsList($token) { @@ -290,7 +292,7 @@ function loadApiloPlatformsList($token) { $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if ($httpCode !== 200) { - return (object)['error' => 'HTTP Error: ' . $httpCode]; + return false; } return json_decode($response); } @@ -309,7 +311,7 @@ function loadApiloOrderPlatformId($token, $orderId) { $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if ($httpCode !== 200) { - return (object)['error' => 'HTTP Error: ' . $httpCode]; + return false; } return json_decode($response)->platformAccountId; } diff --git a/modules/EcmInvoiceOuts/ecommerce.php b/modules/EcmInvoiceOuts/ecommerce.php index d53265be..74cfb457 100644 --- a/modules/EcmInvoiceOuts/ecommerce.php +++ b/modules/EcmInvoiceOuts/ecommerce.php @@ -18,7 +18,11 @@ if (isset($_REQUEST['import_baselinker'])) { } else if (isset($_REQUEST['amazon-wz'])) { include_once(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/amazonWDT/amazonWZ.php'); } else if (isset($_REQUEST['import_apilo'])) { - include_once(getcwd().'/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/importApiloInvoices.php'); + include_once(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/importApiloInvoices.php'); +} else if (isset($_REQUEST['apilo_details'])) { + include_once(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/apiloInvoiceDetails.php'); +} else if (isset($_REQUEST['apilo_products'])) { + include_once(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/checkApiloProducts.php'); } else { include_once(getcwd().'/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/ecommerceInvoicesListView.php'); } diff --git a/modules/EcmSales/eCommerceZS/eCommerceZS.php b/modules/EcmSales/eCommerceZS/eCommerceZS.php index 59d55b5c..7abdda27 100644 --- a/modules/EcmSales/eCommerceZS/eCommerceZS.php +++ b/modules/EcmSales/eCommerceZS/eCommerceZS.php @@ -45,7 +45,7 @@ function getInvoices($dateFrom, $dateTo) { $query = "SELECT ip.ecmproduct_id, ip.code, i.document_no, SUM(ip.quantity) AS quantity, i.register_date, i.id FROM ecommerce_invoices_products AS ip INNER JOIN ecommerce_invoices AS i ON ip.invoice_id = i.id AND i.register_date BETWEEN '$dateFrom' AND '$dateTo' AND i.type='normal' -AND i.origin IN ('allegro', 'shop') +AND (i.origin IN ('allegro', 'shop') OR origin LIKE 'apilo%') WHERE ip.ecmproduct_id != '' AND ip.ecmproduct_id !='165f364e-9301-25ac-5906-58e38f1de4ca' AND i.ecmstockdocout_id IS NULL AND i.ecmsale_id IS NULL GROUP BY i.id, ip.ecmproduct_id diff --git a/modules/Schedulers/Scheduler.php b/modules/Schedulers/Scheduler.php index 5243229f..83170614 100755 --- a/modules/Schedulers/Scheduler.php +++ b/modules/Schedulers/Scheduler.php @@ -340,6 +340,7 @@ class Scheduler extends SugarBean { $this->cleanJobLog(); $allSchedulers = $this->get_full_list('', 'schedulers.status=\'Active\''); + $GLOBALS['log']->info('-----> Scheduler found [ '.count($allSchedulers).' ] ACTIVE jobs'); if(!empty($allSchedulers)) { diff --git a/modules/Schedulers/_AddJobsHere.php b/modules/Schedulers/_AddJobsHere.php index 8b5971ef..fa369024 100644 --- a/modules/Schedulers/_AddJobsHere.php +++ b/modules/Schedulers/_AddJobsHere.php @@ -65,14 +65,15 @@ if (! defined('sugarEntry') || ! sugarEntry) $job_strings = array( 0 => 'getNbpCurrencies', 1 => 'importBaselinkerInvoices', - 2 => 'importBaselinkerCorrectingInvoices' + 2 => 'importBaselinkerCorrectingInvoices', + 3 => 'importApiloInvoices' ); function importApiloInvoices() { try { $GLOBALS['db']->query("USE preDb_0dcc87940d3655fa574b253df04ca1c3;"); require_once('modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/importApiloInvoices.php'); - importApiloInvoices(); + importInvoices(); return true; } catch (Exception $e) { return false;