Fix edge cases with cloning invoice

This commit is contained in:
David Bomba 2020-11-15 19:24:57 +11:00
parent 8d62b704e6
commit a1119f9bbe
7 changed files with 20 additions and 9 deletions

View File

@ -321,7 +321,7 @@ class CheckData extends Command
Client::withTrashed()->cursor()->each(function ($client) use ($wrong_paid_to_dates, $credit_total_applied) { Client::withTrashed()->cursor()->each(function ($client) use ($wrong_paid_to_dates, $credit_total_applied) {
$total_invoice_payments = 0; $total_invoice_payments = 0;
foreach ($client->invoices as $invoice) { foreach ($client->invoices->where('is_deleted', false) as $invoice) {
$total_amount = $invoice->payments->sum('pivot.amount'); $total_amount = $invoice->payments->sum('pivot.amount');
$total_refund = $invoice->payments->sum('pivot.refunded'); $total_refund = $invoice->payments->sum('pivot.refunded');
@ -356,7 +356,7 @@ class CheckData extends Command
$wrong_paid_to_dates = 0; $wrong_paid_to_dates = 0;
Client::cursor()->each(function ($client) use ($wrong_balances) { Client::cursor()->each(function ($client) use ($wrong_balances) {
$client->invoices->where('is_deleted', false)->each(function ($invoice) use ($wrong_balances, $client) { $client->invoices->where('is_deleted', false)->whereIn('status_id', '!=', Invoice::STATUS_DRAFT)->each(function ($invoice) use ($wrong_balances, $client) {
$total_amount = $invoice->payments->sum('pivot.amount'); $total_amount = $invoice->payments->sum('pivot.amount');
$total_refund = $invoice->payments->sum('pivot.refunded'); $total_refund = $invoice->payments->sum('pivot.refunded');
$total_credit = $invoice->credits->sum('amount'); $total_credit = $invoice->credits->sum('amount');

View File

@ -207,14 +207,17 @@ class InvoiceSum
private function setCalculatedAttributes() 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 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->formatValue($this->getTotal(), $this->invoice->client->currency()->precision) - $paid_to_date; if($this->invoice->status_id != Invoice::STATUS_DRAFT)
} else { {
$this->invoice->balance = $this->formatValue($this->getTotal(), $this->invoice->client->currency()->precision); if ($this->invoice->amount != $this->invoice->balance) {
$paid_to_date = $this->invoice->amount - $this->invoice->balance;
$this->invoice->balance = $this->formatValue($this->getTotal(), $this->invoice->client->currency()->precision) - $paid_to_date;
} else {
$this->invoice->balance = $this->formatValue($this->getTotal(), $this->invoice->client->currency()->precision);
}
} }
/* Set new calculated total */ /* Set new calculated total */
$this->invoice->amount = $this->formatValue($this->getTotal(), $this->invoice->client->currency()->precision); $this->invoice->amount = $this->formatValue($this->getTotal(), $this->invoice->client->currency()->precision);

View File

@ -69,7 +69,9 @@ class StoreInvoiceRequest extends Request
$input = $this->decodePrimaryKeys($input); $input = $this->decodePrimaryKeys($input);
$input['line_items'] = isset($input['line_items']) ? $this->cleanItems($input['line_items']) : []; $input['line_items'] = isset($input['line_items']) ? $this->cleanItems($input['line_items']) : [];
$input['amount'] = 0;
$input['balance'] = 0;
$this->replace($input); $this->replace($input);
} }
} }

View File

@ -158,6 +158,7 @@ class BaseRepository
*/ */
protected function alternativeSave($data, $model) protected function alternativeSave($data, $model)
{ {
$class = new ReflectionClass($model); $class = new ReflectionClass($model);
if (array_key_exists('client_id', $data)) { if (array_key_exists('client_id', $data)) {

View File

@ -46,6 +46,7 @@ class MarkSent extends AbstractService
->setStatus(Invoice::STATUS_SENT) ->setStatus(Invoice::STATUS_SENT)
->applyNumber() ->applyNumber()
->setDueDate() ->setDueDate()
->updateBalance($this->invoice->amount)
->save(); ->save();
$this->client->service()->updateBalance($this->invoice->balance)->save(); $this->client->service()->updateBalance($this->invoice->balance)->save();

View File

@ -304,6 +304,8 @@ class HtmlEngine
$data['$product.tax_name2'] = ['value' => '', 'label' => ctrans('texts.tax')]; $data['$product.tax_name2'] = ['value' => '', 'label' => ctrans('texts.tax')];
$data['$product.tax_name3'] = ['value' => '', 'label' => ctrans('texts.tax')]; $data['$product.tax_name3'] = ['value' => '', 'label' => ctrans('texts.tax')];
$data['$product.line_total'] = ['value' => '', 'label' => ctrans('texts.line_total')]; $data['$product.line_total'] = ['value' => '', 'label' => ctrans('texts.line_total')];
$data['$product.description'] = ['value' => '', 'label' => ctrans('texts.description')];
$data['$product.unit_cost'] = ['value' => '', 'label' => ctrans('texts.unit_cost')];
$data['$task.date'] = ['value' => '', 'label' => ctrans('texts.date')]; $data['$task.date'] = ['value' => '', 'label' => ctrans('texts.date')];
$data['$task.discount'] = ['value' => '', 'label' => ctrans('texts.discount')]; $data['$task.discount'] = ['value' => '', 'label' => ctrans('texts.discount')];

View File

@ -618,12 +618,14 @@ trait MakesInvoiceValues
$data[$key][$table_type.'.product_key'] = $item->product_key; $data[$key][$table_type.'.product_key'] = $item->product_key;
$data[$key][$table_type.'.notes'] = $item->notes; $data[$key][$table_type.'.notes'] = $item->notes;
$data[$key][$table_type.'.description'] = $item->notes;
$data[$key][$table_type.'.custom_value1'] = $item->custom_value1; $data[$key][$table_type.'.custom_value1'] = $item->custom_value1;
$data[$key][$table_type.'.custom_value2'] = $item->custom_value2; $data[$key][$table_type.'.custom_value2'] = $item->custom_value2;
$data[$key][$table_type.'.custom_value3'] = $item->custom_value3; $data[$key][$table_type.'.custom_value3'] = $item->custom_value3;
$data[$key][$table_type.'.custom_value4'] = $item->custom_value4; $data[$key][$table_type.'.custom_value4'] = $item->custom_value4;
$data[$key][$table_type.'.quantity'] = $item->quantity; $data[$key][$table_type.'.quantity'] = $item->quantity;
$data[$key][$table_type.'.unit_cost'] = Number::formatMoney($item->cost, $this->client);
$data[$key][$table_type.'.cost'] = Number::formatMoney($item->cost, $this->client); $data[$key][$table_type.'.cost'] = Number::formatMoney($item->cost, $this->client);
$data[$key][$table_type.'.line_total'] = Number::formatMoney($item->line_total, $this->client); $data[$key][$table_type.'.line_total'] = Number::formatMoney($item->line_total, $this->client);