diff --git a/app/Factory/InvoiceFactory.php b/app/Factory/InvoiceFactory.php new file mode 100644 index 000000000000..428b5a2b7765 --- /dev/null +++ b/app/Factory/InvoiceFactory.php @@ -0,0 +1,27 @@ +qty = 0; + $item->cost = 0; + $item->product_key = ''; + $item->notes = ''; + $item->discount = 0; + $item->is_amount_discount = true; + $item->tax_name1 = ''; + $item->tax_rate1 = 0; + $item->tax_name2 = ''; + $item->tax_rate2 = 0; + $item->sort_id = 0; + $item->line_total = 0; + $item->invoice_item_type_id = 0; + + return $item; + } +} + diff --git a/app/Helpers/Invoice/InvoiceCalc.php b/app/Helpers/Invoice/InvoiceCalc.php index ca419b587597..643078b61f30 100644 --- a/app/Helpers/Invoice/InvoiceCalc.php +++ b/app/Helpers/Invoice/InvoiceCalc.php @@ -56,12 +56,20 @@ class InvoiceCalc { $this->calcLineItems() ->calcDiscount() - ->sumCustomValues() - ->calcBalance(); + ->calcCustomValues() + ->calcBalance() + ->calcPartial(); return $this; } + private function calcPartial() + { + if ( !$this->invoice->id && isset($this->invoice->partial) ) { + $this->invoice->partial = max(0, min($this->formatValue($this->invoice->partial, 2), $this->invoice->balance)); + } + } + private function calcDiscount() { if ($this->invoice->discount != 0) { @@ -81,7 +89,15 @@ class InvoiceCalc return $this; } - private function sumBalance() + + /** + * Calculates the balance. + * + * //todo need to understand this better + * + * @return self The balance. + */ + private function calcBalance() { if(isset($this->invoice->id) && $this->invoice->id >= 1) @@ -95,7 +111,7 @@ class InvoiceCalc } - private function sumCustomValues() + private function calcCustomValues() { $this->total += $this->getSubTotal(); diff --git a/database/migrations/2014_10_13_000000_create_users_table.php b/database/migrations/2014_10_13_000000_create_users_table.php index 434bb6eb1dbf..104f5088f010 100644 --- a/database/migrations/2014_10_13_000000_create_users_table.php +++ b/database/migrations/2014_10_13_000000_create_users_table.php @@ -369,7 +369,6 @@ class CreateUsersTable extends Migration $t->string('custom_value3')->nullable(); $t->string('custom_value4')->nullable(); - $t->decimal('total', 13,2); $t->decimal('amount', 13, 2); $t->decimal('balance', 13, 2); $t->decimal('partial', 13, 2)->nullable(); diff --git a/tests/Unit/InvoiceTest.php b/tests/Unit/InvoiceTest.php new file mode 100644 index 000000000000..25e54a146fbf --- /dev/null +++ b/tests/Unit/InvoiceTest.php @@ -0,0 +1,21 @@ +