public function viewByOrders()
 {
     $cc = new ConstraintChain();
     if (isset($this->_data['id'])) {
         $id = $this->_data['id'];
         $cc->add(new Constraint('stitem_id', '=', $id));
     } elseif (isset($this->_data['order_id'])) {
         $order_id = $this->_data['order_id'];
         $cc->add(new Constraint('order_id', '=', $order_id));
     } else {
         $cc->add(new Constraint('type', '=', 'O'));
     }
     $cc->add(new Constraint('status', '=', 'R'));
     $order = new SOrderCollection();
     $order->orderby = array('delivery_note', 'order_number', 'line_number');
     $order->direction = array('ASC', 'ASC', 'ASC');
     $orders = $order->getItemOrders($cc);
     // Create an array of items ordered
     $stitems = array();
     foreach ($orders as $row) {
         $stitems[$row->stitem_id]['in_stock'] = 0;
         if (!isset($stitems[$row->stitem_id]['despatch_action'][$row->despatch_action])) {
             $transferrules = new WHTransferruleCollection(DataObjectFactory::Factory('WHTransferrule'));
             $locations = $transferrules->getFromLocations($row->despatch_action);
             if (count($locations) > 0 and $row->stitem_id) {
                 // Should never be zero or somethingis very wrong!
                 $cc = new ConstraintChain();
                 $cc->add(new Constraint('stitem_id', '=', $row->stitem_id));
                 $cc->add(new Constraint('whlocation_id', 'in', '(' . implode(',', array_keys($locations)) . ')'));
                 $stitems[$row->stitem_id]['despatch_action'][$row->despatch_action] = STBalance::getBalances($cc);
             } else {
                 $stitems[$row->stitem_id]['despatch_action'][$row->despatch_action] = 0;
                 // Flag it as a non-stock item
                 $stitems[$row->stitem_id]['non-stock'] = true;
             }
         }
     }
     // And check orders for stock availability
     // Items not available cannot be despatched!
     $items = array();
     foreach ($orders as $key => $row) {
         // Exclude any order lines that cannot be fulfilled
         // or have already been despatched
         $stitems[$row->stitem_id]['despatch_action'][$row->despatch_action] -= $row->required;
         $sorder = DataObjectFactory::Factory('SOrder');
         $sorder->load($row->order_id);
         $items[$row->order_number]['order_number'] = $row->order_number;
         $items[$row->order_number]['order_id'] = $row->order_id;
         $items[$row->order_number]['customer'] = $row->customer;
         $items[$row->order_number]['del_address'] = $sorder->del_address->address;
         if ($stitems[$row->stitem_id]['non-stock']) {
             $items[$row->order_number]['line_number'][$row->line_number]['despatch'] = true;
         } elseif ($stitems[$row->stitem_id]['despatch_action'][$row->despatch_action] >= 0) {
             $items[$row->order_number]['line_number'][$row->line_number]['despatch'] = true;
         } else {
             $items[$row->order_number]['line_number'][$row->line_number]['despatch'] = false;
         }
         $items[$row->order_number]['line_number'][$row->line_number]['despatch_action'] = $row->despatch_action;
         $items[$row->order_number]['due_despatch_date'] = $row->due_despatch_date;
         $items[$row->order_number]['line_number'][$row->line_number]['stitem'] = $row->stitem;
         $items[$row->order_number]['line_number'][$row->line_number]['item_description'] = $row->item_description;
         $items[$row->order_number]['line_number'][$row->line_number]['delivery_note'] = $row->delivery_note;
         $items[$row->order_number]['line_number'][$row->line_number]['required'] = $row->required;
         $items[$row->order_number]['line_number'][$row->line_number]['stuom'] = $row->stuom;
         $items[$row->order_number]['line_number'][$row->line_number]['id'] = $row->id;
     }
     $this->view->set('orders', $items);
     $sidebar = new SidebarController($this->view);
     $actions = array();
     $actions['viewnotes'] = array('link' => array_merge($this->_modules, array('controller' => $this->name, 'action' => 'index')), 'tag' => 'view despatch notes');
     $sidebar->addList('Actions', $actions);
     $this->view->register('sidebar', $sidebar);
     $this->view->set('sidebar', $sidebar);
     $this->view->set('page_title', $this->getPageName('Despatch', 'View Order Lines for'));
 }
