Commit 5139e2da authored by Karamel's avatar Karamel
Browse files

Merge debt and prepay. Add the change in customer's balance in Ticket.

parent 2ed842b7
......@@ -497,9 +497,9 @@ if ($stmt->execute()) {
$customer->setCard(($row['CARD']===null)?'':$row['CARD']);
}
$customer->setMaxDebt($row['MAXDEBT']);
$customer->setCurrDebt(($row['CURDEBT']===null)?0.0:$row['CURDEBT']);
$customer->setDebtDate($row['CURDATE']);
$customer->setPrepaid($row['PREPAID']);
$debt = ($row['CURDEBT']===null)?0.0:$row['CURDEBT'];
$prepaid = $row['PREPAID'];
$customer->setBalance($prepaid - $debt);
$customer->setFirstName(($row['FIRSTNAME']===null)?'':$row['FIRSTNAME']);
$customer->setLastName(($row['LASTNAME']===null)?'':$row['LASTNAME']);
$customer->setEmail(($row['EMAIL']===null)?'':$row['EMAIL']);
......
......@@ -42,10 +42,10 @@ 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;
/** 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;
......@@ -53,10 +53,17 @@ class TicketAPI extends APIHelper implements API
if ($line->getProduct()->isPrepay()) {
// Get the amount of prepay. It is not affected
// by discounts nor taxes.
$refill += $line->getPrice();
$balance += $line->getPrice();
}
}
return round($refill, 5);
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) {
......@@ -123,36 +130,12 @@ class TicketAPI extends APIHelper implements API
// Check prepayment refill, use and debt.
if ($ticket->getCustomer() !== null) {
$customer = $ticket->getCustomer();
// Prepayment refill
$refill = $this->getPrepaymentRefill($ticket);
if ($refill > 0.005) {
$balance = $this->getBalanceUpdate($ticket);
if ($balance > 0.005 || $balance < -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);
}
$affectedCustomers[$customer->getId()]->addBalance($balance);
}
}
} // foreach ($data as $ticket) end
......
......@@ -35,7 +35,7 @@ class Customer extends DoctrineModel
{
protected function getDirectFieldNames() {
return ['id', 'dispName', 'card',
'maxDebt', 'currDebt', 'debtDate', 'prepaid',
'maxDebt', 'balance',
'firstName', 'lastName', 'email', 'phone1', 'phone2', 'fax',
'addr1', 'addr2', 'zipCode', 'city', 'region', 'country',
'note', 'visible', 'hasImage', 'expireDate'];
......@@ -243,27 +243,38 @@ class Customer extends DoctrineModel
}
/**
* 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.
* Debt/prepaid amount of the Customer. When negative, the customer is
* in debt. When positive, the customer has prepaid. Balance may be out
* of bounds because 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")
*/
protected $prepaid = 0.0;
public function getPrepaid() { return round($this->prepaid, 5); }
public function setPrepaid($prepaid) {
$this->prepaid = round($prepaid, 5);
protected $balance = 0.0;
public function getBalance() { return round($this->balance, 5); }
public function setBalance($balance) {
$this->balance = round($balance, 5);
}
public function addBalance($amount) {
$this->balance = round($this->getBalance() + round($amount, 5), 5);
}
public function addPrepaid($amount) {
$this->prepaid = round($this->getPrepaid() + round($amount, 5), 5);
$this->addBalance($amount);
}
public function removePrepaid($amount) {
$this->addPrepaid(-1*$amount);
$this->addBalance(-1*$amount);
}
public function addDebt($amount) {
$this->addBalance(-1*$amount);
}
public function recoverDebt($amount) {
$this->addBalance($amount);
}
/**
* MaxDebt Amount of the Customer
* MaxDebt Amount of the Customer. It is positive, the balance should
* not go beyond -maxDebt.
* @var float
* @SWG\Property(format="double")
* @Column(type="float")
......@@ -272,32 +283,6 @@ class Customer extends DoctrineModel
public function getMaxDebt() { return round($this->maxDebt, 5); }
public function setMaxDebt($max) { $this->maxDebt = round($max, 5); }
/**
* 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")
*/
protected $currDebt = 0.0;
public function getCurrDebt() { return round($this->currDebt, 5); }
public function setCurrDebt($currDebt) {
$this->currDebt = round($currDebt, 5);
}
public function addCurrDebt($amount) {
$this->currDebt = round($this->getCurrDebt() + round($amount, 5), 5);
}
/**
* @var string|null
* @SWG\Property(format="date-time")
* @Column(type="datetime", nullable=true)
*/
protected $debtDate;
public function getDebtDate() { return $this->debtDate; }
public function setDebtDate($debtDate) { $this->debtDate = $debtDate; }
/**
* Free private note.
* @var string|null
......@@ -346,7 +331,6 @@ 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;
}
......
......@@ -37,7 +37,8 @@ class Ticket extends DoctrineModel
{
public function getDirectFieldNames() {
return ['sequence', 'number', 'date', 'custCount', 'discountRate',
'price', 'totalAmount', 'discountPrice', 'discountTotal'];
'price', 'totalAmount', 'discountPrice', 'discountTotal',
'custBalance'];
}
public function getAssociationFields() {
return [
......@@ -333,6 +334,20 @@ public function getPayments() { return $this->payments; }
$this->discountTotal = round($discountTotal, 5);
}
/**
* Changes in the customer's balance.
* @var float
* @SWG\Property()
* @Column(type="float")
*/
protected $custBalance = 0.0;
public function getCustBalance() {
return round($this->custBalance, 5);
}
public function setCustBalance($custBalance) {
$this->custBalance = round($custBalance, 5);
}
public function toStruct() {
$struct = parent::toStruct();
$struct['date'] = DateUtils::toTimestamp($this->getDate());
......
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