Module CustomFields Cases: Difference between revisions

Jump to navigation Jump to search
Line 635: Line 635:
At this point, everything is done, you should see the following:
At this point, everything is done, you should see the following:
[[File:Cf_wiki_coeff_column.png]]
[[File:Cf_wiki_coeff_column.png]]
=== Implementing the change of price in margin module ===
If you enabled the margin module, you should see that the coefficient is not applied there, but it's easy to fix that.
Open the file /htdocs/core/class/commonobject.class.php and find the function getMarginInfos(), then inside find the following lines:
<source lang="php">
foreach($this->lines as $line) {
    if (empty($line->pa_ht) && isset($line->fk_fournprice) && !$force_price) {
        $product = new ProductFournisseur($this->db);
        if ($product->fetch_product_fournisseur_price($line->fk_fournprice))
            $line->pa_ht = $product->fourn_unitprice * (1 - $product->fourn_remise_percent / 100);
        if (isset($conf->global->MARGIN_TYPE) && $conf->global->MARGIN_TYPE == "2" && $product->fourn_unitcharges > 0)
            $line->pa_ht += $product->fourn_unitcharges;
    }
    // si prix d'achat non renseigné et devrait l'être, alors prix achat = prix vente
    if ((!isset($line->pa_ht) || $line->pa_ht == 0) && $line->subprice > 0 && (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1)) {
        $line->pa_ht = $line->subprice * (1 - ($line->remise_percent / 100));
    }
</source>
This is where the lines are filled with margin infos. The price has just been fetched here, and we will now modify it with the coefficient before the price gets used for margin computation.
Just below the code we found, paste the following:
<source lang="php">
// Coefficient (CustomFields)
// Include the facade API of CustomFields
dol_include_once('/customfields/lib/customfields_aux.lib.php');
// Filling the $object with customfields (you can then access customfields by doing $object->customfields->cf_yourfield)
$linecf = new stdClass();
$linecf->id = (!empty($line->id))?$line->id:$line->rowid;
$linecf->table_element = $line->table_element; // you can use $this->table_element_line here instead of 'facturedet' if you want this feature to work for every module supported by CustomFields
$customfields = customfields_fill_object($linecf);
if (!empty($linecf->customfields->cf_coefficient)) {
    $line->pa_ht = $line->pa_ht * $linecf->customfields->cf_coefficient;
    $line->subprice = $line->subprice * $linecf->customfields->cf_coefficient;
}
</source>
That's all, the price should be reflected in the margin.


= Custom tax per product =
= Custom tax per product =
439

edits

Navigation menu