示例#2
0
 public function viewByOrders()
 {
     $cc = new ConstraintChain();
     $cc->add(new Constraint('type', '=', 'O'));
     if (isset($this->_data['id'])) {
         $id = $this->_data['id'];
         $cc->add(new Constraint('stitem_id', '=', $id));
     }
     $order = new SOrderCollection($this->_templateobject);
     $order->orderby = array('status', 'due_despatch_date', 'order_number');
     $order->direction = array('DESC', 'ASC');
     $orders = $order->getItemOrders($cc);
     // Create an array of items ordered
     $stitems = array();
     foreach ($orders as $row) {
         // ignore PLs without stitem
         if ($row->stitem_id) {
             $stitems[$row->stitem_id]['shortfall'] = 0;
         }
     }
     // Now get the balance for each item across all saleable locations
     foreach ($stitems as $key => $item) {
         $cc = new ConstraintChain();
         $cc->add(new Constraint('stitem_id', '=', $key));
         $stitems[$key]['in_stock'] = STBalance::getBalances($cc);
         $salelocations = WHLocation::getSaleLocations();
         if (empty($salelocations)) {
             $stitems[$key]['for_sale'] = 0;
         } else {
             $cc->add(new Constraint('whlocation_id', 'in', '(' . implode(',', $salelocations) . ')'));
             $stitems[$key]['for_sale'] = STBalance::getBalances($cc);
         }
         $stitems[$key]['in_stock'] -= $stitems[$key]['for_sale'];
         $stitems[$key]['total_orders'] = 0;
     }
     // And finally update the orders with the projected stock balances
     $items = array();
     foreach ($orders as $key => $row) {
         // echo 'count '.$row->id.' - '.SOrder::lineExistsInDespatchLines($row->id).'<br>';
         $items[$key]['id'] = $row->id;
         $items[$key]['stitem_id'] = $row->stitem_id;
         $items[$key]['stitem'] = $row->stitem;
         $items[$key]['item_description'] = $row->item_description;
         $items[$key]['productline_id'] = $row->productline_id;
         $items[$key]['required'] = $row->required;
         $items[$key]['due_despatch_date'] = $row->due_despatch_date;
         $items[$key]['order_number'] = $row->order_number;
         $items[$key]['order_id'] = $row->order_id;
         $items[$key]['customer'] = $row->customer;
         $items[$key]['slmaster_id'] = $row->slmaster_id;
         $items[$key]['stuom'] = $row->stuom;
         $items[$key]['for_sale'] = $stitems[$row->stitem_id]['for_sale'];
         $items[$key]['shortfall'] = 0;
         // ignore PLs without stitem
         if ($row->stitem_id) {
             $cc = new ConstraintChain();
             $cc->add(new Constraint('stitem_id', '=', $row->stitem_id));
             $cc->add(new Constraint('required_by', '<=', $row->due_despatch_date));
             $worders = MFWorkorder::getBalances($cc);
             if ($worders) {
                 $stitems[$row->stitem_id]['on_order'] = $worders[0]['sumbalance'] - $stitems[$row->stitem_id]['total_orders'];
                 $stitems[$row->stitem_id]['total_orders'] = $worders[0]['sumbalance'];
             } else {
                 $stitems[$row->stitem_id]['on_order'] = 0;
             }
         }
         $on_order = $stitems[$row->stitem_id]['on_order'];
         $items[$key]['on_order'] = $stitems[$row->stitem_id]['on_order'];
         $stitems[$row->stitem_id]['for_sale'] -= $items[$key]['required'];
         if ($stitems[$row->stitem_id]['for_sale'] < 0) {
             $stitems[$row->stitem_id]['in_stock'] += $stitems[$row->stitem_id]['for_sale'];
             $stitems[$row->stitem_id]['for_sale'] = 0;
         }
         $stitems[$row->stitem_id]['in_stock'] = $stitems[$row->stitem_id]['in_stock'] - $stitems[$row->stitem_id]['shortfall'] + $stitems[$row->stitem_id]['on_order'];
         // $stitems[$row->stitem_id]['in_stock']=$stitems[$row->stitem_id]['in_stock']-$stitems[$row->stitem_id]['shortfall'];
         if ($stitems[$row->stitem_id]['in_stock'] < 0) {
             $stitems[$row->stitem_id]['shortfall'] -= $stitems[$row->stitem_id]['in_stock'];
             $stitems[$row->stitem_id]['in_stock'] = 0;
             $items[$key]['shortfall'] = $stitems[$row->stitem_id]['shortfall'] < $on_order ? 0 : $stitems[$row->stitem_id]['shortfall'] - $on_order;
             // $items[$key]['shortfall']=$stitems[$row->stitem_id]['shortfall'];
         } else {
             $stitems[$row->stitem_id]['shortfall'] = 0;
         }
         $items[$key]['in_stock'] = $stitems[$row->stitem_id]['in_stock'];
         if (!empty($row->delivery_note)) {
             $items[$key]['status'] = 'Awaiting Despatch';
         } elseif ($row->status == 'R') {
             $items[$key]['status'] = 'Ready for Despatch';
         } else {
             $items[$key]['status'] = '';
         }
         $items[$key]['account_status'] = $row->account_status;
         $items[$key]['despatch_number'] = SOrder::lineExistsInDespatchLines($row->id);
         if ($row->status == 'R') {
             $items[$key]['despatch'] = true;
         } else {
             $items[$key]['despatch'] = false;
         }
     }
     $this->view->set('orders', $items);
     $sidebar = new SidebarController($this->view);
     $actions = array();
     $actions['allcustomer'] = array('link' => array('module' => 'sales_ledger', 'controller' => 'SLCustomers', 'action' => 'index'), 'tag' => 'view all customers');
     $actions['newquote'] = array('link' => array('modules' => $this->_modules, 'controller' => $this->name, 'action' => 'new', 'type' => 'Q'), 'tag' => 'new quote');
     $actions['neworder'] = array('link' => array('modules' => $this->_modules, 'controller' => $this->name, 'action' => 'new', 'type' => 'O'), 'tag' => 'new order');
     $actions['vieworder'] = array('link' => array('modules' => $this->_modules, 'controller' => $this->name, 'action' => 'index'), 'tag' => 'view quotes/orders');
     $actions['viewdespatches'] = array('link' => array('module' => 'despatch', 'controller' => 'sodespatchlines', 'action' => 'viewByOrders'), 'tag' => 'view despatches');
     $sidebar->addList('Actions', $actions);
     $this->view->register('sidebar', $sidebar);
     $this->view->set('sidebar', $sidebar);
     $this->view->set('page_title', $this->getPageName('', 'View availability by'));
 }