/** * Creates a stock record as well as a first record movement. * * @return bool|static */ public function create() { $this->dbStartTransaction(); try { /* * Set insert data */ $insert = ['inventory_id' => $this->getInput('inventory_id'), 'location_id' => $this->getInput('location_id'), 'quantity' => $this->getInput('quantity')]; /* * Create the stock record */ $record = $this->model->create($insert); if ($record) { /* * Fire stock created event */ $this->fireEvent('maintenance.inventory.stock.created', ['stock' => $record]); $this->dbCommitTransaction(); return $record; } $this->dbRollbackTransaction(); return false; } catch (\Exception $e) { $this->dbRollbackTransaction(); return false; } }
/** * Captures the Inventory Stock models restored event * and cascades the restore to all of it's movements. * * @param InventoryStock $stock */ public function restored(InventoryStock $stock) { $movements = $stock->movements()->onlyTrashed()->get(); if (count($movements) > 0) { foreach ($movements as $movement) { $movement->restore(); } } }
<?php use Stevebauman\Maintenance\Models\Inventory; use Stevebauman\Maintenance\Models\InventoryStock; // The inventory observers. Inventory::observe(new \Stevebauman\Maintenance\Handlers\Observers\Inventory\Observer()); InventoryStock::observe(new \Stevebauman\Maintenance\Handlers\Observers\Inventory\StockObserver());