From aad22f607efae528482cf2f4eb43b08ed863a9a0 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 29 Oct 2019 22:44:54 +1100 Subject: [PATCH] set defaults for line items (#3029) --- app/DataMapper/BaseSettings.php | 2 +- app/DataMapper/InvoiceItem.php | 59 ++++++++++++++++++-------- app/Helpers/Invoice/InvoiceItemSum.php | 25 ++++++++++- 3 files changed, 67 insertions(+), 19 deletions(-) diff --git a/app/DataMapper/BaseSettings.php b/app/DataMapper/BaseSettings.php index 479f0cc349ec..ae767a7cc4ff 100644 --- a/app/DataMapper/BaseSettings.php +++ b/app/DataMapper/BaseSettings.php @@ -31,7 +31,7 @@ class BaseSettings return $obj; } - protected static function castAttribute($key, $value) + public static function castAttribute($key, $value) { switch ($key) { diff --git a/app/DataMapper/InvoiceItem.php b/app/DataMapper/InvoiceItem.php index 59b0edf1ba88..e2fcb21f0c6c 100644 --- a/app/DataMapper/InvoiceItem.php +++ b/app/DataMapper/InvoiceItem.php @@ -14,39 +14,64 @@ namespace App\DataMapper; class InvoiceItem { - private $quantity; + public $quantity = 0; - private $cost; + public $cost = 0; - private $product_key; + public $product_key = ''; - private $notes; + public $notes = ''; - private $discount; + public $discount = 0; - private $is_amount_discount; + public $is_amount_discount = false; - private $tax_name1; + public $tax_name1 = ''; - private $tax_rate1; + public $tax_rate1 = 0; - private $tax_name2; + public $tax_name2 = ''; - private $tax_rate2; + public $tax_rate2 = 0; - private $sort_id; + public $tax_name3 = ''; - private $line_total; + public $tax_rate3 = 0; - private $date; + public $sort_id = 0; - private $custom_value1; + public $line_total = 0; - private $custom_value2; + public $date = ''; - private $custom_value3; + public $custom_value1 = ''; - private $custom_value4; + public $custom_value2 = ''; + + public $custom_value3 = ''; + + public $custom_value4 = ''; + public static $casts = [ + 'quantity' => 'float', + 'cost' => 'float', + 'product_key' => 'string', + 'notes' => 'string', + 'discount' => 'float', + 'is_amount_discount' => 'bool', + 'tax_name1' => 'string', + 'tax_name2' => 'string', + 'tax_name3' => 'string', + 'tax_rate1' => 'float', + 'tax_rate2' => 'float', + 'tax_rate3' => 'float', + 'sort_id' => 'int', + 'line_total' => 'float', + 'date' => 'string', + 'custom_value1' => 'string', + 'custom_value2' => 'string', + 'custom_value3' => 'string', + 'custom_value4' => 'string', + ]; } diff --git a/app/Helpers/Invoice/InvoiceItemSum.php b/app/Helpers/Invoice/InvoiceItemSum.php index 15c431213fea..2dc8be53a3a1 100644 --- a/app/Helpers/Invoice/InvoiceItemSum.php +++ b/app/Helpers/Invoice/InvoiceItemSum.php @@ -11,6 +11,8 @@ namespace App\Helpers\Invoice; +use App\DataMapper\BaseSettings; +use App\DataMapper\InvoiceItem; use App\Helpers\Invoice\Discounter; use App\Helpers\Invoice\Taxer; use App\Models\Invoice; @@ -77,7 +79,8 @@ class InvoiceItemSum { foreach($this->invoice->line_items as $this->item) { - $this->sumLineItem() + $this->cleanLineItem() + ->sumLineItem() ->setDiscount() ->calcTaxes() ->push(); @@ -266,4 +269,24 @@ class InvoiceItemSum $this->setTotalTaxes($item_tax); } + + /** + * Sets default values for the line_items + * @return $this + */ + private function cleanLineItem() + { + $invoice_item = (object)get_class_vars(InvoiceItem::class); + unset($invoice_item->casts); + + foreach($invoice_item as $key => $value) + { + + if(!property_exists($this->item, $key)) + $this->item->{$key} = BaseSettings::castAttribute(InvoiceItem::$casts[$key], $value); + } + + + return $this; + } } \ No newline at end of file