Line 196:
Line 196:
= Overloading functions =
= Overloading functions =
+
+
Here are a few concrete use cases of overloading functions to achieve various goals.
+
+
== Dynamic list selection based on another one ==
The goal is to show two custom fields on the Third-Party module: one which gives the zone (secteur) of the third-party (Isere, Alpes du sud, Haute-Savoie...) and the other which gives relative to the zone the list of all the ski resorts (station_a) inside the selected zone.
The goal is to show two custom fields on the Third-Party module: one which gives the zone (secteur) of the third-party (Isere, Alpes du sud, Haute-Savoie...) and the other which gives relative to the zone the list of all the ski resorts (station_a) inside the selected zone.
Line 210:
Line 214:
In CustomFields's admin panel, we create two custom fields:
In CustomFields's admin panel, we create two custom fields:
−
1-secteur: returns the list of all zones - type DropdownBox: enum('- Aucun','Alpes du Sud','export','Haute-Savoie','Isère', etc...)
+
−
2-station_a: returns the list of all ski resorts - type constraint on '''llx_k_station'''
+
1- secteur: returns the list of all zones - type DropdownBox: enum('- Aucun','Alpes du Sud','export','Haute-Savoie','Isère', etc...)
+
+
2- station_a: returns the list of all ski resorts - type constraint on '''llx_k_station'''
Here is the code to put in customfields_fields_extend.lib.php that will allow to show only the ski resorts corresponding to the selected zone:
Here is the code to put in customfields_fields_extend.lib.php that will allow to show only the ski resorts corresponding to the selected zone:
Line 237:
Line 243:
Thank's to manub for giving this case.
Thank's to manub for giving this case.
+
+
== Dynamic discount computation per products lines on propales ==
+
+
The goal here is to compute the discount for a product line in propales based on a computation on three custom fields.
+
+
First, create three custom fields for "Line commercial proposals": vl, cpl and lc.
+
+
Then add the following overloading function at the end of your customfields_fields_extend.lib.php file (read the comments for more informations):
+
+
<source lang="php">
+
function customfields_field_save_propaldet_vl (&$currentmodule, &$object, &$action, &$user, &$customfields, &$field, &$name) {
+
global $db; // allow to access database functions
+
+
// Fetch the new posted values
+
// All $_POST data are generically appended into the $object properties
+
$vl = $object->{$customfields->varprefix.'vl'};
+
$cpl = $object->{$customfields->varprefix.'cpl'};
+
$lc = $object->{$customfields->varprefix.'lc'};
+
+
// Compute the discount
+
$remise_calculee = (100 - $vl) * (100 - $cpl) * (100 - $lc);
+
+
// Clean parameters
+
include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
+
$remise_calculee = price2num($remise_calculee);
+
+
// Assign the new discount into the PropalLine object
+
$object->remise_percent = $remise_calculee;
+
+
// Update the price (won't be automatic)
+
global $mysoc;
+
$localtaxes_type=getLocalTaxesFromRate($object->tva_tx,0,$mysoc);
+
$tabprice=calcul_price_total($object->qty, $object->price, $object->remise_percent, $object->tva_tx, $object->localtax1_tx, $object->localtax2_tx, 0, 'HT', $object->info_bits, $object->type, '', $localtaxes_type);
+
$object->total_ht = $tabprice[0];
+
$object->total_tva = $tabprice[1];
+
$object->total_ttc = $tabprice[2];
+
$object->total_localtax1 = $tabprice[9];
+
$object->total_localtax2 = $tabprice[10];
+
+
// Update into database
+
$object->update(true); // true to disable the trigger, else it will recall customfields, which will call this overloading function, etc. in an infinite loop.
+
$object->update_total();
+
+
//$q = "UPDATE ".MAIN_DB_PREFIX."propaldet SET remise_percent=".$remise_calculee." WHERE rowid=".$object->rowid;
+
//$db->query($q);
+
+
// Finally, cleanup POST data to avoid conflicts next time, reinserting the same discount. Else the fields will remember these values and it may mistakenly reuse the same values.
+
unset($_POST[$customfields->varprefix."vl"]);
+
unset($_POST[$customfields->varprefix."cpl"]);
+
unset($_POST[$customfields->varprefix."lc"]);
+
}
+
</source>
= Linking Dolibarr objects from two different modules =
= Linking Dolibarr objects from two different modules =