diff --git a/htdocs/admin/multicurrency.php b/htdocs/admin/multicurrency.php index 2c9eba444b7b18e0a7209e2417dc78a43039e19d..de3c2f3965ae930597b34772e87cf63ad70665bf 100644 --- a/htdocs/admin/multicurrency.php +++ b/htdocs/admin/multicurrency.php @@ -33,6 +33,7 @@ require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php'; // Translations +$langs->load("admin"); $langs->load("multicurrency"); // Access control @@ -323,8 +324,17 @@ print '<input type="text" name="rate" value="" size="13" placeholder="'.$langs-> print '<input type="submit" class="button" value="'.$langs->trans("Add").'">'; print '</td></form></tr>'; +$var=!$var; +print '<tr '.$bc[$var].'>'; +print '<td>'.$conf->currency.$form->textwithpicto(' ', $langs->trans("BaseCurrency")).'</td>'; +print '<td align="center" width="20"> </td>'; +print '<td align="right" width="300">1'; +print '</td></form></tr>'; + foreach ($TCurrency as &$currency) { + if($currency->code == $conf->currency) continue; + $var=!$var; print '<tr '.$bc[$var].'>'; print '<td>'.$currency->code.' - '.$currency->name.'</td>'; @@ -334,10 +344,12 @@ foreach ($TCurrency as &$currency) print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">'; print '<input type="hidden" name="action" value="update_currency">'; print '<input type="hidden" name="fk_multicurrency" value="'.$currency->id.'">'; - print '<input type="text" name="rate" value="'.($currency->rate->rate ? $currency->rate->rate : '').'" size="13" /> '; + print '1 '.$conf->currency.' = '; + print '<input type="text" name="rate" value="'.($currency->rate->rate ? $currency->rate->rate : '').'" size="13" /> '.$currency->code.' '; print '<input type="submit" name="submit" class="button" value="'.$langs->trans("Modify").'"> '; print '<input type="submit" name="submit" class="button" value="'.$langs->trans("Delete").'">'; print '</form>'; + print '</td></tr>'; } diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php index ab562584b0ad4653ae3637d77cd68aeb9915b422..0228b6a2f1f5fb038c7785448742443cdf1b459a 100644 --- a/htdocs/compta/facture.php +++ b/htdocs/compta/facture.php @@ -281,7 +281,7 @@ if (empty($reshook)) // Multicurrency rate else if ($action == 'setmulticurrencyrate' && $user->rights->facture->creer) { - $result = $object->setMulticurrencyRate(price2num(GETPOST('multicurrency_tx'))); + $result = $object->setMulticurrencyRate(price2num(GETPOST('multicurrency_tx')), GETPOST('calculation_mode', 'int')); } else if ($action == 'setinvoicedate' && $user->rights->facture->creer) @@ -1314,6 +1314,7 @@ if (empty($reshook)) $predef=''; $product_desc=(GETPOST('dp_desc')?GETPOST('dp_desc'):''); $price_ht = GETPOST('price_ht'); + $price_ht_devise = GETPOST('multicurrency_price_ht'); if (GETPOST('prod_entry_mode') == 'free') { $idprod=0; @@ -1348,7 +1349,7 @@ if (empty($reshook)) setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Type')), null, 'errors'); $error ++; } - if (GETPOST('prod_entry_mode') == 'free' && empty($idprod) && (! ($price_ht >= 0) || $price_ht == '')) // Unit price can be 0 but not '' + if (GETPOST('prod_entry_mode') == 'free' && empty($idprod) && (! ($price_ht >= 0) || $price_ht == '') && empty($price_ht_devise)) // Unit price can be 0 but not '' { setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("UnitPriceHT")), null, 'errors'); $error ++; @@ -1505,6 +1506,7 @@ if (empty($reshook)) $desc = $product_desc; $type = GETPOST('type'); $fk_unit= GETPOST('units', 'alpha'); + $pu_ht_devise = price2num($price_ht_devise, 'MU'); } // Margin @@ -1524,7 +1526,7 @@ if (empty($reshook)) setEventMessages($mesg, null, 'errors'); } else { // Insert line - $result = $object->addline($desc, $pu_ht, $qty, $tva_tx, $localtax1_tx, $localtax2_tx, $idprod, $remise_percent, $date_start, $date_end, 0, $info_bits, '', $price_base_type, $pu_ttc, $type, - 1, $special_code, '', 0, GETPOST('fk_parent_line'), $fournprice, $buyingprice, $label, $array_options, $_POST['progress'], '', $fk_unit); + $result = $object->addline($desc, $pu_ht, $qty, $tva_tx, $localtax1_tx, $localtax2_tx, $idprod, $remise_percent, $date_start, $date_end, 0, $info_bits, '', $price_base_type, $pu_ttc, $type, - 1, $special_code, '', 0, GETPOST('fk_parent_line'), $fournprice, $buyingprice, $label, $array_options, $_POST['progress'], '', $fk_unit, $pu_ht_devise); if ($result > 0) { @@ -1604,6 +1606,7 @@ if (empty($reshook)) $pu_ht = GETPOST('price_ht'); $vat_rate = (GETPOST('tva_tx') ? GETPOST('tva_tx') : 0); $qty = GETPOST('qty'); + $pu_ht_devise = GETPOST('multicurrency_subprice'); // Define info_bits $info_bits = 0; @@ -1688,7 +1691,7 @@ if (empty($reshook)) $result = $object->updateline(GETPOST('lineid'), $description, $pu_ht, $qty, GETPOST('remise_percent'), $date_start, $date_end, $vat_rate, $localtax1_rate, $localtax2_rate, 'HT', $info_bits, $type, GETPOST('fk_parent_line'), 0, $fournprice, $buyingprice, $label, $special_code, $array_options, GETPOST('progress'), - $_POST['units']); + $_POST['units'],$pu_ht_devise); if ($result >= 0) { if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) { @@ -3234,10 +3237,16 @@ else if ($id > 0 || ! empty($ref)) print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editmulticurrencyrate&id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetMultiCurrencyCode'), 1) . '</a></td>'; print '</tr></table>'; print '</td><td>'; - if ($action == 'editmulticurrencyrate') { + if ($action == 'editmulticurrencyrate' || $action == 'actualizemulticurrencyrate') { + if($action == 'actualizemulticurrencyrate') { + list($object->fk_multicurrency, $object->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($object->db, $object->multicurrency_code); + } $form->form_multicurrency_rate($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->multicurrency_tx, 'multicurrency_tx', $object->multicurrency_code); } else { $form->form_multicurrency_rate($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->multicurrency_tx, 'none', $object->multicurrency_code); + print '<div class="inline-block"> '; + print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=actualizemulticurrencyrate">'.$langs->trans("ActualizeCurrency").'</a>'; + print '</div>'; } print '</td></tr>'; //} diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index 8e443e5f5562130e56bcb7ebde4258492a252916..67f58ac98756a049ccfb9b53b840c2d95029e3f5 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -2411,9 +2411,10 @@ class Facture extends CommonInvoice * @param int $situation_percent Situation advance percentage * @param int $fk_prev_id Previous situation line id reference * @param string $fk_unit Code of the unit to use. Null to use the default one + * @param double $pu_ht_devise Unit price in currency * @return int <0 if KO, Id of line if OK */ - function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1=0, $txlocaltax2=0, $fk_product=0, $remise_percent=0, $date_start='', $date_end='', $ventil=0, $info_bits=0, $fk_remise_except='', $price_base_type='HT', $pu_ttc=0, $type=self::TYPE_STANDARD, $rang=-1, $special_code=0, $origin='', $origin_id=0, $fk_parent_line=0, $fk_fournprice=null, $pa_ht=0, $label='', $array_options=0, $situation_percent=100, $fk_prev_id='', $fk_unit = null) + function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1=0, $txlocaltax2=0, $fk_product=0, $remise_percent=0, $date_start='', $date_end='', $ventil=0, $info_bits=0, $fk_remise_except='', $price_base_type='HT', $pu_ttc=0, $type=self::TYPE_STANDARD, $rang=-1, $special_code=0, $origin='', $origin_id=0, $fk_parent_line=0, $fk_fournprice=null, $pa_ht=0, $label='', $array_options=0, $situation_percent=100, $fk_prev_id='', $fk_unit = null, $pu_ht_devise = 0) { // Deprecation warning if ($label) { @@ -2493,7 +2494,7 @@ class Facture extends CommonInvoice // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva. - $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $mysoc, $localtaxes_type, $situation_percent, $this->multicurrency_tx); + $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $mysoc, $localtaxes_type, $situation_percent, $this->multicurrency_tx, $pu_ht_devise); $total_ht = $tabprice[0]; $total_tva = $tabprice[1]; @@ -2506,6 +2507,7 @@ class Facture extends CommonInvoice $multicurrency_total_ht = $tabprice[16]; $multicurrency_total_tva = $tabprice[17]; $multicurrency_total_ttc = $tabprice[18]; + $pu_ht_devise = $tabprice[19]; // Rank to use $rangtouse = $rang; @@ -2565,7 +2567,7 @@ class Facture extends CommonInvoice // Multicurrency $this->line->fk_multicurrency = $this->fk_multicurrency; $this->line->multicurrency_code = $this->multicurrency_code; - $this->line->multicurrency_subprice = price2num($this->line->subprice * $this->multicurrency_tx); + $this->line->multicurrency_subprice = $pu_ht_devise; $this->line->multicurrency_total_ht = $multicurrency_total_ht; $this->line->multicurrency_total_tva = $multicurrency_total_tva; $this->line->multicurrency_total_ttc = $multicurrency_total_ttc; @@ -2628,9 +2630,10 @@ class Facture extends CommonInvoice * @param array $array_options extrafields array * @param int $situation_percent Situation advance percentage * @param string $fk_unit Code of the unit to use. Null to use the default one + * @param double $pu_ht_devise Unit price in currency * @return int < 0 if KO, > 0 if OK */ - function updateline($rowid, $desc, $pu, $qty, $remise_percent, $date_start, $date_end, $txtva, $txlocaltax1=0, $txlocaltax2=0, $price_base_type='HT', $info_bits=0, $type= self::TYPE_STANDARD, $fk_parent_line=0, $skip_update_total=0, $fk_fournprice=null, $pa_ht=0, $label='', $special_code=0, $array_options=0, $situation_percent=0, $fk_unit = null) + function updateline($rowid, $desc, $pu, $qty, $remise_percent, $date_start, $date_end, $txtva, $txlocaltax1=0, $txlocaltax2=0, $price_base_type='HT', $info_bits=0, $type= self::TYPE_STANDARD, $fk_parent_line=0, $skip_update_total=0, $fk_fournprice=null, $pa_ht=0, $label='', $special_code=0, $array_options=0, $situation_percent=0, $fk_unit = null, $pu_ht_devise = 0) { global $conf,$user; // Deprecation warning @@ -2688,7 +2691,7 @@ class Facture extends CommonInvoice $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate. } - $tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $mysoc, $localtaxes_type, $situation_percent, $this->multicurrency_tx); + $tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $mysoc, $localtaxes_type, $situation_percent, $this->multicurrency_tx, $pu_ht_devise); $total_ht = $tabprice[0]; $total_tva = $tabprice[1]; @@ -2703,6 +2706,7 @@ class Facture extends CommonInvoice $multicurrency_total_ht = $tabprice[16]; $multicurrency_total_tva = $tabprice[17]; $multicurrency_total_ttc = $tabprice[18]; + $pu_ht_devise = $tabprice[19]; // Old properties: $price, $remise (deprecated) $price = $pu; @@ -2778,7 +2782,7 @@ class Facture extends CommonInvoice $this->line->pa_ht = $pa_ht; // Multicurrency - $this->line->multicurrency_subprice = price2num($this->line->subprice * $this->multicurrency_tx); + $this->line->multicurrency_subprice = $pu_ht_devise; $this->line->multicurrency_total_ht = $multicurrency_total_ht; $this->line->multicurrency_total_tva = $multicurrency_total_tva; $this->line->multicurrency_total_ttc = $multicurrency_total_ttc; diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index e9a7607d011f7c928c69209a7ec08089e73c0af6..c3b3d97d202c08a30040167925d474f085a7193e 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -1491,9 +1491,10 @@ abstract class CommonObject * Change the multicurrency rate * * @param double $rate multicurrency rate + * @param int $mode mode 1 : amounts in company currency will be recalculated, mode 2 : amounts in foreign currency * @return int >0 if OK, <0 if KO */ - function setMulticurrencyRate($rate) + function setMulticurrencyRate($rate, $mode=1) { dol_syslog(get_class($this).'::setMulticurrencyRate('.$id.')'); if ($this->statut >= 0 || $this->element == 'societe') @@ -1513,6 +1514,10 @@ abstract class CommonObject { foreach ($this->lines as &$line) { + if($mode == 1) { + $line->subprice = 0; + } + switch ($this->element) { case 'propal': $this->updateline($line->id, $line->subprice, $line->qty, $line->remise_percent, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, $line->desc, 'HT', $line->info_bits, $line->special_code, $line->fk_parent_line, $line->skip_update_total, $line->fk_fournprice, $line->pa_ht, $line->label, $line->product_type, $line->date_start, $line->date_end, $line->array_options, $line->fk_unit); @@ -1521,7 +1526,7 @@ abstract class CommonObject $this->updateline($line->id, $line->desc, $line->subprice, $line->qty, $line->remise_percent, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, 'HT', $line->info_bits, $line->date_start, $line->date_end, $line->product_type, $line->fk_parent_line, $line->skip_update_total, $line->fk_fournprice, $line->pa_ht, $line->label, $line->special_code, $line->array_options, $line->fk_unit); break; case 'facture': - $this->updateline($line->id, $line->desc, $line->subprice, $line->qty, $line->remise_percent, $line->date_start, $line->date_end, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, 'HT', $line->info_bits, $line->product_type, $line->fk_parent_line, $line->skip_update_total, $line->fk_fournprice, $line->pa_ht, $line->label, $line->special_code, $line->array_options, $line->situation_percent, $line->fk_unit); + $this->updateline($line->id, $line->desc, $line->subprice, $line->qty, $line->remise_percent, $line->date_start, $line->date_end, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, 'HT', $line->info_bits, $line->product_type, $line->fk_parent_line, $line->skip_update_total, $line->fk_fournprice, $line->pa_ht, $line->label, $line->special_code, $line->array_options, $line->situation_percent, $line->fk_unit, $line->multicurrency_subprice); break; case 'supplier_proposal': $this->updateline($line->id, $line->subprice, $line->qty, $line->remise_percent, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, $line->desc, 'HT', $line->info_bits, $line->special_code, $line->fk_parent_line, $line->skip_update_total, $line->fk_fournprice, $line->pa_ht, $line->label, $line->product_type, $line->array_options, $line->ref_fourn); @@ -3307,7 +3312,7 @@ abstract class CommonObject print '<td class="linecoluht" align="right" width="80">'.$langs->trans('PriceUHT').'</td>'; // Multicurrency - if (!empty($conf->multicurrency->enabled)) print '<td class="linecoluht_currency" align="right" width="80">'.$langs->trans('PriceUHTCurrency').'</td>'; + if (!empty($conf->multicurrency->enabled)) print '<td class="linecoluht_currency" align="right" width="80">'.$langs->trans('PriceUHTCurrency', $this->multicurrency_code).'</td>'; if ($inputalsopricewithtax) print '<td align="right" width="80">'.$langs->trans('PriceUTTC').'</td>'; @@ -3343,7 +3348,7 @@ abstract class CommonObject print '<td class="linecolht" align="right">'.$langs->trans('TotalHTShort').'</td>'; // Multicurrency - if (!empty($conf->multicurrency->enabled)) print '<td class="linecoltotalht_currency" align="right">'.$langs->trans('TotalHTShortCurrency').'</td>'; + if (!empty($conf->multicurrency->enabled)) print '<td class="linecoltotalht_currency" align="right">'.$langs->trans('TotalHTShortCurrency', $this->multicurrency_code).'</td>'; if ($outputalsopricetotalwithtax) print '<td align="right" width="80">'.$langs->trans('TotalTTCShort').'</td>'; diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 58f4ff9bbb4a5df5e73d39bb4a2808d2587b32d4..3c54cd0a525e28a6db376d8ce5f04e2fb0efef6b 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -3956,7 +3956,11 @@ class Form print '<form method="POST" action="'.$page.'">'; print '<input type="hidden" name="action" value="setmulticurrencyrate">'; print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">'; - print '<input type="text" name="'.$htmlname.'" value="'.(!empty($rate) ? price($rate) : 1).'" size="10" />'; + print '<input type="text" name="'.$htmlname.'" value="'.(!empty($rate) ? price($rate) : 1).'" size="10" /> '; + print '<select name="calculation_mode">'; + print '<option value="1">'.$currency.' > '.$conf->currency.'</option>'; + print '<option value="2">'.$conf->currency.' > '.$currency.'</option>'; + print '</select> '; print '<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">'; print '</form>'; } @@ -4231,6 +4235,10 @@ class Form } $out.= '</select>'; + // Make select dynamic + include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php'; + $out.= ajax_combobox($htmlname); + return $out; } diff --git a/htdocs/core/lib/price.lib.php b/htdocs/core/lib/price.lib.php index 840687ba69a81a54b96d8835d57434217eb2a72b..e94c0c7dcfe06f20ac856887f94cb12c4fa615fc 100644 --- a/htdocs/core/lib/price.lib.php +++ b/htdocs/core/lib/price.lib.php @@ -50,6 +50,7 @@ * @param array $localtaxes_array Array with localtaxes info array('0'=>type1,'1'=>rate1,'2'=>type2,'3'=>rate2) (loaded by getLocalTaxesFromRate(vatrate, 0, ...) function). * @param integer $progress Situation invoices progress (value from 0 to 100, 100 by default) * @param double $multicurrency_tx Currency rate (1 by default) + * @param double $pu_ht_devise Amount in currency * @return array [ * 0=total_ht, * 1=total_vat, (main vat only) @@ -70,8 +71,9 @@ * 16=multicurrency_total_ht * 17=multicurrency_total_tva * 18=multicurrency_total_ttc + * 19=multicurrency_pu_ht */ -function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller = '', $localtaxes_array='', $progress=100, $multicurrency_tx=1) +function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller = '', $localtaxes_array='', $progress=100, $multicurrency_tx=1, $pu_ht_devise=0) { global $conf,$mysoc,$db; @@ -140,6 +142,14 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocalt } else dol_print_error($db); } + + // pu calculation from pu_devise if pu empty + if(empty($pu) && !empty($pu_ht_devise)) { + $pu = $pu_ht_devise / $multicurrency_tx; + } else { + $pu_ht_devise = $pu * $multicurrency_tx; + } + // initialize total (may be HT or TTC depending on price_base_type) $tot_sans_remise = $pu * $qty * $progress / 100; $tot_avec_remise_ligne = $tot_sans_remise * (1 - ($remise_percent_ligne / 100)); @@ -332,6 +342,7 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocalt $result[16] = price2num($result[0] * $multicurrency_tx, 'MT'); $result[17] = price2num($result[1] * $multicurrency_tx, 'MT'); $result[18] = price2num($result[2] * $multicurrency_tx, 'MT'); + $result[19] = price2num($pu_ht_devise, 'MU'); // initialize result array //for ($i=0; $i <= 18; $i++) $result[$i] = (float) $result[$i]; diff --git a/htdocs/core/tpl/objectline_edit.tpl.php b/htdocs/core/tpl/objectline_edit.tpl.php index 8840879a805a2c842bc090088311d88a1dc2d12c..ce019d58deaf95a269d3c1557421bffed6210a31 100644 --- a/htdocs/core/tpl/objectline_edit.tpl.php +++ b/htdocs/core/tpl/objectline_edit.tpl.php @@ -122,6 +122,7 @@ $coldisplay=-1; // We remove first td print '></td>'; if (!empty($conf->multicurrency->enabled)) { + $colspan++; print '<td align="right"><input rel="'.$object->multicurrency_tx.'" type="text" class="flat" size="8" id="multicurrency_subprice" name="multicurrency_subprice" value="'.price($line->multicurrency_subprice).'" /></td>'; }