public function accrueLine($_ids = '', &$errors = array(), $_reverse_accrual = FALSE) { if (empty($_ids) || !is_array($_ids)) { return FALSE; } $db =& DB::Instance(); $db->StartTrans(); $poreceivedline = DataObjectFactory::Factory('POReceivedLine'); $poreceivedlines = new POReceivedLineCollection($poreceivedline); $sh = new SearchHandler($poreceivedlines, FALSE); $sh->addConstraint(new Constraint('id', 'in', '(' . implode(',', array_keys($_ids)) . ')')); $glparams = DataObjectFactory::Factory('GLParams'); $accrual_control = $glparams->accruals_control_account(); $result = TRUE; if (!empty($accrual_control)) { $cost_centre = $glparams->balance_sheet_cost_centre(); $sh->setFields(array('id', 'glaccount_id', 'glcentre_id', 'item_description', 'net_value', 'order_number', 'rate', 'received_date')); $rows = $poreceivedlines->load($sh, null, RETURN_ROWS); $gltransactions = GLTransaction::makeFromGRN(array('control_glaccount_id' => $accrual_control, 'control_glcentre_id' => $cost_centre, 'reverse_accrual' => $_reverse_accrual), $rows, $errors); if (count($errors) > 0) { $result = FALSE; $errors[] = 'Error saving GL Transaction : ' . $db->ErrorMsg(); } else { // Save the GL Transactions and update the balances if (!GLTransaction::saveTransactions($gltransactions, $errors)) { $result = FALSE; $errors[] = 'Error saving GL Transaction : ' . $db->ErrorMsg(); } } } // Now update the received lines status to accrued if ($result !== FALSE) { if ($_reverse_accrual) { $result = $poreceivedlines->update('status', $poreceivedline->writeOffStatus(), $sh); } else { $result = $poreceivedlines->update('status', $poreceivedline->accrualStatus(), $sh); } if ($result !== FALSE && $result != count($_ids)) { $errors[] = 'Updated ' . $result . ' expected ' . count($_ids); $result = FALSE; } } if ($result === FALSE) { $db->FailTrans(); } $db->CompleteTrans(); return $result; }
/** * Post an invoice to the Sales ledger * - saving to SLTransaction automatically saves to General Ledger */ public function post(&$errors = array()) { if ($this->hasBeenPosted()) { $errors[] = 'Invoice has already been posted'; return false; } $db = DB::Instance(); $db->StartTrans(); // reload the invoice to refresh the dependencies $this->load($this->id); // Validate Header and Line Values for following nine value fields // (1) Net + Tax = Gross // (2) Sum of line values = header value $fields = array('gross_value', 'net_value', 'tax_value', 'base_gross_value', 'base_net_value', 'base_tax_value', 'twin_gross_value', 'twin_net_value', 'twin_tax_value'); $linesum = array(); foreach ($fields as $field) { $linesum[$field] = 0; } // Get sum of values for the lines by field foreach ($this->lines as $line) { if (bcadd($line->net_value, $line->tax_value) != $line->gross_value || bcadd($line->base_net_value, $line->base_tax_value) != $line->base_gross_value || bcadd($line->twin_net_value, $line->twin_tax_value) != $line->twin_gross_value) { $errors[] = 'Line ' . $line->line_number . ' values mismatch (net+tax=>gross)'; } elseif (!$line->update($line->id, 'glaccount_centre_id', 'null')) { $errors[] = 'Error updating invoice line ' . $line->line_number; } foreach ($fields as $field) { $linesum[$field] = bcadd($linesum[$field], $line->{$field}); } } // Check line sum against header foreach ($linesum as $field => $value) { if ($this->{$field} != $value) { $errors[] = $field . ' line sum (' . $value . ') does not match header value ' . $this->{$field}; } } if (bcadd($this->net_value, $this->tax_value) != $this->gross_value || bcadd($this->base_net_value, $this->base_tax_value) != $this->base_gross_value || bcadd($this->twin_net_value, $this->twin_tax_value) != $this->twin_gross_value) { $errors[] = 'Header values mismatch (net+tax=>gross)'; } // Create and save the Ledger Transaction if (count($errors) == 0) { // $transaction = LedgerTransaction::makeFromInvoice($this); $transaction = $this->makeLedgerTransaction(); if ($transaction !== false) { $result = $transaction->save($this, $errors); } else { $result = false; } } // Create and save the GL Transactions if ($result) { // $gl_transactions = GLTransaction::makeFromLedgerTransaction($transaction, $this, $errors); $gl_transactions = $this->makeGLTransactions($errors); if (!is_array($gl_transactions) || count($errors) > 0) { $result = false; } else { $result = GLTransaction::saveTransactions($gl_transactions, $errors); } } // Update the Invoice header status if ($result) { $this->status = 'O'; $result = $this->save(); } if (!$result) { $message = $db->ErrorMsg(); if (!empty($message)) { $errors[] = $message; } $db->FailTrans(); } $db->CompleteTrans(); return $result; }
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 function savejournal() { $flash = Flash::Instance(); $errors = array(); if (!$this->checkParams('Vat')) { sendBack(); } $data = $this->_data['Vat']; if ($data['value']['net'] <= 0 || $data['value']['vat'] <= 0) { $errors[] = 'Net and Vat values must be greater than zero'; } else { $glparams = DataObjectFactory::Factory('GLParams'); $vat_type = 'vat_' . $data['vat_type']; $data['vat_account'] = call_user_func(array($glparams, $vat_type)); if ($data['vat_type'] == 'input') { $data['value']['net'] = bcmul($data['value']['net'], -1); $data['value']['vat'] = bcmul($data['value']['vat'], -1); } $data['transaction_date'] = date(DATE_FORMAT); $gltransactions = GLTransaction::makeFromVATJournalEntry($data, $errors); if (count($errors) == 0 && GLTransaction::saveTransactions($gltransactions, $errors)) { $flash->addMessage('VAT Journal created OK'); sendTo($this->name, '', $this->_modules); } } $flash->addErrors($errors); $this->_data['vat_type'] = $data['vat_type']; $this->_data['glaccount_id'] = $data['glaccount_id']; $this->refresh(); }
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 post(&$errors = array()) { if ($this->hasBeenPosted()) { $errors[] = 'Expense has already been posted'; return false; } $db = DB::Instance(); $db->StartTrans(); $transaction = DataObjectFactory::Factory('ELTransaction'); $trans_id = $db->GenID('ELTransactions_id_seq'); //copy across the fields that are needed foreach ($this->getFields() as $fieldname => $field) { if ($transaction->isField($fieldname)) { $transaction->{$fieldname} = $this->{$fieldname}; } } $transaction->id = $trans_id; $transaction->transaction_type = 'E'; $transaction->status = 'O'; $transaction->our_reference = $this->expense_number; $prefixes = array('', 'base_', 'twin_'); //the outstanding (os) values are the gross values to begin with foreach ($prefixes as $prefix) { $transaction->{$prefix . 'os_value'} = $transaction->{$prefix . 'gross_value'}; } $transaction->transaction_date = $this->expense_date; $result = $transaction->save($this, $errors); // Create and save the GL Transactions if ($result) { $gl_transactions = GLTransaction::makeFromExpenseTransaction($transaction, $this, $errors); if (!is_array($gl_transactions) || count($errors) > 0) { $result = false; } else { $result = GLTransaction::saveTransactions($gl_transactions, $errors); } } // Update the Expense Claim header status if ($result) { $this->status = $this->statusAwaitingPayment(); $result = $this->save(); } if (!$result) { $message = $db->ErrorMsg(); if (!empty($message)) { $errors[] = $message; } $db->FailTrans(); } $db->CompleteTrans(); return $result; }
public function saveGLTransaction(&$data, &$errors) { $db = DB::Instance(); $db->StartTrans(); // Write to General Ledger $gl_trans = GLTransaction::makeFromLedgerJournal($this, $data, $errors); if ($gl_trans === false || !GLTransaction::saveTransactions($gl_trans, $errors)) { $db->FailTrans(); } else { // Return the Ledger Transaction Id $data['ledger_transaction_id'] = $this->id; $data['payment_value'] = $this->gross_value; } return $db->CompleteTrans(); }
public static function currencyAdjustment($data, &$errors = array()) { $db = DB::Instance(); $db->StartTrans(); $glparams = DataObjectFactory::Factory('GLParams'); $currency = DataObjectFactory::Factory('Currency'); $currency->load($glparams->base_currency()); if (!$currency) { $errors[] = 'No currency found'; $db->FailTrans(); $db->CompleteTrans(); return false; } if (empty($data['transaction_date'])) { $data['transaction_date'] = date(DATE_FORMAT); } $glperiod = GLPeriod::getPeriod(fix_date(date(DATE_FORMAT))); if (!$glperiod || count($glperiod) == 0) { $errors[] = 'No period exists for this date'; $db->FailTrans(); $db->CompleteTrans(); return false; } $control = self::getControlAccount($data['original_source'], $errors); $data['glperiods_id'] = $glperiod['id']; $data['source'] = 'G'; $data['type'] = 'CA'; $data['glaccount_id'] = $currency->writeoff_glaccount_id; $data['glcentre_id'] = $currency->glcentre_id; self::setTwinCurrency($data); $gl_trans[] = GLTransaction::Factory($data, $errors); $data['value'] = $data['value'] * -1; $data['glaccount_id'] = $control['glaccount_id']; $data['glcentre_id'] = $control['glcentre_id']; self::setTwinCurrency($data); $gl_trans[] = GLTransaction::Factory($data, $errors); if (count($errors) == 0 && GLTransaction::saveTransactions($gl_trans, $errors) && $db->CompleteTrans()) { return true; } $errors[] = 'Failed to save GL Transaction'; $db->FailTrans(); $db->CompleteTrans(); return false; }
static function save($data, &$errors) { $period = DataObjectFactory::Factory('GLPeriod'); if ($period->loadPeriod($data['purchase_date'])) { $data['purchase_period_id'] = $period->id; } else { $errors[] = 'No period defined for this purchase date'; return; } $data['wd_value'] = $data['purchase_price']; $asset = DataObject::Factory($data, $errors, 'Asset'); if (count($errors) === 0 && $asset->save()) { $data['armaster_id'] = $asset->id; $data['transaction_date'] = $data['purchase_date']; $data['value'] = $data['purchase_price']; $data['to_group_id'] = $data['argroup_id']; $data['to_location_id'] = $data['arlocation_id']; $artransaction = DataObjectFactory::Factory('ARTransaction'); $artransaction = $artransaction->add($data, $artransaction->addition(), $errors); $result = false; if ($artransaction && count($errors) === 0) { $result = $artransaction->save(); } if ($result) { $gltrans = GLTransaction::makeFromAssetTransaction($artransaction, $asset, $errors); } if ($result === FALSE || count($errors) > 0 || GLTransaction::saveTransactions($gltrans, $errors) === FALSE) { $errors[] = 'Failed to create new Asset'; } } }