diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index 3024d424655a..fabfca0982e7 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -321,7 +321,7 @@ class CheckData extends Command Client::withTrashed()->cursor()->each(function ($client) use ($wrong_paid_to_dates, $credit_total_applied) { $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_refund = $invoice->payments->sum('pivot.refunded'); @@ -356,7 +356,7 @@ class CheckData extends Command $wrong_paid_to_dates = 0; 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_refund = $invoice->payments->sum('pivot.refunded'); $total_credit = $invoice->credits->sum('amount'); diff --git a/app/Helpers/Invoice/InvoiceSum.php b/app/Helpers/Invoice/InvoiceSum.php index 69819c4a9bb8..4ed998ba4b4a 100644 --- a/app/Helpers/Invoice/InvoiceSum.php +++ b/app/Helpers/Invoice/InvoiceSum.php @@ -207,14 +207,17 @@ class InvoiceSum 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->formatValue($this->getTotal(), $this->invoice->client->currency()->precision) - $paid_to_date; - } else { - $this->invoice->balance = $this->formatValue($this->getTotal(), $this->invoice->client->currency()->precision); + if($this->invoice->status_id != Invoice::STATUS_DRAFT) + { + 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 */ $this->invoice->amount = $this->formatValue($this->getTotal(), $this->invoice->client->currency()->precision); diff --git a/app/Http/Requests/Invoice/StoreInvoiceRequest.php b/app/Http/Requests/Invoice/StoreInvoiceRequest.php index 893c659251d9..628de0aec37e 100644 --- a/app/Http/Requests/Invoice/StoreInvoiceRequest.php +++ b/app/Http/Requests/Invoice/StoreInvoiceRequest.php @@ -69,7 +69,9 @@ class StoreInvoiceRequest extends Request $input = $this->decodePrimaryKeys($input); $input['line_items'] = isset($input['line_items']) ? $this->cleanItems($input['line_items']) : []; - + $input['amount'] = 0; + $input['balance'] = 0; + $this->replace($input); } } diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 36a884e1d90d..7b0cc2987be4 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -158,6 +158,7 @@ class BaseRepository */ protected function alternativeSave($data, $model) { + $class = new ReflectionClass($model); if (array_key_exists('client_id', $data)) { diff --git a/app/Services/Invoice/MarkSent.php b/app/Services/Invoice/MarkSent.php index c3bd7c903d4d..ed8c8daaa7f1 100644 --- a/app/Services/Invoice/MarkSent.php +++ b/app/Services/Invoice/MarkSent.php @@ -46,6 +46,7 @@ class MarkSent extends AbstractService ->setStatus(Invoice::STATUS_SENT) ->applyNumber() ->setDueDate() + ->updateBalance($this->invoice->amount) ->save(); $this->client->service()->updateBalance($this->invoice->balance)->save(); diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index c6e5cca7d187..169a8b1280ab 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -304,6 +304,8 @@ class HtmlEngine $data['$product.tax_name2'] = ['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.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.discount'] = ['value' => '', 'label' => ctrans('texts.discount')]; diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index 1b7e2e375f31..d303698d80cd 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -618,12 +618,14 @@ trait MakesInvoiceValues $data[$key][$table_type.'.product_key'] = $item->product_key; $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_value2'] = $item->custom_value2; $data[$key][$table_type.'.custom_value3'] = $item->custom_value3; $data[$key][$table_type.'.custom_value4'] = $item->custom_value4; $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.'.line_total'] = Number::formatMoney($item->line_total, $this->client);