From 4c721e5027dbcc95b8e5d0701b0f087c82b918e4 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 5 Feb 2021 07:47:16 +1100 Subject: [PATCH 1/8] fixes for credits --- app/Jobs/Util/Import.php | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index fdb5e32890..04bf3f62b2 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -205,7 +205,7 @@ class Import implements ShouldQueue $this->setInitialCompanyLedgerBalances(); - $this->fixClientBalances(); + // $this->fixClientBalances(); Mail::to($this->user) ->send(new MigrationCompleted($this->company)); @@ -779,6 +779,14 @@ class Import implements ShouldQueue CreditFactory::create($this->company->id, $modified['user_id']) ); + //remove credit balance from ledger + if($credit->balance > 0 && $credit->client->balance > 0){ + $client = $credit->client; + $client->balance -= $credit->balance; + $client->save(); + } + + $key = "credits_{$resource['id']}"; $this->ids['credits'][$key] = [ @@ -1434,19 +1442,19 @@ class Import implements ShouldQueue /* In V4 we use negative invoices (credits) and add then into the client balance. In V5, these sit off ledger and are applied later. This next section will check for credit balances and reduce the client balance so that the V5 balances are correct */ - private function fixClientBalances() - { + // private function fixClientBalances() + // { - Client::cursor()->each(function ($client) { + // Client::cursor()->each(function ($client) { - $credit_balance = $client->credits->where('is_deleted', false)->sum('balance'); + // $credit_balance = $client->credits->where('is_deleted', false)->sum('balance'); - if($credit_balance > 0){ - $client->balance += $credit_balance; - $client->save(); - } + // if($credit_balance > 0){ + // $client->balance += $credit_balance; + // $client->save(); + // } - }); + // }); - } + // } } From 55839d66ed9b370e5f016b230e003aee84266f8d Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 5 Feb 2021 16:02:38 +1100 Subject: [PATCH 2/8] Auto archive invoice - Mark Paid --- app/Jobs/Invoice/InvoiceWorkflowSettings.php | 5 +++-- app/Services/Invoice/MarkPaid.php | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/Jobs/Invoice/InvoiceWorkflowSettings.php b/app/Jobs/Invoice/InvoiceWorkflowSettings.php index 579a7620f3..593ffbb378 100644 --- a/app/Jobs/Invoice/InvoiceWorkflowSettings.php +++ b/app/Jobs/Invoice/InvoiceWorkflowSettings.php @@ -35,10 +35,9 @@ class InvoiceWorkflowSettings implements ShouldQueue * @param Invoice $invoice * @param Client|null $client */ - public function __construct(Invoice $invoice, Client $client = null) + public function __construct(Invoice $invoice) { $this->invoice = $invoice; - $this->client = $client ?? $invoice->client; $this->base_repository = new BaseRepository(); } @@ -49,6 +48,8 @@ class InvoiceWorkflowSettings implements ShouldQueue */ public function handle() { + $this->client = $this->invoice->client; + if ($this->client->getSetting('auto_archive_invoice')) { /* Throws: Payment amount xxx does not match invoice totals. */ $this->base_repository->archive($this->invoice); diff --git a/app/Services/Invoice/MarkPaid.php b/app/Services/Invoice/MarkPaid.php index 55c7e18016..e3f2c1c5bb 100644 --- a/app/Services/Invoice/MarkPaid.php +++ b/app/Services/Invoice/MarkPaid.php @@ -14,6 +14,7 @@ namespace App\Services\Invoice; use App\Events\Invoice\InvoiceWasPaid; use App\Events\Payment\PaymentWasCreated; use App\Factory\PaymentFactory; +use App\Jobs\Invoice\InvoiceWorkflowSettings; use App\Jobs\Payment\EmailPayment; use App\Models\Invoice; use App\Models\Payment; @@ -90,6 +91,8 @@ class MarkPaid extends AbstractService ->updatePaidToDate($payment->amount) ->save(); + InvoiceWorkflowSettings::dispatchNow($this->invoice); + return $this->invoice; } } From 47eabf5434461381de405330aedacd4cff5fe84c Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 5 Feb 2021 16:03:34 +1100 Subject: [PATCH 3/8] Invoice Actions --- app/Services/Invoice/InvoiceService.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 543d034fea..b45e5eb19b 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -12,6 +12,7 @@ namespace App\Services\Invoice; use App\Jobs\Entity\CreateEntityPdf; +use App\Jobs\Invoice\InvoiceWorkflowSettings; use App\Jobs\Util\UnlinkFile; use App\Models\CompanyGateway; use App\Models\Expense; @@ -239,6 +240,9 @@ class InvoiceService public function updateStatus() { if ((int)$this->invoice->balance == 0) { + + InvoiceWorkflowSettings::dispatch($this->invoice); + $this->setStatus(Invoice::STATUS_PAID); } From d7677d1d0cb874e8eb1a09a2310f456b030f5128 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 5 Feb 2021 21:41:34 +1100 Subject: [PATCH 4/8] working on check data --- .../Migration/PaymentMigrationRepository.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/Repositories/Migration/PaymentMigrationRepository.php b/app/Repositories/Migration/PaymentMigrationRepository.php index 470e2401d7..c05c85c19a 100644 --- a/app/Repositories/Migration/PaymentMigrationRepository.php +++ b/app/Repositories/Migration/PaymentMigrationRepository.php @@ -120,11 +120,15 @@ class PaymentMigrationRepository extends BaseRepository $inv->pivot->save(); $inv->paid_to_date += $invoice_totals; + $inv->balance -= $invoice_totals; - if($inv->balance > 0) - $inv->balance -= $invoice_totals; + if($inv->status_id == Invoice::STATUS_PAID) + $inv->balance = 0; - $inv->balance = max(0, $inv->balance); + // if($inv->balance > 0) + // + + // $inv->balance = max(0, $inv->balance); $inv->save(); From 59ae0d3219656fc999b653040456fea5a67dd6be Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 6 Feb 2021 09:31:26 +1100 Subject: [PATCH 5/8] Fixes for migrations - do not applied cancelled payments to invoices --- .../Invoice/InvoiceItemSumInclusive.php | 7 +++- .../Migration/PaymentMigrationRepository.php | 36 +++++++++++-------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/app/Helpers/Invoice/InvoiceItemSumInclusive.php b/app/Helpers/Invoice/InvoiceItemSumInclusive.php index 818f228ab5..9a0d890cb3 100644 --- a/app/Helpers/Invoice/InvoiceItemSumInclusive.php +++ b/app/Helpers/Invoice/InvoiceItemSumInclusive.php @@ -225,7 +225,12 @@ class InvoiceItemSumInclusive $item_tax = 0; foreach ($this->line_items as $this->item) { - $amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount / $this->sub_total)); + + if($this->sub_total == 0) + $amount = $this->item->line_total; + else + $amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount / $this->sub_total)); + $item_tax_rate1_total = $this->calcInclusiveLineTax($this->item->tax_rate1, $amount); $item_tax += $item_tax_rate1_total; diff --git a/app/Repositories/Migration/PaymentMigrationRepository.php b/app/Repositories/Migration/PaymentMigrationRepository.php index c05c85c19a..13b305a065 100644 --- a/app/Repositories/Migration/PaymentMigrationRepository.php +++ b/app/Repositories/Migration/PaymentMigrationRepository.php @@ -92,6 +92,10 @@ class PaymentMigrationRepository extends BaseRepository } $payment->status_id = $data['status_id']; + + if($payment->status_id == Payment::STATUS_CANCELLED) + $payment->is_deleted = true; + $payment->deleted_at = $data['deleted_at'] ?: null; $payment->save(); @@ -113,24 +117,28 @@ class PaymentMigrationRepository extends BaseRepository $payment->invoices()->saveMany($invoices); - $payment->invoices->each(function ($inv) use ($invoice_totals, $refund_totals) { + $payment->invoices->each(function ($inv) use ($invoice_totals, $refund_totals, $payment) { - $inv->pivot->amount = $invoice_totals; - $inv->pivot->refunded = $refund_totals; - $inv->pivot->save(); - $inv->paid_to_date += $invoice_totals; - $inv->balance -= $invoice_totals; + if($payment->status_id != Payment::STATUS_CANCELLED) + { + $inv->pivot->amount = $invoice_totals; + $inv->pivot->refunded = $refund_totals; + $inv->pivot->save(); - if($inv->status_id == Invoice::STATUS_PAID) - $inv->balance = 0; - - // if($inv->balance > 0) - // - - // $inv->balance = max(0, $inv->balance); + $inv->paid_to_date += $invoice_totals; + $inv->balance -= $invoice_totals; - $inv->save(); + if($inv->status_id == Invoice::STATUS_PAID) + $inv->balance = 0; + + // if($inv->balance > 0) + // + + // $inv->balance = max(0, $inv->balance); + + $inv->save(); + } }); } From 773e131afc0bcb97002d9353f1f4fad44ef760b6 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 6 Feb 2021 10:39:40 +1100 Subject: [PATCH 6/8] Fixes for negative taxes --- app/Helpers/Invoice/InvoiceItemSum.php | 9 +++------ app/Helpers/Invoice/InvoiceItemSumInclusive.php | 9 +++------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/app/Helpers/Invoice/InvoiceItemSum.php b/app/Helpers/Invoice/InvoiceItemSum.php index 12b9878b47..49b299d443 100644 --- a/app/Helpers/Invoice/InvoiceItemSum.php +++ b/app/Helpers/Invoice/InvoiceItemSum.php @@ -122,25 +122,22 @@ class InvoiceItemSum $item_tax += $item_tax_rate1_total; - if ($item_tax_rate1_total > 0) { $this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total); - } + $item_tax_rate2_total = $this->calcAmountLineTax($this->item->tax_rate2, $amount); $item_tax += $item_tax_rate2_total; - if ($item_tax_rate2_total > 0) { $this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total); - } + $item_tax_rate3_total = $this->calcAmountLineTax($this->item->tax_rate3, $amount); $item_tax += $item_tax_rate3_total; - if ($item_tax_rate3_total > 0) { $this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total); - } + $this->setTotalTaxes($this->formatValue($item_tax, $this->currency->precision)); diff --git a/app/Helpers/Invoice/InvoiceItemSumInclusive.php b/app/Helpers/Invoice/InvoiceItemSumInclusive.php index 9a0d890cb3..3646ce2605 100644 --- a/app/Helpers/Invoice/InvoiceItemSumInclusive.php +++ b/app/Helpers/Invoice/InvoiceItemSumInclusive.php @@ -119,25 +119,22 @@ class InvoiceItemSumInclusive $item_tax += $this->formatValue($item_tax_rate1_total, $this->currency->precision); - if ($item_tax_rate1_total > 0) { $this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total); - } + $item_tax_rate2_total = $this->calcInclusiveLineTax($this->item->tax_rate2, $amount); $item_tax += $this->formatValue($item_tax_rate2_total, $this->currency->precision); - if ($item_tax_rate2_total > 0) { $this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total); - } + $item_tax_rate3_total = $this->calcInclusiveLineTax($this->item->tax_rate3, $amount); $item_tax += $this->formatValue($item_tax_rate3_total, $this->currency->precision); - if ($item_tax_rate3_total > 0) { $this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total); - } + $this->setTotalTaxes($this->formatValue($item_tax, $this->currency->precision)); From 7c3a6766c021c44c67316dd95d123ed0c2b4c7d8 Mon Sep 17 00:00:00 2001 From: = Date: Sat, 6 Feb 2021 11:18:42 +1100 Subject: [PATCH 7/8] FIxes for Zero taxes --- app/Helpers/Invoice/InvoiceItemSum.php | 5 +++-- app/Helpers/Invoice/InvoiceItemSumInclusive.php | 7 ++++--- tests/Unit/InvoiceInclusiveTest.php | 1 + 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/Helpers/Invoice/InvoiceItemSum.php b/app/Helpers/Invoice/InvoiceItemSum.php index 49b299d443..6cb00c4b6b 100644 --- a/app/Helpers/Invoice/InvoiceItemSum.php +++ b/app/Helpers/Invoice/InvoiceItemSum.php @@ -122,20 +122,21 @@ class InvoiceItemSum $item_tax += $item_tax_rate1_total; + if($item_tax_rate1_total != 0) $this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total); - $item_tax_rate2_total = $this->calcAmountLineTax($this->item->tax_rate2, $amount); $item_tax += $item_tax_rate2_total; + if($item_tax_rate2_total != 0) $this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total); - $item_tax_rate3_total = $this->calcAmountLineTax($this->item->tax_rate3, $amount); $item_tax += $item_tax_rate3_total; + if($item_tax_rate3_total != 0) $this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total); diff --git a/app/Helpers/Invoice/InvoiceItemSumInclusive.php b/app/Helpers/Invoice/InvoiceItemSumInclusive.php index 3646ce2605..a04d4f229e 100644 --- a/app/Helpers/Invoice/InvoiceItemSumInclusive.php +++ b/app/Helpers/Invoice/InvoiceItemSumInclusive.php @@ -119,20 +119,21 @@ class InvoiceItemSumInclusive $item_tax += $this->formatValue($item_tax_rate1_total, $this->currency->precision); - $this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total); - + if($item_tax_rate1_total != 0) + $this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total); $item_tax_rate2_total = $this->calcInclusiveLineTax($this->item->tax_rate2, $amount); $item_tax += $this->formatValue($item_tax_rate2_total, $this->currency->precision); + if($item_tax_rate2_total != 0) $this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total); - $item_tax_rate3_total = $this->calcInclusiveLineTax($this->item->tax_rate3, $amount); $item_tax += $this->formatValue($item_tax_rate3_total, $this->currency->precision); + if($item_tax_rate3_total != 0) $this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total); diff --git a/tests/Unit/InvoiceInclusiveTest.php b/tests/Unit/InvoiceInclusiveTest.php index 5716649a2a..08f5a24e0c 100644 --- a/tests/Unit/InvoiceInclusiveTest.php +++ b/tests/Unit/InvoiceInclusiveTest.php @@ -275,6 +275,7 @@ class InvoiceInclusiveTest extends TestCase $this->assertEquals($this->invoice_calc->getSubTotal(), 19); $this->assertEquals($this->invoice_calc->getTotalDiscount(), 0.95); $this->assertEquals($this->invoice_calc->getTotalTaxes(), 4.92); + nlog($this->invoice_calc->getTaxMap()); $this->assertEquals(count($this->invoice_calc->getTaxMap()), 1); $this->assertEquals($this->invoice_calc->getTotal(), 18.05); $this->assertEquals($this->invoice_calc->getBalance(), 18.05); From a217b086b1046a95b6e779ba9aa446855569d784 Mon Sep 17 00:00:00 2001 From: = Date: Sat, 6 Feb 2021 15:30:01 +1100 Subject: [PATCH 8/8] Delete payment migration --- app/Repositories/Migration/PaymentMigrationRepository.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/Repositories/Migration/PaymentMigrationRepository.php b/app/Repositories/Migration/PaymentMigrationRepository.php index 13b305a065..fc66ebe670 100644 --- a/app/Repositories/Migration/PaymentMigrationRepository.php +++ b/app/Repositories/Migration/PaymentMigrationRepository.php @@ -120,7 +120,7 @@ class PaymentMigrationRepository extends BaseRepository $payment->invoices->each(function ($inv) use ($invoice_totals, $refund_totals, $payment) { - if($payment->status_id != Payment::STATUS_CANCELLED) + if($payment->status_id != Payment::STATUS_CANCELLED || !$payment->is_deleted) { $inv->pivot->amount = $invoice_totals; $inv->pivot->refunded = $refund_totals; @@ -133,8 +133,6 @@ class PaymentMigrationRepository extends BaseRepository $inv->balance = 0; // if($inv->balance > 0) - // - // $inv->balance = max(0, $inv->balance); $inv->save();