diff --git a/Admin/Install/db.json b/Admin/Install/db.json index 8708564..a2bbe9e 100644 --- a/Admin/Install/db.json +++ b/Admin/Install/db.json @@ -291,21 +291,27 @@ "type": "INT", "null": false }, - "billing_out_element_purchase_price": { - "name": "billing_out_element_purchase_price", - "type": "INT", + "billing_out_element_single_salesprice_net": { + "name": "billing_out_element_single_salesprice_net", + "type": "BIGINT", "null": true, "default": null }, - "billing_out_element_price_single": { - "name": "billing_out_element_price_single", - "type": "INT", + "billing_out_element_single_purchaseprice_net": { + "name": "billing_out_element_single_purchaseprice_net", + "type": "BIGINT", "null": true, "default": null }, - "billing_out_element_price_total": { - "name": "billing_out_element_price_total", - "type": "INT", + "billing_out_element_total_salesprice_net": { + "name": "billing_out_element_total_salesprice_net", + "type": "BIGINT", + "null": true, + "default": null + }, + "billing_out_element_total_purchaseprice_net": { + "name": "billing_out_element_total_purchaseprice_net", + "type": "BIGINT", "null": true, "default": null }, diff --git a/Controller/ApiController.php b/Controller/ApiController.php index ddc4a2d..e528d89 100644 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -30,6 +30,7 @@ use phpOMS\Message\ResponseAbstract; use phpOMS\Model\Message\FormValidation; use phpOMS\Utils\Parser\Markdown\Markdown; use Modules\ItemManagement\Models\ItemMapper; +use phpOMS\Localization\Money; /** * Billing class. @@ -138,6 +139,10 @@ final class ApiController extends Controller $element = $this->createBillElementFromRequest($request, $response, $data); $this->createModel($request->header->account, $element, BillElementMapper::class, 'bill_element', $request->getOrigin()); + + $bill = $this->updateBillWithBillElement($element, 1); + $this->updateModel($request->header->account, $element, BillMapper::class, 'bill_element', $request->getOrigin()); + $this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Bill element', 'Bill element successfully created.', $element); } @@ -163,12 +168,41 @@ final class ApiController extends Controller // @todo: which item name should be stored in the database? server language (problem for international company with subsidiaries)? customer default language/customer invoice language? $element->itemNumber = $item->number; $element->itemName = $item->getL11n('name1')->description; - $element->quantity = $request->getData('quantity', 'int');; + $element->quantity = $request->getData('quantity', 'int'); + + $element->singleSalesPriceNet = new Money($request->getData('singlesalespricenet', 'int') ?? $item->salesPrice->getInt()); + $element->totalSalesPriceNet = clone $element->singleSalesPriceNet; + $element->totalSalesPriceNet->mult($element->quantity); + + $element->singlePurchasePriceNet = new Money($item->purchasePrice->getInt()); + $element->totalPurchasePriceNet = clone $element->singlePurchasePriceNet; + $element->totalPurchasePriceNet->mult($element->quantity); } return $element; } + /** + * Method to create a wiki entry from request. + * + * @param BillElement $element Bill element + * @param int $type Change type (0 = update, -1 = remove, +1 = add) + * + * @return BillElement + * + * @since 1.0.0 + */ + public function updateBillWithBillElement(BillElement $element, int $type = 1) : Bill + { + $bill = BillMapper::get($element->bill); + + if ($type === 1) { + $bill->net->add($element->singleSalesPriceNet); + } + + return $bill; + } + /** * Method to validate wiki entry creation from request * diff --git a/Models/Bill.php b/Models/Bill.php index 7f3b799..c5d2cdd 100644 --- a/Models/Bill.php +++ b/Models/Bill.php @@ -193,13 +193,13 @@ class Bill implements \JsonSerializable private $referralName = ''; - public ?Money $net = null; + public Money $net; - public ?Money $gross = null; + public Money $gross; - public ?Money $costs = null; + public Money $costs; - public ?Money $profit = null; + public Money $profit; private $currency = ISO4217CharEnum::_EUR; diff --git a/Models/BillElement.php b/Models/BillElement.php index 55b5072..5c2e30b 100644 --- a/Models/BillElement.php +++ b/Models/BillElement.php @@ -44,11 +44,11 @@ class BillElement implements \JsonSerializable public string $itemDescription = ''; - public int|float $quantity = 0; + public int $quantity = 0; - private $singlePrice = null; + public Money $singleSalesPriceNet; - private $totalPrice = null; + public Money $totalSalesPriceNet; private $singleDiscountP = null; @@ -62,13 +62,17 @@ class BillElement implements \JsonSerializable private $totalPriceNet = null; + public Money $singlePurchasePriceNet; + + public Money $totalPurchasePriceNet; + private $taxP = null; private $taxR = 0.0; - private $singlePriceGross = null; + private $singleSalesPriceGross = null; - private $totalPriceGross = null; + private $totalSalesPriceGross = null; private $event = 0; @@ -76,6 +80,15 @@ class BillElement implements \JsonSerializable public $bill = 0; + public function __construct() + { + $this->singleSalesPriceNet = new Money(); + $this->totalSalesPriceNet = new Money(); + + $this->singlePurchasePriceNet = new Money(); + $this->totalPurchasePriceNet = new Money(); + } + /** * Get id. * @@ -232,32 +245,6 @@ class BillElement implements \JsonSerializable return $this->itemDescription; } - /** - * Set quantity. - * - * @param int|float $quantity Quantity - * - * @return void - * - * @since 1.0.0 - */ - public function setQuantity($quantity) : void - { - $this->quantity = $quantity; - } - - /** - * Get quantity. - * - * @return int|float - * - * @since 1.0.0 - */ - public function getQuantity() - { - return $this->quantity; - } - /** * Set single unit price. * diff --git a/Models/BillElementMapper.php b/Models/BillElementMapper.php index 4be3f86..d5c557f 100644 --- a/Models/BillElementMapper.php +++ b/Models/BillElementMapper.php @@ -41,6 +41,10 @@ final class BillElementMapper extends DataMapperAbstract 'billing_out_element_item_name' => ['name' => 'billing_out_element_item_name', 'type' => 'string', 'internal' => 'itemName'], 'billing_out_element_item_desc' => ['name' => 'billing_out_element_item_desc', 'type' => 'string', 'internal' => 'itemDescription'], 'billing_out_element_quantity' => ['name' => 'billing_out_element_quantity', 'type' => 'int', 'internal' => 'quantity'], + 'billing_out_element_single_salesprice_net' => ['name' => 'billing_out_element_single_salesprice_net', 'type' => 'Serializable', 'internal' => 'singleSalesPriceNet'], + 'billing_out_element_single_purchaseprice_net' => ['name' => 'billing_out_element_single_purchaseprice_net', 'type' => 'Serializable', 'internal' => 'singlePurchasePriceNet'], + 'billing_out_element_total_salesprice_net' => ['name' => 'billing_out_element_total_salesprice_net', 'type' => 'Serializable', 'internal' => 'totalSalesPriceNet'], + 'billing_out_element_total_purchaseprice_net' => ['name' => 'billing_out_element_total_purchaseprice_net', 'type' => 'Serializable', 'internal' => 'totalPurchasePriceNet'], 'billing_out_element_bill' => ['name' => 'billing_out_element_bill', 'type' => 'int', 'internal' => 'bill'], ]; diff --git a/Theme/Backend/sales-bill.tpl.php b/Theme/Backend/sales-bill.tpl.php index 5b898f1..1f3816c 100644 --- a/Theme/Backend/sales-bill.tpl.php +++ b/Theme/Backend/sales-bill.tpl.php @@ -134,14 +134,14 @@ echo $this->getData('nav')->render(); ?>
| = $this->getHtml('Item'); ?> - | = $this->getHtml('Variation'); ?> | = $this->getHtml('Name'); ?> | = $this->getHtml('Quantity'); ?> + | = $this->getHtml('Price'); ?> | = $this->getHtml('Discount'); ?> | = $this->getHtml('DiscountP'); ?> | = $this->getHtml('Bonus'); ?> @@ -150,23 +150,23 @@ echo $this->getData('nav')->render(); ?> | ||||||
| + | - | - | + | + | - | + | = $element->totalSalesPriceNet->getCurrency(); ?> | ||||||
| - | + | + |