Commit 710bd909 authored by Karamel's avatar Karamel
Browse files

Fix old desktop ticket write, add tests.

parent 688e8824
......@@ -136,7 +136,12 @@ class TicketPayment extends DoctrineEmbeddedModel
public static function load($struct, $parentRecord, $dao) {
$struct = PaymentMode::convertIdFromType($struct, $dao);
return parent::load($struct, $parentRecord, $dao);
}
}
public function merge($struct, $dao) {
$struct = PaymentMode::convertIdFromType($struct, $dao);
parent::merge($struct, $dao);
}
public function toStruct() {
$struct = parent::toStruct();
......
<?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\Ticket;
use \Pasteque\Server\Model\TicketPayment;
use \Pasteque\Server\Model\TicketTax;
use \Pasteque\Server\Model\Currency;
use \Pasteque\Server\Model\PaymentMode;
use \Pasteque\Server\Model\Tax;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\DAO\DAOFactory;
use \PHPUnit\Framework\TestCase;
require_once(dirname(dirname(__FILE__)) . "/common_load.php");
/** Test for CashSession and its subclasses CashSessionCat, CashSessionTax... */
class TicketTest extends TestCase
{
protected function setUp(): void {
global $dbInfo;
$this->dao = DAOFactory::getDAO($dbInfo, ['debug' => true]);
$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->currency = new Currency();
$this->currency->setReference('currency');
$this->currency->setLabel('Currency');
$this->dao->write($this->currency);
$this->paymentMode = new PaymentMode();
$this->paymentMode->setReference('payment');
$this->paymentMode->setLabel('Payment');
$this->dao->write($this->paymentMode);
$this->dao->commit();
}
protected function tearDown(): void {
foreach ([TicketTax::class, TicketPayment::class, Ticket::class]
as $class) {
$all = $this->dao->search($class);
foreach($all as $record) {
$this->dao->delete($record);
}
}
$this->dao->delete($this->tax);
$this->dao->delete($this->tax2);
$this->dao->delete($this->currency);
$this->dao->delete($this->paymentMode);
$this->dao->commit();
$this->dao->close();
}
/** Test the old format from Pasteque Desktop 8.0-8.6 which used hardcoded
* payment modes. */
public function testPaymentLoadDesktopOld() {
$struct = ['amount' => 10, 'currencyAmount' => 12,
'desktop' => true, 'dispOrder' => 0,
'type' => $this->paymentMode->getReference(),
'currency' => $this->currency->getId()];
$ticket = new Ticket();
$tktPayment = TicketPayment::loadOrCreate($struct, $ticket,
$this->dao);
$tktPayment->merge($struct, $this->dao);
$this->assertNotNull($tktPayment->getPaymentMode());
$this->assertEquals($this->paymentMode->getReference(),
$tktPayment->getPaymentMode()->getReference());
}
}
......@@ -331,6 +331,58 @@ class HttpCashSessionTest extends TestCase
assertSessionModelEqStruct($snap, $data, $this);
}
public function testCloseOldDesktop() {
// 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();
$structSess = $this->session->toStruct();
$structSess['closeDate'] = '2018-01-01 11:00';
$structSess['closeCash'] = 201.1;
$structSess['expectedCash'] = 201.1;
$structSess['closeType'] = CashSession::CLOSE_SIMPLE;
$structSess['cs'] = 1.1;
$structSess['csPeriod'] = 1.1;
$structSess['csFYear'] = 1.1;
$structSess['csPerpetual'] = 1.1;
$structSess['payments'] = [
['desktop' => true, 'type' => $this->pm->getReference(),
'currency' => $this->curr->getId(),
'amount' => 1.1, 'currencyAmount' => 1.1]
];
$structSess['taxes'] = [
['tax' => $this->tax->getId(), 'taxRate' => $this->tax->getRate(),
'base' => 1.0, 'amount' => 0.1,
'basePeriod' => 1.0, 'amountPeriod' => 0.1,
'baseFYear' => 1.0, 'amountFYear' => 0.1]
];
$structSess['catSales'] = [
['reference' => $this->cat->getReference(), 'label' => $this->cat->getLabel(), 'amount' => 1.0]
];
$structSess['catTaxes'] = [
['tax' => $this->tax->getId(), 'reference' => $this->cat->getReference(), 'label' => $this->cat->getLabel(), 'base' => 1.0, 'amount' => 0.1]
];
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());
$structSess['payments'][0]['paymentMode'] = $this->pm->getId();
assertSessionModelEqStruct($snap, $structSess, $this);
assertSessionModelEqStruct($snap, $data, $this);
}
/** @depends testClose */
public function testCloseNoPerpetualFirst() {
// Open the session and add a ticket
......
......@@ -424,6 +424,37 @@ class HttpTicketTest extends TestCase
$this->assertEquals(0, count($data['errors']), $resp);
}
public function testSingleOldDesktop() {
$tkt = $this->getBaseTicket(1);
$tkt['lines'][] = $this->getBaseLine(0, 'test', 11.0);
$tkt['taxes'][] = $this->getBaseTax(10.0, 1.0);
$tkt['payments'][] = [
'dispOrder' => 0, 'amount' => 11,
'currencyAmount' => 11,
'type' => $this->pm->getReference(),
'desktop' => true,
'currency' => $this->curr->getId()
];
curl_setopt($this->curl, CURLOPT_POSTFIELDS,
'tickets=' . json_encode([$tkt]));
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$data = json_decode($resp, true);
$this->assertNotNull($data, $resp);
$this->assertEquals(1, count($data['successes']), $resp);
$s = $data['successes'][0];
$this->assertEquals($this->cash->getId(), $s['cashRegister'], $resp);
$this->assertEquals($this->session->getSequence(), $s['sequence'],
$resp);
$this->assertEquals(1, $s['number'], $resp);
$snap = readTicketSnapshot($this->cash, $s['sequence'], $s['number'],
$this->dao);
$tkt['payments'][0]['paymentMode'] = $this->pm->getId(); // for equality
$this->assertTicketModelEqStruct($snap, $tkt);
$this->assertEquals(0, count($data['failures']), $resp);
$this->assertEquals(0, count($data['errors']), $resp);
}
/** @depends testSingle
* Write multiple regular tickets. */
public function testMultiple() {
......
Markdown is supported
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