PageRenderTime 63ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/include/utils/InventoryUtils.php

https://bitbucket.org/yousef_fadila/vtiger
PHP | 1088 lines | 774 code | 143 blank | 171 comment | 160 complexity | 25abfea221e9844e6984a9b9316ad291 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /*********************************************************************************
  3. ** The contents of this file are subject to the vtiger CRM Public License Version 1.0
  4. * ("License"); You may not use this file except in compliance with the License
  5. * The Original Code is: vtiger CRM Open Source
  6. * The Initial Developer of the Original Code is vtiger.
  7. * Portions created by vtiger are Copyright (C) vtiger.
  8. * All Rights Reserved.
  9. *
  10. ********************************************************************************/
  11. /**
  12. * This function returns the Product detail block values in array format.
  13. * Input Parameter are $module - module name, $focus - module object, $num_of_products - no.of vtiger_products associated with it * $associated_prod = associated product details
  14. * column vtiger_fields/
  15. */
  16. function getProductDetailsBlockInfo($mode,$module,$focus='',$num_of_products='',$associated_prod='')
  17. {
  18. global $log;
  19. $log->debug("Entering getProductDetailsBlockInfo(".$mode.",".$module.",".$num_of_products.",".$associated_prod.") method ...");
  20. $productDetails = Array();
  21. $productBlock = Array();
  22. if($num_of_products=='')
  23. {
  24. $num_of_products = getNoOfAssocProducts($module,$focus);
  25. }
  26. $productDetails['no_products'] = $num_of_products;
  27. if($associated_prod=='')
  28. {
  29. $productDetails['product_details'] = getAssociatedProducts($module,$focus);
  30. }
  31. else
  32. {
  33. $productDetails['product_details'] = $associated_prod;
  34. }
  35. if($focus != '')
  36. {
  37. $productBlock[] = Array('mode'=>$focus->mode);
  38. $productBlock[] = $productDetails['product_details'];
  39. $productBlock[] = Array('taxvalue' => $focus->column_fields['txtTax']);
  40. $productBlock[] = Array('taxAdjustment' => $focus->column_fields['txtAdjustment']);
  41. $productBlock[] = Array('hdnSubTotal' => $focus->column_fields['hdnSubTotal']);
  42. $productBlock[] = Array('hdnGrandTotal' => $focus->column_fields['hdnGrandTotal']);
  43. }
  44. else
  45. {
  46. $productBlock[] = Array(Array());
  47. }
  48. $log->debug("Exiting getProductDetailsBlockInfo method ...");
  49. return $productBlock;
  50. }
  51. /**
  52. * This function updates the stock information once the product is ordered.
  53. * Param $productid - product id
  54. * Param $qty - product quantity in no's
  55. * Param $mode - mode type
  56. * Param $ext_prod_arr - existing vtiger_products
  57. * Param $module - module name
  58. * return type void
  59. */
  60. function updateStk($product_id,$qty,$mode,$ext_prod_arr,$module)
  61. {
  62. global $log;
  63. $log->debug("Entering updateStk(".$product_id.",".$qty.",".$mode.",".$ext_prod_arr.",".$module.") method ...");
  64. global $adb;
  65. global $current_user;
  66. $log->debug("Inside updateStk function, module=".$module);
  67. $log->debug("Product Id = $product_id & Qty = $qty");
  68. $prod_name = getProductName($product_id);
  69. $qtyinstk= getPrdQtyInStck($product_id);
  70. $log->debug("Prd Qty in Stock ".$qtyinstk);
  71. $upd_qty = $qtyinstk-$qty;
  72. sendPrdStckMail($product_id,$upd_qty,$prod_name,$qtyinstk,$qty,$module);
  73. $log->debug("Exiting updateStk method ...");
  74. }
  75. /**
  76. * This function sends a mail to the handler whenever the product reaches the reorder level.
  77. * Param $product_id - product id
  78. * Param $upd_qty - updated product quantity in no's
  79. * Param $prod_name - product name
  80. * Param $qtyinstk - quantity in stock
  81. * Param $qty - quantity
  82. * Param $module - module name
  83. * return type void
  84. */
  85. function sendPrdStckMail($product_id,$upd_qty,$prod_name,$qtyinstk,$qty,$module)
  86. {
  87. global $log;
  88. $log->debug("Entering sendPrdStckMail(".$product_id.",".$upd_qty.",".$prod_name.",".$qtyinstk.",".$qty.",".$module.") method ...");
  89. global $current_user;
  90. global $adb;
  91. $reorderlevel = getPrdReOrderLevel($product_id);
  92. $log->debug("Inside sendPrdStckMail function, module=".$module);
  93. $log->debug("Prd reorder level ".$reorderlevel);
  94. if($upd_qty < $reorderlevel)
  95. {
  96. //send mail to the handler
  97. $handler = getRecordOwnerId($product_id);
  98. $handler_name = getOwnerName($handler);
  99. if(vtws_isRecordOwnerUser($handler)) {
  100. $to_address = getUserEmail($handler);
  101. } else {
  102. $to_address = implode(',', getDefaultAssigneeEmailIds($handler));
  103. }
  104. //Get the email details from database;
  105. if($module == 'SalesOrder')
  106. {
  107. $notification_table = 'SalesOrderNotification';
  108. $quan_name = '{SOQUANTITY}';
  109. }
  110. if($module == 'Quotes')
  111. {
  112. $notification_table = 'QuoteNotification';
  113. $quan_name = '{QUOTEQUANTITY}';
  114. }
  115. if($module == 'Invoice')
  116. {
  117. $notification_table = 'InvoiceNotification';
  118. }
  119. $query = "select * from vtiger_inventorynotification where notificationname=?";
  120. $result = $adb->pquery($query, array($notification_table));
  121. $subject = $adb->query_result($result,0,'notificationsubject');
  122. $body = $adb->query_result($result,0,'notificationbody');
  123. $status = $adb->query_result($result,0,'status');
  124. if($status == 0 || $status == '')
  125. return false;
  126. $subject = str_replace('{PRODUCTNAME}',$prod_name,$subject);
  127. $body = str_replace('{HANDLER}',$handler_name,$body);
  128. $body = str_replace('{PRODUCTNAME}',$prod_name,$body);
  129. if($module == 'Invoice')
  130. {
  131. $body = str_replace('{CURRENTSTOCK}',$upd_qty,$body);
  132. $body = str_replace('{REORDERLEVELVALUE}',$reorderlevel,$body);
  133. }
  134. else
  135. {
  136. $body = str_replace('{CURRENTSTOCK}',$qtyinstk,$body);
  137. $body = str_replace($quan_name,$qty,$body);
  138. }
  139. $body = str_replace('{CURRENTUSER}',$current_user->user_name,$body);
  140. $mail_status = send_mail($module,$to_address,$current_user->user_name,$current_user->email1,decode_html($subject),nl2br(to_html($body)));
  141. }
  142. $log->debug("Exiting sendPrdStckMail method ...");
  143. }
  144. /**This function is used to get the quantity in stock of a given product
  145. *Param $product_id - product id
  146. *Returns type numeric
  147. */
  148. function getPrdQtyInStck($product_id)
  149. {
  150. global $log;
  151. $log->debug("Entering getPrdQtyInStck(".$product_id.") method ...");
  152. global $adb;
  153. $query1 = "SELECT qtyinstock FROM vtiger_products WHERE productid = ?";
  154. $result=$adb->pquery($query1, array($product_id));
  155. $qtyinstck= $adb->query_result($result,0,"qtyinstock");
  156. $log->debug("Exiting getPrdQtyInStck method ...");
  157. return $qtyinstck;
  158. }
  159. /**This function is used to get the reorder level of a product
  160. *Param $product_id - product id
  161. *Returns type numeric
  162. */
  163. function getPrdReOrderLevel($product_id)
  164. {
  165. global $log;
  166. $log->debug("Entering getPrdReOrderLevel(".$product_id.") method ...");
  167. global $adb;
  168. $query1 = "SELECT reorderlevel FROM vtiger_products WHERE productid = ?";
  169. $result=$adb->pquery($query1, array($product_id));
  170. $reorderlevel= $adb->query_result($result,0,"reorderlevel");
  171. $log->debug("Exiting getPrdReOrderLevel method ...");
  172. return $reorderlevel;
  173. }
  174. /** function to get the taxid
  175. * @param string $type - tax type (VAT or Sales or Service)
  176. * return int $taxid - taxid corresponding to the Tax type from vtiger_inventorytaxinfo vtiger_table
  177. */
  178. function getTaxId($type)
  179. {
  180. global $adb, $log;
  181. $log->debug("Entering into getTaxId($type) function.");
  182. $res = $adb->pquery("SELECT taxid FROM vtiger_inventorytaxinfo WHERE taxname=?", array($type));
  183. $taxid = $adb->query_result($res,0,'taxid');
  184. $log->debug("Exiting from getTaxId($type) function. return value=$taxid");
  185. return $taxid;
  186. }
  187. /** function to get the taxpercentage
  188. * @param string $type - tax type (VAT or Sales or Service)
  189. * return int $taxpercentage - taxpercentage corresponding to the Tax type from vtiger_inventorytaxinfo vtiger_table
  190. */
  191. function getTaxPercentage($type)
  192. {
  193. global $adb, $log;
  194. $log->debug("Entering into getTaxPercentage($type) function.");
  195. $taxpercentage = '';
  196. $res = $adb->pquery("SELECT percentage FROM vtiger_inventorytaxinfo WHERE taxname = ?", array($type));
  197. $taxpercentage = $adb->query_result($res,0,'percentage');
  198. $log->debug("Exiting from getTaxPercentage($type) function. return value=$taxpercentage");
  199. return $taxpercentage;
  200. }
  201. /** function to get the product's taxpercentage
  202. * @param string $type - tax type (VAT or Sales or Service)
  203. * @param id $productid - productid to which we want the tax percentage
  204. * @param id $default - if 'default' then first look for product's tax percentage and product's tax is empty then it will return the default configured tax percentage, else it will return the product's tax (not look for default value)
  205. * return int $taxpercentage - taxpercentage corresponding to the Tax type from vtiger_inventorytaxinfo vtiger_table
  206. */
  207. function getProductTaxPercentage($type,$productid,$default='')
  208. {
  209. global $adb, $log;
  210. $log->debug("Entering into getProductTaxPercentage($type,$productid) function.");
  211. $taxpercentage = '';
  212. $res = $adb->pquery("SELECT taxpercentage
  213. FROM vtiger_inventorytaxinfo
  214. INNER JOIN vtiger_producttaxrel
  215. ON vtiger_inventorytaxinfo.taxid = vtiger_producttaxrel.taxid
  216. WHERE vtiger_producttaxrel.productid = ?
  217. AND vtiger_inventorytaxinfo.taxname = ?", array($productid, $type));
  218. $taxpercentage = $adb->query_result($res,0,'taxpercentage');
  219. //This is to retrive the default configured value if the taxpercentage related to product is empty
  220. if($taxpercentage == '' && $default == 'default')
  221. $taxpercentage = getTaxPercentage($type);
  222. $log->debug("Exiting from getProductTaxPercentage($productid,$type) function. return value=$taxpercentage");
  223. return $taxpercentage;
  224. }
  225. /** Function used to add the history entry in the relevant tables for PO, SO, Quotes and Invoice modules
  226. * @param string $module - current module name
  227. * @param int $id - entity id
  228. * @param string $relatedname - parent name of the entity ie, required field venor name for PO and account name for SO, Quotes and Invoice
  229. * @param float $total - grand total value of the product details included tax
  230. * @param string $history_fldval - history field value ie., quotestage for Quotes and status for PO, SO and Invoice
  231. */
  232. function addInventoryHistory($module, $id, $relatedname, $total, $history_fldval)
  233. {
  234. global $log, $adb;
  235. $log->debug("Entering into function addInventoryHistory($module, $id, $relatedname, $total, $history_fieldvalue)");
  236. $history_table_array = Array(
  237. "PurchaseOrder"=>"vtiger_postatushistory",
  238. "SalesOrder"=>"vtiger_sostatushistory",
  239. "Quotes"=>"vtiger_quotestagehistory",
  240. "Invoice"=>"vtiger_invoicestatushistory"
  241. );
  242. $histid = $adb->getUniqueID($history_table_array[$module]);
  243. $modifiedtime = $adb->formatDate(date('Y-m-d H:i:s'), true);
  244. $query = "insert into $history_table_array[$module] values(?,?,?,?,?,?)";
  245. $qparams = array($histid,$id,$relatedname,$total,$history_fldval,$modifiedtime);
  246. $adb->pquery($query, $qparams);
  247. $log->debug("Exit from function addInventoryHistory");
  248. }
  249. /** Function used to get the list of Tax types as a array
  250. * @param string $available - available or empty where as default is all, if available then the taxes which are available now will be returned otherwise all taxes will be returned
  251. * @param string $sh - sh or empty, if sh passed then the shipping and handling related taxes will be returned
  252. * @param string $mode - edit or empty, if mode is edit, then it will return taxes including desabled.
  253. * @param string $id - crmid or empty, getting crmid to get tax values..
  254. * return array $taxtypes - return all the tax types as a array
  255. */
  256. function getAllTaxes($available='all', $sh='',$mode='',$id='')
  257. {
  258. global $adb, $log;
  259. $log->debug("Entering into the function getAllTaxes($available,$sh,$mode,$id)");
  260. $taxtypes = Array();
  261. if($sh != '' && $sh == 'sh')
  262. {
  263. $tablename = 'vtiger_shippingtaxinfo';
  264. $value_table='vtiger_inventoryshippingrel';
  265. }
  266. else
  267. {
  268. $tablename = 'vtiger_inventorytaxinfo';
  269. $value_table='vtiger_inventoryproductrel';
  270. }
  271. if($mode == 'edit' && $id != '' )
  272. {
  273. //Getting total no of taxes
  274. $result_ids=array();
  275. $result=$adb->pquery("select taxname,taxid from $tablename",array());
  276. $noofrows=$adb->num_rows($result);
  277. $inventory_tax_val_result=$adb->pquery("select * from $value_table where id=?",array($id));
  278. //Finding which taxes are associated with this (SO,PO,Invoice,Quotes) and getting its taxid.
  279. for($i=0;$i<$noofrows;$i++)
  280. {
  281. $taxname=$adb->query_result($result,$i,'taxname');
  282. $taxid=$adb->query_result($result,$i,'taxid');
  283. $tax_val=$adb->query_result($inventory_tax_val_result,0,$taxname);
  284. if($tax_val != '')
  285. {
  286. array_push($result_ids,$taxid);
  287. }
  288. }
  289. //We are selecting taxes using that taxids. So It will get the tax even if the tax is disabled.
  290. $where_ids='';
  291. if (count($result_ids) > 0)
  292. {
  293. $insert_str = str_repeat("?,", count($result_ids)-1);
  294. $insert_str .= "?";
  295. $where_ids="taxid in ($insert_str) or";
  296. }
  297. $res = $adb->pquery("select * from $tablename where $where_ids deleted=0 order by taxid",$result_ids);
  298. }
  299. else
  300. {
  301. //This where condition is added to get all products or only availble products
  302. if($available != 'all' && $available == 'available')
  303. {
  304. $where = " where $tablename.deleted=0";
  305. }
  306. $res = $adb->pquery("select * from $tablename $where order by deleted",array());
  307. }
  308. $noofrows = $adb->num_rows($res);
  309. for($i=0;$i<$noofrows;$i++)
  310. {
  311. $taxtypes[$i]['taxid'] = $adb->query_result($res,$i,'taxid');
  312. $taxtypes[$i]['taxname'] = $adb->query_result($res,$i,'taxname');
  313. $taxtypes[$i]['taxlabel'] = $adb->query_result($res,$i,'taxlabel');
  314. $taxtypes[$i]['percentage'] = $adb->query_result($res,$i,'percentage');
  315. $taxtypes[$i]['deleted'] = $adb->query_result($res,$i,'deleted');
  316. }
  317. $log->debug("Exit from the function getAllTaxes($available,$sh,$mode,$id)");
  318. return $taxtypes;
  319. }
  320. /** Function used to get all the tax details which are associated to the given product
  321. * @param int $productid - product id to which we want to get all the associated taxes
  322. * @param string $available - available or empty or available_associated where as default is all, if available then the taxes which are available now will be returned, if all then all taxes will be returned otherwise if the value is available_associated then all the associated taxes even they are not available and all the available taxes will be retruned
  323. * @return array $tax_details - tax details as a array with productid, taxid, taxname, percentage and deleted
  324. */
  325. function getTaxDetailsForProduct($productid, $available='all')
  326. {
  327. global $log, $adb;
  328. $log->debug("Entering into function getTaxDetailsForProduct($productid)");
  329. if($productid != '')
  330. {
  331. //where condition added to avoid to retrieve the non available taxes
  332. $where = '';
  333. if($available != 'all' && $available == 'available')
  334. {
  335. $where = ' and vtiger_inventorytaxinfo.deleted=0';
  336. }
  337. if($available != 'all' && $available == 'available_associated')
  338. {
  339. $query = "SELECT vtiger_producttaxrel.*, vtiger_inventorytaxinfo.* FROM vtiger_inventorytaxinfo left JOIN vtiger_producttaxrel ON vtiger_inventorytaxinfo.taxid = vtiger_producttaxrel.taxid WHERE vtiger_producttaxrel.productid = ? or vtiger_inventorytaxinfo.deleted=0 GROUP BY vtiger_inventorytaxinfo.taxid";
  340. }
  341. else
  342. {
  343. $query = "SELECT vtiger_producttaxrel.*, vtiger_inventorytaxinfo.* FROM vtiger_inventorytaxinfo INNER JOIN vtiger_producttaxrel ON vtiger_inventorytaxinfo.taxid = vtiger_producttaxrel.taxid WHERE vtiger_producttaxrel.productid = ? $where";
  344. }
  345. $params = array($productid);
  346. //Postgres 8 fixes
  347. if( $adb->dbType == "pgsql")
  348. $query = fixPostgresQuery( $query, $log, 0);
  349. $res = $adb->pquery($query, $params);
  350. for($i=0;$i<$adb->num_rows($res);$i++)
  351. {
  352. $tax_details[$i]['productid'] = $adb->query_result($res,$i,'productid');
  353. $tax_details[$i]['taxid'] = $adb->query_result($res,$i,'taxid');
  354. $tax_details[$i]['taxname'] = $adb->query_result($res,$i,'taxname');
  355. $tax_details[$i]['taxlabel'] = $adb->query_result($res,$i,'taxlabel');
  356. $tax_details[$i]['percentage'] = $adb->query_result($res,$i,'taxpercentage');
  357. $tax_details[$i]['deleted'] = $adb->query_result($res,$i,'deleted');
  358. }
  359. }
  360. else
  361. {
  362. $log->debug("Product id is empty. we cannot retrieve the associated products.");
  363. }
  364. $log->debug("Exit from function getTaxDetailsForProduct($productid)");
  365. return $tax_details;
  366. }
  367. /** Function used to delete the Inventory product details for the passed entity
  368. * @param int $objectid - entity id to which we want to delete the product details from REQUEST values where as the entity will be Purchase Order, Sales Order, Quotes or Invoice
  369. * @param string $return_old_values - string which contains the string return_old_values or may be empty, if the string is return_old_values then before delete old values will be retrieved
  370. * @return array $ext_prod_arr - if the second input parameter is 'return_old_values' then the array which contains the productid and quantity which will be retrieved before delete the product details will be returned otherwise return empty
  371. */
  372. function deleteInventoryProductDetails($focus)
  373. {
  374. global $log, $adb,$updateInventoryProductRel_update_product_array;
  375. $log->debug("Entering into function deleteInventoryProductDetails(".$focus->id.").");
  376. $product_info = $adb->pquery("SELECT productid, quantity, sequence_no, incrementondel from vtiger_inventoryproductrel WHERE id=?",array($focus->id));
  377. $numrows = $adb->num_rows($product_info);
  378. for($index = 0;$index <$numrows;$index++){
  379. $productid = $adb->query_result($product_info,$index,'productid');
  380. $sequence_no = $adb->query_result($product_info,$index,'sequence_no');
  381. $qty = $adb->query_result($product_info,$index,'quantity');
  382. $incrementondel = $adb->query_result($product_info,$index,'incrementondel');
  383. if($incrementondel){
  384. $focus->update_product_array[$focus->id][$sequence_no][$productid]= $qty;
  385. $sub_prod_query = $adb->pquery("SELECT productid from vtiger_inventorysubproductrel WHERE id=? AND sequence_no=?",array($focus->id,$sequence_no));
  386. if($adb->num_rows($sub_prod_query)>0){
  387. for($j=0;$j<$adb->num_rows($sub_prod_query);$j++){
  388. $sub_prod_id = $adb->query_result($sub_prod_query,$j,"productid");
  389. $focus->update_product_array[$focus->id][$sequence_no][$sub_prod_id]= $qty;
  390. }
  391. }
  392. }
  393. }
  394. $updateInventoryProductRel_update_product_array = $focus->update_product_array;
  395. $adb->pquery("delete from vtiger_inventoryproductrel where id=?", array($focus->id));
  396. $adb->pquery("delete from vtiger_inventorysubproductrel where id=?", array($focus->id));
  397. $adb->pquery("delete from vtiger_inventoryshippingrel where id=?", array($focus->id));
  398. $log->debug("Exit from function deleteInventoryProductDetails(".$focus->id.")");
  399. }
  400. function updateInventoryProductRel($entity)
  401. {
  402. global $log, $adb,$updateInventoryProductRel_update_product_array;
  403. $entity_id = vtws_getIdComponents($entity->getId());
  404. $entity_id = $entity_id[1];
  405. $update_product_array = $updateInventoryProductRel_update_product_array;
  406. $log->debug("Entering into function updateInventoryProductRel(".$entity_id.").");
  407. if(!empty($update_product_array)){
  408. foreach($update_product_array as $id=>$seq){
  409. foreach($seq as $seq=>$product_info)
  410. {
  411. foreach($product_info as $key=>$index){
  412. $updqtyinstk= getPrdQtyInStck($key);
  413. $upd_qty = $updqtyinstk+$index;
  414. updateProductQty($key, $upd_qty);
  415. }
  416. }
  417. }
  418. }
  419. $adb->pquery("UPDATE vtiger_inventoryproductrel SET incrementondel=1 WHERE id=?",array($entity_id));
  420. $product_info = $adb->pquery("SELECT productid,sequence_no, quantity from vtiger_inventoryproductrel WHERE id=?",array($entity_id));
  421. $numrows = $adb->num_rows($product_info);
  422. for($index = 0;$index <$numrows;$index++){
  423. $productid = $adb->query_result($product_info,$index,'productid');
  424. $qty = $adb->query_result($product_info,$index,'quantity');
  425. $sequence_no = $adb->query_result($product_info,$index,'sequence_no');
  426. $qtyinstk= getPrdQtyInStck($productid);
  427. $upd_qty = $qtyinstk-$qty;
  428. updateProductQty($productid, $upd_qty);
  429. $sub_prod_query = $adb->pquery("SELECT productid from vtiger_inventorysubproductrel WHERE id=? AND sequence_no=?",array($entity_id,$sequence_no));
  430. if($adb->num_rows($sub_prod_query)>0){
  431. for($j=0;$j<$adb->num_rows($sub_prod_query);$j++){
  432. $sub_prod_id = $adb->query_result($sub_prod_query,$j,"productid");
  433. $sqtyinstk= getPrdQtyInStck($sub_prod_id);
  434. $supd_qty = $sqtyinstk-$qty;
  435. updateProductQty($sub_prod_id, $supd_qty);
  436. }
  437. }
  438. }
  439. $log->debug("Exit from function updateInventoryProductRel(".$entity_id.")");
  440. }
  441. /** Function used to save the Inventory product details for the passed entity
  442. * @param object reference $focus - object reference to which we want to save the product details from REQUEST values where as the entity will be Purchase Order, Sales Order, Quotes or Invoice
  443. * @param string $module - module name
  444. * @param $update_prod_stock - true or false (default), if true we have to update the stock for PO only
  445. * @return void
  446. */
  447. function saveInventoryProductDetails(&$focus, $module, $update_prod_stock='false', $updateDemand='')
  448. {
  449. global $log, $adb;
  450. $id=$focus->id;
  451. $log->debug("Entering into function saveInventoryProductDetails($module).");
  452. //Added to get the convertid
  453. if(isset($_REQUEST['convert_from']) && $_REQUEST['convert_from'] !='')
  454. {
  455. $id=$_REQUEST['return_id'];
  456. }
  457. else if(isset($_REQUEST['duplicate_from']) && $_REQUEST['duplicate_from'] !='')
  458. {
  459. $id=$_REQUEST['duplicate_from'];
  460. }
  461. $ext_prod_arr = Array();
  462. if($focus->mode == 'edit')
  463. {
  464. if($_REQUEST['taxtype'] == 'group')
  465. $all_available_taxes = getAllTaxes('available','','edit',$id);
  466. $return_old_values = '';
  467. if($module != 'PurchaseOrder')
  468. {
  469. $return_old_values = 'return_old_values';
  470. }
  471. //we will retrieve the existing product details and store it in a array and then delete all the existing product details and save new values, retrieve the old value and update stock only for SO, Quotes and Invoice not for PO
  472. //$ext_prod_arr = deleteInventoryProductDetails($focus->id,$return_old_values);
  473. deleteInventoryProductDetails($focus);
  474. }
  475. else
  476. {
  477. if($_REQUEST['taxtype'] == 'group')
  478. $all_available_taxes = getAllTaxes('available','','edit',$id);
  479. }
  480. $tot_no_prod = $_REQUEST['totalProductCount'];
  481. //If the taxtype is group then retrieve all available taxes, else retrive associated taxes for each product inside loop
  482. $prod_seq=1;
  483. for($i=1; $i<=$tot_no_prod; $i++)
  484. {
  485. //if the product is deleted then we should avoid saving the deleted products
  486. if($_REQUEST["deleted".$i] == 1)
  487. continue;
  488. $prod_id = $_REQUEST['hdnProductId'.$i];
  489. if(isset($_REQUEST['productDescription'.$i]))
  490. $description = $_REQUEST['productDescription'.$i];
  491. /*else{
  492. $desc_duery = "select vtiger_crmentity.description AS product_description from vtiger_crmentity where vtiger_crmentity.crmid=?";
  493. $desc_res = $adb->pquery($desc_duery,array($prod_id));
  494. $description = $adb->query_result($desc_res,0,"product_description");
  495. } */
  496. $qty = $_REQUEST['qty'.$i];
  497. $listprice = $_REQUEST['listPrice'.$i];
  498. $comment = $_REQUEST['comment'.$i];
  499. //we have to update the Product stock for PurchaseOrder if $update_prod_stock is true
  500. if($module == 'PurchaseOrder' && $update_prod_stock == 'true')
  501. {
  502. addToProductStock($prod_id,$qty);
  503. }
  504. if($module == 'SalesOrder')
  505. {
  506. if($updateDemand == '-')
  507. {
  508. deductFromProductDemand($prod_id,$qty);
  509. }
  510. elseif($updateDemand == '+')
  511. {
  512. addToProductDemand($prod_id,$qty);
  513. }
  514. }
  515. $query ="insert into vtiger_inventoryproductrel(id, productid, sequence_no, quantity, listprice, comment, description) values(?,?,?,?,?,?,?)";
  516. $qparams = array($focus->id,$prod_id,$prod_seq,$qty,$listprice,$comment,$description);
  517. $adb->pquery($query,$qparams);
  518. $lineitem_id = $adb->getLastInsertID();
  519. $sub_prod_str = $_REQUEST['subproduct_ids'.$i];
  520. if (!empty($sub_prod_str)) {
  521. $sub_prod = split(":",$sub_prod_str);
  522. for($j=0;$j<count($sub_prod);$j++){
  523. $query ="insert into vtiger_inventorysubproductrel(id, sequence_no, productid) values(?,?,?)";
  524. $qparams = array($focus->id,$prod_seq,$sub_prod[$j]);
  525. $adb->pquery($query,$qparams);
  526. }
  527. }
  528. $prod_seq++;
  529. if($module != 'PurchaseOrder')
  530. {
  531. //update the stock with existing details
  532. updateStk($prod_id,$qty,$focus->mode,$ext_prod_arr,$module);
  533. }
  534. //we should update discount and tax details
  535. $updatequery = "update vtiger_inventoryproductrel set ";
  536. $updateparams = array();
  537. //set the discount percentage or discount amount in update query, then set the tax values
  538. if($_REQUEST['discount_type'.$i] == 'percentage')
  539. {
  540. $updatequery .= " discount_percent=?,";
  541. array_push($updateparams, $_REQUEST['discount_percentage'.$i]);
  542. }
  543. elseif($_REQUEST['discount_type'.$i] == 'amount')
  544. {
  545. $updatequery .= " discount_amount=?,";
  546. $discount_amount = $_REQUEST['discount_amount'.$i];
  547. array_push($updateparams, $discount_amount);
  548. }
  549. if($_REQUEST['taxtype'] == 'group')
  550. {
  551. for($tax_count=0;$tax_count<count($all_available_taxes);$tax_count++)
  552. {
  553. $tax_name = $all_available_taxes[$tax_count]['taxname'];
  554. $tax_val = $all_available_taxes[$tax_count]['percentage'];
  555. $request_tax_name = $tax_name."_group_percentage";
  556. if(isset($_REQUEST[$request_tax_name]))
  557. $tax_val =$_REQUEST[$request_tax_name];
  558. $updatequery .= " $tax_name = ?,";
  559. array_push($updateparams,$tax_val);
  560. }
  561. $updatequery = trim($updatequery,',')." where id=? and productid=? and lineitem_id = ?";
  562. array_push($updateparams,$focus->id,$prod_id, $lineitem_id);
  563. }
  564. else
  565. {
  566. $taxes_for_product = getTaxDetailsForProduct($prod_id,'all');
  567. for($tax_count=0;$tax_count<count($taxes_for_product);$tax_count++)
  568. {
  569. $tax_name = $taxes_for_product[$tax_count]['taxname'];
  570. $request_tax_name = $tax_name."_percentage".$i;
  571. $updatequery .= " $tax_name = ?,";
  572. array_push($updateparams, $_REQUEST[$request_tax_name]);
  573. }
  574. $updatequery = trim($updatequery,',')." where id=? and productid=? and lineitem_id = ?";
  575. array_push($updateparams, $focus->id,$prod_id, $lineitem_id);
  576. }
  577. // jens 2006/08/19 - protect against empy update queries
  578. if( !preg_match( '/set\s+where/i', $updatequery)) {
  579. $adb->pquery($updatequery,$updateparams);
  580. }
  581. }
  582. //we should update the netprice (subtotal), taxtype, group discount, S&H charge, S&H taxes, adjustment and total
  583. //netprice, group discount, taxtype, S&H amount, adjustment and total to entity table
  584. $updatequery = " update $focus->table_name set ";
  585. $updateparams = array();
  586. $subtotal = $_REQUEST['subtotal'];
  587. $updatequery .= " subtotal=?,";
  588. array_push($updateparams, $subtotal);
  589. $updatequery .= " taxtype=?,";
  590. array_push($updateparams, $_REQUEST['taxtype']);
  591. //for discount percentage or discount amount
  592. if($_REQUEST['discount_type_final'] == 'percentage')
  593. {
  594. $updatequery .= " discount_percent=?,";
  595. array_push($updateparams, $_REQUEST['discount_percentage_final']);
  596. }
  597. elseif($_REQUEST['discount_type_final'] == 'amount')
  598. {
  599. $discount_amount_final = $_REQUEST['discount_amount_final'];
  600. $updatequery .= " discount_amount=?,";
  601. array_push($updateparams, $discount_amount_final);
  602. }
  603. $shipping_handling_charge = $_REQUEST['shipping_handling_charge'];
  604. $updatequery .= " s_h_amount=?,";
  605. array_push($updateparams, $shipping_handling_charge);
  606. //if the user gave - sign in adjustment then add with the value
  607. $adjustmentType = '';
  608. if($_REQUEST['adjustmentType'] == '-')
  609. $adjustmentType = $_REQUEST['adjustmentType'];
  610. $adjustment = $_REQUEST['adjustment'];
  611. $updatequery .= " adjustment=?,";
  612. array_push($updateparams, $adjustmentType.$adjustment);
  613. $total = $_REQUEST['total'];
  614. $updatequery .= " total=?";
  615. array_push($updateparams, $total);
  616. //$id_array = Array('PurchaseOrder'=>'purchaseorderid','SalesOrder'=>'salesorderid','Quotes'=>'quoteid','Invoice'=>'invoiceid');
  617. //Added where condition to which entity we want to update these values
  618. $updatequery .= " where ".$focus->table_index."=?";
  619. array_push($updateparams, $focus->id);
  620. $adb->pquery($updatequery,$updateparams);
  621. //to save the S&H tax details in vtiger_inventoryshippingrel table
  622. $sh_tax_details = getAllTaxes('all','sh');
  623. $sh_query_fields = "id,";
  624. $sh_query_values = "?,";
  625. $sh_query_params = array($focus->id);
  626. for($i=0;$i<count($sh_tax_details);$i++)
  627. {
  628. $tax_name = $sh_tax_details[$i]['taxname']."_sh_percent";
  629. if($_REQUEST[$tax_name] != '')
  630. {
  631. $sh_query_fields .= $sh_tax_details[$i]['taxname'].",";
  632. $sh_query_values .= "?,";
  633. array_push($sh_query_params, $_REQUEST[$tax_name]);
  634. }
  635. }
  636. $sh_query_fields = trim($sh_query_fields,',');
  637. $sh_query_values = trim($sh_query_values,',');
  638. $sh_query = "insert into vtiger_inventoryshippingrel($sh_query_fields) values($sh_query_values)";
  639. $adb->pquery($sh_query,$sh_query_params);
  640. $log->debug("Exit from function saveInventoryProductDetails($module).");
  641. }
  642. /** function used to get the tax type for the entity (PO, SO, Quotes or Invoice)
  643. * @param string $module - module name
  644. * @param int $id - id of the PO or SO or Quotes or Invoice
  645. * @return string $taxtype - taxtype for the given entity which will be individual or group
  646. */
  647. function getInventoryTaxType($module, $id)
  648. {
  649. global $log, $adb;
  650. $log->debug("Entering into function getInventoryTaxType($module, $id).");
  651. $inv_table_array = Array('PurchaseOrder'=>'vtiger_purchaseorder','SalesOrder'=>'vtiger_salesorder','Quotes'=>'vtiger_quotes','Invoice'=>'vtiger_invoice');
  652. $inv_id_array = Array('PurchaseOrder'=>'purchaseorderid','SalesOrder'=>'salesorderid','Quotes'=>'quoteid','Invoice'=>'invoiceid');
  653. $res = $adb->pquery("select taxtype from $inv_table_array[$module] where $inv_id_array[$module]=?", array($id));
  654. $taxtype = $adb->query_result($res,0,'taxtype');
  655. $log->debug("Exit from function getInventoryTaxType($module, $id).");
  656. return $taxtype;
  657. }
  658. /** function used to get the price type for the entity (PO, SO, Quotes or Invoice)
  659. * @param string $module - module name
  660. * @param int $id - id of the PO or SO or Quotes or Invoice
  661. * @return string $pricetype - pricetype for the given entity which will be unitprice or secondprice
  662. */
  663. function getInventoryCurrencyInfo($module, $id)
  664. {
  665. global $log, $adb;
  666. $log->debug("Entering into function getInventoryCurrencyInfo($module, $id).");
  667. $inv_table_array = Array('PurchaseOrder'=>'vtiger_purchaseorder','SalesOrder'=>'vtiger_salesorder','Quotes'=>'vtiger_quotes','Invoice'=>'vtiger_invoice');
  668. $inv_id_array = Array('PurchaseOrder'=>'purchaseorderid','SalesOrder'=>'salesorderid','Quotes'=>'quoteid','Invoice'=>'invoiceid');
  669. $inventory_table = $inv_table_array[$module];
  670. $inventory_id = $inv_id_array[$module];
  671. $res = $adb->pquery("select currency_id, $inventory_table.conversion_rate as conv_rate, vtiger_currency_info.* from $inventory_table
  672. inner join vtiger_currency_info on $inventory_table.currency_id = vtiger_currency_info.id
  673. where $inventory_id=?", array($id));
  674. $currency_info = array();
  675. $currency_info['currency_id'] = $adb->query_result($res,0,'currency_id');
  676. $currency_info['conversion_rate'] = $adb->query_result($res,0,'conv_rate');
  677. $currency_info['currency_name'] = $adb->query_result($res,0,'currency_name');
  678. $currency_info['currency_code'] = $adb->query_result($res,0,'currency_code');
  679. $currency_info['currency_symbol'] = $adb->query_result($res,0,'currency_symbol');
  680. $log->debug("Exit from function getInventoryCurrencyInfo($module, $id).");
  681. return $currency_info;
  682. }
  683. /** function used to get the taxvalue which is associated with a product for PO/SO/Quotes or Invoice
  684. * @param int $id - id of PO/SO/Quotes or Invoice
  685. * @param int $productid - product id
  686. * @param string $taxname - taxname to which we want the value
  687. * @return float $taxvalue - tax value
  688. */
  689. function getInventoryProductTaxValue($id, $productid, $taxname)
  690. {
  691. global $log, $adb;
  692. $log->debug("Entering into function getInventoryProductTaxValue($id, $productid, $taxname).");
  693. $res = $adb->pquery("select $taxname from vtiger_inventoryproductrel where id = ? and productid = ?", array($id, $productid));
  694. $taxvalue = $adb->query_result($res,0,$taxname);
  695. if($taxvalue == '')
  696. $taxvalue = '0.00';
  697. $log->debug("Exit from function getInventoryProductTaxValue($id, $productid, $taxname).");
  698. return $taxvalue;
  699. }
  700. /** function used to get the shipping & handling tax percentage for the given inventory id and taxname
  701. * @param int $id - entity id which will be PO/SO/Quotes or Invoice id
  702. * @param string $taxname - shipping and handling taxname
  703. * @return float $taxpercentage - shipping and handling taxpercentage which is associated with the given entity
  704. */
  705. function getInventorySHTaxPercent($id, $taxname)
  706. {
  707. global $log, $adb;
  708. $log->debug("Entering into function getInventorySHTaxPercent($id, $taxname)");
  709. $res = $adb->pquery("select $taxname from vtiger_inventoryshippingrel where id= ?", array($id));
  710. $taxpercentage = $adb->query_result($res,0,$taxname);
  711. if($taxpercentage == '')
  712. $taxpercentage = '0.00';
  713. $log->debug("Exit from function getInventorySHTaxPercent($id, $taxname)");
  714. return $taxpercentage;
  715. }
  716. /** Function used to get the list of all Currencies as a array
  717. * @param string available - if 'all' returns all the currencies, default value 'available' returns only the currencies which are available for use.
  718. * return array $currency_details - return details of all the currencies as a array
  719. */
  720. function getAllCurrencies($available='available') {
  721. global $adb, $log;
  722. $log->debug("Entering into function getAllCurrencies($available)");
  723. $sql = "select * from vtiger_currency_info";
  724. if ($available != 'all') {
  725. $sql .= " where currency_status='Active' and deleted=0";
  726. }
  727. $res=$adb->pquery($sql, array());
  728. $noofrows = $adb->num_rows($res);
  729. for($i=0;$i<$noofrows;$i++)
  730. {
  731. $currency_details[$i]['currencylabel'] = $adb->query_result($res,$i,'currency_name');
  732. $currency_details[$i]['currencycode'] = $adb->query_result($res,$i,'currency_code');
  733. $currency_details[$i]['currencysymbol'] = $adb->query_result($res,$i,'currency_symbol');
  734. $currency_details[$i]['curid'] = $adb->query_result($res,$i,'id');
  735. $currency_details[$i]['conversionrate'] = $adb->query_result($res,$i,'conversion_rate');
  736. $currency_details[$i]['curname'] = 'curname' . $adb->query_result($res,$i,'id');
  737. }
  738. $log->debug("Entering into function getAllCurrencies($available)");
  739. return $currency_details;
  740. }
  741. /** Function used to get all the price details for different currencies which are associated to the given product
  742. * @param int $productid - product id to which we want to get all the associated prices
  743. * @param decimal $unit_price - Unit price of the product
  744. * @param string $available - available or available_associated where as default is available, if available then the prices in the currencies which are available now will be returned, otherwise if the value is available_associated then prices of all the associated currencies will be retruned
  745. * @return array $price_details - price details as a array with productid, curid, curname
  746. */
  747. function getPriceDetailsForProduct($productid, $unit_price, $available='available', $itemtype='Products')
  748. {
  749. global $log, $adb;
  750. $log->debug("Entering into function getPriceDetailsForProduct($productid)");
  751. if($productid != '')
  752. {
  753. $product_currency_id = getProductBaseCurrency($productid, $itemtype);
  754. $product_base_conv_rate = getBaseConversionRateForProduct($productid,'edit',$itemtype);
  755. // Detail View
  756. if ($available == 'available_associated') {
  757. $query = "select vtiger_currency_info.*, vtiger_productcurrencyrel.converted_price, vtiger_productcurrencyrel.actual_price
  758. from vtiger_currency_info
  759. inner join vtiger_productcurrencyrel on vtiger_currency_info.id = vtiger_productcurrencyrel.currencyid
  760. where vtiger_currency_info.currency_status = 'Active' and vtiger_currency_info.deleted=0
  761. and vtiger_productcurrencyrel.productid = ? and vtiger_currency_info.id != ?";
  762. $params = array($productid, $product_currency_id);
  763. } else { // Edit View
  764. $query = "select vtiger_currency_info.*, vtiger_productcurrencyrel.converted_price, vtiger_productcurrencyrel.actual_price
  765. from vtiger_currency_info
  766. left join vtiger_productcurrencyrel
  767. on vtiger_currency_info.id = vtiger_productcurrencyrel.currencyid and vtiger_productcurrencyrel.productid = ?
  768. where vtiger_currency_info.currency_status = 'Active' and vtiger_currency_info.deleted=0";
  769. $params = array($productid);
  770. }
  771. //Postgres 8 fixes
  772. if( $adb->dbType == "pgsql")
  773. $query = fixPostgresQuery( $query, $log, 0);
  774. $res = $adb->pquery($query, $params);
  775. for($i=0;$i<$adb->num_rows($res);$i++)
  776. {
  777. $price_details[$i]['productid'] = $productid;
  778. $price_details[$i]['currencylabel'] = $adb->query_result($res,$i,'currency_name');
  779. $price_details[$i]['currencycode'] = $adb->query_result($res,$i,'currency_code');
  780. $price_details[$i]['currencysymbol'] = $adb->query_result($res,$i,'currency_symbol');
  781. $currency_id = $adb->query_result($res,$i,'id');
  782. $price_details[$i]['curid'] = $currency_id;
  783. $price_details[$i]['curname'] = 'curname' . $adb->query_result($res,$i,'id');
  784. $cur_value = $adb->query_result($res,$i,'actual_price');
  785. // Get the conversion rate for the given currency, get the conversion rate of the product currency to base currency.
  786. // Both together will be the actual conversion rate for the given currency.
  787. $conversion_rate = $adb->query_result($res,$i,'conversion_rate');
  788. $actual_conversion_rate = $product_base_conv_rate * $conversion_rate;
  789. if ($cur_value == null || $cur_value == '') {
  790. $price_details[$i]['check_value'] = false;
  791. if ($unit_price != null) {
  792. $cur_value = convertFromMasterCurrency($unit_price, $actual_conversion_rate);
  793. } else {
  794. $cur_value = '0';
  795. }
  796. } else {
  797. $price_details[$i]['check_value'] = true;
  798. }
  799. $price_details[$i]['curvalue'] = CurrencyField::convertToUserFormat($cur_value, null, true);
  800. $price_details[$i]['conversionrate'] = $actual_conversion_rate;
  801. $is_basecurrency = false;
  802. if ($currency_id == $product_currency_id) {
  803. $is_basecurrency = true;
  804. }
  805. $price_details[$i]['is_basecurrency'] = $is_basecurrency;
  806. }
  807. }
  808. else
  809. {
  810. if($available == 'available') { // Create View
  811. global $current_user;
  812. $user_currency_id = fetchCurrency($current_user->id);
  813. $query = "select vtiger_currency_info.* from vtiger_currency_info
  814. where vtiger_currency_info.currency_status = 'Active' and vtiger_currency_info.deleted=0";
  815. $params = array();
  816. $res = $adb->pquery($query, $params);
  817. for($i=0;$i<$adb->num_rows($res);$i++)
  818. {
  819. $price_details[$i]['currencylabel'] = $adb->query_result($res,$i,'currency_name');
  820. $price_details[$i]['currencycode'] = $adb->query_result($res,$i,'currency_code');
  821. $price_details[$i]['currencysymbol'] = $adb->query_result($res,$i,'currency_symbol');
  822. $currency_id = $adb->query_result($res,$i,'id');
  823. $price_details[$i]['curid'] = $currency_id;
  824. $price_details[$i]['curname'] = 'curname' . $adb->query_result($res,$i,'id');
  825. // Get the conversion rate for the given currency, get the conversion rate of the product currency(logged in user's currency) to base currency.
  826. // Both together will be the actual conversion rate for the given currency.
  827. $conversion_rate = $adb->query_result($res,$i,'conversion_rate');
  828. $user_cursym_convrate = getCurrencySymbolandCRate($user_currency_id);
  829. $product_base_conv_rate = 1 / $user_cursym_convrate['rate'];
  830. $actual_conversion_rate = $product_base_conv_rate * $conversion_rate;
  831. $price_details[$i]['check_value'] = false;
  832. $price_details[$i]['curvalue'] = '0';
  833. $price_details[$i]['conversionrate'] = $actual_conversion_rate;
  834. $is_basecurrency = false;
  835. if ($currency_id == $user_currency_id) {
  836. $is_basecurrency = true;
  837. }
  838. $price_details[$i]['is_basecurrency'] = $is_basecurrency;
  839. }
  840. } else {
  841. $log->debug("Product id is empty. we cannot retrieve the associated prices.");
  842. }
  843. }
  844. $log->debug("Exit from function getPriceDetailsForProduct($productid)");
  845. return $price_details;
  846. }
  847. /** Function used to get the base currency used for the given Product
  848. * @param int $productid - product id for which we want to get the id of the base currency
  849. * @return int $currencyid - id of the base currency for the given product
  850. */
  851. function getProductBaseCurrency($productid,$module='Products') {
  852. global $adb, $log;
  853. if ($module == 'Services') {
  854. $sql = "select currency_id from vtiger_service where serviceid=?";
  855. } else {
  856. $sql = "select currency_id from vtiger_products where productid=?";
  857. }
  858. $params = array($productid);
  859. $res = $adb->pquery($sql, $params);
  860. $currencyid = $adb->query_result($res, 0, 'currency_id');
  861. return $currencyid;
  862. }
  863. /** Function used to get the conversion rate for the product base currency with respect to the CRM base currency
  864. * @param int $productid - product id for which we want to get the conversion rate of the base currency
  865. * @param string $mode - Mode in which the function is called
  866. * @return number $conversion_rate - conversion rate of the base currency for the given product based on the CRM base currency
  867. */
  868. function getBaseConversionRateForProduct($productid, $mode='edit', $module='Products') {
  869. global $adb, $log, $current_user;
  870. if ($mode == 'edit') {
  871. if ($module == 'Services') {
  872. $sql = "select conversion_rate from vtiger_service inner join vtiger_currency_info
  873. on vtiger_service.currency_id = vtiger_currency_info.id where vtiger_service.serviceid=?";
  874. } else {
  875. $sql = "select conversion_rate from vtiger_products inner join vtiger_currency_info
  876. on vtiger_products.currency_id = vtiger_currency_info.id where vtiger_products.productid=?";
  877. }
  878. $params = array($productid);
  879. } else {
  880. $sql = "select conversion_rate from vtiger_currency_info where id=?";
  881. $params = array(fetchCurrency($current_user->id));
  882. }
  883. $res = $adb->pquery($sql, $params);
  884. $conv_rate = $adb->query_result($res, 0, 'conversion_rate');
  885. return 1 / $conv_rate;
  886. }
  887. /** Function used to get the prices for the given list of products based in the specified currency
  888. * @param int $currencyid - currency id based on which the prices have to be provided
  889. * @param array $product_ids - List of product id's for which we want to get the price based on given currency
  890. * @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'
  891. */
  892. function getPricesForProducts($currencyid, $product_ids, $module='Products') {
  893. global $adb,$log,$current_user;
  894. $price_list = array();
  895. if (count($product_ids) > 0) {
  896. if ($module == 'Services') {
  897. $query = "SELECT vtiger_currency_info.id, vtiger_currency_info.conversion_rate, " .
  898. "vtiger_service.serviceid AS productid, vtiger_service.unit_price, " .
  899. "vtiger_productcurrencyrel.actual_price " .
  900. "FROM (vtiger_currency_info, vtiger_service) " .
  901. "left join vtiger_productcurrencyrel on vtiger_service.serviceid = vtiger_productcurrencyrel.productid " .
  902. "and vtiger_currency_info.id = vtiger_productcurrencyrel.currencyid " .
  903. "where vtiger_service.serviceid in (". generateQuestionMarks($product_ids) .") and vtiger_currency_info.id = ?";
  904. } else {
  905. $query = "SELECT vtiger_currency_info.id, vtiger_currency_info.conversion_rate, " .
  906. "vtiger_products.productid, vtiger_products.unit_price, " .
  907. "vtiger_productcurrencyrel.actual_price " .
  908. "FROM (vtiger_currency_info, vtiger_products) " .
  909. "left join vtiger_productcurrencyrel on vtiger_products.productid = vtiger_productcurrencyrel.productid " .
  910. "and vtiger_currency_info.id = vtiger_productcurrencyrel.currencyid " .
  911. "where vtiger_products.productid in (". generateQuestionMarks($product_ids) .") and vtiger_currency_info.id = ?";
  912. }
  913. $params = array($product_ids, $currencyid);
  914. $result = $adb->pquery($query, $params);
  915. for($i=0;$i<$adb->num_rows($result);$i++)
  916. {
  917. $product_id = $adb->query_result($result, $i, 'productid');
  918. if(getFieldVisibilityPermission($module,$current_user->id,'unit_price') == '0') {
  919. $actual_price = $adb->query_result($result, $i, 'actual_price');
  920. if ($actual_price == null || $actual_price == '') {
  921. $unit_price = $adb->query_result($result, $i, 'unit_price');
  922. $product_conv_rate = $adb->query_result($result, $i, 'conversion_rate');
  923. $product_base_conv_rate = getBaseConversionRateForProduct($product_id,'edit',$module);
  924. $conversion_rate = $product_conv_rate * $product_base_conv_rate;
  925. $actual_price = $unit_price * $conversion_rate;
  926. }
  927. $price_list[$product_id] = $actual_price;
  928. } else {
  929. $price_list[$product_id] = '';
  930. }
  931. }
  932. }
  933. return $price_list;
  934. }
  935. /** Function used to get the currency used for the given Price book
  936. * @param int $pricebook_id - pricebook id for which we want to get the id of the currency used
  937. * @return int $currencyid - id of the currency used for the given pricebook
  938. */
  939. function getPriceBookCurrency($pricebook_id) {
  940. global $adb;
  941. $result = $adb->pquery("select currency_id from vtiger_pricebook where pricebookid=?", array($pricebook_id));
  942. $currency_id = $adb->query_result($result,0,'currency_id');
  943. return $currency_id;
  944. }
  945. ?>