Example #1
0
 public static function currencyAdjustment(&$data, &$errors)
 {
     $data['original_source'] = 'E';
     $data['reference'] = '';
     $data['comment'] = 'Expense Allocation Currency Adjustment';
     $db = DB::Instance();
     $db->startTrans();
     if (!GLTransaction::currencyAdjustment($data, $errors)) {
         $db->FailTrans();
     }
     return $db->CompleteTrans();
 }
Example #2
0
 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;
 }
Example #3
0
 protected function makeGLTransactionLines($gl_data, &$gl_transactions, &$errors = array())
 {
     $newerrors = array();
     foreach ($this->lines as $line) {
         // Set common gl data for the line
         $line->makeGLTransactions($gl_data);
         $element = GLTransaction::makeCBLine($gl_data, $newerrors);
         if ($element !== FALSE) {
             $gl_transactions[] = $element;
         } else {
             $errors += $newerrors;
             return FALSE;
         }
     }
     return TRUE;
 }
Example #4
0
 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;
 }
Example #5
0
 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();
 }
Example #6
0
 protected function makeGLTransactionLines($gl_data, &$gl_transactions, &$errors = array())
 {
     $newerrors = array();
     // EU Acquisitions only applies to Purchases
     $tax_status = DataObjectFactory::Factory('TaxStatus');
     if ($tax_status->load($this->tax_status_id)) {
         $eu_acquisition = $tax_status->eu_tax == 't';
     } else {
         $errors[] = 'Error getting Tax Status for the Invoice';
         return FALSE;
     }
     if ($eu_acquisition) {
         $eu_gl_data = $gl_data;
         $eu_gl_data['value'] = 0;
         $eu_gl_data['twinvalue'] = 0;
     }
     // Get tax rates
     $tax_rate = DataObjectFactory::Factory('TaxRate');
     $tax_rate->identifierField = 'percentage';
     $tax_rates = $tax_rate->getAll();
     $gl_params = DataObjectFactory::Factory('GLParams');
     $accruals_control_account = $gl_params->accruals_control_account();
     $balance_sheet_cost_centre = $gl_data['glcentre_id'];
     foreach ($this->lines as $line) {
         // Set common gl data for the line
         $line->makeGLTransactions($gl_data);
         if ($accruals_control_account) {
             // Need to check if there is a GRN for this line
             // and whether it is accrued and accrual accounting is enabled
             $poreceivedline = DataObjectFactory::Factory('POReceivedLine');
             $poreceivedline->load($line->grn_id);
             if ($poreceivedline->isLoaded()) {
                 if ($poreceivedline->net_value != $line->net_value) {
                     // Invoice line value differs from received (quantity*price) value
                     // Convert received line net value to base
                     $base_net_value = $poreceivedline->net_value;
                     if ($line->rate != 1) {
                         // Convert to base value
                         $base_net_value = round(bcmul($line->rate, $base_net_value, 4), 2);
                     }
                     $gl_data['base_net_value'] = bcsub($line->base_net_value, $base_net_value);
                     // Need to get the twin value for this value
                     $gl_data['twin_net_value'] = round(bcmul($line->twin_rate, $gl_data['base_net_value'], 4), 2);
                     $original_comment = $gl_data['comment'];
                     $gl_data['comment'] = $gl_data['comment'] . ' - Invoice Variance';
                     // Make the GL Transaction for the variance of the invoice line against GRN
                     // The variance is posted to the invoice line GL Account/Centre
                     $element = GLTransaction::makeCBLine($gl_data, $newerrors);
                     if ($element !== FALSE) {
                         $gl_transactions[] = $element;
                     } else {
                         $errors += $newerrors;
                         return FALSE;
                     }
                     $gl_data['comment'] = $original_comment;
                     $gl_data['base_net_value'] = $base_net_value;
                     $gl_data['twin_net_value'] = $line->twin_net_value - $gl_data['twin_net_value'];
                 }
                 if ($poreceivedline->isAccrued()) {
                     // The GRN has been accrued so need to post to the accruals control
                     $gl_data['glaccount_id'] = $accruals_control_account;
                     $gl_data['glcentre_id'] = $balance_sheet_cost_centre;
                 }
             }
         }
         // Calculate tax value if EU acquisition
         if ($eu_acquisition && $line->tax_rate_id) {
             if (isset($tax_rates[$line->tax_rate_id]) && $tax_rates[$line->tax_rate_id] > 0) {
                 $tax_rate_mult = 1 + $tax_rates[$line->tax_rate_id] / 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;
             }
         }
         // Make the GL Transaction for the invoice line
         $element = GLTransaction::makeCBLine($gl_data, $newerrors);
         if ($element !== FALSE) {
             $gl_transactions[] = $element;
         } else {
             $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) {
                 $errors += $newerrors;
                 return false;
             }
             $gl_transactions[] = $eu_tax_element;
         }
     }
     if (count($errors) > 0) {
         return FALSE;
     } else {
         return TRUE;
     }
 }
