Commit 93122177 authored by Karamel's avatar Karamel
Browse files

Add a script to recompute the customers' balance to retrofix CashsessionAPI->summary.

parent 3754927f
<?php
// Import script. Public Domain, CC-0, WTFPL or anything you want.
// Licensed under GPLv3 or any later by Pasteque.
namespace Pasteque\bin\fixes;
use \Pasteque\Server\AppContext;
use \Pasteque\Server\API\CashsessionAPI;
use \Pasteque\Server\Model\CashSession;
use \Pasteque\Server\Model\CashSessionCustBalance;
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");
}
$v8User = $argv[1];
$projectRoot = dirname(dirname(__DIR__));
require_once $projectRoot . '/vendor/autoload.php';
// Set v8
$cfgFile = $projectRoot . '/config/config.ini';
if (!is_readable($cfgFile)) {
// Check for a moved configuration
$envCfgFile = getenv('PT_CONFIG_' . preg_replace('/[^[:alnum:]]/', '_', $projectRoot));
if (($envCfgFile === false) || !is_readable($envCfgFile)) {
die('No config file found');
}
$cfgFile = $envCfgFile;
}
$ptApp = AppContext::loadFromConfig(parse_ini_file($cfgFile));
unset($cfgFile);
$db = $ptApp->getDbModule()->getDatabase($v8User);
if ($db === false) {
die(sprintf("V8 user %s not found\n", $v8User));
}
$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;
}
}
printf("Found %d sessions with customer balance.\n", count($sessionsToFix));
$api = new CashsessionAPI($dao);
$i = 1;
foreach ($sessionsToFix as $session) {
$summary = $api->summary($session);
$clean = true;
foreach ($summary->get('custBalances') as $fixedBalance) {
foreach ($session->getCustBalances() as $balance) {
if ($balance->getCustomer()->getId() == $fixedBalance->get('customer')) {
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'),
$balance->getCustomer()->getDispName(),
$session->getCashRegister()->getReference(),
$session->getSequence());
$balance->setBalance($fixedBalance->get('balance'));
$dao->write($session);
}
}
}
if ($clean) {
printf("%2d - Session %s-%d is coherent.\n",
$i, $session->getCashRegister()->getReference(),
$session->getSequence());
}
$i++;
}
}
$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