Example #1
0
 function menu_cc_admin($field, $account, $default, $class, $user = false)
 {
     include_once PATH_MODULES . 'account_billing/account_billing.inc.php';
     $acct_bill = new account_billing();
     echo $acct_bill->menu_admin($field, $account, $default, $class, $user);
 }
Example #2
0
 function sweep($type)
 {
     $this->charge_construct();
     include_once PATH_MODULES . 'account_billing/account_billing.inc.php';
     $account_billing = new account_billing();
     include_once PATH_MODULES . 'tax/tax.inc.php';
     $taxObj = new tax();
     include_once PATH_MODULES . 'discount/discount.inc.php';
     $db =& DB();
     $sql = "SELECT DISTINCT\n\t\t\t\t\t" . AGILE_DB_PREFIX . "charge.id,\n\t\t\t\t\t" . AGILE_DB_PREFIX . "charge.account_id,\n\t\t\t\t\t" . AGILE_DB_PREFIX . "charge.service_id,\n\t\t\t\t\t" . AGILE_DB_PREFIX . "charge.amount,\n\t\t\t\t\t" . AGILE_DB_PREFIX . "charge.taxable,\n\t\t\t\t\t" . AGILE_DB_PREFIX . "charge.attributes,\n\t\t\t\t\t" . AGILE_DB_PREFIX . "charge.quantity,\n\t\t\t\t\t" . AGILE_DB_PREFIX . "charge.product_id,\n\t\t\t\t\t" . AGILE_DB_PREFIX . "charge.description,  \n\t\t\t\t\t" . AGILE_DB_PREFIX . "account.affiliate_id,\n\t\t\t\t\t" . AGILE_DB_PREFIX . "account.reseller_id,\n\t\t\t\t\t" . AGILE_DB_PREFIX . "account.country_id,\n\t\t\t\t\t" . AGILE_DB_PREFIX . "account.currency_id, \n\t\t\t\t\t" . AGILE_DB_PREFIX . "account.state \n\t\t\t\tFROM \n\t\t\t\t\t" . AGILE_DB_PREFIX . "charge  \t\t\n\t\t\t\tLEFT JOIN \n\t\t\t\t\t" . AGILE_DB_PREFIX . "account \t\t\t\n\t\t\t\tON \n\t\t\t\t\t" . AGILE_DB_PREFIX . "account.id \t\t = " . AGILE_DB_PREFIX . "charge.account_id \t\t\t\t\t\t\t\n\t\t\t\tWHERE  \n\t\t\t\t\t" . AGILE_DB_PREFIX . "charge.site_id \t = " . $db->qstr(DEFAULT_SITE) . " \n\t\t\t\tAND \n\t\t\t\t\t" . AGILE_DB_PREFIX . "account.site_id \t = " . $db->qstr(DEFAULT_SITE) . "\t\t\t\n\t\t\t\tAND\n\t\t\t\t\t" . AGILE_DB_PREFIX . "charge.status \t = " . $db->qstr('0') . " \n\t\t\t\tAND\n\t\t\t\t\t" . AGILE_DB_PREFIX . "charge.sweep_type = " . $db->qstr($type) . "\n\t\t\t\tORDER BY\n\t\t\t\t\t" . AGILE_DB_PREFIX . "charge.account_id";
     $rs = $db->Execute($sql);
     if ($rs === false) {
         global $C_debug;
         $C_debug->error('charge.inc.php', 'charge :: sweep()', $db->ErrorMsg() . "\r\n\r\n" . $sql);
         return false;
     }
     $account_id = false;
     $invoice_id = false;
     $i = false;
     $i_total = false;
     $invoice_count = 0;
     $sweep_count = 0;
     while (!$rs->EOF) {
         if ($rs->fields['account_id'] != $account_id) {
             $account_id = $rs->fields['account_id'];
             $i = 0;
             $i_total = $this->count_account_charges($account_id, $rs->CurrentRow(), $rs);
             $sub_total = 0;
             $taxable_amount = 0;
             $this_discount_total = 0;
             $tax_amt = 0;
             $discount_amt = 0;
             # Start a new transaction
             $trans =& DB();
             $trans->StartTrans();
             # Start a new invoice
             $invoice_id = $db->GenID(AGILE_DB_PREFIX . 'invoice_id');
             # check for any discounts for the parent invoice or account_id (applied at checkout and should continue to be applied if recurring type discount)
             $discountObj = new discount();
             # get parent invoice id if service specified (for discount checking)
             $parent_invoice_id = false;
             if ($rs->fields['service_id']) {
                 $parentinv = $db->Execute(sqlSelect($db, "service", "invoice_id", "id={$rs->fields['service_id']}"));
                 if ($parentinv && $parentinv->RecordCount()) {
                     $parent_invoice_id = $parentinv->fields['invoice_id'];
                 }
             }
             # get available discounts to this account/service
             $discountObj->available_discounts($account_id, 1, $parent_invoice_id);
         }
         ###########################
         ##### LINE ITEM ACTIONS ###
         ###########################
         if (!empty($account_id)) {
             ### Get the line item id
             $invoice_item_id = $db->GenID(AGILE_DB_PREFIX . 'invoice_item_id');
             ### Set the invoice item details:
             $product_id = $rs->fields['product_id'];
             if (!empty($product_id) && empty($this->product["{$product_id}"])) {
                 $sql = "SELECT sku FROM " . AGILE_DB_PREFIX . "product WHERE  \n\t\t\t\t\t\t\tid \t= " . $db->qstr($product_id) . " AND\n\t\t\t\t\t\t\tsite_id = " . $db->qstr(DEFAULT_SITE);
                 $prod = $db->Execute($sql);
                 if ($prod->RecordCount() > 0) {
                     $sku = $prod->fields['sku'];
                     $this->product["{$product_id}"] = $sku;
                     $product_attr = '';
                     if (!empty($rs->fields['description'])) {
                         $product_attr = "Description=={$rs->fields['description']}\r\n";
                     }
                     $product_attr .= $rs->fields['attributes'];
                 } else {
                     $sku = $rs->fields['description'];
                     $this->product["{$product_id}"] = $sku;
                     $product_attr = $rs->fields['attributes'];
                 }
             } elseif (!empty($this->product["{$product_id}"])) {
                 $sku = $this->product["{$product_id}"];
                 $product_attr = $rs->fields['attributes'];
             } else {
                 $sku = $rs->fields['description'];
                 $product_attr = $rs->fields['attributes'];
             }
             $quantity = $rs->fields['quantity'];
             $price_base = $rs->fields['amount'];
             $item_total_amt = $price_base * $quantity;
             // Calculate any recurring discounts for this account
             $item_discount_amt = $discountObj->calc_all_discounts(1, $invoice_item_id, $rs->fields['product_id'], $item_total_amt, $account_id, $sub_total + $item_total_amt);
             $item_total_amt -= $item_discount_amt;
             $sub_total += $item_total_amt;
             $discount_amt += $item_discount_amt;
             # calculate any taxes for this item
             $item_tax_amt = 0;
             if ($rs->fields['taxable']) {
                 $item_tax_arr = $taxObj->calculate($item_total_amt, $rs->fields['country_id'], $rs->fields['state']);
                 if (is_array($item_tax_arr)) {
                     foreach ($item_tax_arr as $tx) {
                         $item_tax_amt += $tx['rate'];
                     }
                 }
                 $tax_amt += $item_tax_amt;
             }
             ### Add line item to new invoice
             $sql = "INSERT INTO " . AGILE_DB_PREFIX . "invoice_item SET\n\t\t\t\t\t\tid\t\t\t\t\t    = " . $db->qstr($invoice_item_id) . ",\n\t\t\t\t\t\tsite_id\t\t\t\t    = " . $db->qstr(DEFAULT_SITE) . ",\n\t\t\t\t\t\tinvoice_id\t\t\t    = " . $db->qstr($invoice_id) . ",\n\t\t\t\t\t\taccount_id\t\t\t\t= " . $db->qstr($account_id) . ",\n\t\t\t\t\t\tdate_orig\t\t\t    = " . $db->qstr(time()) . ",\n\t\t\t\t\t\tproduct_id\t\t\t    = " . $db->qstr($product_id) . ",\n\t\t\t\t\t\tsku\t\t\t\t\t    = " . $db->qstr($sku) . ",\n\t\t\t\t\t\tquantity\t\t\t    = " . $db->qstr($quantity) . ",\n\t\t\t\t\t\titem_type\t\t\t    = " . $db->qstr('0') . ",\n\t\t\t\t\t\tproduct_attr\t\t    = " . $db->qstr($product_attr) . ", \n\t\t\t\t\t\tprice_type              = " . $db->qstr('0') . ",\n\t\t\t\t\t\tprice_base\t\t\t    = " . $db->qstr($price_base) . ",\n\t\t\t\t\t\tprice_setup\t\t\t    = " . $db->qstr(0) . ",\n\t\t\t\t\t\ttax_amt\t\t\t\t\t= " . $db->qstr($item_tax_amt) . ",\n\t\t\t\t\t\ttotal_amt\t\t\t\t= " . $db->qstr($item_total_amt) . ",\n\t\t\t\t\t\tdiscount_amt\t\t\t= " . $db->qstr($item_discount_amt);
             $trans->Execute($sql);
             # Insert tax records
             $taxObj->invoice_item($invoice_id, $invoice_item_id, $account_id, @$item_tax_arr);
             # Insert discount records
             $discountObj->invoice_item($invoice_id, $invoice_item_id, $account_id);
             ### Update this charge status to billed
             $sql = "UPDATE " . AGILE_DB_PREFIX . "charge SET\n\t\t\t\t\t\tstatus   \t= " . $db->qstr('1') . " \n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\tsite_id\t    = " . $db->qstr(DEFAULT_SITE) . " AND \n\t\t\t\t\t\tid\t\t    = " . $db->qstr($rs->fields['id']);
             $trans->Execute($sql);
             $i++;
             $sweep_count++;
         }
         #######################
         ### INVOICE ACTIONS ###
         #######################
         if ($i_total == $i || $i == $rs->RecordCount()) {
             if ($invoice_id) {
                 ### Get the most recent billing id for this client:
                 if (!isset($billing_id["{$account_id}"])) {
                     $billing_arr = $account_billing->default_billing($account_id);
                     $billing_id["{$account_id}"] = $billing_arr['billing_id'];
                     $checkout_plugin_id["{$account_id}"] = $billing_arr['checkout_plugin_id'];
                 }
                 ### Affiliate & Reseller info:
                 $affiliate_id = $rs->fields['affiliate_id'];
                 $reseller_id = $rs->fields['reseller_id'];
                 $actual_billed_currency_id = $rs->fields['currency_id'];
                 # calculate any taxes
                 @($total = $sub_total + $tax_amt);
                 if ($total <= 0) {
                     $process_status = 1;
                     $billing_status = 1;
                 } else {
                     $process_status = 0;
                     $billing_status = 0;
                 }
                 ### Generate the invoice insert SQL:
                 $sql = "INSERT INTO " . AGILE_DB_PREFIX . "invoice SET\n\t\t\t\t\t\t\t\tid\t\t\t\t\t\t\t= " . $db->qstr($invoice_id) . ",\n\t\t\t\t\t\t\t\tsite_id\t\t\t\t\t\t= " . $db->qstr(DEFAULT_SITE) . ",\n\t\t\t\t\t\t\t\tdate_orig\t\t\t\t\t= " . $db->qstr(time()) . ",\n\t\t\t\t\t\t\t\tdate_last\t\t\t\t\t= " . $db->qstr(time()) . ",\n\t\t\t\t\t\t\t\tprocess_status\t\t\t\t= " . $db->qstr($process_status) . ",\n\t\t\t\t\t\t\t\tbilling_status\t\t\t\t= " . $db->qstr($billing_status) . ",\n\t\t\t\t\t\t\t\tprint_status\t\t\t\t= " . $db->qstr('0') . ",\n\t\t\t\t\t\t\t\taccount_id\t\t\t\t\t= " . $db->qstr($account_id) . ",\n\t\t\t\t\t\t\t\taccount_billing_id\t\t\t= " . $db->qstr($billing_id["{$account_id}"]) . ",\n\t\t\t\t\t\t\t\taffiliate_id\t\t\t\t= " . $db->qstr($affiliate_id) . ",\n\t\t\t\t\t\t\t\treseller_id\t\t\t\t\t= " . $db->qstr($reseller_id) . ",\n\t\t\t\t\t\t\t\tcheckout_plugin_id\t\t\t= " . $db->qstr($checkout_plugin_id["{$account_id}"]) . ",  \n\t\t\t\t\t\t\t\ttax_amt\t\t\t\t\t\t= " . $db->qstr($tax_amt) . ", \n\t\t\t\t\t\t\t\tdiscount_amt\t\t\t\t= " . $db->qstr($discount_amt) . ",\n\t\t\t\t\t\t\t\tactual_billed_currency_id\t= " . $db->qstr($actual_billed_currency_id) . ",\n\t\t\t\t\t\t\t\tactual_billed_amt\t\t\t= " . $db->qstr('0') . ",\n\t\t\t\t\t\t\t\tbilled_currency_id\t\t\t= " . $db->qstr(DEFAULT_CURRENCY) . ",\n\t\t\t\t\t\t\t\tbilled_amt\t\t\t\t\t= " . $db->qstr('0') . ",\n\t\t\t\t\t\t\t\ttotal_amt\t\t\t\t\t= " . $db->qstr($total) . ",\n\t\t\t\t\t\t\t\tnotice_count\t\t\t\t= " . $db->qstr('0') . ",\n\t\t\t\t\t\t\t\tnotice_max\t\t\t\t\t= " . $db->qstr(MAX_BILLING_NOTICE) . ",\n\t\t\t\t\t\t\t\tnotice_next_date\t\t\t= " . $db->qstr(time()) . ",\n\t\t\t\t\t\t\t\tgrace_period\t\t\t\t= " . $db->qstr(GRACE_PERIOD) . ",\n\t\t\t\t\t\t\t\tdue_date\t\t\t\t\t= " . $db->qstr(time());
                 $trans->Execute($sql);
                 ### Close this transaction
                 $trans->CompleteTrans();
                 $i_total = false;
                 $i = false;
                 $account_id = false;
                 $invoice_id = false;
                 $discount = false;
                 $cookie = false;
                 $invoice_count++;
             }
         }
         $rs->MoveNext();
     }
     global $C_debug;
     $C_debug->alert("Swept {$sweep_count} Charge(s) into {$invoice_count} Invoice(s).");
     return true;
 }