Advertisement
moccalotto

Untitled

Mar 24th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Invoice
  2. {
  3. /*
  4. ..... other stuff
  5. */
  6.     public function makeValid()
  7.     {
  8.         if ($this->invoice_number && $this->invoice_date) {
  9.             return;
  10.         }
  11.  
  12.         DB::transaction((function () {
  13.             // lock $this
  14.             Invoice::where('id', $this->id)
  15.                 ->lockForUpdate()
  16.                 ->first();
  17.  
  18.             // get and lock the invoice with the highest invoice number
  19.             $largestCurrentInvoice = Invoice::whereNotNull('invoice_number')
  20.                 ->orderBy('invoice_number', 'DESC')
  21.                 ->lockForUpdate()
  22.                 ->first();
  23.  
  24.             $this->invoice_number = $largestCurrentInvoice
  25.                 ? $largestCurrentInvoice->invoice_number + 1
  26.                 : config('company.invoice_no_base');
  27.  
  28.             $this->invoice_date = Carbon::now();
  29.  
  30.             $this->save();
  31.         }));
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement