vendor = $vendor; $this->purchase_order = $purchase_order; } public function run() { if ($this->purchase_order->number != '') { return $this->purchase_order; } switch ($this->vendor->company->getSetting('counter_number_applied')) { case 'when_saved': $this->trySaving(); break; case 'when_sent': if ($this->invoice->status_id == PurchaseOrder::STATUS_SENT) { $this->trySaving(); } break; default: break; } return $this->purchase_order; } private function trySaving() { $x=1; do{ try{ $this->purchase_order->number = $this->getNextPurchaseOrderNumber($this->purchase_order); $this->purchase_order->saveQuietly(); $this->completed = false; } catch(QueryException $e){ $x++; if($x>10) $this->completed = false; } } while($this->completed); } }