Commit 4028ff71 authored by Karamel's avatar Karamel
Browse files

Add tax by category on CashSession.

parent 08cc7132
......@@ -197,6 +197,7 @@ class CashsessionAPI extends APIHelper implements API
$custBalances = [];
$taxes = [];
$catSales = [];
$catTaxes = [];
// Load tickets and add them to the summary
$tickets = $this->dao->search(Ticket::class,
[new DAOCondition('cashRegister', '=', $cashSession->getCashRegister()),
......@@ -204,6 +205,7 @@ class CashsessionAPI extends APIHelper implements API
$summaryPmts = [];
$summaryTaxes = [];
$summaryCats = []; // Meow?
$summaryCatTaxes = [];
$summaryCusts = [];
foreach ($tickets as $tkt) {
$ticketCount++;
......@@ -262,9 +264,30 @@ class CashsessionAPI extends APIHelper implements API
: 0;
if (!isset($summaryCats[$catId])) {
$summaryCats[$catId] = ['category' => $catId, // desktop
'amount' => 0.0];
'amount' => 0.0, 'taxes' => []];
}
$summaryCats[$catId]['amount'] += $line->getDiscountPrice();
if (!isset($summaryCats[$catId]['taxes'][$taxId])) {
$summaryCats[$catId]['taxes'][$taxId] = ['tax' => $taxId,
'base' => 0.0, 'amount' => 0.0];
}
// Category tax
if (!isset($summaryCatTaxes[$catId])) {
$summaryCatTaxes[$catId] = [];
}
if (!isset($summaryCatTaxes[$catId][$taxId])) {
$ref = ""; $label = ""; // for no category
if ($catId != 0) {
$category = $line->getProduct()->getCategory();
$ref = $category->getReference();
$label = $category->getLabel();
}
$summaryCatTaxes[$catId][$taxId] = ['tax' => $taxId,
'reference' => $ref, 'label' => $label,
'base' => 0.0, 'amount' => 0.0];
}
$summaryCatTaxes[$catId][$taxId]['base'] += $line->getDiscountPrice();
$summaryCatTaxes[$catId][$taxId]['amount'] += $line->getDiscountTax();
}
} // foreach $tickets end
foreach ($summaryPmts as $pmtRef => $sum) {
......@@ -295,6 +318,15 @@ class CashsessionAPI extends APIHelper implements API
}
$catSales[] = $catSum;
}
foreach ($summaryCatTaxes as $catId => $tax) {
foreach ($tax as $taxId => $sum) {
$catTaxSum = new GenericModel();
foreach ($sum as $key => $value) {
$catTaxSum->set($key, $value);
}
$catTaxes[] = $catTaxSum;
}
}
$ret->set('ticketCount', $ticketCount);
$ret->set('custCount', $custCount);
$ret->set('paymentCount', $paymentCount);
......@@ -303,6 +335,7 @@ class CashsessionAPI extends APIHelper implements API
$ret->set('custBalances', $custBalances);
$ret->set('taxes', $taxes);
$ret->set('catSales', $catSales);
$ret->set('catTaxes', $catTaxes);
return $ret;
}
......
......@@ -76,6 +76,12 @@ class CashSession extends DoctrineModel
'array' => true,
'embedded' => true
],
[
'name' => 'catTaxes',
'class' => '\Pasteque\Server\Model\CashSessionCatTax',
'array' => true,
'embedded' => true
],
[
'name' => 'custBalances',
'class' => '\Pasteque\Server\Model\CashSessionCustBalance',
......@@ -390,6 +396,28 @@ class CashSession extends DoctrineModel
$cat->setCashSession($this);
}
/**
* Array of tax totals by category.
* @var \Pasteque\CashSessionCatTax[]
* @SWG\Property()
* @OneToMany(targetEntity="\Pasteque\Server\Model\CashSessionCatTax", mappedBy="cashSession", cascade={"persist"}, orphanRemoval=true)
*/
protected $catTaxes;
public function getCatTaxes() { return $this->catTaxes; }
public function setCatTaxes($catTaxes) {
$this->catTaxes->clear();
foreach ($catTaxes as $catTax) {
$this->addCatTax($catTax);
}
}
public function clearCatTaxes() {
$this->getCatTaxes()->clear();
}
public function addCatTax($catTax) {
$this->catTaxes->add($catTax);
$catTax->setCashSession($this);
}
/**
* Array of total balance change by customer.
* @var \Pasteque\CashSessionCat[]
......@@ -591,6 +619,16 @@ class CashSession extends DoctrineModel
$cat = $struct['catSales'][$i];
unset($cat['id']);
unset($cat['cashSession']);
$struct['catSales'][$i] = $cat;
}
for ($i = 0; $i < count($struct['catTaxes']); $i++) {
$catTax = $struct['catTaxes'][$i];
$tax = $this->getCatTaxes()->get($i)->getTax();
unset($catTax['id']);
unset($catTax['cashSession']);
$catTax['tax'] = $tax->getLabel();
$catTax['taxRate'] = $tax->getRate();
$struct['catTaxes'][$i] = $catTax;
}
return $struct;
}
......
......@@ -50,6 +50,10 @@ class CashSessionCat extends DoctrineModel // Embedded class
];
}
public function __construct() {
$this->taxes = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getId() {
return array('cashSession' => $this->getCashSession()->getId(),
'reference' => $this->getReference());
......
<?php
// Pastèque API
//
// Copyright (C)
// 2012 Scil (http://scil.coop)
// 2017 Karamel, Association Pastèque (karamel@creativekara.fr, https://pasteque.org)
//
// 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/>.
namespace Pasteque\Server\Model;
use \Pasteque\Server\System\DAO\DAO;
use \Pasteque\Server\System\DAO\DoctrineModel;
/**
* Class CashSessionCatTax. Sum of the cs amount by category by tax.
* This class is for fast data analysis only.
* For declarations see FiscalTicket.
* @package Pasteque
* @SWG\Definition(type="object")
* @Entity
* @Table(name="sessioncattaxes")
*/
class CashSessionCatTax extends DoctrineModel // Embedded class
{
public function getDirectFieldNames() {
// Not associative to be able to delete empty categories.
return ['reference', 'label', 'base', 'amount'];
}
public function getAssociationFields() {
return [
[
'name' => 'cashSession',
'class' => '\Pasteque\Server\Model\CashSessionCat',
'null' => true // because embedded
],
[
'name' => 'tax',
'class' => '\Pasteque\Server\Model\Tax',
]
];
}
public function getId() {
return ['cashSession' => $this->getCashSession()->getId(),
'reference' => $this->getReference(),
'tax' => $this->getTax()->getId()];
}
/**
* @var integer
* @SWG\Property
* @ManyToOne(targetEntity="\Pasteque\Server\Model\CashSession", inversedBy="taxes")
* @JoinColumn(name="cashsession_id", referencedColumnName="id", nullable=false)
* @Id
*/
protected $cashSession;
public function getCashSession() { return $this->cashSession; }
public function setCashSession($cashSession) { $this->cashSession = $cashSession; }
/**
* Code of the category, user-friendly ID.
* It is automatically set from label if not explicitely set.
* @var string
* @SWG\Property()
* @Column(type="string")
* @Id
*/
protected $reference;
public function getReference() { return $this->reference; }
public function setReference($ref) { $this->reference = $ref; }
/**
* Label of the category
* @var string
* @SWG\Property()
* @Column(type="string")
*/
protected $label;
public function getLabel() { return $this->label; }
public function setLabel($label) {
$this->label = $label;
if ($this->getReference() === null) {
$this->setReference($label);
}
}
/**
* Id of the tax
* @var integer
* @SWG\Property()
* @ManyToOne(targetEntity="\Pasteque\Server\Model\Tax")
* @JoinColumn(name="tax_id", referencedColumnName="id", nullable=false)
* @Id
*/
protected $tax;
public function getTax() { return $this->tax; }
/** Set the tax. If taxRate is null, it will be set with
* the rate of the tax. */
public function setTax($tax) {
$this->tax = $tax;
}
/**
* @var float
* @SWG\Property()
* @Column(type="float")
*/
protected $base;
public function getBase() { return round($this->base, 5); }
public function setBase($base) {
$this->base = round($base, 5);
}
/**
* @var float
* @SWG\Property()
* @Column(type="float")
*/
protected $amount;
public function getAmount() { return round($this->amount, 5); }
public function setAmount($amount) {
$this->amount = round($amount, 5);
}
}
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