OSB enhancements to date

This commit is contained in:
Deon George
2010-11-30 09:41:08 +11:00
parent 8715a2059b
commit ec6a542bc3
478 changed files with 23423 additions and 9309 deletions

View File

@@ -0,0 +1,33 @@
<?php
/**
* AgileBill - Open Billing Software
*
* This body of work is free software; you can redistribute it and/or
* modify it under the terms of the Open AgileBill License
* License as published at http://www.agileco.com/agilebill/license1-4.txt
*
* Originally authored by Tony Landis, AgileBill LLC
*
* Recent modifications by Deon George
*
* @author Deon George <deonATleenooksDOTnet>
* @copyright 2009 Deon George
* @link http://osb.leenooks.net
*
* @link http://www.agileco.com/
* @copyright 2004-2008 Agileco, LLC.
* @license http://www.agileco.com/agilebill/license1-4.txt
* @author Tony Landis <tony@agileco.com>
* @package AgileBill
* @subpackage Modules:Invoice
*/
/**
* The main AgileBill Payment Class
*
* @package AgileBill
*/
$auth_methods = array (
);
?>

View File

@@ -0,0 +1,41 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports OSB exporting by rending the exportable items.
*
* @package OSB
* @subpackage Export
* @category Models
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Payment extends ORMOSB {
// Relationships
protected $_has_many = array(
'payment_item'=>array(),
'invoice'=>array('through'=>'payment_item'),
);
protected $_belongs_to = array(
'account'=>array(),
'checkout'=>array('foreign_key'=>'checkout_plugin_id'),
);
protected $_sorting = array('date_payment'=>'DESC');
protected $_formats = array(
'date_payment'=>array('Model_Invoice::_filters'=>array('date','d-m-Y')),
'total_amt'=>array('Currency::display'=>array()),
);
/**
* Find all items that are exportable.
*
* @param int $start List payments that were modified this many days ago
*/
public function export($start) {
return ORM::factory('payment')
->where('date_payment','>=',time()-$start*86400)
->find_all();
}
}

View File

@@ -0,0 +1,15 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports OSB exporting by rending the exportable items.
*
* @package OSB
* @subpackage Export
* @category Models
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Payment_Item extends ORMOSB {
protected $_belongs_to = array('payment'=>array(),'invoice'=>array());
}

View File

@@ -0,0 +1,241 @@
<?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()));
}
}
?>

View File