Example #7
0
 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);
 }
Example #8
0
 public function save($pay_data, &$errors)
 {
     $db = DB::Instance();
     $db->StartTrans();
     //		Save the Payment Header
     if (!parent::save()) {
         $errors[] = 'Failed to save Payment Header';
         $db->FailTrans();
         return false;
     }
     $flash = Flash::Instance();
     //		Validate and write purchase ledger, cashbook and general ledger transactions
     $progressbar = new progressBar('creating_pl_transactions');
     $payment_id = $this->id;
     $callback = function ($data, $key) use(&$pay_data, &$errors, $payment_id) {
         $data['cb_account_id'] = $pay_data['cb_account_id'];
         $data['reference'] = $pay_data['reference'];
         $data['cross_ref'] = $data['ext_reference'] = $payment_id;
         $data['description'] = $pay_data['description'];
         $data['transaction_type'] = 'P';
         $data['transaction_date'] = $pay_data['transaction_date'];
         $data['source'] = $pay_data['source'];
         $supplier = DataObjectFactory::Factory('PLSupplier');
         $supplier->load($data['plmaster_id']);
         $data['payment_term_id'] = $supplier->payment_term_id;
         if (PLTransaction::saveTransaction($data, $errors) === false) {
             $errors[] = 'Failed to save payments';
             return false;
         }
         $pay_data['PLTransaction'][$key]['ledger_transaction_id'] = $data['ledger_transaction_id'];
     };
     if ($progressbar->process($pay_data['PLTransaction'], $callback) === FALSE) {
         $db->FailTrans();
         $db->CompleteTrans();
         return FALSE;
     }
     //		Match and update purchase ledger payments
     $payment_total = 0;
     $progressbar = new progressBar('allocate_payments');
     $callback = function ($data, $key) use(&$pay_data, &$errors, $payment_id, &$payment_total) {
         $db = DB::Instance();
         // Get the payment transaction and update it to paid
         $pltransaction = DataObjectFactory::Factory('PLTransaction');
         $pltransaction->load($data['ledger_transaction_id']);
         if (!$pltransaction->update($pltransaction->id, array('status', 'os_value', 'twin_os_value', 'base_os_value'), array('P', '0.00', '0.00', '0.00'))) {
             $errors[] = 'Error updating payment status : ' . $db->ErrorMsg();
             return false;
         }
         // get all the transactions linked for payment to the payment transaction
         $pltransactions = new PLTransactionCollection(DataObjectFactory::Factory('PLTransaction'), 'pl_allocation_overview');
         $pltransactions->getPaid($data);
         // the allocation amount is the gross payment value
         $allocations = array($pltransaction->id => $pltransaction->gross_value);
         $trans_total = 0;
         $trans_base_total = $pltransaction->base_gross_value;
         foreach ($pltransactions as $trans) {
             // now mark all the linked transactions as paid
             // update the invoices linked to thes transactions as paid
             $trans_total = bcadd($trans->gross_value, $trans_total);
             $trans_base_total = bcadd($trans->base_gross_value, $trans_base_total);
             if (!$trans->update($trans->id, array('status', 'for_payment', 'os_value', 'twin_os_value', 'base_os_value'), array($trans->Paid(), false, '0.00', '0.00', '0.00'))) {
                 $errors[] = 'Error updating transaction status : ' . $db->ErrorMsg();
                 return false;
             }
             if ($trans->transaction_type == 'C' || $trans->transaction_type == 'I') {
                 $invoice = DataObjectFactory::Factory('PInvoice');
                 if (!$invoice->updateStatus($trans->our_reference, 'P')) {
                     $errors[] = 'Error updating Invoice : ' . $db->ErrorMsg();
                     return false;
                 }
             }
             $allocations[$trans->id] = $trans->gross_value;
             // Save settlement discount if present?
             if ($trans->settlement_discount > 0 && $trans->include_discount == 't') {
                 // Create GL Journal for settlement discount
                 $discount = array();
                 $discount['gross_value'] = $discount['net_value'] = $trans->settlement_discount;
                 $discount['glaccount_id'] = $trans->pl_discount_glaccount_id;
                 $discount['glcentre_id'] = $trans->pl_discount_glcentre_id;
                 $discount['transaction_date'] = date(DATE_FORMAT);
                 $discount['tax_value'] = '0.00';
                 $discount['source'] = 'P';
                 $discount['transaction_type'] = 'SD';
                 $discount['our_reference'] = $trans->our_reference;
                 $discount['ext_reference'] = $trans->ext_reference;
                 $discount['currency_id'] = $trans->currency_id;
                 $discount['rate'] = $trans->rate;
                 $discount['description'] = !is_null($trans->pl_discount_description) ? $trans->pl_discount_description . ' ' : '';
                 $discount['description'] .= !is_null($trans->description) ? $trans->description : $trans->ext_reference;
                 $discount['payment_term_id'] = $trans->payment_term_id;
                 $discount['plmaster_id'] = $trans->plmaster_id;
                 $discount['status'] = 'P';
                 $pldiscount = PLTransaction::Factory($discount, $errors, 'PLTransaction');
                 if ($pldiscount && $pldiscount->save('', $errors) && $pldiscount->saveGLTransaction($discount, $errors)) {
                     $allocations[$pldiscount->{$pldiscount->idField}] = bcadd($discount['net_value'], 0);
                     $trans_total = bcadd($trans_total, $pldiscount->gross_value);
                     $trans_base_total = bcadd($trans_base_total, $pldiscount->base_gross_value);
                 } else {
                     $errors[] = 'Errror saving PL Transaction Discount : ' . $db->ErrorMsg();
                 }
             }
         }
         if ($data['net_value'] != $pltransaction->gross_value * -1 || $data['net_value'] != $trans_total) {
             $errors[] = 'Transaction Payment mismatch ' . $data['net_value'] . ' ' . $pltransaction->gross_value * -1 . ' ' . $trans_total . ' for ' . $trans->supplier;
             return false;
         }
         // save the allocations
         if (!PLAllocation::saveAllocation($allocations, $payment_id, $errors)) {
             return false;
         }
         if ($trans_base_total != 0) {
             $adj_data = array();
             $errors = array();
             $adj_data['docref'] = $pltransaction->plmaster_id;
             $adj_data['original_source'] = 'P';
             $adj_data['reference'] = '';
             $adj_data['value'] = $trans_base_total * -1;
             $adj_data['comment'] = 'Purchase Allocation Currency Adjustment';
             if (!GLTransaction::currencyAdjustment($adj_data, $errors)) {
                 return false;
             }
         }
         $payment_total = bcadd($payment_total, $trans_total);
     };
     if ($progressbar->process($pay_data['PLTransaction'], $callback) === FALSE) {
         $db->FailTrans();
         $db->CompleteTrans();
         return FALSE;
     }
     if ($payment_total != $pay_data['payment_total']) {
         $errors[] = 'Payment Mismatch - Total ' . $pay_data['payment_total'] . ' not equal sum Transaction Payments ' . $payment_total;
         $db->FailTrans();
         $db->CompleteTrans();
         return false;
     }
     return $db->CompleteTrans();
 }
