示例#1
0
/** This function returns a HTML output of associated vtiger_products for a given entity (Quotes,Invoice,Sales order or Purchase order)
 * Param $module - module name
 * Param $focus - module object
 * Return type string
 */
function getDetailAssociatedProducts($module, $focus)
{
    global $log;
    $log->debug("Entering getDetailAssociatedProducts(" . $module . "," . get_class($focus) . ") method ...");
    global $adb;
    global $mod_strings;
    global $theme;
    global $log;
    global $app_strings, $current_user;
    $theme_path = "themes/" . $theme . "/";
    $image_path = $theme_path . "images/";
    if (vtlib_isModuleActive("Products")) {
        $hide_stock = 'no';
    } else {
        $hide_stock = 'yes';
    }
    if ($module != 'PurchaseOrder') {
        if (GlobalVariable::getVariable('B2B', '1') == '1') {
            $acvid = $focus->column_fields['account_id'];
        } else {
            $acvid = $focus->column_fields['contact_id'];
        }
        if ($hide_stock == 'no') {
            $colspan = '2';
        } else {
            $colspan = '1';
        }
    } else {
        $acvid = $focus->column_fields['vendor_id'];
        $colspan = '1';
    }
    //Get the taxtype of this entity
    $taxtype = getInventoryTaxType($module, $focus->id);
    $currencytype = getInventoryCurrencyInfo($module, $focus->id);
    $output = '';
    //Header Rows
    $output .= '

	<table width="100%"  border="0" align="center" cellpadding="5" cellspacing="0" class="crmTable" id="proTab">
	   <tr valign="top">
	   	<td colspan="' . $colspan . '" class="dvInnerHeader"><b>' . $app_strings['LBL_ITEM_DETAILS'] . '</b></td>
		<td class="dvInnerHeader" align="center" colspan="2"><b>' . $app_strings['LBL_CURRENCY'] . ' : </b>' . getTranslatedCurrencyString($currencytype['currency_name']) . ' (' . $currencytype['currency_symbol'] . ')
		</td>
		<td class="dvInnerHeader" align="center" colspan="2"><b>' . $app_strings['LBL_TAX_MODE'] . ' : </b>' . $app_strings[$taxtype] . '
		</td>
	   </tr>
	   <tr valign="top">
		<td width=40% class="lvtCol"><font color="red">*</font>
			<b>' . $app_strings['LBL_ITEM_NAME'] . '</b>
		</td>';
    //Add Quantity in Stock column for SO, Quotes and Invoice
    if (($module == 'Quotes' || $module == 'SalesOrder' || $module == 'Invoice') && $hide_stock == 'no') {
        $output .= '<td width=10% class="lvtCol"><b>' . $app_strings['LBL_QTY_IN_STOCK'] . '</b></td>';
    }
    $output .= '

		<td width=10% class="lvtCol"><b>' . $app_strings['LBL_QTY'] . '</b></td>
		<td width=10% class="lvtCol" align="right"><b>' . $app_strings['LBL_LIST_PRICE'] . '</b></td>
		<td width=12% nowrap class="lvtCol" align="right"><b>' . $app_strings['LBL_TOTAL'] . '</b></td>
		<td width=13% valign="top" class="lvtCol" align="right"><b>' . $app_strings['LBL_NET_PRICE'] . '</b></td>
	   </tr>
	   	';
    // DG 15 Aug 2006
    // Add "ORDER BY sequence_no" to retain add order on all inventoryproductrel items
    if ($module == 'Quotes' || $module == 'PurchaseOrder' || $module == 'SalesOrder' || $module == 'Invoice') {
        $query = "select case when vtiger_products.productid != '' then vtiger_products.productname else vtiger_service.servicename end as productname," . " case when vtiger_products.productid != '' then 'Products' else 'Services' end as entitytype," . " case when vtiger_products.productid != '' then vtiger_products.unit_price else vtiger_service.unit_price end as unit_price," . " case when vtiger_products.productid != '' then vtiger_products.qtyinstock else 'NA' end as qtyinstock, vtiger_inventoryproductrel.* " . " from vtiger_inventoryproductrel" . " left join vtiger_products on vtiger_products.productid=vtiger_inventoryproductrel.productid " . " left join vtiger_service on vtiger_service.serviceid=vtiger_inventoryproductrel.productid " . " where id=? ORDER BY sequence_no";
    }
    $result = $adb->pquery($query, array($focus->id));
    $num_rows = $adb->num_rows($result);
    $netTotal = '0.00';
    for ($i = 1; $i <= $num_rows; $i++) {
        $sub_prod_query = $adb->pquery("SELECT productid from vtiger_inventorysubproductrel WHERE id=? AND sequence_no=?", array($focus->id, $i));
        $subprodname_str = '';
        if ($adb->num_rows($sub_prod_query) > 0) {
            for ($j = 0; $j < $adb->num_rows($sub_prod_query); $j++) {
                $sprod_id = $adb->query_result($sub_prod_query, $j, 'productid');
                $sprod_name = getProductName($sprod_id);
                $str_sep = "";
                if ($j > 0) {
                    $str_sep = ":";
                }
                $subprodname_str .= $str_sep . " - " . $sprod_name;
            }
        }
        $subprodname_str = str_replace(":", "<br>", $subprodname_str);
        $productid = $adb->query_result($result, $i - 1, 'productid');
        $entitytype = $adb->query_result($result, $i - 1, 'entitytype');
        $productname = $adb->query_result($result, $i - 1, 'productname');
        $productname = '<a href="index.php?action=DetailView&record=' . $productid . '&module=' . $entitytype . '">' . $productname . '</a>';
        if ($subprodname_str != '') {
            $productname .= "<br/><span style='color:#C0C0C0;font-style:italic;'>" . $subprodname_str . "</span>";
        }
        $comment = $adb->query_result($result, $i - 1, 'comment');
        $qtyinstock = $adb->query_result($result, $i - 1, 'qtyinstock');
        $qty = $adb->query_result($result, $i - 1, 'quantity');
        $qty = number_format($qty, 2, '.', '');
        //Convert to 2 decimals
        $unitprice = $adb->query_result($result, $i - 1, 'unit_price');
        $listprice = $adb->query_result($result, $i - 1, 'listprice');
        $total = $qty * $listprice;
        $listprice = number_format($listprice, 2, '.', '');
        //Convert to 2 decimals
        //Product wise Discount calculation - starts
        $discount_percent = $adb->query_result($result, $i - 1, 'discount_percent');
        $discount_amount = $adb->query_result($result, $i - 1, 'discount_amount');
        $totalAfterDiscount = $total;
        $productDiscount = '0.00';
        if ($discount_percent != 'NULL' && $discount_percent != '') {
            $productDiscount = $total * $discount_percent / 100;
            $productDiscount = number_format($productDiscount, 2, '.', '');
            $totalAfterDiscount = $total - $productDiscount;
            //if discount is percent then show the percentage
            $discount_info_message = "{$discount_percent} % of " . CurrencyField::convertToUserFormat($total, null, true) . " = " . CurrencyField::convertToUserFormat($productDiscount, null, true);
        } elseif ($discount_amount != 'NULL' && $discount_amount != '') {
            $productDiscount = $discount_amount;
            $productDiscount = number_format($productDiscount, 2, '.', '');
            $totalAfterDiscount = $total - $productDiscount;
            $discount_info_message = $app_strings['LBL_DIRECT_AMOUNT_DISCOUNT'] . " = " . CurrencyField::convertToUserFormat($productDiscount, null, true);
        } else {
            $discount_info_message = $app_strings['LBL_NO_DISCOUNT_FOR_THIS_LINE_ITEM'];
        }
        //Product wise Discount calculation - ends
        $totalAfterDiscount = number_format($totalAfterDiscount, 2, '.', '');
        //Convert to 2 decimals
        $netprice = $totalAfterDiscount;
        //Calculate the individual tax if taxtype is individual
        if ($taxtype == 'individual') {
            $taxtotal = '0.00';
            $tax_info_message = $app_strings['LBL_TOTAL_AFTER_DISCOUNT'] . " = " . CurrencyField::convertToUserFormat($totalAfterDiscount, null, true) . " \\n";
            $tax_details = getTaxDetailsForProduct($productid, 'all', $acvid);
            for ($tax_count = 0; $tax_count < count($tax_details); $tax_count++) {
                $tax_name = $tax_details[$tax_count]['taxname'];
                $tax_label = $tax_details[$tax_count]['taxlabel'];
                $tax_value = getInventoryProductTaxValue($focus->id, $productid, $tax_name);
                $individual_taxamount = $totalAfterDiscount * $tax_value / 100;
                $individual_taxamount = number_format($individual_taxamount, 2, '.', '');
                //Convert to 2 decimals
                $taxtotal = $taxtotal + $individual_taxamount;
                $taxtotal = number_format($taxtotal, 2, '.', '');
                //Convert to 2 decimals
                $tax_info_message .= "{$tax_label} : {$tax_value} % = " . CurrencyField::convertToUserFormat($individual_taxamount, null, true) . " \\n";
            }
            $tax_info_message .= "\\n " . $app_strings['LBL_TOTAL_TAX_AMOUNT'] . " = " . CurrencyField::convertToUserFormat($taxtotal, null, true);
            $netprice = $netprice + $taxtotal;
            $netprice = number_format($netprice, 2, '.', '');
            //Convert to 2 decimals
        }
        $sc_image_tag = '';
        if ($module == 'Invoice') {
            switch ($entitytype) {
                case 'Services':
                    if (vtlib_isModuleActive('ServiceContracts')) {
                        $sc_image_tag = '<a href="index.php?module=ServiceContracts&action=EditView&service_id=' . $productid . '&sc_related_to=' . $focus->column_fields['account_id'] . '&start_date=' . DateTimeField::convertToUserFormat($focus->column_fields['invoicedate']) . '&return_module=' . $module . '&return_id=' . $focus->id . '">' . '<img border="0" src="' . vtiger_imageurl('handshake.gif', $theme) . '" title="' . getTranslatedString('LBL_ADD_NEW', $module) . " " . getTranslatedString('ServiceContracts', 'ServiceContracts') . '" style="cursor: pointer;" align="absmiddle" />' . '</a>';
                    }
                    break;
                case 'Products':
                    if (vtlib_isModuleActive('Assets')) {
                        $sc_image_tag = '<a href="index.php?module=Assets&action=EditView&invoiceid=' . $focus->id . '&product=' . $productid . '&account=' . $focus->column_fields['account_id'] . '&datesold=' . DateTimeField::convertToUserFormat($focus->column_fields['invoicedate']) . '&return_module=' . $module . '&return_id=' . $focus->id . '" onmouseout="vtlib_listview.trigger(\'invoiceasset.onmouseout\', $(this))" onmouseover="vtlib_listview.trigger(\'cell.onmouseover\', $(this))">' . '<img border="0" src="' . vtiger_imageurl('barcode.png', $theme) . '" title="' . getTranslatedString('LBL_ADD_NEW', $module) . " " . getTranslatedString('Assets', 'Assets') . '" style="cursor: pointer;" align="absmiddle" />' . '<span style="display:none;" vtmodule="Assets" vtfieldname="invoice_product" vtrecordid="' . $focus->id . '::' . $productid . '::' . $i . '" type="vtlib_metainfo"></span>' . '</a>';
                    }
                    break;
                default:
                    $sc_image_tag = '';
            }
        }
        //For Product Name
        $output .= '
			   <tr valign="top">
				<td class="crmTableRow small lineOnTop">
					' . $productname . '&nbsp;' . $sc_image_tag . '
					<br>' . $comment . '
				</td>';
        //Upto this added to display the Product name and comment
        if ($module != 'PurchaseOrder' && $hide_stock == 'no') {
            $output .= '<td class="crmTableRow small lineOnTop">' . $qtyinstock . '</td>';
        }
        $output .= '<td class="crmTableRow small lineOnTop">' . $qty . '</td>';
        $output .= '
			<td class="crmTableRow small lineOnTop" align="right">
				<table width="100%" border="0" cellpadding="5" cellspacing="0">
				   <tr>
				   	<td align="right">' . CurrencyField::convertToUserFormat($listprice, null, true) . '</td>
				   </tr>
				   <tr>
					   <td align="right">(-)&nbsp;<b><a href="javascript:;" onclick="alert(\'' . $discount_info_message . '\'); ">' . $app_strings['LBL_DISCOUNT'] . ' : </a></b></td>
				   </tr>
				   <tr>
				   	<td align="right" nowrap>' . $app_strings['LBL_TOTAL_AFTER_DISCOUNT'] . ' : </td>
				   </tr>';
        if ($taxtype == 'individual') {
            $output .= '
				   <tr>
					   <td align="right" nowrap>(+)&nbsp;<b><a href="javascript:;" onclick="alert(\'' . $tax_info_message . '\');">' . $app_strings['LBL_TAX'] . ' : </a></b></td>
				   </tr>';
        }
        $output .= '
				</table>
			</td>';
        $output .= '
			<td class="crmTableRow small lineOnTop" align="right">
				<table width="100%" border="0" cellpadding="5" cellspacing="0">
				   <tr><td align="right">' . CurrencyField::convertToUserFormat($total, null, true) . '</td></tr>
				   <tr><td align="right">' . CurrencyField::convertToUserFormat($productDiscount, null, true) . '</td></tr>
				   <tr><td align="right" nowrap>' . CurrencyField::convertToUserFormat($totalAfterDiscount, null, true) . '</td></tr>';
        if ($taxtype == 'individual') {
            $output .= '<tr><td align="right" nowrap>' . CurrencyField::convertToUserFormat($taxtotal, null, true) . '</td></tr>';
        }
        $output .= '
				</table>
			</td>';
        $output .= '<td class="crmTableRow small lineOnTop" valign="bottom" align="right">' . CurrencyField::convertToUserFormat($netprice, null, true) . '</td>';
        $output .= '</tr>';
        $netTotal = $netTotal + $netprice;
    }
    $output .= '</table>';
    //$netTotal should be equal to $focus->column_fields['hdnSubTotal']
    $netTotal = $focus->column_fields['hdnSubTotal'];
    $netTotal = number_format($netTotal, 2, '.', '');
    //Convert to 2 decimals
    //Display the total, adjustment, S&H details
    $output .= '<table width="100%" border="0" cellspacing="0" cellpadding="5" class="crmTable">';
    $output .= '<tr>';
    $output .= '<td width="88%" class="crmTableRow small" align="right"><b>' . $app_strings['LBL_NET_TOTAL'] . '</td>';
    $output .= '<td width="12%" class="crmTableRow small" align="right"><b>' . CurrencyField::convertToUserFormat($netTotal, null, true) . '</b></td>';
    $output .= '</tr>';
    //Decide discount
    $finalDiscount = '0.00';
    $final_discount_info = '0';
    //if($focus->column_fields['hdnDiscountPercent'] != '') - previously (before changing to prepared statement) the selected option (either percent or amount) will have value and the other remains empty. So we can find the non selected item by empty check. But now with prepared statement, the non selected option stored as 0
    if ($focus->column_fields['hdnDiscountPercent'] != '0') {
        $finalDiscount = $netTotal * $focus->column_fields['hdnDiscountPercent'] / 100;
        $finalDiscount = number_format($finalDiscount, 2, '.', '');
        $final_discount_info = $focus->column_fields['hdnDiscountPercent'] . " % of " . CurrencyField::convertToUserFormat($netTotal, null, true) . " = " . CurrencyField::convertToUserFormat($finalDiscount, null, true);
    } elseif ($focus->column_fields['hdnDiscountAmount'] != '0') {
        $finalDiscount = $focus->column_fields['hdnDiscountAmount'];
        $finalDiscount = number_format($finalDiscount, 2, '.', '');
        $final_discount_info = CurrencyField::convertToUserFormat($finalDiscount, null, true);
    }
    //Alert the Final Discount amount even it is zero
    $final_discount_info = $app_strings['LBL_FINAL_DISCOUNT_AMOUNT'] . " = {$final_discount_info}";
    $final_discount_info = 'onclick="alert(\'' . $final_discount_info . '\');"';
    $output .= '<tr>';
    $output .= '<td align="right" class="crmTableRow small lineOnTop">(-)&nbsp;<b><a href="javascript:;" ' . $final_discount_info . '>' . $app_strings['LBL_DISCOUNT'] . '</a></b></td>';
    $output .= '<td align="right" class="crmTableRow small lineOnTop">' . CurrencyField::convertToUserFormat($finalDiscount, null, true) . '</td>';
    $output .= '</tr>';
    if ($taxtype == 'group') {
        $taxtotal = '0.00';
        $final_totalAfterDiscount = $netTotal - $finalDiscount;
        $tax_info_message = $app_strings['LBL_TOTAL_AFTER_DISCOUNT'] . " = " . CurrencyField::convertToUserFormat($final_totalAfterDiscount, null, true) . " \\n";
        //First we should get all available taxes and then retrieve the corresponding tax values
        $tax_details = getAllTaxes('available', '', 'edit', $focus->id);
        $ipr_cols = $adb->getColumnNames('vtiger_inventoryproductrel');
        //if taxtype is group then the tax should be same for all products in vtiger_inventoryproductrel table
        for ($tax_count = 0; $tax_count < count($tax_details); $tax_count++) {
            $tax_name = $tax_details[$tax_count]['taxname'];
            $tax_label = $tax_details[$tax_count]['taxlabel'];
            if (in_array($tax_name, $ipr_cols)) {
                $tax_value = $adb->query_result($result, 0, $tax_name);
            } else {
                $tax_value = $tax_details[$tax_count]['percentage'];
            }
            if ($tax_value == '' || $tax_value == 'NULL') {
                $tax_value = '0.00';
            }
            $taxamount = ($netTotal - $finalDiscount) * $tax_value / 100;
            $taxtotal = $taxtotal + $taxamount;
            $tax_info_message .= "{$tax_label} : {$tax_value} % = " . CurrencyField::convertToUserFormat($taxtotal, null, true) . " \\n";
        }
        $tax_info_message .= "\\n " . $app_strings['LBL_TOTAL_TAX_AMOUNT'] . " = " . CurrencyField::convertToUserFormat($taxtotal, null, true);
        $output .= '<tr>';
        $output .= '<td align="right" class="crmTableRow small">(+)&nbsp;<b><a href="javascript:;" onclick="alert(\'' . $tax_info_message . '\');">' . $app_strings['LBL_TAX'] . '</a></b></td>';
        $output .= '<td align="right" class="crmTableRow small">' . CurrencyField::convertToUserFormat($taxtotal, null, true) . '</td>';
        $output .= '</tr>';
    }
    $shAmount = $focus->column_fields['hdnS_H_Amount'] != '' ? $focus->column_fields['hdnS_H_Amount'] : '0.00';
    $shAmount = number_format($shAmount, 2, '.', '');
    //Convert to 2 decimals
    $output .= '<tr>';
    $output .= '<td align="right" class="crmTableRow small">(+)&nbsp;<b>' . $app_strings['LBL_SHIPPING_AND_HANDLING_CHARGES'] . '</b></td>';
    $output .= '<td align="right" class="crmTableRow small">' . CurrencyField::convertToUserFormat($shAmount, null, true) . '</td>';
    $output .= '</tr>';
    //calculate S&H tax
    $shtaxtotal = '0.00';
    //First we should get all available taxes and then retrieve the corresponding tax values
    $shtax_details = getAllTaxes('available', 'sh', 'edit', $focus->id);
    //if taxtype is group then the tax should be same for all products in vtiger_inventoryproductrel table
    $shtax_info_message = $app_strings['LBL_SHIPPING_AND_HANDLING_CHARGE'] . " = " . CurrencyField::convertToUserFormat($shAmount, null, true) . "\\n";
    for ($shtax_count = 0; $shtax_count < count($shtax_details); $shtax_count++) {
        $shtax_name = $shtax_details[$shtax_count]['taxname'];
        $shtax_label = $shtax_details[$shtax_count]['taxlabel'];
        $shtax_percent = getInventorySHTaxPercent($focus->id, $shtax_name);
        $shtaxamount = $shAmount * $shtax_percent / 100;
        $shtaxamount = number_format($shtaxamount, 2, '.', '');
        $shtaxtotal = $shtaxtotal + $shtaxamount;
        $shtax_info_message .= "{$shtax_label} : {$shtax_percent} % = " . CurrencyField::convertToUserFormat($shtaxamount, null, true) . " \\n";
    }
    $shtax_info_message .= "\\n " . $app_strings['LBL_TOTAL_TAX_AMOUNT'] . " = " . CurrencyField::convertToUserFormat($shtaxtotal, null, true);
    $output .= '<tr>';
    $output .= '<td align="right" class="crmTableRow small">(+)&nbsp;<b><a href="javascript:;" onclick="alert(\'' . $shtax_info_message . '\')">' . $app_strings['LBL_TAX_FOR_SHIPPING_AND_HANDLING'] . '</a></b></td>';
    $output .= '<td align="right" class="crmTableRow small">' . CurrencyField::convertToUserFormat($shtaxtotal, null, true) . '</td>';
    $output .= '</tr>';
    $adjustment = $focus->column_fields['txtAdjustment'] != '' ? $focus->column_fields['txtAdjustment'] : '0.00';
    $adjustment = number_format($adjustment, 2, '.', '');
    //Convert to 2 decimals
    $output .= '<tr>';
    $output .= '<td align="right" class="crmTableRow small">&nbsp;<b>' . $app_strings['LBL_ADJUSTMENT'] . '</b></td>';
    $output .= '<td align="right" class="crmTableRow small">' . CurrencyField::convertToUserFormat($adjustment, null, true) . '</td>';
    $output .= '</tr>';
    $grandTotal = $focus->column_fields['hdnGrandTotal'] != '' ? $focus->column_fields['hdnGrandTotal'] : '0.00';
    $grandTotal = number_format($grandTotal, 2, '.', '');
    //Convert to 2 decimals
    $output .= '<tr>';
    $output .= '<td align="right" class="crmTableRow small lineOnTop"><b>' . $app_strings['LBL_GRAND_TOTAL'] . '</b></td>';
    $output .= '<td align="right" class="crmTableRow small lineOnTop">' . CurrencyField::convertToUserFormat($grandTotal, null, true) . '</td>';
    $output .= '</tr>';
    $output .= '</table>';
    $log->debug("Exiting getDetailAssociatedProducts method ...");
    return $output;
}
示例#2
0
 private function x18($x0c, $x0e)
 {
     global $x15d, $x15e, $x15f, $x160, $x161, $x162, $x163, $x164, $x165, $x166, $x167, $x168, $x169, $x16a, $x16b, $x16c, $x16d, $x16e, $x16f, $x170, $x171, $x172, $x173, $x174, $x175, $x176, $x177, $x178;
     $xc5 = $this->x22($x0c, $x0e);
     $x6e = "select case when vtiger_products.productid != '' then vtiger_products.productname else vtiger_service.servicename end as productname," . " case when vtiger_products.productid != '' then vtiger_products.productid else vtiger_service.serviceid end as psid," . " case when vtiger_products.productid != '' then vtiger_products.product_no else vtiger_service.service_no end as psno," . " case when vtiger_products.productid != '' then 'Products' else 'Services' end as entitytype," . " case when vtiger_products.productid != '' then vtiger_products.unit_price else vtiger_service.unit_price end as unit_price," . " case when vtiger_products.productid != '' then vtiger_products.usageunit else vtiger_service.service_usageunit end as usageunit," . " case when vtiger_products.productid != '' then vtiger_products.qty_per_unit else vtiger_service.qty_per_unit end as qty_per_unit," . " case when vtiger_products.productid != '' then vtiger_products.qtyinstock else 'NA' end as qtyinstock," . " case when vtiger_products.productid != '' then c1.description else c2.description end as psdescription, vtiger_inventoryproductrel.* " . " from vtiger_inventoryproductrel" . " left join vtiger_products on vtiger_products.productid=vtiger_inventoryproductrel.productid " . " left join vtiger_crmentity as c1 on c1.crmid = vtiger_products.productid " . " left join vtiger_service on vtiger_service.serviceid=vtiger_inventoryproductrel.productid " . " left join vtiger_crmentity as c2 on c2.crmid = vtiger_service.serviceid " . " where id=? ORDER BY sequence_no";
     $x3e = self::$x0f->pquery($x6e, array($x0e->id));
     $x43 = self::$x0f->num_rows($x3e);
     $xc6 = "0.00";
     $xc7 = 0;
     $xc8 = 0;
     $xc9 = 0;
     list($xca, $xcb) = $this->x1a($x0e->id);
     $xcc = array();
     $xcd = array();
     for ($x2d = 1; $x2d <= $x43; $x2d++) {
         $xce = self::$x0f->pquery("SELECT productid from vtiger_inventorysubproductrel WHERE id=? AND sequence_no=?", array($x0e->id, $x2d));
         $xcf = '';
         if (self::$x0f->num_rows($xce) > 0) {
             for ($x8d = 0; $x8d < self::$x0f->num_rows($xce); $x8d++) {
                 $xd0 = self::$x0f->query_result($xce, $x8d, "productid");
                 $xd1 = getProductName($xd0);
                 $xd2 = "";
                 if ($x8d > 0) {
                     $xd2 = ":";
                 }
                 $xcf .= $xd2 . " - " . $xd1;
             }
         }
         $xcf = $x172(":", "<br>", $xcf);
         $xd3 = self::$x0f->query_result($x3e, $x2d - 1, "psid");
         $xd4 = self::$x0f->query_result($x3e, $x2d - 1, "psno");
         $xd5 = self::$x0f->query_result($x3e, $x2d - 1, "productid");
         $xd6 = self::$x0f->query_result($x3e, $x2d - 1, "entitytype");
         $xd7 = $xd8 = self::$x0f->query_result($x3e, $x2d - 1, "productname");
         if ($xcf != "") {
             $xd8 .= "<br/><span style='color:#C0C0C0;font-style:italic;'>" . $xcf . "</span>";
         }
         $xd9 = self::$x0f->query_result($x3e, $x2d - 1, "comment");
         $xda = self::$x0f->query_result($x3e, $x2d - 1, "psdescription");
         $xdb = self::$x0f->query_result($x3e, $x2d - 1, "description");
         $xdc = self::$x0f->query_result($x3e, $x2d - 1, "qtyinstock");
         $xdd = self::$x0f->query_result($x3e, $x2d - 1, "quantity");
         $xde = self::$x0f->query_result($x3e, $x2d - 1, "qty_per_unit");
         $xdf = self::$x0f->query_result($x3e, $x2d - 1, "usageunit");
         $xe0 = self::$x0f->query_result($x3e, $x2d - 1, "unit_price");
         $xe1 = self::$x0f->query_result($x3e, $x2d - 1, "listprice");
         $xe2 = $xdd * $xe1;
         $xe3 = self::$x0f->query_result($x3e, $x2d - 1, "discount_percent");
         $xe4 = self::$x0f->query_result($x3e, $x2d - 1, "discount_amount");
         $xe5 = $xe2;
         $xe6 = "0.00";
         $xe7 = "";
         if ($xe3 != "NULL" && $xe3 != "") {
             $xe6 = $xe2 * $xe3 / 100;
             $xe5 = $xe2 - $xe6;
             $xe7 = $xe3;
         } elseif ($xe4 != "NULL" && $xe4 != "") {
             $xe6 = $xe4;
             $xe5 = $xe2 - $xe6;
         }
         $xe8 = $xe5;
         if ($xc5 == "individual") {
             $xe9 = "0.00";
             $xea = $x10["LBL_TOTAL_AFTER_DISCOUNT"] . " = {$xe5} \\n";
             $xeb = getTaxDetailsForProduct($xd5, "all");
             $xec = array();
             for ($xed = 0; $xed < $x161($xeb); $xed++) {
                 $xee = $xeb[$xed]["taxname"];
                 $xef = $xeb[$xed]["taxlabel"];
                 $xf0 = getInventoryProductTaxValue($x0e->id, $xd5, $xee);
                 $xf1 = $xe5 * $xf0 / 100;
                 $xe9 = $xe9 + $xf1;
                 if ($xee != "" && $xf0 > 0) {
                     $xf2[$xee . "-" . $xf0]["label"] = $xef;
                     $xf2[$xee . "-" . $xf0]["netto"] += $xe5;
                     $xf2[$xee . "-" . $xf0]["vat"] += $x16f($xf1, self::$x15);
                     $xf2[$xee . "-" . $xf0]["value"] = $xf0;
                     $x15e($xec, $xf0);
                     $x15e($xcd, $xf0);
                 }
             }
             $xe8 = $xe8 + $xe9;
             if ($x161($xec) > 0) {
                 $xf3 = $x160($xec);
             } else {
                 $xf3 = "0.00";
             }
             $xf4["P"][$x2d]["PRODUCTVATPERCENT"] = $this->x23($xf3);
             $xf4["P"][$x2d]["PRODUCTVATSUM"] = $this->x23($xe9);
         }
         if ($xd6 == "Products") {
             $xf4["P"][$x2d]["PRODUCTS_CRMID"] = $xd3;
             $xf4["P"][$x2d]["SERVICES_CRMID"] = "";
         } else {
             $xf4["P"][$x2d]["PRODUCTS_CRMID"] = "";
             $xf4["P"][$x2d]["SERVICES_CRMID"] = $xd3;
         }
         $xf4["P"][$x2d]["PS_CRMID"] = $xd3;
         $xf4["P"][$x2d]["PS_NO"] = $xd4;
         if ($xd9 != "") {
             $xd9 = $x172("\\n", "<br>", $x16d($xd9));
             $xd9 = $x165($xd9, ENT_QUOTES, self::$x11);
             $xd8 .= "<br /><small>" . $xd9 . "</small>";
         }
         $xf4["P"][$x2d]["PRODUCTNAME"] = $xd8;
         $xf4["P"][$x2d]["PRODUCTTITLE"] = $xd7;
         $xda = $x172("\\n", "<br>", $x16d($xda));
         $xf4["P"][$x2d]["PRODUCTDESCRIPTION"] = $x165($xda, ENT_QUOTES, self::$x11);
         $xf4["P"][$x2d]["PRODUCTEDITDESCRIPTION"] = $xd9;
         $xdb = $x172("\\n", "<br>", $x16d($xdb));
         $xf4["P"][$x2d]["CRMNOWPRODUCTDESCRIPTION"] = $x165($xdb, ENT_QUOTES, self::$x11);
         $xf4["P"][$x2d]["PRODUCTLISTPRICE"] = $this->x23($xe1);
         $xf4["P"][$x2d]["PRODUCTTOTAL"] = $this->x23($xe2);
         $xf4["P"][$x2d]["PRODUCTQUANTITY"] = $this->x23($xdd);
         $xf4["P"][$x2d]["PRODUCTQINSTOCK"] = $this->x23($xdc);
         $xf4["P"][$x2d]["PRODUCTPRICE"] = $this->x23($xe0);
         $xf4["P"][$x2d]["PRODUCTPOSITION"] = $x2d;
         $xf4["P"][$x2d]["PRODUCTQTYPERUNIT"] = $this->x23($xde);
         $x83 = $xdf;
         if (!$x168($x178($x83), self::$x18)) {
             $x83 = $this->x20($x83, "Products/Services");
         } else {
             $x83 = "";
         }
         $xf4["P"][$x2d]["PRODUCTUSAGEUNIT"] = $x83;
         $xf4["P"][$x2d]["PRODUCTDISCOUNT"] = $this->x23($xe6);
         $xf4["P"][$x2d]["PRODUCTDISCOUNTPERCENT"] = $this->x23($xe7);
         $xf4["P"][$x2d]["PRODUCTSTOTALAFTERDISCOUNTSUM"] = $xe5;
         $xf4["P"][$x2d]["PRODUCTSTOTALAFTERDISCOUNT"] = $this->x23($xe5);
         $xf4["P"][$x2d]["PRODUCTTOTALSUM"] = $this->x23($xe5 + $xe9);
         $xc7 += $xe5;
         $xc8 += $xe2;
         $xc9 += $xe5 + $xe9;
         $xf4["P"][$x2d]["PRODUCTSTOTALAFTERDISCOUNT_SUBTOTAL"] = $this->x23($xc7);
         $xf4["P"][$x2d]["PRODUCTTOTAL_SUBTOTAL"] = $this->x23($xc8);
         $xf4["P"][$x2d]["PRODUCTTOTALSUM_SUBTOTAL"] = $this->x23($xc9);
         $xcc[$x2d]["\$" . "TOTALAFTERDISCOUNT_SUBTOTAL\$"] = $xf4["P"][$x2d]["PRODUCTSTOTALAFTERDISCOUNT_SUBTOTAL"];
         $xcc[$x2d]["\$" . "TOTAL_SUBTOTAL\$"] = $xf4["P"][$x2d]["PRODUCTTOTAL_SUBTOTAL"];
         $xcc[$x2d]["\$" . "TOTALSUM_SUBTOTAL\$"] = $xf4["P"][$x2d]["PRODUCTTOTALSUM_SUBTOTAL"];
         $xf5 = self::$x0f->query_result($x3e, $x2d - 1, "sequence_no");
         $xf4["P"][$x2d]["PRODUCTSEQUENCE"] = $xf5;
         if (isset($xca[$xd5 . "_" . $xf5])) {
             $xf6 = "";
             $xf7 = "";
             if ($xca[$xd5 . "_" . $xf5]["width"] > 0) {
                 $xf6 = " width='" . $xca[$xd5 . "_" . $xf5]["width"] . "' ";
             }
             if ($xca[$xd5 . "_" . $xf5]["height"] > 0) {
                 $xf7 = " height='" . $xca[$xd5 . "_" . $xf5]["height"] . "' ";
             }
             $xf4["P"][$x2d]["PRODUCTS_IMAGENAME"] = "<img src='" . self::$x12 . "/" . $xca[$xd5 . "_" . $xf5]["src"] . "' " . $xf6 . $xf7 . "/>";
         } elseif (isset($xcb[$xd5 . "_" . $xf5])) {
             $xf4["P"][$x2d]["PRODUCTS_IMAGENAME"] = "<img src='" . self::$x12 . "/" . $xcb[$xd5 . "_" . $xf5]["src"] . "' width='83' />";
         }
         $xf8 = CRMEntity::getInstance("Products");
         if ($xd6 == "Products" && $xd3 != "") {
             $xf8->id = $xd3;
             $this->x24($xf8, $xd3, "Products");
         }
         $xf9 = $this->x21($x0c, $x0e);
         $xfa = $this->x10("Products", $xf8, false, $xf9);
         $xf4["P"][$x2d] = $x15f($xfa, $xf4["P"][$x2d]);
         unset($xf8);
         $xfb = CRMEntity::getInstance("Services");
         if ($xd6 == "Services" && $xd3 != "") {
             $xfb->id = $xd3;
             $this->x24($xfb, $xd3, "Services");
         }
         $xfc = $this->x10("Services", $xfb, false, $xf9);
         $xf4["P"][$x2d] = $x15f($xfc, $xf4["P"][$x2d]);
         unset($xfb);
         $xfd += $xe5;
         $xc6 = $xc6 + $xe8;
     }
     if (self::$x0c == $x0c) {
         self::$x26["subtotalsArray"] = $xcc;
     }
     $xfe = "0.00";
     $xff = "0";
     $x100 = "";
     if ($x0e->column_fields["hdnDiscountPercent"] != "0") {
         $xfe = $xc6 * $x0e->column_fields["hdnDiscountPercent"] / 100;
         $x100 = $x0e->column_fields["hdnDiscountPercent"];
     } elseif ($x0e->column_fields["hdnDiscountAmount"] != "0") {
         $xfe = $x0e->column_fields["hdnDiscountAmount"];
     }
     $xe9 = "0.00";
     if ($xc5 == "group") {
         $x101 = $xc6 - $xfe;
         $xeb = getAllTaxes("available", "", "edit", $x0e->id);
         for ($xed = 0; $xed < $x161($xeb); $xed++) {
             $xee = $xeb[$xed]["taxname"];
             $xef = $xeb[$xed]["taxlabel"];
             $xf0 = self::$x0f->query_result($x3e, 0, $xee);
             if ($xf0 == "" || $xf0 == "NULL") {
                 $xf0 = "0.00";
             }
             $x102 = ($xc6 - $xfe) * $xf0 / 100;
             $xe9 = $xe9 + $x102;
             if ($xee != "" && $xf0 > 0) {
                 $xf2[$xee]["label"] = $xef;
                 $xf2[$xee]["netto"] = $x101;
                 $xf2[$xee]["vat"] += $x102;
                 $xf2[$xee]["value"] = $xf0;
             }
             $x103 += $xf0;
         }
         $x104 = $xe9;
         foreach ($xf4["P"] as $x105 => $x106) {
             $x107 = $xf4["P"][$x105]["PRODUCTSTOTALAFTERDISCOUNTSUM"] * $x103 / 100;
             $x108 = $xf4["P"][$x105]["PRODUCTSTOTALAFTERDISCOUNTSUM"] + $x107;
             $xf4["P"][$x105]["PRODUCTVATPERCENT"] = $this->x23($x103);
             $xf4["P"][$x105]["PRODUCTVATSUM"] = $this->x23($x107);
             $xf4["P"][$x105]["PRODUCTTOTALSUM"] = $this->x23($x108);
         }
     } else {
         if ($x161($xf2) > 0) {
             foreach ($xf2 as $x109 => $x10a) {
                 $x104 += $x10a["vat"];
             }
         } else {
             $x104 = "0.00";
         }
     }
     $x10b = $x0e->column_fields["hdnS_H_Amount"] != "" ? $x0e->column_fields["hdnS_H_Amount"] : "0.00";
     $x10c = "0.00";
     $x10d = getAllTaxes("available", "sh", "edit", $x0e->id);
     for ($x10e = 0; $x10e < $x161($x10d); $x10e++) {
         $x10f = $x10d[$x10e]["taxname"];
         $x110 = $x10d[$x10e]["taxlabel"];
         $x111 = getInventorySHTaxPercent($x0e->id, $x10f);
         $x112 = $x10b * $x111 / 100;
         $x10c = $x10c + $x112;
     }
     $x113 = $xfd - $xfe;
     $x114 = $xfd - $xfe + $x104;
     $xf4["TOTAL"]["NETTOTAL"] = $this->x23($xc6);
     $xf4["TOTAL"]["TOTALWITHOUTVAT"] = $this->x23($xfd);
     $xf4["TOTAL"]["FINALDISCOUNT"] = $this->x23($xfe);
     $xf4["TOTAL"]["FINALDISCOUNTPERCENT"] = $this->x23($x100);
     $xf4["TOTAL"]["TOTALAFTERDISCOUNT"] = $this->x23($x113);
     $xf4["TOTAL"]["TAXTOTAL"] = $this->x23($x104);
     $xf4["TOTAL"]["TAXTOTALPERCENT"] = $this->x23($x103);
     $xf4["TOTAL"]["TOTALWITHVAT"] = $this->x23($x114);
     $xf4["TOTAL"]["SHTAXAMOUNT"] = $this->x23($x10b);
     $xf4["TOTAL"]["SHTAXTOTAL"] = $this->x23($x10c);
     $xf4["TOTAL"]["VATBLOCK"] = $xf2;
     return $xf4;
 }