@@ -0,0 +1,248 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<construct>
<!-- Module name -->
<module>payment</module>
<!-- Module supporting database table -->
<table>payment</table>
<!-- Module dependancy(s) (module wont install if these modules are not yet installed) -->
<dependancy>invoice</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>
</index>
<!-- Database fields -->
<field>
<!-- Record ID -->
<id>
<display>Payment</display>
<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>
<!-- The account this payment belongs to -->
<account_id>
<asso_table>account</asso_table>
<asso_field>first_name,last_name</asso_field>
<type>I8</type>
<validate>any</validate>
</account_id>
<!-- Date Payment -->
<date_payment>
<convert>date-time</convert>
<display>Payment Date</display>
<type>I8</type>
</date_payment>
<!-- Checkout plugin for this payment -->
<checkout_plugin_id>
<asso_table>checkout</asso_table>
<asso_field>name</asso_field>
<display>Checkout Plugin</display>
<type>I4</type>
<validate>any</validate>
</checkout_plugin_id>
<!-- Checkout plugin data -->
<checkout_plugin_data>
<type>C(255)</type>
<convert>array</convert>
</checkout_plugin_data>
<!-- Gross payment amount (local currency) -->
<total_amt>
<type>F</type>
<validate>float</validate>
</total_amt>
<!-- Payment Fees (local currency) -->
<fees_amt>
<type>F</type>
</fees_amt>
<!-- The local currency for this payment -->
<billed_currency_id>
<type>I4</type>
</billed_currency_id>
<!-- The amount in billed_currency that was made -->
<actual_total_amt>
<type>F</type>
</actual_total_amt>
<!-- The currency this payment was paid -->
<actual_billed_currency_id>
<type>I4</type>
</actual_billed_currency_id>
<!-- Payment has been refunded when STATUS = 1 -->
<refund_status>
<display>Refund Status</display>
<type>L</type>
</refund_status>
<!-- Account who made payment -->
<source_id>
<asso_table>account</asso_table>
<asso_field>username</asso_field>
<display>Source</display>
<type>I8</type>
<validate>any</validate>
</source_id>
<!-- Whether payment has been cleared -->
<pending_status>
<display>Pending</display>
<type>L</type>
</pending_status>
<!-- IP Address of Client -->
<notes>
<type>X</type>
</notes>
<!-- IP Address of Client -->
<ip>
<type>C(32)</type>
</ip>
</field>
<!-- Methods for this class, and the fields they have access to, if applicable -->
<method>
<add>account_id,date_payment,checkout_plugin_id,total_amt,fees_amt,notes,source_id</add>
<update>account_id,date_payment,checkout_plugin_id,total_amt,fees_amt,notes,pending_status</update>
<search>id,account_id,date_payment,checkout_plugin_id,total_amt,source_id</search>
<view>id,date_orig,account_id,date_payment,checkout_plugin_id,total_amt,fees_amt,notes,source_id</view>
</method>
<!-- Method triggers -->
<trigger></trigger>
<!-- Template page display titles -->
<title>
<view>Payment</view>
</title>
<!-- Template helpers -->
<tpl>
<search_show>
<checkbox>
<field>id</field>
<type>checkbox</type>
<width>25px</width>
</checkbox>
<account_id>
<field>account_id</field>
</account_id>
<source_id>
<field>source_id</field>
</source_id>
<checkout_plugin_id>
<field>checkout_plugin_id</field>
</checkout_plugin_id>
<date_payment>
<field>date_payment</field>
<type>date</type>
</date_payment>
<total_amt>
<field>total_amt</field>
<type>currency</type>
</total_amt>
</search_show>
<user_search_show>
<checkbox>
<field>id</field>
<type>checkbox</type>
<width>25px</width>
</checkbox>
<id>
<field>id</field>
</id>
<date_payment>
<field>date_orig</field>
<type>date</type>
</date_payment>
<total_amt>
<field>total_amt</field>
<type>currency</type>
</total_amt>
</user_search_show>
<ListUnallocated>
<checkbox>
<field>id</field>
<type>checkbox</type>
<width>25px</width>
</checkbox>
<id>
<field>id</field>
</id>
<account_id>
<field>account_id</field>
</account_id>
<date_payment>
<field>date_orig</field>
<type>date</type>
</date_payment>
<total_amt>
<field>total_amt</field>
<type>currency</type>
</total_amt>
<alloc_amt>
<field>alloc_amt</field>
<type>currency</type>
</alloc_amt>
<balance>
<field>balance</field>
<type>currency</type>
</balance>
<acct_bal>
<field>acct_bal</field>
</acct_bal>
</ListUnallocated>
<ListUnbalanced>
<checkbox>
<field>id</field>
<type>checkbox</type>
<width>25px</width>
</checkbox>
<id>
<field>id</field>
</id>
<account_id>
<field>account_id</field>
</account_id>
<invoice_id>
<field>invoice_id</field>
</invoice_id>
<date_payment>
<field>date_orig</field>
<type>date</type>
</date_payment>
<total_amt>
<field>total_amt</field>
<type>currency</type>
</total_amt>
<billed_amt>
<field>billed_amt</field>
<type>currency</type>
</billed_amt>
<alloc_amt>
<field>alloc_amt</field>
<type>currency</type>
</alloc_amt>
</ListUnbalanced>
</tpl>
</construct>

View File

@@ -0,0 +1,61 @@
<?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></dependancy>
<!-- Translated display to use on the tree -->
<display>Payment</display>
<!-- Display a module in the menu tree -->
<menu_display>1</menu_display>
<!-- MODULE Name -->
<name>payment</name>
<!-- MODULE Notes, these notes show up in the modules table, as a description of the module -->
<notes><![CDATA[This module records payments to invoices]]></notes>
<!-- MODULE Parent, the parent node in the tree -->
<parent>invoice</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>
<display>Add</display>
<menu_display>1</menu_display>
<name>add</name>
<notes><![CDATA[Add records]]></notes>
</add>
<delete>
<name>delete</name>
<notes><![CDATA[Delete records]]></notes>
</delete>
<search>
<display>List</display>
<menu_display>1</menu_display>
<name>search</name>
<notes><![CDATA[List records]]></notes>
<page><![CDATA[core:search&module=%%&_next_page_one=view]]></page>
</search>
<search_form>
<display>Search</display>
<menu_display>1</menu_display>
<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>
</module_method>
</install>