/** * Updates a particular model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id the ID of the model to be updated */ public function actionUpdate($id) { $model = $this->loadModel($id); //$this->_doUpdate($model); if (isset($_POST['SupplierPurchaseProducts'])) { foreach ($_POST['SupplierPurchaseProducts'] as $id => $data) { $SPP = SupplierPurchaseProduct::model()->findByPk($id); $SPP->attributes = $data; $SPP->save(); } } if (isset($_POST['SupplierProducts'])) { foreach ($_POST['SupplierProducts'] as $id => $data) { $SP = SupplierProduct::model()->findByPk($id); $SP->attributes = $data; $SP->save(); } } $SupplierProduct = new SupplierProduct(); if (isset($_POST['SupplierProduct']) && isset($_POST['new_product'])) { $SupplierProduct = new SupplierProduct(); $SupplierProduct->attributes = $_POST['SupplierProduct']; $SupplierProduct->supplier_id = $model->supplier_id; $SupplierProduct->save(); } if (isset($_POST['delete'])) { $SPP = SupplierPurchaseProduct::model()->findByPk($_POST['delete']); $SPP->delete(); } else { if (isset($_POST['add_product'])) { $SPP = new SupplierPurchaseProduct(); $SPP->supplier_product_id = $_POST['supplier_product_id']; $SPP->supplier_purchase_id = $model->id; $SPP->save(); $model->refresh(); } else { if (isset($_POST['new_product']) && $SupplierProduct->validate()) { $SPP = new SupplierPurchaseProduct(); $SPP->supplier_product_id = $SupplierProduct->id; $SPP->supplier_purchase_id = $model->id; $SPP->save(); $model->refresh(); } } } if (isset($_POST['SupplierPurchase'])) { $model->attributes = $_POST['SupplierPurchase']; $model->save(); } $this->layout = '//layouts/column1'; $this->render('update', array('model' => $model, 'SupplierProduct' => $SupplierProduct)); }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate($date = null, $item = null, $supplier = null) { //The products box in the view requires the date_id to be in the URL if (!$date) { $date = DeliveryDate::getCurrentDeliveryDateId(); $this->redirect(array('boxItem/create', 'date' => $date)); } Yii::app()->user->setReturnUrl($this->createUrl('create', array('date' => $date, 'item' => $item, 'supplier' => $supplier))); $model = new BoxItem(); $SelectedDeliveryDate = null; $NewItem = null; if (isset($_POST['bc'])) { foreach ($_POST['bc'] as $boxContents) { foreach ($boxContents['BoxItem'] as $boxItem) { if (isset($boxItem['box_item_id'])) { $BoxItem = BoxItem::model()->findByPk($boxItem['box_item_id']); } else { $BoxItem = new BoxItem(); } //Delete if boxItem exists and quantity has been set to 0 if ($BoxItem && isset($boxItem['box_item_id']) && $boxItem['item_quantity'] == 0) { $BoxItem->delete(); } if ($boxItem['item_quantity'] > 0) { $BoxItem->attributes = $boxItem; $BoxItem->item_value = $boxContents['item_value']; $BoxItem->item_unit = $boxContents['item_unit']; $BoxItem->supplier_product_id = $boxContents['supplier_product_id']; if (isset($boxContents['item_name'])) { $BoxItem->item_name = $boxContents['item_name']; } $SP = $BoxItem->SupplierProduct; if (!$SP) { $SP = SupplierProduct::model()->findByAttributes(array('name' => $boxContents['item_name'], 'supplier_id' => $boxContents['supplier_id'], 'unit' => $boxContents['item_unit'])); if (!$SP) { $SP = new SupplierProduct(); } $SP->supplier_id = $boxContents['supplier_id']; if (isset($boxContents['item_name'])) { $SP->name = $boxContents['item_name']; } $SP->value = $boxContents['item_value']; $SP->unit = $boxContents['item_unit']; $SP->packing_station_id = $boxContents['packing_station_id']; $SP->available_from = 1; $SP->available_to = 12; } $SP->save(); $BoxItem->supplier_product_id = $SP->id; $BoxItem->save(); } } } } $SupplierProducts = new SupplierProduct('search'); $SupplierProducts->unsetAttributes(); // clear any default values if (isset($_GET['SupplierProduct'])) { $SupplierProducts->attributes = $_GET['SupplierProduct']; } $DeliveryDates = DeliveryDate::model()->findAll(); //$DeliveryDates=DeliveryDate::model()->findAll('date > NOW()'); //Get the boxes for the selected date $DeliveryDateBoxes = null; $SelectedDeliveryDate = DeliveryDate::model()->findByPk($date); $DeliveryDateBoxes = $SelectedDeliveryDate->MergedBoxes; //Item has been selected from inventory, if it doesn't exist in the date //Load it to be added as a new row up the top of the box item list $selectedItemId = null; if ($item) { $NewItem = SupplierProduct::model()->findByPk($item); $TmpItem = BoxItem::model()->with('Box')->find('item_name=:itemName AND supplier_id=:supplierId AND item_unit=:itemUnit AND item_value=:itemValue AND Box.delivery_date_id=:deliveryDateId', array(':itemName' => $NewItem->name, ':supplierId' => $NewItem->supplier_id, ':itemUnit' => $NewItem->unit, ':itemValue' => $NewItem->value, ':deliveryDateId' => $date)); if ($TmpItem) { $selectedItemId = $TmpItem->box_item_id; } else { foreach ($DeliveryDateBoxes as $DeliveryDateBox) { $BoxItem = new BoxItem(); $BoxItem->item_name = $NewItem->name; $BoxItem->supplier_id = $NewItem->supplier_id; $BoxItem->item_unit = $NewItem->unit; $BoxItem->item_value = $NewItem->value; $BoxItem->supplier_product_id = $NewItem->id; $BoxItem->packing_station_id = $NewItem->packing_station_id; $BoxItem->item_quantity = 1; $BoxItem->box_id = $DeliveryDateBox->box_id; $BoxItem->save(); $selectedItemId = $BoxItem->box_item_id; } } //$this->redirect(array('boxItem/create','date'=>$date)); } //User chose to add a new entry by clicking a supplier name if ($supplier) { $TmpItem = BoxItem::model()->with('Box')->find('item_name=:itemName AND supplier_id=:supplierId AND item_unit=:itemUnit AND item_value=:itemValue AND Box.delivery_date_id=:deliveryDateId', array(':itemName' => '', ':supplierId' => (int) $supplier, ':itemUnit' => 'KG', ':itemValue' => 0, ':deliveryDateId' => $date)); if ($TmpItem) { $selectedItemId = $TmpItem->box_item_id; } else { foreach ($DeliveryDateBoxes as $DeliveryDateBox) { $BoxItem = new BoxItem(); $BoxItem->item_name = ''; $BoxItem->supplier_id = (int) $supplier; $BoxItem->item_unit = 'KG'; $BoxItem->item_value = 0; $BoxItem->item_quantity = 1; $BoxItem->box_id = $DeliveryDateBox->box_id; $BoxItem->save(); $selectedItemId = $BoxItem->box_item_id; } } } $this->performAjaxValidation($model); if (isset($_POST['BoxItem'])) { if (!empty($_POST['BoxItem']['box_item_id'])) { $model = $this->loadModel($_POST['BoxItem']['box_item_id']); } $model->attributes = $_POST['BoxItem']; $model->save(); } $Customer = new BoxomaticUser('search'); $Customer->unsetAttributes(); if (isset($_GET['BoxomaticUser'])) { $Customer->attributes = $_GET['BoxomaticUser']; } $this->layout = '//layouts/full_width'; $this->render('create', array('model' => $model, 'SupplierProducts' => $SupplierProducts, 'DeliveryDates' => $DeliveryDates, 'DeliveryDateBoxes' => $DeliveryDateBoxes, 'SelectedDeliveryDate' => $SelectedDeliveryDate, 'selectedItemId' => $selectedItemId, 'Customer' => $Customer)); }
/** * @param type $DDs Array of DeliveryDate objects */ public function repeatCurrentOrder($DDs) { $SPs = isset($this->_SupplierProduct[$this->delivery_date_id]) ? $this->_SupplierProduct[$this->delivery_date_id] : array(); $UBs = isset($this->_UserBox[$this->delivery_date_id]) ? $this->_UserBox[$this->delivery_date_id] : array(); $allOk = true; foreach ($DDs as $DD) { //Clear the current order unset($this->_SupplierProduct[$DD->id]); unset($this->_UserBox[$DD->id]); //Repopulate the order foreach ($SPs as $id => $qty) { $Product = SupplierProduct::model()->findByPk($id); $availTo = strtotime($Product->customer_available_to); $availFrom = strtotime($Product->customer_available_from); $curDate = strtotime($DD->date); if ($curDate < $availTo && $curDate > $availFrom) { $this->_SupplierProduct[$DD->id][$id] = $qty; } else { $allOk = false; } } foreach ($UBs as $id => $qty) { //Find the equivalent box type for the current date $CopyBox = Box::model()->findByAttributes(array('box_id' => $id)); $NewBox = Box::model()->findByAttributes(array('size_id' => $CopyBox->size_id, 'delivery_date_id' => $DD->id)); if ($NewBox) { $this->_UserBox[$DD->id][$NewBox->box_id] = $qty; } else { Yii::app()->user->setFlash('warning', 'A box has been removed from your future orders.'); } } } $this->_user->setState('boxocart.SupplierProduct', $this->_SupplierProduct); $this->_user->setState('boxocart.UserBox', $this->_UserBox); return $allOk; }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer the ID of the model to be loaded */ public function loadModel($id) { $model = SupplierProduct::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }