diff --git a/site/controllers/units_controller.php b/site/controllers/units_controller.php index 8ee4fc6..453097a 100644 --- a/site/controllers/units_controller.php +++ b/site/controllers/units_controller.php @@ -169,12 +169,15 @@ class UnitsController extends AppController { array('order' => array('charge_date'), // Models 'ChargeType', - 'Receipt' + 'Receipt' => array(// Models + 'Payment' + ), ) ) ) ); $unit = $this->Unit->read(null, $id); + //pr($unit); $outstanding_deposit = 0; $outstanding_balance = 0; @@ -183,6 +186,10 @@ class UnitsController extends AppController { $outstanding_balance += $charge['total']; foreach ($charge['Receipt'] AS $receipt) { $outstanding_balance -= $receipt['ChargesReceipt']['amount']; + +/* foreach($receipt['Payment'] AS $payment) */ +/* $outstanding_balance -= $payment['amount']; */ + // REVISIT 20090530: // Using hardcoded value for security deposit... // That can't be good! diff --git a/site/views/contacts/view.ctp b/site/views/contacts/view.ctp index 28dcc0a..7d7f48c 100644 --- a/site/views/contacts/view.ctp +++ b/site/views/contacts/view.ctp @@ -157,7 +157,7 @@ echo $this->element('table', * Ledger History */ foreach($contact['Lease'] AS $lease) { - $headers = array(/*'Charge/Receipt'*/'ID', 'Date', /*'Through',*/ 'Type', 'Comment', 'Amount', 'Total'); + $headers = array(/*'Charge/Receipt'*/'Charge', 'Date', 'Through', 'Type', 'Comment', 'Amount', 'Balance', 'Total'); $rows = array(); $row_class = array(); @@ -165,32 +165,28 @@ foreach($contact['Lease'] AS $lease) { $odd = 0; foreach($lease['Charge'] AS $charge) { $amount = $charge['total']; - $running_total += $amount; + + $balance = $amount; + foreach ($charge['Receipt'] AS $receipt) + $balance -= $receipt['ChargesReceipt']['amount']; + + $running_total += $balance; + $rows[] = array($html->link('#'.$charge['id'], array('controller' => 'charges', 'action' => 'view', $charge['id'])), - datefmt($charge['charge_date']) .' - '. datefmt($charge['charge_to_date']), + datefmt($charge['charge_date']), + datefmt($charge['charge_to_date']), $charge['ChargeType']['name'], $charge['comment'], currency($amount), + currency($balance), + //currency($amount - $paid)); currency($running_total)); + $row_class[] = array('charge', (++$odd % 2) ? 'oddrow' : 'evnrow'); - foreach ($charge['Receipt'] AS $receipt) { - $amount = -1 * $receipt['ChargesReceipt']['amount']; - $running_total += $amount; - $rows[] = array($html->link('#'.$receipt['id'], - array('controller' => 'receipts', - 'action' => 'view', - $receipt['id'])), - ' -- ' . datefmt($receipt['stamp']), - 'Payment Applied', - $receipt['comment'], - currency($amount), - currency($running_total)); - $row_class[] = array('receipt', ($odd % 2) ? 'oddrow' : 'evnrow'); - } } echo $this->element('table', diff --git a/site/views/elements/ledger.ctp b/site/views/elements/ledger.ctp new file mode 100644 index 0000000..3e16b9a --- /dev/null +++ b/site/views/elements/ledger.ctp @@ -0,0 +1,321 @@ +'.__('Payments',true).''; */ + +?> + +
+ + +link('#'.$charge['id'], + array('controller' => 'charges', + 'action' => 'view', + $charge['id'])), + datefmt($charge['charge_date']), + datefmt($charge['charge_to_date']), + $charge['ChargeType']['name'], + $charge['comment'], + currency($amount), + currency($balance), + //currency($amount - $paid)); + currency($running_total)); + + $row_class[] = 'charge'; + } + + echo ('
' . "\n"); + echo $this->element('table', + array('class' => 'item ledger list', + 'caption' => $caption, + 'headers' => $headers, + 'rows' => $rows, + 'row_class' => $row_class, + 'column_class' => $headers)); + echo ('
' . "\n"); +} + + +/********************************************************************** + * Receipts Only + */ + +if (isset($ledger['receipts'])) { + $headers = array('Receipt', 'Date', 'Comment', 'Amount', 'Balance', 'Total'); + + $receipts = array(); + foreach ($lease['Charge'] AS $charge) { + foreach($charge['Receipt'] AS $receipt) { + if (!isset($receipts[$receipt['id']])) { + $receipts[$receipt['id']] = $receipt; + $receipts[$receipt['id']]['amount'] = 0; + $receipts[$receipt['id']]['charge'] = 0; + foreach($receipt['Payment'] AS $payment) + $receipts[$receipt['id']]['amount'] += $payment['amount']; + unset($receipts[$receipt['id']]['ChargesReceipt']); + unset($receipts[$receipt['id']]['Payment']); + } + $receipts[$receipt['id']]['charge'] += $receipt['ChargesReceipt']['amount']; + } + } + + usort($receipts, + create_function('$a, $b', + '$adate = TimeHelper::toUnix(isset($a["charge_date"]) ? $a["charge_date"] : $a["stamp"]); ' . + '$bdate = TimeHelper::toUnix(isset($b["charge_date"]) ? $b["charge_date"] : $b["stamp"]); ' . + 'return strcmp($adate, $bdate);') + ); + + $rows = array(); + $row_class = array(); + $running_total = 0; + + foreach ($receipts AS $receipt) { + $amount = $receipt['amount']; + $balance = $amount - $receipt['charge']; + $running_total += $balance; + + $rows[] = array($html->link('#'.$receipt['id'], + array('controller' => 'receipts', + 'action' => 'view', + $receipt['id'])), + datefmt($receipt['stamp']), + $receipt['comment'], + currency($amount), + currency($balance), + currency($running_total)); + + $row_class[] = 'receipt'; + } + + echo ('
' . "\n"); + echo $this->element('table', + array('class' => 'item ledger list', + 'caption' => $caption, + 'headers' => $headers, + 'rows' => $rows, + 'row_class' => $row_class, + 'column_class' => $headers)); + echo ('
' . "\n"); +} + +/* $headers = array('Receipt', 'Date', 'Comment', 'Amount', 'Balance', 'Total'); */ + +/* $rows = array(); */ +/* $row_class = array(); */ +/* $running_total = 0; */ +/* $odd = 0; */ +/* foreach($lease['Receipt'] AS $charge) { */ +/* $amount = $charge['total']; */ + +/* $balance = $amount; */ +/* foreach ($charge['Receipt'] AS $receipt) */ +/* $balance -= $receipt['ChargesReceipt']['amount']; */ + +/* $running_total += $balance; */ + + +/* $charge_amount = 0; */ +/* $payment_amount = 0; */ +/* foreach($receipt['Charge'] AS $charge) */ +/* $charge_amount += $charge['ChargesReceipt']['amount']; */ +/* foreach($receipt['Payment'] AS $payment) */ +/* $payment_amount += $payment['amount']; */ + +/* foreach ($charge['Receipt'] AS $receipt) { */ +/* $amount = -1 * $receipt['ChargesReceipt']['amount']; */ +/* $running_total += $amount; */ +/* $rows[] = array($html->link('#'.$receipt['id'], */ +/* array('controller' => 'receipts', */ +/* 'action' => 'view', */ +/* $receipt['id'])), */ +/* datefmt($receipt['stamp']), */ +/* $receipt['comment'], */ +/* currency($amount), */ +/* currency($running_total)); */ +/* $row_class[] = array('receipt', ($odd % 2) ? 'oddrow' : 'evnrow'); */ +/* } */ + +/* $rows[] = array($html->link('#'.$charge['id'], */ +/* array('controller' => 'charges', */ +/* 'action' => 'view', */ +/* $charge['id'])), */ +/* datefmt($charge['charge_date']), */ +/* datefmt($charge['charge_to_date']), */ +/* $charge['ChargeType']['name'], */ +/* $charge['comment'], */ +/* currency($amount), */ +/* currency($balance), */ +/* //currency($amount - $paid)); */ +/* currency($running_total)); */ + +/* $row_class[] = array('charge', (++$odd % 2) ? 'oddrow' : 'evnrow'); */ +/* } */ + +/* echo ('
' . "\n"); */ +/* echo $this->element('table', */ +/* array('class' => 'item ledger list', */ +/* 'caption' => $caption */ +/* 'suppress_alternate_rows' => true, */ +/* 'headers' => $headers, */ +/* 'rows' => $rows, */ +/* 'row_class' => $row_class, */ +/* 'column_class' => $headers)); */ +/* echo ('
' . "\n"); */ + + +/********************************************************************** + * Mixture + */ + +if (isset($ledger['mix'])) { + $headers = array(/*'Charge/Receipt'*/'ID', 'Date', /*'Through',*/ 'Type', 'Comment', 'Amount', 'Total'); + + $receipts = array(); + foreach ($lease['Charge'] AS $charge) { + foreach($charge['Receipt'] AS $receipt) { + if (!isset($receipts[$receipt['id']])) { + $receipts[$receipt['id']] = $receipt; + $receipts[$receipt['id']]['amount'] = 0; + unset($receipts[$receipt['id']]['ChargesReceipt']); + unset($receipts[$receipt['id']]['Payment']); + } + $receipts[$receipt['id']]['amount'] += $receipt['ChargesReceipt']['amount']; + } + } + + $mix = array_merge($lease['Charge'], $receipts); + usort($mix, + create_function('$a, $b', + '$adate = TimeHelper::toUnix(isset($a["charge_date"]) ? $a["charge_date"] : $a["stamp"]); ' . + '$bdate = TimeHelper::toUnix(isset($b["charge_date"]) ? $b["charge_date"] : $b["stamp"]); ' . + 'return strcmp($adate, $bdate);') + ); + + $rows = array(); + $row_class = array(); + $running_total = 0; + + foreach ($mix AS $row) { + if (isset($row['charge_date'])) { + $controller = 'charges'; + $class = 'charge'; + $rowdate = datefmt($row['charge_date']) .' - '. datefmt($row['charge_to_date']); + $name = $row['ChargeType']['name']; + $amount = $row['total']; + } + else { + $controller = 'receipts'; + $class = 'receipt'; + $rowdate = ' -- ' . datefmt($row['stamp']); + $name = 'Payment Applied'; + $amount = -1 * $row['amount']; + } + + $running_total += $amount; + $rows[] = array($html->link('#'.$row['id'], + array('controller' => $controller, + 'action' => 'view', + $row['id'])), + $rowdate, + $name, + $row['comment'], + currency($amount), + currency($running_total)); + $row_class[] = $class; + } + + echo ('
' . "\n"); + echo $this->element('table', + array('class' => 'item ledger list', + 'caption' => $caption, + //'suppress_alternate_rows' => true, + 'headers' => $headers, + 'rows' => $rows, + 'row_class' => $row_class, + 'column_class' => $headers)); + echo ('
' . "\n"); +} + +/********************************************************************** + * Match + */ + +if (isset($ledger['match'])) { + $headers = array(/*'Charge/Receipt'*/'ID', 'Date', /*'Through',*/ 'Type', 'Comment', 'Amount', 'Total'); + + $rows = array(); + $row_class = array(); + $running_total = 0; + $odd = 0; + foreach($lease['Charge'] AS $charge) { + $amount = $charge['total']; + $running_total += $amount; + $rows[] = array($html->link('#'.$charge['id'], + array('controller' => 'charges', + 'action' => 'view', + $charge['id'])), + datefmt($charge['charge_date']) .' - '. datefmt($charge['charge_to_date']), + $charge['ChargeType']['name'], + $charge['comment'], + currency($amount), + currency($running_total)); + $row_class[] = array('charge', (++$odd % 2) ? 'oddrow' : 'evnrow'); + + foreach ($charge['Receipt'] AS $receipt) { + $amount = -1 * $receipt['ChargesReceipt']['amount']; + $running_total += $amount; + $rows[] = array($html->link('#'.$receipt['id'], + array('controller' => 'receipts', + 'action' => 'view', + $receipt['id'])), + ' -- ' . datefmt($receipt['stamp']), + 'Payment Applied', + $receipt['comment'], + currency($amount), + currency($running_total)); + $row_class[] = array('receipt', ($odd % 2) ? 'oddrow' : 'evnrow'); + } + } + + echo ('
' . "\n"); + echo $this->element('table', + array('class' => 'item ledger list', + 'caption' => $caption, + 'suppress_alternate_rows' => true, + 'headers' => $headers, + 'rows' => $rows, + 'row_class' => $row_class, + 'column_class' => $headers)); + echo ('
' . "\n"); +} \ No newline at end of file diff --git a/site/views/units/view.ctp b/site/views/units/view.ctp index 70555de..04a84a8 100644 --- a/site/views/units/view.ctp +++ b/site/views/units/view.ctp @@ -4,20 +4,20 @@ element('table', * Ledger History */ foreach($unit['Lease'] AS $lease) { - $headers = array(/*'Charge/Receipt'*/'ID', 'Date', /*'Through',*/ 'Type', 'Comment', 'Amount', 'Total'); - - $rows = array(); - $running_total = 0; - $odd = 0; - foreach($lease['Charge'] AS $charge) { - $amount = $charge['total']; - $running_total += $amount; - $rows[] = array($html->link('#'.$charge['id'], - array('controller' => 'charges', - 'action' => 'view', - $charge['id'])), - datefmt($charge['charge_date']) .' - '. datefmt($charge['charge_to_date']), - $charge['ChargeType']['name'], - $charge['comment'], - currency($amount), - currency($running_total)); - $row_class[] = array('charge', (++$odd % 2) ? 'oddrow' : 'evnrow'); - - foreach ($charge['Receipt'] AS $receipt) { - $amount = -1 * $receipt['ChargesReceipt']['amount']; - $running_total += $amount; - $rows[] = array($html->link('#'.$receipt['id'], - array('controller' => 'receipts', - 'action' => 'view', - $receipt['id'])), - ' -- ' . datefmt($receipt['stamp']), - 'Payment Applied', - $receipt['comment'], - currency($amount), - currency($running_total)); - $row_class[] = array('receipt', ($odd % 2) ? 'oddrow' : 'evnrow'); - } - } - - echo $this->element('table', - array('class' => 'item ledger list', - 'caption' => 'Lease #'.$lease['number'].' (Tenant: '.$lease['Contact'][0]['display_name'].')', - 'suppress_alternate_rows' => true, - 'headers' => $headers, - 'rows' => $rows, - 'row_class' => $row_class, - 'column_class' => $headers)); + $caption = 'Lease #'.$lease['number'].' (Tenant: '.$lease['Contact'][0]['display_name'].')'; + echo $this->element('ledger', + array('lease' => $lease, + 'caption' => $caption, + 'ledger' => array('charges'=>1, 'receipts'=>1, 'match'=>1, 'zmix'=>1))); } ?>