Example #1
0
/**	Function used to get the prices for the given list of products based in the specified currency
 *	@param int $currencyid - currency id based on which the prices have to be provided
 *	@param array $product_ids - List of product id's for which we want to get the price based on given currency
 *  @return array $prices_list - List of prices for the given list of products based on the given currency in the form of 'product id' mapped to 'price value'
 */
function getPricesForProducts($currencyid, $product_ids, $module = 'Products')
{
    global $adb, $log, $current_user;
    $price_list = array();
    if (count($product_ids) > 0) {
        if ($module == 'Services') {
            $query = "SELECT vtiger_currency_info.id, vtiger_currency_info.conversion_rate, " . "vtiger_service.serviceid AS productid, vtiger_service.unit_price, " . "vtiger_productcurrencyrel.actual_price " . "FROM (vtiger_currency_info, vtiger_service) " . "left join vtiger_productcurrencyrel on vtiger_service.serviceid = vtiger_productcurrencyrel.productid " . "and vtiger_currency_info.id = vtiger_productcurrencyrel.currencyid " . "where vtiger_service.serviceid in (" . generateQuestionMarks($product_ids) . ") and vtiger_currency_info.id = ?";
        } else {
            $query = "SELECT vtiger_currency_info.id, vtiger_currency_info.conversion_rate, " . "vtiger_products.productid, vtiger_products.unit_price, " . "vtiger_productcurrencyrel.actual_price " . "FROM (vtiger_currency_info, vtiger_products) " . "left join vtiger_productcurrencyrel on vtiger_products.productid = vtiger_productcurrencyrel.productid " . "and vtiger_currency_info.id = vtiger_productcurrencyrel.currencyid " . "where vtiger_products.productid in (" . generateQuestionMarks($product_ids) . ") and vtiger_currency_info.id = ?";
        }
        $params = array($product_ids, $currencyid);
        $result = $adb->pquery($query, $params);
        for ($i = 0; $i < $adb->num_rows($result); $i++) {
            $product_id = $adb->query_result($result, $i, 'productid');
            if (getFieldVisibilityPermission($module, $current_user->id, 'unit_price') == '0') {
                $actual_price = (double) $adb->query_result($result, $i, 'actual_price');
                if ($actual_price == null || $actual_price == '') {
                    $unit_price = $adb->query_result($result, $i, 'unit_price');
                    $product_conv_rate = $adb->query_result($result, $i, 'conversion_rate');
                    $product_base_conv_rate = getBaseConversionRateForProduct($product_id, 'edit', $module);
                    $conversion_rate = $product_conv_rate * $product_base_conv_rate;
                    $actual_price = $unit_price * $conversion_rate;
                }
                $price_list[$product_id] = $actual_price;
            } else {
                $price_list[$product_id] = '';
            }
        }
    }
    return $price_list;
}
Example #2
0
 /**	function to save the service price information in vtiger_servicecurrencyrel table
  *	@param string $tablename - vtiger_tablename to save the service currency relationship (servicecurrencyrel)
  *	@param string $module	 - current module name
  *	$return void
  */
 function insertPriceInformation($tablename, $module)
 {
     global $adb, $log, $current_user;
     $log->debug("Entering into insertPriceInformation({$tablename}, {$module}) method ...");
     //removed the update of currency_id based on the logged in user's preference : fix 6490
     $currency_details = getAllCurrencies('all');
     //Delete the existing currency relationship if any
     if ($this->mode == 'edit' && $_REQUEST['action'] != 'MassEditSave' && $_REQUEST['action'] != 'ProcessDuplicates') {
         for ($i = 0; $i < count($currency_details); $i++) {
             $curid = $currency_details[$i]['curid'];
             $sql = "delete from vtiger_productcurrencyrel where productid=? and currencyid=?";
             $adb->pquery($sql, array($this->id, $curid));
         }
     }
     $service_base_conv_rate = getBaseConversionRateForProduct($this->id, $this->mode, $module);
     //Save the Product - Currency relationship if corresponding currency check box is enabled
     for ($i = 0; $i < count($currency_details); $i++) {
         $curid = $currency_details[$i]['curid'];
         $curname = $currency_details[$i]['currencylabel'];
         $cur_checkname = 'cur_' . $curid . '_check';
         $cur_valuename = 'curname' . $curid;
         $base_currency_check = 'base_currency' . $curid;
         $requestPrice = CurrencyField::convertToDBFormat($_REQUEST['unit_price'], null, true);
         $actualPrice = CurrencyField::convertToDBFormat($_REQUEST[$cur_valuename], null, true);
         if ($_REQUEST[$cur_checkname] == 'on' || $_REQUEST[$cur_checkname] == 1) {
             $conversion_rate = $currency_details[$i]['conversionrate'];
             $actual_conversion_rate = $service_base_conv_rate * $conversion_rate;
             $converted_price = $actual_conversion_rate * $requestPrice;
             $log->debug("Going to save the Product - {$curname} currency relationship");
             $query = "insert into vtiger_productcurrencyrel values(?,?,?,?)";
             $adb->pquery($query, array($this->id, $curid, $converted_price, $actualPrice));
             // Update the Product information with Base Currency choosen by the User.
             if ($_REQUEST['base_currency'] == $cur_valuename) {
                 $adb->pquery("update vtiger_service set currency_id=?, unit_price=? where serviceid=?", array($curid, $actualPrice, $this->id));
             }
         }
     }
     $log->debug("Exiting from insertPriceInformation({$tablename}, {$module}) method ...");
 }