Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zmiana wartości last_cash_id dla billtech_customer_info #82

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions lib/BillTechLinksManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public function getBalanceLink($customerId, $params)

public function updateForAll()
{
global $DB;
$actions = array(
'add' => array(),
'update' => array(),
Expand All @@ -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));
Expand All @@ -99,11 +96,25 @@ public function updateForAll()
}

if ($this->performActions($actions)) {
$this->updateCustomerInfos($customerIds, $maxCashId);
$this->updateCustomerInfos($customerIds);
}
}

public function updateCustomerBalance($customerId)
/**
* @param $customerId integer
* @param $cashId integer
* @return bool
*/
private function checkIfCustomerCashIdExists(integer $customerId, integer $cashId): bool
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ta funkcja nie zwraca bool. Dodałbym castowanie albo poprawił sygnaturę.

global $DB;
return $DB->GetOne('select count(*) from cash where customerid = ? and id = ?', array($customerId, $cashId));
}

/**
* @var $customerId integer
*/
public function updateCustomerBalance(integer $customerId)
{
global $DB;
$this->addMissingCustomerInfo();
Expand All @@ -113,7 +124,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']))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trzeba trochę poprawiać ten kod. Dałbym te warunki pod funkcje pomocnicze o odpowiednich nazwach.

$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));
Expand Down Expand Up @@ -410,19 +421,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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Postarałbym się oddzielić SQL od PHP. Chociaż poprzez formatowanie.

}

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

/**
Expand Down