Exemplo n.º 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;
 }
Exemplo n.º 2
0
 function generateInvoices($ids, $account_id, $invoice_id, $charge_installed = false)
 {
     if (empty($ids)) {
         return false;
     }
     # load required elements
     include_once PATH_MODULES . 'service/service.inc.php';
     include_once PATH_MODULES . 'discount/discount.inc.php';
     include_once PATH_MODULES . 'tax/tax.inc.php';
     $taxObj = new tax();
     $serviceObj = new service();
     # start a transaction
     $db =& DB();
     #$db->debug=true;
     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('invoice.inc.php', 'generateInvoices()', "Transactions not supported in 'mysql' driver. Use 'mysqlt' or 'mysqli' driver");
             return false;
         }
     }
     # generate an invoice id
     $invoice = sqlGenID($db, 'invoice');
     # 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();
     $discountObj->available_discounts($account_id, 1, $invoice_id);
     # beginning totals
     $sub_total = 0;
     $taxable_amount = 0;
     $tax_amt = 0;
     $discount_amt = 0;
     # get the full account and service and invoice details
     $p = AGILE_DB_PREFIX;
     $s = DEFAULT_SITE;
     $sql = "SELECT DISTINCT \n\t\tservice.id, service.parent_id, service.invoice_id, service.invoice_item_id, service.account_id, service.account_billing_id, service.product_id,\n\t\tservice.sku, service.active, service.bind, service.type, service.price, service.price_type, service.taxable, service.date_last_invoice, service.date_next_invoice,\n\t\tservice.recur_type, service.recur_schedule, service.recur_weekday, service.recur_week, service.domain_name,\n\t\tservice.domain_tld, service.domain_type, service.domain_term, service.prod_attr, service.prod_attr_cart,\n\t\taccount.currency_id, account.first_name, account.last_name, account.country_id, account.state, account.invoice_grace, account.invoice_advance_gen, account.affiliate_id as account_affiliate_id,\n\t\tinvoice.affiliate_id, invoice.campaign_id, invoice.reseller_id, invoice.checkout_plugin_id, invoice.checkout_plugin_data, invoice.billed_currency_id, invoice.actual_billed_currency_id \n\t\tFROM {$p}service as service \n\t\tJOIN {$p}account as account ON ( service.account_id=account.id and account.site_id={$s} )\n\t\tLEFT JOIN {$p}invoice as invoice ON ( service.invoice_id=invoice.id and invoice.site_id={$s} )\n\t\tWHERE service.id in ({$ids})";
     $service = $db->Execute($sql);
     if ($service === false) {
         global $C_debug;
         $C_debug->error('invoice.inc.php', 'generateInvoices()1', $sql . " \r\n\r\n " . @$db->ErrorMsg());
         $db->FailTrans();
         return false;
     }
     if ($service && $service->RecordCount()) {
         while (!$service->EOF) {
             if (empty($service->fields['billed_currency_id'])) {
                 $service->fields['billed_currency_id'] = DEFAULT_CURRENCY;
             }
             if (empty($service->fields['actual_billed_currency_id'])) {
                 $service->fields['actual_billed_currency_id'] = $service->fields['billed_currency_id'];
             }
             $this->account_id = $service->fields['account_id'];
             $this->parent_id = $service->fields['invoice_id'];
             $this->account_billing_id = $service->fields['account_billing_id'];
             if (!empty($service->fields['account_affiliate_id'])) {
                 $this->affiliate_id = $service->fields['account_affiliate_id'];
             } else {
                 $this->affiliate_id = $service->fields['affiliate_id'];
             }
             $this->campaign_id = $service->fields['campaign_id'];
             $this->reseller_id = $service->fields['reseller_id'];
             $this->checkout_plugin_id = $service->fields['checkout_plugin_id'];
             $this->checkout_plugin_data = $service->fields['checkout_plugin_data'];
             $this->billed_currency_id = $service->fields['billed_currency_id'];
             $this->actual_billed_currency_id = $service->fields['actual_billed_currency_id'];
             $this->invoice_grace = $service->fields['invoice_grace'];
             $item_tax_amt = 0;
             $item_total_amt = 0;
             $item_discount_amt = 0;
             # gen item_id
             $item = sqlGenID($db, "invoice_item");
             # Calculate any recurring discounts for this item
             $item_total_amt = $service->fields['price'];
             $item_discount_amt = $discountObj->calc_all_discounts(1, $item, $service->fields['product_id'], $service->fields['price'], $service->fields['account_id'], $sub_total + $service->fields['price']);
             $item_total_amt -= $item_discount_amt;
             $sub_total += $item_total_amt;
             $discount_amt += $item_discount_amt;
             # calculate any taxes for this item
             if ($service->fields['taxable'] == 1) {
                 $item_tax_amt = 0;
                 $item_tax_arr = $taxObj->calculate($item_total_amt, $service->fields['country_id'], $service->fields['state']);
                 if (is_array($item_tax_arr)) {
                     foreach ($item_tax_arr as $tx) {
                         $item_tax_amt += $tx['rate'];
                     }
                 }
                 $tax_amt += $item_tax_amt;
             }
             # Calculate next invoice date
             $next_invoice = $serviceObj->calcNextInvoiceDate($service->fields['date_next_invoice'], $service->fields['recur_schedule'], $service->fields['recur_type'], $service->fields['recur_weekday']);
             $due_date = $service->fields['date_next_invoice'];
             $recur_schedule = 0;
             if (!empty($service->fields['recur_schedule'])) {
                 $recur_schedule = $service->fields['recur_schedule'];
             }
             // create the invoice item
             $sql = "INSERT INTO {$p}invoice_item SET\n\t\t\t\t\tid={$item},\n\t\t\t\t\tsite_id={$s}, \n\t\t\t\t\tdate_orig=" . time() . ", \n\t\t\t\t\tinvoice_id = {$invoice}, \n\t\t\t\t\taccount_id={$service->fields['account_id']}, \n\t\t\t\t\tservice_id={$service->fields['id']}, \t\n\t\t\t\t\tproduct_id={$service->fields['product_id']}, \n\t\t\t\t\tproduct_attr=" . $db->qstr($service->fields['prod_attr']) . ", \n\t\t\t\t\tproduct_attr_cart=" . $db->qstr($service->fields['prod_attr_cart']) . ", \t\n\t\t\t\t\tsku=" . $db->qstr($service->fields['sku']) . ", \n\t\t\t\t\tquantity=1, \n\t\t\t\t\titem_type=0, \n\t\t\t\t\tprice_type={$service->fields['price_type']}, \n\t\t\t\t\tprice_base={$service->fields['price']}, \n\t\t\t\t\tprice_setup=0, \n\t\t\t\t\trecurring_schedule={$recur_schedule}, \n\t\t\t\t\tdate_start={$service->fields['date_next_invoice']}, \n\t\t\t\t\tdate_stop={$next_invoice}, \n\t\t\t\t\tdomain_name=" . $db->qstr($service->fields['domain_name']) . ", \n\t\t\t\t\tdomain_tld=" . $db->qstr($service->fields['domain_tld']) . ", \n\t\t\t\t\tdomain_type=" . $db->qstr($service->fields['domain_type']) . ", \n\t\t\t\t\ttax_amt={$tax_amt}, \n\t\t\t\t\ttotal_amt={$item_total_amt}, \n\t\t\t\t\tdiscount_amt={$item_discount_amt}";
             $itemrs = $db->Execute($sql);
             if ($itemrs === false) {
                 global $C_debug;
                 $C_debug->error('invoice.inc.php', 'generateInvoices()2', $sql . " \r\n\r\n " . @$db->ErrorMsg());
                 $db->FailTrans();
                 return false;
             }
             // Insert tax records
             $taxObj->invoice_item($invoice, $item, $service->fields['account_id'], @$item_tax_arr);
             # Insert discount records
             $discountObj->invoice_item($invoice, $item, $service->fields['account_id'], @$discountObj->discount_arr);
             // Update the last & next invoice date for this service
             $sql = "UPDATE {$p}service \n\t\t\t\t\tSET \n\t\t\t\t\tdate_last_invoice = {$service->fields['date_next_invoice']}, \n\t\t\t\t\tdate_next_invoice = {$next_invoice} \n\t\t\t\t\tWHERE\n\t\t\t\t\tsite_id={$s} AND id = {$service->fields['id']} ";
             $srvsrs = $db->Execute($sql);
             if ($srvsrs === false) {
                 global $C_debug;
                 $C_debug->error('invoice.inc.php', 'generateInvoices()3', $sql . " \r\n\r\n " . @$db->ErrorMsg());
                 $db->FailTrans();
                 return false;
             }
             // get any charges for this service and create them as invoice items
             if ($charge_installed) {
                 $sql = "SELECT * FROM " . AGILE_DB_PREFIX . "charge WHERE (status=0 or status is null) and site_id=" . DEFAULT_SITE . " AND service_id = " . $service->fields['id'] . " AND date_orig < " . $service->fields['date_next_invoice'];
                 $charge = $db->Execute($sql);
                 if ($charge && $charge->RecordCount()) {
                     while (!$charge->EOF) {
                         $item_tax_amt = 0;
                         $item_total_amt = 0;
                         $item_discount_amt = 0;
                         // Calculate any recurring discounts for this charge item
                         $item_total_amt = $charge->fields['quantity'] * $charge->fields['amount'];
                         $item_discount_amt = $discountObj->calc_all_discounts(1, $item, $charge->fields['product_id'], $item_total_amt, $service->fields['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
                         if ($charge->fields['taxable'] == 1) {
                             $item_tax_amt = 0;
                             $item_tax_arr = $taxObj->calculate($chargeamt, $service->fields['country_id'], $service->fields['state']);
                             if (is_array($item_tax_arr)) {
                                 foreach ($item_tax_arr as $tx) {
                                     $item_tax_amt += $tx['rate'];
                                 }
                             }
                             $tax_amt += $item_tax_amt;
                         }
                         // create the invoice item
                         $charge_item_id = sqlGenID($db, 'invoice_item');
                         $sql = "INSERT INTO {$p}invoice_item SET \n\t\t\t\t\t\t\t\tid\t\t\t= {$charge_item_id},\t\t\n\t\t\t\t\t\t\t\tsite_id\t\t= {$s},\n\t\t\t\t\t\t\t\tcharge_id\t= {$charge->fields['id']},\n\t\t\t\t\t\t\t\tdate_orig \t= " . time() . ",\n\t\t\t\t\t\t\t\tinvoice_id \t= {$invoice}, \n\t\t\t\t\t\t\t\taccount_id \t= " . $this->account_id . ", \n\t\t\t\t\t\t\t\tservice_id \t= " . $db->qstr($service->fields['id']) . ",\n\t\t\t\t\t\t\t\tproduct_id \t= " . $db->qstr($charge->fields['product_id']) . ", \n\t\t\t\t\t\t\t\tproduct_attr= " . $db->qstr($charge->fields['attributes']) . ",  \t\n\t\t\t\t\t\t\t\tsku \t\t= " . $db->qstr($service->fields['sku']) . ", \n\t\t\t\t\t\t\t\tprice_base \t= " . $db->qstr($charge->fields['amount']) . ", \n\t\t\t\t\t\t\t\tquantity \t= " . $charge->fields['quantity'] . ",  \n\t\t\t\t\t\t\t\titem_type \t= 5,\n\t\t\t\t\t\t\t\tprice_type \t= 0,\n\t\t\t\t\t\t\t\tprice_setup = 0,  \n\t\t\t\t\t\t\t\ttax_amt \t= {$item_tax_amt}, \n\t\t\t\t\t\t\t\ttotal_amt \t= {$item_total_amt}, \n\t\t\t\t\t\t\t\tdiscount_amt= {$item_discount_amt}";
                         $itemrs = $db->Execute($sql);
                         if ($itemrs === false) {
                             global $C_debug;
                             $C_debug->error('invoice.inc.php', 'generateInvoices()4', $sql . " \r\n\r\n " . @$db->ErrorMsg());
                             $db->FailTrans();
                             return false;
                         }
                         # Insert tax records
                         $taxObj->invoice_item($invoice, $charge_item_id, $charge->fields['account_id'], @$item_tax_arr);
                         # Insert discount records
                         $discountObj->invoice_item($invoice, $charge_item_id, $charge->fields['account_id'], @$discountObj->discount_arr);
                         # update charge status
                         $chargers = $db->Execute("UPDATE " . AGILE_DB_PREFIX . "charge set status=1 WHERE id={$charge->fields['id']} AND site_id=" . DEFAULT_SITE);
                         if ($chargers === false) {
                             global $C_debug;
                             $C_debug->error('invoice.inc.php', 'generateInvoices()2', $sql . " \r\n\r\n " . @$db->ErrorMsg());
                             $db->FailTrans();
                             return false;
                         }
                         $charge->MoveNext();
                     }
                 }
             }
             $service->MoveNext();
         }
         // add any taxes
         @($total = $sub_total + $tax_amt);
         // get invoice grace period from global/account
         if (!empty($this->invoice_grace)) {
             $grace_period = $this->invoice_grace;
         } else {
             $grace_period = GRACE_PERIOD;
         }
         $sql = "INSERT INTO {$p}invoice SET \n\t\t\t\tid={$invoice},\n\t\t\t\tsite_id={$s},\n\t\t\t\tdate_orig = " . time() . ", \n\t\t\t\tdate_last = " . time() . ",  \n\t\t\t\tnotice_next_date = " . time() . ",\n\t\t\t\ttype = 1, \n\t\t\t\tprocess_status = 0, \n\t\t\t\tbilling_status = 0, \n\t\t\t\tsuspend_billing = 0, \n\t\t\t\tprint_status = 0, \n\t\t\t\trefund_status = 0, \n\t\t\t\tbilled_amt = 0, \n\t\t\t\tactual_billed_amt = 0, \n\t\t\t\tnotice_count = 0, \n\t\t\t\tparent_id = " . $db->qstr($this->parent_id) . ",\n\t\t\t\taccount_id = {$this->account_id},  \n\t\t\t\taccount_billing_id = " . $db->qstr($this->account_billing_id) . ",\n\t\t\t\taffiliate_id = " . $db->qstr($this->affiliate_id) . ",\n\t\t\t\tcampaign_id = " . $db->qstr($this->campaign_id) . ",\n\t\t\t\treseller_id = " . $db->qstr($this->reseller_id) . ",\n\t\t\t\tcheckout_plugin_id = " . $db->qstr($this->checkout_plugin_id) . ",\n\t\t\t\tcheckout_plugin_data = " . $db->qstr($this->checkout_plugin_data) . ",\n\t\t\t\tactual_billed_currency_id = " . $db->qstr($this->actual_billed_currency_id) . ",\n\t\t\t\tbilled_currency_id = " . $db->qstr($this->billed_currency_id) . ",\n\t\t\t\tnotice_max = " . $db->qstr(MAX_BILLING_NOTICE) . ",  \n\t\t\t\tgrace_period = " . $db->qstr($grace_period) . ",\n\t\t\t\ttax_amt = " . $tax_amt . ",  \n\t\t\t\tdiscount_amt = " . $discount_amt . ",  \n\t\t\t\ttotal_amt = " . $total . ",     \n\t\t\t\tdue_date = {$due_date}";
         $invoicers = $db->Execute($sql);
         if ($invoicers === false) {
             global $C_debug;
             $C_debug->error('invoice.inc.php', 'generateInvoices()2', $sql . " \r\n\r\n " . @$db->ErrorMsg());
             $db->FailTrans();
             return false;
         }
     }
     if (AGILE_DB_TYPE == 'mysqlt') {
         $db->CompleteTrans();
     }
 }