Commit 46fc9246 authored by Karamel's avatar Karamel
Browse files

Add cash session summary route and API for Desktop.

parent 67d9264c
......@@ -3,6 +3,7 @@
use \Pasteque\Server\Model\CashSession;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\API\APICaller;
use \Pasteque\Server\System\API\APIResult;
/**
* GET cashGetbycashRegisterIdGet
......@@ -103,14 +104,22 @@ $app->GET('/api/cash/search/', function($request, $response, $args) {
/**
* GET cashZticketGet
* Summary:
* Notes: get a zticket of a cash session
* Notes: Get the summary of a session, like a preview of data for Z tickets.
* Output-Formats: [application/json]
* @SWG\Get(
* path="/api/cash/zticket/{id}",
* @SWG\Response(response="200", description="get a zticket of a cash session")
* )
*/
$app->GET('/api/cash/zticket/{id}', function($request, $response, $args) {
return $response->withJson(\Pasteque\CashesService::getZTicket($args['id']));
$app->GET('/api/cashsession/summary/{cashregisterid}/{sequence}', function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$cashApiResp = APICaller::run($ptApp, 'cashSession', 'get',
[['cashRegister' => $args['cashregisterid'],
'sequence' => $args['sequence']]]);
if ($cashApiResp->getStatus() != APIResult::STATUS_CALL_OK) {
return $response->withApiResult($cashApiResp);
}
$session = $cashApiResp->getContent();
return $response->withApiResult(APICaller::run($ptApp, 'cashSession',
'summary', $session));
});
......@@ -25,6 +25,8 @@ namespace Pasteque\Server\API;
use \Pasteque\Server\Model\CashRegister;
use \Pasteque\Server\Model\CashSession;
use \Pasteque\Server\Model\FiscalTicket;
use \Pasteque\Server\Model\GenericModel;
use \Pasteque\Server\Model\Ticket;
use \Pasteque\Server\System\DAO\DAOCondition;
/** CRUD API for Role. */
......@@ -169,6 +171,109 @@ class CashsessionAPI extends APIHelper implements API
$this->dao->write($eosTicket);
}
/** Get the summary of a session from registered tickets.
* This is not the Z Ticket, but it looks like it. */
public function summary($cashSession) {
// This one is used by Desktop to show the summary before closing
// the cash session.
$ret = new GenericModel();
// Initialize result
$ret->set('cashRegister', $cashSession->getCashRegister()->getId());
$ret->set('sequence', $cashSession->getSequence());
$ticketCount = 0;
$custCount = null;
$paymentCount = 0;
$cs = 0.0;
$payments = [];
$taxes = [];
$catSales = [];
// Load tickets and add them to the summary
$tickets = $this->dao->search(Ticket::class,
[new DAOCondition('cashRegister', '=', $cashSession->getCashRegister()),
new DAOCondition('sequence', '=', $cashSession->getSequence())]);
$summaryPmts = [];
$summaryTaxes = [];
$summaryCats = []; // Meow?
foreach ($tickets as $tkt) {
$ticketCount++;
if (!empty($tkt->getCustCount())) {
if ($custCount === null) {
$custCount = $tkt->getCustCount();
} else {
$custCount += $tkt->getCustCount();
}
}
$tktPayments = $tkt->getPayments();
// Payments sums
foreach ($tktPayments as $pmt) {
$paymentCount++;
// Because it is for Desktop, it follows the desktop strucure
// of ZTicket.Payment instead of the one from TicketPayment
$pmtRef = $pmt->getPaymentMode()->getReference() . $pmt->getCurrency()->getId();
if (!isset($summaryPmts[$pmtRef])) {
$summaryPmts[$pmtRef] = ['type' => $pmt->getPaymentMode()->getReference(),
'amount' => 0.0,
'currency' => $pmt->getCurrency()->getId(),
'currencyAmount' => 0.0];
}
$summaryPmts[$pmtRef]['amount'] += $pmt->getAmount();
$summaryPmts[$pmtRef]['currencyAmount'] += $pmt->getCurrencyAmount();
}
// Line sums
$tktLines = $tkt->getLines();
foreach ($tktLines as $line) {
$cs += $line->getDiscountPrice();
// Tax
$taxId = $line->getTax()->getId();
if (!isset($summaryTaxes[$taxId])) {
$summaryTaxes[$taxId] = ['tax' => $taxId, // desktop
'base' => 0.0,
'amount' => 0.0];
}
$summaryTaxes[$taxId]['base'] += $line->getDiscountPrice();
$summaryTaxes[$taxId]['amount'] += $line->getDiscountTax();
// Category
$catId = ($line->getProduct() != null)
? $line->getProduct()->getCategory()->getId()
: 0;
if (!isset($summaryCats[$catId])) {
$summaryCats[$catId] = ['category' => $catId, // desktop
'amount' => 0.0];
}
$summaryCats[$catId]['amount'] += $line->getDiscountPrice();
}
} // foreach $tickets end
foreach ($summaryPmts as $pmtRef => $sum) {
$pmtSum = new GenericModel();
foreach ($sum as $key => $value) {
$pmtSum->set($key, $value);
}
$payments[] = $pmtSum;
}
foreach ($summaryTaxes as $taxId => $sum) {
$taxSum = new GenericModel();
foreach ($sum as $key => $value) {
$taxSum->set($key, $value);
}
$taxes[] = $taxSum;
}
foreach ($summaryCats as $catId => $sum) {
$catSum = new GenericModel();
foreach ($sum as $key => $value) {
$catSum->set($key, $value);
}
$catSales[] = $catSum;
}
$ret->set('ticketCount', $ticketCount);
$ret->set('custCount', $custCount);
$ret->set('paymentCount', $paymentCount);
$ret->set('cs', $cs);
$ret->set('payments', $payments);
$ret->set('taxes', $taxes);
$ret->set('catSales', $catSales);
return $ret;
}
/** Delete is disabled. */
public function delete($id) {}
}
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