Changes to AgileBill

This commit is contained in:
Deon George
2009-08-03 14:10:16 +10:00
parent 0a22cfe22c
commit 27aee719b0
1051 changed files with 219109 additions and 117219 deletions

View File

@@ -1,59 +1,239 @@
<?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
*
* For questions, help, comments, discussion, etc., please join the
* Agileco community forums at http://forum.agileco.com/
*
* Originally authored by Tony Landis, AgileBill LLC
*
* Net Term is used to control payment terms for invoices (determining
* when an invoice is overdue) and when services need to be suspended. It
* will be used to trigger when nagging needs to take place (x days after invoice
* creation), and finally when a suspension can be considered (last date after
* being unpaid).
*
* Products are either configured as:
* + Pre-Paid, ie:
* - Must be paid for before provision
* - Must be paid for before the service date for service to continue as active
* = Thus terms are days before Service Date, with the last date being the renewal
* service date.
* = Services unpaid on the service date should be suspended/terminated.
* = Late fees can be applicable, if the service is contracted.
*
* Or
*
* + Post-Paid, ie:
* - Are provisioned when ordered
* - Remain active unless invoice remains unpaid until the last term period
* = Thus terms continue beyond the service date, with the last payment term being
* the suspension date or service date, whichever the latter.
* = Also, late fees are can be applicable.
*
* Terms are specified as positive/negative numbers, where negative numbers
* are days BEFORE the service date.
* + So for Pre-Paid items, the terms could be for example, -28,-21,-14,-7,0
* (zero is the service date and is implied). The invoice will be generated 28
* days before the service date, and the final warning is 7 days before
* (suspension on the service date). Any positive numbers are ignored.
*
* + Post Paid Items, the terms should be for example, -14,-7,0,7,14. The
* invoice will be generated 14 days before the due date, with the final notice
* being 7 days after the service date. The service will be suspended 14 days
* after the service date.
*
* Invoices can be created in advance (XXX setting), however for:
* + Pre paid, processing ignore the first payment term (invoice generation)
* if it is less.
* + Post Paid, processing will ignore the first payment term (invoice generation)
* if it is less.
*
* The invoice due date is the service date, unless modified by XXX.
*
* Terms are attached to the invoice row in the database (net_term_id), and
* extract from there when performing calculations. When that record is null
* it is obtained from the account_id, then site_id (in that order).
*
* When a mix of pre-paid and post paid items are on the same invoice, nagging
* and suspension calculations are determined against the item type, not the
* invoice.
*
* 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>
* @author Tony Landis <tony@agileco.com>
* @package AgileBill
* @version 1.4.93
* @subpackage Modules:Invoice
*/
class net_term
{
var $taxable=1; # are late fees taxable? 0/1
# Open the constructor for this mod
function net_term()
{
# name of this module:
$this->module = "net_term";
/**
* The main AgileBill Net Term Class
*
* @package AgileBill
* @subpackage Modules:Invoice
*/
class net_term extends OSB_module {
var $taxable=1; # @todo (to move to net_term) are late fees taxable? 0/1
# location of the construct XML file:
$this->xml_construct = PATH_MODULES . "" . $this->module . "/" . $this->module . "_construct.xml";
# Does this term period determine that the service should be suspended
private $toSuspend = false;
# Does this term period determine that the invoice should be sent
private $sendInv = false;
# Which notice number should be sent.
private $notice = false;
# Should we send the final notice
private $lastNotice = false;
# open the construct file for parsing
$C_xml = new CORE_xml;
$construct = $C_xml->xml_to_array($this->xml_construct);
private function getTermDetails($tid) {
static $CACHE = array();
$this->method = $construct["construct"]["method"];
$this->trigger = $construct["construct"]["trigger"];
$this->field = $construct["construct"]["field"];
$this->table = $construct["construct"]["table"];
$this->module = $construct["construct"]["module"];
$this->cache = $construct["construct"]["cache"];
$this->order_by = $construct["construct"]["order_by"];
$this->limit = $construct["construct"]["limit"];
if (! isset($CACHED[$tid])) {
$db = &DB();
$CACHED[$tid] = $db->Execute(sqlSelect($db,'net_term','*',sprintf('status=1 AND id=%s',$tid)));
}
return $CACHED[$tid];
}
### Check usergroup&checkout plugin to determin if net terms available (get best)
function termsAllowed($account_id,$checkout_plugin_id)
{
/**
* Set the TermID of this object
* From this we can work out if what interval this is
* @return boolean true if this term is valid, false if not valid
*/
public function checkTermDetails($tid,$date,$prepaid=false) {
# If date > today and prepaid, then suspend
if ($prepaid && time()-$date > 0) {
$this->toSuspend = true;
return true;
}
$rs = $this->getTermDetails($tid);
if ($rs && $rs->RecordCount()==1) {
$terms = explode(',',$rs->fields['terms']);
sort($terms);
# If the date is outside the first term date, then we'll return here
if (time()-$date < $terms[0]*86400)
return false;
$c = -1;
foreach ($terms as $i => $d) {
$dd = time()-$d*86400;
# If this is prepaid, is this our last notice?
if ($prepaid && (isset($terms[$i+1]) && (time()-$date > $terms[$i+1]*86400))) {
$this->lastNotice = true;
break;
}
# If we matched a term period, we need to exit
if ($date >= $dd || ($prepaid && $d > 0))
break;
$c++;
}
if ($this->lastNotice)
return true;
# If we went 1 loop only, then this must be the first match and thus an invoice needs to go out.
if ($c==0)
$this->sendInv = true;
# If we came out with no matches, then we must be in the suspend time
elseif ((! $prepaid && ($date < $dd)) || ($prepaid && $d > 0))
$this->toSuspend = true;
else
$this->notice = $c;
return true;
}
return false;
}
/**
* Return if this term deterimes that the service should be suspended.
*/
public function getSuspend() {
return $toSuspend;
}
/**
* Return if this term deterimes that the invoice should be generated.
*/
public function sendInvoice() {
return $sendInv;
}
/**
* Return the notice number
*/
public function getNoticeNumber() {
return $this->notice;
}
/**
* Get an array of the term dates, including invoice creation and suspension
*
* Optionally pass the invoice created date.
*/
public function getTermDates($id,$created=null,$due=null) {
$td = array();
$db = &DB();
$query = $db->Execute(sqlSelect($db,'net_term','terms',sprintf('id=%s',$id)));
if ($query && $query->RecordCount()) {
$terms = explode(',',$query->fields['terms']);
sort($terms);
if (is_null($due))
$due = time();
if (! is_null($created))
array_push($td,array('date'=>$created,'desc'=>'Invoice Created'));
$duedate = false;
foreach ($terms as $index => $days) {
if ($created && $created >= $due+$days*86400)
continue;
if (! $duedate && $days>0)
array_push($td,array('date'=>$due-$days*86400,'desc'=>'Invoice Due'));
if ($days == 0) {
$desc = 'Invoice Due';
$duedate = true;
} elseif ($index == 0 && ! count($td))
$desc = 'Invoice Created';
elseif ($index == count($terms)-1)
$desc = 'Service Suspension';
else
$desc = 'Notice';
array_push($td,array('date'=>$due+$days*86400,'desc'=>$desc));
$i++;
}
}
return $td;
}
# Check usergroup&checkout plugin to determin if net terms available (get best)
public function termsAllowed($account_id,$checkout_plugin_id) {
$db=&DB();
$rs=&$db->Execute($sql=sqlSelect($db,"net_term","*","status=1 AND checkout_id=$checkout_plugin_id","fee ASC"));
if($rs && $rs->RecordCount() > 0) {
global $C_auth;
$rs=&$db->Execute($sql=sqlSelect($db,"net_term","*","status=1 AND checkout_id=$checkout_plugin_id","fee ASC"));
if($rs && $rs->RecordCount() > 0) {
global $C_auth;
while(!$rs->EOF) {
$availarr = unserialize($rs->fields['group_avail']);
$availarr = unserialize($rs->fields['group_avail']);
if(is_array($availarr)) {
foreach($availarr as $g) {
if($C_auth->auth_group_by_account_id($account_id,$g)) return $rs->fields['id'];
@@ -64,42 +244,41 @@ class net_term
return 0;
}
### Task to generate late charges & insert into charge module:
function task($VAR)
{
# Task to generate late charges & insert into charge module:
function task($VAR) {
require_once(PATH_MODULES.'email_template/email_template.inc.php');
require_once(PATH_MODULES.'invoice/invoice.inc.php');
$invoice = new invoice;
# get active net terms
$db=&DB();
$rs=&$db->Execute($sql=sqlSelect($db,"net_term","*","status=1"));
if($rs && $rs->RecordCount() > 0)
{
// loop through net terms
$rs=&$db->Execute($sql=sqlSelect($db,"net_term","*","status=1"));
if($rs && $rs->RecordCount() > 0)
{
// loop through net terms
while(!$rs->EOF)
{
$id = $rs->fields['id'];
$last_interval = mktime(0,0,0,date('m'), date('d')-$rs->fields['terms'], date('Y'));
$id = $rs->fields['id'];
$last_interval = mktime(0,0,0,date('m'), date('d')-$rs->fields['terms'], date('Y'));
$i=&$db->Execute($sql=sqlSelect($db,"invoice",
"id,account_id,total_amt,billed_amt,due_date,net_term_date_last,net_term_intervals",
"id,account_id,total_amt,billed_amt,due_date,net_term_date_last,net_term_intervals",
"net_term_id = $id AND
(suspend_billing = 0 OR suspend_billing IS NULL) AND
(billing_status = 0 OR billing_status IS NULL) AND
(billing_status = 0 OR billing_status IS NULL) AND
due_date <= $last_interval AND
net_term_date_last <= $last_interval"));
if($i && $i->RecordCount() > 0)
{
net_term_date_last <= $last_interval"));
if($i && $i->RecordCount() > 0)
{
// loop through invoices
while(!$i->EOF)
{
$terms = $rs->fields['terms'];
while(!$i->EOF)
{
$terms = $rs->fields['terms'];
echo "<BR>" . $start_interval = $i->fields['net_term_date_last'];
echo "<BR>" . $stop_interval = $start_interval+(86400*$terms);
echo "<BR>" . $stop_interval = $start_interval+(86400*$terms);
echo "<BR>". date(UNIX_DATE_FORMAT,$start_interval);
@@ -108,138 +287,59 @@ class net_term
// suspend invoice
$arr['id'] = $i->fields['id'];
$na =& $invoice->voidInvoice($arr,$invoice);
$na =& $invoice->voidInvoice($arr,$invoice);
// suspend billing status
$fields=Array('suspend_billing'=>1);
$db->Execute($sql=sqlUpdate($db,"invoice",$fields,"id = {$i->fields['id']}"));
$db->Execute($sql=sqlUpdate($db,"invoice",$fields,"id = {$i->fields['id']}"));
// send suspend e-mail
if($rs->fields['enable_emails']) {
$email = new email_template;
$email->send('net_term_suspend', $i->fields['account_id'], $i->fields['id'], $rs->fields['suspend_intervals'], $i->fields['net_term_intervals']);
$email->send('net_term_suspend', $i->fields['account_id'], $i->fields['id'], $rs->fields['suspend_intervals'], $i->fields['net_term_intervals']);
}
}
else
}
else
{
// calc late fee
if($rs->fields['fee_type'] == 1)
if($rs->fields['fee_type'] == 2)
$fee = $rs->fields['fee'];
else
elseif($rs->fields['fee_type'] == 1)
$fee = ($i->fields['total_amt'] - $i->fields['billed_amt']) * $rs->fields['fee'];
// create late charge
if($fee>0)
if($fee>0)
{
$fields=Array( 'date_orig'=> time(),
'status'=> 0,
'account_id'=> $i->fields['account_id'],
'status'=> 0,
'account_id'=> $i->fields['account_id'],
'amount'=> $fee,
'sweep_type'=> $rs->fields['sweep_type'],
'taxable'=> $this->taxable,
'quantity' => 1,
'attributes'=> "Name=={$rs->fields['name']}\r\nInterval==".date(UNIX_DATE_FORMAT,$start_interval)." - ".date(UNIX_DATE_FORMAT,$stop_interval), // todo: translate
'description'=> $rs->fields['sku']);
'description'=> $rs->fields['sku']);
$db->Execute($sql=sqlInsert($db,"charge",$fields));
// update invoice
$_fields['net_term_intervals'] = $i->fields['net_term_intervals']+1;
$_fields['net_term_date_last'] = $stop_interval;
$db->Execute($sql=sqlUpdate($db,"invoice",$_fields,"id={$i->fields['id']}"));
$_fields['net_term_date_last'] = $stop_interval;
$db->Execute($sql=sqlUpdate($db,"invoice",$_fields,"id={$i->fields['id']}"));
echo "<BR><BR>$sql";
}
}
// send late fee/payment reminder e-mail:
if($rs->fields['enable_emails']){
$email = new email_template;
$email->send('net_term_late_notice', $i->fields['account_id'], $i->fields['id'], number_format($fee,2), number_format($rs->fields['suspend_intervals']-$i->fields['net_term_intervals']));
}
$email->send('net_term_late_notice', $i->fields['account_id'], $i->fields['id'], number_format($fee,2), number_format($rs->fields['suspend_intervals']-$i->fields['net_term_intervals']));
}
}
$i->MoveNext();
}
}
}
$rs->MoveNext();
}
}
}
}
##############################
## ADD ##
##############################
function add($VAR)
{
$type = "add";
$this->method["$type"] = explode(",", $this->method["$type"]);
$db = new CORE_database;
$db->add($VAR, $this, $type);
}
##############################
## VIEW ##
##############################
function view($VAR)
{
$type = "view";
$this->method["$type"] = explode(",", $this->method["$type"]);
$db = new CORE_database;
$db->view($VAR, $this, $type);
}
##############################
## UPDATE ##
##############################
function update($VAR)
{
$type = "update";
$this->method["$type"] = explode(",", $this->method["$type"]);
$db = new CORE_database;
$db->update($VAR, $this, $type);
}
##############################
## DELETE ##
##############################
function delete($VAR)
{
$db = new CORE_database;
$db->mass_delete($VAR, $this, "");
}
##############################
## SEARCH FORM ##
##############################
function search_form($VAR)
{
$type = "search";
$this->method["$type"] = explode(",", $this->method["$type"]);
$db = new CORE_database;
$db->search_form($VAR, $this, $type);
}
##############################
## SEARCH ##
##############################
function search($VAR)
{
$type = "search";
$this->method["$type"] = explode(",", $this->method["$type"]);
$db = new CORE_database;
$db->search($VAR, $this, $type);
}
##############################
## SEARCH SHOW ##
##############################
function search_show($VAR)
{
$type = "search";
$this->method["$type"] = explode(",", $this->method["$type"]);
$db = new CORE_database;
$db->search_show($VAR, $this, $type);
}
}
?>
?>

View File

@@ -1,70 +1,135 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<construct>
<module>net_term</module>
<table>net_term</table>
<dependancy>charge</dependancy>
<cache>0</cache>
<order_by>name</order_by>
<limit>35</limit>
<field>
<id>
<type>I4</type>
<unique>1</unique>
</id>
<site_id>
<type>I4</type>
</site_id>
<status>
<type>L</type>
</status>
<group_avail>
<type>X2</type>
<validate>any</validate>
<convert>array</convert>
</group_avail>
<checkout_id>
<type>I4</type>
<asso_table>checkout</asso_table>
<asso_field>name</asso_field>
</checkout_id>
<name>
<type>C(16)</type>
<min_len>2</min_len>
<max_len>16</max_len>
<validate>alphanumeric</validate>
<unique>1</unique>
</name>
<sku>
<type>C(16)</type>
<validate>alphanumeric</validate>
</sku>
<fee_type>
<type>L</type>
</fee_type>
<fee>
<type>F</type>
<validate>float</validate>
</fee>
<suspend_intervals>
<type>I4</type>
<validate>numeric</validate>
</suspend_intervals>
<enable_emails>
<type>L</type>
</enable_emails>
<sweep_type>
<type>I4</type>
</sweep_type>
<terms>
<type>I4</type>
</terms>
</field>
<method>
<add>id,status,group_avail,checkout_id,name,sku,fee_type,fee,suspend_intervals,enable_emails,sweep_type,terms</add>
<update>id,status,group_avail,checkout_id,name,sku,fee_type,fee,suspend_intervals,enable_emails,sweep_type,terms</update>
<delete>id,status,group_avail,checkout_id,name,sku,fee_type,fee,suspend_intervals,enable_emails,sweep_type,terms</delete>
<view>id,status,group_avail,checkout_id,name,sku,fee_type,fee,suspend_intervals,enable_emails,sweep_type,terms</view>
<search>id,status,checkout_id,name,sku,fee_type,fee,suspend_intervals,enable_emails,sweep_type,terms</search>
</method>
<trigger>0</trigger>
</construct>
<construct>
<!-- Module name -->
<module>net_term</module>
<!-- Module supporting database table -->
<table>net_term</table>
<!-- Module dependancy(s) (module wont install if these modules are not yet installed) -->
<dependancy></dependancy>
<!-- DB cache in seconds -->
<cache>0</cache>
<!-- Default order_by field for SQL queries -->
<order_by>name</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>
<index>1</index>
<type>I4</type>
<unique>1</unique>
</id>
<!-- Site ID -->
<site_id>
<index>1</index>
<type>I4</type>
</site_id>
<!-- Record active (BOOL)-->
<status>
<display>Active</display>
<type>L</type>
</status>
<!-- Applicable to Group -->
<group_avail>
<type>X2</type>
<validate>any</validate>
<convert>array</convert>
</group_avail>
<!-- Checkout Plugins that this term is valid for -->
<checkout_id>
<type>X2</type>
<convert>array</convert>
</checkout_id>
<!-- Free form name for this term eg: NET30DAYS -->
<name>
<type>C(16)</type>
<min_len>2</min_len>
<max_len>16</max_len>
<validate>alphanumeric</validate>
<unique>1</unique>
</name>
<!-- @deprecate -->
<sku>
<type>C(16)</type>
<validate>alphanumeric</validate>
</sku>
<!-- Fee type if terms exceeded Fixed/Percentage -->
<fee_type>
<type>L</type>
</fee_type>
<!-- Fee $x or %x -->
<fee>
<type>F</type>
</fee>
<fee_day>
<type>I4</type>
</fee_day>
<!-- Day to suspend service (overrides last term date) -->
<suspend_day>
<type>I4</type>
</suspend_day>
<!-- Enable email notification for this term -->
<enable_emails>
<type>L</type>
</enable_emails>
<!-- How often to check for violations -->
<sweep_type>
<type>I4</type>
</sweep_type>
<!-- Terms: eg -7,7,14,21,28 (Negative terms ignored for new services to be provisioned) -->
<terms>
<type>C(32)</type>
<validate>any</validate>
</terms>
<!-- XX Number of days in advance of the service due date to generate invoices -->
<invoice_advance_gen>
<type>I4</type>
</invoice_advance_gen>
</field>
<!-- Methods for this class, and the fields they have access to, if applicable -->
<method>
<add>status,group_avail,name,fee_type,fee,fee_day,suspend_day,enable_emails,sweep_type,terms,checkout_id</add>
<delete>id,status,group_avail,name,fee_type,fee,fee_day,suspend_day,enable_emails,sweep_type,terms,checkout_id</delete>
<search>id,status,name,fee</search>
<update>id,status,group_avail,name,fee_type,fee,fee_day,suspend_day,enable_emails,sweep_type,terms,checkout_id</update>
<view>id,status,group_avail,name,fee_type,fee,fee_day,suspend_day,enable_emails,sweep_type,terms,checkout_id</view>
</method>
<!-- Method triggers -->
<trigger></trigger>
<!-- Template page display titles -->
<title>
</title>
<!-- Template helpers -->
<tpl>
<search_show>
<checkbox>
<field>id</field>
<type>checkbox</type>
<width>25px</width>
</checkbox>
<status>
<field>status</field>
<type>bool_icon</type>
<width>20px</width>
</status>
<name>
<field>name</field>
</name>
<fee>
<field>fee</field>
</fee>
</search_show>
</tpl>
</construct>

View File

@@ -1,37 +1,55 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<install>
<module_properties>
<name>net_term</name>
<parent>setup</parent>
<notes>This module controls the groups and checkout plugins that net terms will be available to</notes>
<menu_display>1</menu_display>
<dependancy>charge</dependancy>
<sub_modules></sub_modules>
</module_properties>
<sql_inserts>
<module_method>
<add>
<name>add</name>
<page>%%:add</page>
<menu_display>1</menu_display>
</add>
<update>
<name>update</name>
</update>
<delete>
<name>delete</name>
</delete>
<view>
<name>view</name>
<page>core:search&amp;module=%%&amp;_escape=1</page>
<menu_display>1</menu_display>
</view>
<search>
<name>search</name>
</search>
<search_show>
<name>search_show</name>
</search_show>
</module_method>
</sql_inserts>
</install>
<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>Terms</display>
<!-- Display a module in the menu tree -->
<menu_display>1</menu_display>
<!-- MODULE Name -->
<name>net_term</name>
<!-- MODULE Notes, these notes show up in the modules table, as a description of the module -->
<notes><![CDATA[This module controls the groups and checkout plugins that net terms will be available to]]></notes>
<!-- MODULE Parent, the parent node in the tree -->
<parent>setup</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_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>