360 lines
9.5 KiB
PHP
360 lines
9.5 KiB
PHP
<?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 Module:Charge
|
|
*/
|
|
|
|
/**
|
|
* The main AgileBill Charge Class
|
|
*
|
|
* @package AgileBill
|
|
* @subpackage Module:Charge
|
|
*/
|
|
class charge extends OSB_module {
|
|
private $xmlrpc = false;
|
|
|
|
public function task_SweepDaily() {
|
|
$this->sweep(0);
|
|
}
|
|
|
|
public function task_SweepWeekly() {
|
|
$this->sweep(1);
|
|
}
|
|
|
|
public function task_SweepMonthly() {
|
|
$this->sweep(2);
|
|
}
|
|
|
|
public function task_SweepQuarterly() {
|
|
$this->sweep(3);
|
|
}
|
|
|
|
public function task_SweepSemiAnnually() {
|
|
$this->sweep(4);
|
|
}
|
|
|
|
public function task_SweepAnnually() {
|
|
$this->sweep(5);
|
|
}
|
|
|
|
private function sweep($type) {
|
|
global $C_list;
|
|
|
|
# Load required elements
|
|
include_once(PATH_MODULES.'invoice/invoice.inc.php');
|
|
include_once(PATH_MODULES.'account/account.inc.php');
|
|
|
|
$account_billing = false;
|
|
if ($C_list->is_installed('account_billing')) {
|
|
include_once(PATH_MODULES.'account_billing/account_billing.inc.php');
|
|
$abo = new account_billing;
|
|
}
|
|
|
|
$charges = $this->sql_GetRecords(array('where'=>array('status'=>0,'sweep_type'=>$type),'orderby'=>'account_id,date_orig'));
|
|
|
|
# No charges
|
|
if (! count($charges))
|
|
return true;
|
|
|
|
# Start a transaction
|
|
$db = &DB();
|
|
if (AGILE_DB_TYPE == 'mysqlt') {
|
|
$db->StartTrans();
|
|
|
|
if (! $db->hasTransactions) {
|
|
global $C_debug;
|
|
|
|
$msg = "Transactions not supported in 'mysql' driver. Use 'mysqlt' or 'mysqli' driver";
|
|
$C_debug->alert($msg);
|
|
$C_debug->error(__FILE__,__METHOD__,$msg);
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
$ao = false;
|
|
$io = false;
|
|
foreach ($charges as $charge) {
|
|
# First Run
|
|
if (! $ao) {
|
|
$ao = new account($charge['account_id']);
|
|
}
|
|
|
|
# Next run, are we onto a new account?
|
|
if ($ao->getRecordAttr('id') != $charge['account_id']) {
|
|
$rs = $io->sql_SaveRecord(true);
|
|
|
|
if (! $rs) {
|
|
global $C_debug;
|
|
|
|
$C_debug->error(__FILE__,__METHOD__,$db->ErrorMsg());
|
|
$db->FailTrans();
|
|
|
|
return false;
|
|
}
|
|
|
|
if (AGILE_DB_TYPE == 'mysqlt')
|
|
$db->CompleteTrans();
|
|
|
|
# Start next invoice
|
|
$io = false;
|
|
|
|
# Load our new account object
|
|
$ao = new account($charge['account_id']);
|
|
}
|
|
|
|
if (! $io) {
|
|
# Generate an invoice id
|
|
$io = new invoice();
|
|
$io->setRecordAttr('id',sqlGenID($db,'invoice'));
|
|
$io->setRecordAttr('account_id',$charge['account_id']);
|
|
$io->setRecordAttr('billed_currency_id',$ao->getRecordAttr('currency_id'));
|
|
$io->setRecordAttr('actual_billed_currency_id',DEFAULT_CURRENCY);
|
|
$io->setRecordAttr('reseller_id',$ao->getRecordAttr('reseller_id'));
|
|
$io->setRecordAttr('checkout_plugin_id',$ao->getRecordAttr('checkout_plugin_id'));
|
|
$io->setRecordAttr('checkout_plugin_data',$ao->getRecordAttr('checkout_plugin_data'));
|
|
$io->setRecordAttr('grace_period',$ao->getRecordAttr('invoice_grace'));
|
|
$io->setRecordAttr('affiliate_id',$ao->getRecordAttr('affiliate_id'));
|
|
$io->setRecordAttr('campaign_id',null);
|
|
$io->setRecordAttr('notice_next_date',time());
|
|
$io->setRecordAttr('billing_status',0);
|
|
$io->setRecordAttr('print_status',0);
|
|
$io->setRecordAttr('process_status',0);
|
|
$io->setRecordAttr('status',1);
|
|
// $io->setRecordAttr('suspend_billing',0);
|
|
$io->setRecordAttr('billed_amt',0);
|
|
$io->setRecordAttr('actual_billed_amt',0);
|
|
$io->setRecordAttr('notice_count',0);
|
|
$io->setRecordAttr('type',1);
|
|
$io->setRecordAttr('notice_max',MAX_BILLING_NOTICE);
|
|
$io->setRecordAttr('account_billing_id',null);
|
|
|
|
# @todo this should be a system default
|
|
$io->setRecordAttr('due_date',time()+7*86400);
|
|
}
|
|
|
|
$io->aaddItem(array(
|
|
'charge_id'=>$charge['id'],
|
|
'date_start'=>$charge['date_orig'],
|
|
'date_stop'=>$charge['date_orig'],
|
|
'domain_name'=>null,
|
|
'domain_tld'=>null,
|
|
'domain_type'=>null,
|
|
'domain_term'=>null,
|
|
'item_type'=>5,
|
|
'price_setup'=>0,
|
|
'price_base'=>$charge['amount'],
|
|
'price_type'=>3,
|
|
'product_id'=>$charge['product_id'],
|
|
'product_name'=>$charge['description'],
|
|
'product_attr'=>$charge['attributes'],
|
|
'product_attr_cart'=>null,
|
|
'quantity'=>$charge['quantity'],
|
|
'recurring_schedule'=>null,
|
|
'service_id'=>$charge['service_id'],
|
|
'sku'=>null
|
|
));
|
|
|
|
# @todo Move this update, need to incase the charge add fails.
|
|
$db->Execute(sqlUpdate($db,'charge',array('status'=>1),array('id'=>$charge['id'])));
|
|
}
|
|
|
|
# Save invoice
|
|
if ($io) {
|
|
$rs = $io->sql_SaveRecord(true);
|
|
|
|
if (! $rs) {
|
|
global $C_debug;
|
|
|
|
$C_debug->error(__FILE__,__METHOD__,$db->ErrorMsg());
|
|
$db->FailTrans();
|
|
|
|
return false;
|
|
}
|
|
|
|
if (AGILE_DB_TYPE == 'mysqlt')
|
|
$db->CompleteTrans();
|
|
}
|
|
}
|
|
|
|
function api($VAR) {
|
|
$db = &DB();
|
|
|
|
# amount
|
|
if(@$VAR['amount'] <= 0) {
|
|
return $this->api_return(0,'','Invalid value supplied for the \'amount\' parameter, must be greater than 0');
|
|
} else {
|
|
$amount = $VAR['amount'];
|
|
}
|
|
|
|
# sweep_type
|
|
if(@$VAR['sweep_type'] <= 6) {
|
|
$sweep_type = $VAR['sweep_type'];
|
|
} else {
|
|
return $this->api_return(0,'','Invalid value supplied for the \'sweep_type\' parameter, must be 0-6');
|
|
}
|
|
|
|
# account_id OR service_id
|
|
if(empty($VAR['account_id']) && empty($VAR['service_id'])) {
|
|
return $this->api_return(0,'','Either the \'account_id\' or \'service_id\' parameter must be provided');
|
|
} else {
|
|
|
|
# check the account id
|
|
if(!empty($VAR['account_id'])) {
|
|
$sql = "SELECT * FROM ".AGILE_DB_PREFIX."account WHERE
|
|
id = " . $db->qstr($VAR['account_id']) . " OR
|
|
username = " . $db->qstr($VAR['account_id']) . " AND
|
|
site_id = " . $db->qstr(DEFAULT_SITE);
|
|
$rs = $db->Execute($sql);
|
|
if ($rs === false) {
|
|
global $C_debug;
|
|
$C_debug->error('charge.inc.php','charge :: api()', $db->ErrorMsg(). "\r\n\r\n". $sql);
|
|
}
|
|
if($rs->RecordCount() == 1) {
|
|
$account_id = $rs->fields['id'];
|
|
} else {
|
|
return $this->api_return(0,'','The \'account_id\' value provided does not exist');
|
|
}
|
|
|
|
# check the service id
|
|
} elseif(!empty($VAR['service_id'])) {
|
|
$sql = "SELECT id,account_id FROM ".AGILE_DB_PREFIX."service WHERE
|
|
site_id = " . $db->qstr(DEFAULT_SITE) . " AND
|
|
id = " . $db->qstr($VAR['service_id']);
|
|
$rs = $db->Execute($sql);
|
|
if ($rs === false) {
|
|
global $C_debug;
|
|
$C_debug->error('charge.inc.php','charge :: api()', $db->ErrorMsg(). "\r\n\r\n". $sql);
|
|
}
|
|
if($rs->RecordCount() == 1) {
|
|
$service_id = $VAR['service_id'];
|
|
$account_id = $rs->fields['account_id'];
|
|
} else {
|
|
return $this->api_return(0,'','The \'service_id\' value provided does not exist');
|
|
}
|
|
}
|
|
}
|
|
|
|
# taxable
|
|
if(empty($VAR['taxable']))
|
|
$taxable = 0;
|
|
else
|
|
$taxable = $VAR['taxable'];
|
|
|
|
# attributes
|
|
if(!empty($VAR['attributes'])) {
|
|
@$attributes = ereg_replace("@@", "\r\n", $VAR['attributes']);
|
|
@$attributes = ereg_replace("--", "==", $attributes);
|
|
} else {
|
|
$attributes = false;
|
|
}
|
|
|
|
# quantity
|
|
if(empty($VAR['quantity']))
|
|
$quantity = 1;
|
|
else
|
|
$quantity = $VAR['quantity'];
|
|
|
|
# product id
|
|
if(empty($VAR['product_id'])) {
|
|
$product_id = false;
|
|
} else {
|
|
$product_id = $VAR['product_id'];
|
|
}
|
|
|
|
# description
|
|
if(empty($VAR['description'])) {
|
|
$description = false;
|
|
} else {
|
|
$description = $VAR['description'];
|
|
}
|
|
|
|
/* Start: SQL Insert Statement */
|
|
$sql = "SELECT * FROM ".AGILE_DB_PREFIX."charge WHERE id = -1";
|
|
$rs = $db->Execute($sql);
|
|
|
|
$id = $db->GenID(AGILE_DB_PREFIX . 'charge_id');
|
|
$insert = Array ( 'id' => $id,
|
|
'site_id' => DEFAULT_SITE,
|
|
'date_orig' => time(),
|
|
'status' => 0,
|
|
'sweep_type' => $sweep_type,
|
|
'account_id' => @$account_id,
|
|
'service_id' => @$service_id,
|
|
'product_id' => @$product_id,
|
|
'amount' => $amount,
|
|
'quantity' => $quantity,
|
|
'taxable' => $taxable,
|
|
'attributes' => $attributes,
|
|
'description' => $description );
|
|
$sql = $db->GetInsertSQL($rs, $insert);
|
|
$result = $db->Execute($sql);
|
|
if ($result === false) {
|
|
global $C_debug;
|
|
$C_debug->error('charge.inc.php','charge :: api()', $db->ErrorMsg(). "\r\n\r\n". $sql);
|
|
return $this->api_return(0,'','The SQL insert failed!');
|
|
} else {
|
|
return $this->api_return(1,$id,'');
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function api_return($status=0,$id='',$error='') {
|
|
if (! $this->xmlrpc) {
|
|
echo "status=={$status}++charge_id={$id}++error=={$error}";
|
|
} else {
|
|
$arr = array('status'=>$status, 'charge_id'=>$id, 'error'=> $error);
|
|
return $arr;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Add a record
|
|
*/
|
|
public function add($VAR) {
|
|
if (! empty($VAR['attributes'])) {
|
|
$attr = '';
|
|
|
|
for ($i=0; $i<count($VAR['attributes']); $i++)
|
|
if (! empty($VAR['attributes'][$i][0]))
|
|
$attr .= sprintf("%s==%s\r\n",$VAR['attributes'][$i][0],$VAR['attributes'][$i][1]);
|
|
|
|
$VAR['charge_attributes'] = $attr;
|
|
}
|
|
|
|
return parent::add($VAR);
|
|
}
|
|
|
|
/**
|
|
* Import records
|
|
*/
|
|
function import($VAR) {
|
|
include_once(PATH_CORE.'import.inc.php');
|
|
$import = new CORE_import;
|
|
|
|
if (! empty($VAR['confirm']))
|
|
$import->do_import($VAR,$this);
|
|
else
|
|
$import->prepare_import($VAR,$this);
|
|
}
|
|
}
|
|
?>
|