diff --git a/app/Helpers/Invoice/InvoiceItemSum.php b/app/Helpers/Invoice/InvoiceItemSum.php index 12b9878b47..6cb00c4b6b 100644 --- a/app/Helpers/Invoice/InvoiceItemSum.php +++ b/app/Helpers/Invoice/InvoiceItemSum.php @@ -122,25 +122,23 @@ class InvoiceItemSum $item_tax += $item_tax_rate1_total; - if ($item_tax_rate1_total > 0) { + 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) { + 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) { + 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 818f228ab5..a04d4f229e 100644 --- a/app/Helpers/Invoice/InvoiceItemSumInclusive.php +++ b/app/Helpers/Invoice/InvoiceItemSumInclusive.php @@ -119,25 +119,23 @@ 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); - } + 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) { + 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) { + 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)); @@ -225,7 +223,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/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/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(); + // } - }); + // }); - } + // } } diff --git a/app/Repositories/Migration/PaymentMigrationRepository.php b/app/Repositories/Migration/PaymentMigrationRepository.php index 470e2401d7..fc66ebe670 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,20 +117,26 @@ 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; + if($payment->status_id != Payment::STATUS_CANCELLED || !$payment->is_deleted) + { + $inv->pivot->amount = $invoice_totals; + $inv->pivot->refunded = $refund_totals; + $inv->pivot->save(); - if($inv->balance > 0) + $inv->paid_to_date += $invoice_totals; $inv->balance -= $invoice_totals; - - $inv->balance = max(0, $inv->balance); - $inv->save(); + if($inv->status_id == Invoice::STATUS_PAID) + $inv->balance = 0; + + // if($inv->balance > 0) + // $inv->balance = max(0, $inv->balance); + + $inv->save(); + } }); } 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); } 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; } } 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);