private function moveStock(&$errors = array()) { $customer = DataObjectFactory::Factory('SLCustomer'); $customer = $customer->load($this->slmaster_id); if ($customer) { $stock_errors = false; foreach ($this->lines as $line) { if ($line->move_stock == 't' && !is_null($line->stitem_id)) { $data = array(); if ($this->transaction_type == 'I') { $data['qty'] = $line->sales_qty; $data['process_name'] = 'SI'; } else { // Reverse the transaction for a Credit Note $data['qty'] = bcmul($line->sales_qty, -1); $data['process_name'] = 'S' . $this->transaction_type; } $data['process_id'] = $this->id; $data['stitem_id'] = $line->stitem_id; if (is_null($line->sales_order_id)) { $data['whaction_id'] = $customer->despatch_action; } else { $sorder = DataObjectFactory::Factory('SOrder'); $sorder->load($line->sales_order_id); $data['whaction_id'] = $sorder->despatch_action; } $result = false; if (STTransaction::getTransferLocations($data, $errors)) { $st_errors = array(); $models = STTransaction::prepareMove($data, $st_errors); if (count($st_errors) === 0) { foreach ($models as $model) { $result = $model->save($st_errors); if ($result === false) { $stock_errors = true; } } } if (count($st_errors) > 0) { $errors = array_merge($errors, $st_errors); } } else { $errors[] = 'Error getting transfer locations'; return false; } } } if ($stock_errors) { $errors[] = 'Error updating stock'; return false; } return true; } $errors[] = 'Failed to load Customer details'; return false; }
public function confirm_despatch() { if (!$this->checkParams('SODespatchLine')) { sendBack(); } $flash = Flash::Instance(); $db = DB::Instance(); $db->StartTrans(); $errors = array(); foreach ($this->_data['SODespatchLine'] as $key => $value) { if (isset($value['confirm_despatch'])) { // Check Customer if (!isset($value['slmaster_id'])) { $errors['DN' . $key] = 'Cannot find Customer reference for DN' . $key; $db->FailTrans(); continue; } $customer = DataObjectFactory::Factory('SLCustomer'); $customer->load($value['slmaster_id']); if (!$customer->isLoaded()) { $errors['DN' . $key] = 'Cannot find Customer details for DN' . $key; $db->FailTrans(); continue; } elseif ($customer->accountStopped()) { $errors['DN' . $key] = 'Cannot Confirm Despatch for DN' . $key . ' (' . $customer->name . ') Account Stopped'; $db->FailTrans(); continue; } // Get all the despatch lines for this Despatch Note $despatches = new SODespatchLineCollection($this->_templateobject); $sh = new SearchHandler($despatches, false); $sh->addConstraint(new Constraint('despatch_number', '=', $key)); $despatches->load($sh); foreach ($despatches as $despatch) { if ($despatch->stitem_id != '') { // Create transaction pair for Dispatch $data = array(); $data['qty'] = $despatch->despatch_qty; $data['process_name'] = 'D'; $data['process_id'] = $despatch->despatch_number; $data['whaction_id'] = $despatch->despatch_action; $data['stitem_id'] = $despatch->stitem_id; $result = false; if (STTransaction::getTransferLocations($data, $errors)) { $models = STTransaction::prepareMove($data, $errors); if (count($errors) === 0) { foreach ($models as $model) { $result = $model->save($errors); if ($result === false) { break; } } } } if ($result === false) { $flash->addErrors($errors); $flash->addError('Error updating stock'); $db->FailTrans(); sendBack(); } } $despatchline = DataObjectFactory::Factory('SODespatchLine'); $result = $despatchline->update($despatch->id, 'status', 'D'); if ($result === false) { $flash->addError('Error updating Despatch Note ' . $despatch_note); $db->FailTrans(); sendBack(); } $orderline = DataObjectFactory::Factory('SOrderLine'); $orderline->load($despatch->orderline_id); $data = array(); $data['id'] = $despatch->orderline_id; $data['os_qty'] = $orderline->os_qty - $despatch->despatch_qty; $data['del_qty'] = $despatch->despatch_qty; $data['actual_despatch_date'] = date(DATE_FORMAT); $data['status'] = 'D'; $orderline = DataObject::Factory($data, $errors, 'SOrderLine'); if ($orderline) { $result = $orderline->save(); } else { $result = false; } if ($result === false) { $flash->addError('Error updating order line ' . $db->ErrorMsg()); $db->FailTrans(); sendBack(); } $order = DataObjectFactory::Factory('SOrder'); if ($order->load($orderline->order_id)) { if (!$order->save()) { $flash->addError('Error updating order ' . $db->ErrorMsg()); $db->FailTrans(); sendBack(); } } } } } if (count($errors) === 0 && $db->CompleteTrans()) { $flash->addMessage('Despatches confirmed'); sendTo($this->name, 'index', $this->_modules); } else { $flash->addErrors($errors); $db->FailTrans(); sendBack(); } }