public static function Factory(SOrder $header, $line_data, &$errors) { if (empty($line_data['order_id'])) { $line_data['order_id'] = $header->id; } if (empty($line_data['line_number'])) { $line_data['line_number'] = $header->getNextLineNumber(); } $line_data['item_description'] = $line_data['description']; if ($line_data['productline_id'] == -1) { $line_data['productline_id'] = ''; $line_data['stitem_id'] = ''; } else { $productline = DataObjectFactory::Factory('SOProductline'); $productline->load($line_data['productline_id']); if ($productline->isLoaded()) { $productlineheader = $productline->product_detail; if (is_null($productlineheader->stitem_id)) { $line_data['item_description'] = $productline->getDescription(); $line_data['stitem_id'] = ''; } else { $line_data['item_description'] = $productlineheader->stitem; $line_data['stitem_id'] = $productlineheader->stitem_id; } if (empty($line_data['price'])) { $line_data['price'] = $productline->getPrice('', '', $productline->slmaster_id); } if (empty($line_data['glaccount_id'])) { $line_data['glaccount_id'] = $productline->glaccount_id; } if (empty($line_data['glcentre_id'])) { $line_data['glcentre_id'] = $productline->glcentre_id; } if (empty($line_data['stuom_id'])) { $line_data['stuom_id'] = $productlineheader->stuom_id; } if (empty($line_data['tax_rate_id'])) { $line_data['tax_rate_id'] = $productlineheader->tax_rate_id; } } // Check if glaccount_centre_id exists - can be any value including null if (!array_key_exists('glaccount_centre_id', $line_data)) { $line_data['glaccount_centre_id'] = GLAccountCentre::getAccountCentreId($line_data['glaccount_id'], $line_data['glcentre_id'], $errors); } if (empty($line_data['net_value'])) { $line_data['net_value'] = bcmul($line_data['price'], $line_data['revised_qty']); } } if ($line_data['revised_qty'] > 0 && $line_data['price'] > 0) { if (empty($line_data['id'])) { // New Line $line_data['order_qty'] = $line_data['os_qty'] = $line_data['revised_qty']; $line_data['status'] = 'N'; } else { // Amended Line if ($line_data['status'] == 'N') { $line_data['os_qty'] = $line_data['revised_qty']; } } } else { $errors[] = 'Zero quantity or net value'; } if (count($errors) > 0) { return false; } if (empty($line_data['description'])) { $line_data['description'] = $line_data['item_description']; } $line_data['line_discount'] = 0; $line_data['currency_id'] = $header->currency_id; $line_data['rate'] = $header->rate; $line_data['twin_currency_id'] = $header->twin_currency_id; $line_data['twin_rate'] = $header->twin_rate; $line_data['base_net_value'] = bcadd(round(bcdiv($line_data['net_value'], $line_data['rate'], 4), 2), 0); $line_data['twin_net_value'] = bcadd(round(bcmul($line_data['base_net_value'], $line_data['twin_rate'], 4), 2), 0); if (empty($line_data['due_delivery_date'])) { $line_data['due_delivery_date'] = un_fix_date($header->due_date); } if (empty($line_data['due_despatch_date'])) { $line_data['due_despatch_date'] = un_fix_date($header->despatch_date); } return parent::Factory($line_data, $errors, 'SOrderLine'); }
public function save_model($data) { // Used to save Order Header and Order Lines from import or copy of existing $flash = Flash::Instance(); if (empty($data['SOrder']) || empty($data['SOrderLine'])) { $flash->addError('Error trying to save order'); return false; } $errors = array(); $db = DB::Instance(); $db->StartTrans(); $header = $data['SOrder']; $lines_data = DataObjectCollection::joinArray($data['SOrderLine'], 0); if (!$lines_data || empty($lines_data)) { $lines_data[] = $data['SOrderLine']; } $order = SOrder::Factory($header, $errors); if (!$order || count($errors) > 0) { $errors[] = 'Order validation failed'; } elseif (!$order->save()) { $errors[] = 'Order creation failed'; } foreach ($lines_data as $line) { $line['order_id'] = $order->{$order->idField}; $orderline = SOrderLine::Factory($order, $line, $errors); if (!$orderline || count($errors) > 0) { $errors[] = 'Order Line validation failed for line ' . $line['line_number']; } elseif (!$orderline->save()) { $errors[] = 'Order Line creation failed for line ' . $line['line_number']; } } if (count($errors) === 0) { if (!$order->save()) { $errors[] = 'Error updating Sales Order totals'; } else { $result = array('internal_id' => $order->{$order->idField}, 'internal_identifier_field' => $order->identifierField, 'internal_identifier_value' => $order->getidentifierValue()); } } if (count($errors) > 0) { $flash->addErrors($errors); $db->FailTrans(); $result = false; } $db->CompleteTrans(); return $result; }
public function getMaterialsTotals() { // do not have project_id on SO // probably need new table to link project/task to PO/SO $orders = new POrder(); $cc = new ConstraintChain(); $cc->add(new Constraint('project_id', '=', $this->id)); $totals['Materials']['total_costs'] = $orders->getSumFields(array('net_value'), $cc); $orders = new SOrder(); $cc = new ConstraintChain(); $cc->add(new Constraint('project_id', '=', $this->id)); $totals['Materials']['total_charges'] = $orders->getSumFields(array('net_value'), $cc); return $totals; }
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')); }