OSB enhancements to date
This commit is contained in:
@@ -29,5 +29,192 @@
|
||||
* @subpackage Module:Invoice
|
||||
*/
|
||||
class invoice_item extends OSB_module {
|
||||
# Hold the Tax information for this object
|
||||
private $tax_arr;
|
||||
# Hold the Discount information for this object
|
||||
private $discount_arr;
|
||||
# The prorate rate for this item
|
||||
private $prorata = 1;
|
||||
# Set the base price - used to work out the recurring amount
|
||||
private $base = 0;
|
||||
|
||||
private function setBase() {
|
||||
if ($this->base && $this->prorata)
|
||||
$this->setRecordAttr('price_base',$this->base*$this->prorata);
|
||||
}
|
||||
|
||||
public function setProRata($amt) {
|
||||
$this->prorata = $amt;
|
||||
|
||||
# Automatically reduce the base rate
|
||||
$this->setBase();
|
||||
}
|
||||
|
||||
public function setBaseRate($amt) {
|
||||
$this->base = $amt;
|
||||
|
||||
# Automatically reduce the base rate
|
||||
$this->setBase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the tax for an item record
|
||||
*
|
||||
* @uses account
|
||||
* @uses tax
|
||||
*/
|
||||
private function pCalcTax() {
|
||||
$total = 0;
|
||||
|
||||
foreach ($this->getTaxArr() as $tax)
|
||||
$total += $tax['rate'];
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
public function getTaxArr() {
|
||||
if (! is_array($this->tax_arr)) {
|
||||
# If we dont have an Account ID we cant calculate tax
|
||||
if (is_null($this->getRecordAttr('account_id')))
|
||||
return false;
|
||||
|
||||
include_once(PATH_MODULES.'tax/tax.inc.php');
|
||||
$to = new tax;
|
||||
|
||||
include_once(PATH_MODULES.'account/account.inc.php');
|
||||
$ao = new account($this->getRecordAttr('account_id'));
|
||||
|
||||
$total = ($this->getRecordAttr('price_base')+$this->getRecordAttr('price_setup'))*$this->getRecordAttr('quantity')-$this->getRecordAttr('discount_amt');
|
||||
$this->tax_arr = $to->calculate($total,$ao->getRecordAttr('country_id'),$ao->getRecordAttr('state'));
|
||||
|
||||
$this->setRecordAttr('tax_amt',$this->pCalcTax());
|
||||
}
|
||||
|
||||
return $this->tax_arr;
|
||||
}
|
||||
|
||||
public function sGetTaxAmt() {
|
||||
return $this->pCalcTax();
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate Discounts applicable for item
|
||||
*
|
||||
* @uses discount
|
||||
*/
|
||||
private function pCalcDiscount($iamt) {
|
||||
$total = 0;
|
||||
|
||||
foreach ($this->getDiscountArr($iamt) as $discount)
|
||||
$total += $discount['amount'];
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
public function getDiscountArr($iamt) {
|
||||
if (! is_array($this->discount_arr)) {
|
||||
include_once(PATH_MODULES.'discount/discount.inc.php');
|
||||
$do = new discount;
|
||||
|
||||
$total = ($this->getRecordAttr('price_base')+$this->getRecordAttr('price_setup'))*$this->getRecordAttr('quantity');
|
||||
$this->discount_arr = $do->calc_all_discounts(1,$this->getRecordAttr('product_id'),$total,$this->getRecordAttr('account_id'),$iamt);
|
||||
|
||||
$this->setRecordAttr('discount_amt',$this->pCalcDiscount($iamt));
|
||||
}
|
||||
|
||||
return $this->discount_arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Discount Total for this Item
|
||||
*/
|
||||
public function sGetDiscountAmt($total=0) {
|
||||
return $this->pCalcDiscount($total);
|
||||
}
|
||||
|
||||
public function sGetSubTotalAmt() {
|
||||
return ($this->getRecordAttr('price_base')+$this->getRecordAttr('price_setup'))*$this->getRecordAttr('quantity');
|
||||
}
|
||||
|
||||
public function sGetTotalAmt($recalc=false) {
|
||||
static $total;
|
||||
|
||||
if ($total && ! $recalc)
|
||||
return $total;
|
||||
else
|
||||
$total = 0;
|
||||
|
||||
# Work out our discount if we havent already
|
||||
if (is_null($this->getRecordAttr('discount_amt')))
|
||||
$this->setRecordAttr('discount_amt',$this->pCalcDiscount($this->sGetSubTotalAmt()));
|
||||
|
||||
# Work out our tax if we havent already
|
||||
if (is_null($this->getRecordAttr('tax_amt')))
|
||||
$this->setRecordAttr('tax_amt',$this->sGetTaxAmt());
|
||||
|
||||
$total = ($this->getRecordAttr('price_base')+$this->getRecordAttr('price_setup'))*$this->getRecordAttr('quantity')
|
||||
-$this->getRecordAttr('discount_amt')
|
||||
+$this->getRecordAttr('tax_amt');
|
||||
|
||||
$this->setRecordAttr('total_amt',$total);
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Work out the recurring amount
|
||||
*
|
||||
* This amount excludes taxes and discounts
|
||||
*
|
||||
* @uses product
|
||||
*/
|
||||
public function sGetRecurAmt() {
|
||||
if ($this->getRecordAttr('price_type') != 1)
|
||||
return 0;
|
||||
|
||||
return $this->base*$this->getRecordAttr('quantity');
|
||||
}
|
||||
|
||||
/**
|
||||
* Save records
|
||||
*
|
||||
* Taxes and discounts should already be set and configured.
|
||||
*
|
||||
* @uses discount
|
||||
* @uses tax
|
||||
*/
|
||||
public function sql_SaveRecord($noconvert=false,$calc=true) {
|
||||
# If our discounts and taxes were calculated already we can skip them
|
||||
if ($calc) {
|
||||
$this->setRecordAttr('discount_amt',$this->pCalcDiscount($this->sGetSubTotalAmt()));
|
||||
$this->setRecordAttr('tax_amt',$this->sGetTaxAmt());
|
||||
$this->setRecordAttr('total_amt',$this->sGetTotalAmt(true));
|
||||
}
|
||||
|
||||
if ($id = parent::sql_SaveRecord($noconvert)) {
|
||||
# Save the Discount Object
|
||||
include_once(PATH_MODULES.'discount/discount.inc.php');
|
||||
$do = new discount;
|
||||
|
||||
include_once(PATH_MODULES.'tax/tax.inc.php');
|
||||
$to = new tax;
|
||||
|
||||
# Save the Tax & Discount Details
|
||||
if ($this->discount_arr)
|
||||
$do->invoice_item($this->getRecordAttr('invoice_id'),$id,$this->getRecordAttr('account_id'),$this->discount_arr);
|
||||
|
||||
if ($this->tax_arr)
|
||||
$to->invoice_item($this->getRecordAttr('invoice_id'),$id,$this->getRecordAttr('account_id'),$this->tax_arr);
|
||||
}
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the items on an invoice
|
||||
*/
|
||||
public function sInvoiceItems($id) {
|
||||
return $this->sql_GetRecords(array('where'=>array('invoice_id'=>$id)));
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
@@ -38,16 +38,22 @@
|
||||
</site_id>
|
||||
<date_orig>
|
||||
<type>I8</type>
|
||||
</date_orig>
|
||||
</date_orig>
|
||||
<!-- DELETED
|
||||
<parent_id>
|
||||
<type>I8</type>
|
||||
</parent_id>
|
||||
-->
|
||||
<!-- DELETED
|
||||
<account_id>
|
||||
<type>I8</type>
|
||||
</account_id>
|
||||
-->
|
||||
<!-- If service_id is NULL, it is either a 1 time charge, or a new service not yet provisioned, details should be in product_attr_cart -->
|
||||
<service_id>
|
||||
<type>I8</type>
|
||||
</service_id>
|
||||
<!-- @todo add NOT NULL -->
|
||||
<invoice_id>
|
||||
<type>I8</type>
|
||||
</invoice_id>
|
||||
@@ -57,19 +63,22 @@
|
||||
<charge_id>
|
||||
<type>I8</type>
|
||||
</charge_id>
|
||||
<!-- DELETED
|
||||
<sku>
|
||||
<type>C(128)</type>
|
||||
</sku>
|
||||
-->
|
||||
<quantity>
|
||||
<type>I4</type>
|
||||
<type>F</type>
|
||||
</quantity>
|
||||
<!-- 0=recurring, 1=prod/service/hosting, 2=domain, 3=addhoc, 5=charge module -->
|
||||
<item_type>
|
||||
<type>L</type>
|
||||
</item_type>
|
||||
<product_attr>
|
||||
<type>X2</type>
|
||||
<convert>array</convert>
|
||||
<html>1</html>
|
||||
<type>X2</type>
|
||||
</product_attr>
|
||||
<product_attr_cart>
|
||||
<type>X2</type>
|
||||
@@ -86,7 +95,7 @@
|
||||
<discount_amt>
|
||||
<type>F</type>
|
||||
</discount_amt>
|
||||
<!-- 0=new, 1=re-occuring, 2=trial, 999=setup/discount -->
|
||||
<!-- 0=new, 1=re-occuring, 2=trial, 3=charges, 999=setup/discount // @TODO need to merge this with item_type - seems redundant? -->
|
||||
<price_type>
|
||||
<type>L</type>
|
||||
</price_type>
|
||||
@@ -121,8 +130,8 @@
|
||||
</field>
|
||||
<!-- define all the methods for this class, and the fields they have access to, if applicable. -->
|
||||
<method>
|
||||
<add>id,site_id,date_orig,invoice_id,product_id,sku,quantity,item_type,product_attr,product_attr_cart,price_base,price_setup,recurring_schedule,domain_name,domain_tld,domain_term,domain_type,invoice_item_parent_id</add>
|
||||
<update>id,site_id,date_orig,invoice_id,product_id,sku,quantity,item_type,product_attr,product_attr_cart,price_base,price_setup,recurring_schedule,domain_name,domain_tld,domain_term,domain_type,invoice_item_parent_id</update>
|
||||
<add>account_id,invoice_id,product_id,product_name,service_id,charge_id,quantity,item_type,product_attr,product_attr_cart,price_type,price_base,price_setup,total_amt,discount_amt,tax_amt,recurring_schedule,domain_name,domain_tld,domain_term,domain_type,date_start,date_stop</add>
|
||||
<update>date_orig,invoice_id,product_id,sku,quantity,item_type,product_attr,product_attr_cart,price_base,price_setup,recurring_schedule,domain_name,domain_tld,domain_term,domain_type,invoice_item_parent_id</update>
|
||||
<delete>id,site_id,date_orig,invoice_id,product_id,sku,quantity,item_type,product_attr,product_attr_cart,price_base,price_setup,recurring_schedule,domain_name,domain_tld,domain_term,domain_type,invoice_item_parent_id</delete>
|
||||
<view>id,site_id,date_orig,invoice_id,product_id,sku,quantity,item_type,product_attr,product_attr_cart,price_base,price_setup,recurring_schedule,domain_name,domain_tld,domain_term,domain_type,invoice_item_parent_id</view>
|
||||
<search>id,site_id,date_orig,invoice_id,product_id,sku,quantity,item_type,product_attr,product_attr_cart,price_base,price_setup,recurring_schedule,domain_name,domain_tld,domain_term,domain_type,invoice_item_parent_id</search>
|
||||
|
Reference in New Issue
Block a user