Ejemplo n.º 1
0
 public function save()
 {
     if (!$this->CheckParams($this->modeltype)) {
         sendBack();
     }
     $flash = Flash::Instance();
     $errors = array();
     $db = DB::Instance();
     $db->StartTrans();
     $current_accounts = array();
     if (isset($this->_data[$this->modeltype]['id'])) {
         $centre = $this->_templateobject;
         $centre->load($this->_data[$this->modeltype]['id']);
         if ($centre->isLoaded()) {
             foreach ($centre->accounts as $accountcentre) {
                 // Delete any existing entries not in the supplied list
                 if (!empty($this->_data[$this->modeltype]['account_id']) && in_array($accountcentre->glaccount_id, $this->_data[$this->modeltype]['account_id'])) {
                     $current_accounts[$accountcentre->glaccount_id] = $accountcentre->glaccount_id;
                 } elseif (!$accountcentre->delete(null, $errors)) {
                     $errors[] = 'Failed to update Account Centre Reference ' . $accountcentre->glaccount;
                 }
             }
         }
     }
     if (count($errors) === 0 && parent::save_model($this->modeltype)) {
         if (isset($this->_data[$this->modeltype]['account_id'])) {
             foreach ($this->_data[$this->modeltype]['account_id'] as $account_id) {
                 if (!key_exists($account_id, $current_accounts)) {
                     $data = array();
                     $data['glaccount_id'] = $account_id;
                     $data['glcentre_id'] = $this->saved_model->id;
                     $accountcentre = GLAccountCentre::Factory($data, $errors, 'GLAccountCentre');
                     if ($accountcentre) {
                         $accountcentre->save();
                         if (!$accountcentre) {
                             $errors[] = 'Failed to save reference to Account';
                             break;
                         }
                     } else {
                         $errors[] = 'Failed to save reference to Account';
                         break;
                     }
                 }
             }
         }
         if (count($errors) == 0) {
             $db->CompleteTrans();
             sendTo($this->name, 'index', $this->_modules);
         }
     } else {
         $errors[] = 'Failed to save GL Centre';
     }
     $db->FailTrans();
     $flash->addErrors($errors);
     $this->refresh();
 }
Ejemplo n.º 2
0
 public function save()
 {
     if (!$this->CheckParams($this->modeltype)) {
         sendBack();
     }
     $flash = Flash::Instance();
     $errors = array();
     $db = DB::Instance();
     $db->StartTrans();
     if (parent::save_model($this->modeltype, $this->_data[$this->modeltype], $errors)) {
         if (isset($this->_data[$this->modeltype]['primary_account']) && $this->_data[$this->modeltype]['primary_account'] == 'on') {
             $cbaccounts = new CBAccountCollection($this->_templateobject);
             $sh = new SearchHandler($cbaccounts, FALSE);
             $sh->addConstraint(new Constraint('id', '!=', $this->saved_model->id));
             if ($cbaccounts->update('primary_account', FALSE, $sh) === FALSE) {
                 $errors[] = 'Error updating bank accounts';
             }
         } else {
             // Check that there is only one primary account
             $cc = new ConstraintChain();
             $cc->add(new Constraint('primary_account', 'is', TRUE));
             $count = $this->_templateobject->getCount($cc);
             if ($count == 0) {
                 $flash->addWarning('No primary account defined');
             } elseif ($count > 1) {
                 $flash->addWarning('Multiple primary accounts defined');
             }
         }
     } else {
         $errors[] = 'Error saving Bank Account details : ' . $db->ErrorMsg();
     }
     if (count($errors) > 0) {
         $flash->addErrors($errors);
         $db->FailTrans();
     }
     if ($db->CompleteTrans()) {
         sendTo($this->name, 'index', $this->_modules);
     }
     $this->refresh();
 }
Ejemplo n.º 3
0
 public function save()
 {
     $errors = array();
     $flash = Flash::Instance();
     $db = DB::Instance();
     if (!$this->checkParams($this->modeltype)) {
         sendBack();
     }
     $company = DataObjectFactory::Factory('Company');
     $company->load($this->_data[$this->modeltype]['company_id']);
     if (!$company->isLoaded()) {
         $flash->addError('Invalid company');
         sendBack();
     }
     if ($this->_data[$this->modeltype]['email_invoice_id'] == 0) {
         $this->_data[$this->modeltype]['email_invoice_id'] = NULL;
     }
     if ($this->_data[$this->modeltype]['email_statement_id'] == 0) {
         $this->_data[$this->modeltype]['email_statement_id'] = NULL;
     }
     if (parent::save_model($this->modeltype, $this->_data[$this->modeltype], $errors)) {
         sendTo($this->name, 'view', $this->_modules, array('id' => $this->saved_model->id));
     } else {
         if (count($errors) > 0) {
             $flash->addErrors($errors);
         }
         $flash->addError('Error saving Customer ' . $db->ErrorMsg());
     }
     if (isset($this->_data[$this->modeltype]['id'])) {
         $this->_data['id'] = $this->_data[$this->modeltype]['id'];
     }
     $this->refresh();
 }
Ejemplo n.º 4
0
 public function save()
 {
     if (!$this->CheckParams($this->modeltype)) {
         sendBack();
     }
     $flash = Flash::Instance();
     $errors = array();
     $data = $this->_data[$this->modeltype];
     if (empty($data['id'])) {
         $data['next_due_date'] = $data['start_date'];
         $data['status'] = 'A';
     }
     if ($data['status'] == 'A' && !empty($data['glaccount_id']) && !empty($data['glcentre_id']) && $data['source'] != 'SR' && $data['source'] != 'PP') {
         $data['glaccount_centre_id'] = GLAccountCentre::getAccountCentreId($data['glaccount_id'], $data['glcentre_id'], $errors);
     } else {
         unset($data['glaccount_id']);
         unset($data['glcentre_id']);
         unset($data['glaccount_centre_id']);
     }
     if (empty($data['net_value']) && empty($data['tax_value']) && empty($data['gross_value'])) {
         $flash->addError('No value entered');
     } else {
         if (empty($data['net_value']) || $data['net_value'] == 0) {
             $data['net_value'] = $data['gross_value'];
         }
         if (empty($data['tax_value'])) {
             $data['tax_value'] = 0;
         }
         if (empty($data['gross_value']) || $data['gross_value'] == 0) {
             $data['gross_value'] = bcadd($data['net_value'], $data['tax_value']);
         }
     }
     if (count($errors) === 0 && parent::save_model($this->modeltype, $data)) {
         sendTo($this->name, 'index', $this->_modules);
     } else {
         $flash->addErrors($errors);
         $this->refresh();
     }
 }