client_service = $client_service; $this->invoice = $invoice; } public function run() { if ($this->invoice->status_id == Invoice::STATUS_DRAFT) { $this->invoice->service()->markSent(); } /* Create Payment */ $payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id); $payment->amount = $this->invoice->balance; $payment->applied = $this->invoice->balance; $payment->number = $this->getNextPaymentNumber($this->invoice->client); $payment->status_id = Payment::STATUS_COMPLETED; $payment->client_id = $this->invoice->client_id; $payment->transaction_reference = ctrans('texts.manual_entry'); $payment->currency_id = $this->invoice->client->getSetting('currency_id'); /* Create a payment relationship to the invoice entity */ $payment->save(); $payment->invoices()->attach($this->invoice->id, [ 'amount' => $payment->amount ]); $this->invoice->service() ->updateBalance($payment->amount*-1) ->setStatus(Invoice::STATUS_PAID) ->applyNumber() ->save(); /* Update Invoice balance */ event(new PaymentWasCreated($payment, $payment->company)); $payment->ledger() ->updatePaymentBalance($payment->amount*-1); $this->client_service ->updateBalance($payment->amount*-1) ->updatePaidToDate($payment->amount) ->save(); return $this->invoice; } }