/** * @param void * @return ConstraintChain * * Returns a constraintchain containing OR'd constraints for each status checked on the form */ public function toConstraint() { $cc = false; if (!is_array($this->value)) { $this->value = array($this->value); } $cc = new ConstraintChain(); $codes = $this->value_set ? $this->value : array_flip($this->default); $db = DB::Instance(); $date = fix_date(date(DATE_FORMAT)); foreach ($codes as $code => $on) { if ($code != '') { switch ($code) { case 'Raised by me': $cc->add(new Constraint('raised_by', '=', getCurrentUser()->username)); break; case 'Other Orders': $cc->add(new Constraint('raised_by', '!=', getCurrentUser()->username), 'OR'); break; case 'Authorised by me': $c = new ConstraintChain(); $c->add(new Constraint('authorised_by', '=', getCurrentUser()->username)); $c->add(new Constraint('date_authorised', 'is not', 'NULL')); $cc->add($c, 'OR'); break; case 'Awaiting Authorisation': $awaitingauth = new POAwaitingAuthCollection(new POAwaitingAuth()); $authlist = $awaitingauth->getOrderList(getCurrentUser()->username); if (empty($authlist)) { $authlist = '-1'; } $c = new ConstraintChain(); $c->add(new Constraint('type', '=', 'R')); $c->add(new Constraint('authorised_by', 'is', 'NULL')); $c->add(new Constraint('raised_by', '!=', getCurrentUser()->username)); $c->add(new Constraint('id', 'in', '(' . $authlist . ')')); $cc->add($c, 'OR'); break; } } } return $cc; }
public function save(&$errors = array()) { // use_sorder_delivery == true makes no sense if a sales order link is not selected if (!$this->sales_order_id) { $this->use_sorder_delivery = 'f'; } $linestatuses = $this->getLineStatuses(); $linestatus = $linestatuses['count']; if (($this->someLinesReceived($linestatus) || $this->someLinesInvoiced($linestatus)) && !$this->allLinesReceivedOrInvoiced($linestatus)) { $this->status = $this->partReceivedStatus(); } elseif ($this->allLinesCancelled($linestatus)) { $this->status = $this->cancelStatus(); } elseif ($this->allLinesReceived($linestatus) || $this->allLinesReceivedOrInvoiced($linestatus) && !$this->allLinesInvoiced($linestatus)) { $this->status = $this->receivedStatus(); } elseif ($this->allLinesInvoiced($linestatus)) { $this->status = $this->invoiceStatus(); } elseif ($this->allLinesAwaitingDelivery($linestatus) && $this->status != $this->acknowledgedStatus()) { $this->status = $this->orderSentStatus(); } elseif ($this->allLinesNew($linestatus)) { $this->status = $this->newStatus(); } $prev_net_value = $this->base_net_value; $po_line = DataObjectFactory::Factory('POrderLine'); $cc = new ConstraintChain(); $cc->add(new Constraint('order_id', '=', $this->id)); $cc->add(new Constraint('status', '!=', $po_line->cancelStatus())); $totals = $po_line->getSumFields(array('net_value', 'twin_net_value', 'base_net_value'), $cc, 'po_lines'); unset($totals['numrows']); // set the correct totals back to the order header foreach ($totals as $field => $value) { $this->{$field} = bcadd($value, 0); } // Check the authorisation limits on the order if ($this->type != 'T' && $prev_net_value != $this->base_net_value) { $porderlines_summary = new POrderLineCollection($po_line); $porderlines_summary->getAuthSummary($this->id); $this->authorised_by = $this->checkAuthLimits($porderlines_summary); if (!is_null($this->authorised_by) && EGS_USERNAME == $this->authorised_by && $this->base_net_value > 0 && $porderlines_summary->count() > 0) { $this->type = 'O'; $this->date_authorised = date(DATE_FORMAT); } elseif ($this->type == 'O') { $this->type = 'R'; $this->authorised_by = $this->date_authorised = null; $awaitingauth = new POAwaitingAuthCollection(); $awaitingauth->loadBy(null, $this->id); $awaitingauth->deleteAll(); $result = $this->saveAuthList($porderlines_summary, $errors); } } $db = DB::Instance(); $db->StartTrans(); $result = parent::save(); if (!$result) { $errors[] = 'Error saving order header : ' . $db->errorMsg(); $db->FailTrans(); } $db->CompleteTrans(); return $result; }