This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/payment/payment.inc.php
2011-05-03 09:49:04 +10:00

242 lines
9.4 KiB
PHP

<?php
/**
* osBilling - Open Billing Software
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Originally authored by Deon George
*
* @author Deon George <deonATleenooksDOTnet>
* @copyright 2009 Deon George
* @link http://osb.leenooks.net
* @license http://www.gnu.org/licenses/
* @package AgileBill
* @subpackage Modules:Payment
*/
/**
* The main AgileBill Payment Class
*
* @package AgileBill
* @subpackage Modules:Payment
*/
class payment extends OSB_module {
public function allocate($VAR) { return $this->tmAllocate($VAR); }
/**
* Template Method to support the allocation of payments to invoices
*
* @uses payment_item
* @uses invoice
*/
public function tmAllocate($VAR) {
$db = DB();
$invoices = array();
include_once(PATH_MODULES.'payment_item/payment_item.inc.php');
$pi = new payment_item;
include_once(PATH_MODULES.'invoice/invoice.inc.php');
$io = new Invoice;
# Get our payment information
$payment = $this->sPaymentsBal($VAR['payment_id']);
# If this payment has been allocated to some invoices, collect those details
$payment_items = $pi->sPaymentAllocation($VAR['payment_id']);
foreach ($io->sInvoicesBal($payment['account_id']) as $index => $details) {
$invoices[$index] = $details;
# Get additional information on the invoices to be displayed.
$invoices[$index]['id'] = $index;
$invoices[$index]['_C'] = sprintf('row%s',$i++%2 ? 1 : 2);
if (isset($payment_items[$index])) {
$invoices[$index]['alloc_amt'] = $payment_items[$index];
unset($payment_items[$index]);
}
}
# Left over payment_items are fully paid invoices
if (count($payment_items)) {
foreach ($io->sInvoicesAcc($payment['account_id'],array_keys($payment_items)) as $index => $details) {
$invoices[$index] = $details;
# Get additional information on the invoices to be displayed.
$invoices[$index]['id'] = $index;
$invoices[$index]['_C'] = sprintf('row%s',$i++%2 ? 1 : 2);
$invoices[$index]['alloc_amt'] = $payment_items[$index];
}
}
# Sort the entries
ksort($invoices);
global $smarty;
$smarty->assign('invoices',$invoices);
$smarty->assign('results',count($invoices));
}
/**
* Compare the billed_amt with the payments, and auto fix when the allocation is not correct
*/
public function taskAuditPaymentItems() {
$db = DB();
$rs = $db->Execute(sprintf('SELECT A.id,A.account_id,A.billed_amt,A.total_amt,IFNULL(A.credit_amt,0) as credit_amt,ROUND(SUM(IFNULL(B.alloc_amt,0)),4) AS alloc_amt FROM %sinvoice A LEFT OUTER JOIN %spayment_item B ON (A.id=B.invoice_id) WHERE A.site_id=%s GROUP BY A.id HAVING A.billed_amt != alloc_amt',AGILE_DB_PREFIX,AGILE_DB_PREFIX,DEFAULT_SITE));
if ($rs && $rs->RecordCount()) {
while (! $rs->EOF) {
$db->Execute(sqlUpdate('invoice',array('billed_amt'=>$rs->fields['alloc_amt']),array('where'=>array('id'=>$rs->fields['id']))));
# Add a memo
$db->Execute(sqlInsert($db,'invoice_memo',array('date_orig'=>time(),'invoice_id'=>$rs->fields['id'],'account_id'=>$rs->fields['account_id'],'type'=>'audit','memo'=>sprintf('Payments applied to this invoice have been changed, due to over allocation, was: %3.2f, payments applied: %3.2f, credits applied: %3.2f',$rs->fields['billed_amt'],$rs->fields['alloc_amt'],$rs->fields['credit_amt']))));
$rs->MoveNext();
}
}
}
public function ListUnbalanced($VAR) { return $this->tmListUnbalanced($VAR); }
/**
* List all invoices that are out of balance with the payment tables.
*
* @return void
*/
public function tmListUnbalanced() {
global $smarty;
$db = DB();
$smart = array();
$counter = 0;
$rs = $db->Execute(sprintf('SELECT B.payment_id AS id,A.account_id,B.invoice_id,A.billed_amt,A.total_amt,IFNULL(A.credit_amt,0) as credit_amt,B.payment_id,ROUND(SUM(IFNULL(B.alloc_amt,0)),4) AS alloc_amt,A.date_orig FROM %sinvoice A LEFT OUTER JOIN %spayment_item B ON (A.id=B.invoice_id) WHERE A.site_id=%s GROUP BY A.id HAVING A.billed_amt != alloc_amt OR A.total_amt < A.billed_amt ORDER BY A.account_id,A.id',AGILE_DB_PREFIX,AGILE_DB_PREFIX,DEFAULT_SITE));
if ($rs && $rs->RecordCount()) {
while (! $rs->EOF) {
$rs->fields['_C'] = ++$counter%s ? 'row1' : 'row2';
array_push($smart,$rs->fields);
$rs->MoveNext();
}
}
$smarty->assign('results',count($smart));
$smarty->assign('limit',count($smart));
$smarty->assign('search_show',$smart);
}
public function ListUnallocated($VAR) { return $this->tmListUnallocated($VAR); }
/**
* Return payments that still have unallocated balances
*
* @return void
* @uses invoice
*/
public function tmListUnallocated($VAR) {
global $smarty;
require_once(PATH_MODULES.'invoice/invoice.inc.php');
$io = new invoice;
$accounts = $io->sAccountsBal();
$smart = array();
$counter = 0;
foreach ($this->sPaymentsBal() as $payment) {
$payment['_C'] = ++$counter%s ? 'row1' : 'row2';
$payment['balance'] = $payment['total_amt']-$payment['alloc_amt'];
$payment['acct_bal'] = isset($accounts[$payment['account_id']]) ? $accounts[$payment['account_id']] : 0;
array_push($smart,$payment);
}
$smarty->assign('results',count($smart));
$smarty->assign('limit',count($smart));
$smarty->assign('search_show',$smart);
}
public function ListInvoicesBalance($VAR) { return $this->tmListInvoicesBalance($VAR); }
public function tmListInvoicesBalance($VAR) {
global $smarty;
$smart = array();
$counter = 0;
foreach ($this->sAccountsBal() as $account_id => $invoices) {
foreach ($invoices as $index => $values) {
$smart[$index] = $values;
$smart[$index]['_C'] = ++$counter%s ? 'row1' : 'row2';
}
}
$smarty->assign('results',count($smart));
$smarty->assign('limit',count($smart));
$smarty->assign('search_show',$smart);
}
/**
* Return a list of payments with an unallocated balance
*
* @param $payment_id Return only this payment ID, otherwise return all payment IDs.
* @return array Payments with a unallocated balance
*/
public function sPaymentsBal($payment_id=null) {
static $sPaymentsBal = array();
if (! count($sPaymentsBal) || (! is_null($payment_id) && ! isset($sPaymentsBal[$payment_id]))) {
$db = &DB();
$rs = $db->Execute(sprintf('SELECT A.id,A.account_id,A.total_amt,ROUND(SUM(IFNULL(B.alloc_amt,0)),4) AS alloc_amt,A.date_orig FROM %spayment A LEFT OUTER JOIN %spayment_item B ON (B.payment_id=A.id) WHERE A.site_id=%s GROUP BY (A.id) HAVING %s alloc_amt != A.total_amt ORDER BY A.account_id',
AGILE_DB_PREFIX,AGILE_DB_PREFIX,DEFAULT_SITE,is_null($payment_id) ? '' : sprintf('A.id=%s OR',$payment_id)));
if ($rs && $rs->RecordCount()) {
while (! $rs->EOF) {
$sPaymentsBal[$rs->fields['id']]['id'] = $rs->fields['id'];
$sPaymentsBal[$rs->fields['id']]['account_id'] = $rs->fields['account_id'];
$sPaymentsBal[$rs->fields['id']]['total_amt'] = $rs->fields['total_amt'];
$sPaymentsBal[$rs->fields['id']]['alloc_amt'] = $rs->fields['alloc_amt'];
$sPaymentsBal[$rs->fields['id']]['date_orig'] = $rs->fields['date_orig'];
$rs->MoveNext();
}
}
}
return (is_null($payment_id) ? $sPaymentsBal : (isset($sPaymentsBal[$payment_id]) ? $sPaymentsBal[$payment_id] : array()));
}
/**
* Return a list of invoices with unbalanced payments
*
* @param $account_id Return invoices with unbalanced payments for an account, otherwise return all accounts
* @return array Invoices with unbalanced payments
*/
public function sAccountsBal($account_id) {
static $sAccountsBal = array();
if (! count($sAccountsBal) || (! is_null($account_id) && ! isset($sAccountsBal[$account_id]))) {
$db = &DB();
$rs = $db->Execute(sprintf('SELECT A.id,A.account_id,A.total_amt,A.billed_amt,IFNULL(A.credit_amt,0) as credit_amt,ROUND(SUM(IFNULL(B.alloc_amt,0)),4) AS alloc_amt,A.date_orig,B.payment_id FROM %sinvoice A INNER JOIN %spayment_item B ON (B.invoice_id=A.id) WHERE A.site_id=%s GROUP BY (A.id) HAVING %s A.total_amt!=alloc_amt+credit_amt ORDER BY A.account_id,A.id',
AGILE_DB_PREFIX,AGILE_DB_PREFIX,DEFAULT_SITE,is_null($account_id) ? '' : sprintf('A.account_id=%s AND',$account_id)));
if ($rs && $rs->RecordCount()) {
while (! $rs->EOF) {
$sAccountsBal[$rs->fields['account_id']][$rs->fields['id']]['id'] = $rs->fields['payment_id'];
$sAccountsBal[$rs->fields['account_id']][$rs->fields['id']]['invoice_id'] = $rs->fields['id'];
$sAccountsBal[$rs->fields['account_id']][$rs->fields['id']]['account_id'] = $rs->fields['account_id'];
$sAccountsBal[$rs->fields['account_id']][$rs->fields['id']]['total_amt'] = $rs->fields['total_amt'];
$sAccountsBal[$rs->fields['account_id']][$rs->fields['id']]['alloc_amt'] = $rs->fields['alloc_amt'];
$sAccountsBal[$rs->fields['account_id']][$rs->fields['id']]['date_orig'] = $rs->fields['date_orig'];
$rs->MoveNext();
}
}
}
return (is_null($account_id) ? $sAccountsBal : (isset($sAccountsBal[$account_id]) ? $sAccountsBal[$account_id] : array()));
}
}
?>