示例#1
0
 public function save_contras()
 {
     if (!$this->loadData()) {
         $this->dataError();
         sendBack();
     }
     $db = DB::Instance();
     $db->StartTrans();
     $flash = Flash::Instance();
     $errors = array();
     $transactions = array();
     $contras_sessionobject = new SessionData('sl_contras');
     foreach ($this->_data['SLTransaction'] as $id => $data) {
         $data['contra'] = isset($data['contra']) && $data['contra'] == 'on';
         $contras_sessionobject->updatePageData($id, $data, $errors);
     }
     $contra_total = isset($this->_data['contra_total']) ? $this->_data['contra_total'] : '0.00';
     $contra_sum = 0;
     foreach ($contras_sessionobject->getPageData($errors) as $id => $data) {
         if (isset($data['contra']) && $data['contra'] == 'on') {
             // using bcadd to format value
             $transactions[$id] = bcadd($data['os_value'], 0);
             $contra_sum = bcadd($contra_sum, $data['os_value']);
         }
     }
     if (count($transactions) == 0) {
         $errors[] = 'You must select at least one transaction';
     } elseif ($contra_total == $contra_sum) {
         $pl_journal_seq = $db->GenID('pl_journals_id_seq');
         $sl_journal_seq = $db->GenID('sl_journals_id_seq');
         // Create the PL and SL contra journals
         $sltransaction = DataObjectFactory::Factory('SLTransaction');
         $sltransaction->load($id);
         $slcontra = array();
         $slcontra['gross_value'] = $slcontra['net_value'] = bcmul($contra_sum, -1);
         $glparams = DataObjectFactory::Factory('GLParams');
         $slcontra['glaccount_id'] = $glparams->contras_control_account();
         $slcontra['glcentre_id'] = $glparams->balance_sheet_cost_centre();
         $slcontra['transaction_date'] = date(DATE_FORMAT);
         $slcontra['tax_value'] = '0.00';
         $slcontra['source'] = 'S';
         $slcontra['transaction_type'] = 'J';
         $slcontra['our_reference'] = $sl_journal_seq;
         $slcontra['currency_id'] = $this->_data['SLCustomer']['currency_id'];
         $slcontra['rate'] = $this->_data['SLCustomer']['rate'];
         $slcontra['payment_term_id'] = $this->_data['SLCustomer']['payment_term_id'];
         $plcontra = $slcontra;
         $slcontra['slmaster_id'] = $this->_data['SLCustomer']['id'];
         $slcontra['description'] = 'Contra Sales Ledger - PL Ref:' . $pl_journal_seq;
         $sltrans = SLTransaction::Factory($slcontra, $errors, 'SLTransaction');
         if ($sltrans && $sltrans->save('', $errors) && $sltrans->saveGLTransaction($slcontra, $errors)) {
             $transactions[$sltrans->{$sltrans->idField}] = bcadd($slcontra['net_value'], 0);
         } else {
             $errors[] = 'Errror saving SL Transaction Contra : ' . $db->ErrorMsg();
             $flash->addErrors($errors);
         }
         $plcontra['source'] = 'P';
         $plcontra['our_reference'] = $pl_journal_seq;
         $plcontra['description'] = 'Contra Purchase Ledger - SL Ref:' . $sl_journal_seq;
         $plcontra['gross_value'] = $plcontra['net_value'] = bcmul($contra_sum, -1);
         $supplier = DataObjectFactory::Factory('PLSupplier');
         $supplier->loadBy('company_id', $this->_data['SLCustomer']['company_id']);
         if ($supplier->isLoaded()) {
             $plcontra['plmaster_id'] = $supplier->{$supplier->idField};
             $pltrans = PLTransaction::Factory($plcontra, $errors, 'PLTransaction');
         } else {
             $pltrans = FALSE;
         }
         if (!$pltrans || !$pltrans->save('', $errors) || !$pltrans->saveGLTransaction($plcontra, $errors)) {
             $errors[] = 'Errror saving PL Transaction Contra : ' . $db->ErrorMsg();
             $flash->addErrors($errors);
         }
     } else {
         $errors[] = 'Transactions sum mismatch Sum: ' . $contra_sum . ' Control Total: ' . $contra_total;
     }
     if (count($errors) > 0 || !SLTransaction::allocatePayment($transactions, $this->_data['id'], $errors) || !SLAllocation::saveAllocation($transactions, $errors)) {
         $db->FailTrans();
     }
     if ($db->CompleteTrans()) {
         $contras_sessionobject->clear();
         $flash->addMessage('Contra Transactions matched');
         sendTo($this->name, 'view', $this->_modules, array('id' => $this->_data['id']));
     }
     $flash->addErrors($errors);
     $this->outstanding_transactions();
 }
 public function delete_selected()
 {
     if (!$this->checkParams('session_data_key')) {
         $this->dataError();
         sendBack();
     }
     $flash = Flash::Instance();
     $db = DB::Instance();
     $db->StartTrans();
     $errors = array();
     $session_data_key = $this->_data['session_data_key'];
     $page_data = new SessionData($session_data_key);
     foreach ($this->_data[$this->modeltype] as $id => $fields) {
         if (!isset($fields['select']) && isset($fields['_checkbox_exists_select'])) {
             $page_data->deletePageData($id);
         } else {
             $page_data->updatePageData($id, $fields, $errors);
         }
     }
     $data = $page_data->getPageData();
     // Could do with a progress bar here as the number of records could be large
     $delete_count = 0;
     if (count($data) > 0) {
         $progressBar = new Progressbar('soproductline_delete_unused');
         $callback = function ($fields, $id) use(&$delete_count) {
             if ($fields['select'] == 'on') {
                 $productline = DataObjectFactory::Factory('SOProductLine');
                 $productline->load($id);
                 if (!$productline->isLoaded() || !$productline->delete($id, $errors)) {
                     return FALSE;
                 }
                 $delete_count++;
             }
         };
         if ($progressBar->process($data, $callback) === FALSE) {
             $errors[] = 'Failed to delete product line';
         }
     } else {
         $flash->addWarning('Nothing selected to delete');
     }
     // reset timeout to 30 seconds to allow time to redisplay the page
     // hopefully, it will be quicker than this!
     set_time_limit(30);
     if (count($errors) > 0) {
         $flash->addErrors($errors);
         $flash->addError($db->ErrorMsg());
         $db->FailTrans();
         $db->CompleteTrans();
         $this->refresh();
     } else {
         $page_data->clear();
         $db->CompleteTrans();
         $flash->addMessage($delete_count . ' record' . get_plural_string($delete_count) . ' archived successfully');
         sendTo($this->name, 'unused', $this->_modules);
     }
 }