diff --git a/site/models/transaction.php b/site/models/transaction.php index 7960cf8..d23ae0a 100644 --- a/site/models/transaction.php +++ b/site/models/transaction.php @@ -42,7 +42,11 @@ class Transaction extends AppModel { // Create some models for convenience $A = new Account(); - $C = new Customer(); + + // Determine the total charges on the invoice + $grand_total = 0; + foreach ($data['LedgerEntry'] AS $entry) + $grand_total += $entry['amount']; // Create a transaction for the invoice $invoice_transaction = new Transaction(); @@ -56,8 +60,33 @@ class Transaction extends AppModel { if (!$ar_transaction->save($data['Transaction'], false)) return false; + // Create an account receivable entry + // I would prefer to do this last, as it feels more + // logical to me that the entries would flow from the + // charge to the A/R, but in reality it makes no + // difference to the end result. Entering the A/R + // first allows us to reconcile each charge on the + // invoice as we enter them. Otherwise, we would + // need to loop again at the end to reconcile, and + // would need to save each charge entry id in the + // interim. This keeps the logic simple. + // debit: A/R credit: Invoice + $ar_entry_data = array + ('debit_ledger_id' => $A->currentLedgerID($A->accountReceivableAccountID()), + 'credit_ledger_id' => $A->currentLedgerID($A->invoiceAccountID()), + 'transaction_id' => $ar_transaction->id, + 'amount' => $grand_total, + 'lease_id' => $lease_id, + 'customer_id' => $customer_id, + ); + + // Create a new A/R entry from the data + $ar_entry = new LedgerEntry(); + $ar_entry->create(); + if (!$ar_entry->save($ar_entry_data, false)) + return false; + // Go through the entered charges - $grand_total = 0; foreach ($data['LedgerEntry'] AS $entry) { // Invoice Transaction // debit: Invoice credit: Charge @@ -94,39 +123,20 @@ class Transaction extends AppModel { if (!$invoice_entry->save($entry, false)) return false; - $grand_total += $entry['amount']; + // Reconcile the Invoice account. Our two entries are: + // debit: Invoice credit: Charge + // debit: A/R credit: Invoice + // Since this is from the perspective of the Invoice account, + // the credit entry is the Invoice<->A/R, and the debit + // entry is the actual invoice ledger entry. + $R = new Reconciliation(); + $R->create(); + if (!$R->save(array('debit_ledger_entry_id' => $invoice_entry->id, + 'credit_ledger_entry_id' => $ar_entry->id, + 'amount' => $entry['amount']), false)) + return false; } - // Create an account receivable entry - // debit: A/R credit: Invoice - $ar_entry_data = array - ('debit_ledger_id' => $A->currentLedgerID($A->accountReceivableAccountID()), - 'credit_ledger_id' => $A->currentLedgerID($A->invoiceAccountID()), - 'transaction_id' => $ar_transaction->id, - 'amount' => $grand_total, - 'lease_id' => $lease_id, - 'customer_id' => $customer_id, - ); - - // Create a new A/R entry from the data - $ar_entry = new LedgerEntry(); - $ar_entry->create(); - if (!$ar_entry->save($ar_entry_data, false)) - return false; - - // Reconcile the Invoice account. Our two entries are: - // debit: Invoice credit: Charge - // debit: A/R credit: Invoice - // Since this is from the perspective of the Invoice account, - // the credit entry is the Invoice<->A/R, and the debit - // entry is the actual invoice ledger entry. - $R = new Reconciliation(); - $R->create(); - if (!$R->save(array('debit_ledger_entry_id' => $invoice_entry->id, - 'credit_ledger_entry_id' => $ar_entry->id, - 'amount' => $grand_total), false)) - return false; - // Return indication of success return true; }