From a2a0e6738e8698688712a975eb5eb7b51a38f113 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 14 May 2019 20:27:47 +1000 Subject: [PATCH] Working on adding transactions to the company ledger --- app/Helpers/Invoice/InvoiceCalc.php | 20 ++++++++++++++++--- app/Jobs/Invoice/ApplyPaymentToInvoice.php | 2 +- app/Jobs/Invoice/MarkPaid.php | 5 +---- .../2014_10_13_000000_create_users_table.php | 3 +-- tests/Integration/MarkPaidInvoiceTest.php | 9 +-------- tests/MockAccountData.php | 5 ++--- 6 files changed, 23 insertions(+), 21 deletions(-) diff --git a/app/Helpers/Invoice/InvoiceCalc.php b/app/Helpers/Invoice/InvoiceCalc.php index 53df6de41e3a..7742e7460e7d 100644 --- a/app/Helpers/Invoice/InvoiceCalc.php +++ b/app/Helpers/Invoice/InvoiceCalc.php @@ -303,20 +303,34 @@ class InvoiceCalc public function getInvoice() { - //todo build invoice values here and return Invoice - + //Build invoice values here and return Invoice + $this->setCalculatedAttributes(); + return $this->invoice; } /** - * Build $this->invoice variable after + * Build $this->invoice variables after * calculations have been performed. */ private function setCalculatedAttributes() { + /* If amount != balance then some money has been paid on the invoice, need to subtract this difference from the total to set the new balance */ + if($this->invoice->amount != $this->invoice->balance) + { + $paid_to_date = $this->invoice->amount - $this->invoice->balance; + + $this->invoice->balance = $this->getTotal() - $paid_to_date; + } + else + $this->invoice->balance = $this->getTotal(); + + /* Set new calculated total */ + $this->invoice->amount = $this->getTotal(); } + /* private function setDiscount($amount, $discount, $is_amount_discount) { diff --git a/app/Jobs/Invoice/ApplyPaymentToInvoice.php b/app/Jobs/Invoice/ApplyPaymentToInvoice.php index 30c323b7fb1b..af4b47f9d6cb 100644 --- a/app/Jobs/Invoice/ApplyPaymentToInvoice.php +++ b/app/Jobs/Invoice/ApplyPaymentToInvoice.php @@ -53,7 +53,7 @@ class ApplyPaymentToInvoice implements ShouldQueue public function handle() { - /* The amoun we are adjusting the invoice by*/ + /* The amount we are adjusting the invoice by*/ $adjustment = $this->payment->amount * -1; /* Calculate if the amount paid is less than the partial value. diff --git a/app/Jobs/Invoice/MarkPaid.php b/app/Jobs/Invoice/MarkPaid.php index ab0558ed93a7..6ff3d6073ea2 100644 --- a/app/Jobs/Invoice/MarkPaid.php +++ b/app/Jobs/Invoice/MarkPaid.php @@ -61,10 +61,7 @@ class MarkPaid implements ShouldQueue /* Create a payment relationship to the invoice entity */ $payment->invoices()->save($this->invoice); -//Log::error($payment); -//Log::error('num of payment invoice relations '.count($payment->invoices)); -//Log::error(print_r($payment->invoices,1)); - /* Need to engineer the ability to pass an array of invoices to the activity handler*/ + $data = [ 'payment_id' => $payment->id, 'invoice_ids' => [ 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 0e71a6c91e91..c34d0b07f341 100644 --- a/database/migrations/2014_10_13_000000_create_users_table.php +++ b/database/migrations/2014_10_13_000000_create_users_table.php @@ -856,8 +856,7 @@ class CreateUsersTable extends Migration $table->unsignedInteger('user_id')->nullable(); $table->decimal('adjustment', 13, 2)->nullable(); - $table->decimal('balance', 13, 2)->nullable(); - $table->decimal('balance_carried_forward', 13, 2)->nullable(); + $table->decimal('balance', 13, 2)->nullable(); //this is the clients balance carried forward $table->text('notes'); $table->unsignedInteger('company_ledger_id'); diff --git a/tests/Integration/MarkPaidInvoiceTest.php b/tests/Integration/MarkPaidInvoiceTest.php index 36e762afd75e..034c39e143e9 100644 --- a/tests/Integration/MarkPaidInvoiceTest.php +++ b/tests/Integration/MarkPaidInvoiceTest.php @@ -10,9 +10,6 @@ use App\Models\Payment; use App\Models\User; use Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase; use Illuminate\Foundation\Testing\DatabaseTransactions; -use Illuminate\Support\Facades\DB; -use Illuminate\Support\Facades\Hash; -use Illuminate\Support\Facades\Log; use Tests\MockAccountData; use Tests\TestCase; @@ -47,17 +44,13 @@ class MarkPaidInvoiceTest extends TestCase $this->assertEquals(1, count($invoice->payments)); - foreach($invoice->payments as $payment) { - Log::error($payment); + //Log::error($payment); $this->assertEquals(10, $payment->amount); } - - $this->assertEquals(Invoice::STATUS_PAID, $invoice->status_id); - $this->assertEquals(0.00, $invoice->balance); } diff --git a/tests/MockAccountData.php b/tests/MockAccountData.php index 3218d4fa42a9..a64d167506c2 100644 --- a/tests/MockAccountData.php +++ b/tests/MockAccountData.php @@ -78,9 +78,8 @@ trait MockAccountData $this->invoice_calc = new InvoiceCalc($this->invoice, $this->settings); $this->invoice_calc->build(); - $this->invoice->amount = $this->invoice_calc->getTotal(); - $this->invoice->balance = $this->invoice_calc->getTotal(); - + $this->invoice = $this->invoice_calc->getInvoice(); + $this->invoice->save(); $this->invoice->fresh();