/** * Change the quantity of an item */ function changeqty($VAR) { if (empty($this->account_id)) { $this->account_id = SESS_ACCOUNT; } if (empty($this->session_id)) { $this->session_id = SESS; } @($id = $VAR['id']); @($qty = $VAR['qty']); if ($id <= 0) { return; } $db =& DB(); if ($qty == '0') { # Product Plugin Level deletion $cartid =& $db->Execute($sql = sqlSelect($db, "cart", "*", "id=::{$id}:: AND session_id = ::{$this->session_id}::")); if ($cartid) { $product =& $db->Execute($sql = sqlSelect($db, "product", "*", "id=::{$cartid->fields['product_id']}::")); } if (!empty($product->fields['prod_plugin']) && !empty($product->fields['prod_plugin_data'])) { $prodplgfile = PATH_PLUGINS . 'product/' . $product->fields['prod_plugin_file'] . '.php'; if (is_file($prodplgfile)) { include_once PATH_PLUGINS . 'product/' . $product->fields['prod_plugin_file'] . '.php'; eval('$prodplg = new plgn_prov_' . $product->fields['prod_plugin_file'] . ';'); if (is_object($prodplg)) { if (is_callable(array($prodplg, 'delete_cart'))) { $prodplg->delete_cart($VAR, $cartid->fields); } } } } # delete main cart items & subitems: $sql = ''; if (empty($this->admin)) { $sql = "AND session_id = ::{$this->session_id}::"; } $rs =& $db->Execute($sql = sqlDelete($db, "cart", "(id=::{$id}:: OR cart_parent_id=::{$id}:: ) {$sql}")); global $smarty; $smarty->assign('js', false); return false; } # update the quantity: if (!eregi("^[0-9]{1,5}\$", $qty)) { $qty = 1; } if ($qty < 1) { $qty = 1; } if (!$this->admin) { $sql_extra = " AND session_id=::{$this->session_id}::"; } else { $sql_extra = ''; } if ($VAR["type"] == 1) { $fields = array('quantity' => $qty); $db->Execute($sql = sqlUpdate($db, "cart", $fields, "id=::{$id}:: {$sql_extra}")); } else { if ($VAR["type"] == 2) { $fields = array('recurr_schedule' => $VAR["schedule"]); $db->Execute($sql = sqlUpdate($db, "cart", $fields, "id=::{$id}:: {$sql_extra}")); } else { if ($VAR["type"] == 3) { # change domain term $fields = array('domain_term' => $VAR["term"]); $db->Execute($sql = sqlUpdate($db, "cart", $fields, "id=::{$id}:: {$sql_extra}")); } } } # get the product id: $result = $db->Execute(sqlSelect($db, "cart", "*", "id={$id} {$sql_extra}", "cart_type,date_orig")); # get the product details: $product = $db->Execute(sqlSelect($db, "product", "*", "id={$result->fields["product_id"]}")); if ($result->fields["cart_type"] == "2") { # domain name, get pricing include_once PATH_MODULES . 'host_tld/host_tld.inc.php'; $tldObj = new host_tld(); $tldprice = $tldObj->price_tld_arr($result->fields["domain_tld"], $result->fields["host_type"], false, false, false, $this->account_id); $qty = $result->fields["domain_term"]; $base_price = $tldprice[$qty]; $setup_price = 0; } else { if ($result->fields["cart_type"] == "3") { # ad-hoc, get price $base_price = $result->fields["ad_hoc_amount"] * $result->fields["quantity"]; $setup_price = $result->fields["ad_hoc_setup"] * $result->fields["quantity"]; } else { include_once PATH_MODULES . 'product/product.inc.php'; $productObj = new product(); # get pricing for this product: $prod_price = $productObj->price_prod($product->fields, $result->fields["recurr_schedule"], $this->account); $setup_price = $prod_price["setup"] * $result->fields["quantity"]; $base_price = $prod_price["base"] * $result->fields["quantity"]; # get pricing for any attributes: $attr_price = $productObj->price_attr($product->fields, $result->fields["product_attr"], $result->fields["recurr_schedule"], $this->account); $setup_price += $attr_price["setup"] * $result->fields["quantity"]; $base_price += $attr_price["base"] * $result->fields["quantity"]; # get the qty $qty = $result->fields["quantity"]; } } # set the smarty fields: global $smarty; $smarty->assign('qty', $qty); $smarty->assign('base', $base_price); $smarty->assign('setup', $setup_price); $smarty->assign('js', true); return; }
function invoiceItemToService($invoice_item_id, $invoice, $service_id = false) { include_once PATH_MODULES . 'product/product.inc.php'; $product = new product(); $trial = false; $db =& DB(); # Get the invoice_item record $item =& $db->Execute(sqlSelect($db, "invoice_item", "*", "id = {$invoice_item_id}")); # Get the product details $prod =& $db->Execute(sqlSelect($db, "product", "*", "id = {$item->fields['product_id']}")); # Determine Price, Price Type, and Next Invoice Date: if ($item->fields['price_type'] == '2') { ### Item is trial for another item: $trial = true; # Determine trial length. $tl = $prod->fields['price_trial_length_type']; if ($tl == 0) { $this->next_invoice = time() + $prod->fields['price_trial_length'] * 86400; } elseif ($tl == 1) { $this->next_invoice = time() + $prod->fields['price_trial_length'] * 86400 * 7; } elseif ($tl == 2) { $this->next_invoice = mktime(0, 0, 0, date('m') + $prod->fields['price_trial_length'], date('d'), date('Y')); } else { $this->next_invoice = time() + 365 * 86400; } # get the details of the permanent item $q = "SELECT * FROM " . AGILE_DB_PREFIX . "product WHERE\n\t\t \t id \t\t= " . $db->qstr($prod->fields['price_trial_prod']) . " AND\n\t\t \t site_id \t= " . $db->qstr(DEFAULT_SITE); $prod = $db->Execute($q); /* set the product id to the perm item */ $item->fields['product_id'] = $prod->fields['id']; $this->recurring_schedule = $item->fields['recurring_schedule']; ### Get the price $price = $product->price_prod($prod->fields, $prod->fields['price_recurr_default'], $invoice->fields['account_id'], false); $this->price = @$price['base'] / $item->fields['quantity']; $this->bind = '1'; $item->fields['sku'] = $prod->fields['sku']; } elseif ($item->fields['price_type'] == '1') { # Recurring Item $this->recurring_schedule = $item->fields['recurring_schedule']; $this->price = $item->fields['price_base'] / $item->fields['quantity']; $this->bind = '1'; # Determine the next invoice date: $this->next_invoice = $this->calcNextInvoiceDate($invoice->fields['due_date'], $this->recurring_schedule, $prod->fields['price_recurr_type'], $prod->fields['price_recurr_weekday'], $prod->fields['price_recurr_week']); } elseif ($item->fields['price_type'] == '0') { # One-time charge $this->recurring_schedule = ''; $this->next_invoice = ''; $this->price = $item->fields['price_base'] / $item->fields['quantity']; $this->bind = '0'; } else { return false; } # If set-date type recurring transaction, determine full price: if (!$trial && $prod->fields['price_type'] == '1' && $prod->fields['price_recurr_type'] == '1') { # Get the base product price: $price = $product->price_prod($prod->fields, $this->recurring_schedule, $invoice->fields['account_id'], false); $this->price = $price['base'] / $item->fields['quantity']; # Get the price of any attributes: $price = $product->price_attr($prod->fields, $item->fields['product_attr_cart'], $this->recurring_schedule, $invoice->fields['account_id'], false); $this->price += $price['base'] / $item->fields['quantity']; } # Service settings: $this->active = '1'; $this->queue = 'new'; $this->host_ip = ''; $this->host_username = ''; $this->host_password = ''; $this->domain_host_tld_id = ''; $this->domain_host_registrar_id = ''; $this->domain_date_expire = ''; # Parent ID $this->parent_id = $service_id; # determine if groups defined: $groups_defined = false; if (!empty($prod->fields['assoc_grant_group'])) { // type > 0 or num of days defined? if ($prod->fields['assoc_grant_group_type'] > 0 || $prod->fields['assoc_grant_group_days'] > 0) { // actual groups defined? $grant_groups = unserialize($prod->fields['assoc_grant_group']); if (is_array($grant_groups) && count($grant_groups) > 0) { foreach ($grant_groups as $key => $group_id) { if ($group_id > 0) { $groups_defined = true; break; } } } } if (!$groups_defined) { $prod->fields['assoc_grant_group'] = false; $prod->fields['assoc_grant_group_type'] = false; $prod->fields['assoc_grant_group_days'] = false; } } # Determine the Service Type: $this->type = 'none'; if ($item->fields['item_type'] == '0') { # NONE, GROUP, PRODUCT, OR PRODUCT_GROUP: if (!$groups_defined && empty($prod->fields['prod_plugin'])) { $this->type = 'none'; } else { if ($groups_defined && !empty($prod->fields['prod_plugin'])) { $this->type = 'product_group'; } elseif (!empty($prod->fields['prod_plugin'])) { $this->type = 'product'; } elseif ($groups_defined) { $this->type = 'group'; } } } elseif ($item->fields['item_type'] == '1') { # HOSTING: $this->type = 'host'; $this->host_ip = ''; $this->host_username = ''; $this->host_password = ''; # Is group access also defined? if (!empty($prod->fields['assoc_grant_group'])) { $this->type = 'host_group'; } } elseif ($item->fields['item_type'] == '2') { # DOMAIN: $this->type = 'domain'; $this->domain_date_expire = time() + $item->fields['domain_term'] * (86400 * 365); # Get the host_tld_id $q = "SELECT id, registrar_plugin_id FROM " . AGILE_DB_PREFIX . "host_tld WHERE\n\t\t \t name \t\t= " . $db->qstr($item->fields['domain_tld']) . " AND\n\t\t \t site_id \t= " . $db->qstr(DEFAULT_SITE); $tld = $db->Execute($q); $this->domain_host_tld_id = $tld->fields['id']; $this->domain_host_registrar_id = $tld->fields['registrar_plugin_id']; } if ($this->type == "none" && $this->recurring_schedule == "") { # do not create service for one-time charge with no hosting,domain, or group settings } else { # Create the service record(s): for ($iii = 0; $iii < $item->fields['quantity']; $iii++) { $this->id = sqlGenID($db, "service"); $fields = array('date_orig' => time(), 'date_orig' => time(), 'parent_id' => $this->parent_id, 'invoice_id' => $item->fields['invoice_id'], 'invoice_item_id' => $invoice_item_id, 'account_id' => $invoice->fields['account_id'], 'account_billing_id' => $invoice->fields['account_billing_id'], 'product_id' => $item->fields['product_id'], 'sku' => $item->fields['sku'], 'active' => $this->active, 'bind' => $this->bind, 'type' => $this->type, 'queue' => $this->queue, 'price' => $this->price, 'price_type' => $item->fields['price_type'], 'taxable' => $prod->fields['taxable'], 'date_last_invoice' => $invoice->fields['date_orig'], 'date_next_invoice' => $this->next_invoice, 'recur_schedule' => $this->recurring_schedule, 'recur_type' => $prod->fields['price_recurr_type'], 'recur_weekday' => $prod->fields['price_recurr_weekday'], 'recur_week' => $prod->fields['price_recurr_week'], 'recur_schedule_change' => $prod->fields['price_recurr_schedule'], 'recur_cancel' => $prod->fields['price_recurr_cancel'], 'recur_modify' => $prod->fields['price_recurr_modify'], 'group_grant' => $prod->fields['assoc_grant_group'], 'group_type' => $prod->fields['assoc_grant_group_type'], 'group_days' => $prod->fields['assoc_grant_group_days'], 'host_server_id' => $prod->fields['host_server_id'], 'host_provision_plugin_data' => $prod->fields['host_provision_plugin_data'], 'host_ip' => $this->host_ip, 'host_username' => $this->host_username, 'host_password' => $this->host_password, 'domain_name' => $item->fields['domain_name'], 'domain_tld' => $item->fields['domain_tld'], 'domain_term' => $item->fields['domain_term'], 'domain_type' => $item->fields['domain_type'], 'domain_date_expire' => $this->domain_date_expire, 'domain_host_tld_id' => $this->domain_host_tld_id, 'domain_host_registrar_id' => $this->domain_host_registrar_id, 'prod_attr' => $item->fields['product_attr'], 'prod_attr_cart' => $item->fields['product_attr_cart'], 'prod_plugin_name' => @$prod->fields["prod_plugin_file"], 'prod_plugin_data' => @$prod->fields["prod_plugin_data"]); $rs =& $db->Execute(sqlInsert($db, "service", $fields, $this->id)); if ($rs === false) { global $C_debug; $C_debug->error('service.inc.php', 'invoiceItemToService', $q . " | " . @$db->ErrorMsg()); } else { # Run the queue on this item: $arr['id'] = $this->id; $this->queue($arr, $this); } } } # Create any discount codes: if ($prod->fields['discount'] == '1' && !empty($prod->fields['discount_amount'])) { $id = $db->GenID(AGILE_DB_PREFIX . 'discount_id'); $q = "INSERT INTO " . AGILE_DB_PREFIX . "discount SET\n\t \tid\t\t\t\t\t= " . $db->qstr($id) . ",\n\t \tsite_id\t\t\t\t= " . $db->qstr(DEFAULT_SITE) . ",\n\t \tdate_orig\t\t\t= " . $db->qstr(time()) . ",\n\t \tdate_start\t\t\t= " . $db->qstr(time()) . ",\n\t \tstatus\t\t\t\t= " . $db->qstr('1') . ",\n\t \tname\t\t\t\t= " . $db->qstr('DISCOUNT-' . $id) . ",\n\t \tnotes\t\t\t\t= " . $db->qstr('Autogenerated for Invoice Number ' . $item->fields['invoice_id'] . ', SKU ' . $item->fields['sku']) . ",\n\t \tmax_usage_account \t= " . $db->qstr('1') . ",\n\t \tmax_usage_global\t= " . $db->qstr('1') . ",\n\t \tavail_account_id\t= " . $db->qstr($invoice->fields['account_id']) . ",\n\t \tnew_status\t\t\t= " . $db->qstr('1') . ",\n\t \tnew_type\t\t\t= " . $db->qstr('1') . ",\n\t \tnew_rate\t\t\t= " . $db->qstr($prod->fields['discount_amount']) . ",\n\t \trecurr_status\t\t= " . $db->qstr('1') . ",\n\t \trecurr_type\t\t\t= " . $db->qstr('1') . ",\n\t \trecurr_rate\t\t\t= " . $db->qstr($prod->fields['discount_amount']); $db->Execute($q); } return true; }
/** * Add an invoice item * * @param int $id Reference ID for use in Cart or false * @param object $taxObj Object for Tax Calculation * @param object $discountObj Object for Discount Calculation * @param int $item_type 0/1=Product/Service/Hosting 2=Domain 3=Add Hoc * @param string $taxable True, False, or 'validate' to locate the specified $product id and verify * @param int $service_id If this is for a service upgrade, this will be defined * @param int $parent_id Item Parent Id * @param int $product_id Item Product Id * @param array $product_attr Item attributes from the cart/prev service * @param string $product_name Item product name * @param string $sku Item Product SKU * @param int $quantity Item Quantity * @param float $price_base Item Base price * @param float $price_setup Item Setup Price * @param float $discount_manual Ad Hoc Discount Amount * @param int $recurring_schedule Item recurring schedule, 0=week, 1=month, 2=quarter, 3=semi-annual, 4=annual, 5=bi-year * @param int $date_start Date service started * @param int $date_stop Date service stops * @param string $domain_name Domain name * @param string $domain_tld Domain TLD * @param int $domain_term Domain Term * @param string $domain_type Domain Type (register, transfer, renew, park, ns_transfer) */ function addItem($id, &$taxObj, &$discountObj, $item_type, $taxable = false, $service_id = false, $parent_id = false, $product_id = false, $product_attr = false, $product_name = false, $sku = false, $quantity = 1, $price_base = false, $price_setup = false, $discount_manual = false, $recurring_schedule = false, $date_start = false, $date_stop = false, $domain_name = false, $domain_tld = false, $domain_term = false, $domain_type = false) { $tax_amt = 0; $total_amt = 0; $discount_amt = 0; // define correct qty if ($quantity <= 0) { $quantity = 1; } // determine the reference id for this item if ($id > 0) { $this->item_id = $id; } else { $this->item_id++; } // get the product details if ($product_id && $item_type < 2) { $db =& DB(); $product = $db->Execute(sqlSelect($db, "product", "*", "id={$product_id}")); if ($product && $product->RecordCount()) { $taxable = $product->fields['taxable']; $this->product["{$this->item_id}"] = $product->fields; } // get the tld details } elseif ($item_type == 2) { $db =& DB(); $tld = $db->Execute(sqlSelect($db, "host_tld", "*", "name=::{$domain_tld}::")); if ($tld && $tld->RecordCount()) { $taxable = $tld->fields['taxable']; } } // get the product pricing details if product $price_type = 0; if ($price_base === false && $price_setup === false && $product_id && $item_type < 2) { if ($product && $product->RecordCount()) { $price_type = $product->fields['price_type']; $sku = $product->fields['sku']; include_once PATH_MODULES . 'product/product.inc.php'; $productObj = new product(); // get pricing for this product: $prod_price = $productObj->price_prod($product->fields, $recurring_schedule, $this->account_id); $price_base = $prod_price["base"]; $price_setup = $prod_price["setup"]; // get the recurring price (do NOT prorate!) $prod_price = $productObj->price_prod($product->fields, $recurring_schedule, $this->account_id, false); $recur_price = $prod_price["base"]; // calculate any product attributes fees $attr_price = $productObj->price_attr($product->fields, $product_attr, $recurring_schedule, $this->account_id); $price_base += $attr_price["base"]; $price_setup += $attr_price["setup"]; // determine price type for checkout if ($product->fields["price_type"] == '0') { $this->any_new = true; } else { if ($product->fields["price_type"] == '1') { $this->any_recurring = true; } else { if ($product->fields["price_type"] == '2') { $this->any_trial = true; } } } } else { $this->any_new = true; } } else { $this->any_new = true; } // get the TLD pricing details if domain if ($price_base === false && $price_setup === false && $domain_tld && $domain_term && $domain_type) { include_once PATH_MODULES . 'host_tld/host_tld.inc.php'; $tldObj = new host_tld(); $tldprice = $tldObj->price_tld_arr($domain_tld, $domain_type, false, false, false, $this->account_id); if ($domain_type == "park") { $price_base = $tldprice; } else { $price_base = $tldprice["{$domain_term}"]; $this->tld_arr["{$this->item_id}"] = $tldprice; } } // set total amount for this line item before attributes, taxes, or discounts $price_base *= $quantity; $price_setup *= $quantity; $total_amt = $price_setup + $price_base; // set the total recurring amount $recur_price *= $quantity; // format product attributes for storage $product_attr_cart = false; if (($item_type == 0 || $item_type > 2) && is_array($product_attr)) { $product_attr_cart = $this->get_product_attr_cart($product_attr); } // recurring taxes and arrays if ($recur_price > 0 && $price_type == 1) { // increment the total invoice recurring amount $this->recur_amt += $recur_price; // determine taxes for the recurring amount if ($this->tax && $taxable && $recur_price > 0 && $this->account_id) { $recur_tax_arr = $taxObj->calculate($recur_price, $this->country_id, $this->state); if (is_array($recur_tax_arr)) { foreach ($recur_tax_arr as $tx) { $this->recur_amt += $tx['rate']; } } } // get the recurring arrays for price and invoice if ($product && $product->RecordCount()) { $this->price_arr["{$this->item_id}"] = $productObj->price_recurr_arr($product->fields, $this->account_id); $this->recur_arr[] = array('price' => $recur_price, 'recurr_schedule' => $recurring_schedule, 'recurr_type' => $product->fields['price_recurr_type'], 'recurr_weekday' => $product->fields['price_recurr_weekday'], 'recurr_week' => $product->fields['price_recurr_week']); } } // calculate any ad-hoc line item level (admin) discounts if ($this->discount && $discount_manual > 0) { $total_amt -= $discount_manual; $discount_amt += $discount_manual; $this->discount_amt += $discount_amt; $discountObj->add_manual_discount($discount_manual, 'MISC', $this->item_id); } // account level discounts if ($this->discount && $this->account_id) { // calculate any database level discounts for this item (both account specific and session specific) $discount_amt = $discountObj->calc_all_discounts(0, $this->item_id, $product_id, $total_amt, $this->account_id, $this->total_amt + $total_amt); $total_amt -= $discount_amt; $this->discount_amt += $discount_amt; } // add to total discount array if (is_array($discountObj->discount_arr)) { $this->discount_arr["{$this->item_id}"] = $discountObj->discount_arr; } // increment invoice total amount $this->total_amt += $total_amt; // calculate any taxes for current item if ($this->tax && $taxable && $total_amt > 0 && $this->account_id) { $tax_arr = $taxObj->calculate($total_amt, $this->country_id, $this->state); if (is_array($tax_arr)) { foreach ($tax_arr as $tx) { $tax_amt += $tx['rate']; } $this->item_tax["{$this->item_id}"] = $tax_arr; $this->tax_arr["{$this->item_id}"] = $tax_arr; } $this->tax_amt += $tax_amt; $this->total_amt += $tax_amt; } // store the fields to an array $this->invoice_item["{$this->item_id}"] = array('item_type' => $item_type, 'price_type' => $price_type, 'taxable' => $taxable, 'service_id' => $service_id, 'parent_id' => $parent_id, 'product_id' => $product_id, 'product_attr' => $product_attr, 'product_attr_cart' => $product_attr_cart, 'product_name' => $product_name, 'sku' => $sku, 'quantity' => $quantity, 'price_base' => $price_base, 'price_setup' => $price_setup, 'recurring_schedule' => $recurring_schedule, 'date_start' => $date_start, 'date_stop' => $date_stop, 'domain_name' => $domain_name, 'domain_tld' => $domain_tld, 'domain_term' => $domain_term, 'domain_type' => $domain_type, 'total_amt' => $total_amt, 'tax_amt' => $tax_amt, 'discount_amt' => $discount_amt); }