Files
pmgr/models/transaction.php
abijah aa36213698 Moved invoice/receipt code into Account. It appears to be broken in that the receipt ledger_entries are all getting a uniqe transaction. I'll have to look at this tomorrow.
git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629/site@306 97e9348a-65ac-dc4b-aefc-98561f571b83
2009-07-11 07:26:17 +00:00

118 lines
3.9 KiB
PHP

<?php
class Transaction extends AppModel {
var $name = 'Transaction';
var $validate = array(
'stamp' => array('date')
);
var $belongsTo = array(
);
var $hasMany = array(
'LedgerEntry',
);
/**************************************************************************
**************************************************************************
**************************************************************************
* function: addInvoice
* - Adds a new invoice transaction
*/
function addInvoice($data, $customer_id, $lease_id = null) {
// Create some models for convenience
$A = new Account();
//pr(compact('data', 'customer_id', 'lease_id'));
// Assume this will succeed
$ret = true;
// Determine the total charges on the invoice
$grand_total = 0;
foreach ($data['LedgerEntry'] AS $entry)
$grand_total += $entry['amount'];
$ids = $this->LedgerEntry->Ledger->Account->postLedgerEntry
(array_intersect_key($data, array('Transaction'=>1, 'transaction_id'=>1)),
null,
array('debit_ledger_id' => $A->currentLedgerID($A->accountReceivableAccountID()),
'credit_ledger_id' => $A->currentLedgerID($A->invoiceAccountID()),
'customer_id' => $customer_id,
'lease_id' => $lease_id,
'amount' => $grand_total));
$ar_entry_id = $ids['id'];
// Go through the entered charges
$invoice_transaction = array_intersect_key($data, array('Transaction'=>1, 'transaction_id'=>1));
foreach ($data['LedgerEntry'] AS $entry) {
//pr(compact('entry'));
// Create the receipt entry, and reconcile the credit side
// of the double-entry (which should be A/R) as a payment.
$ids = $this->LedgerEntry->Ledger->Account->postLedgerEntry
($invoice_transaction,
array_intersect_key($entry, array('MonetarySource'=>1))
+ array_intersect_key($entry, array('account_id'=>1)),
array('debit_ledger_id' => $A->currentLedgerID($A->invoiceAccountID()),
'credit_ledger_id' => $A->currentLedgerID($entry['account_id']),
'customer_id' => $customer_id,
'lease_id' => $lease_id)
+ $entry,
array('debit' => array(array('LedgerEntry' => array('id' => $ar_entry_id,
'amount' => $entry['amount']))))
);
if ($ids['error'])
$ret = false;
$invoice_transaction = array_intersect_key($ids, array('transaction_id'=>1));
}
return $ret;
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: addReceipt
* - Adds a new receipt transaction
*/
function addReceipt($data, $customer_id, $lease_id = null) {
// Create some models for convenience
$A = new Account();
// Assume this will succeed
$ret = true;
// Go through the entered payments
$receipt_transaction = array_intersect_key($data, array('Transaction'=>1, 'transaction_id'=>1));
foreach ($data['LedgerEntry'] AS $entry) {
// Create the receipt entry, and reconcile the credit side
// of the double-entry (which should be A/R) as a payment.
$ids = $this->LedgerEntry->Ledger->Account->postLedgerEntry
($receipt_transaction,
array_intersect_key($entry, array('MonetarySource'=>1))
+ array_intersect_key($entry, array('account_id'=>1)),
array('debit_ledger_id' => $A->currentLedgerID($entry['account_id']),
'credit_ledger_id' => $A->currentLedgerID($A->receiptAccountID()),
'customer_id' => $customer_id,
'lease_id' => $lease_id)
+ $entry,
'receipt');
if ($ids['error'])
$ret = false;
$receipt_transaction = array_intersect_key($ids, array('transaction_id'=>1));
}
return $ret;
}
}
?>