1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00

Merge pull request #4839 from turbo124/v5-develop

Allow negative taxes
This commit is contained in:
David Bomba 2021-02-07 18:39:13 +11:00 committed by GitHub
commit 49aa2c8409
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 67 additions and 39 deletions

View File

@ -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));

View File

@ -119,25 +119,23 @@ class InvoiceItemSumInclusive
$item_tax += $this->formatValue($item_tax_rate1_total, $this->currency->precision);
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->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) {
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;

View File

@ -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);

View File

@ -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();
// }
// });
// }
}

View File

@ -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) {
if($payment->status_id != Payment::STATUS_CANCELLED || !$payment->is_deleted)
{
$inv->pivot->amount = $invoice_totals;
$inv->pivot->refunded = $refund_totals;
$inv->pivot->save();
$inv->paid_to_date += $invoice_totals;
if($inv->balance > 0)
$inv->balance -= $invoice_totals;
$inv->balance = max(0, $inv->balance);
if($inv->status_id == Invoice::STATUS_PAID)
$inv->balance = 0;
// if($inv->balance > 0)
// $inv->balance = max(0, $inv->balance);
$inv->save();
}
});
}

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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);