diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index d2548e27e414..8e04496b3782 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -174,8 +174,10 @@ class CheckData extends Command CompanyUser::query()->cursor()->each(function ($cu) { if (CompanyToken::where('user_id', $cu->user_id)->where('company_id', $cu->company_id)->where('is_system', 1)->doesntExist()) { - $this->logMessage("Creating missing company token for user # {$cu->user->id} for company id # {$cu->company->id}"); - (new CreateCompanyToken($cu->company, $cu->user, 'System'))->handle(); + $this->logMessage("Creating missing company token for user # {$cu->user_id} for company id # {$cu->company_id}"); + + if($cu->company && $cu->user) + (new CreateCompanyToken($cu->company, $cu->user, 'System'))->handle(); } }); diff --git a/app/Http/Controllers/ClientPortal/PrePaymentController.php b/app/Http/Controllers/ClientPortal/PrePaymentController.php index a0fc28317970..010aaf745be6 100644 --- a/app/Http/Controllers/ClientPortal/PrePaymentController.php +++ b/app/Http/Controllers/ClientPortal/PrePaymentController.php @@ -46,8 +46,6 @@ class PrePaymentController extends Controller public function process(StorePrePaymentRequest $request) { - - $invoices = collect(); $invoice = InvoiceFactory::create(auth()->guard('contact')->user()->company_id, auth()->guard('contact')->user()->user_id); $invoice->due_date = now()->format('Y-m-d'); @@ -55,11 +53,11 @@ class PrePaymentController extends Controller $invoice->client_id = auth()->guard('contact')->user()->client_id; $line_item = new InvoiceItem(); - $line_item->cost = $request->amount; + $line_item->cost = (float)$request->amount; $line_item->quantity = 1; $line_item->product_key = ctrans('texts.pre_payment'); $line_item->notes = $request->notes; - $line_item->type_id = 1; + $line_item->type_id = '1'; $items = []; $items[] = $line_item; @@ -80,25 +78,33 @@ class PrePaymentController extends Controller ->fillDefaults() ->save(); + $total = $invoice->balance; + //format totals - $formatted_total = Number::formatMoney($request->amount, auth()->guard('contact')->user()->client); + $formatted_total = Number::formatMoney($invoice->amount, auth()->guard('contact')->user()->client); $payment_methods = auth()->guard('contact')->user()->client->service()->getPaymentMethods($request->amount); //if there is only one payment method -> lets return straight to the payment page - + $invoices = collect(); $invoices->push($invoice); + $invoices->map(function ($invoice) { + $invoice->balance = Number::formatValue($invoice->balance, $invoice->client->currency()); + return $invoice; + }); + $data = [ 'settings' => auth()->guard('contact')->user()->client->getMergedSettings(), 'invoices' => $invoices, 'formatted_total' => $formatted_total, 'payment_methods' => $payment_methods, 'hashed_ids' => $invoices->pluck('hashed_id'), - 'total' => $request->amount, + 'total' => $total, 'pre_payment' => true, ]; -nlog($data); + + return $this->render('invoices.payment', $data); } diff --git a/app/Repositories/InvoiceRepository.php b/app/Repositories/InvoiceRepository.php index 1490cfaec06f..f88c5a295776 100644 --- a/app/Repositories/InvoiceRepository.php +++ b/app/Repositories/InvoiceRepository.php @@ -83,6 +83,9 @@ class InvoiceRepository extends BaseRepository */ public function restore($invoice) :Invoice { + if($invoice->is_proforma) + return $invoice; + //if we have just archived, only perform a soft restore if (! $invoice->is_deleted) { parent::restore($invoice); diff --git a/app/Services/ClientPortal/InstantPayment.php b/app/Services/ClientPortal/InstantPayment.php index 359d1e9b8b73..6dcae4a17921 100644 --- a/app/Services/ClientPortal/InstantPayment.php +++ b/app/Services/ClientPortal/InstantPayment.php @@ -214,7 +214,7 @@ class InstantPayment $credit_totals = 0; } - $hash_data = ['invoices' => $payable_invoices->toArray(), 'credits' => $credit_totals, 'amount_with_fee' => max(0, (($invoice_totals + $fee_totals) - $credit_totals))]; + $hash_data = ['invoices' => $payable_invoices->toArray(), 'credits' => $credit_totals, 'amount_with_fee' => max(0, (($invoice_totals + $fee_totals) - $credit_totals)), 'pre_payment' => $this->request->pre_payment]; if ($this->request->query('hash')) { $hash_data['billing_context'] = Cache::get($this->request->query('hash')); diff --git a/app/Services/Payment/UpdateInvoicePayment.php b/app/Services/Payment/UpdateInvoicePayment.php index 9ffdbe5b02d2..0fe1a9001edc 100644 --- a/app/Services/Payment/UpdateInvoicePayment.php +++ b/app/Services/Payment/UpdateInvoicePayment.php @@ -82,10 +82,22 @@ class UpdateInvoicePayment ->save(); if ($invoice->is_proforma) { - if (strlen($invoice->number) > 1 && str_starts_with($invoice->number, "####")) { - $invoice->number = ''; + + //keep proforma's hidden + if(property_exists($this->payment_hash->data, 'pre_payment') && $this->payment_hash->data->pre_payment == "1"){ + + $invoice->payments()->each(function ($p) { + $p->pivot->forceDelete(); + }); + + $invoice->is_deleted = true; + $invoice->deleted_at = now(); + $invoice->saveQuietly(); + return; } - + + if (strlen($invoice->number) > 1 && str_starts_with($invoice->number, "####")) + $invoice->number = ''; $invoice->is_proforma = false; diff --git a/resources/views/portal/ninja2020/invoices/payment.blade.php b/resources/views/portal/ninja2020/invoices/payment.blade.php index b88390f3f294..adc7b32c90b6 100644 --- a/resources/views/portal/ninja2020/invoices/payment.blade.php +++ b/resources/views/portal/ninja2020/invoices/payment.blade.php @@ -13,6 +13,7 @@ +