Commit fc73a2b0 authored by Karamel's avatar Karamel
Browse files

Add period and fiscal year sum in CashSessionTax. Update and add tests.

parent 6e3ea035
......@@ -436,6 +436,25 @@ class CashSession extends DoctrineModel
* to report and reset the right sums. */
public function initSums($prevCashSession) {
if (!$prevCashSession->isClosed()) { return; }
// Copy taxes sums
if ($prevCashSession->closeType != static::CLOSE_FYEAR) {
foreach ($prevCashSession->getTaxes() as $tax) {
$newTax = new CashSessionTax();
$newTax->setTax($tax->getTax());
$newTax->setTaxRate($tax->getTaxRate());
switch ($prevCashSession->closeType) {
case static::CLOSE_SIMPLE: // Keep period
$newTax->setBasePeriod($tax->getBasePeriod());
$newTax->setAmountPeriod($tax->getAmountPeriod());
// nobreak;
case static::CLOSE_PERIOD: // Keep fiscal year
$newTax->setBaseFYear($tax->getBaseFYear());
$newTax->setAmountFYear($tax->getAmountFYear());
}
$this->addTax($newTax);
}
}
// Copy CS sums
switch ($prevCashSession->closeType) {
case static::CLOSE_SIMPLE:
// Keep period
......@@ -479,11 +498,33 @@ class CashSession extends DoctrineModel
public static function fromStruct($struct, $dao, $embedded = false) {
// Prevent updating sums when the session is not closed.
$structTaxes = null;
if (empty($struct['closeDate'])) {
unset($struct['csPeriod']);
unset($struct['csFYear']);
unset($struct['taxes']);
} else {
// Save and unset taxes to merge data manually
$structTaxes = $struct['taxes'];
unset($struct['taxes']);
}
$model = parent::fromStruct($struct, $dao, $embedded);
// Now that the session id is retreived, we can load the taxes
if ($structTaxes !== null && !empty($structTaxes)) {
foreach ($structTaxes as $sTax) {
$merged = false;
foreach ($model->getTaxes() as $mTax) {
if ($sTax['tax'] == $mTax->getTax()->getId()) {
$mTax->merge($sTax, $dao);
$merged = true;
}
}
if (!$merged) {
$tax = CashSessionTax::fromStruct($sTax, $dao);
$model->addTax($tax);
}
}
}
// Load closeType if set
if (!empty($struct['closeType'])) {
$model->closeType = $struct['closeType'];
......
......@@ -37,7 +37,9 @@ use \Pasteque\Server\System\DAO\DoctrineModel;
class CashSessionTax extends DoctrineModel // Embedded class
{
public function getDirectFieldNames() {
return ['sequence', 'base', 'taxRate', 'amount'];
return ['taxRate',
'base', 'basePeriod', 'baseFYear',
'amount', 'amountPeriod', 'amountFYear'];
}
public function getAssociationFields() {
return [
......@@ -100,24 +102,84 @@ class CashSessionTax extends DoctrineModel // Embedded class
/**
* @var float
* @SWG\Property()
* @Column(type="float")
* @Column(type="float", nullable=false)
*/
protected $base;
protected $base = 0.0;
public function getBase() { return round($this->base, 5); }
public function setBase($base) {
$this->base = round($base, 5);
}
/**
* Tax base total by period.
* @var float
* @SWG\Property(format="double", nullable=false)
* @Column(type="float", nullable=false)
*/
protected $basePeriod = 0.0;
public function getBasePeriod() {
return round($this->basePeriod, 5);
}
public function setBasePeriod($basePeriod) {
$this->basePeriod = round($basePeriod, 5);
}
/**
* Tax base total by fiscal year.
* @var float
* @SWG\Property(format="double", nullable=false)
* @Column(type="float", nullable=false)
*/
protected $baseFYear = 0.0;
public function getBaseFYear() {
return round($this->baseFYear, 5);
}
public function setBaseFYear($baseFYear) {
$this->baseFYear = round($baseFYear, 5);
}
/**
* Total amount of tax.
* @var float
* @SWG\Property()
* @Column(type="float")
* @Column(type="float", nullable=false)
*/
protected $amount;
protected $amount = 0.0;
public function getAmount() { return round($this->amount, 5); }
public function setAmount($amount) {
$this->amount = round($amount, 5);
}
/**
* Tax amount total by period.
* @var float
* @SWG\Property(format="double", nullable=false)
* @Column(type="float", nullable=false)
*/
protected $amountPeriod = 0.0;
public function getAmountPeriod() {
return round($this->amountPeriod, 5);
}
public function setAmountPeriod($amountPeriod) {
$this->amountPeriod = round($amountPeriod, 5);
}
/**
* Tax amount total by fiscal year.
* @var float
* @SWG\Property(format="double", nullable=false)
* @Column(type="float", nullable=false)
*/
protected $amountFYear = 0.0;
public function getAmountFYear() {
return round($this->amountFYear, 5);
}
public function setAmountFYear($amountFYear) {
$this->amountFYear = round($amountFYear, 5);
}
/** Merge data from a struct (without cashSession set) into this record. */
public function merge($struct, $dao) {
$this->directFieldsFromStruct($struct, $dao);
}
}
......@@ -24,7 +24,9 @@ namespace Pasteque\Server;
use \Pasteque\Server\API\CashsessionAPI;
use \Pasteque\Server\Model\CashRegister;
use \Pasteque\Server\Model\CashSession;
use \Pasteque\Server\Model\CashSessionTax;
use \Pasteque\Server\Model\FiscalTicket;
use \Pasteque\Server\Model\Tax;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\DAO\DAOFactory;
use \PHPUnit\Framework\TestCase;
......@@ -45,6 +47,7 @@ class CashsessionAPITest extends TestCase
{
private $dao;
private $api;
private $tax;
protected function setUp() {
global $dbInfo;
......@@ -54,19 +57,22 @@ class CashsessionAPITest extends TestCase
$this->cashRegister->setReference('register');
$this->cashRegister->setLabel('Register');
$this->dao->write($this->cashRegister);
$this->tax = new Tax();
$this->tax->setLabel('tax');
$this->tax->setRate(0.1);
$this->dao->write($this->tax);
$this->dao->commit();
}
protected function tearDown() {
$taxes = $this->dao->search(CashSessionTax::class);
foreach ($taxes as $tax) { $this->dao->delete($tax); }
$all = $this->dao->search(CashSession::class);
foreach($all as $record) {
$this->dao->delete($record);
}
foreach($all as $record) { $this->dao->delete($record); }
$ftkt = $this->dao->search(FiscalTicket::class);
foreach($ftkt as $tkt) {
$this->dao->delete($tkt);
}
foreach($ftkt as $tkt) { $this->dao->delete($tkt); }
$this->dao->delete($this->cashRegister);
$this->dao->delete($this->tax);
$this->dao->commit();
$this->dao->close();
}
......@@ -161,6 +167,22 @@ class CashsessionAPITest extends TestCase
$session->setCS($cs);
$session->setCSPeriod($session->getCSPeriod() + $cs);
$session->setCSFYear($session->getCSFYear() + $cs);
$tax = null;
if (count($session->getTaxes()) > 0) {
$tax = $session->getTaxes()->get(0);
} else {
$tax = new CashSessionTax();
$tax->setTax($this->tax);
$tax->setTaxRate($this->tax->getRate());
$session->addTax($tax);
}
$amount = $cs * $this->tax->getRate();
$tax->setBase($cs);
$tax->setAmount($amount);
$tax->setBasePeriod($tax->getBasePeriod() + $cs);
$tax->setAmountPeriod($tax->getAmountPeriod() + $amount);
$tax->setBaseFYear($tax->getBaseFYear() + $cs);
$tax->setAmountFYear($tax->getAmountFYear() + $amount);
if ($closeType !== null) { $session->setCloseType($closeType); }
$this->api->write($session);
}
......@@ -175,22 +197,54 @@ class CashsessionAPITest extends TestCase
$this->assertEquals(10.0, $session->getCS());
$this->assertEquals(10.0, $session->getCSPeriod());
$this->assertEquals(10.0, $session->getCSFYear());
$this->assertEquals(1, count($session->getTaxes()));
$tax = $session->getTaxes()->get(0);
$this->assertEquals(10.0, $tax->getBase());
$this->assertEquals(1.0, $tax->getAmount());
$this->assertEquals(10.0, $tax->getBasePeriod());
$this->assertEquals(1.0, $tax->getAmountPeriod());
$this->assertEquals(10.0, $tax->getBaseFYear());
$this->assertEquals(1.0, $tax->getAmountFYear());
// Check next session
$next = $this->api->get($this->cashRegister->getId());
$this->assertNotNull($next);
$this->assertNull($next->getCS());
$this->assertEquals(10.0, $next->getCSPeriod());
$this->assertEquals(10.0, $next->getCSFYear());
$this->assertEquals(1, count($next->getTaxes()));
$tax = $next->getTaxes()->get(0);
$this->assertEquals(0.0, $tax->getBase());
$this->assertEquals(0.0, $tax->getAmount());
$this->assertEquals(10.0, $tax->getBasePeriod());
$this->assertEquals(1.0, $tax->getAmountPeriod());
$this->assertEquals(10.0, $tax->getBaseFYear());
$this->assertEquals(1.0, $tax->getAmountFYear());
// Redo to check sum
$this->openAndClose($next, 20.0);
$this->assertEquals(20.0, $next->getCS());
$this->assertEquals(30.0, $next->getCSPeriod());
$this->assertEquals(30.0, $next->getCSFYear());
$this->assertEquals(1, count($next->getTaxes()));
$tax = $next->getTaxes()->get(0);
$this->assertEquals(20.0, $tax->getBase());
$this->assertEquals(2.0, $tax->getAmount());
$this->assertEquals(30.0, $tax->getBasePeriod());
$this->assertEquals(3.0, $tax->getAmountPeriod());
$this->assertEquals(30.0, $tax->getBaseFYear());
$this->assertEquals(3.0, $tax->getAmountFYear());
$next2 = $this->api->get($this->cashRegister->getId());
$this->assertNotNull($next2);
$this->assertNull($next2->getCS());
$this->assertEquals(30.0, $next2->getCSPeriod());
$this->assertEquals(30.0, $next2->getCSFYear());
$this->assertEquals(1, count($next2->getTaxes()));
$tax = $next2->getTaxes()->get(0);
$this->assertEquals(0.0, $tax->getBase());
$this->assertEquals(0.0, $tax->getAmount());
$this->assertEquals(30.0, $tax->getBasePeriod());
$this->assertEquals(3.0, $tax->getAmountPeriod());
$this->assertEquals(30.0, $tax->getBaseFYear());
$this->assertEquals(3.0, $tax->getAmountFYear());
}
/** @depends testNextSessionSimple */
......@@ -202,22 +256,54 @@ class CashsessionAPITest extends TestCase
$this->assertEquals(10.0, $session->getCS());
$this->assertEquals(10.0, $session->getCSPeriod());
$this->assertEquals(10.0, $session->getCSFYear());
$this->assertEquals(1, count($session->getTaxes()));
$tax = $session->getTaxes()->get(0);
$this->assertEquals(10.0, $tax->getBase());
$this->assertEquals(1.0, $tax->getAmount());
$this->assertEquals(10.0, $tax->getBasePeriod());
$this->assertEquals(1.0, $tax->getAmountPeriod());
$this->assertEquals(10.0, $tax->getBaseFYear());
$this->assertEquals(1.0, $tax->getAmountFYear());
// Check next session
$next = $this->api->get($this->cashRegister->getId());
$this->assertNotNull($next);
$this->assertNull($next->getCS());
$this->assertEquals(0.0, $next->getCSPeriod());
$this->assertEquals(10.0, $next->getCSFYear());
$this->assertEquals(1, count($next->getTaxes()));
$tax = $next->getTaxes()->get(0);
$this->assertEquals(0.0, $tax->getBase());
$this->assertEquals(0.0, $tax->getAmount());
$this->assertEquals(0.0, $tax->getBasePeriod());
$this->assertEquals(0.0, $tax->getAmountPeriod());
$this->assertEquals(10.0, $tax->getBaseFYear());
$this->assertEquals(1.0, $tax->getAmountFYear());
// Redo to check sum
$this->openAndClose($next, 20.0, CashSession::CLOSE_PERIOD);
$this->assertEquals(20.0, $next->getCS());
$this->assertEquals(20.0, $next->getCSPeriod());
$this->assertEquals(30.0, $next->getCSFYear());
$this->assertEquals(1, count($next->getTaxes()));
$tax = $next->getTaxes()->get(0);
$this->assertEquals(20.0, $tax->getBase());
$this->assertEquals(2.0, $tax->getAmount());
$this->assertEquals(20.0, $tax->getBasePeriod());
$this->assertEquals(2.0, $tax->getAmountPeriod());
$this->assertEquals(30.0, $tax->getBaseFYear());
$this->assertEquals(3.0, $tax->getAmountFYear());
$next2 = $this->api->get($this->cashRegister->getId());
$this->assertNotNull($next2);
$this->assertNull($next2->getCS());
$this->assertEquals(0.0, $next2->getCSPeriod());
$this->assertEquals(30.0, $next2->getCSFYear());
$this->assertEquals(1, count($next2->getTaxes()));
$tax = $next2->getTaxes()->get(0);
$this->assertEquals(0.0, $tax->getBase());
$this->assertEquals(0.0, $tax->getAmount());
$this->assertEquals(0.0, $tax->getBasePeriod());
$this->assertEquals(0.0, $tax->getAmountPeriod());
$this->assertEquals(30.0, $tax->getBaseFYear());
$this->assertEquals(3.0, $tax->getAmountFYear());
}
/** @depends testNextSessionSimple */
......@@ -229,12 +315,21 @@ class CashsessionAPITest extends TestCase
$this->assertEquals(10.0, $session->getCS());
$this->assertEquals(10.0, $session->getCSPeriod());
$this->assertEquals(10.0, $session->getCSFYear());
$this->assertEquals(1, count($session->getTaxes()));
$tax = $session->getTaxes()->get(0);
$this->assertEquals(10.0, $tax->getBase());
$this->assertEquals(1.0, $tax->getAmount());
$this->assertEquals(10.0, $tax->getBasePeriod());
$this->assertEquals(1.0, $tax->getAmountPeriod());
$this->assertEquals(10.0, $tax->getBaseFYear());
$this->assertEquals(1.0, $tax->getAmountFYear());
// Check next session
$next = $this->api->get($this->cashRegister->getId());
$this->assertNotNull($next);
$this->assertEquals(0.0, $next->getCS());
$this->assertEquals(0.0, $next->getCSPeriod());
$this->assertEquals(0.0, $next->getCSFYear());
$this->assertEquals(0, count($next->getTaxes()));
}
/** @depends testWrite
......
......@@ -26,6 +26,7 @@ use \Pasteque\Server\Model\CashSession;
use \Pasteque\Server\Model\CashSessionCat;
use \Pasteque\Server\Model\CashSessionCustBalance;
use \Pasteque\Server\Model\CashSessionTax;
use \Pasteque\Server\Model\Tax;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\DAO\DAOFactory;
use \PHPUnit\Framework\TestCase;
......@@ -42,6 +43,14 @@ class CashSessionTest extends TestCase
$this->cashRegister->setReference('cash');
$this->cashRegister->setLabel('Cash');
$this->dao->write($this->cashRegister);
$this->tax = new Tax();
$this->tax->setLabel('tax');
$this->tax->setRate(0.1);
$this->tax2 = new Tax();
$this->tax2->setLabel('tax2');
$this->tax2->setRate(0.2);
$this->dao->write($this->tax);
$this->dao->write($this->tax2);
$this->dao->commit();
}
......@@ -54,6 +63,8 @@ class CashSessionTest extends TestCase
}
}
$this->dao->delete($this->cashRegister);
$this->dao->delete($this->tax);
$this->dao->delete($this->tax2);
$this->dao->commit();
$this->dao->close();
}
......@@ -331,15 +342,88 @@ class CashSessionTest extends TestCase
$session = $this->initEmptySession();
$session->setCSPeriod(10.0);
$session->setCSFYear(20.0);
$tax = new CashSessionTax();
$tax->setTax($this->tax);
$tax->setTaxRate($this->tax->getRate());
$tax->setBase(10.0);
$tax->setAmount(1.0);
$tax->setBasePeriod(20.0);
$tax->setAmountPeriod(2.0);
$tax->setBaseFYear(30.0);
$tax->setAmountFYear(3.0);
$session->addTax($tax);
$this->dao->write($session);
$this->dao->commit();
$struct = $this->emptyStruct();
$struct['csPeriod'] = 15.0;
$struct['csFYear'] = 25.0;
$struct['taxes'] = [['tax' => $this->tax->getId(),
'base' => 15.0, 'amount' => 1.5,
'basePeriod' => 25, 'amountPeriod' => 2.5,
'baseFYear' => 35, 'amountFYear' => 3.5]];
$session = CashSession::fromStruct($struct, $this->dao);
$this->assertEquals(10.0, $session->getCSPeriod());
$this->assertEquals(20.0, $session->getCSFYear());
$this->assertEquals(1, count($session->getTaxes()));
$taxLine = $session->getTaxes()->get(0);
$this->assertEquals(10.0, $taxLine->getBase());
$this->assertEquals(1.0, $taxLine->getAmount());
$this->assertEquals(20, $taxLine->getBasePeriod());
$this->assertEquals(2.0, $taxLine->getAmountPeriod());
$this->assertEquals(30.0, $taxLine->getBaseFYear());
$this->assertEquals(3.0, $taxLine->getAmountFYear());
}
public function testFromStructMergeSums() {
$session = $this->initEmptySession();
$session->setCSPeriod(10.0);
$session->setCSFYear(20.0);
$tax = new CashSessionTax();
$tax->setTax($this->tax);
$tax->setTaxRate($this->tax->getRate());
$tax->setBase(10.0);
$tax->setAmount(1.0);
$tax->setBasePeriod(20.0);
$tax->setAmountPeriod(2.0);
$tax->setBaseFYear(30.0);
$tax->setAmountFYear(3.0);
$session->addTax($tax);
$this->dao->write($session);
$this->dao->commit();
$struct = $this->emptyStruct();
$struct['csPeriod'] = 20.0;
$struct['csFYear'] = 30.0;
$struct['closeDate'] = DateUtils::toTimestamp(new \DateTime());
$struct['taxes'] = [['tax' => $this->tax->getId(),
'base' => 15.0, 'amount' => 1.5,
'basePeriod' => 25, 'amountPeriod' => 2.5,
'baseFYear' => 35, 'amountFYear' => 3.5],
['tax' => $this->tax2->getId(),
'base' => 5.0, 'amount' => 1.0,
'basePeriod' => 6.0, 'amountPeriod' => 1.2,
'baseFYear' => 7.0, 'amountFYear' => 1.4]];
$session = CashSession::fromStruct($struct, $this->dao);
$this->assertEquals(20.0, $session->getCSPeriod());
$this->assertEquals(30.0, $session->getCSFYear());
$this->assertEquals(2, count($session->getTaxes()));
$taxLine = $session->getTaxes()->get(0);
$this->assertEquals($this->tax->getId(), $taxLine->getTax()->getId());
$this->assertEquals(15.0, $taxLine->getBase());
$this->assertEquals(1.5, $taxLine->getAmount());
$this->assertEquals(25, $taxLine->getBasePeriod());
$this->assertEquals(2.5, $taxLine->getAmountPeriod());
$this->assertEquals(35.0, $taxLine->getBaseFYear());
$this->assertEquals(3.5, $taxLine->getAmountFYear());
$taxLine2 = $session->getTaxes()->get(1);
$this->assertEquals($this->tax2->getId(), $taxLine2->getTax()->getId());
$this->assertEquals(5.0, $taxLine2->getBase());
$this->assertEquals(1.0, $taxLine2->getAmount());
$this->assertEquals(6, $taxLine2->getBasePeriod());
$this->assertEquals(1.2, $taxLine2->getAmountPeriod());
$this->assertEquals(7.0, $taxLine2->getBaseFYear());
$this->assertEquals(1.4, $taxLine2->getAmountFYear());
}
public function testToStructDates() {
$session = $this->initEmptySession();
$struct = $session->toStruct();
......
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