function revalue($data, &$errors = array()) { if (empty($data['transaction_date'])) { $data['transaction_date'] = date(DATE_FORMAT); } $glperiod = GLPeriod::getPeriod(fix_date($data['transaction_date'])); if (!$glperiod || count($glperiod) == 0) { $errors[] = 'No period exists for this date'; return false; } $data['value'] = bcsub($this->glbalance(), $data['new_balance']); $data['glperiods_id'] = $glperiod['id']; $data['source'] = 'C'; $data['type'] = 'V'; $data['glaccount_id'] = $this->currency_detail->writeoff_glaccount_id; $data['glcentre_id'] = $this->currency_detail->glcentre_id; GLTransaction::setTwinCurrency($data); $gl_trans[] = GLTransaction::Factory($data, $errors); $data['value'] = bcmul($data['value'], -1); $data['glaccount_id'] = $this->glaccount_id; $data['glcentre_id'] = $this->glcentre_id; GLTransaction::setTwinCurrency($data); $gl_trans[] = GLTransaction::Factory($data, $errors); $db = DB::Instance(); $db->StartTrans(); if (count($errors) == 0 && GLTransaction::saveTransactions($gl_trans, $errors)) { return $db->CompleteTrans(); } $errors[] = 'Failed to save GL Transaction'; $db->FailTrans(); $db->CompleteTrans(); return false; }
public static function useDefault($search_data = null, &$errors = array(), $defaults = null) { $search = new VatSearch($defaults); $search->addSearchField('box', 'Box', 'hidden', '', 'hidden'); $default_year = date('Y'); $default_tax_period = 1; $glperiod = GLPeriod::getPeriod(date('Y-m-d')); if ($glperiod && count($glperiod) > 0) { $default_year = $glperiod['year']; $default_tax_period = $glperiod['tax_period']; } $search->addSearchField('year', 'Year', 'select', $default_year, 'basic'); $search->addSearchField('tax_period', 'Tax Period', 'select', $default_tax_period, 'basic'); $glperiods = new GLPeriodCollection(); $sh = new SearchHandler($glperiods, false); $sh->setOrderBy('year'); $glperiods->load($sh); $glperiods = $glperiods->getContents(); $options = array(); foreach ($glperiods as $glperiod) { if (!array_key_exists($glperiod->year, $options)) { $options[$glperiod->year] = $glperiod->year; } } $search->setOptions('year', $options); $tax_periods = GLPeriod::getTaxPeriods(); $options = array_combine($tax_periods, $tax_periods); $search->setOptions('tax_period', $options); $search->setSearchData($search_data, $errors); return $search; }
public function index() { $errors = array(); $s_data = array(); // Preserve any search criteria selection so that the context is maintained if (isset($this->_data['Search'])) { $s_data = $this->_data['Search']; } elseif (!isset($this->_data['orderby']) && !isset($this->_data['page'])) { // Either this is the first entry to the page or the search has been cleared // and orderby or paging is not selected // so set context from calling module $currentPeriod = GLPeriod::getPeriod(date('Y-m-d')); if ($currentPeriod && count($currentPeriod) > 0) { $s_data['glperiods_id'] = array($currentPeriod['id']); } if (isset($this->_data['glaccount_id'])) { $s_data['glaccount_id'] = $this->_data['glaccount_id']; } if (isset($this->_data['glcentre_id'])) { $s_data['glcentre_id'] = $this->_data['glcentre_id']; } } $this->setSearch('glbalancesSearch', 'useDefault', $s_data); $this->view->set('clickaction', 'edit'); parent::index(new GLBudgetCollection($this->_templateobject)); $sidebar = new SidebarController($this->view); $sidebarlist = array(); $sidebarlist['new'] = array('tag' => 'New Budget', 'link' => array_merge($this->_modules, array('controller' => $this->name, 'action' => 'new'))); $sidebarlist['viewaccounts'] = array('tag' => 'View All Accounts', 'link' => array_merge($this->_modules, array('controller' => 'glaccounts', 'action' => 'index'))); $sidebarlist['viewcentres'] = array('tag' => 'View All Centres', 'link' => array_merge($this->_modules, array('controller' => 'glcentres', 'action' => 'index'))); $sidebar->addList('Actions', $sidebarlist); $this->view->register('sidebar', $sidebar); $this->view->set('sidebar', $sidebar); }
static function Factory($data, &$errors, $do_name = __CLASS__) { $do = DataObjectFactory::Factory($do_name); $db = DB::Instance(); if (empty($data['docref'])) { $data['docref'] = $db->GenID('gl_transactions_docref_seq'); } $glperiod = GLPeriod::getPeriod(fix_date($data['transaction_date'])); if (!$glperiod || count($glperiod) == 0) { $errors[] = 'No period exists for this date'; } $data['glperiods_id'] = $glperiod['id']; $data['status'] = $do->newStatus(); return parent::Factory($data, $errors, $do); }
public static function trialBalance(&$search_data = null, &$errors = array(), $defaults = null) { $search = new glbalancesSearch($defaults); // Search by Period $currentPeriod = GLPeriod::getPeriod(date('Y-m-d')); if ($currentPeriod && count($currentPeriod) > 0) { $default_period = $currentPeriod['id']; } $search->addSearchField('glperiods_id', 'Period', 'select', $default_period, 'basic'); $glperiod = new GLPeriod(); $glperiods = $glperiod->getAll(); $search->setOptions('glperiods_id', $glperiods); $search->setSearchData($search_data, $errors, 'trialBalance'); return $search; }
protected function makeGLTransactions(&$errors = array()) { $newerrors = array(); //sort out the header details $gl_transactions = array(); $gl_data = array(); //the gl docref is the invoice number $gl_data['docref'] = $this->invoice_number; $gl_data['reference'] = $this->our_reference; //dates should be the same $gl_data['transaction_date'] = un_fix_date($this->invoice_date); //first character of class identifies source $gl_data['source'] = substr(strtoupper(get_class($this)), 0, 1); //type depends on Invoice or Credit Note $gl_data['type'] = $this->transaction_type; //the description is one from a number of bits of information //(description is compulsory for GL, but the options aren't for SLTransaction and SInvoice) $desc = $this->description; $ext_ref = $this->ext_reference; $sales_order_id = $this->sales_order_id; if (!empty($desc)) { $header_desc = $desc; } elseif (!empty($ext_ref)) { $header_desc = $ext_ref; } elseif (!empty($sales_order_id)) { $header_desc = $sales_order_id; } else { $header_desc = $this->invoice_number; } $gl_data['comment'] = $header_desc; //another docref $gl_data['docref2'] = $sales_order_id; // set the period based on invoice date $glperiod = GLPeriod::getPeriod($this->invoice_date); if (!$glperiod || count($glperiod) == 0) { $errors[] = 'No period exists for this date'; return false; } $gl_data['glperiods_id'] = $glperiod['id']; $gl_data['twin_currency_id'] = $this->twin_currency_id; $gl_data['twin_rate'] = $this->twin_rate; //there needs to be a tax element $gl_data['base_tax_value'] = $this->base_tax_value; $gl_data['twin_tax_value'] = $this->twin_tax_value; $vat_element = GLTransaction::makeCBTax($gl_data, $newerrors); if ($vat_element !== false) { $gl_transactions[] = $vat_element; } else { $errors += $newerrors; return false; } //this is the control element (used to balance the tax and lines) $gl_data['base_gross_value'] = $this->base_gross_value; $gl_data['twin_gross_value'] = $this->twin_gross_value; $gl_params = DataObjectFactory::Factory('GLParams'); $gl_data['glaccount_id'] = $this->get_ledger_control_account($gl_params, $errors); if ($gl_data['glaccount_id'] === FALSE) { return FALSE; } $gl_data['glcentre_id'] = $this->get_balance_sheet_cost_centre($gl_params, $errors); if ($gl_data['glcentre_id'] === FALSE) { return FALSE; } $control = GLTransaction::makeCBControl($gl_data, $newerrors); if ($control !== false) { $gl_transactions[] = $control; } else { $errors += $newerrors; return false; } // Now do the GL Transactions for each invoice line $this->makeGLTransactionLines($gl_data, $gl_transactions, $errors); return $gl_transactions; }
function loadJournals($data, &$errors = array(), &$warnings = array()) { $system = system::Instance(); $data_in = $this->convertData($data); if (is_array($data_in) && count($data_in) > 0) { $db = DB::Instance(); $docref = $db->GenID('gl_transactions_docref_seq'); $gl_transactions = array(); foreach ($data_in as $gl_data) { foreach ($gl_data as $model => $fields) { $glperiod = GLPeriod::getPeriod(fix_date($fields['transaction_date'])); if (!$glperiod || count($glperiod) == 0) { $errors[] = 'No period exists for this date'; break; } $fields['source'] = 'G'; $fields['type'] = 'J'; $fields['docref'] = $docref; $fields['glperiods_id'] = $glperiod['id']; GLTransaction::setTwinCurrency($fields); $gl_transaction = GLTransaction::Factory($fields, $errors); $gl_transactions[] = $gl_transaction; } } if (count($gl_transactions) > 0 && !GLTransaction::saveTransactions($gl_transactions, $errors)) { $errors[] = 'Failed to save journal import'; } } else { $errors[] = 'No data found or error converting data'; } if (count($errors) > 0) { return false; } return array('internal_id' => $gl_transaction->id, 'internal_identifier_field' => 'docref', 'internal_identifier_value' => $docref); }
public static function saveGLtransaction($cb_trans, $data, &$errors) { $desc = $cb_trans->description; $gl_data = $data; $gl_data['comment'] = !empty($desc) ? $desc : $cb_trans->reference; $gl_data['docref'] = $cb_trans->reference; $gl_data['reference'] = $cb_trans->ext_reference; $gl_data['control_docref'] = $cb_trans->control_reference; $glperiod = GLPeriod::getPeriod($cb_trans->transaction_date); if (!$glperiod || count($glperiod) == 0) { $errors[] = 'No period exists for this date'; return false; } $db = DB::Instance(); $db->StartTrans(); $gl_data['glperiods_id'] = $glperiod['id']; $gl_trans = GLTransaction::makeFromCashbookTransaction($gl_data, $errors); if ($gl_trans === false) { $errors[] = 'Invalid GL transaction'; } if (count($errors) == 0 && !GLTransaction::saveTransactions($gl_trans, $errors)) { $errors[] = 'Error saving GL transaction'; $db->FailTrans(); $gl_trans = FALSE; } $db->CompleteTrans(); return $gl_trans; }
public function getPeriod($_trandate = '') { if (isset($this->_data['ajax'])) { if (!empty($this->_data['id'])) { $_trandate = $this->_data['id']; } } $current = GLPeriod::getPeriod(fix_date($_trandate)); if (isset($this->_data['ajax'])) { $this->view->set('value', $current['year'] . ' - period ' . $current['period']); $this->setTemplateName('text_inner'); } else { return $current['year'] . ' - period ' . $current['period']; } }
function closePeriod($tax_period, $year, &$errors) { $db = DB::Instance(); $db->StartTrans(); foreach ($this->glperiod_ids as $glperiod_id) { $glperiod = DataObjectFactory::Factory('GLPeriod'); $glperiod->load($glperiod_id); if ($glperiod->isLoaded()) { $glperiod->tax_period_closed = true; if (!$glperiod->save()) { $errors[] = 'Error trying to close tax period'; break; } } else { $errors[] = 'Error trying to close tax period'; break; } } if (count($errors) == 0) { $this->tax_period_closed = true; $output_tax = $this->getVATSum(1); $input_tax = $this->getVATSum(4); $total_tax = bcsub($input_tax, $output_tax); $input_tax = bcmul($input_tax, -1); // $total_tax=$this->getVATSum(5); // if ($total_tax!=($output_tax-$input_tax)) { // $errors[]='Total VAT does not equal Output Tax minus Input Tax'; // } } if (count($errors) == 0) { $net_tax_element = array(); $glparams = DataObjectFactory::Factory('GLParams'); $net_tax_element['glcentre_id'] = $glparams->balance_sheet_cost_centre(); $glperiod = GLPeriod::getPeriod(date('Y-m-d')); if (!$glperiod || count($glperiod) == 0) { $errors[] = 'No period exists for this date'; } else { $net_tax_element['glperiods_id'] = $glperiod['id']; $net_tax_element['docref'] = $year . '-' . $tax_period; $net_tax_element['transaction_date'] = date(DATE_FORMAT); $net_tax_element['source'] = 'V'; // V = VAT Return $net_tax_element['type'] = 'N'; // N = Net Tax, P = Payment $net_tax_element['comment'] = 'VAT Return: ' . $year . ' - Tax Period ' . $tax_period; $net_tax_element['value'] = $input_tax; $net_tax_element['glaccount_id'] = $this->control_accounts['vat_input']; $this->setTwinCurrency($net_tax_element); $gltransactions[] = GLTransaction::Factory($net_tax_element, $errors, 'GLTransaction'); $net_tax_element['value'] = $output_tax; $net_tax_element['glaccount_id'] = $this->control_accounts['vat_output']; $this->setTwinCurrency($net_tax_element); $gltransactions[] = GLTransaction::Factory($net_tax_element, $errors, 'GLTransaction'); $net_tax_element['value'] = $total_tax; $net_tax_element['glaccount_id'] = $this->control_accounts['vat_control']; $this->setTwinCurrency($net_tax_element); $gltransactions[] = GLTransaction::Factory($net_tax_element, $errors, 'GLTransaction'); $this->saveTransactions($gltransactions, $errors); } } if (count($errors) > 0) { $db->FailTrans(); } return $db->CompleteTrans(); }
public static function makeFromLedgerTransaction(LedgerTransaction $transaction, Invoice $invoice, &$errors = array()) { $newerrors = array(); $db = DB::Instance(); $db->StartTrans(); // sort out the header details $gl_transactions = array(); $gl_data = array(); // the gl docref is the invoice number $gl_data['docref'] = $invoice->invoice_number; $gl_data['reference'] = $invoice->our_reference; // dates should be the same $gl_data['transaction_date'] = un_fix_date($invoice->invoice_date); // first character of class identifies source $gl_data['source'] = substr(strtoupper(get_class($transaction)), 0, 1); // type depends on Invoice or Credit Note $gl_data['type'] = $invoice->transaction_type; // the description is one from a number of bits of information // (description is compulsory for GL, but the options aren't for SLTransaction and SInvoice) $desc = $invoice->description; $ext_ref = $invoice->ext_reference; $sales_order_id = $invoice->sales_order_id; if (!empty($desc)) { $header_desc = $desc; } elseif (!empty($ext_ref)) { $header_desc = $ext_ref; } elseif (!empty($sales_order_id)) { $header_desc = $sales_order_id; } else { $header_desc = $invoice->invoice_number; } $gl_data['comment'] = $header_desc; // another docref $gl_data['docref2'] = $invoice->sales_order_id; // set the period based on invoice date $glperiod = GLPeriod::getPeriod($invoice->invoice_date); if (!$glperiod || count($glperiod) == 0) { $errors[] = 'No period exists for this date'; $db->FailTrans(); $db->CompleteTrans(); return false; } $gl_data['glperiods_id'] = $glperiod['id']; $gl_data['twin_currency_id'] = $invoice->twin_currency_id; $gl_data['twin_rate'] = $invoice->twin_rate; // there needs to be a tax element $gl_data['base_tax_value'] = $invoice->base_tax_value; $gl_data['twin_tax_value'] = $invoice->twin_tax_value; $vat_element = GLTransaction::makeCBTax($gl_data, $newerrors); if ($vat_element !== false) { $gl_transactions[] = $vat_element; } else { $db->FailTrans(); $db->CompleteTrans(); $errors += $newerrors; return false; } // EU acquisition? $eu_acquisition = false; if ($gl_data['source'] == 'P') { $tax_status = DataObjectFactory::Factory('TaxStatus'); if ($tax_status->load($invoice->tax_status_id)) { $eu_acquisition = $tax_status->eu_tax == 't'; } } $eu_gl_data = $gl_data; $eu_gl_data['value'] = 0; $eu_gl_data['twinvalue'] = 0; // this is the control element (used to balance the tax and lines) $gl_data['base_gross_value'] = $invoice->base_gross_value; $gl_data['twin_gross_value'] = $invoice->twin_gross_value; $control = self::getControlAccount($gl_data['source'], $errors); foreach ($control as $key => $value) { $gl_data[$key] = $value; } $control = GLTransaction::makeCBControl($gl_data, $newerrors); if ($control !== false) { $gl_transactions[] = $control; } else { $db->FailTrans(); $db->CompleteTrans(); $errors += $newerrors; return false; } // then do the invoice lines $lines = $invoice->lines; foreach ($lines as $line) { // provide some alternatives to get a comment $i_desc = $line->item_description; $desc = !empty($i_desc) ? $i_desc : $line->description; $desc = !empty($desc) ? $desc : ''; $gl_data['comment'] = $desc; $gl_data['glaccount_id'] = $line->glaccount_id; $gl_data['glcentre_id'] = $line->glcentre_id; $gl_data['base_net_value'] = $line->base_net_value; $gl_data['twin_net_value'] = $line->twin_net_value; // Calculate tax value if EU acquisition if ($eu_acquisition && $line->tax_rate_id) { $tax_rate = DataObjectFactory::Factory('TaxRate'); if ($tax_rate->load($line->tax_rate_id) && $tax_rate->percentage > 0) { $tax_rate_mult = 1 + $tax_rate->percentage / 100; $eu_gl_data['value'] += $line->base_net_value * $tax_rate_mult - $line->base_net_value; $eu_gl_data['twinvalue'] += $line->twin_net_value * $tax_rate_mult - $line->twin_net_value; } } $element = GLTransaction::makeCBLine($gl_data, $newerrors); if ($element !== false) { $gl_transactions[] = $element; } else { $db->FailTrans(); $db->CompleteTrans(); $errors += $newerrors; return false; } } if ($eu_acquisition) { $eu_tax_elements = GLTransaction::makeEuTax($eu_gl_data, $newerrors); foreach ($eu_tax_elements as $eu_tax_element) { if ($eu_tax_element === false) { $db->FailTrans(); $db->CompleteTrans(); $errors += $newerrors; return false; } $gl_transactions[] = $eu_tax_element; } } $db->CompleteTrans(); return $gl_transactions; }