git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629/site@308 97e9348a-65ac-dc4b-aefc-98561f571b83
108 lines
3.3 KiB
PHP
108 lines
3.3 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'];
|
|
|
|
// 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->accountReceivableAccountID()),
|
|
'credit_ledger_id' => $A->currentLedgerID($entry['account_id']),
|
|
'customer_id' => $customer_id,
|
|
'lease_id' => $lease_id)
|
|
+ $entry
|
|
);
|
|
|
|
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,
|
|
'split_transaction_id'=>1));
|
|
}
|
|
|
|
return $ret;
|
|
}
|
|
|
|
}
|
|
?>
|