public function delete()
 {
     if (empty($this->_data[$this->modeltype]['id'])) {
         $this->dataError();
         sendBack();
     }
     $flash = Flash::Instance();
     $expenseline = $this->_uses[$this->modeltype];
     $expenseline->load($this->_data[$this->modeltype]['id']);
     if ($expenseline->isLoaded() && $expenseline->delete()) {
         $flash->addMessage('Expense Line Deleted');
         if (isset($this->_data['dialog'])) {
             $link = array('modules' => $this->_modules, 'controller' => 'expenses', 'action' => 'view', 'other' => array('id' => $expenseline->expenses_header_id));
             $flash->save();
             echo parent::returnJSONResponse(TRUE, array('redirect' => '/?' . setParamsString($link)));
             exit;
         } else {
             sendTo('expenses', 'view', $this->_modules, array('id' => $expenseline->expenses_header_id));
         }
     }
     $flash->addError('Error deleting Expense Line');
     $this->_data['id'] = $this->_data[$this->modeltype]['id'];
     $this->_data['expenses_header_id'] = $this->_data[$this->modeltype]['expenses_header_id'];
     $this->refresh();
 }
Example #2
0
 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()
 {
     $flash = Flash::Instance();
     $errors = array();
     $data = $this->_data['SInvoiceLine'];
     if (empty($data['invoice_id'])) {
         $errors[] = 'invoice header not defined';
     } else {
         $sinvoice = DataObjectFactory::Factory('SInvoice');
         if (!$sinvoice->load($data['invoice_id'])) {
             $errors[] = 'Cannot find invoice header';
         } elseif ($sinvoice->isLatest($this->_data['SInvoice'], $errors)) {
             $sinvoiceline = SInvoiceLine::Factory($sinvoice, $data, $errors);
             if ($sinvoiceline && count($errors) == 0) {
                 if (!$sinvoiceline->save($sinvoice)) {
                     $errors[] = 'Failed to save Sales ' . $sinvoice->getFormatted('transaction_type') . ' line';
                 }
             }
         }
     }
     if (count($errors) == 0) {
         $flash->addMessage('Sales ' . $sinvoice->getFormatted('transaction_type') . ' Line Saved');
         if (isset($this->_data['saveAnother'])) {
             $other = array('invoice_id' => $sinvoiceline->invoice_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 = 'sinvoices';
             $other = array('id' => $sinvoiceline->invoice_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 {
         $flash->addErrors($errors);
         $this->_data['id'] = $this->_data['SInvoiceLine']['id'];
         $this->_data['invoice_id'] = $this->_data['SInvoiceLine']['invoice_id'];
         $this->refresh();
     }
 }