439
edits
(→Change printed total price per product without tampering the subprice in the database: Adding margin implementation) |
(→Change printed total price per product without tampering the subprice in the database: Add implementation in remaining debt to pay + images) |
||
| Line 677: | Line 677: | ||
That's all, the price should be reflected in the margin. | That's all, the price should be reflected in the margin. | ||
[[File:Cf_wiki_coeff_margin.png]] | |||
=== Implementing the change of price in remaining debt to pay === | |||
Open /htdocs/core/class/commonobject.class.php and look for the function update_price(). | |||
You should see the following: | |||
<source lang="php"> | |||
function update_price($exclspec=0,$roundingadjust='none',$nodatabaseupdate=0,$seller='') | |||
{ | |||
global $conf; | |||
include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; | |||
</source> | |||
Just below the include_once line, add the following: | |||
<source lang="php"> | |||
// Coefficient (CustomFields) | |||
include_once DOL_DOCUMENT_ROOT.'/customfields/class/customfields.class.php'; | |||
include_once DOL_DOCUMENT_ROOT.'/customfields/conf/conf_customfields_func.lib.php'; | |||
$customfields = new CustomFields($this->db, $this->table_element_line); | |||
// Fetching line ids from parent object id | |||
$line_ids = $customfields->fetchAny('rowid', MAIN_DB_PREFIX.$this->table_element_line, $this->fk_element.' = '.$this->id); | |||
$line_ids = array_values_recursive('rowid', $line_ids); | |||
// Load custom fields | |||
$cf_records = $customfields->fetch($line_ids); | |||
// Reassociate the records with the line id (instead of the customfields record id) | |||
$cf_line_cfrowid_to_rowid = array_reassociate('rowid', 'fk_'.$this->table_element_line, $cf_records); | |||
$tmp = $cf_records; | |||
$cf_records = array(); | |||
foreach ($tmp as $k=>$v) { $cf_records[$cf_line_cfrowid_to_rowid[$k]] = $tmp[$k]; } | |||
</source> | |||
This will load the coefficient custom field (and also any other custom field you have defined for lines). | |||
Now scroll below, and you should see this: | |||
<source lang="php"> | |||
while ($i < $num) | |||
{ | |||
$obj = $this->db->fetch_object($resql); | |||
// Note: There is no check on detail line and no check on total, if $forcedroundingmode = 'none' | |||
if ($forcedroundingmode == '0') // Check if data on line are consistent. This may solve lines that were not consistent because set with $forcedroundingmode='auto' | |||
{ | |||
$localtax_array=array($obj->localtax1_type,$obj->localtax1_tx,$obj->localtax2_type,$obj->localtax2_tx); | |||
$tmpcal=calcul_price_total($obj->qty, $obj->up, $obj->remise_percent, $obj->vatrate, $obj->localtax1_tx, $obj->localtax2_tx, 0, 'HT', $obj->info_bits, $obj->product_type, $seller, $localtax_array); | |||
$diff=price2num($tmpcal[1] - $obj->total_tva, 'MT', 1); | |||
if ($diff) | |||
{ | |||
$sqlfix="UPDATE ".MAIN_DB_PREFIX.$this->table_element_line." SET ".$fieldtva." = ".$tmpcal[1].", total_ttc = ".$tmpcal[2]." WHERE rowid = ".$obj->rowid; | |||
dol_syslog('We found unconsistent data into detailed line (difference of '.$diff.') for line rowid = '.$obj->rowid." (total vat of line calculated=".$tmpcal[1].", database=".$obj->total_tva."). We fix the total_vat and total_ttc of line by running sqlfix = ".$sqlfix); | |||
$resqlfix=$this->db->query($sqlfix); | |||
if (! $resqlfix) dol_print_error($this->db,'Failed to update line'); | |||
$obj->total_tva = $tmpcal[1]; | |||
$obj->total_ttc = $tmpcal[2]; | |||
// | |||
} | |||
} | |||
</source> | |||
Just after, add the following: | |||
<source lang="php"> | |||
// Coefficient (CustomFields) | |||
$cf_coeff = $cf_records[$obj->rowid]->coefficient; | |||
if (empty($cf_coeff) and $cf_coeff !== 0) $cf_coeff = 1; // by default, an empty coefficient = 1 | |||
$obj->total_ht = $obj->total_ht * $cf_coeff; | |||
$obj->total_tva = $obj->total_tva * $cf_coeff; | |||
$obj->total_localtax1 = $obj->total_localtax1 * $cf_coeff; | |||
$obj->total_localtax2 = $obj->total_localtax2 * $cf_coeff; | |||
$obj->total_ttc = $obj->total_ttc * $cf_coeff; | |||
</source> | |||
This should do the trick. Now go to one of your invoices (or whatever module you are targetting), and you should see this: | |||
[[File:Cf_wiki_coeff_remaining_debt.png.png]] | |||
= Custom tax per product = | = Custom tax per product = | ||
edits