/** * Get the date target */ static function dateTarget($dateId) { $Item = Box::model()->with('UserBoxes')->find(array('select' => 'SUM(quantity * box_price) as total', 'condition' => 'delivery_date_id = ' . $dateId . '')); return $Item ? $Item->total : 0; }
<td class="value"><strong><?php echo SnapFormat::currency(BoxItem::dateTarget($SelectedDeliveryDate->id)); ?> </strong></td> <td></td> </tr> <tr> <td class="total" colspan="3"> Box Retail: </td> <?php $totalRetal = 0; foreach ($DeliveryDateBoxes as $DeliveryDateBoxMerged) { $dateBoxIds = explode(',', $DeliveryDateBoxMerged->box_ids); foreach ($dateBoxIds as $dateBoxId) { $DateBox = Box::model()->findByPk($dateBoxId); $retail = $value = $DateBox->retailPrice; $totalRetal += $retail; ?> <td class="value <?php echo $retail > $DateBox->box_price ? 'red' : ''; ?> "><?php echo SnapFormat::currency($retail); ?> </td> <?php } ?> <?php }
/** * @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 = Box::model()->findByPk((int) $id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
/** * Generate a packing list spreadsheet for a given date_id */ public function actionGenerateOrderList($date) { $tablePrefix = SnapUtil::config('boxomatic/tablePrefix'); $sql = ' SELECT SUM(item_quantity) as total, SUM(item_quantity * item_value) as total_price, item_value, GROUP_CONCAT(DISTINCT t.box_id ORDER BY t.box_id DESC) AS box_ids, GROUP_CONCAT(DISTINCT `box_item_id` ORDER BY `BoxItems`.box_id DESC) as box_item_ids, `BoxItems`.`box_item_id`, `BoxItems`.`item_name`, `BoxItems`.`item_unit`, `Supplier`.`name` FROM `' . $tablePrefix . 'boxes` `t` LEFT OUTER JOIN `' . $tablePrefix . 'user_boxes` `UserBoxes` ON (`UserBoxes`.`box_id`=`t`.`box_id`) LEFT OUTER JOIN `' . $tablePrefix . 'box_items` `BoxItems` ON (`BoxItems`.`box_id`=`t`.`box_id`) LEFT OUTER JOIN `' . $tablePrefix . 'suppliers` `Supplier` ON (`BoxItems`.`supplier_id`=`Supplier`.`id`) WHERE ( delivery_date_id=' . $date . ' AND user_box_id is not null AND ( UserBoxes.status=' . UserBox::STATUS_APPROVED . ' OR UserBoxes.status=' . UserBox::STATUS_DELIVERED . ' ) ) GROUP BY name,item_name ORDER BY name; '; $connection = Yii::app()->db; $command = $connection->createCommand($sql); $dataReader = $command->query(); $items = $dataReader->readAll(); if (empty($items)) { echo 'No customer orders!'; exit; } $phpExcelPath = Yii::getPathOfAlias('boxomatic.external.PHPExcel'); $DateBoxes = Box::model()->with('BoxSize')->findAll(array('condition' => 'delivery_date_id = ' . $date, 'order' => 'box_size_name DESC')); //disable Yii's Autoload because it messes with PHPExcel's autoloader spl_autoload_unregister(array('YiiBase', 'autoload')); include $phpExcelPath . DIRECTORY_SEPARATOR . 'PHPExcel.php'; $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Supplier'); $objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Item'); $objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Total Quantity'); $objPHPExcel->getActiveSheet()->SetCellValue('D1', 'Unit'); $objPHPExcel->getActiveSheet()->SetCellValue('E1', 'Unit Price'); $objPHPExcel->getActiveSheet()->SetCellValue('F1', 'Total Price'); $alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $boxIds = explode(',', $items[0]['box_ids']); $pos = 6; spl_autoload_register(array('YiiBase', 'autoload')); foreach ($DateBoxes as $n => $Box) { $custCount = $Box->customerCount; $objPHPExcel->getActiveSheet()->SetCellValue($alpha[$pos] . '1', $Box->BoxSize->box_size_name . ' (' . $custCount . ')'); $objPHPExcel->getActiveSheet()->getColumnDimension($alpha[$pos])->setAutoSize(true); $pos++; } $objPHPExcel->getActiveSheet()->getStyle("A1:" . $alpha[$pos] . '1')->applyFromArray(array("font" => array("bold" => true))); $row = 2; foreach ($items as $item) { $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, $item['name']); $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, $item['item_name']); $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, $item['total']); $objPHPExcel->getActiveSheet()->SetCellValue('D' . $row, $item['item_unit']); $objPHPExcel->getActiveSheet()->SetCellValue('E' . $row, $item['item_value']); $objPHPExcel->getActiveSheet()->SetCellValue('F' . $row, $item['total_price']); $boxIds = explode(',', $item['box_ids']); $boxItemIds = explode(',', $item['box_item_ids']); $pos = 6; foreach ($DateBoxes as $Box) { $BoxItem = null; $biPos = false; $biPos = array_search($Box->box_id, $boxIds); if ($biPos !== false) { $BoxItem = BoxItem::model()->findByAttributes(array('box_id' => $Box->box_id, 'box_item_id' => $boxItemIds[$biPos])); } $quantity = $BoxItem && !empty($BoxItem->item_quantity) ? $BoxItem->item_quantity : '0'; $objPHPExcel->getActiveSheet()->SetCellValue($alpha[$pos] . $row, $quantity); $pos++; } $row++; } $objPHPExcel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true); $objPHPExcel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true); $objPHPExcel->getActiveSheet()->getColumnDimension("C")->setAutoSize(true); $objPHPExcel->getActiveSheet()->getColumnDimension("D")->setAutoSize(true); $objPHPExcel->getActiveSheet()->getColumnDimension("E")->setAutoSize(true); $objPHPExcel->getActiveSheet()->getColumnDimension("F")->setAutoSize(true); // Rename sheet $objPHPExcel->getActiveSheet()->setTitle('Packing List'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment; filename="ordering-list-' . date('Ymd') . '"'); $objWriter->save('php://output'); exit; }
/** * Manages all models. */ public function actionOrder($show = 4) { $model = new UserBox(); $Customer = Customer::model()->findByPk(Yii::app()->user->user_id); $deadlineDays = SnapUtil::config('boxomatic/orderDeadlineDays'); if (isset($_GET['all'])) { $DeliveryDates = DeliveryDate::model()->findAll(); } else { $DeliveryDates = DeliveryDate::model()->with('Boxes')->findAll(array('condition' => 'date_sub(date, interval -1 week) > NOW()', 'limit' => $show + 1)); } $BoxSizes = BoxSize::model()->findAll(array('order' => 'box_size_name DESC')); if (isset($_POST['btn_recurring'])) { $monthsAdvance = (int) $_POST['months_advance']; $startingFrom = $_POST['starting_from']; $every = $_POST['every']; $locationId = $_POST['Customer']['delivery_location_key']; $custLocationId = new CDbExpression('NULL'); if (strpos($locationId, '-')) { //has a customer location $parts = explode('-', $locationId); $locationId = $parts[1]; $custLocationId = $parts[0]; } $Location = Location::model()->findByPk($locationId); foreach ($_POST['Recurring'] as $key => $quantity) { $boxSizeId = str_replace('bs_', '', $key); $Boxes = Box::model()->with('DeliveryDate')->findAll("\n\t\t\t\t\tdate >= '{$startingFrom}' AND\n\t\t\t\t\tdate <= date_add('{$startingFrom}', interval {$monthsAdvance} month) AND\n\t\t\t\t\tdate_sub(date, interval {$deadlineDays} day) > NOW() AND\n\t\t\t\t\tsize_id={$boxSizeId}"); $n = 0; foreach ($Boxes as $Box) { $CustBoxes = UserBox::model()->findAllByAttributes(array('user_id' => $Customer->user_id, 'box_id' => $Box->box_id)); foreach ($CustBoxes as $CustBox) { $CustBox->delete(); } $n++; if ($n % 2 == 0 && $every == 'fortnight') { continue; } //Create extra customer box rows for ($i = 0; $i < $quantity; $i++) { $CustBox = new UserBox(); $CustBox->user_id = $Customer->user_id; $CustBox->box_id = $Box->box_id; $CustBox->quantity = 1; $CustBox->delivery_cost = $Location->location_delivery_value; $CustBox->save(); $CustDeliveryDate = Order::model()->findByAttributes(array('user_id' => $Customer->user_id, 'delivery_date_id' => $CustBox->Box->delivery_date_id)); if (!$CustDeliveryDate) { $CustDeliveryDate = new Order(); $CustDeliveryDate->user_id = $Customer->user_id; $CustDeliveryDate->delivery_date_id = $CustBox->Box->delivery_date_id; } $CustDeliveryDate->location_id = $locationId; $CustDeliveryDate->customer_location_id = $custLocationId; $CustDeliveryDate->save(); } } } } if (isset($_POST['btn_clear_orders'])) { //Get all boxes beyond the deadline date $Boxes = Box::model()->with('DeliveryDate')->findAll("\n\t\t\t\tdate_sub(date, interval {$deadlineDays} day) > NOW()"); foreach ($Boxes as $Box) { $CustBox = UserBox::model()->findByAttributes(array('user_id' => $Customer->user_id, 'box_id' => $Box->box_id)); //Only create a records if an entry doesn't already exist if ($CustBox) { $CustBox->delete(); } } } if (isset($_POST['Orders'])) { foreach ($_POST['Orders'] as $boxId => $quantity) { $Box = Box::model()->findByPk($boxId); $CustBoxes = UserBox::model()->with('Box')->findAll(array('condition' => 'user_id=:customerId AND size_id=:sizeId AND delivery_date_id=:deliveryDateId', 'params' => array(':customerId' => $Customer->user_id, ':sizeId' => $Box->size_id, ':deliveryDateId' => $Box->delivery_date_id))); $curQuantity = count($CustBoxes); $diff = $quantity - $curQuantity; if ($diff > 0) { //Create extra customer box rows for ($i = 0; $i < $diff; $i++) { $CustBox = new UserBox(); $CustBox->user_id = $Customer->user_id; $CustBox->box_id = $boxId; $CustBox->quantity = 1; $CustBox->delivery_cost = $Customer->Location->location_delivery_value; $CustBox->save(); } } if ($diff < 0) { //Remove any boxes the customer no longer wants; $diff = abs($diff); for ($i = 0; $i < $diff; $i++) { $CustBoxes[$i]->delete(); } } } } if (isset($_POST['CustDeliveryDates'])) { foreach ($_POST['CustDeliveryDates'] as $key => $locationId) { $CustDeliveryDate = Order::model()->findByPk($key); $custLocationId = new CDbExpression('NULL'); if (strpos($locationId, '-')) { //has a customer location $parts = explode('-', $locationId); $locationId = $parts[1]; $custLocationId = $parts[0]; } $CustDeliveryDate->location_id = $locationId; $CustDeliveryDate->customer_location_id = $custLocationId; $CustDeliveryDate->save(); $CustBoxesDate = UserBox::model()->with('Box')->findAll('user_id=:customerId AND Box.delivery_date_id=:dateId', array('customerId' => Yii::app()->user->user_id, 'dateId' => $CustDeliveryDate->delivery_date_id)); foreach ($CustBoxesDate as $CustBox) { $CustBox->delivery_cost = $CustDeliveryDate->Location->location_delivery_value; $CustBox->save(); } } } $this->render('order', array('model' => $model, 'DeliveryDates' => $DeliveryDates, 'Customer' => $Customer, 'BoxSizes' => $BoxSizes, 'deadline' => strtotime('+' . $deadlineDays . ' days'), 'show' => $show)); }
/** * Manages all models. */ public function actionOrder($date = null, $cat = null, $show = 5, $location = null) { if (!$cat) { $cat = SnapUtil::config('boxomatic/supplier_product_feature_category'); } $customerId = Yii::app()->user->user_id; $Customer = Customer::model()->findByPk($customerId); $deadlineDays = SnapUtil::config('boxomatic/orderDeadlineDays'); if (!$date) { $date = DeliveryDate::getCurrentDeliveryDateId(); } $updatedExtras = array(); $updatedOrders = array(); $Category = Category::model()->findByPk($cat); $DeliveryDate = DeliveryDate::model()->findByPk($date); $AllDeliveryDates = false; $pastDeadline = false; $CustDeliveryDate = false; if ($Customer) { $CustDeliveryDate = Order::model()->findByAttributes(array('delivery_date_id' => $date, 'user_id' => $customerId)); if (!$CustDeliveryDate) { $CustDeliveryDate = new Order(); $CustDeliveryDate->delivery_date_id = $date; $CustDeliveryDate->user_id = $customerId; $CustDeliveryDate->location_id = $Customer->location_id; $CustDeliveryDate->save(); } $AllDeliveryDates = DeliveryDate::model()->with('Locations')->findAll("Locations.location_id = " . $CustDeliveryDate->location_id); $deadline = strtotime('+' . $deadlineDays . ' days'); $pastDeadline = strtotime($DeliveryDate->date) < $deadline; if ($pastDeadline) { Yii::app()->user->setFlash('warning', 'Order deadline has passed, order cannot be changed.'); } } if (!$Customer && (isset($_POST['supplier_purchases']) || isset($_POST['boxes']))) { Yii::app()->user->setFlash('error', 'You must register to make an order.'); $this->redirect(array('site/register')); } if ($location) { $locationId = $location; $custLocationId = new CDbExpression('NULL'); if (strpos($locationId, '-')) { //has a customer location $parts = explode('-', $locationId); $locationId = $parts[1]; $custLocationId = $parts[0]; } //$Location=Location::model()->findByPk($locationId); $CustDeliveryDate->location_id = $locationId; $CustDeliveryDate->customer_location_id = $custLocationId; $CustDeliveryDate->save(); $CustDeliveryDate->refresh(); } if (isset($_POST['btn_recurring'])) { $monthsAdvance = (int) $_POST['months_advance']; $startingFrom = $_POST['starting_from']; $every = $_POST['every']; $locationId = $_POST['Order']['delivery_location_key']; $custLocationId = new CDbExpression('NULL'); if (strpos($locationId, '-')) { //has a customer location $parts = explode('-', $locationId); $locationId = $parts[1]; $custLocationId = $parts[0]; } $dayOfWeek = date('N', strtotime($CustDeliveryDate->DeliveryDate->date)) + 1; if ($dayOfWeek == 8) { $dayOfWeek = 1; } $orderedExtras = OrderItem::findCustomerExtras($customerId, $date); $orderedBoxes = UserBox::model()->with('Box')->findAllByAttributes(array('user_id' => $Customer->user_id), 'delivery_date_id=' . $date); $DeliveryDates = DeliveryDate::model()->findAll("\n\t\t\t\t\tdate >= '{$startingFrom}' AND\n\t\t\t\t\tdate <= DATE_ADD('{$startingFrom}', interval {$monthsAdvance} MONTH) AND\n\t\t\t\t\tdate_sub(date, interval {$deadlineDays} day) > NOW() AND\n\t\t\t\t\tDAYOFWEEK(date) = '" . $dayOfWeek . "'"); $n = 0; foreach ($DeliveryDates as $DD) { $CustDD = Order::model()->findByAttributes(array('delivery_date_id' => $DD->id, 'user_id' => $customerId)); if (!$CustDD) { $CustDD = new Order(); $CustDD->delivery_date_id = $DD->id; $CustDD->user_id = $customerId; $CustDD->location_id = $CustDeliveryDate->location_id; $CustDD->save(); } //Delete any extras already ordered $TBDExtras = OrderItem::findCustomerExtras($customerId, $DD->id); foreach ($TBDExtras as $TBDExtra) { $TBDExtra->delete(); } //Delete any extras already ordered $TBDBoxes = UserBox::model()->with('Box')->findAllByAttributes(array('user_id' => $Customer->user_id), 'delivery_date_id=' . $CustDD->delivery_date_id); foreach ($TBDBoxes as $TBDBox) { $TBDBox->delete(); } $n++; if ($n % 2 == 0 && $every == 'fortnight') { continue; } //Copy current days order foreach ($orderedExtras as $orderedExt) { $extra = new OrderItem(); //give the customer the extra $extra->quantity = $orderedExt->quantity; $extra->order_id = $CustDD->id; $extra->supplier_purchase_id = $orderedExt->supplier_purchase_id; $extra->price = $orderedExt->price; $extra->packing_station_id = $orderedExt->packing_station_id; $extra->name = $orderedExt->name; $extra->unit = $orderedExt->unit; $extra->save(); } //Copy current days boxxes foreach ($orderedBoxes as $orderedBox) { $EquivBox = Box::model()->findByAttributes(array('size_id' => $orderedBox->Box->size_id, 'delivery_date_id' => $DD->id)); $box = new UserBox(); $box->attributes = $orderedBox->attributes; $box->user_box_id = null; $box->box_id = $EquivBox->box_id; $box->save(); } } Yii::app()->user->setFlash('success', 'Recurring order set.'); } if (isset($_POST['btn_clear_orders'])) { $orderedExtras = OrderItem::model()->with(array('Order' => array('with' => 'DeliveryDate')))->findAll("DATE_SUB(date, INTERVAL {$deadlineDays} DAY) > NOW() AND user_id = " . $Customer->user_id); foreach ($orderedExtras as $ext) { $ext->delete(); } //Get all boxes beyond the deadline date $Boxes = Box::model()->with('DeliveryDate')->findAll("DATE_SUB(date, interval {$deadlineDays} day) > NOW()"); foreach ($Boxes as $Box) { $CustBox = UserBox::model()->findByAttributes(array('user_id' => $Customer->user_id, 'box_id' => $Box->box_id)); if ($CustBox) { $CustBox->delete(); } } } if (isset($_POST['extras'])) { foreach ($_POST['extras'] as $id => $quantity) { $model = $this->loadModel($id); if ($model->Order->user_id == Yii::app()->user->user_id) { if ($quantity == 0) { $model->delete(); } else { $model->quantity = $quantity; $model->save(); $updatedOrders[$model->id] = $model; } } } } if (isset($_POST['supplier_purchases'])) { foreach ($_POST['supplier_purchases'] as $purchaseId => $quantity) { if ($quantity == 0) { continue; } $extra = OrderItem::model()->with('Order')->findByAttributes(array('supplier_purchase_id' => $purchaseId, 'order_id' => $CustDeliveryDate->id)); if (!$extra) { $extra = new OrderItem(); } $Purchase = SupplierPurchase::model()->findByPk($purchaseId); $SupplierProduct = $Purchase->supplierProduct; //give the customer the extra $extra->quantity += $quantity; $extra->order_id = $CustDeliveryDate->id; $extra->supplier_purchase_id = $purchaseId; $extra->price = $Purchase->item_sales_price; $extra->packing_station_id = $SupplierProduct->packing_station_id; $extra->name = $SupplierProduct->name; $extra->unit = $SupplierProduct->unit; $updatedExtras[$extra->supplier_purchase_id] = $extra; $extra->save(); } } if (isset($_POST['boxes'])) { foreach ($_POST['boxes'] as $boxId => $quantity) { $Box = Box::model()->findByPk($boxId); $CustBoxes = UserBox::model()->with('Box')->findAll(array('condition' => 'user_id=:customerId AND size_id=:sizeId AND delivery_date_id=:deliveryDateId', 'params' => array(':customerId' => $customerId, ':sizeId' => $Box->size_id, ':deliveryDateId' => $Box->delivery_date_id))); $curQuantity = count($CustBoxes); $diff = $quantity - $curQuantity; if ($diff > 0) { //Create extra customer box rows for ($i = 0; $i < $diff; $i++) { $CustBox = new UserBox(); $CustBox->user_id = $customerId; $CustBox->box_id = $boxId; $CustBox->quantity = 1; $CustBox->delivery_cost = $Customer->Location->location_delivery_value; $CustBox->save(); } } if ($diff < 0) { //Remove any boxes the customer no longer wants; $diff = abs($diff); for ($i = 0; $i < $diff; $i++) { $CustBoxes[$i]->delete(); } } } } $orderedExtras = OrderItem::findCustomerExtras($customerId, $date); $dpOrderedExtras = new CActiveDataProvider('OrderItem'); $dpOrderedExtras->setData($orderedExtras); $DeliveryDates = DeliveryDate::model()->with('Boxes')->findAll(array('condition' => 'DATE_SUB(date, INTERVAL -1 week) > NOW() AND date < DATE_ADD(NOW(), INTERVAL 1 MONTH)')); $this->render('order', array('pastDeadline' => $pastDeadline, 'orderedExtras' => $dpOrderedExtras, 'updatedExtras' => $updatedExtras, 'updatedOrders' => $updatedOrders, 'DeliveryDate' => $DeliveryDate, 'DeliveryDates' => $DeliveryDates, 'AllDeliveryDates' => $AllDeliveryDates, 'model' => new OrderItem(), 'Category' => $Category, 'Customer' => $Customer, 'CustDeliveryDate' => $CustDeliveryDate, 'curCat' => $cat)); }