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/charge/charge.inc.php

360 lines
9.5 KiB
PHP
Raw Normal View History

<?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
2009-08-03 04:10:16 +00:00
*
* 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
2009-08-03 04:10:16 +00:00
* @author Tony Landis <tony@agileco.com>
* @package AgileBill
2009-08-03 04:10:16 +00:00
* @subpackage Module:Charge
*/
2009-08-03 04:10:16 +00:00
/**
* The main AgileBill Charge Class
*
* @package AgileBill
* @subpackage Module:Charge
*/
class charge extends OSB_module {
2010-11-29 22:41:08 +00:00
private $xmlrpc = false;
2010-11-29 22:41:08 +00:00
public function task_SweepDaily() {
$this->sweep(0);
}
2010-11-29 22:41:08 +00:00
public function task_SweepWeekly() {
$this->sweep(1);
}
2010-11-29 22:41:08 +00:00
public function task_SweepMonthly() {
$this->sweep(2);
}
2010-11-29 22:41:08 +00:00
public function task_SweepQuarterly() {
$this->sweep(3);
}
2010-11-29 22:41:08 +00:00
public function task_SweepSemiAnnually() {
$this->sweep(4);
}
2010-11-29 22:41:08 +00:00
public function task_SweepAnnually() {
$this->sweep(5);
2009-08-03 04:10:16 +00:00
}
2010-11-29 22:41:08 +00:00
private function sweep($type) {
global $C_list;
2010-11-29 22:41:08 +00:00
# Load required elements
include_once(PATH_MODULES.'invoice/invoice.inc.php');
include_once(PATH_MODULES.'account/account.inc.php');
2010-11-29 22:41:08 +00:00
$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'));
2010-11-29 22:41:08 +00:00
# No charges
if (! count($charges))
return true;
# Start a transaction
$db = &DB();
2010-11-29 22:41:08 +00:00
if (AGILE_DB_TYPE == 'mysqlt') {
$db->StartTrans();
2010-11-29 22:41:08 +00:00
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);
2010-11-29 22:41:08 +00:00
return false;
}
2010-11-29 22:41:08 +00:00
}
2010-11-29 22:41:08 +00:00
$ao = false;
$io = false;
foreach ($charges as $charge) {
# First Run
if (! $ao) {
$ao = new account($charge['account_id']);
}
2010-11-29 22:41:08 +00:00
# Next run, are we onto a new account?
if ($ao->getRecordAttr('id') != $charge['account_id']) {
$rs = $io->sql_SaveRecord(true);
2010-11-29 22:41:08 +00:00
if (! $rs) {
global $C_debug;
$C_debug->error(__FILE__,__METHOD__,$db->ErrorMsg());
$db->FailTrans();
2010-11-29 22:41:08 +00:00
return false;
2009-08-03 04:10:16 +00:00
}
2010-11-29 22:41:08 +00:00
if (AGILE_DB_TYPE == 'mysqlt')
$db->CompleteTrans();
2010-11-29 22:41:08 +00:00
# Start next invoice
$io = false;
2010-11-29 22:41:08 +00:00
# Load our new account object
$ao = new account($charge['account_id']);
}
2010-11-29 22:41:08 +00:00
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);
2009-08-03 04:10:16 +00:00
}
2010-11-29 22:41:08 +00:00
$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'])));
}
2010-11-29 22:41:08 +00:00
# Save invoice
if ($io) {
$rs = $io->sql_SaveRecord(true);
if (! $rs) {
global $C_debug;
2010-11-29 22:41:08 +00:00
$C_debug->error(__FILE__,__METHOD__,$db->ErrorMsg());
$db->FailTrans();
return false;
}
2010-11-29 22:41:08 +00:00
if (AGILE_DB_TYPE == 'mysqlt')
$db->CompleteTrans();
}
}
2009-08-03 04:10:16 +00:00
function api($VAR) {
$db = &DB();
# amount
if(@$VAR['amount'] <= 0) {
2009-08-03 04:10:16 +00:00
return $this->api_return(0,'','Invalid value supplied for the \'amount\' parameter, must be greater than 0');
} else {
$amount = $VAR['amount'];
}
# sweep_type
2009-08-03 04:10:16 +00:00
if(@$VAR['sweep_type'] <= 6) {
$sweep_type = $VAR['sweep_type'];
} else {
2009-08-03 04:10:16 +00:00
return $this->api_return(0,'','Invalid value supplied for the \'sweep_type\' parameter, must be 0-6');
}
# account_id OR service_id
2009-08-03 04:10:16 +00:00
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 {
2009-08-03 04:10:16 +00:00
# check the account id
2010-11-29 22:41:08 +00:00
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);
2009-08-03 04:10:16 +00:00
$rs = $db->Execute($sql);
if ($rs === false) {
global $C_debug;
2009-08-03 04:10:16 +00:00
$C_debug->error('charge.inc.php','charge :: api()', $db->ErrorMsg(). "\r\n\r\n". $sql);
}
2010-11-29 22:41:08 +00:00
if($rs->RecordCount() == 1) {
$account_id = $rs->fields['id'];
2010-11-29 22:41:08 +00:00
} else {
2009-08-03 04:10:16 +00:00
return $this->api_return(0,'','The \'account_id\' value provided does not exist');
}
2009-08-03 04:10:16 +00:00
# check the service id
2010-11-29 22:41:08 +00:00
} elseif(!empty($VAR['service_id'])) {
2009-08-03 04:10:16 +00:00
$sql = "SELECT id,account_id FROM ".AGILE_DB_PREFIX."service WHERE
site_id = " . $db->qstr(DEFAULT_SITE) . " AND
id = " . $db->qstr($VAR['service_id']);
2009-08-03 04:10:16 +00:00
$rs = $db->Execute($sql);
if ($rs === false) {
global $C_debug;
2009-08-03 04:10:16 +00:00
$C_debug->error('charge.inc.php','charge :: api()', $db->ErrorMsg(). "\r\n\r\n". $sql);
}
2010-11-29 22:41:08 +00:00
if($rs->RecordCount() == 1) {
$service_id = $VAR['service_id'];
$account_id = $rs->fields['account_id'];
} else {
2009-08-03 04:10:16 +00:00
return $this->api_return(0,'','The \'service_id\' value provided does not exist');
}
}
}
# taxable
2009-08-03 04:10:16 +00:00
if(empty($VAR['taxable']))
$taxable = 0;
else
$taxable = $VAR['taxable'];
# attributes
2009-08-03 04:10:16 +00:00
if(!empty($VAR['attributes'])) {
@$attributes = ereg_replace("@@", "\r\n", $VAR['attributes']);
@$attributes = ereg_replace("--", "==", $attributes);
} else {
$attributes = false;
}
# quantity
2009-08-03 04:10:16 +00:00
if(empty($VAR['quantity']))
$quantity = 1;
else
$quantity = $VAR['quantity'];
# product id
if(empty($VAR['product_id'])) {
$product_id = false;
2009-08-03 04:10:16 +00:00
} else {
$product_id = $VAR['product_id'];
}
# description
if(empty($VAR['description'])) {
$description = false;
2009-08-03 04:10:16 +00:00
} else {
$description = $VAR['description'];
}
2009-08-03 04:10:16 +00:00
/* Start: SQL Insert Statement */
$sql = "SELECT * FROM ".AGILE_DB_PREFIX."charge WHERE id = -1";
2009-08-03 04:10:16 +00:00
$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,
2009-08-03 04:10:16 +00:00
'description' => $description );
$sql = $db->GetInsertSQL($rs, $insert);
2009-08-03 04:10:16 +00:00
$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);
2009-08-03 04:10:16 +00:00
return $this->api_return(0,'','The SQL insert failed!');
2010-11-29 22:41:08 +00:00
} else {
return $this->api_return(1,$id,'');
2009-08-03 04:10:16 +00:00
}
return true;
}
2009-08-03 04:10:16 +00:00
function api_return($status=0,$id='',$error='') {
if (! $this->xmlrpc) {
echo "status=={$status}++charge_id={$id}++error=={$error}";
} else {
2009-08-03 04:10:16 +00:00
$arr = array('status'=>$status, 'charge_id'=>$id, 'error'=> $error);
return $arr;
}
}
2009-08-03 04:10:16 +00:00
/**
* Add a record
*/
2010-11-29 22:41:08 +00:00
public function add($VAR) {
2009-08-03 04:10:16 +00:00
if (! empty($VAR['attributes'])) {
$attr = '';
2009-08-03 04:10:16 +00:00
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;
}
2010-11-29 22:41:08 +00:00
return parent::add($VAR);
}
2009-08-03 04:10:16 +00:00
/**
* Import records
*/
function import($VAR) {
include_once(PATH_CORE.'import.inc.php');
$import = new CORE_import;
2009-08-03 04:10:16 +00:00
if (! empty($VAR['confirm']))
$import->do_import($VAR,$this);
else
2009-08-03 04:10:16 +00:00
$import->prepare_import($VAR,$this);
}
}
2009-08-03 04:10:16 +00:00
?>