Example #1
0
function SumUpConfigOptionsOrder($id, $configoptions, $promocode, $paymentmethod = "", $checkout = "")
{
    global $CONFIG;
    global $_LANG;
    global $upgradeslist;
    global $orderamount;
    global $orderdescription;
    global $applytax;
    $_SESSION['upgradeids'] = array();
    $result = select_query("tblhosting", "packageid,domain,nextduedate,billingcycle", array("userid" => $_SESSION['uid'], "id" => $id));
    $data = mysql_fetch_array($result);
    $packageid = $data['packageid'];
    $domain = $data['domain'];
    $nextduedate = $data['nextduedate'];
    $billingcycle = $data['billingcycle'];
    $result = select_query("tblproducts", "name,tax", array("id" => $packageid));
    $data = mysql_fetch_array($result);
    $productname = $data['name'];
    $applytax = $data['tax'];
    if ($domain) {
        $productname .= " - " . $domain;
    }
    $year = substr($nextduedate, 0, 4);
    $month = substr($nextduedate, 5, 2);
    $day = substr($nextduedate, 8, 2);
    $cyclemonths = getBillingCycleMonths($billingcycle);
    $prevduedate = date("Y-m-d", mktime(0, 0, 0, $month - $cyclemonths, $day, $year));
    $totaldays = round((strtotime($nextduedate) - strtotime($prevduedate)) / 86400);
    $todaysdate = date("Ymd");
    $todaysdate = strtotime($todaysdate);
    $nextduedatetime = strtotime($nextduedate);
    $days = round(($nextduedatetime - $todaysdate) / 86400);
    if ($days < 0) {
        $days = $totaldays;
    }
    $percentage = $days / $totaldays;
    $discount = 0;
    $promoqualifies = true;
    if ($promocode) {
        $promodata = validateUpgradePromo($promocode);
        if (is_array($promodata)) {
            $appliesto = $promodata['appliesto'];
            $cycles = $promodata['cycles'];
            $promotype = $promodata['type'];
            $promovalue = $promodata['value'];
            $discounttype = $promodata['discounttype'];
            $upgradeconfigoptions = $promodata['configoptions'];
            $promodesc = $promodata['desc'];
            if ($promotype != "configoptions") {
                $promoqualifies = false;
            }
            if (count($appliesto) && $appliesto[0] && !in_array($packageid, $appliesto)) {
                $promoqualifies = false;
            }
            if (count($cycles) && $cycles[0] && !in_array($billingcycle, $cycles)) {
                $promoqualifies = false;
            }
            if ($discounttype == "Percentage") {
                $promovalue = $promovalue / 100;
            }
        }
        if ($promovalue == 0) {
            $promodata = get_query_vals("tblpromotions", "upgrades, upgradeconfig, type,value", array("lifetimepromo" => 1, "recurring" => 1, "code" => $promocode));
            if (is_array($promodata)) {
                if ($promodata['upgrades'] == 1) {
                    $upgradeconfig = unserialize($promodata['upgradeconfig']);
                    if ($upgradeconfig['type'] != "configoptions") {
                        $promoqualifies = false;
                    }
                    $promovalue = $upgradeconfig['value'];
                    $discounttype = $upgradeconfig['discounttype'];
                    if ($discounttype == "Percentage") {
                        $promovalue = $promovalue / 100;
                    }
                    $promoqualifies = true;
                } else {
                    $promoqualifies = false;
                }
            }
        }
    }
    $configoptions = getCartConfigOptions($packageid, $configoptions, $billingcycle);
    $oldconfigoptions = getCartConfigOptions($packageid, "", $billingcycle, $id);
    $subtotal = 0;
    foreach ($configoptions as $key => $configoption) {
        $configid = $configoption['id'];
        $configname = $configoption['optionname'];
        $optiontype = $configoption['optiontype'];
        $new_selectedvalue = $configoption['selectedvalue'];
        $new_selectedqty = $configoption['selectedqty'];
        $new_selectedname = $configoption['selectedname'];
        $new_selectedsetup = $configoption['selectedsetup'];
        $new_selectedrecurring = $configoption['selectedrecurring'];
        $old_selectedvalue = $oldconfigoptions[$key]['selectedvalue'];
        $old_selectedqty = $oldconfigoptions[$key]['selectedqty'];
        $old_selectedname = $oldconfigoptions[$key]['selectedname'];
        $old_selectedsetup = $oldconfigoptions[$key]['selectedsetup'];
        $old_selectedrecurring = $oldconfigoptions[$key]['selectedrecurring'];
        if (($optiontype == 1 || $optiontype == 2) && $new_selectedvalue != $old_selectedvalue || ($optiontype == 3 || $optiontype == 4) && $new_selectedqty != $old_selectedqty) {
            $difference = $new_selectedrecurring - $old_selectedrecurring;
            $amountdue = $difference * $percentage;
            $amountdue = format_as_currency($amountdue);
            if (!$CONFIG['CreditOnDowngrade'] && $amountdue < 0) {
                $amountdue = format_as_currency(0);
            }
            if ($optiontype == 1 || $optiontype == 2) {
                $db_orig_value = $old_selectedvalue;
                $db_new_value = $new_selectedvalue;
                $originalvalue = $old_selectedname;
                $newvalue = $new_selectedname;
            } else {
                if ($optiontype == 3) {
                    $db_orig_value = $old_selectedqty;
                    $db_new_value = $new_selectedqty;
                    if ($old_selectedqty) {
                        $originalvalue = $_LANG['yes'];
                        $newvalue = $_LANG['no'];
                    } else {
                        $originalvalue = $_LANG['no'];
                        $newvalue = $_LANG['yes'];
                    }
                } else {
                    if ($optiontype == 4) {
                        $new_selectedqty = (int) $new_selectedqty;
                        if ($new_selectedqty < 0) {
                            $new_selectedqty = 0;
                        }
                        $db_orig_value = $old_selectedqty;
                        $db_new_value = $new_selectedqty;
                        $originalvalue = $old_selectedqty;
                        $newvalue = $new_selectedqty . " x " . $configoption['options'][0]['nameonly'];
                    }
                }
            }
            $subtotal += $amountdue;
            $itemdiscount = 0;
            if ($promoqualifies && 0 < $amountdue && (!count($upgradeconfigoptions) || in_array($configid, $upgradeconfigoptions))) {
                $itemdiscount = $discounttype == "Percentage" ? round($amountdue * $promovalue, 2) : ($amountdue < $promovalue ? $amountdue : $promovalue);
            }
            $discount += $itemdiscount;
            $upgradearray[] = array("configname" => $configname, "originalvalue" => $originalvalue, "newvalue" => $newvalue, "price" => formatCurrency($amountdue));
            if ($checkout) {
                if ($orderdescription) {
                    $orderdescription .= "<br>\r\n<br>\r\n";
                }
                $orderdescription .= $_LANG['upgradedowngradeconfigoptions'] . (": " . $configname . " - " . $originalvalue . " => " . $newvalue . "<br>\r\nAmount Due: ") . formatCurrency($amountdue);
                $paid = "N";
                if ($amountdue <= 0) {
                    $paid = "Y";
                }
                $amountwithdiscount = $amountdue - $itemdiscount;
                $upgradeid = insert_query("tblupgrades", array("type" => "configoptions", "date" => "now()", "relid" => $id, "originalvalue" => "" . $configid . "=>" . $db_orig_value, "newvalue" => $db_new_value, "amount" => $amountwithdiscount, "recurringchange" => $difference, "status" => "Pending", "paid" => $paid));
                $_SESSION['upgradeids'][] = $upgradeid;
                if (0 < $amountdue) {
                    insert_query("tblinvoiceitems", array("userid" => $_SESSION['uid'], "type" => "Upgrade", "relid" => $upgradeid, "description" => $_LANG['upgradedowngradeconfigoptions'] . (": " . $productname . "\r\n" . $configname . ": " . $originalvalue . " => " . $newvalue . " (") . getTodaysDate() . " - " . fromMySQLDate($nextduedate) . ")", "amount" => $amountdue, "taxed" => $applytax, "duedate" => "now()", "paymentmethod" => $paymentmethod));
                    if (0 < $itemdiscount) {
                        insert_query("tblinvoiceitems", array("userid" => $_SESSION['uid'], "description" => $_LANG['orderpromotioncode'] . ": " . $promocode . " - " . $promodesc, "amount" => $itemdiscount * (0 - 1), "taxed" => $applytax, "duedate" => "now()", "paymentmethod" => $paymentmethod));
                    }
                    $orderamount += $amountwithdiscount;
                    continue;
                }
                if ($CONFIG['CreditOnDowngrade']) {
                    $creditamount = $amountdue * (0 - 1);
                    insert_query("tblcredit", array("clientid" => $_SESSION['uid'], "date" => "now()", "description" => "Upgrade/Downgrade Credit", "amount" => $creditamount));
                    update_query("tblclients", array("credit" => "+=" . $creditamount), array("id" => (int) $_SESSION['uid']));
                }
                doUpgrade($upgradeid);
                continue;
            }
            continue;
        }
    }
    if (!count($upgradearray)) {
        redir("type=configoptions&id=" . (int) $id, "upgrade.php");
    }
    $GLOBALS['subtotal'] = $subtotal;
    $GLOBALS['qualifies'] = $promoqualifies;
    $GLOBALS['discount'] = $discount;
    return $upgradearray;
}
Example #2
0
function getProrataValues($billingcycle, $amount, $proratadate, $proratachargenextmonth, $day, $month, $year, $userid)
{
    global $CONFIG;
    if ($CONFIG['ProrataClientsAnniversaryDate']) {
        $result = select_query("tblclients", "datecreated", array("id" => $userid));
        $data = mysql_fetch_array($result);
        $clientregdate = $data[0];
        $clientregdate = explode("-", $clientregdate);
        $proratadate = $clientregdate[2];
        if ($proratadate <= 0) {
            $proratadate = date("d");
        }
    }
    $billingcycle = str_replace("-", "", strtolower($billingcycle));
    $proratamonths = getBillingCycleMonths($billingcycle);
    if ($billingcycle != "monthly") {
        $proratachargenextmonth = 0;
    }
    if ($billingcycle == "monthly") {
        if ($day < $proratadate) {
            $proratamonth = $month;
        } else {
            $proratamonth = $month + 1;
        }
    } else {
        $proratamonth = $month + $proratamonths;
    }
    $proratadateuntil = date("Y-m-d", mktime(0, 0, 0, $proratamonth, $proratadate, $year));
    $proratainvoicedate = date("Y-m-d", mktime(0, 0, 0, $proratamonth, $proratadate - 1, $year));
    $monthnumdays = array("31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31");
    if ($year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0) {
        $monthnumdays[1] = 29;
    }
    $totaldays = $extraamount = 0;
    if ($billingcycle == "monthly") {
        if ($proratachargenextmonth < $proratadate && $day < $proratadate && $proratachargenextmonth <= $day || $proratadate <= $proratachargenextmonth && $proratadate <= $day && $proratachargenextmonth <= $day) {
            ++$proratamonth;
            $extraamount = $amount;
        }
        $totaldays += $monthnumdays[$month - 1];
        $days = ceil((strtotime($proratadateuntil) - strtotime("" . $year . "-" . $month . "-" . $day)) / (60 * 60 * 24));
        $proratadateuntil = date("Y-m-d", mktime(0, 0, 0, $proratamonth, $proratadate, $year));
        $proratainvoicedate = date("Y-m-d", mktime(0, 0, 0, $proratamonth, $proratadate - 1, $year));
    } else {
        $counter = $month;
        while ($counter <= $month + ($proratamonths - 1)) {
            $month2 = round($counter);
            if (12 < $month2) {
                $month2 = $month2 - 12;
            }
            if (12 < $month2) {
                $month2 = $month2 - 12;
            }
            if (12 < $month2) {
                $month2 = $month2 - 12;
            }
            $totaldays += $monthnumdays[$month2 - 1];
            ++$counter;
        }
        $days = ceil((strtotime($proratadateuntil) - strtotime("" . $year . "-" . $month . "-" . $day)) / (60 * 60 * 24));
    }
    $prorataamount = round($amount * ($days / $totaldays), 2) + $extraamount;
    $days = ceil((strtotime($proratadateuntil) - strtotime("" . $year . "-" . $month . "-" . $day)) / (60 * 60 * 24));
    return array("amount" => $prorataamount, "date" => $proratadateuntil, "invoicedate" => $proratainvoicedate, "days" => $days);
}
/**
 *
 * @ WHMCS FULL DECODED & NULLED
 *
 * @ Version  : 5.2.15
 * @ Author   : MTIMER
 * @ Release on : 2013-12-24
 * @ Website  : http://www.mtimer.cn
 *
 **/