示例#3
0
 function getAssociatedProducts($module, $focus, $seid = '')
 {
     $adb = PearDatabase::getInstance();
     $current_user = vglobal('current_user');
     $log = vglobal('log');
     $theme = vglobal('theme');
     $log->debug("Entering OSSCosts_Record_Model getAssociatedProducts(" . $module . "," . get_class($focus) . "," . $seid . "='') method ...");
     $output = '';
     $no_of_decimal_places = getCurrencyDecimalPlaces();
     $theme_path = "themes/" . $theme . "/";
     $image_path = $theme_path . "images/";
     $product_Detail = array();
     if ($module == 'OSSCosts') {
         $query = "SELECT\n\t\t\t\t\t\tcase when vtiger_products.productid != '' then vtiger_products.productname else vtiger_service.servicename end as productname,\n\t\t\t\t\t\tcase when vtiger_products.productid != '' then vtiger_products.productcode else vtiger_service.service_no end as productcode,\n\t\t\t\t\t\tcase when vtiger_products.productid != '' then vtiger_products.unit_price else vtiger_service.unit_price end as unit_price,\n\t\t\t\t\t\tcase when vtiger_products.productid != '' then vtiger_products.qtyinstock else 'NA' end as qtyinstock,\n\t\t\t\t\t\tcase when vtiger_products.productid != '' then 'Products' else 'Services' end as entitytype,\n\t\t\t\t\t\t\t\t\tvtiger_inventoryproductrel.listprice,\n\t\t\t\t\t\t\t\t\tvtiger_inventoryproductrel.description AS product_description,\n\t\t\t\t\t\t\t\t\tvtiger_inventoryproductrel.*,vtiger_crmentity.deleted,\n\t\t\t\t\t\t\t\t\tvtiger_products.usageunit,\n\t\t\t\t\t\t\t\t\tvtiger_service.service_usageunit\n\t\t\t\t\t\t\t\t\tFROM vtiger_inventoryproductrel\n\t\t\t\t\t\t\t\t\tLEFT JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_inventoryproductrel.productid\n\t\t\t\t\t\t\t\t\tLEFT JOIN vtiger_products\n\t\t\t\t\t\t\t\t\t\t\tON vtiger_products.productid=vtiger_inventoryproductrel.productid\n\t\t\t\t\t\t\t\t\tLEFT JOIN vtiger_service\n\t\t\t\t\t\t\t\t\t\t\tON vtiger_service.serviceid=vtiger_inventoryproductrel.productid\n\t\t\t\t\t\t\t\t\tWHERE id=?\n\t\t\t\t\t\t\t\t\tORDER BY sequence_no";
         $params = array($focus->id);
     } elseif ($module == 'Products') {
         $query = "SELECT\n\t\t\t\t\t\t\t\t\tvtiger_products.productid,\n\t\t\t\t\t\t\t\t\tvtiger_products.productcode,\n\t\t\t\t\t\t\t\t\tvtiger_products.productname,\n\t\t\t\t\t\t\t\t\tvtiger_products.unit_price,\n\t\t\t\t\t\t\t\t\tvtiger_products.usageunit,\n\t\t\t\t\t\t\t\t\tvtiger_products.qtyinstock,vtiger_crmentity.deleted,\n\t\t\t\t\t\t\t\t\tvtiger_crmentity.description AS product_description,\n\t\t\t\t\t\t\t\t\t'Products' AS entitytype\n\t\t\t\t\t\t\t\t\tFROM vtiger_products\n\t\t\t\t\t\t\t\t\tINNER JOIN vtiger_crmentity\n\t\t\t\t\t\t\t\t\t\t\tON vtiger_crmentity.crmid=vtiger_products.productid\n\t\t\t\t\t\t\t\t\tWHERE vtiger_crmentity.deleted=0\n\t\t\t\t\t\t\t\t\t\t\tAND productid=?";
         $params = array($seid);
     } elseif ($module == 'Services') {
         $query = "SELECT\n\t\t\t\t\t\t\t\t\tvtiger_service.serviceid AS productid,\n\t\t\t\t\t\t\t\t\t'NA' AS productcode,\n\t\t\t\t\t\t\t\t\tvtiger_service.servicename AS productname,\n\t\t\t\t\t\t\t\t\tvtiger_service.unit_price AS unit_price,\n\t\t\t\t\t\t\t\t\tvtiger_service.service_usageunit AS usageunit,\n\t\t\t\t\t\t\t\t\t'NA' AS qtyinstock,vtiger_crmentity.deleted,\n\t\t\t\t\t\t\t\t\tvtiger_crmentity.description AS product_description,\n\t\t\t\t\t\t\t\t\t'Services' AS entitytype\n\t\t\t\t\t\t\t\t\tFROM vtiger_service\n\t\t\t\t\t\t\t\t\tINNER JOIN vtiger_crmentity\n\t\t\t\t\t\t\t\t\t\t\tON vtiger_crmentity.crmid=vtiger_service.serviceid\n\t\t\t\t\t\t\t\t\tWHERE vtiger_crmentity.deleted=0\n\t\t\t\t\t\t\t\t\t\t\tAND serviceid=?";
         $params = array($seid);
     }
     $result = $adb->pquery($query, $params);
     $num_rows = $adb->num_rows($result);
     $finalTaxTotal = '0.00';
     for ($i = 1; $i <= $num_rows; $i++) {
         $deleted = $adb->query_result($result, $i - 1, 'deleted');
         $hdnProductId = $adb->query_result($result, $i - 1, 'productid');
         $hdnProductcode = $adb->query_result($result, $i - 1, 'productcode');
         $productname = $adb->query_result($result, $i - 1, 'productname');
         $productdescription = $adb->query_result($result, $i - 1, 'product_description');
         $comment = $adb->query_result($result, $i - 1, 'comment');
         $qtyinstock = $adb->query_result($result, $i - 1, 'qtyinstock');
         $qty = $adb->query_result($result, $i - 1, 'quantity');
         $unitprice = $adb->query_result($result, $i - 1, 'unit_price');
         $listprice = $adb->query_result($result, $i - 1, 'listprice');
         $entitytype = $adb->query_result($result, $i - 1, 'entitytype');
         if ($entitytype == 'Services') {
             $usageunit = vtranslate($adb->query_result($result, $i - 1, 'service_usageunit'), $entitytype);
         } else {
             $usageunit = vtranslate($adb->query_result($result, $i - 1, 'usageunit'), $entitytype);
         }
         $tax = $adb->query_result($result, $i - 1, 'tax');
         if ($deleted || !isset($deleted)) {
             $product_Detail[$i]['productDeleted' . $i] = true;
         } elseif (!$deleted) {
             $product_Detail[$i]['productDeleted' . $i] = false;
         }
         if (!empty($entitytype)) {
             $product_Detail[$i]['entityType' . $i] = $entitytype;
         }
         if ($listprice == '') {
             $listprice = $unitprice;
         }
         if ($qty == '') {
             $qty = 1;
         }
         //calculate productTotal
         $productTotal = $qty * $listprice;
         //Delete link in First column
         if ($i != 1) {
             $product_Detail[$i]['delRow' . $i] = "Del";
         }
         if (empty($focus->mode) && $seid != '') {
             $sub_prod_query = $adb->pquery("SELECT crmid as prod_id from vtiger_seproductsrel WHERE productid=? AND setype='Products'", array($seid));
         } else {
             $sub_prod_query = $adb->pquery("SELECT productid as prod_id from vtiger_inventorysubproductrel WHERE id=? AND sequence_no=?", array($focus->id, $i));
         }
         $subprodid_str = '';
         $subprodname_str = '';
         $subProductArray = array();
         if ($adb->num_rows($sub_prod_query) > 0) {
             for ($j = 0; $j < $adb->num_rows($sub_prod_query); $j++) {
                 $sprod_id = $adb->query_result($sub_prod_query, $j, 'prod_id');
                 $sprod_name = $subProductArray[] = getProductName($sprod_id);
                 $str_sep = "";
                 if ($j > 0) {
                     $str_sep = ":";
                 }
                 $subprodid_str .= $str_sep . $sprod_id;
                 $subprodname_str .= $str_sep . " - " . $sprod_name;
             }
         }
         $subprodname_str = str_replace(":", "<br>", $subprodname_str);
         $product_Detail[$i]['subProductArray' . $i] = $subProductArray;
         $product_Detail[$i]['hdnProductId' . $i] = $hdnProductId;
         $product_Detail[$i]['productName' . $i] = from_html($productname);
         /* Added to fix the issue Product Pop-up name display*/
         if ($_REQUEST['action'] == 'CreateSOPDF' || $_REQUEST['action'] == 'CreatePDF' || $_REQUEST['action'] == 'SendPDFMail') {
             $product_Detail[$i]['productName' . $i] = htmlspecialchars($product_Detail[$i]['productName' . $i]);
         }
         $product_Detail[$i]['hdnProductcode' . $i] = $hdnProductcode;
         $product_Detail[$i]['productDescription' . $i] = from_html($productdescription);
         if ($module == 'Potentials' || $module == 'Products' || $module == 'Services') {
             $product_Detail[$i]['comment' . $i] = $productdescription;
         } else {
             $product_Detail[$i]['comment' . $i] = $comment;
         }
         if ($module != 'PurchaseOrder' && $focus->object_name != 'Order') {
             $product_Detail[$i]['qtyInStock' . $i] = decimalFormat($qtyinstock);
         }
         $listprice = number_format($listprice, $no_of_decimal_places, '.', '');
         $product_Detail[$i]['qty' . $i] = decimalFormat($qty);
         $product_Detail[$i]['listPrice' . $i] = $listprice;
         $product_Detail[$i]['unitPrice' . $i] = number_format($unitprice, $no_of_decimal_places, '.', '');
         $product_Detail[$i]['usageUnit' . $i] = $usageunit;
         $product_Detail[$i]['productTotal' . $i] = $productTotal;
         $product_Detail[$i]['subproduct_ids' . $i] = $subprodid_str;
         $product_Detail[$i]['subprod_names' . $i] = $subprodname_str;
         $product_Detail[$i]['tax' . $i] = $tax;
         $discount_percent = decimalFormat($adb->query_result($result, $i - 1, 'discount_percent'));
         $discount_amount = $adb->query_result($result, $i - 1, 'discount_amount');
         $discount_amount = decimalFormat(number_format($discount_amount, $no_of_decimal_places, '.', ''));
         $discountTotal = '0.00';
         //Based on the discount percent or amount we will show the discount details
         //To avoid NaN javascript error, here we assign 0 initially to' %of price' and 'Direct Price reduction'(for Each Product)
         $product_Detail[$i]['discount_percent' . $i] = 0;
         $product_Detail[$i]['discount_amount' . $i] = 0;
         if ($discount_percent != 'NULL' && $discount_percent != '') {
             $product_Detail[$i]['discount_type' . $i] = "percentage";
             $product_Detail[$i]['discount_percent' . $i] = $discount_percent;
             $product_Detail[$i]['checked_discount_percent' . $i] = ' checked';
             $product_Detail[$i]['style_discount_percent' . $i] = ' style="visibility:visible"';
             $product_Detail[$i]['style_discount_amount' . $i] = ' style="visibility:hidden"';
             $discountTotal = $productTotal * $discount_percent / 100;
         } elseif ($discount_amount != 'NULL' && $discount_amount != '') {
             $product_Detail[$i]['discount_type' . $i] = "amount";
             $product_Detail[$i]['discount_amount' . $i] = $discount_amount;
             $product_Detail[$i]['checked_discount_amount' . $i] = ' checked';
             $product_Detail[$i]['style_discount_amount' . $i] = ' style="visibility:visible"';
             $product_Detail[$i]['style_discount_percent' . $i] = ' style="visibility:hidden"';
             $discountTotal = $discount_amount;
         } else {
             $product_Detail[$i]['checked_discount_zero' . $i] = ' checked';
         }
         $totalAfterDiscount = $productTotal - $discountTotal;
         $totalAfterDiscount = number_format($totalAfterDiscount, $no_of_decimal_places, '.', '');
         $discountTotal = number_format($discountTotal, $no_of_decimal_places, '.', '');
         $product_Detail[$i]['discountTotal' . $i] = $discountTotal;
         $product_Detail[$i]['totalAfterDiscount' . $i] = $totalAfterDiscount;
         $amount = '0.00';
         $tax_details = getTaxDetailsForProduct($hdnProductId, 'all');
         //First we should get all available taxes and then retrieve the corresponding tax values
         $allTaxes = getAllTaxes('available', '', 'edit', $focus->id);
         $taxtype = $this->getInventoryTaxType($module, $focus->id);
         for ($tax_count = 0; $tax_count < count($tax_details); $tax_count++) {
             $tax_name = $tax_details[$tax_count]['taxname'];
             $tax_label = $tax_details[$tax_count]['taxlabel'];
             $tax_value = '0.00';
             //condition to avoid this function call when create new PO/SO/Quotes/Invoice from Product module
             if ($focus->id != '') {
                 if ($taxtype == 'individual') {
                     //if individual then show the entered tax percentage
                     $tax_value = getInventoryProductTaxValue($focus->id, $hdnProductId, $tax_name);
                 } else {
                     //if group tax then we have to show the default value when change to individual tax
                     $tax_value = $tax_details[$tax_count]['percentage'];
                 }
             } else {
                 //if the above function not called then assign the default associated value of the product
                 $tax_value = $tax_details[$tax_count]['percentage'];
             }
             $product_Detail[$i]['taxes'][$tax_count]['taxname'] = $tax_name;
             $product_Detail[$i]['taxes'][$tax_count]['taxlabel'] = $tax_label;
             $product_Detail[$i]['taxes'][$tax_count]['percentage'] = $tax_value;
             $amount = $totalAfterDiscount * $tax_value / 100;
             $amount = number_format($amount, $no_of_decimal_places, '.', '');
             $product_Detail[$i]['taxes'][$tax_count]['amount'] = $amount;
             if ($tax == $tax_name) {
                 $finalTaxTotal += $amount;
                 $product_Detail[$i]['taxTotal' . $i] = $amount;
             }
         }
         if ($taxtype == 'group') {
             foreach ($allTaxes as $key => $value) {
                 if ($tax == $value['taxname']) {
                     $amount = $totalAfterDiscount * $value['percentage'] / 100;
                     $amount = number_format($amount, $no_of_decimal_places, '.', '');
                     $product_Detail[$i]['taxes'][$tax]['amount'] = $amount;
                     $finalTaxTotal += $amount;
                     $product_Detail[$i]['taxTotal' . $i] = $amount;
                 }
             }
         }
         //Calculate netprice
         $netPrice = $totalAfterDiscount + number_format($product_Detail[$i]['taxTotal' . $i], $no_of_decimal_places, '.', '');
         //if condition is added to call this function when we create PO/SO/Quotes/Invoice from Product module
         $product_Detail[$i]['netPrice' . $i] = $netPrice;
     }
     //set the taxtype
     $product_Detail[1]['final_details']['taxtype'] = $taxtype;
     //Get the Final Discount, S&H charge, Tax for S&H  values
     //To set the Final Discount details
     $finalDiscount = '0.00';
     $product_Detail[1]['final_details']['discount_type_final'] = 'zero';
     $subTotal = $focus->column_fields['hdnSubTotal'] != '' ? $focus->column_fields['hdnSubTotal'] : '0.00';
     $subTotal = number_format($subTotal, $no_of_decimal_places, '.', '');
     $product_Detail[1]['final_details']['hdnSubTotal'] = $subTotal;
     $discountPercent = $focus->column_fields['hdnDiscountPercent'] != '' ? $focus->column_fields['hdnDiscountPercent'] : '0.00';
     $discountAmount = $focus->column_fields['hdnDiscountAmount'] != '' ? $focus->column_fields['hdnDiscountAmount'] : '0.00';
     if ($discountPercent != '0') {
         $discountAmount = $product_Detail[1]['final_details']['hdnSubTotal'] * $discountPercent / 100;
     }
     //To avoid NaN javascript error, here we assign 0 initially to' %of price' and 'Direct Price reduction'(For Final Discount)
     $discount_amount_final = '0.00';
     $discount_amount_final = number_format($discount_amount_final, $no_of_decimal_places, '.', '');
     $product_Detail[1]['final_details']['discount_percentage_final'] = 0;
     $product_Detail[1]['final_details']['discount_amount_final'] = $discount_amount_final;
     if ($focus->column_fields['hdnDiscountPercent'] != '0') {
         $finalDiscount = $subTotal * $discountPercent / 100;
         $product_Detail[1]['final_details']['discount_type_final'] = 'percentage';
         $product_Detail[1]['final_details']['discount_percentage_final'] = $discountPercent;
         $product_Detail[1]['final_details']['checked_discount_percentage_final'] = ' checked';
         $product_Detail[1]['final_details']['style_discount_percentage_final'] = ' style="visibility:visible"';
         $product_Detail[1]['final_details']['style_discount_amount_final'] = ' style="visibility:hidden"';
     } elseif ($focus->column_fields['hdnDiscountAmount'] != '0' && $focus->column_fields['hdnDiscountAmount'] != '') {
         $finalDiscount = $focus->column_fields['hdnDiscountAmount'];
         $product_Detail[1]['final_details']['discount_type_final'] = 'amount';
         $product_Detail[1]['final_details']['discount_amount_final'] = $discountAmount;
         $product_Detail[1]['final_details']['checked_discount_amount_final'] = ' checked';
         $product_Detail[1]['final_details']['style_discount_amount_final'] = ' style="visibility:visible"';
         $product_Detail[1]['final_details']['style_discount_percentage_final'] = ' style="visibility:hidden"';
     }
     $finalDiscount = number_format($finalDiscount, $no_of_decimal_places, '.', '');
     $product_Detail[1]['final_details']['discountTotal_final'] = $finalDiscount;
     //To set the Final Tax values
     //we will get all taxes. if individual then show the product related taxes only else show all taxes
     //suppose user want to change individual to group or vice versa in edit time the we have to show all taxes. so that here we will store all the taxes and based on need we will show the corresponding taxes
     for ($tax_count = 0; $tax_count < count($allTaxes); $tax_count++) {
         $tax_name = $allTaxes[$tax_count]['taxname'];
         $tax_label = $allTaxes[$tax_count]['taxlabel'];
         //if taxtype is individual and want to change to group during edit time then we have to show the all available taxes and their default values
         //Also taxtype is group and want to change to individual during edit time then we have to provide the asspciated taxes and their default tax values for individual products
         if ($taxtype == 'group') {
             $tax_percent = $adb->query_result($result, 0, $tax_name);
         } else {
             $tax_percent = $allTaxes[$tax_count]['percentage'];
         }
         //$adb->query_result($result,0,$tax_name);
         if ($tax_percent == '' || $tax_percent == 'NULL') {
             $tax_percent = '0.00';
         }
         $taxamount = ($subTotal - $finalDiscount) * $tax_percent / 100;
         $taxamount = number_format($taxamount, $no_of_decimal_places, '.', '');
         $product_Detail[1]['final_details']['taxes'][$tax_count]['taxname'] = $tax_name;
         $product_Detail[1]['final_details']['taxes'][$tax_count]['taxlabel'] = $tax_label;
         $product_Detail[1]['final_details']['taxes'][$tax_count]['percentage'] = $tax_percent;
         $product_Detail[1]['final_details']['taxes'][$tax_count]['amount'] = $taxamount;
     }
     $product_Detail[1]['final_details']['tax_totalamount'] = $finalTaxTotal;
     $product_Detail[1]['final_details']['tax'] = $tax;
     //To set the grand total
     $grandTotal = $focus->column_fields['hdnGrandTotal'] != '' ? $focus->column_fields['hdnGrandTotal'] : '0.00';
     $grandTotal = number_format($grandTotal, $no_of_decimal_places, '.', '');
     $product_Detail[1]['final_details']['grandTotal'] = $grandTotal;
     $log->debug("Exiting OSSCosts_Record_Model getAssociatedProducts method ...");
     return $product_Detail;
 }
