diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index b301416c09bf..72719684673a 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -14,6 +14,7 @@ use App\Http\Requests\Invoice\ShowInvoiceRequest; use App\Http\Requests\Invoice\StoreInvoiceRequest; use App\Http\Requests\Invoice\UpdateInvoiceRequest; use App\Jobs\Entity\ActionEntity; +use App\Jobs\Invoice\StoreInvoice; use App\Models\Invoice; use App\Repositories\BaseRepository; use App\Repositories\InvoiceRepository; @@ -102,6 +103,8 @@ class InvoiceController extends BaseController $invoice = $this->invoice_repo->save($request->all(), InvoiceFactory::create(auth()->user()->company()->id, auth()->user()->id)); + $invoice = StoreInvoice::dispatchNow($invoice, $request->all()); + return $this->itemResponse($invoice); } diff --git a/app/Http/Requests/Invoice/StoreInvoiceRequest.php b/app/Http/Requests/Invoice/StoreInvoiceRequest.php index 6f7388f978c1..b76b33729c87 100644 --- a/app/Http/Requests/Invoice/StoreInvoiceRequest.php +++ b/app/Http/Requests/Invoice/StoreInvoiceRequest.php @@ -3,6 +3,7 @@ namespace App\Http\Requests\Invoice; use App\Http\Requests\Request; +use App\Models\ClientContact; use App\Models\Invoice; class StoreInvoiceRequest extends Request @@ -20,17 +21,31 @@ class StoreInvoiceRequest extends Request public function rules() { + $this->sanitize(); + return [ + 'client_id' => 'required', 'documents' => 'mimes:png,ai,svg,jpeg,tiff,pdf,gif,psd,txt,doc,xls,ppt,xlsx,docx,pptx', ]; } - public function sanitize() +public function sanitize() +{ + $input = $this->all(); + + /** If we have an email address instead of a client_id - harvest the client_id here */ + if(isset($input['email']) && !$input['client_id']) { - //do post processing of invoice request here, ie. invoice_items + $contact = ClientContact::company()->whereEmail($input['email'])->first(); + + if($contact) + $input['client_id'] = $contact->client_id; } + $this->replace($input); +} + public function messages() { diff --git a/app/Jobs/Invoice/StoreInvoice.php b/app/Jobs/Invoice/StoreInvoice.php new file mode 100644 index 000000000000..01f57b3c142b --- /dev/null +++ b/app/Jobs/Invoice/StoreInvoice.php @@ -0,0 +1,97 @@ +invoice = $invoice; + + $this->data = $data; + + } + + /** + * Execute the job. + * + * We expect the Invoice object along with + * the request in array form + * + * Embedded in the request may be additionals + * attributes which require additional work to be + * done in this job, these include - but are not limited to: + * + * 1. email_invoice - Email the Invoice + * 2. mark_paid - Mark the invoice as paid (Generates a payment against the invoice) + * 3. ...... + * + * @return NULL|Invoice + */ + public function handle(InvoiceRepository $invoice_repo) : ?Invoice + { + + $payment = false; + + /* Test if we should auto-bill the invoice */ + if((bool)$invoice->settings->auto_bill) + { + + $this->invoice = $invoice_repo->markSent($this->invoice); + + //fire autobill - todo - the PAYMENT class will update the INVOICE status. + // $payment = + + } + + if(isset($this->data['email_invoice']) && (bool)$this->data['email_invoice']) + { + + $this->invoice = $invoice_repo->markSent($this->invoice); + + //fire invoice job (the job performs the filtering logic of the email recipients... if any.) + } + + if(isset($this->data['mark_paid']) && (bool)$this->data['mark_paid']) + { + + $this->invoice = $invoice_repo->markSent($this->invoice); + + // generate a manual payment against the invoice + // the PAYMENT class will update the INVOICE status. + //$payment = + + } + + /* Payment Notifications */ + if($payment) + { + //fire payment notifications here + } + + if(isset($data['download_invoice']) && (bool)$this->data['download_invoice']) + { + //fire invoice download and return PDF response from here + } + + } +} diff --git a/app/Repositories/InvoiceRepository.php b/app/Repositories/InvoiceRepository.php index 8b26b876d779..24872bf0a9ca 100644 --- a/app/Repositories/InvoiceRepository.php +++ b/app/Repositories/InvoiceRepository.php @@ -34,19 +34,19 @@ class InvoiceRepository extends BaseRepository */ public function save($data, Invoice $invoice) : ?Invoice { + $invoice->fill($data); $invoice->save(); - $invoice_calc = new InvoiceCalc($invoice, $invoice->settings); $invoice = $invoice_calc->build()->getInvoice(); - - //fire events here that cascading from the saving of an invoice - //ie. client balance update... + $invoice->save(); + return $invoice; + }