Files
pmgr/models/tender.php

208 lines
6.9 KiB
PHP

<?php
class Tender extends AppModel {
var $belongsTo = array(
'TenderType',
'Customer',
'LedgerEntry',
'DepositTransaction' => array(
'className' => 'Transaction',
),
'NsfTransaction' => array(
'className' => 'Transaction',
),
);
/**************************************************************************
**************************************************************************
**************************************************************************
* function: verifyTender
* - Verifies consistenty of new tender data
* (not in a pre-existing tender)
*/
function verifyTender($tender) {
/* pr(array("Tender::verifyTender()" */
/* => compact('tender'))); */
if (empty($tender['tender_type_id'])) {
/* pr(array("Tender::verifyTender()" */
/* => "Tender verification failed")); */
return false;
}
return true;
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: addTender
* - Inserts new Tender into the database
*/
function addTender($tender) {
/* pr(array('Tender::addTender' => */
/* compact('tender'))); */
$ret = array();
if (!$this->verifyTender($tender))
return array('error' => true) + $ret;
// Come up with a (not necessarily unique) name for the tender.
// For checks & money orders, this will be based on the check
// number. For other types of tender, we'll just use the
// generic name of the monetary account.
// REVISIT <AP>: 20090723
// I would like to have cash named "Cash #1234", where
// the number would correspond to either the Tender ID
// or the LedgerEntry ID.
if (empty($tender['name']) && !empty($tender['account_id'])) {
$tender['name'] = $this->LedgerEntry->Account->name($tender['account_id']);
if ($tender['account_id'] == $this->LedgerEntry->Account->checkAccountID() ||
$tender['account_id'] == $this->LedgerEntry->Account->moneyOrderAccountID()) {
$tender['name'] .= ' #' . $tender['data1'];
}
}
/* pr(array('Tender::addTender' => */
/* array('checkpoint' => 'Pre-Save') */
/* + array('Tender' => $tender))); */
$this->create();
if (!$this->save($tender))
return array('error' => true) + $ret;
$ret['tender_id'] = $this->id;
return $ret + array('error' => false);
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: nsf
* - Flags the ledger entry as having insufficient funds
*
* Steps:
* - Get information from Check (C1); for amount $A
* - Find Bank Deposit matching to Tender
* - New Transaction (T1)
* - New Bank Deposit (D1)
* - New Tender (N1); NSF; D1,
* - Add new LedgerEntry (L1a); T1; debit:bank; -$A
* - Add new LedgerEntry (L1b); T1; credit:NSF; -$A
* - Add new LedgerEntry (L2a); T1; debit:NSF; -$A; N1
* - Add new LedgerEntry (L2b); T1; credit:A/R; -$A
* - For Tx associated with LE associated with C1:
* - For each Payment SE of Tx:
* - Add new StatementEntry (S1n); T1; PAYMENT; -1*S1n.amount
* - New Transaction (T2) (?????)
* - Add new StatementEntry (S2); T2; CHARGE; NSF; $35
* - Add new LedgerEntry (L3a); T2; credit:NSF-Fee; $35
* - Add new LedgerEntry (L3b); T2; debit:A/R; $35
* - Set C1.nsf_tx = T1
* - Re-Reconcile (customer may have running credit)
*/
function nsf($id, $stamp = null) {
pr(array('Tender::nsf',
compact('id')));
// Get information about this NSF item.
$this->id = $id;
$tender = $this->find
('first', array
('contain' =>
array('LedgerEntry',
'DepositTransaction',
'NsfTransaction'),
));
//'conditions' => array(array('Tender.id' => $id)),
pr($tender);
if (!empty($tender['NsfTransaction']['id']))
die("Item has already been set as NSF");
if (empty($tender['DepositTransaction']['id']))
die("Item has not been deposited yet");
// Enter the NSF
$result = $this->DepositTransaction->addDeposit
(array('Transaction' => array(),
'Entry' => array(array('tender_id' => null,
'account_id' => $this->LedgerEntry->Account->nsfAccountID(),
'amount' => -1 * $tender['LedgerEntry']['amount'],
))),
$tender['DepositTransaction']['account_id']);
pr(compact('result'));
if ($result['error'])
die("Unable to save NSF transaction");
$this->id = $id;
$this->saveField('nsf_transaction_id', $result['transaction_id']);
$nsf_deposit = $this->DepositTransaction->find
('first', array('contain' => false, 'id' => $result['transaction_id']));
$nsf_deposit = $nsf_deposit['DepositTransaction'];
$nsf_ledger_entry = $this->LedgerEntry->find
('first', array
('contain' => array('Transaction' =>
array(//'fields' => array(),
'StatementEntry' =>
array(//'fields' => array(),
),
),
),
'conditions' => array('LedgerEntry.id' => $tender['LedgerEntry']['id']),
));
pr(compact('nsf_ledger_entry'));
$bounce = array('Transaction' => array(), 'Entry' => array());
$bounce['Transaction']['stamp'] = $nsf_deposit['stamp'];
$bounce['Transaction']['account_id'] = $this->LedgerEntry->Account->nsfAccountID();
$bounce['Transaction']['customer_id'] = $tender['Tender']['customer_id'];
$bounce['Transaction']['amount'] = -1 * $tender['LedgerEntry']['amount'];
foreach ($nsf_ledger_entry['Transaction']['StatementEntry'] AS $payment) {
if ($payment['type'] === 'SURPLUS') {
$payment['type'] = 'VOID';
$this->NsfTransaction->StatementEntry->id = $payment['id'];
$this->NsfTransaction->StatementEntry->saveField('type', $payment['type']);
}
else {
$bounce['Entry'][] =
array('type' => $payment['type'],
'amount' => -1 * $payment['amount'],
'account_id' => $this->LedgerEntry->Account->nsfAccountID(),
'customer_id' => $payment['customer_id'],
'lease_id' => $payment['lease_id'],
'charge_entry_id' => $payment['charge_entry_id'],
'effective_date' => $nsf_deposit['stamp'],
);
}
}
pr(compact('bounce'));
$result = $this->NsfTransaction->addNsf($bounce);
if ($result['error'])
die("Unable to save Bounce transaction");
// REVISIT <AP>: 20090730
// Add NSF Charge
/* $nsf_fee_account_id = $A->nsfChargeAccountID(); */
return true;
}
}
?>