Example #9
0
 public function post($errors = array())
 {
     $db = db::Instance();
     // Check it hasn't been posted!
     if (!$this->isUnposted()) {
         $errors[] = 'Journal has already been posted';
         return FALSE;
     }
     // Check it is a standard journal
     if (!$this->isStandardJournal()) {
         $errors[] = 'Template Journal cannot be posted';
         return FALSE;
     }
     // Get all unposted transcations
     $this->NewTransactions();
     $sh = new SearchHandler($this->transactions, false);
     $sh->addConstraint($this->childConstraint());
     $unposted = $this->transactions->load($sh, null, RETURN_ROWS);
     // Save transactions to GL Transactions
     $db->startTrans();
     foreach ($unposted as $transaction) {
         unset($transaction['id']);
         $transaction['transaction_date'] = un_fix_date($this->transaction_date);
         $transaction['glperiods_id'] = $this->glperiods_id;
         GLTransaction::setTwinCurrency($transaction);
         $gltransaction = GLTransaction::Factory($transaction);
         if (!$gltransaction->save()) {
             $errors[] = 'Error saving journal transaction : ' . $db->ErrorMsg();
         } elseif (!$gltransaction->updateBalance($errors)) {
             $errors[] = 'Error updating GL balance : ' . $db->ErrorMsg();
         }
         if ($this->accrual == 't') {
             $transaction['glperiods_id'] = $this->accrual_period_id;
             $transaction['comment'] = 'Reverse ' . $transaction['comment'];
             $transaction['value'] = bcmul($transaction['value'], -1);
             $gltransaction = GLTransaction::Factory($transaction);
             if (!$gltransaction->save()) {
                 $errors[] = 'Error saving journal transaction : ' . $db->ErrorMsg();
             } elseif (!$gltransaction->updateBalance($errors)) {
                 $errors[] = 'Error updating GL balance : ' . $db->ErrorMsg();
             }
         }
     }
     // Update the header status if no errors so far
     if (empty($errors) && !$this->update($this->{$this->idField}, 'status', $this->postedStatus())) {
         $errors[] = 'Error updating journal header status : ' . $db->ErrorMsg();
     }
     // Delete the unposted transactions if posted OK
     if (count($errors) > 0 || !$this->transactions->delete($sh)) {
         $errors[] = 'Error updating posted journals : ' . $db->ErrorMsg();
         $db->FailTrans();
     }
     return $db->CompleteTrans();
 }
