Commit f001d5d2 authored by Karamel's avatar Karamel
Browse files

Update Ticket to set data in FiscalTicket instead of the printed result.

parent f2fbc38b
......@@ -83,16 +83,12 @@ class TicketAPI extends APIHelper implements API
|| !$session->isOpened()){
throw new \BadMethodCallException('Tickets must be assigned to an opened cash session.');
}
// No printed content
if (empty($ticket->getPrintedContent())) {
throw new \BadMethodCallException('Missing printed content for fiscal ticket.');
}
// Create associated fiscal ticket
$fiscalTicket = new FiscalTicket();
$fiscalTicket->setType(FiscalTicket::TYPE_TICKET);
$fiscalTicket->setSequence(FiscalTicket::getTicketSequence($ticket));
$fiscalTicket->setNumber($ticket->getNumber());
$fiscalTicket->setContent($ticket->getPrintedContent());
$fiscalTicket->setContent(json_encode($ticket->toStone()));
// Check continuity and integrity
if ($ticket->getNumber() == 1 && $previousFTicket !== null) {
// No.1 with a previous ticket
......
......@@ -22,6 +22,7 @@
namespace Pasteque\Server\Model;
use \Pasteque\Server\API\VersionAPI;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\DAO\DoctrineModel;
......@@ -249,17 +250,6 @@ public function getPayments() { return $this->payments; }
$this->discountProfile = $discountProfile;
}
/** XML content of the printed ticket.
* It is not stored in database but used when saving tickets
* to generate the associated FiscalTicket. It is build with
* fromStruct but not passed to toStruct at it is only meant
* for internal use. */
protected $printedContent;
public function getPrintedContent() { return $this->printedContent; }
public function setPrintedContent($content) {
$this->printedContent = $content;
}
/** Get subtotal, total and tax amount before ticket discount. This includes
* line discounts. */
public function getTotals() {
......@@ -300,18 +290,68 @@ public function getPayments() { return $this->payments; }
return $amounts;
}
public static function fromStruct($struct, $dao, $embedded = false) {
$model = parent::fromStruct($struct, $dao, $embedded);
if (isset($struct['printedContent'])) {
$model->setPrintedContent($struct['printedContent']);
}
return $model;
}
public function toStruct() {
$struct = parent::toStruct();
$struct['date'] = DateUtils::toTimestamp($this->getDate());
return $struct;
}
/** Create struct of the full data to be written in stone
* (that is, FiscalTicket). */
public function toStone() {
$struct = $this->toStruct();
// Include source version in case it has to be parsed.
$struct['version'] = VersionAPI::VERSION;
// Format date in human-readable format
$struct['date'] = $this->getDate()->format('Y-m-d H:i:s');
// Fetch associative fields and include the data.
unset($struct['id']);
$struct['cashRegister'] = ['reference' => $this->getCashRegister()->getReference(),
'label' => $this->getCashRegister()->getLabel()];
$struct['user'] = $this->getUser()->getName();
for ($i = 0; $i < count($struct['lines']); $i++) {
// Lines already has all the unlinked values.
// Just delete references.
$line = $struct['lines'][$i];
unset($line['id']);
unset($line['ticket']);
unset($line['product']);
unset($line['tax']);
$struct['lines'][$i] = $line;
}
for ($i = 0; $i < count($struct['payments']); $i++) {
$payment = $struct['payments'][$i];
unset($payment['id']);
unset($payment['ticket']);
$paymentMode = $this->getPayments()->get($i)->getPaymentMode();
$currency = $this->getPayments()->get($i)->getCurrency();
$payment['paymentMode'] = ['reference' =>$paymentMode->getReference(),
'label' => $paymentMode->getLabel()];
$payment['currency'] = ['reference' => $currency->getReference(),
'label' => $currency->getLabel()];
$struct['payments'][$i] = $payment;
}
if ($this->getCustomer() !== null) {
// Delete all non-identification fields
$c = $this->getCustomer()->toStruct();
unset($c['id']);
unset($c['maxDebt']);
unset($c['currDebt']);
unset($c['debtDate']);
unset($c['prepaid']);
unset($c['note']);
unset($c['visible']);
unset($c['hasImage']);
unset($c['expireDate']);
$struct['customer'] = $c;
}
if ($this->getTariffArea() !== null) {
$struct['tariffArea'] = ['reference' => $this->getTariffArea()->getReference(),
'label' => $this->getTariffArea()->getLabel()];
}
if ($this->getDiscountProfile() !== null) {
$struct['discountProfile'] = $this->getDiscountProfile()->getLabel();
}
return $struct;
}
}
......@@ -214,7 +214,6 @@ class TicketAPITest extends TestCase
'amount' => 11.0,
'currencyAmount' => 11.0], $this->dao);
$tkt->addPayment($pm1);
$tkt->setPrintedContent('Hey hey!');
$this->api->write($tkt);
}
......@@ -239,7 +238,6 @@ class TicketAPITest extends TestCase
'amount' => 11.0,
'currencyAmount' => 11.0], $this->dao);
$tkt->addPayment($pm1);
$tkt->setPrintedContent('Hey hey!');
$this->api->write($tkt);
$readTkt = $this->dao->read(Ticket::class, $tkt->getId());
$this->assertNotNull($readTkt);
......@@ -289,7 +287,6 @@ class TicketAPITest extends TestCase
'amount' => 11.0,
'currencyAmount' => 11.0], $this->dao);
$tkt1->addPayment($pm11);
$tkt1->setPrintedContent('Hey hey!');
$this->api->write($tkt1);
$line21 = TicketLine::fromStruct(['dispOrder' => 1, 'price' => 10.0,
'unitPrice' => 10.0,
......@@ -310,7 +307,6 @@ class TicketAPITest extends TestCase
'amount' => 11.0,
'currencyAmount' => 11.0], $this->dao);
$tkt2->addPayment($pm21);
$tkt2->setPrintedContent('Hey hey!');
$this->api->write($tkt2);
}
......@@ -338,7 +334,6 @@ class TicketAPITest extends TestCase
'amount' => 11.0,
'currencyAmount' => 11.0], $this->dao);
$tkt->addPayment($pm1);
$tkt->setPrintedContent('Hey hey!');
$this->api->write($tkt);
$this->api->write($tkt);
}
......@@ -370,7 +365,6 @@ class TicketAPITest extends TestCase
'amount' => 11.0,
'currencyAmount' => 11.0], $this->dao);
$tkt->addPayment($pm1);
$tkt->setPrintedContent('Hey hey!');
$this->api->write($tkt);
}
......
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