private function getItemPlan()
 {
     // Get the list of saleable items
     $s_data = array();
     // Set context from calling module
     if (isset($this->_data['stitem_id'])) {
         $s_data['stitem_id'] = $this->_data['stitem_id'];
     }
     if (isset($this->_data['prod_group_id'])) {
         $s_data['prod_group_id'] = $this->_data['prod_group_id'];
     }
     $this->setSearch('productlinesSearch', 'customerItems', $s_data);
     $items = new SOProductlineHeaderCollection($this->_templateobject);
     $sh = $this->setSearchHandler($items);
     $items->getItems($sh);
     parent::index($items, $sh);
     // Now get totals for Sales Orders, In Stock, Work Orders
     $itemplan = array();
     foreach ($items as $item) {
         $stitem = DataObjectFactory::Factory('STItem');
         if ($stitem->load($item->stitem_id)) {
             $itemplan[$item->stitem_id]['stitem_id'] = $item->stitem_id;
             $itemplan[$item->stitem_id]['stitem'] = $item->stitem;
             $itemplan[$item->stitem_id]['uom_name'] = $item->uom_name;
             $itemplan[$item->stitem_id]['in_stock'] = $stitem->currentBalance();
             $itemplan[$item->stitem_id]['required'] = 0;
             $itemplan[$item->stitem_id]['on_order']['po_value'] = 0;
             $itemplan[$item->stitem_id]['on_order']['wo_value'] = 0;
             $porders = $stitem->getPOrderLines();
             foreach ($porders as $porder) {
                 $itemplan[$item->stitem_id]['on_order']['po_value'] += round($stitem->convertToUoM($porder->stuom_id, $stitem->uom_id, $porder->os_qty), $stitem->qty_decimals);
             }
             $sorders = $stitem->getSOrderLines();
             foreach ($sorders as $sorder) {
                 $itemplan[$item->stitem_id]['required'] += round($stitem->convertToUoM($sorder->stuom_id, $stitem->uom_id, $sorder->os_qty), $stitem->qty_decimals);
             }
             $worders = $stitem->getWorkOrders();
             foreach ($worders as $worder) {
                 $itemplan[$item->stitem_id]['on_order']['wo_value'] += round($worder->order_qty - $worder->made_qty < 0 ? 0 : $worder->order_qty - $worder->made_qty, $stitem->qty_decimals);
             }
             $salelocations = WHLocation::getSaleLocations();
             if (empty($salelocations)) {
                 $itemplan[$item->stitem_id]['for_sale'] = 0;
             } else {
                 $cc = new ConstraintChain();
                 $cc->add(new Constraint('stitem_id', '=', $item->stitem_id));
                 $cc->add(new Constraint('whlocation_id', 'in', '(' . implode(',', $salelocations) . ')'));
                 $itemplan[$item->stitem_id]['for_sale'] = STBalance::getBalances($cc);
             }
             $itemplan[$item->stitem_id]['in_stock'] -= $itemplan[$item->stitem_id]['for_sale'];
             $itemplan[$item->stitem_id]['actual_shortfall'] = $itemplan[$item->stitem_id]['required'] - ($itemplan[$item->stitem_id]['for_sale'] + $itemplan[$item->stitem_id]['in_stock']);
             $itemplan[$item->stitem_id]['shortfall'] = $itemplan[$item->stitem_id]['actual_shortfall'] - $itemplan[$item->stitem_id]['on_order']['po_value'] - $itemplan[$item->stitem_id]['on_order']['wo_value'];
             $itemplan[$item->stitem_id]['indicator'] = 'green';
             if ($itemplan[$item->stitem_id]['actual_shortfall'] <= 0) {
                 $itemplan[$item->stitem_id]['actual_shortfall'] = 0;
             } else {
                 $itemplan[$item->stitem_id]['indicator'] = 'amber';
             }
             if ($itemplan[$item->stitem_id]['shortfall'] <= 0) {
                 $itemplan[$item->stitem_id]['shortfall'] = 0;
             } else {
                 $itemplan[$item->stitem_id]['indicator'] = 'red';
             }
         }
     }
     return $itemplan;
 }
示例#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'));
 }