Commit d094dd96 authored by Karamel's avatar Karamel
Browse files

Fix updating customer's balance on ticket save. Add the API test.

parent 6450c151
......@@ -42,30 +42,6 @@ class TicketAPI extends APIHelper implements API
return count($search) == 0;
}
/** Get the amount of prepayment refill and debts if any.
* @return The balance update amount (positive for refill). */
private function getBalanceUpdate($ticket) {
$balance = 0.0;
foreach ($ticket->getLines() as $line) {
if ($line->getProduct() === null) {
continue;
}
if ($line->getProduct()->isPrepay()) {
// Get the amount of prepay. It is not affected
// by discounts nor taxes.
$balance += $line->getPrice();
}
}
foreach ($ticket->getPayments() as $payment) {
$pm = $payment->getPaymentMode();
// Prepayment
if ($pm->usesPrepay() || $pm->usesDebt()) {
$balance -= $payment->getAmount();
}
}
return round($balance, 5);
}
public function write($data) {
$this->supportOrDie($data);
$data = (is_array($data)) ? $data : array($data);
......@@ -135,7 +111,7 @@ class TicketAPI extends APIHelper implements API
// Check prepayment refill, use and debt.
if ($ticket->getCustomer() !== null) {
$customer = $ticket->getCustomer();
$balance = $this->getBalanceUpdate($ticket);
$balance = $ticket->getCustBalance();
if ($balance > 0.005 || $balance < -0.005) {
if (empty($affectedCustomers[$customer->getId()])) {
$affectedCustomers[$customer->getId()] = $customer;
......
......@@ -425,6 +425,43 @@ class TicketAPITest extends TestCase
}
/** @depends testSaveTicket */
public function testCustBalanceTicket() {
$cust = new Customer();
$cust->setDispName('Customer');
$this->dao->write($cust);
$this->dao->commit();
$tkt = new Ticket();
$tkt->setCashRegister($this->session->getCashRegister());
$tkt->setSequence($this->session->getSequence());
$tkt->setNumber(1);
$tkt->setDate(new \DateTime('2018-01-01 8:05'));
$tkt->setUser($this->user);
$tkt->setTaxedPrice(11.0);
$tkt->setFinalPrice(10.0); $tkt->setFinalTaxedPrice(11.0);
$tkt->setCustomer($cust);
$tkt->setCustBalance(10.0);
$this->api->write($tkt);
$readCust = $this->dao->readSnapshot(Customer::class, $cust->getId());
$this->assertNotNull($readCust);
$this->assertEquals(10.0, $readCust->getBalance());
// Redo to check addition
$tkt2 = new Ticket();
$tkt2->setCashRegister($this->session->getCashRegister());
$tkt2->setSequence($this->session->getSequence());
$tkt2->setNumber(2);
$tkt2->setDate(new \DateTime('2018-01-01 8:05'));
$tkt2->setUser($this->user);
$tkt2->setTaxedPrice(5.5);
$tkt2->setFinalPrice(5.0); $tkt2->setFinalTaxedPrice(11.0);
$tkt2->setCustomer($cust);
$tkt2->setCustBalance(5.0);
$this->api->write($tkt2);
$readCust = $this->dao->readSnapshot(Customer::class, $cust->getId());
$this->assertNotNull($readCust);
$this->assertEquals(15.0, $readCust->getBalance());
}
/** @depends testSaveTicket */
public function testTopCustomers() {
$cTop = new Customer();
......
Supports Markdown
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