From 8ebfc49ad27fb2c9a1bcf84fa5f04ce45b51e011 Mon Sep 17 00:00:00 2001 From: OpenXE <> Date: Thu, 27 Jun 2024 21:38:18 +0200 Subject: [PATCH] artikeluebertragen WIP bulk transfer, switch to logger --- cronjobs/artikeluebertragen.php | 86 ++++++++++++++++++++++++++++-- www/lib/class.remote.php | 87 +++++++++++++++++++++++-------- www/pages/shopimporter_mirakl.php | 5 +- 3 files changed, 151 insertions(+), 27 deletions(-) diff --git a/cronjobs/artikeluebertragen.php b/cronjobs/artikeluebertragen.php index 36267271..f068e93e 100644 --- a/cronjobs/artikeluebertragen.php +++ b/cronjobs/artikeluebertragen.php @@ -1,5 +1,10 @@ Container->get('Logger'); + if (file_exists(dirname(__DIR__) . '/www/lib/class.erpapi_custom.php') && !class_exists('erpAPICustom')) { include_once dirname(__DIR__) . '/www/lib/class.erpapi_custom.php'; } @@ -29,7 +34,9 @@ if (empty($app->remote)) { } } -$app->erp->LogFile('Cronjob Artikeluebertragung Start'); +$logger->debug( + 'Start' +); $app->DB->Update( "UPDATE `prozessstarter` @@ -38,11 +45,14 @@ $app->DB->Update( ); if ($app->DB->Select("SELECT `mutex` FROM `prozessstarter` WHERE (`parameter` = 'artikeluebertragen') LIMIT 1") == 1) { - return; + $logger->debug( + 'Mutex' + ); +// return; } $articles = $app->DB->SelectArr( - 'SELECT `id`,`shop`,`artikel` FROM `shopexport_artikeluebertragen_check` ORDER BY `id` LIMIT 10' + 'SELECT `id`,`shop`,`artikel` FROM `shopexport_artikeluebertragen_check` ORDER BY `id`' ); if (!empty($articles)) { /** @var Shopexport $objShopexport */ @@ -52,6 +62,13 @@ if (!empty($articles)) { } } +$logger->debug( + 'Prepare', + [ + 'articles' => $articles + ] +); + $anzChecked = []; $anzChanged = []; $lastids = []; @@ -101,6 +118,9 @@ while (!empty($articles)) { $anzChecked[$article['shop']] ); if (method_exists($app->erp, 'canRunCronjob') && !$app->erp->canRunCronjob(['artikeluebertragen'])) { + $logger->debug( + '!canRunCronjob' + ); return; } $articles = $app->DB->SelectArr( @@ -119,6 +139,8 @@ $app->DB->Update( SET `letzteausfuerhung`=NOW(), `mutex` = 1,`mutexcounter`=0 WHERE `parameter` = 'artikeluebertragen'" ); + +/* while ($check > 0) { $shopartikel = $app->DB->Query( "SELECT `id`,`shop`,`artikel`,`check_nr` FROM `shopexport_artikeluebertragen` ORDER BY `id` LIMIT 10" @@ -164,7 +186,65 @@ while ($check > 0) { } $check = $app->DB->Select('SELECT COUNT(`id`) FROM `shopexport_artikeluebertragen`'); +}*/ + + +$sql = "SELECT DISTINCT `shop`, shopexport.`bezeichnung` FROM `shopexport_artikeluebertragen` INNER JOIN shopexport ON shopexport.id = `shop`"; +$shops_to_transmit = $app->DB->SelectArr($sql); + +//$app->erp->LogFile('Cronjob artikeluebertragen '.(!empty($shops_to_transmit)?count($shops_to_transmit):0)." shops", print_r($shops_to_transmit, true)); +$logger->debug( + '{count} Shops', + [ + 'count' => (!empty($shops_to_transmit)?count($shops_to_transmit):0), + 'shops_to_transmit' => $shops_to_transmit + ] +); + +foreach ($shops_to_transmit as $shop_to_transmit) { + $sql = "SELECT `artikel` FROM `shopexport_artikeluebertragen` WHERE `shop` = '".$shop_to_transmit['shop']."'"; + $articles_to_transmit = $app->DB->SelectArr($sql); + + $logger->debug( + '{bezeichnung} (Shop {shop_to_transmit}) {count} Artikel', + [ + 'shop_to_transmit' => $shop_to_transmit['shop'], + 'bezeichnung' => $shop_to_transmit['bezeichnung'], + 'count' => (!empty($articles_to_transmit)?count($articles_to_transmit):0), + 'articles_to_transmit' => $articles_to_transmit + ] + ); + + if (!empty($articles_to_transmit)) { + + $article_ids_to_transmit = array_column($articles_to_transmit, 'artikel'); + + try { + $result = $app->remote->RemoteSendArticleList($shop_to_transmit['shop'], $article_ids_to_transmit); // Expected result is array $articles_to_transmit, field status contains transmission status + } catch (Execption $exception) { + $logger->error( + 'Fehler {bezeichnung} (Shop {shop_to_transmit}) {count} Artikel', + [ + 'shop_to_transmit' => $shop_to_transmit['shop'], + 'bezeichnung' => $shop_to_transmit['bezeichnung'], + 'count' => (!empty($articles_to_transmit)?count($articles_to_transmit):0), + 'exception' => $exception + ] + ); + } + + foreach ($result as $article) { + $app->erp->LagerSync($article['artikel'], true); + } + } else { + + } } + +$logger->debug( + 'Ende' +); + $app->DB->Update( "UPDATE `prozessstarter` SET `letzteausfuerhung`= NOW(), `mutex` = 0,`mutexcounter`=0 diff --git a/www/lib/class.remote.php b/www/lib/class.remote.php index 0ba1f2a5..943c0893 100644 --- a/www/lib/class.remote.php +++ b/www/lib/class.remote.php @@ -1,5 +1,6 @@ app=$app; + $this->logger = $app->Container->get('Logger'); } /** @@ -1171,6 +1176,14 @@ class Remote */ public function RemoteSendArticleList($id,$artikel_arr, $extnummer = '', $nurlager = false) { + + $this->logger->debug( + 'RemoteSendArticleList', + [ + 'artikel_arr' => $artikel_arr + ] + ); + if(!class_exists('ObjGenArtikel') && is_file(dirname(__DIR__) . '/objectapi/mysql/_gen/object.gen.artikel.php')){ include_once dirname(__DIR__) . '/objectapi/mysql/_gen/object.gen.artikel.php'; @@ -1217,8 +1230,16 @@ class Remote for($i=0;$i<$cartikel_arr;$i++) { - $artikel = $artikel_arr[$i]; + + $this->logger->debug( + 'RemoteSendArticleList', + [ + 'artikel' => $artikel, + 'i' => $i + ] + ); + $lagerexport = $this->app->erp->GetArtikelShopEinstellung('autolagerlampe', $artikel, $shopexportarr); $tmp->Select($artikel); $data[$i] = ['artikel' => $artikel,'artikelid' => $artikel]; @@ -1642,17 +1663,19 @@ class Remote AND (v.objekt = 'Standard' OR v.objekt = '') AND (v.adresse = '0' OR v.adresse = '') AND (v.gueltig_bis >= NOW() OR v.gueltig_bis = '0000-00-00') ORDER BY v.preis DESC LIMIT 1"); - $priceInformation = reset($priceInformation); - $defaultPrice = $priceInformation['preis']; - $defaultCurrency = $priceInformation['waehrung'] ?: 'EUR'; - if($preisgruppe && method_exists($this->app->erp, 'GetVerkaufspreisGruppe')){ - $defaultCurrency = 'EUR'; //the follow up function imply EUR as the default currency - $defaultPrice = $this->app->erp->GetVerkaufspreisGruppe($artikel, 1, $preisgruppe); - } - - $data[$i]['waehrung'] = $defaultCurrency; - $data[$i]['preis'] = $defaultPrice; - + + if (!empty($priceInformation)) { + $priceInformation = reset($priceInformation); + $defaultPrice = $priceInformation['preis']; + $defaultCurrency = $priceInformation['waehrung'] ?: 'EUR'; + if($preisgruppe && method_exists($this->app->erp, 'GetVerkaufspreisGruppe')){ + $defaultCurrency = 'EUR'; //the follow up function imply EUR as the default currency + $defaultPrice = $this->app->erp->GetVerkaufspreisGruppe($artikel, 1, $preisgruppe); + } + $data[$i]['waehrung'] = $defaultCurrency; + $data[$i]['preis'] = $defaultPrice; + } + if(!empty($tmp->GetSteuersatz()) && $tmp->GetSteuersatz() != -1){ $data[$i]['steuersatz'] = (float)$tmp->GetSteuersatz(); }elseif($data[$i]['umsatzsteuer'] === 'ermaessigt'){ @@ -1759,8 +1782,6 @@ class Remote * @deprecated Ende */ } - - $data[$i]['bruttopreis'] = $data[$i]['preis'] * $steuer; } $data[$i]['checksum'] = $tmp->GetChecksum(); @@ -2053,11 +2074,11 @@ class Remote $data[$i]['matrix_varianten']['artikel'][$eigenschaft['artikel']][$iv]['values'] = $matrixdaten[$iv]['wert']; } } - + $result = null; if(empty($data)) { continue; - } + } if (!empty($lagerexport)) { $result = $this->sendlistlager($i, $id, $data); } @@ -2182,7 +2203,7 @@ class Remote }else{ $data[$i]['variantevorhanden'] = 0; } - + $result = null; if(empty($data)){ continue; @@ -2445,11 +2466,19 @@ class Remote $this->app->DB->Update($query); $this->app->erp->AuftragProtokoll($orderId, 'Versandmeldung an Shop fehlgeschlagen', $bearbeiter); - $this->app->erp->Logfile('Versandmeldung an Shop fehlgeschlagen', print_r([ +/* $this->app->erp->Logfile('Versandmeldung an Shop fehlgeschlagen', print_r([ 'orderId' => $orderId, 'shopId' => $shopId, - 'message' => $response->getMessage()],true)); - + 'message' => $response->getMessage()],true));*/ + + $this->logger->error('Versandmeldung an Shop fehlgeschlagen', + [ + 'orderId' => $orderId, + 'shopId' => $shopId, + 'message' => $response->getMessage() + ] + ); + return; } @@ -2857,7 +2886,14 @@ class Remote $post_data['data'] = base64_encode(serialize($data)); $client->timeout = 120; if(!$client->post($geturl,$post_data)) { - $this->app->erp->LogFile(mysqli_real_escape_string($this->app->DB->connection,'An error occurred: '.$client->getError())); +// $this->app->erp->LogFile(mysqli_real_escape_string($this->app->DB->connection,'An error occurred: '.$client->getError())); + + $this->logger->error('An error occurred', + [ + 'error' => $client->getError() + ] + ); + throw new Exception('An error occurred: '.$client->getError()); //return 'Netzwerkverbindung von WaWison zu Shopimporter fehlgeschlagen: '.$client->getError(); } @@ -2932,7 +2968,14 @@ class Remote if(!$client->post($geturl,$post_data)) { - $this->app->erp->LogFile(mysqli_real_escape_string($this->app->DB->connection,'An error occurred: '.$client->getError())); +// $this->app->erp->LogFile(mysqli_real_escape_string($this->app->DB->connection,'An error occurred: '.$client->getError())); + + $this->logger->error('An error occurred', + [ + 'error' => $client->getError() + ] + ); + throw new Exception('An error occurred: '.$client->getError()); } return unserialize($aes->decrypt(base64_decode($client->getContent()))); diff --git a/www/pages/shopimporter_mirakl.php b/www/pages/shopimporter_mirakl.php index f482f221..a99ad6ea 100644 --- a/www/pages/shopimporter_mirakl.php +++ b/www/pages/shopimporter_mirakl.php @@ -384,6 +384,7 @@ class Shopimporter_Mirakl extends ShopimporterBase { $this->Log("Angebotsync nach Mirakl hat Fehler", print_r($offer_export_result, true)); $message .= $komma."Angebotsync in Mirakl hat Fehler"; } else { + $this->Log("Angebotsync nach Mirakl ok", print_r($offer_export_result, true)); $message .= $komma."Angebotsimport in Mirakl ok"; } @@ -801,11 +802,11 @@ class Shopimporter_Mirakl extends ShopimporterBase { * returncode articleList 0 = ok, 20 rejected from mirakl */ private function mirakl_create_products(array $articleList) : array { - + $mirakl_create_products_return_value = array(); $mirakl_create_products_return_value['returncode'] = 0; $mirakl_create_products_return_value['articleList'] = $articleList; - + $number_of_articles = 0; $this->Log('Produktexport Start', print_r($csv,true));