From 70f2c0887b01d63154b7999b14cf0ac2618ad22f Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Mon, 29 Nov 2021 20:08:13 +0100 Subject: [PATCH] fix bugs --- Admin/Install/Media/bill.pdf.php | 8 +++++-- Admin/Install/db.json | 18 ++++++++-------- Controller/ApiController.php | 12 +++++------ Models/Bill.php | 4 ++++ Models/BillMapper.php | 2 +- Models/PurchaseBillMapper.php | 24 ++++++++++----------- Models/SalesBillMapper.php | 30 +++++++++++++-------------- Theme/Backend/sales-bill-list.tpl.php | 4 ++-- tests/Models/BillTest.php | 16 +++++++++----- 9 files changed, 65 insertions(+), 53 deletions(-) diff --git a/Admin/Install/Media/bill.pdf.php b/Admin/Install/Media/bill.pdf.php index 1215b94..d9b53b2 100755 --- a/Admin/Install/Media/bill.pdf.php +++ b/Admin/Install/Media/bill.pdf.php @@ -4,8 +4,12 @@ use Mpdf\Mpdf; /** * @var \phpOMS\Views\View $this + * @var \Modules\Billing\Models\Bill */ $bill = $this->getData('bill'); +/** + * @var \Modules\Billing\Models\BillElement[] $elements + */ $elements = $bill->getElements(); $mpdf = new Mpdf([ @@ -140,7 +144,7 @@ $html .= ' Subtotal: - ' . $bill->net->getCurrency(null) . ' + ' . $bill->netSales->getCurrency(null) . ' Tax: @@ -152,7 +156,7 @@ $html .= ' TOTAL: - ' . $bill->gross->getCurrency(null) . ' + ' . $bill->grossSales->getCurrency(null) . ' Deposit: diff --git a/Admin/Install/db.json b/Admin/Install/db.json index 951677a..5d5aa95 100755 --- a/Admin/Install/db.json +++ b/Admin/Install/db.json @@ -361,7 +361,7 @@ "default": null }, "billing_bill_element_single_grosslistprice": { - "name": "billing_bill_element_single_netlistprice", + "name": "billing_bill_element_single_grosslistprice", "type": "BIGINT", "null": true, "default": null @@ -373,7 +373,7 @@ "default": null }, "billing_bill_element_total_grosslistprice": { - "name": "billing_bill_element_total_netlistprice", + "name": "billing_bill_element_total_grosslistprice", "type": "BIGINT", "null": true, "default": null @@ -385,7 +385,7 @@ "default": null }, "billing_bill_element_single_grosssalesprice": { - "name": "billing_bill_element_single_netsalesprice", + "name": "billing_bill_element_single_grosssalesprice", "type": "BIGINT", "null": true, "default": null @@ -397,7 +397,7 @@ "default": null }, "billing_bill_element_total_grosssalesprice": { - "name": "billing_bill_element_total_netsalesprice", + "name": "billing_bill_element_total_grosssalesprice", "type": "BIGINT", "null": true, "default": null @@ -409,7 +409,7 @@ "default": null }, "billing_bill_element_single_grosspurchaseprice": { - "name": "billing_bill_element_single_netpurchaseprice", + "name": "billing_bill_element_single_grosspurchaseprice", "type": "BIGINT", "null": true, "default": null @@ -421,7 +421,7 @@ "default": null }, "billing_bill_element_total_grosspurchaseprice": { - "name": "billing_bill_element_total_netpurchaseprice", + "name": "billing_bill_element_total_grosspurchaseprice", "type": "BIGINT", "null": true, "default": null @@ -433,7 +433,7 @@ "default": null }, "billing_bill_element_single_grossprofit": { - "name": "billing_bill_element_single_netprofit", + "name": "billing_bill_element_single_grossprofit", "type": "BIGINT", "null": true, "default": null @@ -445,7 +445,7 @@ "default": null }, "billing_bill_element_total_grossprofit": { - "name": "billing_bill_element_total_netprofit", + "name": "billing_bill_element_total_grossprofit", "type": "BIGINT", "null": true, "default": null @@ -579,7 +579,7 @@ "primary": true, "autoincrement": true }, - "billing_bill_note_item": { + "billing_bill_note_bill": { "name": "billing_bill_note_bill", "type": "INT", "null": false, diff --git a/Controller/ApiController.php b/Controller/ApiController.php index e6b591f..45dc94e 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -290,8 +290,8 @@ final class ApiController extends Controller public function updateBillWithBillElement(Bill $bill, BillElement $element, int $type = 1) : Bill { if ($type === 1) { - $bill->net->add($element->singleSalesPriceNet); - $bill->costs->add($element->singlePurchasePriceNet); + $bill->netSales->add($element->totalSalesPriceNet); + $bill->netCosts->add($element->totalPurchasePriceNet); } return $bill; @@ -412,7 +412,7 @@ final class ApiController extends Controller } /** - * Api method to create item files + * Api method to create bill files * * @param RequestAbstract $request Request * @param ResponseAbstract $response Response @@ -427,13 +427,13 @@ final class ApiController extends Controller public function apiNoteCreate(RequestAbstract $request, ResponseAbstract $response, $data = null) : void { if (!empty($val = $this->validateNoteCreate($request))) { - $response->set('item_note_create', new FormValidation($val)); + $response->set('bill_note_create', new FormValidation($val)); $response->header->status = RequestStatusCode::R_400; return; } - $request->setData('virtualpath', '/Modules/Billing/Articles/' . $request->getData('id'), true); + $request->setData('virtualpath', '/Modules/Billing/Bills/' . $request->getData('id'), true); $this->app->moduleManager->get('Editor')->apiEditorCreate($request, $response, $data); if ($response->header->status !== RequestStatusCode::R_200) { @@ -445,7 +445,7 @@ final class ApiController extends Controller } /** - * Validate item note create request + * Validate bill note create request * * @param RequestAbstract $request Request * diff --git a/Models/Bill.php b/Models/Bill.php index 69f39a0..7d1f75a 100755 --- a/Models/Bill.php +++ b/Models/Bill.php @@ -479,6 +479,10 @@ class Bill implements \JsonSerializable */ public function getNumber() : string { + if (empty($this->number)) { + $this->buildNumber(); + } + return $this->number; } diff --git a/Models/BillMapper.php b/Models/BillMapper.php index e6bb02e..640adc4 100755 --- a/Models/BillMapper.php +++ b/Models/BillMapper.php @@ -104,7 +104,7 @@ class BillMapper extends DataMapperAbstract 'mapper' => EditorDocMapper::class, /* mapper of the related object */ 'table' => 'billing_bill_note', /* table of the related object, null if no relation table is used (many->1) */ 'external' => 'billing_bill_note_doc', - 'self' => 'billing_bill_note_item', + 'self' => 'billing_bill_note_bill', ], ]; diff --git a/Models/PurchaseBillMapper.php b/Models/PurchaseBillMapper.php index 248062e..3682845 100755 --- a/Models/PurchaseBillMapper.php +++ b/Models/PurchaseBillMapper.php @@ -80,7 +80,7 @@ final class PurchaseBillMapper extends BillMapper public static function getPurchaseByItemId(int $id, \DateTime $start, \DateTime $end) : Money { $query = new Builder(self::$db); - $result = $query->select('SUM(billing_bill_element_total_purchaseprice_net)') + $result = $query->select('SUM(billing_bill_element_total_netpurchaseprice)') ->from(self::$table) ->leftJoin(BillElementMapper::getTable()) ->on(self::$table . '.billing_bill_id', '=', BillElementMapper::getTable() . '.billing_bill_element_bill') @@ -99,7 +99,7 @@ final class PurchaseBillMapper extends BillMapper public static function getPurchaseBySupplierId(int $id, \DateTime $start, \DateTime $end) : Money { $query = new Builder(self::$db); - $result = $query->select('SUM(billing_bill_net)') + $result = $query->select('SUM(billing_bill_netcosts)') ->from(self::$table) ->where(self::$table . '.billing_bill_supplier', '=', $id) ->andWhere(self::$table . '.billing_bill_performance_date', '>=', $start) @@ -116,7 +116,7 @@ final class PurchaseBillMapper extends BillMapper public static function getAvgPurchasePriceByItemId(int $id, \DateTime $start, \DateTime $end) : Money { $query = new Builder(self::$db); - $result = $query->select('SUM(billing_bill_element_single_purchaseprice_net)', 'COUNT(billing_bill_element_total_purchaseprice_net)') + $result = $query->select('SUM(billing_bill_element_single_netpurchaseprice)', 'COUNT(billing_bill_element_total_netpurchaseprice)') ->from(self::$table) ->leftJoin(BillElementMapper::getTable()) ->on(self::$table . '.billing_bill_id', '=', BillElementMapper::getTable() . '.billing_bill_element_bill') @@ -192,7 +192,7 @@ final class PurchaseBillMapper extends BillMapper // @todo: limit is not working correctly... only returns / 2 or something like that?. Maybe because bills arent unique? - $query ??= self::getQuery(null, [], RelationType::ALL, $depth); + $query = self::getQuery(null, [], RelationType::ALL, $depth); $query->leftJoin(BillElementMapper::getTable(), BillElementMapper::getTable() . '_d' . $depth) ->on(self::$table . '_d' . $depth . '.billing_bill_id', '=', BillElementMapper::getTable() . '_d' . $depth . '.billing_bill_element_bill') ->where(BillElementMapper::getTable() . '_d' . $depth . '.billing_bill_element_item', '=', $id) @@ -216,7 +216,7 @@ final class PurchaseBillMapper extends BillMapper // @todo: limit is not working correctly... only returns / 2 or something like that?. Maybe because bills arent unique? - $query ??= self::getQuery(null, [], RelationType::ALL, $depth); + $query = self::getQuery(null, [], RelationType::ALL, $depth); $query->where(self::$table . '_d' . $depth . '.billing_bill_supplier', '=', $id) ->limit($limit); @@ -236,8 +236,8 @@ final class PurchaseBillMapper extends BillMapper { $depth = 3; - $query ??= SupplierMapper::getQuery(null, [], RelationType::ALL, $depth); - $query->selectAs('SUM(billing_bill_element_total_purchaseprice_net)', 'net_purchase') + $query = SupplierMapper::getQuery(null, [], RelationType::ALL, $depth); + $query->selectAs('SUM(billing_bill_element_total_netpurchaseprice)', 'net_purchase') ->leftJoin(self::$table, self::$table . '_d' . $depth) ->on(SupplierMapper::getTable() . '_d' . $depth . '.suppliermgmt_supplier_id', '=', self::$table . '_d' . $depth . '.billing_bill_supplier') ->leftJoin(BillElementMapper::getTable(), BillElementMapper::getTable() . '_d' . $depth) @@ -262,7 +262,7 @@ final class PurchaseBillMapper extends BillMapper { $query = new Builder(self::$db); $result = $query->select(CountryMapper::getTable() . '.country_region') - ->selectAs('SUM(billing_bill_element_total_purchaseprice_net)', 'net_purchase') + ->selectAs('SUM(billing_bill_element_total_netpurchaseprice)', 'net_purchase') ->from(self::$table) ->leftJoin(BillElementMapper::getTable()) ->on(self::$table . '.billing_bill_id', '=', BillElementMapper::getTable() . '.billing_bill_element_bill') @@ -285,7 +285,7 @@ final class PurchaseBillMapper extends BillMapper { $query = new Builder(self::$db); $result = $query->select(CountryMapper::getTable() . '.country_code2') - ->selectAs('SUM(billing_bill_element_total_purchaseprice_net)', 'net_purchase') + ->selectAs('SUM(billing_bill_element_total_netpurchaseprice)', 'net_purchase') ->from(self::$table) ->leftJoin(BillElementMapper::getTable()) ->on(self::$table . '.billing_bill_id', '=', BillElementMapper::getTable() . '.billing_bill_element_bill') @@ -309,8 +309,7 @@ final class PurchaseBillMapper extends BillMapper public static function getItemMonthlyPurchaseCosts(int $id, \DateTime $start, \DateTime $end) : array { $query = new Builder(self::$db); - $result = $query->selectAs('SUM(billing_bill_element_total_purchaseprice_net)', 'net_purchase') - ->selectAs('SUM(billing_bill_element_total_purchaseprice_net)', 'net_costs') + $result = $query->selectAs('SUM(billing_bill_element_total_netpurchaseprice)', 'net_purchase') ->selectAs('YEAR(billing_bill_performance_date)', 'year') ->selectAs('MONTH(billing_bill_performance_date)', 'month') ->from(self::$table) @@ -333,8 +332,7 @@ final class PurchaseBillMapper extends BillMapper public static function getSupplierMonthlyPurchaseCosts(int $id, \DateTime $start, \DateTime $end) : array { $query = new Builder(self::$db); - $result = $query->selectAs('SUM(billing_bill_net)', 'net_purchase') - ->selectAs('SUM(billing_bill_costs)', 'net_costs') + $result = $query->selectAs('SUM(billing_bill_netcosts)', 'net_purchase') ->selectAs('YEAR(billing_bill_performance_date)', 'year') ->selectAs('MONTH(billing_bill_performance_date)', 'month') ->from(self::$table) diff --git a/Models/SalesBillMapper.php b/Models/SalesBillMapper.php index a8c7b9e..04c9576 100755 --- a/Models/SalesBillMapper.php +++ b/Models/SalesBillMapper.php @@ -80,7 +80,7 @@ final class SalesBillMapper extends BillMapper public static function getSalesByItemId(int $id, \DateTime $start, \DateTime $end) : Money { $query = new Builder(self::$db); - $result = $query->select('SUM(billing_bill_element_total_salesprice_net)') + $result = $query->select('SUM(billing_bill_element_total_netsalesprice)') ->from(self::$table) ->leftJoin(BillElementMapper::getTable()) ->on(self::$table . '.billing_bill_id', '=', BillElementMapper::getTable() . '.billing_bill_element_bill') @@ -99,7 +99,7 @@ final class SalesBillMapper extends BillMapper public static function getSalesByClientId(int $id, \DateTime $start, \DateTime $end) : Money { $query = new Builder(self::$db); - $result = $query->select('SUM(billing_bill_net)') + $result = $query->select('SUM(billing_bill_netsales)') ->from(self::$table) ->where(self::$table . '.billing_bill_client', '=', $id) ->andWhere(self::$table . '.billing_bill_performance_date', '>=', $start) @@ -116,7 +116,7 @@ final class SalesBillMapper extends BillMapper public static function getAvgSalesPriceByItemId(int $id, \DateTime $start, \DateTime $end) : Money { $query = new Builder(self::$db); - $result = $query->select('SUM(billing_bill_element_single_salesprice_net)', 'COUNT(billing_bill_element_total_salesprice_net)') + $result = $query->select('SUM(billing_bill_element_single_netsalesprice)', 'COUNT(billing_bill_element_total_netsalesprice)') ->from(self::$table) ->leftJoin(BillElementMapper::getTable()) ->on(self::$table . '.billing_bill_id', '=', BillElementMapper::getTable() . '.billing_bill_element_bill') @@ -192,7 +192,7 @@ final class SalesBillMapper extends BillMapper // @todo: limit is not working correctly... only returns / 2 or something like that?. Maybe because bills arent unique? - $query ??= self::getQuery(null, [], RelationType::ALL, $depth); + $query = self::getQuery(null, [], RelationType::ALL, $depth); $query->leftJoin(BillElementMapper::getTable(), BillElementMapper::getTable() . '_d' . $depth) ->on(self::$table . '_d' . $depth . '.billing_bill_id', '=', BillElementMapper::getTable() . '_d' . $depth . '.billing_bill_element_bill') ->where(BillElementMapper::getTable() . '_d' . $depth . '.billing_bill_element_item', '=', $id) @@ -216,7 +216,7 @@ final class SalesBillMapper extends BillMapper // @todo: limit is not working correctly... only returns / 2 or something like that?. Maybe because bills arent unique? - $query ??= self::getQuery(null, [], RelationType::ALL, $depth); + $query = self::getQuery(null, [], RelationType::ALL, $depth); $query->where(self::$table . '_d' . $depth . '.billing_bill_client', '=', $id) ->limit($limit); @@ -236,8 +236,8 @@ final class SalesBillMapper extends BillMapper { $depth = 3; - $query ??= ClientMapper::getQuery(null, [], RelationType::ALL, $depth); - $query->selectAs('SUM(billing_bill_element_total_salesprice_net)', 'net_sales') + $query = ClientMapper::getQuery(null, [], RelationType::ALL, $depth); + $query->selectAs('SUM(billing_bill_element_total_netsalesprice)', 'net_sales') ->leftJoin(self::$table, self::$table . '_d' . $depth) ->on(ClientMapper::getTable() . '_d' . $depth . '.clientmgmt_client_id', '=', self::$table . '_d' . $depth . '.billing_bill_client') ->leftJoin(BillElementMapper::getTable(), BillElementMapper::getTable() . '_d' . $depth) @@ -264,7 +264,7 @@ final class SalesBillMapper extends BillMapper // @todo: limit is not working correctly... only returns / 2 or something like that?. Maybe because bills arent unique? - $query ??= self::getQuery(null, [], RelationType::ALL, $depth); + $query = self::getQuery(null, [], RelationType::ALL, $depth); $query->leftJoin(BillElementMapper::getTable(), BillElementMapper::getTable() . '_d' . $depth) ->on(self::$table . '_d' . $depth . '.billing_bill_id', '=', BillElementMapper::getTable() . '_d' . $depth . '.billing_bill_element_bill') ->where(BillElementMapper::getTable() . '_d' . $depth . '.billing_bill_element_item', '=', $id) @@ -288,7 +288,7 @@ final class SalesBillMapper extends BillMapper // @todo: limit is not working correctly... only returns / 2 or something like that?. Maybe because bills arent unique? - $query ??= BillElementMapper::getQuery(null, [], RelationType::ALL, $depth); + $query = BillElementMapper::getQuery(null, [], RelationType::ALL, $depth); $query->leftJoin(self::$table, self::$table . '_d' . $depth) ->on(BillElementMapper::getTable() . '_d' . $depth . '.billing_bill_element_bill', '=', self::$table . '_d' . $depth . '.billing_bill_id') ->where(self::$table . '_d' . $depth . '.billing_bill_client', '=', $client) @@ -310,7 +310,7 @@ final class SalesBillMapper extends BillMapper { $query = new Builder(self::$db); $result = $query->select(CountryMapper::getTable() . '.country_region') - ->selectAs('SUM(billing_bill_element_total_salesprice_net)', 'net_sales') + ->selectAs('SUM(billing_bill_element_total_netsalesprice)', 'net_sales') ->from(self::$table) ->leftJoin(BillElementMapper::getTable()) ->on(self::$table . '.billing_bill_id', '=', BillElementMapper::getTable() . '.billing_bill_element_bill') @@ -333,7 +333,7 @@ final class SalesBillMapper extends BillMapper { $query = new Builder(self::$db); $result = $query->select(CountryMapper::getTable() . '.country_code2') - ->selectAs('SUM(billing_bill_element_total_salesprice_net)', 'net_sales') + ->selectAs('SUM(billing_bill_element_total_netsalesprice)', 'net_sales') ->from(self::$table) ->leftJoin(BillElementMapper::getTable()) ->on(self::$table . '.billing_bill_id', '=', BillElementMapper::getTable() . '.billing_bill_element_bill') @@ -357,8 +357,8 @@ final class SalesBillMapper extends BillMapper public static function getItemMonthlySalesCosts(int $id, \DateTime $start, \DateTime $end) : array { $query = new Builder(self::$db); - $result = $query->selectAs('SUM(billing_bill_element_total_salesprice_net)', 'net_sales') - ->selectAs('SUM(billing_bill_element_total_purchaseprice_net)', 'net_costs') + $result = $query->selectAs('SUM(billing_bill_element_total_netsalesprice)', 'net_sales') + ->selectAs('SUM(billing_bill_element_total_netpurchaseprice)', 'net_costs') ->selectAs('YEAR(billing_bill_performance_date)', 'year') ->selectAs('MONTH(billing_bill_performance_date)', 'month') ->from(self::$table) @@ -381,8 +381,8 @@ final class SalesBillMapper extends BillMapper public static function getClientMonthlySalesCosts(int $id, \DateTime $start, \DateTime $end) : array { $query = new Builder(self::$db); - $result = $query->selectAs('SUM(billing_bill_net)', 'net_sales') - ->selectAs('SUM(billing_bill_costs)', 'net_costs') + $result = $query->selectAs('SUM(billing_bill_netsales)', 'net_sales') + ->selectAs('SUM(billing_bill_netcosts)', 'net_costs') ->selectAs('YEAR(billing_bill_performance_date)', 'year') ->selectAs('MONTH(billing_bill_performance_date)', 'month') ->from(self::$table) diff --git a/Theme/Backend/sales-bill-list.tpl.php b/Theme/Backend/sales-bill-list.tpl.php index 8eb7e66..61673cb 100755 --- a/Theme/Backend/sales-bill-list.tpl.php +++ b/Theme/Backend/sales-bill-list.tpl.php @@ -181,8 +181,8 @@ echo $this->getData('nav')->render(); ?> billZip; ?> billCity; ?> billCountry; ?> - net->getCurrency(); ?> - profit->getCurrency(); ?> + netSales->getCurrency(); ?> + netProfit->getCurrency(); ?> createdAt->format('Y-m-d'); ?> diff --git a/tests/Models/BillTest.php b/tests/Models/BillTest.php index 34420e6..7602873 100644 --- a/tests/Models/BillTest.php +++ b/tests/Models/BillTest.php @@ -70,10 +70,14 @@ final class BillTest extends \PHPUnit\Framework\TestCase self::assertEquals('', $this->bill->billZip); self::assertEquals('', $this->bill->billCountry); - self::assertInstanceOf('\phpOMS\Localization\Money', $this->bill->net); - self::assertInstanceOf('\phpOMS\Localization\Money', $this->bill->gross); - self::assertInstanceOf('\phpOMS\Localization\Money', $this->bill->costs); - self::assertInstanceOf('\phpOMS\Localization\Money', $this->bill->profit); + self::assertInstanceOf('\phpOMS\Localization\Money', $this->bill->netSales); + self::assertInstanceOf('\phpOMS\Localization\Money', $this->bill->grossSales); + + self::assertInstanceOf('\phpOMS\Localization\Money', $this->bill->netProfit); + self::assertInstanceOf('\phpOMS\Localization\Money', $this->bill->grossProfit); + + self::assertInstanceOf('\phpOMS\Localization\Money', $this->bill->netCosts); + self::assertInstanceOf('\phpOMS\Localization\Money', $this->bill->grossCosts); self::assertEquals(0, $this->bill->payment); self::assertEquals('', $this->bill->paymentText); @@ -89,7 +93,7 @@ final class BillTest extends \PHPUnit\Framework\TestCase */ public function testNumberRendering() : void { - $this->bill->number = '{y}{m}{d}-{id}'; + $this->bill->numberFormat = '{y}{m}{d}-{id}'; self::assertEquals(\date('Y') . \date('m') . \date('d') . '-0', $this->bill->getNumber()); } @@ -171,6 +175,7 @@ final class BillTest extends \PHPUnit\Framework\TestCase public function testSerialize() : void { $this->bill->number = '123456'; + $this->bill->numberFormat = '{y}'; $this->bill->type = 2; $this->bill->shipTo = 'To'; $this->bill->shipFAO = 'FAO'; @@ -189,6 +194,7 @@ final class BillTest extends \PHPUnit\Framework\TestCase [ 'id' => 0, 'number' => '123456', + 'numberFormat' => '{y}', 'type' => 2, 'shipTo' => 'To', 'shipFAO' => 'FAO',