import apilo

This commit is contained in:
2025-09-29 17:18:32 +00:00
parent f709c11d00
commit 601b31439c
12 changed files with 238 additions and 39 deletions

View File

@@ -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

View File

@@ -0,0 +1,41 @@
<?php
$id = $_REQUEST['apilo_details'];
$apilo_config = loadApiloConfiguration();
brecho(loadApiloOrder($apilo_config['token'], $id));
function loadApiloOrder($token, $orderId) {
$url = "https://twinpol.apilo.com/rest/api/orders/".$orderId."/";
$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 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 brecho($msg)
{
echo '<br><pre>';
var_dump($msg);
echo PHP_EOL;
echo '</pre><br>';
}

View File

@@ -0,0 +1,57 @@
<?php
echo 'halo';
$apilo_config = loadApiloConfiguration();
$products = loadApiloProducts($apilo_config['token']);
$db = $GLOBALS['db'];
foreach ($products->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.'<br>';
}
}
echo '---------------------<br><br><br>';
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 '<br><pre>';
var_dump($msg);
echo PHP_EOL;
echo '</pre><br>';
}

View File

@@ -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");
}

View File

@@ -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
);
}
return $invoices;
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 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;
}

View File

@@ -149,7 +149,12 @@
{$ROW.currency}
</td>
<td>
{$ROW.total_netto}
{$ROW.total_netto} <br>
{$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}
<span style="color: red; font-weight: bold; cursor: pointer" onclick="openDetails('{$ROW.order_no}')">!!!</span>
{/if}
</td>
<td>
{$ROW.total_brutto}

View File

@@ -1,10 +1,12 @@
<?php
function importApiloInvoices()
// ?XDEBUG_SESSION_START=PHPSTORM
//importApiloInvoices();
function importInvoices()
{
$apilo_config = loadApiloConfiguration();
$db = $GLOBALS['db'];
$dbRes = $db->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;
}

View File

@@ -19,6 +19,10 @@ if (isset($_REQUEST['import_baselinker'])) {
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');
} 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');
}

View File

@@ -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

View File

@@ -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)) {

View File

@@ -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;