public function save_despatchnote() { if (!$this->checkParams('sodespatchlines')) { sendBack(); } $flash = Flash::Instance(); $db = DB::Instance(); $db->StartTrans(); $errors = array(); $despatch = array(); $despatchline = array(); // Group Despatch Lines By Order foreach ($this->_data['sodespatchlines'] as $key => $value) { $orderline = DataObjectFactory::Factory('SOrderLine'); $orderline->load($key); if ($orderline) { $order = DataObjectFactory::Factory('SOrder'); $order->load($orderline->order_id); $despatch[$order->id][$orderline->id] = SODespatchLine::makeLine($order, $orderline, $errors); } } if (SODespatchLine::createDespatchNote($despatch, $errors) && count($errors) === 0 && $db->CompleteTrans()) { $flash->addMessage('Despatch Notes added successfully'); sendTo($this->name, 'index', $this->_modules); } else { $errors[] = 'Error creating Despatch Note'; $flash->addErrors($errors); $db->FailTrans(); $db->CompleteTrans(); $this->refresh(); } }
/** * createSODespatchNote - Create a Despatch Note From the Sales Order * * Marks any order lines with status 'New' as released and creates a despatch note. * Excludes order lines that are not linked to a productline or where the product * is marked as not 'despatcheable'. */ public function createSODespatchNote() { $flash = Flash::Instance(); $db = DB::Instance(); $db->StartTrans(); // Release the order lines $orderline = DataObjectFactory::Factory('SOrderLine'); $orderlines = new SOrderLineCollection($orderline); $sh = new SearchHandler($orderlines, false); $sh->addConstraint(new Constraint('status', '=', $orderline->newStatus())); $sh->addConstraint(new Constraint('order_id', '=', $this->_data['id'])); $sh->addConstraint(new Constraint('productline_id', 'IS NOT', 'NULL')); $sh->addConstraint(new Constraint('not_despatchable', 'IS NOT', true)); $orderlines->load($sh); if (count($orderlines) > 0) { $errors = array(); $despatch = array(); $despatchline = array(); $order = DataObjectFactory::Factory('SOrder'); $order->load($this->_data['id']); foreach ($orderlines as $line) { if ($line->delivery_note == '' && $line->stitem == '') { $line->update($line->id, 'status', $line->awaitingDespatchStatus()); $despatch[$this->_data['id']][$line->id] = SODespatchLine::makeLine($order, $line, $errors); } } $despatch_num = SODespatchLine::createDespatchNote($despatch, $errors); if ($despatch_num && count($errors) === 0 && $db->CompleteTrans()) { // Redirect to the desptch note view sendTo('sodespatchlines', 'view', 'despatch', ['id' => $despatch_num]); } else { $errors[] = 'Error creating Despatch Note'; $flash->addErrors($errors); $db->FailTrans(); $db->CompleteTrans(); $this->refresh(); } } else { $flash->addMessage("No lines available to create a despatch note"); sendBack(); } }