/** * Returns a list of all the warehouses with a value in use for the stock * GridField instance. Will create records for products that don't have * them. * * @return DataList */ public function getStockForEachWarehouse() { $warehouses = ProductWarehouse::get(); $output = new ArrayList(); $defaults = ProductWarehouseStock::config()->get('defaults'); foreach ($warehouses as $warehouse) { $stock = $this->getStockForWarehouse($warehouse); $output->push($stock); } return $output; }
/** * Returns a list of all the warehouses with a value in use for the stock * GridField instance. Will create records for products that don't have * them. * * @return DataList */ public function getStockForEachWarehouse() { $warehouses = ProductWarehouse::get(); $output = new ArrayList(); $defaults = ProductWarehouseStock::config()->get('defaults'); foreach ($warehouses as $warehouse) { $base = $this->getStockBaseIdentifier(); $record = $this->getWarehouseStock()->first(); if (!$record) { $record = Injector::inst()->create('ProductWarehouseStock'); $record->WarehouseID = $warehouse->ID; $record->ProductID = $this->owner->ID; $record->ProductClass = $base; $record->Quantity = $defaults['Quantity']; $record->write(); } $output->push($record); } return $output; }
public function handleSave(GridField $grid, DataObjectInterface $record) { $data = $grid->Value(); $base = ClassInfo::baseDataClass($record); if (isset($data['GridFieldEditableColumns'])) { // go through every warehouse and make sure the have either 0 stock // or take the value from this $warehouses = ProductWarehouse::get(); foreach ($warehouses as $warehouse) { $stock = $record->getStockForWarehouse($warehouse); $quantity = null; if (isset($data['GridFieldEditableColumns'][$stock->ID])) { if (isset($data['GridFieldEditableColumns'][$stock->ID]['Quantity'])) { $quantity = (int) $data['GridFieldEditableColumns'][$stock->ID]['Quantity']; } } $stock->Quantity = $quantity; $stock->write(); } } }