Commit 7bf8defc authored by Karamel's avatar Karamel
Browse files

Fix the customer's balance fixing script.

parent ba7c0143
......@@ -6,14 +6,17 @@ namespace Pasteque\bin\fixes;
use \Pasteque\Server\AppContext;
use \Pasteque\Server\API\CashsessionAPI;
use \Pasteque\Server\API\TicketAPI;
use \Pasteque\Server\Model\CashSession;
use \Pasteque\Server\Model\CashSessionCustBalance;
use \Pasteque\Server\Model\Customer;
use \Pasteque\Server\System\DAO\DAOCondition;
if ($argc < 2) {
die("recomputepre8.3custbalances.php <v8 user id>\n"
. " Recompute the customers' balance in Z tickets.\n"
. " CashsessionAPI->summary gave incorrect values before 8.3\n"
. " but it didn't affected the customer's balances.\n");
. " but it didn't affected the customer's balance.\n");
}
$v8User = $argv[1];
......@@ -42,45 +45,88 @@ $ptApp->login($ptApp->getIdentModule()->getUser($v8User));
$dao = $ptApp->getDao();
$sessionIds = [];
$sessionsToFix = [];
$custBalances = $dao->search(CashSessionCustBalance::class);
foreach ($custBalances as $custBal) {
$sess = $custBal->getCashSession();
if (!in_array($sess->getId(), $sessionIds)) {
$sessionIds[] = $sess->getId();
$sessionsToFix[] = $sess;
$sessionToFix = [];
$fixCount = 0;
$fixSessCount = 0;
/* Get all tickets where the customer's balance was changed
* and group them by session. */
$tktApi = new TicketAPI($dao);
$tkts = $tktApi->search(new DAOCondition('custBalance', '!=', 0.0), null, null, 'number');
foreach ($tkts as $tkt) {
$sessId = sprintf('%d-%d', $tkt->getCashRegister()->getId(), $tkt->getSequence());
if (!array_key_exists($sessId, $sessionToFix)) {
$sessionToFix[$sessId] = ['cashRegister' => $tkt->getCashRegister(),
'sequence' => $tkt->getSequence(), 'customers' => []];
$fixSessCount++;
}
$custId = $tkt->getCustomer()->getId();
if (!array_key_exists($tkt->getCustomer()->getId(),
$sessionToFix[$sessId]['customers'])) {
$sessionToFix[$sessId]['customers'][$tkt->getCustomer()->getId()] = true;
$fixCount++;
}
}
printf("Found %d sessions with customer balance.\n", count($sessionsToFix));
printf("Found %d balances within %d sessions.\n",
$fixCount, $fixSessCount);
if ($fixCount > 0 || $fixSessCount > 0) {
printf("Balance num, session num - operation\n------------------------------------\n");
}
$api = new CashsessionAPI($dao);
$i = 1;
foreach ($sessionsToFix as $session) {
$summary = $api->summary($session);
// Recompute the customer's balance for each session and fix it if required.
$sessionApi = new CashsessionAPI($dao);
$balanceIndex = 1;
$sessionIndex = 1;
foreach ($sessionToFix as $sessionInfo) {
$session = CashSession::load($sessionInfo, $dao);
$summary = $sessionApi->summary($session);
$clean = true;
foreach ($summary->get('custBalances') as $fixedBalance) {
$balanceFound = false;
foreach ($session->getCustBalances() as $balance) {
if ($balance->getCustomer()->getId() == $fixedBalance->get('customer')) {
$balanceFound = true;
if (abs($balance->getBalance() - $fixedBalance->get('balance')) > 0.005) {
$clean = false;
printf("%2d - Fixing balance from %.2f to %.2f for %s, session %s-%d\n",
$i, $balance->getBalance(), $fixedBalance->get('balance'),
printf("%3d, %3d - Fixing balance from %.2f to %.2f for %s, session %s-%d\n",
$balanceIndex, $sessionIndex,
$balance->getBalance(), $fixedBalance->get('balance'),
$balance->getCustomer()->getDispName(),
$session->getCashRegister()->getReference(),
$session->getSequence());
$balance->setBalance($fixedBalance->get('balance'));
$dao->write($session);
} else {
printf("%3d, %3d - Balance %.2f for %s is coherent, session %s-%d\n",
$balanceIndex, $sessionIndex,
$balance->getBalance(), $balance->getCustomer()->getDispName(),
$session->getCashRegister()->getReference(),
$session->getSequence());
}
break;
}
}
if ($clean) {
printf("%2d - Session %s-%d is coherent.\n",
$i, $session->getCashRegister()->getReference(),
$session->getSequence());
if (!$balanceFound) {
$clean = false;
$custBalance = new CashSessionCustBalance();
$customer = $dao->read(Customer::class, $fixedBalance->get('customer'));
$custBalance->setCustomer($customer);
$custBalance->setBalance($fixedBalance->get('balance'));
printf("%3d, %3d - Adding balance %.2f for %s, session %s-%d\n",
$balanceIndex, $sessionIndex,
$fixedBalance->get('balance'), $customer->getDispName(),
$session->getCashRegister()->getReference(),
$session->getSequence());
$session->addCustBalances($custBalance);
}
$i++;
$balanceIndex++;
}
$sessionIndex++;
if (!$clean) {
$dao->write($session);
}
}
$dao->commit();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment