Commit 2243a414 authored by Karamel's avatar Karamel
Browse files

Restore prepay and debt. Fix nextTicketId increment on ticket save.

parent c3c25bfb
......@@ -42,11 +42,29 @@ class TicketAPI extends APIHelper implements API
return count($search) == 0;
}
/** Get the amount of prepayment refill if any.
* @return The refill amount. */
private function getPrepaymentRefill($ticket) {
$refill = 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.
$refill += $line->getPrice();
}
}
return round($refill, 5);
}
public function write($data) {
$this->supportOrDie($data);
$data = (is_array($data)) ? $data : array($data);
$previousFTicket = null;
$affectedCashRegisters = [];
$affectedCustomers = [];
foreach ($data as $ticket) {
// Defensive checks
// Reserved number 0
......@@ -106,8 +124,48 @@ class TicketAPI extends APIHelper implements API
if (!isset($affectedCashRegisters[$cashReg->getId()])) {
$affectedCashRegisters[$cashReg->getId()] = $cashReg;
}
}
// Check prepayment refill, use and debt.
if ($ticket->getCustomer() !== null) {
$customer = $ticket->getCustomer();
// Prepayment refill
$refill = $this->getPrepaymentRefill($ticket);
if ($refill > 0.005) {
if (empty($affectedCustomers[$customer->getId()])) {
$affectedCustomers[$customer->getId()] = $customer;
}
$affectedCustomers[$customer->getId()]->addPrepaid($refill);
}
// Prepayment use and debt
// There is no check about maxDebt and positive prepayment.
// Those one are done client-side, maybe with conflicts
// but the transactions are already done.
foreach ($ticket->getPayments() as $payment) {
$pm = $payment->getPaymentMode();
// Prepayment
if ($pm->usesPrepay()) {
if (empty($affectedCustomers[$customer->getId()])) {
$affectedCustomers[$customer->getId()] = $customer;
}
$affectedCustomers[$customer->getId()]->removePrepaid($payment->getAmount());
}
// Debt
if ($pm->usesDebt()) {
if (empty($affectedCustomers[$customer->getId()])) {
$affectedCustomers[$customer->getId()] = $customer;
}
$affectedCustomers[$customer->getId()]->addCurrDebt($payment->getAmount());
$now = new \DateTime();
$affectedCustomers[$customer->getId()]->setDebtDate($now);
}
}
}
} // foreach ($data as $ticket) end
$this->updateEOSTicket($previousFTicket);
// Update customers
foreach ($affectedCustomers as $id => $customer) {
$this->dao->write($customer);
}
$this->dao->commit();
// Update nextTicketId
foreach ($affectedCashRegisters as $id => $cashRegister) {
$search = $this->dao->search(Ticket::class,
......
......@@ -243,7 +243,9 @@ class Customer extends DoctrineModel
}
/**
* Prepaid Amount of the Customer
* Prepaid Amount of the Customer. It can be negative because the
* transactions are checked on client side and if it passed,
* the transaction was validated and must be acounted.
* @var float
* @SWG\Property(format="double")
* @Column(type="float")
......@@ -271,7 +273,9 @@ class Customer extends DoctrineModel
public function setMaxDebt($max) { $this->maxDebt = round($max, 5); }
/**
* CurrDebt Amount of the Customer
* CurrDebt Amount of the Customer. It can be negative or more than
* maxDebt because the transactions are checked on client side and
* if it passed, the transaction was validated and must be acounted.
* @var float
* @SWG\Property(format="double")
* @Column(type="float")
......@@ -342,6 +346,7 @@ class Customer extends DoctrineModel
$struct['number'] = $this->getId();
$struct['key'] = sprintf('%d-%s', $this->getId(), $this->getDispName());
$struct['expireDate'] = DateUtils::toTimestamp($this->getExpireDate());
$struct['debtDate'] = DateUtils::toTimestamp($this->getDebtDate());
return $struct;
}
......
......@@ -124,6 +124,13 @@ class PaymentMode extends DoctrineModel
public function getType() { return $this->type; }
public function setType($type) { $this->type = $type; }
public function usesDebt() {
return ($this->type & static::CUST_DEBT) > 0;
}
public function usesPrepay() {
return ($this->type & static::CUST_PREPAID) > 0;
}
/**
* @OneToMany(targetEntity="\Pasteque\Server\Model\PaymentModeValue", mappedBy="paymentMode", cascade={"persist"}, orphanRemoval=true) */
protected $values;
......
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