function getCartConfigOptions($pid, $values, $cycle, $accountid = "", $orderform = "")
{
    global $CONFIG;
    global $_LANG;
    global $currency;
    if ($cycle == "onetime") {
        $cycle = "monthly";
    }
    $configoptions = array();
    $cycle = strtolower(str_replace("-", "", $cycle));
    if ($cycle == "one time") {
        $cycle = "monthly";
    }
    $showhidden = $_SESSION['adminid'] && defined("ADMINAREA") ? true : false;
    if (!function_exists("getBillingCycleMonths")) {
        require ROOTDIR . "/includes/invoicefunctions.php";
    }
    $cyclemonths = getBillingCycleMonths($cycle);
    if ($accountid) {
        $values = array();
        $result = select_query("tblhostingconfigoptions", "", array("relid" => $accountid));
        while ($data = mysql_fetch_array($result)) {
            $configid = $data['configid'];
            $result2 = select_query("tblproductconfigoptions", "", array("id" => $configid));
            $data2 = mysql_fetch_array($result2);
            $optiontype = $data2['optiontype'];
            if ($optiontype == 3 || $optiontype == 4) {
                $configoptionvalue = $data['qty'];
            } else {
                $configoptionvalue = $data['optionid'];
            }
            $values[$configid] = $configoptionvalue;
        }
    }
    $where = array("pid" => $pid);
    if (!$showhidden) {
        $where['hidden'] = 0;
    }
    $result2 = select_query("tblproductconfigoptions", "", $where, "order` ASC,`id", "ASC", "", "tblproductconfiglinks ON tblproductconfiglinks.gid=tblproductconfigoptions.gid");
    while ($data2 = mysql_fetch_array($result2)) {
        $optionid = $data2['id'];
        $optionname = $data2['optionname'];
        $optiontype = $data2['optiontype'];
        $optionhidden = $data2['hidden'];
        $qtyminimum = $data2['qtyminimum'];
        $qtymaximum = $data2['qtymaximum'];
        if (strpos($optionname, "|")) {
            $optionname = explode("|", $optionname);
            $optionname = trim($optionname[1]);
        }
        $options = array();
        $selectedqty = 0;
        $selvalue = $values[$optionid];
        if ($optiontype == "3") {
            $result3 = select_query("tblproductconfigoptionssub", "", array("configid" => $optionid));
            $data3 = mysql_fetch_array($result3);
            $opid = $data3['id'];
            $ophidden = $data3['hidden'];
            $opname = $data3['optionname'];
            if (strpos($opname, "|")) {
                $opname = explode("|", $opname);
                $opname = trim($opname[1]);
            }
            $opnameonly = $opname;
            $result4 = select_query("tblpricing", "", array("type" => "configoptions", "currency" => $currency['id'], "relid" => $opid));
            $data = mysql_fetch_array($result4);
            $setup = $data[substr($cycle, 0, 1) . "setupfee"];
            $price = $fullprice = $data[$cycle];
            if ($orderform && $CONFIG['ProductMonthlyPricingBreakdown']) {
                $price = $price / $cyclemonths;
            }
            if (0 < $price) {
                $opname .= " " . formatCurrency($price);
            }
            $setupvalue = 0 < $setup ? " + " . formatCurrency($setup) . " " . $_LANG['ordersetupfee'] : "";
            $options[] = array("id" => $opid, "hidden" => $ophidden, "name" => $opname . $setupvalue, "nameonly" => $opnameonly, "recurring" => $price);
            if (!$selvalue) {
                $selvalue = 0;
            }
            $selectedoption = $selsetup = $selrecurring = "";
            $selectedqty = $selvalue;
            $selvalue = $opid;
            $selname = $_LANG['no'];
            if ($selectedqty) {
                $selname = $_LANG['yes'];
                $selectedoption = $opname;
                $selsetup = $setup;
                $selrecurring = $fullprice;
            }
        } else {
            if ($optiontype == "4") {
                $result3 = select_query("tblproductconfigoptionssub", "", array("configid" => $optionid));
                $data3 = mysql_fetch_array($result3);
                $opid = $data3['id'];
                $ophidden = $data3['hidden'];
                $opname = $data3['optionname'];
                if (strpos($opname, "|")) {
                    $opname = explode("|", $opname);
                    $opname = trim($opname[1]);
                }
                $opnameonly = $opname;
                $result4 = select_query("tblpricing", "", array("type" => "configoptions", "currency" => $currency['id'], "relid" => $opid));
                $data = mysql_fetch_array($result4);
                $setup = $data[substr($cycle, 0, 1) . "setupfee"];
                $price = $fullprice = $data[$cycle];
                if ($orderform && $CONFIG['ProductMonthlyPricingBreakdown']) {
                    $price = $price / $cyclemonths;
                }
                if (0 < $price) {
                    $opname .= " " . formatCurrency($price);
                }
                $setupvalue = 0 < $setup ? " + " . formatCurrency($setup) . " " . $_LANG['ordersetupfee'] : "";
                $options[] = array("id" => $opid, "hidden" => $ophidden, "name" => $opname . $setupvalue, "nameonly" => $opnameonly, "recurring" => $price);
                if (!is_numeric($selvalue) || $selvalue < 0) {
                    $selvalue = $qtyminimum;
                }
                $selectedqty = $selvalue;
                $selvalue = $opid;
                $selname = $selectedqty;
                $selectedoption = $opname;
                $selsetup = $setup * $selectedqty;
                $selrecurring = $fullprice * $selectedqty;
            } else {
                $result3 = select_query("tblproductconfigoptionssub", "", array("configid" => $optionid), "sortorder` ASC,`id", "ASC");
                while ($data3 = mysql_fetch_array($result3)) {
                    $opid = $data3['id'];
                    $opname = $data3['optionname'];
                    $ophidden = $data3['hidden'];
                    if (strpos($opname, "|")) {
                        $opname = explode("|", $opname);
                        $opname = trim($opname[1]);
                    }
                    $opnameonly = $opname;
                    $result4 = select_query("tblpricing", "", array("type" => "configoptions", "currency" => $currency['id'], "relid" => $opid));
                    $data = mysql_fetch_array($result4);
                    $setup = $data[substr($cycle, 0, 1) . "setupfee"];
                    $price = $fullprice = $data[$cycle];
                    if ($orderform && $CONFIG['ProductMonthlyPricingBreakdown']) {
                        $price = $price / $cyclemonths;
                    }
                    if (0 < $price) {
                        $opname .= " " . formatCurrency($price);
                    }
                    $setupvalue = 0 < $setup ? " + " . formatCurrency($setup) . " " . $_LANG['ordersetupfee'] : "";
                    if ($showhidden || !$ophidden) {
                        $options[] = array("id" => $opid, "name" => $opname . $setupvalue, "nameonly" => $opnameonly, "recurring" => $price, "hidden" => $ophidden);
                    }
                    if ($opid == $selvalue || !$selvalue) {
                        $selname = $opnameonly;
                        $selectedoption = $opname;
                        $selsetup = $setup;
                        $selrecurring = $fullprice;
                        $selvalue = $opid;
                    }
                }
            }
        }
        $configoptions[] = array("id" => $optionid, "hidden" => $optionhidden, "optionname" => $optionname, "optiontype" => $optiontype, "selectedvalue" => $selvalue, "selectedqty" => $selectedqty, "selectedname" => $selname, "selectedoption" => $selectedoption, "selectedsetup" => $selsetup, "selectedrecurring" => $selrecurring, "qtyminimum" => $qtyminimum, "qtymaximum" => $qtymaximum, "options" => $options);
    }
    return $configoptions;
}
Example #4
0
function createInvoicesProcess($data, $noemails = "", $nocredit = "")
{
    global $whmcs;
    global $cron;
    global $CONFIG;
    global $_LANG;
    global $invoicecount;
    global $invoiceid;
    $itemid = $data['id'];
    $userid = $data['userid'];
    $type = $data['type'];
    $relid = $data['relid'];
    $duedate = $data['duedate'];
    $paymentmethod = $invpaymentmethod = $data['paymentmethod'];
    if (!$invpaymentmethod) {
        if (!function_exists("getClientsPaymentMethod")) {
            require ROOTDIR . "/includes/clientfunctions.php";
        }
        $invpaymentmethod = getClientsPaymentMethod($userid);
        update_query("tblinvoiceitems", array("paymentmethod" => $invpaymentmethod), array("id" => $itemid));
    }
    if ($itemid) {
        if (get_query_val("tblinvoiceitems", "invoiceid", array("id" => $itemid))) {
            return false;
        }
    }
    $taxrate = $taxrate2 = 0;
    if ($CONFIG['TaxEnabled']) {
        $data = get_query_vals("tblclients", "taxexempt,state,country,separateinvoices", array("id" => $userid));
        $taxexempt = $data['taxexempt'];
        $taxstate = $data['state'];
        $taxcountry = $data['country'];
        if (!$taxexempt) {
            $taxrate = getTaxRate(1, $taxstate, $taxcountry);
            $taxrate2 = getTaxRate(2, $taxstate, $taxcountry);
            $taxrate = $taxrate['rate'];
            $taxrate2 = $taxrate2['rate'];
        }
    }
    $invoiceid = insert_query("tblinvoices", array("date" => "now()", "duedate" => $duedate, "userid" => $userid, "status" => "Unpaid", "taxrate" => $taxrate, "taxrate2" => $taxrate2, "paymentmethod" => $invpaymentmethod, "notes" => $invoicenotes));
    if ($itemid) {
        update_query("tblinvoiceitems", array("invoiceid" => $invoiceid), array("invoiceid" => "0", "userid" => $userid, "type" => "Promo" . $type, "relid" => $relid));
        $where = array("id" => $itemid);
    } else {
        $where = array("invoiceid" => "", "duedate" => $duedate, "userid" => $userid, "paymentmethod" => $paymentmethod);
    }
    update_query("tblinvoiceitems", array("invoiceid" => $invoiceid), $where);
    logActivity("Created Invoice - Invoice ID: " . $invoiceid, $userid);
    if (is_object($cron)) {
        $cron->logActivityDebug("Generated Invoice #" . $invoiceid);
    }
    $billableitemstax = $CONFIG['TaxEnabled'] && $CONFIG['TaxCustomInvoices'] ? "1" : "0";
    $result2 = select_query("tblbillableitems", "", array("userid" => $userid, "invoiceaction" => "2", "invoicecount" => "0"));
    while ($data = mysql_fetch_array($result2)) {
        insert_query("tblinvoiceitems", array("invoiceid" => $invoiceid, "userid" => $userid, "type" => "Item", "relid" => $data['id'], "description" => $data['description'], "amount" => $data['amount'], "taxed" => $billableitemstax));
        update_query("tblbillableitems", array("invoicecount" => "+1"), array("id" => $data['id']));
    }
    updateInvoiceTotal($invoiceid);
    $data2 = get_query_vals("tblclients", "credit,groupid", array("id" => $userid));
    $credit = $data2['credit'];
    $groupid = $data2['groupid'];
    $data2 = get_query_vals("tblinvoices", "subtotal,total", array("id" => $invoiceid));
    $subtotal = $data2['subtotal'];
    $total = $data2['total'];
    $isaddfundsinvoice = get_query_val("tblinvoiceitems", "COUNT(id)", "invoiceid='" . $invoiceid . "' AND (type='AddFunds' OR type='Invoice')");
    if ($groupid && !$isaddfundsinvoice) {
        $discountpercent = get_query_val("tblclientgroups", "discountpercent", array("id" => $groupid));
        if (0 < $discountpercent) {
            $discountamount = $subtotal * ($discountpercent / 100) * (0 - 1);
            insert_query("tblinvoiceitems", array("invoiceid" => $invoiceid, "userid" => $userid, "type" => "GroupDiscount", "description" => $_LANG['clientgroupdiscount'], "amount" => $discountamount, "taxed" => "1"));
            updateInvoiceTotal($invoiceid);
            $data2 = get_query_vals("tblclients", "credit,groupid", array("id" => $userid));
            $credit = $data2['credit'];
            $groupid = $data2['groupid'];
            $data2 = get_query_vals("tblinvoices", "subtotal,total", array("id" => $invoiceid));
            $subtotal = $data2['subtotal'];
            $total = $data2['total'];
        }
    }
    if ($whmcs->get_config("ContinuousInvoiceGeneration")) {
        $result2 = select_query("tblinvoiceitems", "", array("invoiceid" => $invoiceid));
        while ($data = mysql_fetch_array($result2)) {
            $type = $data['type'];
            $relid = $data['relid'];
            $nextinvoicedate = $data['duedate'];
            $year = substr($nextinvoicedate, 0, 4);
            $month = substr($nextinvoicedate, 5, 2);
            $day = substr($nextinvoicedate, 8, 2);
            $proratabilling = false;
            if ($type == "Hosting") {
                $data = get_query_vals("tblhosting", "billingcycle,packageid,regdate,nextduedate", array("id" => $relid));
                $billingcycle = $data['billingcycle'];
                $packageid = $data['packageid'];
                $regdate = $data['regdate'];
                $nextduedate = $data['nextduedate'];
                $data = get_query_vals("tblproducts", "proratabilling,proratadate,proratachargenextmonth", array("id" => $packageid));
                $proratabilling = $data['proratabilling'];
                $proratadate = $data['proratadate'];
                $proratachargenextmonth = $data['proratachargenextmonth'];
                $proratamonths = getBillingCycleMonths($billingcycle);
                $nextinvoicedate = date("Ymd", mktime(0, 0, 0, $month + $proratamonths, $day, $year));
            } else {
                if ($type == "Domain" || $type == "DomainRegister" || $type == "DomainTransfer") {
                    $data = get_query_vals("tbldomains", "registrationperiod,nextduedate", array("id" => $relid));
                    $registrationperiod = $data['registrationperiod'];
                    $nextduedate = explode("-", $data['nextduedate']);
                    $billingcycle = "";
                    $nextinvoicedate = date("Ymd", mktime(0, 0, 0, $nextduedate[1], $nextduedate[2], $nextduedate[0] + $registrationperiod));
                } else {
                    if ($type == "Addon") {
                        $billingcycle = get_query_val("tblhostingaddons", "billingcycle", array("id" => $relid));
                        $proratamonths = getBillingCycleMonths($billingcycle);
                        $nextinvoicedate = date("Ymd", mktime(0, 0, 0, $month + $proratamonths, $day, $year));
                    }
                }
            }
            if ($billingcycle == "One Time") {
                $nextinvoicedate = "00000000";
            }
            if ($regdate == $nextduedate && $proratabilling) {
                if ($billingcycle != "Monthly") {
                    $proratachargenextmonth = 0;
                }
                $orderyear = substr($regdate, 0, 4);
                $ordermonth = substr($regdate, 5, 2);
                $orderday = substr($regdate, 8, 2);
                if ($orderday < $proratadate) {
                    $proratamonth = $ordermonth;
                } else {
                    $proratamonth = $ordermonth + 1;
                }
                $days = (strtotime(date("Y-m-d", mktime(0, 0, 0, $proratamonth, $proratadate, $orderyear))) - strtotime(date("Y-m-d"))) / (60 * 60 * 24);
                $totaldays = 30;
                $nextinvoicedate = date("Y-m-d", mktime(0, 0, 0, $proratamonth, $proratadate, $orderyear));
                if ($proratachargenextmonth <= $orderday && $days < 31) {
                    $nextinvoicedate = date("Y-m-d", mktime(0, 0, 0, $proratamonth + $proratamonths, $proratadate, $orderyear));
                }
            }
            if ($type == "Hosting") {
                update_query("tblhosting", array("nextinvoicedate" => $nextinvoicedate), array("id" => $relid));
            }
            if ($type == "Domain" || $type == "DomainRegister" || $type == "DomainTransfer") {
                update_query("tbldomains", array("nextinvoicedate" => $nextinvoicedate), array("id" => $relid));
            }
            if ($type == "Addon") {
                update_query("tblhostingaddons", array("nextinvoicedate" => $nextinvoicedate), array("id" => $relid));
            }
        }
    }
    $doprocesspaid = false;
    if (!$nocredit && $credit != "0.00" && !$CONFIG['NoAutoApplyCredit']) {
        if ($total <= $credit) {
            $creditleft = $credit - $total;
            $credit = $total;
            $doprocesspaid = true;
        } else {
            $creditleft = 0;
        }
        logActivity("Credit Automatically Applied at Invoice Creation - Invoice ID: " . $invoiceid . " - Amount: " . $credit, $userid);
        insert_query("tblcredit", array("clientid" => $userid, "date" => "now()", "description" => "Credit Applied to Invoice #" . $invoiceid, "amount" => $credit * (0 - 1)));
        update_query("tblclients", array("credit" => $creditleft), array("id" => $userid));
        update_query("tblinvoices", array("credit" => $credit), array("id" => $invoiceid));
        updateInvoiceTotal($invoiceid);
    }
    run_hook("InvoiceCreationPreEmail", array("invoiceid" => $invoiceid));
    if ($doprocesspaid) {
        processPaidInvoice($invoiceid);
    }
    $result2 = select_query("tblpaymentgateways", "value", array("gateway" => $invpaymentmethod, "setting" => "type"));
    $data2 = mysql_fetch_array($result2);
    $paymenttype = $data2['value'];
    if ($noemails != "true") {
        sendMessage(($paymenttype == "CC" || $paymenttype == "OfflineCC" ? "Credit Card " : "") . "Invoice Created", $invoiceid);
    }
    $result2 = select_query("tblinvoices", "total", array("id" => $invoiceid, "status" => "Unpaid"));
    $data2 = mysql_fetch_array($result2);
    $total = $data2['total'];
    if ($total == "0.00") {
        processPaidInvoice($invoiceid);
    }
    run_hook("InvoiceCreated", array("invoiceid" => $invoiceid));
    $invoicetotal = 0;
    ++$invoicecount;
    if (1 < $CONFIG['InvoiceIncrement']) {
        $invoiceincrement = $CONFIG['InvoiceIncrement'] - 1;
        $counter = 1;
        while ($counter <= $invoiceincrement) {
            $tempinvoiceid = insert_query("tblinvoices", array("date" => "now()"));
            delete_query("tblinvoices", array("id" => $tempinvoiceid));
            $counter += 1;
        }
    }
}
Example #5
0
function getRecurringBillingValues($invoiceid)
{
    global $CONFIG;
    $firstcycleperiod = $firstcycleunits = "";
    $invoiceid = (int) $invoiceid;
    $result = select_query("tblinvoiceitems", "tblinvoiceitems.relid,tblinvoiceitems.taxed,tblhosting.userid,tblhosting.amount,tblhosting.billingcycle,tblhosting.packageid,tblhosting.regdate,tblhosting.nextduedate", array("invoiceid" => $invoiceid, "type" => "Hosting"), "tblinvoiceitems`.`id", "ASC", "", "tblhosting ON tblhosting.id=tblinvoiceitems.relid");
    $data = mysql_fetch_array($result);
    $relid = $data['relid'];
    $taxed = $data['taxed'];
    $userid = $data['userid'];
    $recurringamount = $data['amount'];
    $billingcycle = $data['billingcycle'];
    $packageid = $data['packageid'];
    $regdate = $data['regdate'];
    $nextduedate = $data['nextduedate'];
    if (!$relid || $billingcycle == "One Time" || $billingcycle == "Free Account") {
        return false;
    }
    $result = select_query("tblinvoices", "total,taxrate,taxrate2,paymentmethod,(SELECT SUM(amountin)-SUM(amountout) FROM tblaccounts WHERE invoiceid=tblinvoices.id) AS amountpaid", array("id" => $invoiceid));
    $data = mysql_fetch_array($result);
    $total = $data['total'];
    $taxrate = $data['taxrate'];
    $taxrate2 = $data['taxrate2'];
    $paymentmethod = $data['paymentmethod'];
    $amountpaid = $data['amountpaid'];
    $firstpaymentamount = $total - $amountpaid;
    $recurringcycleperiod = getBillingCycleMonths($billingcycle);
    $recurringcycleunits = "Months";
    if (12 <= $recurringcycleperiod) {
        $recurringcycleperiod = $recurringcycleperiod / 12;
        $recurringcycleunits = "Years";
    }
    $recurringamount = 0;
    $query = "SELECT tblhosting.amount,tblinvoiceitems.taxed FROM tblinvoiceitems INNER JOIN tblhosting ON tblhosting.id=tblinvoiceitems.relid WHERE tblinvoiceitems.invoiceid='" . (int) $invoiceid . "' AND tblinvoiceitems.type='Hosting' AND tblhosting.billingcycle='" . db_escape_string($billingcycle) . "'";
    $result = full_query($query);
    while ($data = mysql_fetch_array($result)) {
        $prodamount = $data[0];
        $taxed = $data[1];
        if ($CONFIG['TaxType'] == "Exclusive" && $taxed) {
            if ($CONFIG['TaxL2Compound']) {
                $prodamount = $prodamount + $prodamount * ($taxrate / 100);
                $prodamount = $prodamount + $prodamount * ($taxrate2 / 100);
            } else {
                $prodamount = $prodamount + $prodamount * ($taxrate / 100) + $prodamount * ($taxrate2 / 100);
            }
        }
        $recurringamount += $prodamount;
    }
    $query = "SELECT tblhostingaddons.recurring,tblhostingaddons.tax FROM tblinvoiceitems INNER JOIN tblhostingaddons ON tblhostingaddons.id=tblinvoiceitems.relid WHERE tblinvoiceitems.invoiceid='" . (int) $invoiceid . "' AND tblinvoiceitems.type='Addon' AND tblhostingaddons.billingcycle='" . db_escape_string($billingcycle) . "'";
    $result = full_query($query);
    while ($data = mysql_fetch_array($result)) {
        $addonamount = $data[0];
        $addontax = $data[1];
        if ($CONFIG['TaxType'] == "Exclusive" && $addontax) {
            if ($CONFIG['TaxL2Compound']) {
                $addonamount = $addonamount + $addonamount * ($taxrate / 100);
                $addonamount = $addonamount + $addonamount * ($taxrate2 / 100);
            } else {
                $addonamount = $addonamount + $addonamount * ($taxrate / 100) + $addonamount * ($taxrate2 / 100);
            }
        }
        $recurringamount += $addonamount;
    }
    if (in_array($billingcycle, array("Annually", "Biennially", "Triennially"))) {
        $cycleregperiods = array("Annually" => "1", "Biennially" => "2", "Triennially" => "3");
        $query = "SELECT SUM(tbldomains.recurringamount) FROM tblinvoiceitems INNER JOIN tbldomains ON tbldomains.id=tblinvoiceitems.relid WHERE tblinvoiceitems.invoiceid='" . (int) $invoiceid . "' AND tblinvoiceitems.type IN ('DomainRegister','DomainTransfer','Domain') AND tbldomains.registrationperiod='" . db_escape_string($cycleregperiods[$billingcycle]) . "'";
        $result = full_query($query);
        $data = mysql_fetch_array($result);
        $domainamount = $data[0];
        if ($CONFIG['TaxType'] == "Exclusive" && $CONFIG['TaxDomains']) {
            if ($CONFIG['TaxL2Compound']) {
                $domainamount = $domainamount + $domainamount * ($taxrate / 100);
                $domainamount = $domainamount + $domainamount * ($taxrate2 / 100);
            } else {
                $domainamount = $domainamount + $domainamount * ($taxrate / 100) + $domainamount * ($taxrate2 / 100);
            }
        }
        $recurringamount += $domainamount;
    }
    $result = select_query("tblinvoices", "duedate", array("id" => $invoiceid));
    $data = mysql_fetch_array($result);
    $invoiceduedate = $data['duedate'];
    $invoiceduedate = str_replace("-", "", $invoiceduedate);
    $overdue = $invoiceduedate < date("Ymd") ? true : false;
    $result = select_query("tblproducts", "proratabilling,proratadate,proratachargenextmonth", array("id" => $packageid));
    $data = mysql_fetch_array($result);
    $proratabilling = $data['proratabilling'];
    $proratadate = $data['proratadate'];
    $proratachargenextmonth = $data['proratachargenextmonth'];
    if ($regdate == $nextduedate && $proratabilling) {
        $orderyear = substr($regdate, 0, 4);
        $ordermonth = substr($regdate, 5, 2);
        $orderday = substr($regdate, 8, 2);
        if (!function_exists("getProrataValues")) {
            require ROOTDIR . "/includes/invoicefunctions.php";
        }
        $proratavals = getProrataValues($billingcycle, 0, $proratadate, $proratachargenextmonth, $orderday, $ordermonth, $orderyear);
        $firstcycleperiod = $proratavals['days'];
        $firstcycleunits = "Days";
    }
    if (!$firstcycleperiod) {
        $firstcycleperiod = $recurringcycleperiod;
    }
    if (!$firstcycleunits) {
        $firstcycleunits = $recurringcycleunits;
    }
    $result = select_query("tblpaymentgateways", "value", array("gateway" => $paymentmethod, "setting" => "convertto"));
    $data = mysql_fetch_array($result);
    $convertto = $data[0];
    if ($convertto) {
        $currency = getCurrency($userid);
        $firstpaymentamount = convertCurrency($firstpaymentamount, $currency['id'], $convertto);
        $recurringamount = convertCurrency($recurringamount, $currency['id'], $convertto);
    }
    $firstpaymentamount = format_as_currency($firstpaymentamount);
    $recurringamount = format_as_currency($recurringamount);
    $returndata = array();
    $returndata['primaryserviceid'] = $relid;
    if ($firstpaymentamount != $recurringamount) {
        $returndata['firstpaymentamount'] = $firstpaymentamount;
        $returndata['firstcycleperiod'] = $firstcycleperiod;
        $returndata['firstcycleunits'] = $firstcycleunits;
    }
    $returndata['recurringamount'] = $recurringamount;
    $returndata['recurringcycleperiod'] = $recurringcycleperiod;
    $returndata['recurringcycleunits'] = $recurringcycleunits;
    $returndata['overdue'] = $overdue;
    return $returndata;
}
Example #6
0
 public function calcConfigOptionsUpgradeDue()
 {
     global $whmcs;
     $pid = $this->getProductInfo("pid");
     $domain = $this->getProductInfo("domain");
     $nextduedate = $this->getProductInfo("nextduedate");
     $billingcycle = $this->getProductInfo("billingcycle");
     $productname = $this->getProductInfo("productname");
     $applytax = $this->getProductInfo("tax");
     if ($domain) {
         $productname .= " - " . $domain;
     }
     $year = substr($nextduedate, 0, 4);
     $month = substr($nextduedate, 5, 2);
     $day = substr($nextduedate, 8, 2);
     $cyclemonths = getBillingCycleMonths($billingcycle);
     $prevduedate = date("Y-m-d", mktime(0, 0, 0, $month - $cyclemonths, $day, $year));
     $totaldays = round((strtotime($nextduedate) - strtotime($prevduedate)) / 86400);
     $todaysdate = date("Y-m-d");
     $todaysdate = strtotime($todaysdate);
     $nextduedatetime = strtotime($nextduedate);
     $days = round(($nextduedatetime - $todaysdate) / 86400);
     if ($days < 0) {
         $days = $totaldays;
     }
     $percentage = $days / $totaldays;
     $newconfigoptions = getCartConfigOptions($pid, $this->newconfigoptions, $billingcycle);
     $oldconfigoptions = getCartConfigOptions($pid, "", $billingcycle, $id);
     foreach ($newconfigoptions as $key => $configoption) {
         $configid = $configoption['id'];
         $configname = $configoption['optionname'];
         $optiontype = $configoption['optiontype'];
         $new_selectedvalue = $configoption['selectedvalue'];
         $new_selectedqty = $configoption['selectedqty'];
         $new_selectedname = $configoption['selectedname'];
         $new_selectedsetup = $configoption['selectedsetup'];
         $new_selectedrecurring = $configoption['selectedrecurring'];
         $old_selectedvalue = $oldconfigoptions[$key]['selectedvalue'];
         $old_selectedqty = $oldconfigoptions[$key]['selectedqty'];
         $old_selectedname = $oldconfigoptions[$key]['selectedname'];
         $old_selectedsetup = $oldconfigoptions[$key]['selectedsetup'];
         $old_selectedrecurring = $oldconfigoptions[$key]['selectedrecurring'];
         if (($optiontype == 1 || $optiontype == 2) && $new_selectedvalue != $old_selectedvalue || ($optiontype == 3 || $optiontype == 4) && $new_selectedqty != $old_selectedqty) {
             $difference = $new_selectedrecurring - $old_selectedrecurring;
             $amountdue = $difference * $percentage;
             $amountdue = format_as_currency($amountdue);
             if (!$CONFIG['CreditOnDowngrade'] && $amountdue < 0) {
                 $amountdue = format_as_currency(0);
             }
             if ($optiontype == 1 || $optiontype == 2) {
                 $db_orig_value = $old_selectedvalue;
                 $db_new_value = $new_selectedvalue;
                 $originalvalue = $old_selectedname;
                 $newvalue = $new_selectedname;
             } else {
                 if ($optiontype == 3) {
                     $db_orig_value = $old_selectedqty;
                     $db_new_value = $new_selectedqty;
                     if ($old_selectedqty) {
                         $originalvalue = $whmcs->get_lang("yes");
                         $newvalue = $whmcs->get_lang("no");
                     } else {
                         $originalvalue = $whmcs->get_lang("no");
                         $newvalue = $whmcs->get_lang("yes");
                     }
                 } else {
                     if ($optiontype == 4) {
                         $new_selectedqty = (int) $new_selectedqty;
                         if ($new_selectedqty < 0) {
                             $new_selectedqty = 11;
                         }
                         $db_orig_value = $old_selectedqty;
                         $db_new_value = $new_selectedqty;
                         $originalvalue = $old_selectedqty;
                         $newvalue = $new_selectedqty . " " . $configoption['options'][0]['nameonly'];
                     }
                 }
             }
             $itemdiscount = 0;
             if ($promoqualifies && 0 < $amountdue && (!count($upgradeconfigoptions) || in_array($configid, $upgradeconfigoptions))) {
                 $itemdiscount = $discounttype == "Percentage" ? round($amountdue * $promovalue, 2) : ($amountdue < $promovalue ? $amountdue : $promovalue);
             }
             $discount += $itemdiscount;
             $description = $productname . "\r\n" . $configname . ": " . (0 < $amountdue ? "Upgrade" : "Downgrade") . " from " . $originalvalue . " to " . $newvalue . " (" . getTodaysDate() . " - " . fromMySQLDate($nextduedate) . ")" . "\r\n" . "Regular Recurring Amount Increase: " . formatCurrency($difference) . "";
             $this->addLineItem($description, $amountdue);
             $upgradearray[] = array("configname" => $configname, "originalvalue" => $originalvalue, "newvalue" => $newvalue, "price" => formatCurrency($amountdue));
             continue;
         }
     }
     return $upgradearray;
 }