public static function purchaseOrders($search_data = null, &$errors, $defaults = null) { $search = new ProjectcostchargeSearch($defaults); $search->addSearchField('project_id', 'project_id', 'hidden', '', 'hidden', false); $search->addSearchField('plmaster_id', 'Supplier', 'select'); $account = new PLSupplier(); $accounts = $account->getAll(); $options = array('' => 'All'); $options += $accounts; $search->setOptions('plmaster_id', $options); $search->addSearchField('order_id', 'Orders', 'select'); $unassigned_list = new POrderLine(); $unassigned_list->idField = 'order_id'; $unassigned_list->identifierField = 'order_number'; $cc = new ConstraintChain(); $subquery = "select item_id from project_costs_charges where item_type='PO'"; $cc->add(new Constraint('id', 'not in', '(' . $subquery . ')')); $search->setOptions('order_id', $unassigned_list->getAll($cc, false, true)); $search->setSearchData($search_data, $errors); return $search; }
public function save() { $db = DB::Instance(); $flash = Flash::Instance(); $errors = array(); $data = $this->_data[$this->modeltype]; if (empty($data['order_id'])) { $errors[] = 'Order header not defined'; } else { $porder = DataObjectFactory::Factory('POrder'); if (!$porder->load($data['order_id'])) { $errors[] = 'Cannot find order header'; } } $db->StartTrans(); if ($porder->isLoaded() && $porder->isLatest($this->_data['POrder'], $errors)) { if (isset($data['cancel_line'])) { $data['status'] = $this->_templateobject->cancelStatus(); if ($this->_templateobject->update($data['id'], array('status', 'glaccount_centre_id'), array($this->_templateobject->cancelStatus(), 'null')) && $porder->save()) { $flash->addMessage($porder->getFormatted('type') . ' Line cancelled'); } else { $errors[] = 'Failed to cancel ' . $porder->getFormatted('type') . ' Line'; } } elseif (isset($data['complete_line'])) { $porderline = $this->_uses[$this->modeltype]; $porderline->load($data['id']); $poreceivedline = DataObjectFactory::Factory('POReceivedLine'); if ($porderline->del_qty == $poreceivedline->invoicedQty($data['id'])) { $fields = array('status', 'glaccount_centre_id'); $values = array($this->_templateobject->invoiceStatus(), 'null'); } else { $fields = array('status'); $values = array($this->_templateobject->receivedStatus()); } $fields[] = 'os_qty'; $values[] = 0; if ($this->_templateobject->update($data['id'], $fields, $values) && $porder->save()) { $flash->addMessage($porder->getFormatted('type') . ' Line completed'); } else { $errors[] = 'Failed to complete ' . $porder->getFormatted('type') . ' Line'; } } elseif ($data['status'] == $this->_templateobject->newStatus() || $data['status'] == $this->_templateobject->awaitingDeliveryStatus() || $data['status'] == $this->_templateobject->partReceivedStatus() || $data['status'] == $this->_templateobject->receivedStatus()) { $porderline = POrderLine::Factory($porder, $data, $errors); if ($porderline && count($errors) == 0) { if ($porder->due_date < $porderline->due_delivery_date) { $porder->due_date = $porderline->due_delivery_date; } if (!$porderline->save($porder, $errors)) { $errors[] = 'Failed to save ' . $porder->getFormatted('type') . ' Line'; } else { $flash->addMessage($porder->getFormatted('type') . ' Line Saved'); } } } } if (count($errors) == 0) { $db->CompleteTrans(); if (isset($this->_data['saveAnother'])) { $other = array('order_id' => $data['order_id']); if (isset($this->_data['dialog'])) { $other += array('dialog' => ''); } if (isset($this->_data['ajax'])) { $other += array('ajax' => ''); } sendTo($this->name, 'new', $this->_modules, $other); } else { $action = 'view'; $controller = 'porders'; $other = array('id' => $data['order_id']); } if (isset($this->_data['dialog'])) { $link = array('modules' => $this->_modules, 'controller' => $controller, 'action' => $action, 'other' => $other); $flash->save(); echo parent::returnJSONResponse(TRUE, array('redirect' => '/?' . setParamsString($link))); exit; } else { sendTo($controller, $action, $this->_modules, $other); } } else { $db->FailTrans(); $db->CompleteTrans(); $flash->addErrors($errors); $this->_data['id'] = $this->_data[$this->modeltype]['id']; $this->_data['order_id'] = $this->_data[$this->modeltype]['order_id']; $this->refresh(); } }
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['POrder']) || empty($data['POrderLine'])) { $flash->addError('Error trying to import order'); return false; } $errors = array(); $db = DB::Instance(); $db->StartTrans(); $header = $data['POrder']; $lines_data = DataObjectCollection::joinArray($data['POrderLine'], 0); if (!$lines_data || empty($lines_data)) { $lines_data[] = $data['POrderLine']; } $order = POrder::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 = POrderLine::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 Purchase 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; }