diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index 3024d424655a..e9fec0c084d0 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -321,9 +321,9 @@ 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) { - $total_amount = $invoice->payments->sum('pivot.amount'); - $total_refund = $invoice->payments->sum('pivot.refunded'); + foreach ($client->invoices->where('is_deleted', false) as $invoice) { + $total_amount = $invoice->payments->whereNull('deleted_at')->sum('pivot.amount'); + $total_refund = $invoice->payments->whereNull('deleted_at')->sum('pivot.refunded'); $total_invoice_payments += ($total_amount - $total_refund); } @@ -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/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index 4aa6dde3e805..96cb5401f3b2 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -438,11 +438,13 @@ class PaymentController extends BaseController */ public function destroy(DestroyPaymentRequest $request, Payment $payment) { - $payment->service()->reversePayment(); + // $payment->service()->deletePayment(); - $payment->is_deleted = true; - $payment->save(); - $payment->delete(); + // $payment->is_deleted = true; + // $payment->save(); + // $payment->delete(); + + $this->payment_repo->delete($payment); return $this->itemResponse($payment); } 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/Models/Paymentable.php b/app/Models/Paymentable.php index 1e013fc211a3..56b8e9134558 100644 --- a/app/Models/Paymentable.php +++ b/app/Models/Paymentable.php @@ -12,9 +12,12 @@ namespace App\Models; use Illuminate\Database\Eloquent\Relations\Pivot; +use Illuminate\Database\Eloquent\SoftDeletes; class Paymentable extends Pivot { + use SoftDeletes; + protected $table = 'paymentables'; //protected $dateFormat = 'Y-m-d H:i:s.u'; 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/Services/Payment/DeletePayment.php b/app/Services/Payment/DeletePayment.php index d9d145fb5d0d..6157f40ccc2a 100644 --- a/app/Services/Payment/DeletePayment.php +++ b/app/Services/Payment/DeletePayment.php @@ -40,6 +40,7 @@ class DeletePayment ->updateCreditables() //return the credits first ->adjustInvoices() ->updateClient() + ->deletePaymentables() ->save(); } @@ -51,6 +52,13 @@ class DeletePayment //set applied amount to 0 + private function deletePaymentables() + { + $this->payment->paymentables()->update(['deleted_at' => now()]); + + return $this; + } + private function updateClient() { $this->payment->client->service()->updatePaidToDate(-1 * $this->payment->amount)->save(); @@ -66,7 +74,7 @@ class DeletePayment $paymentable_invoice->ledger()->updateInvoiceBalance($paymentable_invoice->pivot->amount)->save(); $paymentable_invoice->client->service()->updateBalance($paymentable_invoice->pivot->amount)->save(); - if (floatval($paymentable_invoice->balance) == 0) { + if ($paymentable_invoice->balance == $paymentable_invoice->amount) { $paymentable_invoice->service()->setStatus(Invoice::STATUS_SENT)->save(); } else { $paymentable_invoice->service()->setStatus(Invoice::STATUS_PARTIAL)->save(); diff --git a/app/Transformers/PaymentableTransformer.php b/app/Transformers/PaymentableTransformer.php index f171e4b330bd..b4e36a317556 100644 --- a/app/Transformers/PaymentableTransformer.php +++ b/app/Transformers/PaymentableTransformer.php @@ -46,6 +46,7 @@ class PaymentableTransformer extends EntityTransformer 'refunded' => (float) $paymentable->refunded, 'created_at' => (int) $paymentable->created_at, 'updated_at' => (int) $paymentable->updated_at, + 'archived_at' => (int) $paymentable->deleted_at, ]; } } diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index a06a98918dd9..5a1f48addc08 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 b7a3d8a1476e..5b3245ca8c01 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -617,6 +617,7 @@ 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; @@ -625,6 +626,8 @@ trait MakesInvoiceValues $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); if (isset($item->discount) && $item->discount > 0) { diff --git a/database/migrations/2020_11_15_203755_soft_delete_paymentables.php b/database/migrations/2020_11_15_203755_soft_delete_paymentables.php new file mode 100644 index 000000000000..b0f275a7960d --- /dev/null +++ b/database/migrations/2020_11_15_203755_soft_delete_paymentables.php @@ -0,0 +1,30 @@ +softDeletes('deleted_at', 6); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/resources/views/email/template/dark.blade.php b/resources/views/email/template/dark.blade.php index fbe72dafa938..5ebfe46d5a35 100644 --- a/resources/views/email/template/dark.blade.php +++ b/resources/views/email/template/dark.blade.php @@ -1,7 +1,7 @@ @component('email.template.master', ['design' => 'dark', 'settings' => $settings, 'whitelabel' => $whitelabel]) @slot('header') - @component('email.components.header', ['p' => $body, 'logo' => 'https://www.invoiceninja.com/wp-content/uploads/2019/01/InvoiceNinja-Logo-Round-300x300.png']) + @component('email.components.header', ['p' => $body, 'logo' => $settings->company_logo ?: 'https://www.invoiceninja.com/wp-content/uploads/2019/01/InvoiceNinja-Logo-Round-300x300.png']) @if(isset($title)) {{$title}} diff --git a/resources/views/email/template/light.blade.php b/resources/views/email/template/light.blade.php index 12771ba932eb..a95799e9e282 100644 --- a/resources/views/email/template/light.blade.php +++ b/resources/views/email/template/light.blade.php @@ -1,7 +1,7 @@ @component('email.template.master', ['design' => 'light', 'settings' => $settings, 'whitelabel' => $whitelabel]) @slot('header') - @component('email.components.header', ['p' => $body, 'logo' => 'https://www.invoiceninja.com/wp-content/uploads/2019/01/InvoiceNinja-Logo-Round-300x300.png']) + @component('email.components.header', ['p' => $body, 'logo' => $settings->company_logo ?: 'https://www.invoiceninja.com/wp-content/uploads/2019/01/InvoiceNinja-Logo-Round-300x300.png']) @if(isset($title)) {{$title}} diff --git a/resources/views/portal/ninja2020/layout/app.blade.php b/resources/views/portal/ninja2020/layout/app.blade.php index b0f8e88f0ed0..5ace4893a31a 100644 --- a/resources/views/portal/ninja2020/layout/app.blade.php +++ b/resources/views/portal/ninja2020/layout/app.blade.php @@ -56,7 +56,7 @@ - @if((bool) Ninja::isSelfHost()) + @if((bool) \App\Utils\Ninja::isSelfHost()) @@ -67,7 +67,7 @@ {{-- Feel free to push anything to header using @push('header') --}} @stack('head') - @if((bool) Ninja::isSelfHost()) + @if((bool) \App\Utils\Ninja::isSelfHost()) {!! $client->getSetting('portal_custom_head') !!} @endif @@ -92,12 +92,12 @@ @yield('footer') @stack('footer') - @if((bool) Ninja::isSelfHost()) + @if((bool) \App\Utils\Ninja::isSelfHost()) {!! $client->getSetting('portal_custom_footer') !!} @endif - @if((bool) Ninja::isSelfHost()) + @if((bool) \App\Utils\Ninja::isSelfHost())