amount; $invoice->fill($data); $invoice->save(); if(isset($data['client_contacts'])) { foreach($data['client_contacts'] as $contact) { if($contact['send_invoice'] == 1) { $client_contact = ClientContact::find($this->decodePrimaryKey($contact['id'])); $client_contact->send_invoice = true; $client_contact->save(); } } } event(new CreateInvoiceInvitation($invoice)); $invoice_calc = new InvoiceCalc($invoice, $invoice->settings); $invoice = $invoice_calc->build()->getInvoice(); $invoice->save(); $finished_amount = $invoice->amount; if($finished_amount != $starting_amount) UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, ($finished_amount - $starting_amount)); return $invoice; } /** * Mark the invoice as sent. * * @param \App\Models\Invoice $invoice The invoice * * @return Invoice|\App\Models\Invoice|null Return the invoice object */ public function markSent(Invoice $invoice) : ?Invoice { /* Return immediately if status is not draft */ if($invoice->status_id != Invoice::STATUS_DRAFT) return $invoice; $invoice->status_id = Invoice::STATUS_SENT; $this->markInvitationsSent(); $invoice->save(); UpdateCompanyLedgerWithInvoice::dispatchNow($this->invoice, $this->balance); return $invoice; } /** * Updates Invites to SENT * * @param \App\Models\Invoice $invoice The invoice */ private function markInvitationsSent(Invoice $invoice) :void { $invoice->invitations->each(function($invitation) { if(!isset($invitation->sent_date)) { $invitation->sent_date = Carbon::now(); $invitation->save(); } }); } }