This HowTo require a safe handling of PHP language, because you need to customize the settings in code.
Also you must understand, how our EventHandler module works and you need to install this module before you start.
This howto handle the following problem:
You have a field “Purchasing price” in Products module and wants to automatically load the price from this field, instead the default unit price, if you create a PurchaseOrder or another custom Inventory module.
To solve this, we define 3 Events. If you have installed Colorizer with File modifications, you only needs to add 2 new Events into vTiger Source.
At first create an EventHandler, like I describe in EventHandler Description. You need this file to do the custom assignments.
Let us start with implementation of the events within vTiger CRM code.
File: layouts/vlayout/modules/Inventory/resources/Edit.js ~Line 1712
Search:
var dataUrl = "index.php?module=Inventory&action=GetTaxes&record="+selectedItemData.id+"¤cy_id="+jQuery('#currency_id option:selected').val();
Replace with:
var dataUrl = "index.php?module=Inventory&action=GetTaxes&record="+selectedItemData.id+"¤cy_id="+jQuery('#currency_id option:selected').val()+"&src_module=" + jQuery('#module').val();
File: modules/Products/models/Record.php ~Line 17
Search:
return 'index.php?module=Inventory&action=GetTaxes&record='. $this->getId();
Insert before:
return EventHandler_Module_Model::do_filter('vtiger.filter.product.gettaxesurl', 'index.php?module=Inventory&action=GetTaxes&record='. $this->getId(), $this);
File: modules/Inventory/actions/GetTaxes.php ~Line 36
Search:
$response->setResult(array( $recordId => array( 'id'=>$recordId, 'name'=>decode_html($recordModel->getName()), 'taxes'=>$taxes, 'listprice'=>$listPrice, 'listpricevalues'=>$listPriceValues, 'description' => decode_html($recordModel->get('description')), 'quantityInStock' => $recordModel->get('qtyinstock') )));
Replace with:
$response->setResult(EventHandler_Module_Model::do_filter('vtiger.filter.productdata',array( $recordId => array( 'id'=>$recordId, 'name'=>decode_html($recordModel->getName()), 'taxes'=>$taxes, 'listprice'=>$listPrice, 'listpricevalues'=>$listPriceValues, 'description' => decode_html($recordModel->get('description')), 'quantityInStock' => $recordModel->get('qtyinstock') )), $recordModel));
The next one only needs to be done, if you do NOT have the Colorizer installed.
You could check this, if you search for vtiger.filter.listview.querygenerator.query in the following file. If you found something, do NOT add a second one.
Also maybe Colorizer ask to delete your manually added filter, if you install this module later. Because it is the same filter, it is not a problem to remove and let Colorizer add automatically.
File: modules/Products/models/ListView.php ~Line 118
Search: (You will found two matches within the file. Choose the one in function getListViewEntries)
$listQuery = $this->getQuery();
Insert After:
$this->set( 'query_generator', EventHandler_Module_Model::do_filter( array( 'vtiger.filter.listview.querygenerator.after', 'vtiger.filter.listview.'.strtolower($moduleName).'.querygenerator.after' ), $this->get('query_generator'), $pagingModel ) ); $listQuery = EventHandler_Module_Model::do_filter("vtiger.filter.listview.querygenerator.query", $listQuery, $this->get('query_generator'));
This was all within vTigerCRM source.
Now you need to attach your created EventHandler to these 3 Events:
vtiger.filter.product.gettaxesurl vtiger.filter.productdata vtiger.filter.listview.querygenerator.query
If you already use the handleFilter function for other filters, please filter the information you need from the next source. If you do not have a handleFilter function in your class, you could use this one:
//....
public function handleFilter($filtername, $return) {
$adb = \PearDatabase::getInstance();
switch($filtername) {
case 'vtiger.filter.product.gettaxesurl':
$return .= '&src_module='.$_REQUEST['module'];
break;
case 'vtiger.filter.productdata':
if($_REQUEST['src_module'] == 'PurchaseOrder') {
$fieldName = 'cf_852';
/**
* @var $record \Vtiger_Record_Model
*/
$record = func_get_arg(2); // Here a "2" is not shown, but copied
$data = $return[$record->getId()];
$return[$record->getId()]['listprice'] = $record->get($fieldName);
foreach($data['listpricevalues'] as $index => $value) {
$return[$record->getId()]['listpricevalues'][$index] = $record->get($fieldName);
}
}
break;
case 'vtiger.filter.listview.querygenerator.query':
if($_REQUEST['module'] == 'PurchaseOrder') {
if(strpos($return, 'vtiger_productcf') === false) {
$parts = explode(' vtiger_products ', $return);
$parts[1] = ' INNER JOIN vtiger_productcf ON (vtiger_productcf.productid = vtiger_products.productid) ' . $parts[1];
}
$return = implode(' vtiger_products ', $parts);
$return = str_replace('vtiger_products.unit_price', 'vtiger_productcf.cf_852 as unit_price', $return);
}
break;
}
return $return;
}
//...
In this example, the custom field with the price is cf_852. You MUST change this.
Also if you want to change the module, which will trigger this field, change “PurchaseOrder” to another module. It will works for every Inventory module.
Like you recognize, it only does something if the module is correct. So you shouldn’t get problems in other modules. But like any modification: Test all possible situations to be sure, it works.