示例#4
0
/** This function returns the detailed list of vtiger_products associated to a given entity or a record.
* Param $module - module name
* Param $focus - module object
* Param $seid - sales entity id
* Return type is an object array
*/
function getAssociatedProducts($module, $focus, $seid = '')
{
    global $log, $adb, $theme, $current_user;
    $log->debug("Entering getAssociatedProducts(" . $module . "," . get_class($focus) . "," . $seid . "='') method ...");
    $theme_path = "themes/" . $theme . "/";
    $image_path = $theme_path . "images/";
    $product_Detail = array();
    if ($module == 'Quotes' || $module == 'PurchaseOrder' || $module == 'SalesOrder' || $module == 'Invoice') {
        $query = "SELECT\n\t\t\tcase when vtiger_products.productid != '' then vtiger_products.productname else vtiger_service.servicename end as productname,\n\t\t\tcase when vtiger_products.productid != '' then vtiger_products.productcode else vtiger_service.service_no end as productcode,\n\t\t\tcase when vtiger_products.productid != '' then vtiger_products.unit_price else vtiger_service.unit_price end as unit_price,\n\t\t\tcase when vtiger_products.productid != '' then vtiger_products.qtyinstock else 'NA' end as qtyinstock,\n\t\t\tcase when vtiger_products.productid != '' then 'Products' else 'Services' end as entitytype,\n\t\t\tvtiger_inventoryproductrel.listprice,\n\t\t\tvtiger_inventoryproductrel.description AS product_description,\n\t\t\tvtiger_inventoryproductrel.*\n\t\t\tFROM vtiger_inventoryproductrel\n\t\t\tLEFT JOIN vtiger_products ON vtiger_products.productid=vtiger_inventoryproductrel.productid\n\t\t\tLEFT JOIN vtiger_service ON vtiger_service.serviceid=vtiger_inventoryproductrel.productid\n\t\t\tWHERE id=? ORDER BY sequence_no";
        $params = array($focus->id);
        if ($module != 'PurchaseOrder') {
            if (GlobalVariable::getVariable('B2B', '1') == '1') {
                $acvid = $focus->column_fields['account_id'];
            } else {
                $acvid = $focus->column_fields['contact_id'];
            }
        } else {
            $acvid = $focus->column_fields['vendor_id'];
        }
    } elseif ($module == 'Potentials') {
        $query = "SELECT vtiger_products.productid, vtiger_products.productname, vtiger_products.productcode,\n\t\t\tvtiger_products.unit_price, vtiger_products.qtyinstock, vtiger_crmentity.description AS product_description,\n\t\t\t'Products' AS entitytype\n\t\t\tFROM vtiger_products\n\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_products.productid\n\t\t\tINNER JOIN vtiger_seproductsrel ON vtiger_seproductsrel.productid=vtiger_products.productid\n\t\t\tWHERE vtiger_seproductsrel.crmid=?";
        $query .= " UNION SELECT vtiger_service.serviceid AS productid, vtiger_service.servicename AS productname,\n\t\t\t'NA' AS productcode, vtiger_service.unit_price AS unit_price, 'NA' AS qtyinstock,\n\t\t\tvtiger_crmentity.description AS product_description, 'Services' AS entitytype\n\t\t\tFROM vtiger_service\n\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_service.serviceid\n\t\t\tINNER JOIN vtiger_crmentityrel ON vtiger_crmentityrel.relcrmid=vtiger_service.serviceid\n\t\t\tWHERE vtiger_crmentityrel.crmid=?";
        $params = array($seid, $seid);
    } elseif ($module == 'Products') {
        $query = "SELECT vtiger_products.productid, vtiger_products.productcode, vtiger_products.productname,\n\t\t\tvtiger_products.unit_price, vtiger_products.qtyinstock, vtiger_crmentity.description AS product_description,\n\t\t\t'Products' AS entitytype\n\t\t\tFROM vtiger_products\n\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_products.productid\n\t\t\tWHERE vtiger_crmentity.deleted=0 AND productid=?";
        $params = array($seid);
    } elseif ($module == 'Services') {
        $query = "SELECT vtiger_service.serviceid AS productid, 'NA' AS productcode, vtiger_service.servicename AS productname,\n\t\t\tvtiger_service.unit_price AS unit_price, 'NA' AS qtyinstock, vtiger_crmentity.description AS product_description,\n\t\t\t'Services' AS entitytype\n\t\t\tFROM vtiger_service\n\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_service.serviceid\n\t\t\tWHERE vtiger_crmentity.deleted=0 AND serviceid=?";
        $params = array($seid);
    }
    $cbMap = cbMap::getMapByName($module . 'InventoryDetails', 'MasterDetailLayout');
    $MDMapFound = $cbMap != null;
    if ($MDMapFound) {
        $cbMapFields = $cbMap->MasterDetailLayout();
    }
    $result = $adb->pquery($query, $params);
    $num_rows = $adb->num_rows($result);
    for ($i = 1; $i <= $num_rows; $i++) {
        $hdnProductId = $adb->query_result($result, $i - 1, 'productid');
        $hdnProductcode = $adb->query_result($result, $i - 1, 'productcode');
        $productname = $adb->query_result($result, $i - 1, 'productname');
        $productdescription = $adb->query_result($result, $i - 1, 'product_description');
        $comment = $adb->query_result($result, $i - 1, 'comment');
        $qtyinstock = $adb->query_result($result, $i - 1, 'qtyinstock');
        $qty = $adb->query_result($result, $i - 1, 'quantity');
        $unitprice = $adb->query_result($result, $i - 1, 'unit_price');
        $listprice = $adb->query_result($result, $i - 1, 'listprice');
        $entitytype = $adb->query_result($result, $i - 1, 'entitytype');
        if (!empty($entitytype)) {
            $product_Detail[$i]['entityType' . $i] = $entitytype;
        }
        $product_Detail[$i]['lineitem_id' . $i] = $adb->query_result($result, $i - 1, 'lineitem_id');
        if ($listprice == '') {
            $listprice = $unitprice;
        }
        if ($qty == '') {
            $qty = 1;
        }
        //calculate productTotal
        $productTotal = $qty * $listprice;
        //Delete link in First column
        if ($i != 1) {
            $product_Detail[$i]['delRow' . $i] = "Del";
        }
        if (empty($focus->mode) && $seid != '') {
            $sub_prod_query = $adb->pquery("SELECT crmid as prod_id from vtiger_seproductsrel WHERE productid=? AND setype='Products'", array($seid));
        } else {
            $sub_prod_query = $adb->pquery("SELECT productid as prod_id from vtiger_inventorysubproductrel WHERE id=? AND sequence_no=?", array($focus->id, $i));
        }
        $subprodid_str = '';
        $subprodname_str = '';
        $subProductArray = array();
        if ($adb->num_rows($sub_prod_query) > 0) {
            for ($j = 0; $j < $adb->num_rows($sub_prod_query); $j++) {
                $sprod_id = $adb->query_result($sub_prod_query, $j, 'prod_id');
                $sprod_name = $subProductArray[] = getProductName($sprod_id);
                $str_sep = "";
                if ($j > 0) {
                    $str_sep = ":";
                }
                $subprodid_str .= $str_sep . $sprod_id;
                $subprodname_str .= $str_sep . " - " . $sprod_name;
            }
        }
        $subprodname_str = str_replace(":", "<br>", $subprodname_str);
        $product_Detail[$i]['subProductArray' . $i] = $subProductArray;
        $product_Detail[$i]['hdnProductId' . $i] = $hdnProductId;
        $product_Detail[$i]['productName' . $i] = from_html($productname);
        /* Added to fix the issue Product Pop-up name display*/
        if ($_REQUEST['action'] == 'CreateSOPDF' || $_REQUEST['action'] == 'CreatePDF' || $_REQUEST['action'] == 'SendPDFMail') {
            $product_Detail[$i]['productName' . $i] = htmlspecialchars($product_Detail[$i]['productName' . $i]);
        }
        $product_Detail[$i]['hdnProductcode' . $i] = $hdnProductcode;
        $product_Detail[$i]['productDescription' . $i] = from_html($productdescription);
        if ($module == 'Potentials' || $module == 'Products' || $module == 'Services') {
            $product_Detail[$i]['comment' . $i] = $productdescription;
        } else {
            $product_Detail[$i]['comment' . $i] = $comment;
        }
        if ($MDMapFound) {
            foreach ($cbMapFields['detailview']['fields'] as $mdfield) {
                $mdrs = $adb->pquery('select ' . $mdfield['fieldinfo']['name'] . ' from vtiger_inventorydetails
						inner join vtiger_crmentity on crmid=vtiger_inventorydetails.inventorydetailsid
						inner join vtiger_inventorydetailscf on vtiger_inventorydetailscf.inventorydetailsid=vtiger_inventorydetails.inventorydetailsid
						where deleted=0 and related_to=? and lineitem_id=?', array($focus->id, $adb->query_result($result, $i - 1, 'lineitem_id')));
                if ($mdrs) {
                    $col_fields = array();
                    $col_fields[$mdfield['fieldinfo']['name']] = $adb->query_result($mdrs, 0, 0);
                    $foutput = getOutputHtml($mdfield['fieldinfo']['uitype'], $mdfield['fieldinfo']['name'], $mdfield['fieldinfo']['label'], 100, $col_fields, 0, 'InventoryDetails', 'edit', $mdfield['fieldinfo']['typeofdata']);
                    $product_Detail[$i]['moreinfo' . $i][] = $foutput;
                }
            }
        }
        if ($module != 'PurchaseOrder' && $focus->object_name != 'Order') {
            $product_Detail[$i]['qtyInStock' . $i] = $qtyinstock;
        }
        $qty = number_format($qty, 2, '.', '');
        //Convert to 2 decimals
        $listprice = number_format($listprice, 2, '.', '');
        //Convert to 2 decimals
        $product_Detail[$i]['qty' . $i] = $qty;
        $product_Detail[$i]['listPrice' . $i] = $listprice;
        $product_Detail[$i]['unitPrice' . $i] = $unitprice;
        $product_Detail[$i]['productTotal' . $i] = $productTotal;
        $product_Detail[$i]['subproduct_ids' . $i] = $subprodid_str;
        $product_Detail[$i]['subprod_names' . $i] = $subprodname_str;
        $discount_percent = $adb->query_result($result, $i - 1, 'discount_percent');
        $discount_amount = $adb->query_result($result, $i - 1, 'discount_amount');
        $discount_amount = number_format(is_numeric($discount_amount) ? $discount_amount : 0, 2, '.', '');
        //Convert to 2 decimals
        $discountTotal = '0.00';
        //Based on the discount percent or amount we will show the discount details
        //To avoid NaN javascript error, here we assign 0 initially to' %of price' and 'Direct Price reduction'(for Each Product)
        $product_Detail[$i]['discount_percent' . $i] = 0;
        $product_Detail[$i]['discount_amount' . $i] = 0;
        if ($discount_percent != 'NULL' && $discount_percent != '') {
            $product_Detail[$i]['discount_type' . $i] = "percentage";
            $product_Detail[$i]['discount_percent' . $i] = $discount_percent;
            $product_Detail[$i]['checked_discount_percent' . $i] = ' checked';
            $product_Detail[$i]['style_discount_percent' . $i] = ' style="visibility:visible"';
            $product_Detail[$i]['style_discount_amount' . $i] = ' style="visibility:hidden"';
            $discountTotal = $productTotal * $discount_percent / 100;
        } elseif ($discount_amount != 'NULL' && $discount_amount != '') {
            $product_Detail[$i]['discount_type' . $i] = "amount";
            $product_Detail[$i]['discount_amount' . $i] = $discount_amount;
            $product_Detail[$i]['checked_discount_amount' . $i] = ' checked';
            $product_Detail[$i]['style_discount_amount' . $i] = ' style="visibility:visible"';
            $product_Detail[$i]['style_discount_percent' . $i] = ' style="visibility:hidden"';
            $discountTotal = $discount_amount;
        } else {
            $product_Detail[$i]['checked_discount_zero' . $i] = ' checked';
        }
        $totalAfterDiscount = $productTotal - $discountTotal;
        $product_Detail[$i]['discountTotal' . $i] = $discountTotal;
        $product_Detail[$i]['totalAfterDiscount' . $i] = $totalAfterDiscount;
        $taxTotal = '0.00';
        $product_Detail[$i]['taxTotal' . $i] = $taxTotal;
        //Calculate netprice
        $netPrice = $totalAfterDiscount + $taxTotal;
        //if condition is added to call this function when we create PO/SO/Quotes/Invoice from Product module
        if ($module == 'PurchaseOrder' || $module == 'SalesOrder' || $module == 'Quotes' || $module == 'Invoice') {
            $taxtype = getInventoryTaxType($module, $focus->id);
            if ($taxtype == 'individual') {
                //Add the tax with product total and assign to netprice
                $netPrice = $netPrice + $taxTotal;
            }
        }
        $product_Detail[$i]['netPrice' . $i] = $netPrice;
        //First we will get all associated taxes as array
        $tax_details = getTaxDetailsForProduct($hdnProductId, 'all', $acvid);
        //Now retrieve the tax values from the current query with the name
        for ($tax_count = 0; $tax_count < count($tax_details); $tax_count++) {
            $tax_name = $tax_details[$tax_count]['taxname'];
            $tax_label = $tax_details[$tax_count]['taxlabel'];
            $tax_value = '0.00';
            //condition to avoid this function call when create new PO/SO/Quotes/Invoice from Product module
            if ($focus->id != '') {
                if ($taxtype == 'individual') {
                    //if individual then show the entered tax percentage
                    $tax_value = getInventoryProductTaxValue($focus->id, $hdnProductId, $tax_name);
                } else {
                    //if group tax then we have to show the default value when change to individual tax
                    $tax_value = $tax_details[$tax_count]['percentage'];
                }
            } else {
                //if the above function not called then assign the default associated value of the product
                $tax_value = $tax_details[$tax_count]['percentage'];
            }
            $product_Detail[$i]['taxes'][$tax_count]['taxname'] = $tax_name;
            $product_Detail[$i]['taxes'][$tax_count]['taxlabel'] = $tax_label;
            $product_Detail[$i]['taxes'][$tax_count]['percentage'] = $tax_value;
        }
    }
    //set the taxtype
    $product_Detail[1]['final_details']['taxtype'] = $taxtype;
    //Get the Final Discount, S&H charge, Tax for S&H and Adjustment values
    //To set the Final Discount details
    $finalDiscount = '0.00';
    $product_Detail[1]['final_details']['discount_type_final'] = 'zero';
    $subTotal = $focus->column_fields['hdnSubTotal'] != '' ? $focus->column_fields['hdnSubTotal'] : '0.00';
    $subTotal = number_format($subTotal, 2, '.', '');
    //Convert to 2 decimals
    $product_Detail[1]['final_details']['hdnSubTotal'] = $subTotal;
    $discountPercent = $focus->column_fields['hdnDiscountPercent'] != '' ? $focus->column_fields['hdnDiscountPercent'] : '0.00';
    $discountAmount = $focus->column_fields['hdnDiscountAmount'] != '' ? $focus->column_fields['hdnDiscountAmount'] : '0.00';
    $discountAmount = number_format($discountAmount, 2, '.', '');
    //Convert to 2 decimals
    //To avoid NaN javascript error, here we assign 0 initially to' %of price' and 'Direct Price reduction'(For Final Discount)
    $product_Detail[1]['final_details']['discount_percentage_final'] = 0;
    $product_Detail[1]['final_details']['discount_amount_final'] = 0;
    if ($focus->column_fields['hdnDiscountPercent'] != '0') {
        $finalDiscount = $subTotal * $discountPercent / 100;
        $product_Detail[1]['final_details']['discount_type_final'] = 'percentage';
        $product_Detail[1]['final_details']['discount_percentage_final'] = $discountPercent;
        $product_Detail[1]['final_details']['checked_discount_percentage_final'] = ' checked';
        $product_Detail[1]['final_details']['style_discount_percentage_final'] = ' style="visibility:visible"';
        $product_Detail[1]['final_details']['style_discount_amount_final'] = ' style="visibility:hidden"';
    } elseif ($focus->column_fields['hdnDiscountAmount'] != '0') {
        $finalDiscount = $focus->column_fields['hdnDiscountAmount'];
        $product_Detail[1]['final_details']['discount_type_final'] = 'amount';
        $product_Detail[1]['final_details']['discount_amount_final'] = $discountAmount;
        $product_Detail[1]['final_details']['checked_discount_amount_final'] = ' checked';
        $product_Detail[1]['final_details']['style_discount_amount_final'] = ' style="visibility:visible"';
        $product_Detail[1]['final_details']['style_discount_percentage_final'] = ' style="visibility:hidden"';
    }
    $finalDiscount = number_format($finalDiscount, 2, '.', '');
    //Convert to 2 decimals
    $product_Detail[1]['final_details']['discountTotal_final'] = $finalDiscount;
    //To set the Final Tax values
    //we will get all taxes. if individual then show the product related taxes only else show all taxes
    //suppose user want to change individual to group or vice versa in edit time the we have to show all taxes. so that here we will store all the taxes and based on need we will show the corresponding taxes
    $taxtotal = '0.00';
    //First we should get all available taxes and then retrieve the corresponding tax values
    $tax_details = getAllTaxes('available', '', 'edit', $focus->id);
    $ipr_cols = $adb->getColumnNames('vtiger_inventoryproductrel');
    for ($tax_count = 0; $tax_count < count($tax_details); $tax_count++) {
        $tax_name = $tax_details[$tax_count]['taxname'];
        $tax_label = $tax_details[$tax_count]['taxlabel'];
        //if taxtype is individual and want to change to group during edit time then we have to show the all available taxes and their default values
        //Also taxtype is group and want to change to individual during edit time then we have to provide the asspciated taxes and their default tax values for individual products
        if ($taxtype == 'group') {
            if (in_array($tax_name, $ipr_cols)) {
                $tax_percent = $adb->query_result($result, 0, $tax_name);
            } else {
                $tax_percent = $tax_details[$tax_count]['percentage'];
            }
        } else {
            $tax_percent = $tax_details[$tax_count]['percentage'];
        }
        //$adb->query_result($result,0,$tax_name);
        if ($tax_percent == '' || $tax_percent == 'NULL') {
            $tax_percent = '0.00';
        }
        $taxamount = ($subTotal - $finalDiscount) * $tax_percent / 100;
        $taxamount = number_format($taxamount, 2, '.', '');
        //Convert to 2 decimals
        $taxtotal = $taxtotal + $taxamount;
        $product_Detail[1]['final_details']['taxes'][$tax_count]['taxname'] = $tax_name;
        $product_Detail[1]['final_details']['taxes'][$tax_count]['taxlabel'] = $tax_label;
        $product_Detail[1]['final_details']['taxes'][$tax_count]['percentage'] = $tax_percent;
        $product_Detail[1]['final_details']['taxes'][$tax_count]['amount'] = $taxamount;
    }
    $product_Detail[1]['final_details']['tax_totalamount'] = $taxtotal;
    //To set the Shipping & Handling charge
    $shCharge = $focus->column_fields['hdnS_H_Amount'] != '' ? $focus->column_fields['hdnS_H_Amount'] : '0.00';
    $shCharge = number_format($shCharge, 2, '.', '');
    //Convert to 2 decimals
    $product_Detail[1]['final_details']['shipping_handling_charge'] = $shCharge;
    //To set the Shipping & Handling tax values
    //calculate S&H tax
    $shtaxtotal = '0.00';
    //First we should get all available taxes and then retrieve the corresponding tax values
    $shtax_details = getAllTaxes('available', 'sh', 'edit', $focus->id);
    //if taxtype is group then the tax should be same for all products in vtiger_inventoryproductrel table
    for ($shtax_count = 0; $shtax_count < count($shtax_details); $shtax_count++) {
        $shtax_name = $shtax_details[$shtax_count]['taxname'];
        $shtax_label = $shtax_details[$shtax_count]['taxlabel'];
        $shtax_percent = '0.00';
        //if condition is added to call this function when we create PO/SO/Quotes/Invoice from Product module
        if ($module == 'PurchaseOrder' || $module == 'SalesOrder' || $module == 'Quotes' || $module == 'Invoice') {
            $shtax_percent = getInventorySHTaxPercent($focus->id, $shtax_name);
        }
        $shtaxamount = $shCharge * $shtax_percent / 100;
        $shtaxtotal = $shtaxtotal + $shtaxamount;
        $product_Detail[1]['final_details']['sh_taxes'][$shtax_count]['taxname'] = $shtax_name;
        $product_Detail[1]['final_details']['sh_taxes'][$shtax_count]['taxlabel'] = $shtax_label;
        $product_Detail[1]['final_details']['sh_taxes'][$shtax_count]['percentage'] = $shtax_percent;
        $product_Detail[1]['final_details']['sh_taxes'][$shtax_count]['amount'] = $shtaxamount;
    }
    $product_Detail[1]['final_details']['shtax_totalamount'] = $shtaxtotal;
    //To set the Adjustment value
    $adjustment = $focus->column_fields['txtAdjustment'] != '' ? $focus->column_fields['txtAdjustment'] : '0.00';
    $adjustment = number_format($adjustment, 2, '.', '');
    //Convert to 2 decimals
    $product_Detail[1]['final_details']['adjustment'] = $adjustment;
    //To set the grand total
    $grandTotal = $focus->column_fields['hdnGrandTotal'] != '' ? $focus->column_fields['hdnGrandTotal'] : '0.00';
    $grandTotal = number_format($grandTotal, 2, '.', '');
    //Convert to 2 decimals
    $product_Detail[1]['final_details']['grandTotal'] = $grandTotal;
    $log->debug("Exiting getAssociatedProducts method ...");
    return $product_Detail;
}
示例#5
0
 private function x15()
 {
     global $x0b, $x0c, $x0d, $x0e, $x0f, $x10, $x11, $x12, $x13, $x14, $x15, $x16, $x17, $x18, $x19, $x1a, $x1b, $x1c, $x1d, $x1e, $x1f, $x20, $x21, $x22, $x23, $x24, $x25, $x26;
     $taxtype = $this->x1d();
     $currencytype = $this->x1c();
     $query = "select case when vtiger_products.productid != '' then vtiger_products.productname else vtiger_service.servicename end as productname," . " case when vtiger_products.productid != '' then vtiger_products.productid else vtiger_service.serviceid end as psid," . " case when vtiger_products.productid != '' then vtiger_products.product_no else vtiger_service.service_no end as psno," . " case when vtiger_products.productid != '' then 'Products' else 'Services' end as entitytype," . " case when vtiger_products.productid != '' then vtiger_products.unit_price else vtiger_service.unit_price end as unit_price," . " case when vtiger_products.productid != '' then vtiger_products.usageunit else vtiger_service.service_usageunit end as usageunit," . " case when vtiger_products.productid != '' then vtiger_products.qty_per_unit else vtiger_service.qty_per_unit end as qty_per_unit," . " case when vtiger_products.productid != '' then vtiger_products.qtyinstock else 'NA' end as qtyinstock," . " case when vtiger_products.productid != '' then c1.description else c2.description end as psdescription, vtiger_inventoryproductrel.* " . " from vtiger_inventoryproductrel" . " left join vtiger_products on vtiger_products.productid=vtiger_inventoryproductrel.productid " . " left join vtiger_crmentity as c1 on c1.crmid = vtiger_products.productid " . " left join vtiger_service on vtiger_service.serviceid=vtiger_inventoryproductrel.productid " . " left join vtiger_crmentity as c2 on c2.crmid = vtiger_service.serviceid " . " where id=? ORDER BY sequence_no";
     $result = $this->db->pquery($query, array($this->focus->id));
     $num_rows = $this->db->num_rows($result);
     $netTotal = "0.00";
     $totalAfterDiscount_subtotal = 0;
     $total_subtotal = 0;
     $totalsum_subtotal = 0;
     $images = $this->x17();
     $Total_Tax_Values = array();
     for ($i = 1; $i <= $num_rows; $i++) {
         $sub_prod_query = $this->db->pquery("SELECT productid from vtiger_inventorysubproductrel WHERE id=? AND sequence_no=?", array($this->focus->id, $i));
         $subprodname_str = '';
         if ($this->db->num_rows($sub_prod_query) > 0) {
             for ($j = 0; $j < $this->db->num_rows($sub_prod_query); $j++) {
                 $sprod_id = $this->db->query_result($sub_prod_query, $j, "productid");
                 $sprod_name = getProductName($sprod_id);
                 $str_sep = "";
                 if ($j > 0) {
                     $str_sep = ":";
                 }
                 $subprodname_str .= $str_sep . " - " . $sprod_name;
             }
         }
         $subprodname_str = $x1e(":", "<br>", $subprodname_str);
         $psid = $this->db->query_result($result, $i - 1, "psid");
         $psno = $this->db->query_result($result, $i - 1, "psno");
         $productid = $this->db->query_result($result, $i - 1, "productid");
         $entitytype = $this->db->query_result($result, $i - 1, "entitytype");
         $producttitle = $productname = $this->db->query_result($result, $i - 1, "productname");
         if ($subprodname_str != "") {
             $productname .= "<br/><span style='color:#C0C0C0;font-style:italic;'>" . $subprodname_str . "</span>";
         }
         $comment = $this->db->query_result($result, $i - 1, "comment");
         $psdescription = $this->db->query_result($result, $i - 1, "psdescription");
         $inventory_prodrel_desc = $this->db->query_result($result, $i - 1, "description");
         $qtyinstock = $this->db->query_result($result, $i - 1, "qtyinstock");
         $qty = $this->db->query_result($result, $i - 1, "quantity");
         $qty_per_unit = $this->db->query_result($result, $i - 1, "qty_per_unit");
         $usageunit = $this->db->query_result($result, $i - 1, "usageunit");
         $unitprice = $this->db->query_result($result, $i - 1, "unit_price");
         $listprice = $this->db->query_result($result, $i - 1, "listprice");
         $total = $qty * $listprice;
         $discount_percent = $this->db->query_result($result, $i - 1, "discount_percent");
         $discount_amount = $this->db->query_result($result, $i - 1, "discount_amount");
         $totalAfterDiscount = $total;
         $productDiscount = "0.00";
         $productDiscountPercent = "";
         if ($discount_percent != "NULL" && $discount_percent != "") {
             $productDiscount = $total * $discount_percent / 100;
             $totalAfterDiscount = $total - $productDiscount;
             $productDiscountPercent = $discount_percent;
         } elseif ($discount_amount != "NULL" && $discount_amount != "") {
             $productDiscount = $discount_amount;
             $totalAfterDiscount = $total - $productDiscount;
         }
         $netprice = $totalAfterDiscount;
         if ($taxtype == "individual") {
             $taxtotal = "0.00";
             $tax_info_message = $mod_strings["LBL_TOTAL_AFTER_DISCOUNT"] . " = {$totalAfterDiscount} \\n";
             $tax_details = getTaxDetailsForProduct($productid, "all");
             $Tax_Values = array();
             for ($tax_count = 0; $tax_count < $x0f($tax_details); $tax_count++) {
                 $tax_name = $tax_details[$tax_count]["taxname"];
                 $tax_label = $tax_details[$tax_count]["taxlabel"];
                 $tax_value = getInventoryProductTaxValue($this->focus->id, $productid, $tax_name);
                 $individual_taxamount = $totalAfterDiscount * $tax_value / 100;
                 $taxtotal = $taxtotal + $individual_taxamount;
                 if ($tax_name != "" && $tax_value > 0) {
                     $vatblock[$tax_name . "-" . $tax_value]["label"] = $tax_label;
                     $vatblock[$tax_name . "-" . $tax_value]["netto"] += $totalAfterDiscount;
                     $vatblock[$tax_name . "-" . $tax_value]["vat"] += $x1c($individual_taxamount, $this->decimals);
                     $vatblock[$tax_name . "-" . $tax_value]["value"] = $tax_value;
                     $x0c($Tax_Values, $tax_value);
                     $x0c($Total_Tax_Values, $tax_value);
                 }
                 $netprice = $netprice + $taxtotal;
             }
             if ($x0f($Tax_Values) > 0) {
                 $tax_avg_value = $x0e($Tax_Values);
             } else {
                 $tax_avg_value = "0.00";
             }
             $Details["P"][$i]["PRODUCTVATPERCENT"] = $this->x1e($tax_avg_value);
             $Details["P"][$i]["PRODUCTVATSUM"] = $this->x1e($taxtotal);
         }
         if ($entitytype == "Products") {
             $Details["P"][$i]["PRODUCTS_CRMID"] = $psid;
             $Details["P"][$i]["SERVICES_CRMID"] = "";
         } else {
             $Details["P"][$i]["PRODUCTS_CRMID"] = "";
             $Details["P"][$i]["SERVICES_CRMID"] = $psid;
         }
         $Details["P"][$i]["PS_CRMID"] = $psid;
         $Details["P"][$i]["PS_NO"] = $psno;
         if ($comment != "") {
             $comment = $x1e("\\n", "<br>", $x1a($comment));
             $comment = $x14($comment, ENT_QUOTES, "utf-8");
             $productname .= "<br /><small>" . $comment . "</small>";
         }
         $Details["P"][$i]["PRODUCTNAME"] = $productname;
         $Details["P"][$i]["PRODUCTTITLE"] = $producttitle;
         $psdescription = $x1e("\\n", "<br>", $x1a($psdescription));
         $Details["P"][$i]["PRODUCTDESCRIPTION"] = $x14($psdescription, ENT_QUOTES, "utf-8");
         $Details["P"][$i]["PRODUCTEDITDESCRIPTION"] = $comment;
         $inventory_prodrel_desc = $x1e("\\n", "<br>", $x1a($inventory_prodrel_desc));
         $Details["P"][$i]["CRMNOWPRODUCTDESCRIPTION"] = $x14($inventory_prodrel_desc, ENT_QUOTES, "utf-8");
         $Details["P"][$i]["PRODUCTLISTPRICE"] = $this->x1e($listprice);
         $Details["P"][$i]["PRODUCTTOTAL"] = $this->x1e($total);
         $Details["P"][$i]["PRODUCTQUANTITY"] = $this->x1e($qty);
         $Details["P"][$i]["PRODUCTQINSTOCK"] = $this->x1e($qtyinstock);
         $Details["P"][$i]["PRODUCTPRICE"] = $this->x1e($unitprice);
         $Details["P"][$i]["PRODUCTPOSITION"] = $i;
         $Details["P"][$i]["PRODUCTQTYPERUNIT"] = $this->x1e($qty_per_unit);
         $Details["P"][$i]["PRODUCTUSAGEUNIT"] = $this->x1b($usageunit, "Products/Services");
         $Details["P"][$i]["PRODUCTDISCOUNT"] = $this->x1e($productDiscount);
         $Details["P"][$i]["PRODUCTDISCOUNTPERCENT"] = $this->x1e($productDiscountPercent);
         $Details["P"][$i]["PRODUCTSTOTALAFTERDISCOUNTSUM"] = $totalAfterDiscount;
         $Details["P"][$i]["PRODUCTSTOTALAFTERDISCOUNT"] = $this->x1e($totalAfterDiscount);
         $Details["P"][$i]["PRODUCTTOTALSUM"] = $this->x1e($totalAfterDiscount + $taxtotal);
         $totalAfterDiscount_subtotal += $totalAfterDiscount;
         $total_subtotal += $total;
         $totalsum_subtotal += $totalAfterDiscount + $taxtotal;
         $Details["P"][$i]["PRODUCTSTOTALAFTERDISCOUNT_SUBTOTAL"] = $this->x1e($totalAfterDiscount_subtotal);
         $Details["P"][$i]["PRODUCTTOTAL_SUBTOTAL"] = $this->x1e($total_subtotal);
         $Details["P"][$i]["PRODUCTTOTALSUM_SUBTOTAL"] = $this->x1e($totalsum_subtotal);
         $sequence = $this->db->query_result($result, $i - 1, "sequence_no");
         $Details["P"][$i]["PRODUCTSEQUENCE"] = $sequence;
         if (isset($images[$productid . "_" . $sequence])) {
             $width = "";
             $height = "";
             if ($images[$productid . "_" . $sequence]["width"] > 0) {
                 $width = " width='" . $images[$productid . "_" . $sequence]["width"] . "' ";
             }
             if ($images[$productid . "_" . $sequence]["height"] > 0) {
                 $height = " height='" . $images[$productid . "_" . $sequence]["height"] . "' ";
             }
             $Details["P"][$i]["PRODUCTS_IMAGENAME"] = "<img src='" . $this->site_url . "/" . $images[$productid . "_" . $sequence]["src"] . "' " . $width . $height . "/>";
         }
         $focus_p = CRMEntity::getInstance("Products");
         if ($entitytype == "Products" && $psid != "") {
             $focus_p->id = $psid;
             $this->x1f($focus_p, $psid, "Products");
         }
         $Array_P = $this->x0e("Products", $focus_p, false, true);
         $Details["P"][$i] = $x0d($Array_P, $Details["P"][$i]);
         unset($focus_p);
         $focus_s = CRMEntity::getInstance("Services");
         if ($entitytype == "Services" && $psid != "") {
             $focus_s->id = $psid;
             $this->x1f($focus_s, $psid, "Services");
         }
         $Array_S = $this->x0e("Services", $focus_s, false, true);
         $Details["P"][$i] = $x0d($Array_S, $Details["P"][$i]);
         unset($focus_s);
         $sumwithoutvat += $totalAfterDiscount;
         $netTotal = $netTotal + $netprice;
     }
     $finalDiscount = "0.00";
     $final_discount_info = "0";
     $finalDiscountPercent = "";
     if ($this->focus->column_fields["hdnDiscountPercent"] != "0") {
         $finalDiscount = $netTotal * $this->focus->column_fields["hdnDiscountPercent"] / 100;
         $finalDiscountPercent = $this->focus->column_fields["hdnDiscountPercent"];
     } elseif ($this->focus->column_fields["hdnDiscountAmount"] != "0") {
         $finalDiscount = $this->focus->column_fields["hdnDiscountAmount"];
     }
     $taxtotal = "0.00";
     if ($taxtype == "group") {
         $final_totalAfterDiscount = $netTotal - $finalDiscount;
         $tax_details = getAllTaxes("available", "", "edit", $this->focus->id);
         for ($tax_count = 0; $tax_count < $x0f($tax_details); $tax_count++) {
             $tax_name = $tax_details[$tax_count]["taxname"];
             $tax_label = $tax_details[$tax_count]["taxlabel"];
             $tax_value = $this->db->query_result($result, 0, $tax_name);
             if ($tax_value == "" || $tax_value == "NULL") {
                 $tax_value = "0.00";
             }
             $taxamount = ($netTotal - $finalDiscount) * $tax_value / 100;
             $taxtotal = $taxtotal + $taxamount;
             if ($tax_name != "" && $tax_value > 0) {
                 $vatblock[$tax_name]["label"] = $tax_label;
                 $vatblock[$tax_name]["netto"] = $final_totalAfterDiscount;
                 $vatblock[$tax_name]["vat"] += $taxamount;
                 $vatblock[$tax_name]["value"] = $tax_value;
             }
             $total_vat_percent += $tax_value;
         }
         $vat_value = $taxtotal;
         foreach ($Details["P"] as $keyP => $valueP) {
             $productvatsum = $Details["P"][$keyP]["PRODUCTSTOTALAFTERDISCOUNTSUM"] * $total_vat_percent / 100;
             $producttotalsum = $Details["P"][$keyP]["PRODUCTSTOTALAFTERDISCOUNTSUM"] + $productvatsum;
             $Details["P"][$keyP]["PRODUCTVATPERCENT"] = $this->x1e($total_vat_percent);
             $Details["P"][$keyP]["PRODUCTVATSUM"] = $this->x1e($productvatsum);
             $Details["P"][$keyP]["PRODUCTTOTALSUM"] = $this->x1e($producttotalsum);
         }
     } else {
         if ($x0f($vatblock) > 0) {
             foreach ($vatblock as $keyM => $valueM) {
                 $vat_value += $valueM["vat"];
             }
         } else {
             $vat_value = "0.00";
         }
     }
     $shAmount = $this->focus->column_fields["hdnS_H_Amount"] != "" ? $this->focus->column_fields["hdnS_H_Amount"] : "0.00";
     $shtaxtotal = "0.00";
     $shtax_details = getAllTaxes("available", "sh", "edit", $this->focus->id);
     for ($shtax_count = 0; $shtax_count < $x0f($shtax_details); $shtax_count++) {
         $shtax_name = $shtax_details[$shtax_count]["taxname"];
         $shtax_label = $shtax_details[$shtax_count]["taxlabel"];
         $shtax_percent = getInventorySHTaxPercent($this->focus->id, $shtax_name);
         $shtaxamount = $shAmount * $shtax_percent / 100;
         $shtaxtotal = $shtaxtotal + $shtaxamount;
     }
     $totalafterdiscount = $sumwithoutvat - $finalDiscount;
     $totalwithvat = $sumwithoutvat - $finalDiscount + $vat_value;
     $Details["TOTAL"]["NETTOTAL"] = $this->x1e($netTotal);
     $Details["TOTAL"]["TOTALWITHOUTVAT"] = $this->x1e($sumwithoutvat);
     $Details["TOTAL"]["FINALDISCOUNT"] = $this->x1e($finalDiscount);
     $Details["TOTAL"]["FINALDISCOUNTPERCENT"] = $this->x1e($finalDiscountPercent);
     $Details["TOTAL"]["TOTALAFTERDISCOUNT"] = $this->x1e($totalafterdiscount);
     $Details["TOTAL"]["TAXTOTAL"] = $this->x1e($vat_value);
     $Details["TOTAL"]["TAXTOTALPERCENT"] = $this->x1e($total_vat_percent);
     $Details["TOTAL"]["TOTALWITHVAT"] = $this->x1e($totalwithvat);
     $Details["TOTAL"]["SHTAXAMOUNT"] = $this->x1e($shAmount);
     $Details["TOTAL"]["SHTAXTOTAL"] = $this->x1e($shtaxtotal);
     $Details["TOTAL"]["VATBLOCK"] = $vatblock;
     return $Details;
 }
/** This function returns a HTML output of associated vtiger_products for a given entity (Quotes,Invoice,Sales order or Purchase order)
 * Param $module - module name
 * Param $focus - module object
 * Return type string
 */
function getDetailAssociatedProducts($module, $focus)
{
    global $log;
    $log->debug("Entering getDetailAssociatedProducts(" . $module . "," . get_class($focus) . ") method ...");
    global $adb, $default_charset;
    global $mod_strings;
    global $theme;
    global $log;
    global $app_strings, $current_user, $current_language;
    $theme_path = "themes/" . $theme . "/";
    $image_path = $theme_path . "images/";
    //crm-now: added to select comma or dot as numberformat
    //European Format
    if ($current_language == 'en_us') {
        //US Format
        $decimal_precision = 2;
        $decimals_separator = '.';
        $thousands_separator = ',';
    } else {
        //European Format
        $decimal_precision = 2;
        $decimals_separator = ',';
        $thousands_separator = '.';
    }
    if ($module != 'PurchaseOrder') {
        $colspan = '2';
    } else {
        $colspan = '1';
    }
    //Get the taxtype of this entity
    $taxtype = getInventoryTaxType($module, $focus->id);
    $currencytype = getInventoryCurrencyInfo($module, $focus->id);
    $output = '';
    //Header Rows
    $output .= '

	<table width="100%"  border="0" align="center" cellpadding="5" cellspacing="0" class="crmTable" id="proTab">
	   <tr valign="top">
	   	<td colspan="' . $colspan . '" class="dvInnerHeader"><b>' . $app_strings['LBL_ITEM_DETAILS'] . '</b></td>
		<td class="dvInnerHeader" align="center" colspan="2"><b>' . $app_strings['LBL_CURRENCY'] . ' : </b>' . getTranslatedCurrencyString($currencytype['currency_name']) . ' (' . $currencytype['currency_symbol'] . ')
		</td>
		<td class="dvInnerHeader" align="center" colspan="2"><b>' . $app_strings['LBL_TAX_MODE'] . ' : </b>' . $app_strings[$taxtype] . '
		</td>
	   </tr>
	   <tr valign="top">
		<td width=40% class="lvtCol"><font color="red">*</font>
			<b>' . $app_strings['LBL_ITEM_NAME'] . '</b>
		</td>';
    //Add Quantity in Stock column for SO, Quotes and Invoice
    if ($module == 'Quotes' || $module == 'SalesOrder' || $module == 'Invoice') {
        $output .= '<td width=10% class="lvtCol"><b>' . $app_strings['LBL_QTY_IN_STOCK'] . '</b></td>';
    }
    $output .= '
	
		<td width=10% class="lvtCol"><b>' . $app_strings['LBL_QTY'] . '</b></td>
		<td width=10% class="lvtCol" align="right"><b>' . $app_strings['LBL_LIST_PRICE'] . '</b></td>
		<td width=12% nowrap class="lvtCol" align="right"><b>' . $app_strings['LBL_TOTAL'] . '</b></td>
		<td width=13% valign="top" class="lvtCol" align="right"><b>' . $app_strings['LBL_NET_PRICE'] . '</b></td>
	   </tr>
	   	';
    // DG 15 Aug 2006
    // Add "ORDER BY sequence_no" to retain add order on all inventoryproductrel items
    if ($module == 'Quotes' || $module == 'PurchaseOrder' || $module == 'SalesOrder' || $module == 'Invoice') {
        $query = "select case when vtiger_products.productid != '' then vtiger_products.productname else vtiger_service.servicename end as productname," . " case when vtiger_products.productid != '' then 'Products' else 'Services' end as entitytype," . " case when vtiger_products.productid != '' then vtiger_products.unit_price else vtiger_service.unit_price end as unit_price," . " case when vtiger_products.productid != '' then vtiger_products.productcode else 'NA' end as productcode," . " case when vtiger_products.productid != '' then vtiger_products.qtyinstock else 'NA' end as qtyinstock, vtiger_inventoryproductrel.* " . " from vtiger_inventoryproductrel" . " left join vtiger_products on vtiger_products.productid=vtiger_inventoryproductrel.productid " . " left join vtiger_service on vtiger_service.serviceid=vtiger_inventoryproductrel.productid " . " where id=? ORDER BY sequence_no";
    }
    $result = $adb->pquery($query, array($focus->id));
    $num_rows = $adb->num_rows($result);
    $netTotal = '0.00';
    for ($i = 1; $i <= $num_rows; $i++) {
        $sub_prod_query = $adb->pquery("SELECT productid from vtiger_inventorysubproductrel WHERE id=? AND sequence_no=?", array($focus->id, $i));
        $subprodname_str = '';
        if ($adb->num_rows($sub_prod_query) > 0) {
            for ($j = 0; $j < $adb->num_rows($sub_prod_query); $j++) {
                $sprod_id = $adb->query_result($sub_prod_query, $j, 'productid');
                $sprod_name = getProductName($sprod_id);
                $str_sep = "";
                if ($j > 0) {
                    $str_sep = ":";
                }
                $subprodname_str .= $str_sep . " - " . $sprod_name;
            }
        }
        $subprodname_str = str_replace(":", "<br>", $subprodname_str);
        $productid = $adb->query_result($result, $i - 1, 'productid');
        $entitytype = $adb->query_result($result, $i - 1, 'entitytype');
        $productname = $adb->query_result($result, $i - 1, 'productname');
        //crm-now added to display product code and description
        $productcode = $adb->query_result($result, $i - 1, 'productcode');
        //html to utf8 conversion (necessary because stored at inventoryproductrel table)
        $productdescription = nl2br($adb->query_result($result, $i - 1, 'description'));
        $productdescription = html_entity_decode($productdescription, ENT_QUOTES, $default_charset);
        $comment = nl2br(from_html($adb->query_result($result, $i - 1, 'comment')));
        if ($subprodname_str != '') {
            $productname .= "<br/><span style='color:#C0C0C0;font-style:italic;'>" . $subprodname_str . "</span>";
        }
        //$comment=$adb->query_result($result,$i-1,'comment');
        $qtyinstock = $adb->query_result($result, $i - 1, 'qtyinstock');
        $qty = $adb->query_result($result, $i - 1, 'quantity');
        $unitprice = $adb->query_result($result, $i - 1, 'unit_price');
        $listprice = $adb->query_result($result, $i - 1, 'listprice');
        $listpriceformated = number_format($listprice, $decimal_precision, $decimals_separator, $thousands_separator);
        $total = $qty * $listprice;
        $totalformated = number_format($total, $decimal_precision, $decimals_separator, $thousands_separator);
        //Product wise Discount calculation - starts
        $discount_percent = $adb->query_result($result, $i - 1, 'discount_percent');
        $discount_amount = $adb->query_result($result, $i - 1, 'discount_amount');
        $totalAfterDiscount = $total;
        $totalAfterDiscountformated = number_format($total, $decimal_precision, $decimals_separator, $thousands_separator);
        $productDiscount = '0.00';
        $productDiscountformated = number_format($productDiscount, $decimal_precision, $decimals_separator, $thousands_separator);
        if ($discount_percent != 'NULL' && $discount_percent != '') {
            $productDiscount = $total * $discount_percent / 100;
            $productDiscountformated = number_format($productDiscount, $decimal_precision, $decimals_separator, $thousands_separator);
            $totalAfterDiscount = $total - $productDiscount;
            $totalAfterDiscountformated = number_format($totalAfterDiscount, $decimal_precision, $decimals_separator, $thousands_separator);
            //if discount is percent then show the percentage
            $discount_info_message = "{$discount_percent} % " . $app_strings['of_string'] . " {$total} = {$productDiscountformated}";
        } elseif ($discount_amount != 'NULL' && $discount_amount != '') {
            $productDiscount = $discount_amount;
            $productDiscountformated = number_format($productDiscount, $decimal_precision, $decimals_separator, $thousands_separator);
            $totalAfterDiscount = $total - $productDiscount;
            $totalAfterDiscountformated = number_format($totalAfterDiscount, $decimal_precision, $decimals_separator, $thousands_separator);
            $discount_info_message = $app_strings['LBL_DIRECT_AMOUNT_DISCOUNT'] . " = {$productDiscountformated}";
        } else {
            $discount_info_message = $app_strings['LBL_NO_DISCOUNT_FOR_THIS_LINE_ITEM'];
        }
        //Product wise Discount calculation - ends
        $netprice = $totalAfterDiscount;
        //Calculate the individual tax if taxtype is individual
        if ($taxtype == 'individual') {
            $taxtotal = '0.00';
            $tax_info_message = $app_strings['LBL_TOTAL_AFTER_DISCOUNT'] . " = {$totalAfterDiscountformated} \\n";
            $tax_details = getTaxDetailsForProduct($productid, 'all');
            for ($tax_count = 0; $tax_count < count($tax_details); $tax_count++) {
                $tax_name = $tax_details[$tax_count]['taxname'];
                $tax_label = $tax_details[$tax_count]['taxlabel'];
                $tax_value = getInventoryProductTaxValue($focus->id, $productid, $tax_name);
                $individual_taxamount = $totalAfterDiscount * $tax_value / 100;
                $individual_taxamountformated = number_format(round($individual_taxamount, 2), $decimal_precision, $decimals_separator, $thousands_separator);
                $taxtotal = $taxtotal + $individual_taxamount;
                $taxtotalformated = number_format(round($taxtotal, 2), $decimal_precision, $decimals_separator, $thousands_separator);
                $tax_info_message .= "{$tax_label} : {$tax_value} % = {$individual_taxamountformated} \\n";
            }
            $tax_info_message .= "\\n " . $app_strings['LBL_TOTAL_TAX_AMOUNT'] . " = {$taxtotalformated}";
            $netprice = $netprice + $taxtotal;
        }
        $sc_image_tag = '';
        if ($entitytype == 'Services') {
            $sc_image_tag = '<a href="index.php?module=ServiceContracts&action=EditView&service_id=' . $productid . '&return_module=' . $module . '&return_id=' . $focus->id . '">' . '<img border="0" src="' . vtiger_imageurl('handshake.gif', $theme) . '" title="' . getTranslatedString('Add Service Contract', $module) . '" style="cursor: pointer;" align="absmiddle" />' . '</a>';
        }
        //For Product Name
        $output .= '
			   <tr valign="top">
				<td class="crmTableRow small lineOnTop">
					<font color="gray">' . $productcode . '</font>
					<br><font color="black">' . $productname . '</font>
					<br><font color="gray">' . $productdescription . '</font>
					<br><font color="gray">' . $comment . '</font>
				</td>';
        //Upto this added to display the Product name and comment
        if ($module != 'PurchaseOrder') {
            $output .= '<td class="crmTableRow small lineOnTop">' . $qtyinstock . '</td>';
        }
        $output .= '<td class="crmTableRow small lineOnTop">' . $qty . '</td>';
        $output .= '
			<td class="crmTableRow small lineOnTop" align="right">
				<table width="100%" border="0" cellpadding="5" cellspacing="0">
				   <tr>
				   	<td align="right">' . $listpriceformated . '</td>
				   </tr>
				   <tr>
					   <td align="right">(-)&nbsp;<b><a href="javascript:;" onclick="alert(\'' . $discount_info_message . '\'); ">' . $app_strings['LBL_DISCOUNT'] . ' : </a></b></td>
				   </tr>
				   <tr>
				   	<td align="right" nowrap>' . $app_strings['LBL_TOTAL_AFTER_DISCOUNT'] . ' : </td>
				   </tr>';
        if ($taxtype == 'individual') {
            $output .= '
				   <tr>
					   <td align="right" nowrap>(+)&nbsp;<b><a href="javascript:;" onclick="alert(\'' . $tax_info_message . '\');">' . $app_strings['LBL_TAX'] . ' : </a></b></td>
				   </tr>';
        }
        $output .= '
				</table>
			</td>';
        $output .= '
			<td class="crmTableRow small lineOnTop" align="right">
				<table width="100%" border="0" cellpadding="5" cellspacing="0">
				   <tr><td align="right">' . $totalformated . '</td></tr>
				   <tr><td align="right">' . $productDiscountformated . '</td></tr>
				   <tr><td align="right" nowrap>' . $totalAfterDiscountformated . '</td></tr>';
        if ($taxtype == 'individual') {
            $output .= '<tr><td align="right" nowrap>' . $taxtotalformated . '</td></tr>';
        }
        $output .= '		   
				</table>
			</td>';
        $output .= '<td class="crmTableRow small lineOnTop" valign="bottom" align="right">' . number_format(round($netprice, 2), $decimal_precision, $decimals_separator, $thousands_separator) . '</td>';
        $output .= '</tr>';
        $netTotal = $netTotal + $netprice;
    }
    $output .= '</table>';
    //$netTotal should be equal to $focus->column_fields['hdnSubTotal']
    $netTotal = $focus->column_fields['hdnSubTotal'];
    $netTotalformated = number_format($netTotal, $decimal_precision, $decimals_separator, $thousands_separator);
    //Display the total, adjustment, S&H details
    $output .= '<table width="100%" border="0" cellspacing="0" cellpadding="5" class="crmTable">';
    $output .= '<tr>';
    $output .= '<td width="88%" class="crmTableRow small" align="right"><b>' . $app_strings['LBL_NET_TOTAL'] . '</td>';
    $output .= '<td width="12%" class="crmTableRow small" align="right"><b>' . $netTotalformated . '</b></td>';
    $output .= '</tr>';
    //Decide discount
    $finalDiscount = '0.00';
    $final_discount_info = '0';
    //if($focus->column_fields['hdnDiscountPercent'] != '') - previously (before changing to prepared statement) the selected option (either percent or amount) will have value and the other remains empty. So we can find the non selected item by empty check. But now with prepared statement, the non selected option stored as 0
    if ($focus->column_fields['hdnDiscountPercent'] != '0') {
        $finalDiscount = $netTotal * $focus->column_fields['hdnDiscountPercent'] / 100;
        $finalDiscountformated = number_format($finalDiscount, $decimal_precision, $decimals_separator, $thousands_separator);
        $final_discount_info = $focus->column_fields['hdnDiscountPercent'] . " % " . $app_strings['of_string'] . " {$netTotalformated} = {$finalDiscountformated}";
    } elseif ($focus->column_fields['hdnDiscountAmount'] != '0') {
        $finalDiscount = $focus->column_fields['hdnDiscountAmount'];
        $finalDiscountformated = number_format($finalDiscount, $decimal_precision, $decimals_separator, $thousands_separator);
        $final_discount_info = $finalDiscount;
    }
    //Alert the Final Discount amount even it is zero
    $final_discount_info = $app_strings['LBL_FINAL_DISCOUNT_AMOUNT'] . " = {$final_discount_info}";
    $final_discount_info = 'onclick="alert(\'' . $final_discount_info . '\');"';
    $output .= '<tr>';
    $output .= '<td align="right" class="crmTableRow small lineOnTop">(-)&nbsp;<b><a href="javascript:;" ' . $final_discount_info . '>' . $app_strings['LBL_DISCOUNT'] . '</a></b></td>';
    $output .= '<td align="right" class="crmTableRow small lineOnTop">' . $finalDiscountformated . '</td>';
    $output .= '</tr>';
    if ($taxtype == 'group') {
        $taxtotal = '0.00';
        $final_totalAfterDiscount = $netTotal - $finalDiscount;
        $final_totalAfterDiscountformated = number_format($final_totalAfterDiscount, $decimal_precision, $decimals_separator, $thousands_separator);
        $tax_info_message = $app_strings['LBL_TOTAL_AFTER_DISCOUNT'] . " = {$final_totalAfterDiscountformated} \\n";
        //First we should get all available taxes and then retrieve the corresponding tax values
        $tax_details = getAllTaxes('available', '', 'edit', $focus->id);
        //if taxtype is group then the tax should be same for all products in vtiger_inventoryproductrel table
        for ($tax_count = 0; $tax_count < count($tax_details); $tax_count++) {
            $tax_name = $tax_details[$tax_count]['taxname'];
            $tax_label = $tax_details[$tax_count]['taxlabel'];
            $tax_value = $adb->query_result($result, 0, $tax_name);
            if ($tax_value == '' || $tax_value == 'NULL') {
                $tax_value = '0.00';
            }
            $taxamount = ($netTotal - $finalDiscount) * $tax_value / 100;
            $taxamountformated = number_format($taxamount, $decimal_precision, $decimals_separator, $thousands_separator);
            $taxtotal = $taxtotal + $taxamount;
            $taxtotalformated = number_format($taxtotal, $decimal_precision, $decimals_separator, $thousands_separator);
            $tax_info_message .= "{$tax_label} : {$tax_value} % = {$taxamountformated} \\n";
        }
        $tax_info_message .= "\\n " . $app_strings['LBL_TOTAL_TAX_AMOUNT'] . " = {$taxtotalformated}";
        $output .= '<tr>';
        $output .= '<td align="right" class="crmTableRow small">(+)&nbsp;<b><a href="javascript:;" onclick="alert(\'' . $tax_info_message . '\');">' . $app_strings['LBL_TAX'] . '</a></b></td>';
        $output .= '<td align="right" class="crmTableRow small">' . $taxtotalformated . '</td>';
        $output .= '</tr>';
    }
    $shAmount = $focus->column_fields['hdnS_H_Amount'] != '' ? $focus->column_fields['hdnS_H_Amount'] : '0.00';
    $shAmountformated = number_format($shAmount, $decimal_precision, $decimals_separator, $thousands_separator);
    $output .= '<tr>';
    $output .= '<td align="right" class="crmTableRow small">(+)&nbsp;<b>' . $app_strings['LBL_SHIPPING_AND_HANDLING_CHARGES'] . '</b></td>';
    $output .= '<td align="right" class="crmTableRow small">' . $shAmountformated . '</td>';
    $output .= '</tr>';
    //calculate S&H tax
    $shtaxtotal = '0.00';
    //First we should get all available taxes and then retrieve the corresponding tax values
    $shtax_details = getAllTaxes('available', 'sh', 'edit', $focus->id);
    //if taxtype is group then the tax should be same for all products in vtiger_inventoryproductrel table
    $shtax_info_message = $app_strings['LBL_SHIPPING_AND_HANDLING_CHARGE'] . " = {$shAmountformated} \\n";
    for ($shtax_count = 0; $shtax_count < count($shtax_details); $shtax_count++) {
        $shtax_name = $shtax_details[$shtax_count]['taxname'];
        $shtax_label = $shtax_details[$shtax_count]['taxlabel'];
        $shtax_percent = getInventorySHTaxPercent($focus->id, $shtax_name);
        $shtaxamount = $shAmount * $shtax_percent / 100;
        $shtaxamountformated = number_format($shtaxamount, $decimal_precision, $decimals_separator, $thousands_separator);
        $shtaxtotal = $shtaxtotal + $shtaxamount;
        $shtaxtotalformated = number_format($shtaxtotal, $decimal_precision, $decimals_separator, $thousands_separator);
        $shtax_info_message .= "{$shtax_label} : {$shtax_percent} % = {$shtaxamountformated} \\n";
    }
    $shtax_info_message .= "\\n " . $app_strings['LBL_TOTAL_TAX_AMOUNT'] . " = {$shtaxtotalformated}";
    $output .= '<tr>';
    $output .= '<td align="right" class="crmTableRow small">(+)&nbsp;<b><a href="javascript:;" onclick="alert(\'' . $shtax_info_message . '\')">' . $app_strings['LBL_TAX_FOR_SHIPPING_AND_HANDLING'] . '</a></b></td>';
    $output .= '<td align="right" class="crmTableRow small">' . $shtaxtotalformated . '</td>';
    $output .= '</tr>';
    $adjustment = $focus->column_fields['txtAdjustment'] != '' ? $focus->column_fields['txtAdjustment'] : '0.00';
    $adjustmentformated = number_format($adjustment, $decimal_precision, $decimals_separator, $thousands_separator);
    $output .= '<tr>';
    $output .= '<td align="right" class="crmTableRow small">&nbsp;<b>' . $app_strings['LBL_ADJUSTMENT'] . '</b></td>';
    $output .= '<td align="right" class="crmTableRow small">' . $adjustmentformated . '</td>';
    $output .= '</tr>';
    $grandTotal = $focus->column_fields['hdnGrandTotal'] != '' ? $focus->column_fields['hdnGrandTotal'] : '0.00';
    $grandTotalformated = number_format($grandTotal, $decimal_precision, $decimals_separator, $thousands_separator);
    $output .= '<tr>';
    $output .= '<td align="right" class="crmTableRow small lineOnTop"><b>' . $app_strings['LBL_GRAND_TOTAL'] . '</b></td>';
    $output .= '<td align="right" class="crmTableRow small lineOnTop">' . $grandTotalformated . '</td>';
    $output .= '</tr>';
    $output .= '</table>';
    $log->debug("Exiting getDetailAssociatedProducts method ...");
    return $output;
}
/** This function returns the detailed list of vtiger_products associated to a given entity or a record.
* Param $module - module name
* Param $focus - module object
* Param $seid - sales entity id
* Return type is an object array
*/
function getAssociatedProducts($module, $focus, $seid = '')
{
    global $log;
    $log->debug("Entering getAssociatedProducts(" . $module . "," . get_class($focus) . "," . $seid . "='') method ...");
    global $adb;
    $output = '';
    global $theme, $current_user;
    $no_of_decimal_places = getCurrencyDecimalPlaces();
    $theme_path = "themes/" . $theme . "/";
    $image_path = $theme_path . "images/";
    $product_Detail = array();
    // DG 15 Aug 2006
    // Add "ORDER BY sequence_no" to retain add order on all inventoryproductrel items
    // SalesPlatform.ru begin Add Act and Consignment modules
    if ($module == 'Quotes' || $module == 'PurchaseOrder' || $module == 'SalesOrder' || $module == 'Invoice' || $module == 'Act' || $module == 'Consignment') {
        // SalesPlatform.ru begin
        $query = "SELECT \n\t\t\t\t\tcase when vtiger_products.productid != '' then vtiger_products.productname else vtiger_service.servicename end as productname,\n \t\t            case when vtiger_products.productid != '' then vtiger_products.productcode else vtiger_service.service_no end as productcode, \n\t\t\t\t\tcase when vtiger_products.productid != '' then vtiger_products.usageunit else vtiger_service.service_usageunit end as usageunit,\t\t\t\t\t\t\t\t\t\n\t\t\t\t\tcase when vtiger_products.productid != '' then vtiger_products.unit_code else vtiger_service.unit_code end as unit_code,\n\t\t\t\t\tcase when vtiger_products.productid != '' then vtiger_products.unit_price else vtiger_service.unit_price end as unit_price,\t\t\t\t\t\t\t\t\t\n\t\t\t\t\tcase when vtiger_products.productid != '' then vtiger_products.manuf_country else '--' end as manuf_country,\t\t\t\t\t\t\t\t\t\n\t\t\t\t\tcase when vtiger_products.productid != '' then vtiger_products.manuf_country_code else '--' end as manuf_country_code,\n\t\t\t\t\tcase when vtiger_products.productid != '' then vtiger_products.customs_id else '--' end as customs_id,\t\t\t\t\t\t\t\t\t\n \t\t            case when vtiger_products.productid != '' then vtiger_products.qtyinstock else 'NA' end as qtyinstock,\n \t\t            case when vtiger_products.productid != '' then 'Products' else 'Services' end as entitytype,\n \t\t                        vtiger_inventoryproductrel.listprice, \n \t\t                        vtiger_crmentity.description AS product_description, \n \t\t                        vtiger_inventoryproductrel.* , vtiger_crmentity.deleted\n                                    , attachments.attachment_id, attachments.attachment_name, attachments.attachment_path\n \t                            FROM vtiger_inventoryproductrel \n                                LEFT JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_inventoryproductrel.productid\n \t\t                        LEFT JOIN vtiger_products \n \t\t                                ON vtiger_products.productid=vtiger_inventoryproductrel.productid \n \t\t                        LEFT JOIN vtiger_service \n \t\t                                ON vtiger_service.serviceid=vtiger_inventoryproductrel.productid \n                                        LEFT JOIN (\n                                                    SELECT vtiger_attachments.attachmentsid AS attachment_id,\n                                                        vtiger_seattachmentsrel.crmid AS attachment_crmid, \n                                                        vtiger_attachments.name AS attachment_name, \n                                                        vtiger_attachments.path AS attachment_path \n                                                    FROM vtiger_seattachmentsrel, vtiger_attachments, vtiger_inventoryproductrel \n                                                    WHERE vtiger_seattachmentsrel.crmid=vtiger_inventoryproductrel.productid AND \n                                                        vtiger_attachments.attachmentsid=vtiger_seattachmentsrel.attachmentsid \n                                                    GROUP BY attachment_crmid DESC\n                                                  ) attachments\n                                                ON attachments.attachment_crmid=vtiger_inventoryproductrel.productid                                                \n \t\t                        WHERE id=?\n \t\t                        ORDER BY sequence_no";
        /*		$query="SELECT 
            case when vtiger_products.productid != '' then vtiger_products.productname else vtiger_service.servicename end as productname,
            case when vtiger_products.productid != '' then vtiger_products.productcode else vtiger_service.service_no end as productcode, 
            case when vtiger_products.productid != '' then vtiger_products.usageunit else vtiger_service.service_usageunit end as usageunit,									
            case when vtiger_products.productid != '' then vtiger_products.unit_price else vtiger_service.unit_price end as unit_price,									
            case when vtiger_products.productid != '' then vtiger_products.qtyinstock else 'NA' end as qtyinstock,
            case when vtiger_products.productid != '' then 'Products' else 'Services' end as entitytype,
                        vtiger_inventoryproductrel.listprice, 
                        vtiger_inventoryproductrel.description AS product_description, 
                        vtiger_inventoryproductrel.* 
                        FROM vtiger_inventoryproductrel 
                        LEFT JOIN vtiger_products 
                                ON vtiger_products.productid=vtiger_inventoryproductrel.productid 
                        LEFT JOIN vtiger_service 
                                ON vtiger_service.serviceid=vtiger_inventoryproductrel.productid 
                        WHERE id=?
                        ORDER BY sequence_no"; */
        // SalesPlatform.ru end
        $params = array($focus->id);
    } elseif ($module == 'Potentials') {
        $query = "SELECT\n \t\t                        vtiger_products.productname,\n \t\t                        vtiger_products.productcode,\n \t\t                        vtiger_products.unit_price,\n \t\t                        vtiger_products.qtyinstock,\n \t\t                        vtiger_seproductsrel.*,vtiger_crmentity.deleted,\n \t\t                        vtiger_crmentity.description AS product_description\n \t\t                        FROM vtiger_products\n \t\t                        INNER JOIN vtiger_crmentity\n \t\t                                ON vtiger_crmentity.crmid=vtiger_products.productid\n \t\t                        INNER JOIN vtiger_seproductsrel\n \t\t                                ON vtiger_seproductsrel.productid=vtiger_products.productid\n \t\t                        WHERE vtiger_seproductsrel.crmid=?";
        $params = array($seid);
    } elseif ($module == 'Products') {
        $query = "SELECT\n \t\t                        vtiger_products.productid,\n \t\t                        vtiger_products.productcode,\n \t\t                        vtiger_products.productname,\n \t\t                        vtiger_products.unit_price,\n \t\t                        vtiger_products.qtyinstock,vtiger_crmentity.deleted,\n \t\t                        vtiger_crmentity.description AS product_description,\n \t\t                        'Products' AS entitytype\n \t\t                        FROM vtiger_products\n \t\t                        INNER JOIN vtiger_crmentity\n \t\t                                ON vtiger_crmentity.crmid=vtiger_products.productid\n \t\t                        WHERE vtiger_crmentity.deleted=0\n \t\t                                AND productid=?";
        $params = array($seid);
    } elseif ($module == 'Services') {
        $query = "SELECT\n \t\t                        vtiger_service.serviceid AS productid,\n \t\t                        'NA' AS productcode,\n \t\t                        vtiger_service.servicename AS productname,\n \t\t                        vtiger_service.unit_price AS unit_price,\n \t\t                        'NA' AS qtyinstock,vtiger_crmentity.deleted,\n \t\t                        vtiger_crmentity.description AS product_description,\n \t\t                       \t'Services' AS entitytype\n \t\t\t\t\t\t\t\tFROM vtiger_service\n \t\t                        INNER JOIN vtiger_crmentity\n \t\t                                ON vtiger_crmentity.crmid=vtiger_service.serviceid\n \t\t                        WHERE vtiger_crmentity.deleted=0\n \t\t                                AND serviceid=?";
        $params = array($seid);
    }
    $result = $adb->pquery($query, $params);
    $num_rows = $adb->num_rows($result);
    for ($i = 1; $i <= $num_rows; $i++) {
        $deleted = $adb->query_result($result, $i - 1, 'deleted');
        $hdnProductId = $adb->query_result($result, $i - 1, 'productid');
        $hdnProductcode = $adb->query_result($result, $i - 1, 'productcode');
        $productname = $adb->query_result($result, $i - 1, 'productname');
        $productdescription = $adb->query_result($result, $i - 1, 'product_description');
        $comment = $adb->query_result($result, $i - 1, 'comment');
        $qtyinstock = $adb->query_result($result, $i - 1, 'qtyinstock');
        $qty = $adb->query_result($result, $i - 1, 'quantity');
        $unitprice = $adb->query_result($result, $i - 1, 'unit_price');
        $listprice = $adb->query_result($result, $i - 1, 'listprice');
        $entitytype = $adb->query_result($result, $i - 1, 'entitytype');
        // SalesPlatform.ru begin
        if ($module == 'Quotes' || $module == 'PurchaseOrder' || $module == 'SalesOrder' || $module == 'Invoice' || $module == 'Act' || $module == 'Consignment') {
            $manuf_country = $adb->query_result($result, $i - 1, 'manuf_country');
            $customs_id = $adb->query_result($result, $i - 1, 'customs_id');
            $manuf_country_code = $adb->query_result($result, $i - 1, 'manuf_country_code');
            $unit_code = $adb->query_result($result, $i - 1, 'unit_code');
            $usageunit = $adb->query_result($result, $i - 1, 'usageunit');
            $attachment_id = $adb->query_result($result, $i - 1, 'attachment_id');
            $attachment_name = $adb->query_result($result, $i - 1, 'attachment_name');
            $attachment_path = $adb->query_result($result, $i - 1, 'attachment_path');
        }
        // SalesPlatform.ru end
        if ($deleted || !isset($deleted)) {
            $product_Detail[$i]['productDeleted' . $i] = true;
        } elseif (!$deleted) {
            $product_Detail[$i]['productDeleted' . $i] = false;
        }
        if (!empty($entitytype)) {
            $product_Detail[$i]['entityType' . $i] = $entitytype;
        }
        if ($listprice == '') {
            $listprice = $unitprice;
        }
        if ($qty == '') {
            $qty = 1;
        }
        //calculate productTotal
        $productTotal = $qty * $listprice;
        //Delete link in First column
        if ($i != 1) {
            $product_Detail[$i]['delRow' . $i] = "Del";
        }
        if (empty($focus->mode) && $seid != '') {
            $sub_prod_query = $adb->pquery("SELECT crmid as prod_id from vtiger_seproductsrel WHERE productid=? AND setype='Products'", array($seid));
        } else {
            $sub_prod_query = $adb->pquery("SELECT productid as prod_id from vtiger_inventorysubproductrel WHERE id=? AND sequence_no=?", array($focus->id, $i));
        }
        $subprodid_str = '';
        $subprodname_str = '';
        $subProductArray = array();
        if ($adb->num_rows($sub_prod_query) > 0) {
            for ($j = 0; $j < $adb->num_rows($sub_prod_query); $j++) {
                $sprod_id = $adb->query_result($sub_prod_query, $j, 'prod_id');
                $sprod_name = $subProductArray[] = getProductName($sprod_id);
                $str_sep = "";
                if ($j > 0) {
                    $str_sep = ":";
                }
                $subprodid_str .= $str_sep . $sprod_id;
                if (isset($sprod_name)) {
                    $subprodname_str .= $str_sep . " - " . $sprod_name;
                }
            }
        }
        $subprodname_str = str_replace(":", "<br>", $subprodname_str);
        $product_Detail[$i]['subProductArray' . $i] = $subProductArray;
        $product_Detail[$i]['hdnProductId' . $i] = $hdnProductId;
        $product_Detail[$i]['productName' . $i] = from_html($productname);
        /* Added to fix the issue Product Pop-up name display*/
        if ($_REQUEST['action'] == 'CreateSOPDF' || $_REQUEST['action'] == 'CreatePDF' || $_REQUEST['action'] == 'SendPDFMail') {
            $product_Detail[$i]['productName' . $i] = htmlspecialchars($product_Detail[$i]['productName' . $i]);
        }
        $product_Detail[$i]['hdnProductcode' . $i] = $hdnProductcode;
        $product_Detail[$i]['productDescription' . $i] = from_html($productdescription);
        if ($module == 'Potentials' || $module == 'Products' || $module == 'Services') {
            $product_Detail[$i]['comment' . $i] = $productdescription;
        } else {
            $product_Detail[$i]['comment' . $i] = $comment;
        }
        // SalesPlatform.ru begin Added acts and consignments
        if ($module != 'PurchaseOrder' && $focus->object_name != 'Order' && $module != 'Act' && $module != 'Consignment') {
            $product_Detail[$i]['qtyInStock' . $i] = decimalFormat($qtyinstock);
        }
        $listprice = number_format($listprice, $no_of_decimal_places, '.', '');
        $product_Detail[$i]['qty' . $i] = decimalFormat($qty);
        $product_Detail[$i]['listPrice' . $i] = $listprice;
        $product_Detail[$i]['unitPrice' . $i] = number_format($unitprice, $no_of_decimal_places, '.', '');
        $product_Detail[$i]['productTotal' . $i] = $productTotal;
        $product_Detail[$i]['subproduct_ids' . $i] = $subprodid_str;
        $product_Detail[$i]['subprod_names' . $i] = $subprodname_str;
        $discount_percent = decimalFormat($adb->query_result($result, $i - 1, 'discount_percent'));
        $discount_amount = $adb->query_result($result, $i - 1, 'discount_amount');
        $discount_amount = decimalFormat(number_format($discount_amount, $no_of_decimal_places, '.', ''));
        $discountTotal = '0';
        //Based on the discount percent or amount we will show the discount details
        //To avoid NaN javascript error, here we assign 0 initially to' %of price' and 'Direct Price reduction'(for Each Product)
        $product_Detail[$i]['discount_percent' . $i] = 0;
        $product_Detail[$i]['discount_amount' . $i] = 0;
        if (!empty($discount_percent)) {
            $product_Detail[$i]['discount_type' . $i] = "percentage";
            $product_Detail[$i]['discount_percent' . $i] = $discount_percent;
            $product_Detail[$i]['checked_discount_percent' . $i] = ' checked';
            $product_Detail[$i]['style_discount_percent' . $i] = ' style="visibility:visible"';
            $product_Detail[$i]['style_discount_amount' . $i] = ' style="visibility:hidden"';
            $discountTotal = $productTotal * $discount_percent / 100;
        } elseif (!empty($discount_amount)) {
            $product_Detail[$i]['discount_type' . $i] = "amount";
            $product_Detail[$i]['discount_amount' . $i] = $discount_amount;
            $product_Detail[$i]['checked_discount_amount' . $i] = ' checked';
            $product_Detail[$i]['style_discount_amount' . $i] = ' style="visibility:visible"';
            $product_Detail[$i]['style_discount_percent' . $i] = ' style="visibility:hidden"';
            $discountTotal = $discount_amount;
        } else {
            $product_Detail[$i]['checked_discount_zero' . $i] = ' checked';
        }
        $totalAfterDiscount = $productTotal - $discountTotal;
        $totalAfterDiscount = number_format($totalAfterDiscount, $no_of_decimal_places, '.', '');
        $discountTotal = number_format($discountTotal, $no_of_decimal_places, '.', '');
        $product_Detail[$i]['discountTotal' . $i] = $discountTotal;
        $product_Detail[$i]['totalAfterDiscount' . $i] = $totalAfterDiscount;
        // SalesPlatform.ru begin
        if ($module == 'Quotes' || $module == 'PurchaseOrder' || $module == 'SalesOrder' || $module == 'Invoice' || $module == 'Act' || $module == 'Consignment') {
            $product_Detail[$i]['manufCountry' . $i] = $manuf_country;
            $product_Detail[$i]['customsId' . $i] = $customs_id;
            $product_Detail[$i]['manufCountryCode' . $i] = $manuf_country_code;
            $product_Detail[$i]['unitCode' . $i] = $unit_code;
            $product_Detail[$i]['usageunit' . $i] = $usageunit;
            $product_Detail[$i]['attachmentId' . $i] = $attachment_id;
            $product_Detail[$i]['attachmentName' . $i] = $attachment_name;
            $product_Detail[$i]['attachmentPath' . $i] = $attachment_path;
        }
        // SalesPlatform.ru end
        $taxTotal = '0.00';
        $taxTotal = number_format($taxTotal, $no_of_decimal_places, '.', '');
        $product_Detail[$i]['taxTotal' . $i] = $taxTotal;
        //Calculate netprice
        $netPrice = $totalAfterDiscount + $taxTotal;
        //if condition is added to call this function when we create PO/SO/Quotes/Invoice from Product module
        // SalesPlatform.ru begin Added acts and consignments
        if ($module == 'PurchaseOrder' || $module == 'SalesOrder' || $module == 'Quotes' || $module == 'Invoice' || $module == 'Act' || $module == 'Consignment') {
            $taxtype = getInventoryTaxType($module, $focus->id);
            if ($taxtype == 'individual') {
                //Add the tax with product total and assign to netprice
                $netPrice = $netPrice + $taxTotal;
            }
        }
        $product_Detail[$i]['netPrice' . $i] = $netPrice;
        //First we will get all associated taxes as array
        $tax_details = getTaxDetailsForProduct($hdnProductId, 'all');
        //Now retrieve the tax values from the current query with the name
        for ($tax_count = 0; $tax_count < count($tax_details); $tax_count++) {
            $tax_name = $tax_details[$tax_count]['taxname'];
            $tax_label = $tax_details[$tax_count]['taxlabel'];
            $tax_value = '0';
            //condition to avoid this function call when create new PO/SO/Quotes/Invoice from Product module
            if ($focus->id != '') {
                if ($taxtype == 'individual') {
                    //if individual then show the entered tax percentage
                    $tax_value = getInventoryProductTaxValue($focus->id, $hdnProductId, $tax_name);
                } else {
                    //if group tax then we have to show the default value when change to individual tax
                    $tax_value = $tax_details[$tax_count]['percentage'];
                }
            } else {
                //if the above function not called then assign the default associated value of the product
                $tax_value = $tax_details[$tax_count]['percentage'];
            }
            $product_Detail[$i]['taxes'][$tax_count]['taxname'] = $tax_name;
            $product_Detail[$i]['taxes'][$tax_count]['taxlabel'] = $tax_label;
            $product_Detail[$i]['taxes'][$tax_count]['percentage'] = $tax_value;
        }
    }
    //set the taxtype
    $product_Detail[1]['final_details']['taxtype'] = $taxtype;
    //Get the Final Discount, S&H charge, Tax for S&H and Adjustment values
    //To set the Final Discount details
    $finalDiscount = '0';
    $product_Detail[1]['final_details']['discount_type_final'] = 'zero';
    $subTotal = $focus->column_fields['hdnSubTotal'] != '' ? $focus->column_fields['hdnSubTotal'] : '0';
    $subTotal = number_format($subTotal, $no_of_decimal_places, '.', '');
    $product_Detail[1]['final_details']['hdnSubTotal'] = $subTotal;
    $discountPercent = $focus->column_fields['hdnDiscountPercent'] != '' ? $focus->column_fields['hdnDiscountPercent'] : '0';
    $discountAmount = $focus->column_fields['hdnDiscountAmount'] != '' ? $focus->column_fields['hdnDiscountAmount'] : '0';
    if ($discountPercent != '0') {
        $discountAmount = $product_Detail[1]['final_details']['hdnSubTotal'] * $discountPercent / 100;
    }
    //To avoid NaN javascript error, here we assign 0 initially to' %of price' and 'Direct Price reduction'(For Final Discount)
    $discount_amount_final = '0';
    $discount_amount_final = number_format($discount_amount_final, $no_of_decimal_places, '.', '');
    $product_Detail[1]['final_details']['discount_percentage_final'] = 0;
    $product_Detail[1]['final_details']['discount_amount_final'] = $discount_amount_final;
    //fix for opensource issue not saving invoice data properly
    if (!empty($focus->column_fields['hdnDiscountPercent'])) {
        $finalDiscount = $subTotal * $discountPercent / 100;
        $product_Detail[1]['final_details']['discount_type_final'] = 'percentage';
        $product_Detail[1]['final_details']['discount_percentage_final'] = $discountPercent;
        $product_Detail[1]['final_details']['checked_discount_percentage_final'] = ' checked';
        $product_Detail[1]['final_details']['style_discount_percentage_final'] = ' style="visibility:visible"';
        $product_Detail[1]['final_details']['style_discount_amount_final'] = ' style="visibility:hidden"';
    } elseif (!empty($focus->column_fields['hdnDiscountAmount'])) {
        $finalDiscount = $focus->column_fields['hdnDiscountAmount'];
        $product_Detail[1]['final_details']['discount_type_final'] = 'amount';
        $product_Detail[1]['final_details']['discount_amount_final'] = $discountAmount;
        $product_Detail[1]['final_details']['checked_discount_amount_final'] = ' checked';
        $product_Detail[1]['final_details']['style_discount_amount_final'] = ' style="visibility:visible"';
        $product_Detail[1]['final_details']['style_discount_percentage_final'] = ' style="visibility:hidden"';
    }
    $finalDiscount = number_format($finalDiscount, $no_of_decimal_places, '.', '');
    $product_Detail[1]['final_details']['discountTotal_final'] = $finalDiscount;
    //To set the Final Tax values
    //we will get all taxes. if individual then show the product related taxes only else show all taxes
    //suppose user want to change individual to group or vice versa in edit time the we have to show all taxes. so that here we will store all the taxes and based on need we will show the corresponding taxes
    $taxtotal = '0';
    //First we should get all available taxes and then retrieve the corresponding tax values
    $tax_details = getAllTaxes('available', '', 'edit', $focus->id);
    for ($tax_count = 0; $tax_count < count($tax_details); $tax_count++) {
        $tax_name = $tax_details[$tax_count]['taxname'];
        $tax_label = $tax_details[$tax_count]['taxlabel'];
        //if taxtype is individual and want to change to group during edit time then we have to show the all available taxes and their default values
        //Also taxtype is group and want to change to individual during edit time then we have to provide the asspciated taxes and their default tax values for individual products
        // SalesPlatform.ru begin
        if ($taxtype == 'group' || $taxtype == 'group_tax_inc') {
            //if($taxtype == 'group')
            // SalesPlatform.ru end
            $tax_percent = $adb->query_result($result, 0, $tax_name);
        } else {
            $tax_percent = $tax_details[$tax_count]['percentage'];
        }
        //$adb->query_result($result,0,$tax_name);
        if ($tax_percent == '' || $tax_percent == 'NULL') {
            $tax_percent = '0.00';
        }
        // SalesPlatform.ru begin
        if ($taxtype == 'group_tax_inc') {
            $taxamount = ($subTotal - $finalDiscount) * $tax_percent / (100.0 + $tax_percent);
        } else {
            // SalesPlatform.ru end
            $taxamount = ($subTotal - $finalDiscount) * $tax_percent / 100;
        }
        $taxamount = number_format($taxamount, $no_of_decimal_places, '.', '');
        $taxtotal = $taxtotal + $taxamount;
        $product_Detail[1]['final_details']['taxes'][$tax_count]['taxname'] = $tax_name;
        $product_Detail[1]['final_details']['taxes'][$tax_count]['taxlabel'] = $tax_label;
        $product_Detail[1]['final_details']['taxes'][$tax_count]['percentage'] = $tax_percent;
        $product_Detail[1]['final_details']['taxes'][$tax_count]['amount'] = $taxamount;
    }
    $product_Detail[1]['final_details']['tax_totalamount'] = $taxtotal;
    //To set the Shipping & Handling charge
    $shCharge = $focus->column_fields['hdnS_H_Amount'] != '' ? $focus->column_fields['hdnS_H_Amount'] : '0    ';
    $shCharge = number_format($shCharge, $no_of_decimal_places, '.', '');
    $product_Detail[1]['final_details']['shipping_handling_charge'] = $shCharge;
    //To set the Shipping & Handling tax values
    //calculate S&H tax
    $shtaxtotal = '0';
    //First we should get all available taxes and then retrieve the corresponding tax values
    $shtax_details = getAllTaxes('available', 'sh', 'edit', $focus->id);
    //if taxtype is group then the tax should be same for all products in vtiger_inventoryproductrel table
    for ($shtax_count = 0; $shtax_count < count($shtax_details); $shtax_count++) {
        $shtax_name = $shtax_details[$shtax_count]['taxname'];
        $shtax_label = $shtax_details[$shtax_count]['taxlabel'];
        $shtax_percent = '0';
        //if condition is added to call this function when we create PO/SO/Quotes/Invoice from Product module
        // SalesPlatform.ru begin Added acts and consignments
        if ($module == 'PurchaseOrder' || $module == 'SalesOrder' || $module == 'Quotes' || $module == 'Invoice' || $module == 'Act' || $module == 'Consignment') {
            $shtax_percent = getInventorySHTaxPercent($focus->id, $shtax_name);
        }
        // SalesPlatform.ru begin
        if ($taxtype == 'group_tax_inc') {
            $shtaxamount = $shCharge * $shtax_percent / (100 + $shtax_percent);
        } else {
            // SalesPlatform.ru end
            $shtaxamount = $shCharge * $shtax_percent / 100;
        }
        $shtaxtotal = $shtaxtotal + $shtaxamount;
        $product_Detail[1]['final_details']['sh_taxes'][$shtax_count]['taxname'] = $shtax_name;
        $product_Detail[1]['final_details']['sh_taxes'][$shtax_count]['taxlabel'] = $shtax_label;
        $product_Detail[1]['final_details']['sh_taxes'][$shtax_count]['percentage'] = $shtax_percent;
        $product_Detail[1]['final_details']['sh_taxes'][$shtax_count]['amount'] = $shtaxamount;
    }
    $shtaxtotal = number_format($shtaxtotal, $no_of_decimal_places, '.', '');
    $product_Detail[1]['final_details']['shtax_totalamount'] = $shtaxtotal;
    //To set the Adjustment value
    $adjustment = $focus->column_fields['txtAdjustment'] != '' ? $focus->column_fields['txtAdjustment'] : '0';
    $adjustment = number_format($adjustment, $no_of_decimal_places, '.', '');
    $product_Detail[1]['final_details']['adjustment'] = $adjustment;
    //To set the grand total
    $grandTotal = $focus->column_fields['hdnGrandTotal'] != '' ? $focus->column_fields['hdnGrandTotal'] : '0';
    $grandTotal = number_format($grandTotal, $no_of_decimal_places, '.', '');
    $product_Detail[1]['final_details']['grandTotal'] = $grandTotal;
    $log->debug("Exiting getAssociatedProducts method ...");
    return $product_Detail;
}