OSB enhancements to date
This commit is contained in:
159
modules/payment_item/payment_item.inc.php
Normal file
159
modules/payment_item/payment_item.inc.php
Normal file
@@ -0,0 +1,159 @@
|
||||
<?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_item extends OSB_module {
|
||||
public function allocate($VAR) { return $this->doAllocate($VAR); }
|
||||
/**
|
||||
* Allocate a payment to invoices
|
||||
*
|
||||
* @uses invoice
|
||||
* @uses payment
|
||||
*/
|
||||
public function doAllocate($VAR) {
|
||||
$db = DB();
|
||||
|
||||
# Get the payment invoices for this account.
|
||||
include_once(PATH_MODULES.'payment/payment.inc.php');
|
||||
$po = new payment;
|
||||
|
||||
# Get our payment information
|
||||
if (! $payment = $po->sPaymentsBal($VAR['payment_id']))
|
||||
return false;
|
||||
|
||||
# Check that our allocation is correct.
|
||||
$total = round(array_sum($VAR['payment_item_allocate']),2);
|
||||
|
||||
if ($total > $payment['total_amt']) {
|
||||
global $C_debug;
|
||||
$C_debug->alert(sprintf('%s: (%3.2f/%3.2f) ?',_('Payment over allocated'),$total,$payment['total_amt']));
|
||||
return false;
|
||||
}
|
||||
|
||||
# If this payment has been allocated to some invoices, collect those details
|
||||
$payment_items = $this->sPaymentAllocation($VAR['payment_id']);
|
||||
|
||||
# Get the unpaid invoices for this account.
|
||||
include_once(PATH_MODULES.'invoice/invoice.inc.php');
|
||||
$io = new invoice;
|
||||
$invoices = $io->sInvoicesAcc($payment['account_id'],array_keys($VAR['payment_item_allocate']));
|
||||
|
||||
# Apply the new allocation
|
||||
foreach ($VAR['payment_item_allocate'] as $invoice => $alloc) {
|
||||
if (! trim($alloc))
|
||||
continue;
|
||||
|
||||
# Sanity check
|
||||
if (! isset($invoices[$invoice])) {
|
||||
global $C_debug;
|
||||
$C_debug->alert(sprintf('Payment for invoice doesnt exist? (%s)?',$invoices['invoice']));
|
||||
|
||||
# This payment has been processed
|
||||
if (isset($payment_items[$invoice]))
|
||||
unset($payment_items[$invoice]);
|
||||
|
||||
continue;
|
||||
|
||||
} elseif ((isset($payment_items[$invoice]) && $invoices[$invoice]['balance']+$payment_items[$invoice] < $alloc)
|
||||
|| (! isset($payment_items[$invoice]) && $invoices[$invoice]['balance'] < $alloc)) {
|
||||
|
||||
global $C_debug;
|
||||
$C_debug->alert(sprintf('Over allocation for invoice %s - remaining balance is %s?',$invoice,$invoices[$invoice]['balance']));
|
||||
|
||||
# This payment has been processed
|
||||
if (isset($payment_items[$invoice]))
|
||||
unset($payment_items[$invoice]);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
# Have we already applied payments to this invoice in the past
|
||||
if (isset($payment_items[$invoice])) {
|
||||
if ($alloc != $payment_items[$invoice]) {
|
||||
# Record the adjusted payment details
|
||||
$db->Execute(sqlUpdate('payment_item',array('date_last'=>time(),'alloc_amt'=>$alloc),array('where'=>array('payment_id'=>$VAR['payment_id'],'invoice_id'=>$invoice,'alloc_amt'=>$payment_items[$invoice]))));
|
||||
|
||||
# Adjust the invoice balance
|
||||
$db->Execute(sqlUpdate('invoice',array('billed_amt'=>$invoices[$invoice]['billed_amt']+$alloc-$payment_items[$invoice]),array('where'=>array('id'=>$invoice,'billed_amt'=>$invoices[$invoice]['billed_amt']))));
|
||||
}
|
||||
|
||||
# This payment has been processed
|
||||
unset($payment_items[$invoice]);
|
||||
|
||||
# New payment allocation
|
||||
} else {
|
||||
# Reduce the invoice balance
|
||||
$db->Execute(sqlUpdate('invoice',array('billed_amt'=>$invoices[$invoice]['billed_amt']+$alloc),array('where'=>array('id'=>$invoice))));
|
||||
|
||||
# Record the payment
|
||||
$db->Execute(sqlInsert($db,'payment_item',array('date_orig'=>time(),'date_last'=>time(),'payment_id'=>$VAR['payment_id'],'invoice_id'=>$invoice,'alloc_amt'=>$alloc)));
|
||||
}
|
||||
}
|
||||
|
||||
# Any left over payments need to be reversed
|
||||
foreach ($payment_items as $invoice => $alloc) {
|
||||
if (! $alloc)
|
||||
continue;
|
||||
|
||||
$db->Execute(sqlUpdate('payment_item',array('date_last'=>time(),'alloc_amt'=>0),array('where'=>array('payment_id'=>$VAR['payment_id'],'invoice_id'=>$invoice,'alloc_amt'=>$payment_items[$invoice]))));
|
||||
$db->Execute(sqlUpdate('invoice',array('billed_amt'=>$invoices[$invoice]['billed_amt']-$alloc),array('where'=>array('id'=>$invoice,'billed_amt'=>$invoices[$invoice]['billed_amt']))));
|
||||
}
|
||||
|
||||
# Force the refresh of the account balances
|
||||
$io->sInvoicesBal(null,true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the payment allocation for a payment ID
|
||||
*
|
||||
* @param $payment_id Payment ID to retrieve
|
||||
* @return array Invoices and the payment location
|
||||
*/
|
||||
public function sPaymentAllocation($payment_id) {
|
||||
static $sPaymentAllocation = array();
|
||||
|
||||
if (! isset($sPaymentsBalance[$payment_id])) {
|
||||
$db = DB();
|
||||
$rs = $db->Execute(sqlSelect('payment_item','invoice_id,alloc_amt',array('where'=>array('payment_id'=>$payment_id))));
|
||||
|
||||
if ($rs && $rs->RecordCount()) {
|
||||
while (! $rs->EOF) {
|
||||
$sPaymentsBalance[$payment_id][$rs->fields['invoice_id']] = $rs->fields['alloc_amt'];
|
||||
$rs->MoveNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $sPaymentsBalance[$payment_id];
|
||||
}
|
||||
}
|
||||
?>
|
82
modules/payment_item/payment_item_construct.xml
Normal file
82
modules/payment_item/payment_item_construct.xml
Normal file
@@ -0,0 +1,82 @@
|
||||
<?xml version="1.0" encoding="ISO-8859-1" ?>
|
||||
<construct>
|
||||
<!-- Module name -->
|
||||
<module>payment_item</module>
|
||||
<!-- Module supporting database table -->
|
||||
<table>payment_item</table>
|
||||
<!-- Module dependancy(s) (module wont install if these modules are not yet installed) -->
|
||||
<dependancy>payment</dependancy>
|
||||
<!-- DB cache in seconds -->
|
||||
<cache>0</cache>
|
||||
<!-- Default order_by field for SQL queries -->
|
||||
<order_by>date_orig</order_by>
|
||||
<!-- Default SQL limit for SQL queries -->
|
||||
<limit>25</limit>
|
||||
<!-- Schema version (used to determine if the schema has change during upgrades) -->
|
||||
<version>1</version>
|
||||
|
||||
<!-- Database indexes -->
|
||||
<index>
|
||||
<!--
|
||||
id,site_id,payment_id,invoice_id
|
||||
-->
|
||||
</index>
|
||||
|
||||
<!-- Database fields -->
|
||||
<field>
|
||||
<!-- Record ID -->
|
||||
<id>
|
||||
<index>1</index>
|
||||
<type>I4</type>
|
||||
<unique>1</unique>
|
||||
</id>
|
||||
<!-- Site ID -->
|
||||
<site_id>
|
||||
<index>1</index>
|
||||
<type>I4</type>
|
||||
</site_id>
|
||||
<!-- Date record created -->
|
||||
<date_orig>
|
||||
<convert>date-now</convert>
|
||||
<display>Date Created</display>
|
||||
<type>I8</type>
|
||||
</date_orig>
|
||||
<!-- Date record updated -->
|
||||
<date_last>
|
||||
<convert>date-now</convert>
|
||||
<display>Date Updated</display>
|
||||
<type>I8</type>
|
||||
</date_last>
|
||||
<!-- Payment ID -->
|
||||
<payment_id>
|
||||
<asso_table>account</asso_table>
|
||||
<type>I8</type>
|
||||
</payment_id>
|
||||
<!-- Invoice ID -->
|
||||
<invoice_id>
|
||||
<asso_table>invoice</asso_table>
|
||||
<type>I8</type>
|
||||
</invoice_id>
|
||||
<!-- Gross amount allocated -->
|
||||
<alloc_amt>
|
||||
<type>F</type>
|
||||
<validate>float</validate>
|
||||
</alloc_amt>
|
||||
</field>
|
||||
|
||||
<!-- Methods for this class, and the fields they have access to, if applicable -->
|
||||
<method>
|
||||
<add>payment_id,invoice_id,alloc_amt</add>
|
||||
</method>
|
||||
|
||||
<!-- Method triggers -->
|
||||
<trigger></trigger>
|
||||
|
||||
<!-- Template page display titles -->
|
||||
<title>
|
||||
</title>
|
||||
|
||||
<!-- Template helpers -->
|
||||
<tpl>
|
||||
</tpl>
|
||||
</construct>
|
58
modules/payment_item/payment_item_install.xml
Normal file
58
modules/payment_item/payment_item_install.xml
Normal file
@@ -0,0 +1,58 @@
|
||||
<?xml version="1.0" encoding="ISO-8859-1" ?>
|
||||
<install>
|
||||
<!-- Tree Menu Module Properties -->
|
||||
<module_properties>
|
||||
<!-- MODULE Dependancy, this module wont be installed if the dependant modules dont exist -->
|
||||
<dependancy>payment</dependancy>
|
||||
<!-- Translated display to use on the tree -->
|
||||
<display></display>
|
||||
<!-- Display a module in the menu tree -->
|
||||
<menu_display>0</menu_display>
|
||||
<!-- MODULE Name -->
|
||||
<name>payment_item</name>
|
||||
<!-- MODULE Notes, these notes show up in the modules table, as a description of the module -->
|
||||
<notes><![CDATA[This module records payments allocated to invoices]]></notes>
|
||||
<!-- MODULE Parent, the parent node in the tree -->
|
||||
<parent>payment</parent>
|
||||
<!-- SUB Modules to install with this one -->
|
||||
<sub_modules></sub_modules>
|
||||
<!-- MODULE Type (core|base), core modules cannot be deleted, unrecognised types are ignored. -->
|
||||
<type></type>
|
||||
</module_properties>
|
||||
|
||||
<!-- Tree Menu & Module Methods to load, they will be assigned the group permissions on install time, as selected by the user. -->
|
||||
<module_method>
|
||||
<add>
|
||||
<name>add</name>
|
||||
<notes><![CDATA[Add records]]></notes>
|
||||
</add>
|
||||
<delete>
|
||||
<name>delete</name>
|
||||
<notes><![CDATA[Delete records]]></notes>
|
||||
</delete>
|
||||
<search>
|
||||
<name>search</name>
|
||||
<notes><![CDATA[List records]]></notes>
|
||||
<page><![CDATA[core:search&module=%%&_next_page_one=view]]></page>
|
||||
</search>
|
||||
<search_form>
|
||||
<name>search_form</name>
|
||||
<notes><![CDATA[Search for records]]></notes>
|
||||
</search_form>
|
||||
<search_show>
|
||||
<name>search_show</name>
|
||||
<notes><![CDATA[Show the results of a search]]></notes>
|
||||
</search_show>
|
||||
<update>
|
||||
<name>update</name>
|
||||
<notes><![CDATA[Update a record]]></notes>
|
||||
</update>
|
||||
<view>
|
||||
<name>view</name>
|
||||
<notes><![CDATA[View a record]]></notes>
|
||||
</view>
|
||||
<allocate>
|
||||
<notes><![CDATA[Allocate payments to an invoice]]></notes>
|
||||
</allocate>
|
||||
</module_method>
|
||||
</install>
|
Reference in New Issue
Block a user