Example #1
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;
 }
Example #2
0
 /** Invoice expiring domains p2 
  */
 function generatedomaininvoice($VAR)
 {
     include_once PATH_MODULES . 'tax/tax.inc.php';
     $taxObj = new tax();
     $db =& DB();
     if (is_array($VAR)) {
         $expire = time();
         $rs = $db->Execute(sqlSelect($db, 'service', '*', " id = ::{$VAR['id']}:: AND active=1\n\t        \t\t\t\tAND type = 'domain' AND queue = 'none' AND\n\t        \t\t\t\t( domain_type = 'register' OR domain_type = 'transfer' OR domain_type = 'renew'  ) AND\n\t        \t\t\t\t( suspend_billing = 0 OR suspend_billing IS NULL ) "));
         $service = $rs->fields;
     } else {
         $service = $VAR;
     }
     if (empty($service['id'])) {
         global $C_debug;
         $C_debug->alert("Unable to generate domain renweal invoice due to domain status.");
         return false;
     }
     # Get the parent invoice details:
     if (!empty($service['invoice_id'])) {
         $rs = $db->Execute(sqlSelect($db, 'invoice', '*', " id = {$service['invoice_id']} ", ""));
         $invoice = $rs->fields;
     } else {
         $invoice = false;
     }
     # Get the account details:
     $rs = $db->Execute(sqlSelect($db, 'account', '*', " id = {$service['account_id']} ", ""));
     $account = $rs->fields;
     # Get the account price
     include_once PATH_MODULES . 'host_tld/host_tld.inc.php';
     $tldObj = new host_tld();
     $tld_arr = $tldObj->price_tld_arr($service['domain_tld'], 'renew', false, false, false, $service['account_id']);
     foreach ($tld_arr as $term => $price) {
         break;
     }
     # Calculate taxes:
     $rs = $db->Execute($sql = sqlSelect($db, "host_tld", "taxable", "name = ::{$service['domain_tld']}::"));
     if ($service['taxable'] || @$rs->fields['taxable']) {
         $tax_arr = $taxObj->calculate($price, $account["country_id"], $account["state"]);
     } else {
         $tax_arr = false;
     }
     $total = $price;
     $tax_amt = 0;
     if (is_array($tax_arr)) {
         foreach ($tax_arr as $tx) {
             $tax_amt += $tx['rate'];
         }
         $total += $tax_amt;
     }
     # calculate the dates
     $expire = $service['domain_date_expire'] + $term * 86400;
     $due_date = $service['domain_date_expire'] - 86400 * 3;
     # Create the invoice
     $id = sqlGenID($db, "invoice");
     $insert = $db->Execute($sql = sqlInsert($db, "invoice", array('date_orig' => time(), 'date_last' => time(), 'type' => 2, 'process_status' => 0, 'billing_status' => 0, 'suspend_billing' => 0, 'print_status' => 0, 'parent_id' => $service['invoice_id'], 'account_id' => $service['account_id'], 'account_billing_id' => $service['account_billing_id'], 'affiliate_id' => @$invoice['affiliate_id'], 'campaign_id' => @$invoice['campaign_id'], 'reseller_id' => @$invoice['reseller_id'], 'checkout_plugin_id' => @$invoice['checkout_plugin_id'], 'tax_amt' => $tax_amt, 'discount_arr' => serialize(@$discount_arr), 'discount_amt' => @$discount_amt, 'total_amt' => $total, 'billed_amt' => 0, 'billed_currency_id' => DEFAULT_CURRENCY, 'actual_billed_amt' => 0, 'actual_billed_currency_id' => @$invoice['actual_billed_currency_id'], 'notice_count' => 0, 'notice_next_date' => time(), 'notice_max' => MAX_BILLING_NOTICE, 'grace_period' => 0, 'due_date' => $due_date), $id));
     # create the invoice item:
     if ($insert) {
         $db->Execute($idx = sqlInsert($db, "invoice_item", array('date_orig' => time(), 'invoice_id' => $id, 'account_id' => $service['account_id'], 'service_id' => $service['id'], 'sku' => 'DOMAIN-RENEW', 'quantity' => 1, 'item_type' => 2, 'price_type' => 0, 'price_base' => $price, 'price_setup' => 0, 'domain_type' => 'renew', 'date_start' => $service['domain_date_expire'], 'date_stop' => $expire, 'domain_name' => $service['domain_name'], 'domain_tld' => $service['domain_tld'], 'domain_term' => $term, 'tax_amt' => $tax_amt, 'total_amt' => $price)));
         # Insert tax records
         $taxObj->invoice_item($id, $idx, $service['account_id'], @$item_tax_arr);
         # Update the service record
         $fields = array('active' => 0);
         $db->Execute(sqlUpdate($db, "service", $fields, "id = {$service['id']}"));
         global $C_debug;
         $C_debug->alert("Generated domain renewal invoice for {$service['domain_name']}.{$service['domain_tld']}");
         return $id;
     }
 }