From 8caa0fd747d96b32f84fc153426f30fe8022e2d6 Mon Sep 17 00:00:00 2001 From: Sebastian Miecielica Date: Tue, 1 Feb 2022 19:02:32 +0100 Subject: [PATCH 1/2] Set last sync id per user not globally --- lib/BillTechLinksManager.php | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/BillTechLinksManager.php b/lib/BillTechLinksManager.php index b933dc6..7588244 100644 --- a/lib/BillTechLinksManager.php +++ b/lib/BillTechLinksManager.php @@ -68,7 +68,6 @@ public function getBalanceLink($customerId, $params) public function updateForAll() { - global $DB; $actions = array( 'add' => array(), 'update' => array(), @@ -85,8 +84,6 @@ public function updateForAll() return; } - $maxCashId = $DB->GetOne("select max(id) from cash"); - foreach ($customerIds as $idx => $customerId) { echo "Collecting actions for customer " . ($idx + 1) . " of " . count($customerIds) . "\n"; $actions = array_merge_recursive($actions, $this->getCustomerUpdateBalanceActions($customerId)); @@ -99,10 +96,16 @@ public function updateForAll() } if ($this->performActions($actions)) { - $this->updateCustomerInfos($customerIds, $maxCashId); + $this->updateCustomerInfos($customerIds); } } + private function checkIfCustomerCashIdExists($customerId, $cashId) + { + global $DB; + return $DB->GetOne('select count(*) from cash where customerid = ? and id = ?', array($customerId, $cashId)); + } + public function updateCustomerBalance($customerId) { global $DB; @@ -113,7 +116,7 @@ public function updateCustomerBalance($customerId) where bci.customer_id = ? group by bci.customer_id", array($customerId)); - if ($customerInfo['new_last_cash_id'] > $customerInfo['last_cash_id']) { + if ($customerInfo['new_last_cash_id'] > $customerInfo['last_cash_id'] || ($customerInfo['new_last_cash_id'] < $customerInfo['last_cash_id'] && !$this->checkIfCustomerCashIdExists($customerId, $customerInfo['last_cash_id']))) { $actions = $this->getCustomerUpdateBalanceActions($customerId); if ($this->performActions($actions)) { $DB->Execute("update billtech_customer_info set last_cash_id = ? where customer_id = ?", array($customerInfo['new_last_cash_id'], $customerId)); @@ -410,19 +413,18 @@ private function getCustomerIdsForUpdate() left join billtech_customer_info bci on bci.customer_id = cu.id left join cash ca on ca.customerid = cu.id group by bci.customer_id, bci.last_cash_id - having bci.last_cash_id <= coalesce(max(ca.id), 0);"); + having bci.last_cash_id != coalesce(max(ca.id), 0);")?: array(); } /** * @param array $customerIds - * @param $maxCashId */ - private function updateCustomerInfos(array $customerIds, $maxCashId) + private function updateCustomerInfos(array $customerIds) { global $DB; - $params = $customerIds; - array_unshift($params, $maxCashId); - $DB->Execute("update billtech_customer_info set last_cash_id = ? where customer_id in (" . BillTech::repeatWithSeparator("?", ",", count($customerIds)) . ")", $params); + $DB->Execute("update billtech_customer_info bci + set last_cash_id = (select max(c.id) from cash c where c.customerid = bci.customer_id) + where customer_id in (".implode(',', $customerIds).");"); } /** From b64bd26a1d972103c97c7f0a61de52140865c516 Mon Sep 17 00:00:00 2001 From: Sebastian Miecielica Date: Tue, 1 Feb 2022 22:03:02 +0100 Subject: [PATCH 2/2] Add typing --- lib/BillTechLinksManager.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/BillTechLinksManager.php b/lib/BillTechLinksManager.php index 7588244..927a3ee 100644 --- a/lib/BillTechLinksManager.php +++ b/lib/BillTechLinksManager.php @@ -100,13 +100,21 @@ public function updateForAll() } } - private function checkIfCustomerCashIdExists($customerId, $cashId) - { + /** + * @param $customerId integer + * @param $cashId integer + * @return bool + */ + private function checkIfCustomerCashIdExists(integer $customerId, integer $cashId): bool + { global $DB; return $DB->GetOne('select count(*) from cash where customerid = ? and id = ?', array($customerId, $cashId)); } - public function updateCustomerBalance($customerId) + /** + * @var $customerId integer + */ + public function updateCustomerBalance(integer $customerId) { global $DB; $this->addMissingCustomerInfo(); @@ -423,8 +431,8 @@ private function updateCustomerInfos(array $customerIds) { global $DB; $DB->Execute("update billtech_customer_info bci - set last_cash_id = (select max(c.id) from cash c where c.customerid = bci.customer_id) - where customer_id in (".implode(',', $customerIds).");"); + set last_cash_id = (select max(c.id) from cash c where c.customerid = bci.customer_id) + where customer_id in (".implode(',', $customerIds).");"); } /**