diff --git a/site/controllers/transactions_controller.php b/site/controllers/transactions_controller.php index a73ea42..484a93f 100644 --- a/site/controllers/transactions_controller.php +++ b/site/controllers/transactions_controller.php @@ -109,18 +109,180 @@ class TransactionsController extends AppController { echo('

THIS IS NOT A POST FOR SOME REASON

'); return; } - //pr($this->data); + //pr(array('thisdata' => $this->data)); - $amount = 0; - foreach ($this->data['LedgerEntry'] AS $entry) { - $amount += isset($entry['amount']) ? $entry['amount'] : 0; + if (isset($this->data['customer_id'])) { + $C = new Customer(); + $C->recursive = -1; + $customer = $C->find + ('first', + array('contain' => array('Account.CurrentLedger.id'), + 'fields' => false, + 'conditions' => array('Customer.id', $this->data['customer_id']), + )); + $ledger_id = $customer['Account']['CurrentLedger']['id']; + } + else { + // Payment by Unit, Lease, etc + $ledger_id = 0; } - $cust_id = $this->data['Customer']['id']; - $cust = new Customer(); - $unreconciled = $cust->findUnreconciledLedgerEntries($cust_id); - $reconciled = $cust->reconcileNewLedgerEntry($cust_id, 'credit', $amount); - pr(compact('amount', 'unreconciled', 'reconciled')); + $amount = 0; + foreach ($this->data['LedgerEntry'] AS &$entry) { + $reconciled = $C->reconcileNewLedgerEntry($this->data['customer_id'], + 'credit', + $entry['amount']); + pr(compact('entry', 'reconciled')); + + foreach ($reconciled['debit']['entry'] AS $rec) { + $entry['DebitReconciliationLedgerEntry'] = + array('amount' => $rec['applied'], + //'debit_ledger_entry_id' + 'credit_ledger_entry_id' => $rec['id'] + ); + } + + $amount += isset($entry['amount']) ? $entry['amount'] : 0; + $entry['debit_ledger_id'] = 6; // Cash/Payments + $entry['credit_ledger_id'] = $ledger_id; + } + + + pr($this->data); + $T = new Transaction(); + $T->create(); + if ($T->saveAll($this->data, + array( + 'validate' => false, + //'fieldList' => array(), + //'callbacks' => true, + ))) { + $tid = $T->id; + $this->Session->setFlash(__("New Transaction Created ($tid)!", true)); + //$this->redirect(array('action'=>'view', $mid)); + } + else { + $this->autoRender = false; + pr(array('checkpoint' => "saveAll failed")); + } + pr($T->data); + + $C = new Customer(); + $LE = new LedgerEntry(); +/* $reconciled = $C->reconcileNewLedgerEntry($this->data['customer_id'], */ +/* 'credit', */ +/* $amount); */ +/* pr(compact('amount', 'unreconciled', 'reconciled')); */ +/* return; */ + + foreach ($this->data['LedgerEntry'] AS &$entry) { + $reconciled = $C->reconcileNewLedgerEntry($this->data['customer_id'], + 'credit', + $entry['amount']); + pr(compact('entry', 'reconciled')); + continue; + + foreach ($reconciled['debit']['entry'] AS $rec) { + $data = array('LedgerEntry' => + array('DebitReconciliationLedgerEntry' => + array('amount' => $rec['applied'], + //'debit_ledger_entry_id' + 'credit_ledger_entry_id' => $rec['id'] + ), + ), + ); + //'DebitReconciliationLedgerEntry' => array( + //pr(compact('amount', 'unreconciled', 'reconciled')); + } + } + + } + + function saveTest() { + $data = + array( +/* 'Customer' => array */ +/* ('id' => 7, */ +/* ), */ + + 'LedgerEntry' => array + ( + '0' => array( + 'amount' => 100, + 'debit_ledger_id' => 1, + 'credit_ledger_id' => 2, + 'MonetarySource' => array('name' => 'testmoney', 'monetary_type_id' => 2), + ), + '1' => array( + 'amount' => 101, + 'debit_ledger_id' => 1, + 'credit_ledger_id' => 2, + 'MonetarySource' => array('name' => 'testmoney2', 'monetary_type_id' => 2), + ), + ), + + 'Transaction' => array + ( + 'stamp' => '06/18/2009', + 'comment' => 'no comment', + ), + ); + + $data = +/* array( */ +/* 'LedgerEntry' => array */ +/* ( */ +/* '0' => */ + array( + 'amount' => 100, + 'debit_ledger_id' => 1, + 'credit_ledger_id' => 2, + 'transaction_id' => 1, + 'DebitReconciliationLedgerEntry' => + array('amount' => 44, + //'debit_ledger_entry_id' + 'credit_ledger_entry_id' => 17, + ), +/* ), */ +/* ), */ + + ); + + //$M = new Transaction(); + $M = new LedgerEntry(); + $M->create(); + $retval = $M->saveAll($data, + array( + 'validate' => false, + 'fieldList' => array(), + 'callbacks' => true, + )); + + $mid = $M->id; + pr(compact('retval', 'mid')); + if ($mid) { + $this->Session->setFlash(__("New Transaction Created ($mid)!", true)); + //$this->redirect(array('action'=>'view', $mid)); + } + else { + $this->autoRender = false; + pr(array('checkpoint' => "saveAll failed")); + } + + +/* $LE = new LedgerEntry(); */ +/* $LE->create(); */ +/* $ret = $LE->save($data, */ +/* array( */ +/* 'validate' => false, */ +/* 'fieldList' => array(), */ +/* 'callbacks' => true, */ +/* )); */ +/* $leid = $LE->id; */ +/* pr(array('checkpoint' => "New Ledger Entry", */ +/* compact('leid', 'ret'))); */ + //pr($LE); + } }