public function saveStatus() { if (!$this->loadData()) { $this->dataError(); sendBack(); } $transaction = $this->_uses[$this->modeltype]; $flash = Flash::Instance(); $errors = array(); $data = $this->_data[$this->modeltype]; $twin_transaction = null; if ($data['revised_qty'] < 0) { $errors[] = 'Quantity must be a positive number'; } if ($transaction && $transaction->current_balance() < $data['revised_qty']) { $errors[] = 'There is insufficient balance to resolve this error'; } if ($transaction && count($errors) == 0) { $twin_transaction = $transaction->getTwinTransaction(); } else { $flash->addErrors($errors); $this->_data['id'] = $data['id']; $this->_data['status'] = $data['status']; $this->changeStatus(); $this->_templateName = $this->getTemplateName('changestatus'); return; } $success = true; $db = DB::Instance(); $db->StartTrans(); $transaction->status = $data['status']; $transaction->remarks = $data['remarks']; if ($transaction->save($errors) === false) { $errors[] = 'Error saving transaction status'; $success = false; $db->FailTrans(); } elseif ($twin_transaction) { $twin_transaction->status = $data['status']; $twin_transaction->remarks = $data['remarks']; if ($twin_transaction->save($errors) === false) { $errors[] = 'Error saving transaction status'; $success = false; $db->FailTrans(); } } if ($success && $data['status'] == 'R') { $new_transactions = array($transaction); if ($twin_transaction) { $new_transactions[] = $twin_transaction; } $transfer_rule = new WHTransferrule(); $transfer_id = $transfer_rule->getTransferId(); $skip_fields = array('id', 'transfer_id', 'created', 'balance', 'status'); foreach ($new_transactions as $key => $new_transaction) { $new_data = array(); $fields = $new_transaction->toArray(); foreach ($fields as $field_name => $field) { if (in_array($field_name, $skip_fields)) { continue; } $new_data[$field_name] = $field['value']; } $new_data['transfer_id'] = $transfer_id->transfer_id; $new_data['balance'] = 0; $new_data['status'] = 'O'; $new_data['qty'] = $new_data['error_qty'] < 0 ? $data['revised_qty'] * -1 : $data['revised_qty']; $new_data['error_qty'] = 0; $new_transactions[$key] = STTransaction::Factory($new_data, $errors, $this->modeltype); if ($new_transactions[$key] !== false) { continue; } $errors[] = 'Error transferring stock'; $success = false; break; } if ($success) { foreach ($new_transactions as $new_transaction) { $new_transaction->save($errors); if (count($errors) == 0) { continue; } $errors[] = 'Error transferring stock'; $success = false; $db->FailTrans(); break; } } } $db->CompleteTrans(); if ($success) { $flash->addMessage('Transaction status changed successfully'); sendTo($this->name, 'view', $this->_modules, array('id' => $data['id'])); } else { $flash->addErrors($errors); $this->_data['id'] = $data['id']; $this->_data['status'] = $data['status']; $this->changeStatus(); $this->_templateName = $this->getTemplateName('changestatus'); } }
public function getToLocations($_whlocation_id = '', $_whaction_id = '') { // used by ajax to get a list of locations for a given WH Action and From Location if (isset($this->_data['ajax'])) { if (!empty($this->_data['whlocation_id'])) { $_whlocation_id = $this->_data['whlocation_id']; } if (!empty($this->_data['whaction_id'])) { $_whaction_id = $this->_data['whaction_id']; } } $transfer_rule = new WHTransferrule(); $locations = $transfer_rule->getToLocations($_whaction_id, $_whlocation_id); if (isset($this->_data['ajax'])) { $this->view->set('options', $locations); $this->setTemplateName('select_options'); } else { return $locations; } }