/**
     * 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));
    }
 /**
  * Manages all models.
  */
 public function actionAdmin()
 {
     $model = new SupplierProduct('search');
     $model->unsetAttributes();
     // clear any default values
     if (isset($_GET['SupplierProduct'])) {
         $model->attributes = $_GET['SupplierProduct'];
     }
     $this->render('admin', array('model' => $model));
 }