Commit ea314451 authored by Karamel's avatar Karamel
Browse files

More fixes for the new embedded objects. Cash session tests and some test fixes.

parent 7962106c
......@@ -352,6 +352,10 @@ class CashSession extends DoctrineModel
$this->taxes->add($tax);
$tax->setCashSession($this);
}
public function removeTax($tax) {
$this->taxes->removeElement($tax);
$tax->setCashSession(null);
}
/**
* Array of total amount of payments by mode.
......@@ -374,6 +378,11 @@ class CashSession extends DoctrineModel
$this->payments->add($payment);
$payment->setCashSession($this);
}
public function removePayment($payment) {
$this->payments->removeElement($payment);
$payment->setCashSession(null);
}
/**
* Array of total amount of cs by category.
......@@ -396,6 +405,11 @@ class CashSession extends DoctrineModel
$this->catSales->add($cat);
$cat->setCashSession($this);
}
public function removeCatSales($cat) {
$this->catSales->removeElement($tax);
$cat->setCashSession(null);
}
/**
* Array of tax totals by category.
......@@ -418,6 +432,11 @@ class CashSession extends DoctrineModel
$this->catTaxes->add($catTax);
$catTax->setCashSession($this);
}
public function removeCatTax($catTax) {
$this->catTaxes->removeElement($catTax);
$catTax->setCashSession(null);
}
/**
* Array of total balance change by customer.
......@@ -440,6 +459,11 @@ class CashSession extends DoctrineModel
$this->custBalances->add($custBalance);
$custBalance->setCashSession($this);
}
public function removeCustBalance($custBalance) {
$this->custBalances->removeElement($custBalance);
$custBalance->setCashSession(null);
}
/** Operation flag for closing. See constants.
* It is not exported and used only when registering a closed session. */
......
......@@ -56,7 +56,7 @@ class CashSessionCat extends DoctrineModel // Embedded class
public function getId() {
if ($this->getCashSession() === null) {
return ['cashSession' => null, 'reference' => $this->getReference];
return ['cashSession' => null, 'reference' => $this->getReference()];
} else {
return ['cashSession' => $this->getCashSession()->getId(),
'reference' => $this->getReference()];
......
......@@ -64,7 +64,7 @@ class UserTest extends TestCase
$user->setPassword(sprintf('sha1:%s', $hash));
$this->assertTrue($user->authenticate($pwd));
$this->assertFalse($user->authenticate($hash));
$this->assertTrue($user->authenticate(sprintf('sha1:%s', $hash));
$this->assertTrue($user->authenticate(sprintf('sha1:%s', $hash)));
$this->assertFalse($user->authenticate(null));
$this->assertFalse($user->authenticate(''));
}
......@@ -74,7 +74,7 @@ class UserTest extends TestCase
$user = new User();
$user->setPassword(sprintf('plain:%s', $pwd));
$this->assertTrue($user->authenticate($pwd));
$this->assertTrue($user->authenticate(sprintf('plain:%s', $pwd));
$this->assertTrue($user->authenticate(sprintf('plain:%s', $pwd)));
$this->assertFalse($user->authenticate('PafPasteque'));
$this->assertFalse($user->authenticate(null));
$this->assertFalse($user->authenticate(''));
......@@ -90,4 +90,4 @@ class UserTest extends TestCase
$this->assertFalse($user->authenticate(''));
}
}
\ No newline at end of file
}
<?php
// Pastèque Web back office
//
// Copyright (C) 2013 Scil (http://scil.coop)
//
// This file is part of Pastèque.
//
// Pastèque is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Pastèque is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Pastèque. If not, see <http://www.gnu.org/licenses/>.
// Some utility functions to test tickets.
use \Pasteque\Server\Model\Session;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\DAO\DAOCondition;
/** Check equality of a ticket model and a ticket structure.i
* Should be used with a model and a dao snapshot to prevent mixing references.
* @param $ut Unit Test ($this in test classes). */
function assertSessionModelEqModel($m1, $m2, $ut) {
$ut->assertEquals($m1->getCashRegister()->getId(), $m2->getCashRegister()->getId());
$ut->assertEquals($m1->getSequence(), $m2->getSequence());
$ut->assertTrue(DateUtils::equals($m1->getOpenDate(), $m2->getOpenDate()));
$ut->assertTrue(DateUtils::equals($m1->getCloseDate(), $m2->getCloseDate()));
$ut->assertEquals($m1->getOpenCash(), $m2->getOpenCash());
$ut->assertEquals($m1->getCloseCash(), $m2->getCloseCash());
$ut->assertEquals($m1->getExpectedCash(), $m2->getExpectedCash());
$ut->assertEquals($m1->getTicketCount(), $m2->getTicketCount());
$ut->assertEquals($m1->getCustCount(), $m2->getCustCount());
$ut->assertEquals($m1->getCs(), $m2->getCs());
$ut->assertEquals($m1->getCsPeriod(), $m2->getCsPeriod());
$ut->assertEquals($m1->getCsFYear(), $m2->getCsFYear());
// Check payments
$ut->assertEquals(count($m1->getPayments()), count($m2->getPayments()));
for ($i = 0; $i < count($m1->getPayments()); $i++) {
$m1Pmt = $m1->getPayments()->get($i);
$m2Pmt = $m2->getPayments()->get($i);
$ut->assertEquals($m1Pmt->getAmount(), $m2Pmt->getAmount());
$ut->assertEquals($m1Pmt->getCurrencyAmount(), $m2Pmt->getCurrencyAmount());
$ut->assertEquals($m1Pmt->getPaymentMode()->getId(), $m2Pmt->getPaymentMode()->getId());
$ut->assertEquals($m1Pmt->getCurrency()->getId(), $m2Pmt->getCurrency()->getId());
}
// Check taxes
$ut->assertEquals(count($m1->getTaxes()), count($m2->getTaxes()));
for ($i = 0; $i < count($m1->getTaxes()); $i++) {
$m1Tax = $m1->getTaxes()->get($i);
$m2Tax = $m2->getTaxes()->get($i);
$ut->assertEquals($m1Tax->getTaxRate(), $m2Tax->getTaxRate());
$ut->assertEquals($m1Tax->getBase(), $m2Tax->getBase());
$ut->assertEquals($m1Tax->getBasePeriod(), $m2Tax->getBasePeriod());
$ut->assertEquals($m1Tax->getBaseFYear(), $m2Tax->getBaseFYear());
$ut->assertEquals($m1Tax->getAmount(), $m2Tax->getAmount());
$ut->assertEquals($m1Tax->getAmountPeriod(), $m2Tax->getAmountPeriod());
$ut->assertEquals($m1Tax->getAmountFYear(), $m2Tax->getAmountFYear());
$ut->assertEquals($m1Tax->getTax()->getId(), $m2Tax->getTax()->getId());
}
// Check category sales
$ut->assertEquals(count($m1->getCatSales()), count($m2->getCatSales()));
for ($i = 0; $i < count($m2->getCatSales()); $i++) {
$m1Cat = $m1->getCatSales()->get($i);
$m2Cat = $m2->getCatSales()->get($i);
$ut->assertEquals($m1Cat->getReference(), $m2Cat->getReference());
$ut->assertEquals($m1Cat->getLabel(), $m2Cat->getLabel());
$ut->assertEquals($m1Cat->getAmount(), $m2Cat->getAmount());
}
// Check category taxes
$ut->assertEquals(count($m1->getCatTaxes()), count($m2->getCatTaxes()));
for ($i = 0; $i < count($m1->getCatTaxes()); $i++) {
$m1CatTax = $m1->getCatTaxes()->get($i);
$m2CatTax = $m2->getCatTaxes()->get($i);
$ut->assertEquals($m1CatTax->getReference(), $m2CatTax->getReference());
$ut->assertEquals($m1CatTax->getLabel(), $m2CatTax->getLabel());
$ut->assertEquals($m1CatTax->getBase(), $m2CatTax->getBase());
$ut->assertEquals($m1CatTax->getAmount(), $m2CatTax->getAmount());
$ut->assertEquals($m1CatTax->getTax()->getId(), $m2CatTax->getTax()->getId());
}
// Check cust balances
$ut->assertEquals(count($m1->getCustBalances()), count($m2->getCustBalances()));
for ($i = 0; $i < count($m1->getCustBalances()); $i++) {
$m1CB = $m1->getCustBalances()->get($i);
$m2CB = $m2->getCustBalances()->get($i);
$ut->assertEquals($m1CB->getBalance(), $m2CB->getBalance());
$ut->assertEquals($m1CB->getCustomer()->getId(), $m2CB->getCustomer()->getId());
}
}
/** Check equality of a ticket model and a ticket structure.
* @param $ut Unit Test ($this in test classes). */
function assertSessionModelEqStruct($model, $struct, $ut) {
$ut->assertEquals($model->getCashRegister()->getId(), $struct['cashRegister']);
$ut->assertEquals($model->getSequence(), $struct['sequence']);
$ut->assertTrue(DateUtils::equals($model->getOpenDate(), $struct['openDate']));
$ut->assertTrue(DateUtils::equals($model->getCloseDate(), $struct['closeDate']));
$ut->assertEquals($model->getOpenCash(), $struct['openCash']);
$ut->assertEquals($model->getCloseCash(), $struct['closeCash']);
$ut->assertEquals($model->getExpectedCash(), $struct['expectedCash']);
$ut->assertEquals($model->getTicketCount(), $struct['ticketCount']);
$ut->assertEquals($model->getCustCount(), $struct['custCount']);
$ut->assertEquals($model->getCs(), $struct['cs']);
$ut->assertEquals($model->getCsPeriod(), $struct['csPeriod']);
$ut->assertEquals($model->getCsFYear(), $struct['csFYear']);
// Check payments
$ut->assertEquals(count($model->getPayments()), count($struct['payments']));
for ($i = 0; $i < count($struct['payments']); $i++) {
$m1Pmt = $model->getPayments()->get($i);
$m2Pmt = $struct['payments'][$i];
$ut->assertEquals($m1Pmt->getAmount(), $m2Pmt['amount']);
$ut->assertEquals($m1Pmt->getCurrencyAmount(), $m2Pmt['currencyAmount']);
$ut->assertEquals($m1Pmt->getPaymentMode()->getId(), $m2Pmt['paymentMode']);
$ut->assertEquals($m1Pmt->getCurrency()->getId(), $m2Pmt['currency']);
}
// Check taxes
$ut->assertEquals(count($model->getTaxes()), count($struct['taxes']));
for ($i = 0; $i < count($struct['taxes']); $i++) {
$m1Tax = $model->getTaxes()->get($i);
$m2Tax = $struct['taxes'][$i];
$ut->assertEquals($m1Tax->getTaxRate(), $m2Tax['taxRate']);
$ut->assertEquals($m1Tax->getBase(), $m2Tax['base']);
$ut->assertEquals($m1Tax->getBasePeriod(), $m2Tax['basePeriod']);
$ut->assertEquals($m1Tax->getBaseFYear(), $m2Tax['baseFYear']);
$ut->assertEquals($m1Tax->getAmount(), $m2Tax['amount']);
$ut->assertEquals($m1Tax->getAmountPeriod(), $m2Tax['amountPeriod']);
$ut->assertEquals($m1Tax->getAmountFYear(), $m2Tax['amountFYear']);
$ut->assertEquals($m1Tax->getTax()->getId(), $m2Tax['tax']);
}
// Check category sales
$ut->assertEquals(count($model->getCatSales()), count($struct['catSales']));
for ($i = 0; $i < count($struct['catSales']); $i++) {
$m1Cat = $model->getCatSales()->get($i);
$m2Cat = $struct['catSales'][$i];
$ut->assertEquals($m1Cat->getReference(), $m2Cat['reference']);
$ut->assertEquals($m1Cat->getLabel(), $m2Cat['label']);
$ut->assertEquals($m1Cat->getAmount(), $m2Cat['amount']);
}
// Check category taxes
$ut->assertEquals(count($model->getCatTaxes()), count($struct['catTaxes']));
for ($i = 0; $i < count($struct['catTaxes']); $i++) {
$m1CatTax = $model->getCatTaxes()->get($i);
$m2CatTax = $struct['catTaxes'][$i];
$ut->assertEquals($m1CatTax->getReference(), $m2CatTax['reference']);
$ut->assertEquals($m1CatTax->getLabel(), $m2CatTax['label']);
$ut->assertEquals($m1CatTax->getBase(), $m2CatTax['base']);
$ut->assertEquals($m1CatTax->getAmount(), $m2CatTax['amount']);
$ut->assertEquals($m1CatTax->getTax()->getId(), $m2CatTax['tax']);
}
// Check cust balances
$ut->assertEquals(count($model->getCustBalances()), count($struct['custBalances']));
for ($i = 0; $i < count($model->getCustBalances()); $i++) {
$m1CB = $model->getCustBalances()->get($i);
$m2CB = $struct['custBalances'][$i];
$ut->assertEquals($m1CB->getBalance(), $m2CB['balance']);
$ut->assertEquals($m1CB->getCustomer()->getId(), $m2CB['customer']);
}
}
function readSessionSnapshot($cashRegister, $sequence, $dao) {
$search = $dao->search(CashSession::class,
[new DAOCondition('cashRegister', '=', $cashRegister),
new DAOCondition('sequence', '=', $sequence)]);
if (count($search) > 0) {
return $dao->readSnapshot(CashSession::class, $search[0]->getId());
}
return null;
}
......@@ -20,7 +20,12 @@
// Some utility functions to test tickets.
use \Pasteque\Server\Model\Currency;
use \Pasteque\Server\Model\PaymentMode;
use \Pasteque\Server\Model\Ticket;
use \Pasteque\Server\Model\TicketLine;
use \Pasteque\Server\Model\TicketPayment;
use \Pasteque\Server\Model\TicketTax;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\DAO\DAOCondition;
......@@ -68,6 +73,84 @@ function getBasePayment($paymentMode, $currency, $dispOrder, $amount) {
'currency' => $currency->getId()];
}
function ticketNew($cashRegister, $sequence, $number, $date, $user) {
$tkt = new Ticket();
$tkt->setCashRegister($cashRegister);
$tkt->setSequence($sequence);
$tkt->setNumber($number);
$tkt->setDate(DateUtils::readDate($date));
$tkt->setUser($user);
return $tkt;
}
function ticketAddLine($tkt, $product, $qty, $discountRate = 0.0) {
$tktLine = new TicketLine();
$tktLine->setDispOrder(count($tkt->getLines()) + 1);
$tktLine->setProduct($product);
$tktLine->setTax($product->getTax());
$tktLine->setTaxRate($product->getTax()->getRate());
$tktLine->setTaxedUnitPrice(round($product->getPriceSell() * (1 + $product->getTax()->getRate()), 2));
$tktLine->setQuantity($qty);
$tktLine->setTaxedPrice(round($tktLine->getTaxedUnitPrice() * $qty, 2));
if ($discountRate != 0.0) {
$tktLine->setDiscountRate($discountRate);
$tktLine->setFinalTaxedPrice(round($tktLine->getTaxedPrice() * (1 - $discountRate), 2));
} else {
$tktLine->setFinalTaxedPrice($tktLine->getTaxedPrice());
}
$tkt->addLine($tktLine);
}
function ticketAddPayment($tkt, $paymentMode, $currency, $amount) {
$tktPmt = new TicketPayment();
$tktPmt->setPaymentMode($paymentMode);
$tktPmt->setCurrency($currency);
$tktPmt->setAmount($amount);
$tktPmt->setCurrencyAmount(round($amount * $currency->getRate(), 2));
$tktPmt->setDispOrder(count($tkt->getPayments()) + 1);
$tkt->addPayment($tktPmt);
}
function ticketFinalize($tkt) {
$tktTaxes = [];
$totalPrice = 0.0;
$totalBase = 0.0;
// Add all taxed prices
foreach ($tkt->getLines() as $line) {
$tax = $line->getTax();
if (empty($tktTaxes[$tax->getId()])) {
$tktTax = new TicketTax();
$tktTax->setTax($tax);
$tktTaxes[$tax->getId()] = ['tax' => $tktTax, 'sum' => 0.0];
}
$tktTaxes[$tax->getId()]['sum'] = round($tktTaxes[$tax->getId()]['sum'] + $line->getFinalTaxedPrice(), 2);
$totalPrice = round($totalPrice + $line->getFinalTaxedPrice());
}
$tkt->setTaxedPrice($totalPrice);
// Apply ticket discount
if ($tkt->getDiscountRate() != 0.0) {
$totalPrice = round($totalPrice * (1 - $tkt->getDiscountRate()), 2);
foreach ($tktTaxes as $t) {
$t['sum'] = round($t['sum'] * (1 - $tkt->getDiscountRate()), 2);
}
}
// Compute final base and amount
foreach ($tktTaxes as $t) {
$tktTax = $t['tax'];
$tktTax->setBase(round($t['sum'] / (1 + $tktTax->getTaxRate()), 2));
$tktTax->setAmount(round($t['sum'] - $tktTax->getBase(), 2));
$tkt->addTax($tktTax);
$totalBase = round($totalBase + $tktTax->getBase(), 2);
}
// Set final prices
$tkt->setFinalPrice($totalBase);
if ($tkt->getDiscountRate() != 0.0) {
$tkt->setFinalTaxedPrice(round($totalPrice * (1 - $tkt->getDiscountRate()), 2));
} else {
$tkt->setFinalTaxedPrice($totalPrice);
}
}
/** Check equality of a ticket model and a ticket structure.i
* Should be used with a model and a dao snapshot to prevent mixing references.
* @param $ut Unit Test ($this in test classes). */
......
<?php
// Pasteque server testing
//
// Copyright (C)
// 2012 Scil (http://scil.coop)
// 2017 Karamel, Association Pastèque (karamel@creativekara.fr, https://pasteque.org)
//
// This file is part of Pasteque.
//
// Pasteque is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Pasteque is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Pasteque. If not, see <http://www.gnu.org/licenses/>.
namespace Pasteque\Server;
use \Pasteque\Server\Model\Category;
use \Pasteque\Server\Model\CashRegister;
use \Pasteque\Server\Model\CashSession;
use \Pasteque\Server\Model\CashSessionCat;
use \Pasteque\Server\Model\CashSessionCatTax;
use \Pasteque\Server\Model\CashSessionPayment;
use \Pasteque\Server\Model\CashSessionTax;
use \Pasteque\Server\Model\Currency;
use \Pasteque\Server\Model\Customer;
use \Pasteque\Server\Model\FiscalTicket;
use \Pasteque\Server\Model\PaymentMode;
use \Pasteque\Server\Model\Product;
use \Pasteque\Server\Model\Role;
use \Pasteque\Server\Model\Tax;
use \Pasteque\Server\Model\TariffArea;
use \Pasteque\Server\Model\Ticket;
use \Pasteque\Server\Model\TicketLine;
use \Pasteque\Server\Model\TicketTax;
use \Pasteque\Server\Model\TicketPayment;
use \Pasteque\Server\Model\User;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\Login;
use \Pasteque\Server\System\DAO\DAOCondition;
use \Pasteque\Server\System\DAO\DAOFactory;
use \PHPUnit\Framework\TestCase;
require_once(dirname(dirname(__FILE__)) . '/common_load.php');
require_once(dirname(dirname(__FILE__)) . '/common_session.php');
require_once(dirname(dirname(__FILE__)) . '/common_ticket.php');
class HttpCashSessionTest extends TestCase
{
private $curl;
private static $token;
private $dao;
private $tax;
private $cat;
private $prd;
private $cash;
private $pm;
private $curr;
private $role;
private $user;
public static function setUpBeforeClass() {
static::$token = obtainToken();
}
public static function tearDownAfterClass() {
}
protected function setUp() {
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token]);
global $dbInfo;
$this->dao = DAOFactory::getDAO($dbInfo, ['debug' => true]);
$this->cat = new Category();
$this->cat->setReference('category');
$this->cat->setLabel('Category');
$this->dao->write($this->cat);
$this->tax= new Tax();
$this->tax->setLabel('VAT');
$this->tax->setRate(0.1);
$this->dao->write($this->tax);
$this->prd = new Product();
$this->prd->setReference('product');
$this->prd->setLabel('Product');
$this->prd->setTax($this->tax);
$this->prd->setCategory($this->cat);
$this->prd->setPriceSell(1.0);
$this->dao->write($this->prd);
$this->pm = new PaymentMode();
$this->pm->setReference('pm');
$this->pm->setLabel('Payment mode');
$this->dao->write($this->pm);
$this->curr = new Currency();
$this->curr->setReference('curr');
$this->curr->setLabel('Currency');
$this->curr->setMain(true);
$this->dao->write($this->curr);
$this->cash = new CashRegister();
$this->cash->setReference('cash');
$this->cash->setLabel('Cash');
$this->dao->write($this->cash);
$this->role = new Role();
$this->role->setName('role');
$this->dao->write($this->role);
$this->user = new User();
$this->user->setName('user');
$this->user->setRole($this->role);
$this->dao->write($this->user);
$this->session = new CashSession();
$this->session->setCashRegister($this->cash);
$this->session->setSequence(1);
$this->dao->write($this->session);
$this->dao->commit();
}
protected function tearDown() {
curl_close($this->curl);
foreach ([FiscalTicket::class, TicketPayment::class, TicketTax::class,
TicketLine::class, Ticket::class, Customer::class,
CashSessionTax::class, CashSessionCatTax::class,
CashSessionCat::class, CashSessionPayment::class,
CashSession::class, CashRegister::class, User::class,
Role::class, Product::class, Category::class,
Tax::class, PaymentMode::class, Currency::class]
as $class) {
$all = $this->dao->search($class);
foreach($all as $record) {
$this->dao->delete($record);
}
}
$this->dao->commit();
$this->dao->close();
}
public function testOpen() {
$structSess = $this->session->toStruct();
$structSess['openDate'] = DateUtils::toTimestamp(new \DateTime('2018-01-01 10:00'));
$structSess['openCash'] = 200.0;
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/cash'));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->curl, CURLOPT_POSTFIELDS,
'session=' . json_encode($structSess));
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$data = json_decode($resp, true);
$this->assertNotNull($data);
$snap = $this->dao->readSnapshot(CashSession::class, $this->session->getId());
$this->assertTrue(DateUtils::equals($structSess['openDate'], $snap->getOpenDate()));
assertSessionModelEqStruct($snap, $structSess, $this);
assertSessionModelEqStruct($snap, $data, $this);
}
/** @depends testOpen */
public function testCloseEmpty() {
$structSess = $this->session->toStruct();
$structSess['openDate'] = DateUtils::toTimestamp(new \DateTime('2018-01-01 10:00'));
$structSess['openCash'] = 200.0;
$structSess['closeCash'] = 200.0;
$structSess['expectedCash'] = 200.0;
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/cash'));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->curl, CURLOPT_POSTFIELDS,
'session=' . json_encode($structSess));
curl_exec($this->curl);
$structSess['closeDate'] = DateUtils::toTimestamp(new \DateTime('2018-01-01 10:02'));
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/cash'));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->curl, CURLOPT_POSTFIELDS,
'session=' . json_encode($structSess));
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$data = json_decode($resp, true);
$this->assertNotNull($data, $resp);
$snap = $this->dao->readSnapshot(CashSession::class, $this->session->getId());
$this->assertTrue(DateUtils::equals($structSess['openDate'], $snap->getOpenDate()));
assertSessionModelEqStruct($snap, $structSess, $this);
assertSessionModelEqStruct($snap, $data, $this);
}
public function testSummary() {
// Open the session and add a ticket
$this->session->setOpenDate(new \DateTime('2018-01-01 10:00'));
$this->session->setOpenCash(200.0);
$this->dao->write($this->session);
$this->dao->commit();
$tkt = ticketNew($this->session->getCashRegister(), $this->session->getSequence(), 1, '2018-01-01 10:08', $this->user);
ticketAddLine($tkt, $this->prd, 1);
ticketAddPayment($tkt, $this->pm, $this->curr, 1.1);
ticketFinalize($tkt);
$this->dao->write($tkt);
$this->dao->commit();
// Get summary and check it
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/cashsession/summary/' . $this->session->getCashRegister()->getId() . '/' . $this->session->getSequence()));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'GET');
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$data = json_decode($resp, true);
$this->assertNotNull($data, $resp);
// Check basic data
$this->assertEquals($this->session->getCashRegister()->getId(), $data['cashRegister']);
$this->assertEquals($this->session->getSequence(), $data['sequence']);
$this->assertEquals(1, $data['ticketCount']);
$this->assertEquals(null, $data['custCount']);
$this->assertEquals(1, $data['paymentCount']);
$this->assertEquals(1.0, $data['cs']);
// Check payments
$this->assertEquals(1, count($data['payments']));
$pmt = $data['payments'][0];
$this->assertEquals($this->pm->getReference(), $pmt['type']);
$this->assertEquals($this->curr->getId(), $pmt['currency']);
$this->assertEquals(1.1, $pmt['amount']);
$this->assertEquals(1.1, $pmt['currencyAmount']);