Example #10
0
 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;
 }
Example #11
0
 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();
 }
Example #12
0
 static function allocatePayment($transactions, $customer_id, &$errors)
 {
     $db = DB::Instance();
     $db->StartTrans();
     $total = 0;
     $base_total = 0;
     foreach ($transactions as $id => $value) {
         $trans = DataObjectFactory::Factory('SLTransaction');
         $trans->load($id);
         $total = bcadd($total, $value);
         $base_total = bcadd($base_total, $trans->base_os_value);
         $trans->os_value = bcsub($trans->os_value, $value);
         $trans_store[] = $trans;
     }
     if ($total == 0) {
         foreach ($trans_store as $transaction) {
             if ($transaction->os_value == 0) {
                 $transaction->status = $transaction->paid();
                 $transaction->twin_os_value = 0.0;
                 $transaction->base_os_value = 0.0;
             } else {
                 $transaction->status = $transaction->partPaid();
                 $transaction->setTwinBaseValue('os_value');
             }
             $base_total = bcsub($base_total, $transaction->base_os_value);
             $result = $transaction->saveForPayment($errors);
             if ($result === false) {
                 $errors[] = 'Error saving transaction';
                 $db->FailTrans();
                 $db->CompleteTrans();
                 return false;
             }
             if ($transaction->os_value == 0 && ($transaction->transaction_type == 'C' || $transaction->transaction_type == 'I')) {
                 $invoice = DataObjectFactory::Factory('SInvoice');
                 if (!$invoice->updateStatus($transaction->our_reference, 'P')) {
                     $errors[] = 'Error updating Invoice';
                     $db->FailTrans();
                     $db->CompleteTrans();
                     return false;
                 }
             }
         }
         if ($base_total != 0) {
             $data = array();
             $data['docref'] = $customer_id;
             $data['original_source'] = 'S';
             $data['reference'] = '';
             $data['value'] = $base_total;
             $data['comment'] = 'Sales Allocation Currency Adjustment';
             if (!GLTransaction::currencyAdjustment($data, $errors)) {
                 $db->FailTrans();
                 $db->CompleteTrans();
                 return false;
             }
         }
     } else {
         $errors[] = 'Transactions must sum to zero';
         $db->FailTrans();
         $db->CompleteTrans();
         return false;
     }
     return $db->CompleteTrans();
 }
Example #13
0
 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;
 }
Example #14
0
 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();
 }
Example #15
0
 public static function makeCBControl($transaction, &$errors = array())
 {
     $mult = self::$multipliers[$transaction['source']][$transaction['type']];
     $transaction['value'] = $mult * -1 * $transaction['base_gross_value'];
     $transaction['twinvalue'] = $mult * -1 * $transaction['twin_gross_value'];
     return GLTransaction::Factory($transaction, $errors);
 }
Example #16
0
 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';
         }
     }
 }