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; }
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; }