I created an extra fiels on invoices (called “program”) to allow user to select a service (so « rowid » of this service is stored in array_options[‹ options_program ›]). Do do that, I create a list from a table and I put following value : product:ref|label:rowid::fk_product_type=1
Now, I would like to create a second extra field where I would like to retrieve automaticaly the “description” on the service selected in my extrafield “program”. What code do I need to put in dolibarr field dedicated to the calculation of this new extrafield?
By searching on the internet and on the forum, I tested following 3 ways:
$SEL$ description FROM llx_product WHERE rowid=$objectoffield->array_options['options_program']
mysqli_fetch_array($resql = $db->query(sprintf('SELECT description FROM llx_product WHERE rowid = "' %s $objectoffield->array_options['options_program'] %s "', chr(46), chr(46))))["description"];
fetch_array($db->query("SELECT description FROM llx_product WHERE rowid = ?", $objectoffield->array_options['options_program']))['description']
So now, I would like to do a little bit more complex: I want to retrieve list of countries from the extra field “country” of the service (can be multiple values, comma separated) linked to the invoice (still via the extrafield “program”). So I did the same:
Unfortunatly, this formula is bringing list of countries IDs (instead of labels) coma separated.
So I tried the double look-up to retrieve labels from c_country table:
About countries, there is a Ccountry class in core/class/ccountry.class.php - but it is not possible to use this class in extrafields because this class is not loaded…
To do what you want, you must build and execute a query on your llx_c_country table, where “v3wn_” is the prefix you use…
With this following formula, you can get the result you expect…
($myval=((($myobj = new Product($db)) && ($myobj->fetchNoCompute($objectoffield->array_options[‘options_program’]))) > 0 ? $myobj->array_options[‘options_country’]: ‘0’)) &&
($sql=dol_concatdesc(‘SELECT label FROM v3wn_c_country WHERE rowid =’, $myval)) &&
($resql = $db->query($sql)) &&
($obj = $db->fetch_object($resql)) > 0 ? $obj->label : ‘’
retrieve well the country ID (in the example I gave, if I select “Namibia” on the associated service, the field is well dispalying the corresponding id: 161)
The next part of the formula you provided is also well working when I replace manualy the variable “$myval” with the ID:
I have a similar issue with this topic. I hope you can help if possible.
I want to create a new field recording the remaining unpaid balance for each customer. If I use it in the “societe” module, I can use the formula “$object->getOutstandingBills(‘customer’, 0)[‘opened’]”. But I want to print it on the invoices sent to customers, so I think I need to create an extra field similar to this in the “facture” module, or somehow print the “unpaid” extra field from the “societe” module on the invoice using the ODT form. I don’t know how to call the calculation formula from another module. Thank you very much.
Thank you for your replies (sorry for the delay I had heathcare issues).
It would be great! I never created a pluggin for now, I tryed to read the wiki page of the module builder but didn’t understand how it can be used to add button on existing module (like invoicing). Do you know if there is a tutorial for newbees somewhere?