Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Invoice
- {
- /*
- ..... other stuff
- */
- public function makeValid()
- {
- if ($this->invoice_number && $this->invoice_date) {
- return;
- }
- DB::transaction((function () {
- // lock $this
- Invoice::where('id', $this->id)
- ->lockForUpdate()
- ->first();
- // get and lock the invoice with the highest invoice number
- $largestCurrentInvoice = Invoice::whereNotNull('invoice_number')
- ->orderBy('invoice_number', 'DESC')
- ->lockForUpdate()
- ->first();
- $this->invoice_number = $largestCurrentInvoice
- ? $largestCurrentInvoice->invoice_number + 1
- : config('company.invoice_no_base');
- $this->invoice_date = Carbon::now();
- $this->save();
- }));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement