439
edits
(→Implementing the change of price in margin module: Add a note) |
(→Refresh the price instantly: Add javascript instant refresh code + more infos about commercial proposals) |
||
| Line 504: | Line 504: | ||
function customfields_field_aftersave_facture_global_coefficient (&$currentmodule, &$object, &$action, &$user, &$customfields, &$field, &$name, &$value, $fields) { | function customfields_field_aftersave_facture_global_coefficient (&$currentmodule, &$object, &$action, &$user, &$customfields, &$field, &$name, &$value, $fields) { | ||
// Force refreshing total price | // Force refreshing total price | ||
$object | $object->update_price(0, 'auto'); | ||
} | } | ||
</source> | </source> | ||
| Line 512: | Line 512: | ||
However, you will still notice one last glitch: when you edit the global_coefficient, the total price isn't reflected instantly, you must refresh the webpage once to see the new price. This is because of how the datasheet and the CustomFields hook are designed: CustomFields is called only exactly where the custom fields are place, thus it is called after the total price, the margin and the remaining debt are printed. Thus, CustomFields updates the value of your custom field only after all those informations get printed, and thus it cannot modify them at this stage. That's why those information are out-of-date, and you must refresh the webpage once to get to see the latest values. | However, you will still notice one last glitch: when you edit the global_coefficient, the total price isn't reflected instantly, you must refresh the webpage once to see the new price. This is because of how the datasheet and the CustomFields hook are designed: CustomFields is called only exactly where the custom fields are place, thus it is called after the total price, the margin and the remaining debt are printed. Thus, CustomFields updates the value of your custom field only after all those informations get printed, and thus it cannot modify them at this stage. That's why those information are out-of-date, and you must refresh the webpage once to get to see the latest values. | ||
There is | There is a workaround for this: refresh automatically the webpage (but delete the POST data to avoid an infinite loop). You just need to use this overloading code instead of the one above: | ||
<source lang="php"> | |||
function customfields_field_aftersave_facture_global_coefficient (&$currentmodule, &$object, &$action, &$user, &$customfields, &$field, &$name, &$value, $fields) { | |||
// Force refreshing total price | |||
$object->update_price(0, 'auto'); | |||
// Force refresh the page to update prices that were printed before the custom field aftersave was triggered | |||
print('<script type="text/javascript">window.location.href = window.location.pathname + window.location.search;</script>'); | |||
} | |||
</source> | |||
Another way to fix this issue would be to modify how CustomFields save the custom fields, by moving this stage at the very top. However, this would need another hook, which should be called at the very beginning of the page loading, which should be made only for this purpose (PageLoading), and should be available in all modules supported by CustomFields in order to not break anything. | |||
Note that you will need to create one overloading function for each module you want to support with the global_coefficient custom field. For example, if you want to support both invoices and commercial proposals, you will need two overloading functions: | |||
<source lang="php"> | |||
// Global coefficient for invoices (note the "facture" in the name of the function) | |||
function customfields_field_aftersave_facture_global_coefficient (&$currentmodule, &$object, &$action, &$user, &$customfields, &$field, &$name, &$value, $fields) { | |||
// Force refreshing total price | |||
$object->update_price(0, 'auto'); | |||
// Force refresh the page to update prices that were printed before the custom field aftersave was triggered | |||
print('<script type="text/javascript">window.location.href = window.location.pathname + window.location.search;</script>'); | |||
} | |||
// Global coefficient for commercial proposals (note the "propal" in the name of the function) | |||
function customfields_field_aftersave_propal_global_coefficient (&$currentmodule, &$object, &$action, &$user, &$customfields, &$field, &$name, &$value, $fields) { | |||
// Force refreshing total price | |||
$object->update_price(0, 'auto'); | |||
// Force refresh the page to update prices that were printed before the custom field aftersave was triggered | |||
print('<script type="text/javascript">window.location.href = window.location.pathname + window.location.search;</script>'); | |||
} | |||
</source> | |||
= Modify the total price PER product/service line with a coefficient (without tampering the discount) = | = Modify the total price PER product/service line with a coefficient (without tampering the discount) = | ||
edits