From d9240e184d962e4e9ae9cdaa493641749980aca6 Mon Sep 17 00:00:00 2001 From: Xenomporio <> Date: Mon, 18 Jul 2022 20:27:17 +0200 Subject: [PATCH] Autoversand_manuell cronjob now able to send orders with delivery date in the future, if selected by user --- cronjobs/autoversand_manuell.php | 26 +++- cronjobs/tickets.php | 212 +++++++++++++++++++++++++++++++ www/pages/auftrag.php | 5 +- 3 files changed, 234 insertions(+), 9 deletions(-) create mode 100644 cronjobs/tickets.php diff --git a/cronjobs/autoversand_manuell.php b/cronjobs/autoversand_manuell.php index 1d03e569..99c2654d 100644 --- a/cronjobs/autoversand_manuell.php +++ b/cronjobs/autoversand_manuell.php @@ -59,6 +59,24 @@ if(!defined('FPDF_FONTPATH')) $cronjobname = 'autoversand_manuell'; +$mutex = $app->DB->Select( + "SELECT MAX(`mutex`) FROM `prozessstarter` WHERE (`parameter` = '".$cronjobname."')" +); +if($mutex){ + $app->DB->Update( + "UPDATE `prozessstarter` + SET `mutexcounter`=`mutexcounter`+1 + WHERE `mutex` = 1 AND (`parameter` = '".$cronjobname."')" + ); + + file_append($debugfile,"MUTEX"); + + return; +} +$app->DB->Update( + "UPDATE `prozessstarter` SET `mutex`='1', `mutexcounter` = 0 WHERE (`parameter` = '".$cronjobname."')" +); + // START APPLICATION $objAuftrag = $app->loadModule('auftrag'); @@ -71,11 +89,7 @@ $pendingorders = $app->DB->SelectArr( "SELECT id FROM auftrag AS a WHERE a.id!='' AND (a.belegnr!=0 OR a.belegnr!='') - AND a.status='freigegeben' AND a.autoversand='1' AND a.cronjobkommissionierung > 0 - AND a.inbearbeitung=0 AND a.nachlieferung!='1' AND a.vorkasse_ok='1' - AND a.porto_ok='1' AND a.lager_ok='1' AND a.check_ok='1' AND a.ust_ok='1' - AND a.liefertermin_ok='1' AND kreditlimit_ok='1' AND liefersperre_ok='1' - " + AND a.status='freigegeben' AND a.cronjobkommissionierung > 0" ); if (!is_null($pendingorders)) { @@ -84,7 +98,7 @@ if (!is_null($pendingorders)) { foreach ($pendingorders as $pendingorder) { /* Process each order */ - if($objAuftrag->AuftragVersand($pendingorder['id'])) { + if($objAuftrag->AuftragVersand($pendingorder['id'],true)) { // Ignore shipdate -> The order has been marked, send it $processed_orders_num++; } else { } diff --git a/cronjobs/tickets.php b/cronjobs/tickets.php new file mode 100644 index 00000000..d10b66b2 --- /dev/null +++ b/cronjobs/tickets.php @@ -0,0 +1,212 @@ +erp; +$conf = $app->Conf; + +/** @var Logger $logger */ +$logger = $app->Container->get('Logger'); + +$cronjobname = 'tickets'; +/* +$mutex = $app->DB->Select( + "SELECT MAX(`mutex`) FROM `prozessstarter` WHERE (`parameter` = '".$cronjobname."')" +); +if($mutex){ + $app->DB->Update( + "UPDATE `prozessstarter` + SET `mutexcounter`=`mutexcounter`+1 + WHERE `mutex` = 1 AND (`parameter` = '".$cronjobname."')" + ); + + file_append($debugfile,"MUTEX"); + + return; +} +$app->DB->Update( + "UPDATE `prozessstarter` SET `mutex`='1', `mutexcounter` = 0 WHERE (`parameter` = '".$cronjobname."')" +); +*/ + +// get all email Accounts that have the ticket system active +/** @var EmailAccountGateway $accountGateway */ +$accountGateway = $app->Container->get('EmailAccountGateway'); +$accounts = $accountGateway->getAccountsWithTicketActive(); + + file_append($debugfile,"Accs:".count($accounts).";"); + + +// only load services if there is at least one account to import (performance) +$ticketModule = null; +$factory = null; +$configProvider = null; +$formatHelper = null; +$importHelperFactory = null; +if(!empty($accounts)){ + /** @var Ticket $ticketModule */ + $ticketModule = $app->erp->LoadModul('ticket'); + /** @var MailClientFactory $factory */ + $factory = $app->Container->get('MailClientFactory'); + /** @var MailClientConfigProvider $configProvider */ + $configProvider = $app->Container->get('MailClientConfigProvider'); + /** @var TicketFormatter $formatHelper */ + $formatHelper = $app->Container->get('TicketFormatter'); + /** @var TicketImportHelperFactory $importHelperFactory */ + $importHelperFactory = $app->Container->get('TicketImportHelperFactory'); +} + +$totalEmailsImportCount = 0; +foreach ($accounts as $account) { + $logger->debug( + 'Start imap ticket import for {email}', + ['email' => $account->getEmailAddress(), 'account' => $account] + ); + + file_append($debugfile,"Account ".$account->getemailAddress()); + + // create mail client + try { + $mailConfig = $configProvider->createImapConfigFromAccount($account); + $mailClient = $factory->createImapClient($mailConfig); + } catch (Exception $e) { + $logger->error('Failed to create email client', ['error' => (string)$e, 'account' => $account]); + + file_append($debugfile,"Failed 1"); + + continue; + } + + file_append($debugfile,"Connect to ".."SSL: ".$configProvider->isSslEnabled()." auth ".getAuthType()."\n"); + + // connect mail client + try { + try { + $mailClient->connect(); + + file_append($debugfile,"Meh"); + + } catch (Exception $e) { + $logger->error('Error during imap connection', ['error' => (string)$e, 'account' => $account]); + + file_append($debugfile,"Error ".(string)$e); + + continue; + } + } + + file_append($debugfile,"2"); + + // connet to INBOX folder + try { + $mailClient->selectFolder('INBOX'); + } catch (Exception $e) { + $logger->error('Failed to select INBOX folder', ['error' => (string)$e, 'account' => $account]); + + + file_append($debugfile,"Failed 2"); + + + continue; + } + + $projectId = $account->getProjectId() > 0 ? $account->getProjectId() : 1; + $delete_msg = 0; + $daysold = $account->getBackupDeleteAfterDays(); + + // determine search criteria for new messages + $datet = '2012-12-24'; + if ($account->getImportStartDateAsString() !== '0000-00-00') { + $datesince = date('d-M-Y', strtotime($account->getImportStartDateAsString())); + $criteria = 'UNSEEN SINCE ' . $datesince; + } else { + $criteria = 'UNSEEN'; + } + + file_append($debugfile,"3"); + + // search new messages + try { + $searchResult = $mailClient->searchMessages($criteria); + } catch (Exception $e) { + $logger->error('Error during imap search', ['exception' => $e]); + + file_append($debugfile,"Failed 3"); + + + continue; + } + $logger->debug('unread emails to import: {message_count}', ['message_count' => count($searchResult)]); + + // set mutex if there is more than 5 emails to import + if (count($searchResult) > 5) { + $app->DB->Update( + "UPDATE `prozessstarter` + SET `mutex`=1, `mutexcounter` = 0, `letzteausfuerhung` = NOW() + WHERE (`parameter` = '".$cronjobname."')" + ); + } + $importer = $importHelperFactory->create($mailClient, $account, $projectId); + $insertedMailsCount = $importer->importMessages($searchResult); + $totalEmailsImportCount += $insertedMailsCount; + + // set mutex if the total amount of imported emails is more than 10 + if ($totalEmailsImportCount > 10) { + $app->DB->Update( + "UPDATE `prozessstarter` + SET `mutex`=1, `mutexcounter` = 0, `letzteausfuerhung` = NOW() + WHERE (`parameter` = '".$cronjobname."')" + ); + } + + $mailClient->expunge(); + $mailClient->disconnect(); + + if ( + method_exists($app->erp, 'canRunCronjob') + && !$app->erp->canRunCronjob(['supportmails', 'tickets']) + ) { + + $logger->error('Tickets error'); + + file_append($debugfile,"Failed 5"); + + + return; + } + $app->DB->Update( + "UPDATE `prozessstarter` + SET `mutex`=1, `mutexcounter` = 0, `letzteausfuerhung` = NOW() + WHERE (`parameter` = '".$cronjobname."')" + ); +} + +$app->DB->Update( + "UPDATE `prozessstarter` SET `mutex`=0,`mutexcounter`=0 WHERE (`parameter` = '".$cronjobname."')" +); + +file_append($debugfile,"END"); diff --git a/www/pages/auftrag.php b/www/pages/auftrag.php index b56b6adb..c22a1b29 100644 --- a/www/pages/auftrag.php +++ b/www/pages/auftrag.php @@ -695,7 +695,7 @@ class Auftrag extends GenAuftrag FROM auftrag a"; - $status_where = 'a.cronjobkommissionierung > 0 AND a.lager_ok=1 AND a.porto_ok=1 AND a.ust_ok=1 AND a.vorkasse_ok=1 AND a.nachnahme_ok=1 AND a.autoversand=1 AND a.check_ok=1 AND a.kreditlimit_ok=1 AND a.liefersperre_ok=1'; // liefertermin_ok special treatment + $status_where = 'a.cronjobkommissionierung > 0'; $where = "a.status = 'freigegeben' AND ".$status_where; // $groupby = ""; @@ -6439,8 +6439,7 @@ Die Gesamtsumme stimmt nicht mehr mit ursprünglich festgelegten Betrag '. $unversendet = $this->app->DB->Select("SELECT count(a.id) FROM auftrag as a LEFT JOIN projekt p ON p.id=a.projekt WHERE a.id!='' AND (a.belegnr!=0 OR a.belegnr!='') AND a.status='freigegeben' AND a.inbearbeitung=0 AND a.nachlieferung!='1' AND a.autoversand='1' AND a.cronjobkommissionierung = 0 AND a.liefertermin_ok='1' AND kreditlimit_ok='1' AND liefersperre_ok='1' AND a.vorkasse_ok='1' AND a.porto_ok='1' AND a.lager_ok='1' AND a.check_ok='1' AND a.ust_ok='1' " . $this->app->erp->ProjektRechte('p.id', true, 'a.vertriebid')); - $warteschleife = $this->app->DB->Select("SELECT count(a.id) FROM auftrag as a LEFT JOIN projekt p ON p.id=a.projekt WHERE a.id!='' AND (a.belegnr!=0 OR a.belegnr!='') AND a.status='freigegeben' AND a.inbearbeitung=0 AND a.nachlieferung!='1' AND a.autoversand='1' AND a.liefertermin_ok='1' AND kreditlimit_ok='1' AND liefersperre_ok='1' - AND a.vorkasse_ok='1' AND a.porto_ok='1' AND a.lager_ok='1' AND a.check_ok='1' AND a.ust_ok='1' AND a.cronjobkommissionierung > 0 " . $this->app->erp->ProjektRechte('p.id', true, "a.vertriebid")); + $warteschleife = $this->app->DB->Select("SELECT count(a.id) FROM auftrag as a LEFT JOIN projekt p ON p.id=a.projekt WHERE a.id!='' AND (a.belegnr!=0 OR a.belegnr!='') AND a.status='freigegeben' AND a.cronjobkommissionierung > 0 " . $this->app->erp->ProjektRechte('p.id', true, "a.vertriebid")); if($unversendet > 0) { $unversendet ='('.$unversendet.')';