diff --git a/htdocs/commande/card.php b/htdocs/commande/card.php
index 224c443754a..c40a64032c0 100644
--- a/htdocs/commande/card.php
+++ b/htdocs/commande/card.php
@@ -278,6 +278,7 @@ if (empty($reshook)) {
if (!empty($origin) && !empty($originid)) {
// Parse element/subelement (ex: project_task)
$element = $subelement = $origin;
+ $regs = array();
if (preg_match('/^([^_]+)_([^_]+)/i', $origin, $regs)) {
$element = $regs [1];
$subelement = $regs [2];
@@ -1394,6 +1395,7 @@ if (empty($reshook)) {
/*
* View
*/
+
$title = $langs->trans('Order')." - ".$langs->trans('Card');
$help_url = 'EN:Customers_Orders|FR:Commandes_Clients|ES:Pedidos de clientes|DE:Modul_Kundenaufträge';
llxHeader('', $title, $help_url);
@@ -2347,17 +2349,17 @@ if ($action == 'create' && $usercancreate) {
if (!empty($conf->multicurrency->enabled) && ($object->multicurrency_code != $conf->currency)) {
// Multicurrency Amount HT
print '
| '.$form->editfieldkey('MulticurrencyAmountHT', 'multicurrency_total_ht', '', $object, 0).' | ';
- print ''.price($object->multicurrency_total_ht, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)).' | ';
+ print ''.price($object->multicurrency_total_ht, '', $langs, 0, -1, -1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)).' | ';
print '
';
// Multicurrency Amount VAT
print '| '.$form->editfieldkey('MulticurrencyAmountVAT', 'multicurrency_total_tva', '', $object, 0).' | ';
- print ''.price($object->multicurrency_total_tva, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)).' | ';
+ print ''.price($object->multicurrency_total_tva, '', $langs, 0, -1, -1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)).' | ';
print '
';
// Multicurrency Amount TTC
print '| '.$form->editfieldkey('MulticurrencyAmountTTC', 'multicurrency_total_ttc', '', $object, 0).' | ';
- print ''.price($object->multicurrency_total_ttc, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)).' | ';
+ print ''.price($object->multicurrency_total_ttc, '', $langs, 0, -1, -1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)).' | ';
print '
';
}
@@ -2367,23 +2369,23 @@ if ($action == 'create' && $usercancreate) {
$alert = ' '.img_warning($langs->trans('OrderMinAmount').': '.price($object->thirdparty->order_min_amount));
}
print '| '.$langs->trans('AmountHT').' | ';
- print ''.price($object->total_ht, 1, '', 1, - 1, - 1, $conf->currency).$alert.' | ';
+ print ''.price($object->total_ht, 1, '', 1, -1, -1, $conf->currency).$alert.' | ';
// Total VAT
- print '
| '.$langs->trans('AmountVAT').' | '.price($object->total_tva, 1, '', 1, - 1, - 1, $conf->currency).' |
';
+ print '| '.$langs->trans('AmountVAT').' | '.price($object->total_tva, 1, '', 1, -1, -1, $conf->currency).' |
';
// Amount Local Taxes
if ($mysoc->localtax1_assuj == "1" || $object->total_localtax1 != 0) { // Localtax1
print '| '.$langs->transcountry("AmountLT1", $mysoc->country_code).' | ';
- print ''.price($object->total_localtax1, 1, '', 1, - 1, - 1, $conf->currency).' |
';
+ print ''.price($object->total_localtax1, 1, '', 1, -1, -1, $conf->currency).' | ';
}
if ($mysoc->localtax2_assuj == "1" || $object->total_localtax2 != 0) { // Localtax2 IRPF
print '| '.$langs->transcountry("AmountLT2", $mysoc->country_code).' | ';
- print ''.price($object->total_localtax2, 1, '', 1, - 1, - 1, $conf->currency).' |
';
+ print ''.price($object->total_localtax2, 1, '', 1, -1, -1, $conf->currency).' | ';
}
// Total TTC
- print '| '.$langs->trans('AmountTTC').' | '.price($object->total_ttc, 1, '', 1, - 1, - 1, $conf->currency).' |
';
+ print '| '.$langs->trans('AmountTTC').' | '.price($object->total_ttc, 1, '', 1, -1, -1, $conf->currency).' |
';
// Statut
//print '| ' . $langs->trans('Status') . ' | ' . $object->getLibStatut(4) . ' |
';
diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php
index 1f0edd23401..d53b8829686 100644
--- a/htdocs/core/class/commonobject.class.php
+++ b/htdocs/core/class/commonobject.class.php
@@ -3526,7 +3526,7 @@ abstract class CommonObject
$this->db->free($resql);
- // Now update global field total_ht, total_ttc and tva
+ // Now update global field total_ht, total_ttc, total_tva, total_localtax1, total_localtax2, multicurrency_total_*
$fieldht = 'total_ht';
$fieldtva = 'tva';
$fieldlocaltax1 = 'localtax1';
@@ -3569,6 +3569,7 @@ abstract class CommonObject
dol_syslog(get_class($this)."::update_price", LOG_DEBUG);
$resql = $this->db->query($sql);
+
if (!$resql) {
$error++;
$this->error = $this->db->lasterror();
diff --git a/htdocs/expensereport/card.php b/htdocs/expensereport/card.php
index c01c720f481..138b98bd419 100644
--- a/htdocs/expensereport/card.php
+++ b/htdocs/expensereport/card.php
@@ -220,7 +220,7 @@ if (empty($reshook)) {
$object->fk_user_author = $user->id;
}
- // Check that expense report is for a user inside the hierarchy or advanced permission for all is set
+ // Check that expense report is for a user inside the hierarchy, or that advanced permission for all is set
if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->expensereport->creer))
|| (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->expensereport->creer) && empty($user->rights->expensereport->writeall_advance))) {
$error++;
@@ -1095,7 +1095,7 @@ if (empty($reshook)) {
$action = '';
}
- if ((int) $tmpvat < 0 || $tmpvat == '') {
+ if ((float) $tmpvat < 0 || $tmpvat === '') {
$error++;
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("VAT")), null, 'errors');
$action = '';
@@ -1190,7 +1190,6 @@ if (empty($reshook)) {
}
}
- $object->update_totaux_del($object_ligne->total_ht, $object_ligne->total_tva);
header("Location: ".$_SERVER["PHP_SELF"]."?id=".GETPOST('id', 'int'));
exit;
} else {
@@ -1240,7 +1239,7 @@ if (empty($reshook)) {
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type")), null, 'errors');
$action = '';
}
- if ((int) $tmpvat < 0 || $tmpvat == '') {
+ if ((float) $tmpvat < 0 || $tmpvat == '') {
$error++;
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Vat")), null, 'errors');
$action = '';
@@ -1277,8 +1276,6 @@ if (empty($reshook)) {
}
}
- $result = $object->recalculer($id);
-
//header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
//exit;
} else {
@@ -1441,8 +1438,6 @@ if ($action == 'create') {
} elseif ($id > 0 || $ref) {
$result = $object->fetch($id, $ref);
- $res = $object->fetch_optionals();
-
if ($result > 0) {
if (!in_array($object->fk_user_author, $user->getAllChildIds(1))) {
if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous)
@@ -1843,12 +1838,22 @@ if ($action == 'create') {
print '';
print '| '.$langs->trans("AmountVAT").' | ';
- print ''.price($object->total_tva, 1, '', 1, - 1, - 1, $conf->currency).' | ';
+ print ''.price($object->total_tva, 1, '', 1, -1, -1, $conf->currency).' | ';
print '
';
+ // Amount Local Taxes
+ if ($mysoc->localtax1_assuj == "1" || $object->total_localtax1 != 0) { // Localtax1
+ print '| '.$langs->transcountry("AmountLT1", $mysoc->country_code).' | ';
+ print ''.price($object->total_localtax1, 1, '', 1, -1, -1, $conf->currency).' |
';
+ }
+ if ($mysoc->localtax2_assuj == "1" || $object->total_localtax2 != 0) { // Localtax2 IRPF
+ print '| '.$langs->transcountry("AmountLT2", $mysoc->country_code).' | ';
+ print ''.price($object->total_localtax2, 1, '', 1, -1, -1, $conf->currency).' |
';
+ }
+
print '';
print '| '.$langs->trans("AmountTTC").' | ';
- print ''.price($object->total_ttc, 1, '', 1, - 1, - 1, $conf->currency).' | ';
+ print ''.price($object->total_ttc, 1, '', 1, -1, -1, $conf->currency).' | ';
print '
';
// List of payments already done
@@ -2053,7 +2058,7 @@ if ($action == 'create') {
// Comment
print ''.dol_nl2br($line->comments).' | ';
// VAT rate
- print ''.vatrate($line->vatrate, true).' | ';
+ print ''.vatrate($line->vatrate.($line->vat_src_code ? ' ('.$line->vat_src_code.')' : ''), true).' | ';
// Unit price HT
print '';
if (!empty($line->value_unit_ht)) {
@@ -2260,8 +2265,9 @@ if ($action == 'create') {
print ' | ';
// VAT
+ $selectedvat = price2num($line->vatrate).($line->vat_src_code ? ' ('.$line->vat_src_code.')' : '');
print '';
- print $form->load_tva('vatrate', (GETPOSTISSET("vatrate") ? GETPOST("vatrate") : $line->vatrate), $mysoc, '', 0, 0, '', false, 1);
+ print $form->load_tva('vatrate', (GETPOSTISSET("vatrate") ? GETPOST("vatrate") : $selectedvat), $mysoc, '', 0, 0, '', false, 1);
print ' | ';
// Unit price
diff --git a/htdocs/expensereport/class/expensereport.class.php b/htdocs/expensereport/class/expensereport.class.php
index d9e97baa81c..86852640596 100644
--- a/htdocs/expensereport/class/expensereport.class.php
+++ b/htdocs/expensereport/class/expensereport.class.php
@@ -80,8 +80,6 @@ class ExpenseReport extends CommonObject
*/
public $fk_statut;
- public $vat_src_code;
-
public $fk_c_paiement;
public $paid;
@@ -124,6 +122,9 @@ class ExpenseReport extends CommonObject
// Paiement
public $user_paid_infos;
+ public $localtax1; // for backward compatibility (real field should be total_localtax1 defined into CommonObject)
+ public $localtax2; // for backward compatibility (real field should be total_localtax2 defined into CommonObject)
+
/**
* Draft status
@@ -213,6 +214,10 @@ class ExpenseReport extends CommonObject
$this->total_ht = 0;
$this->total_ttc = 0;
$this->total_tva = 0;
+ $this->total_localtax1 = 0;
+ $this->total_localtax2 = 0;
+ $this->localtax1 = 0; // For backward compatibility
+ $this->localtax2 = 0; // For backward compatibility
$this->modepaymentid = 0;
// List of language codes for status
@@ -314,12 +319,18 @@ class ExpenseReport extends CommonObject
$newndfline->fk_project = $line->fk_project;
$newndfline->vatrate = $line->vatrate;
$newndfline->vat_src_code = $line->vat_src_code;
+ $newndfline->localtax1_tx = $line->localtax1_tx;
+ $newndfline->localtax2_tx = $line->localtax2_tx;
+ $newndfline->localtax1_type = $line->localtax1_type;
+ $newndfline->localtax2_type = $line->localtax2_type;
$newndfline->comments = $line->comments;
$newndfline->qty = $line->qty;
$newndfline->value_unit = $line->value_unit;
$newndfline->total_ht = $line->total_ht;
$newndfline->total_ttc = $line->total_ttc;
$newndfline->total_tva = $line->total_tva;
+ $newndfline->total_localtax1 = $line->total_localtax1;
+ $newndfline->total_localtax2 = $line->total_localtax2;
$newndfline->date = $line->date;
$newndfline->rule_warning_message = $line->rule_warning_message;
$newndfline->fk_c_exp_tax_cat = $line->fk_c_exp_tax_cat;
@@ -539,7 +550,8 @@ class ExpenseReport extends CommonObject
$sql = "SELECT d.rowid, d.entity, d.ref, d.note_public, d.note_private,"; // DEFAULT
$sql .= " d.detail_refuse, d.detail_cancel, d.fk_user_refuse, d.fk_user_cancel,"; // ACTIONS
$sql .= " d.date_refuse, d.date_cancel,"; // ACTIONS
- $sql .= " d.total_ht, d.total_ttc, d.total_tva,"; // TOTAUX (int)
+ $sql .= " d.total_ht, d.total_ttc, d.total_tva,";
+ $sql .= " d.localtax1 as total_localtax1, d.localtax2 as total_localtax2,";
$sql .= " d.date_debut, d.date_fin, d.date_create, d.tms as date_modif, d.date_valid, d.date_approve,"; // DATES (datetime)
$sql .= " d.fk_user_creat, d.fk_user_author, d.fk_user_modif, d.fk_user_validator,";
$sql .= " d.fk_user_valid, d.fk_user_approve,";
@@ -565,6 +577,11 @@ class ExpenseReport extends CommonObject
$this->total_ht = $obj->total_ht;
$this->total_tva = $obj->total_tva;
$this->total_ttc = $obj->total_ttc;
+ $this->localtax1 = $obj->total_localtax1; // For backward compatibility
+ $this->localtax2 = $obj->total_localtax2; // For backward compatibility
+ $this->total_localtax1 = $obj->total_localtax1;
+ $this->total_localtax2 = $obj->total_localtax2;
+
$this->note_public = $obj->note_public;
$this->note_private = $obj->note_private;
$this->detail_refuse = $obj->detail_refuse;
@@ -962,54 +979,6 @@ class ExpenseReport extends CommonObject
}
}
- /**
- * recalculer
- * TODO Replace this with call to update_price if not already done
- *
- * @param int $id Id of expense report
- * @return int <0 if KO, >0 if OK
- */
- public function recalculer($id)
- {
- $sql = 'SELECT tt.total_ht, tt.total_ttc, tt.total_tva';
- $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element_line.' as tt';
- $sql .= ' WHERE tt.'.$this->fk_element.' = '.((int) $id);
-
- $total_ht = 0; $total_tva = 0; $total_ttc = 0;
-
- $result = $this->db->query($sql);
- if ($result) {
- $num = $this->db->num_rows($result);
- $i = 0;
- while ($i < $num) {
- $objp = $this->db->fetch_object($result);
- $total_ht += $objp->total_ht;
- $total_tva += $objp->total_tva;
- $i++;
- }
-
- $total_ttc = $total_ht + $total_tva;
- $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
- $sql .= " total_ht = ".price2num($total_ht, 'MT');
- $sql .= " , total_ttc = ".price2num($total_ttc, 'MT');
- $sql .= " , total_tva = ".price2num($total_tva, 'MT');
- $sql .= " WHERE rowid = ".((int) $id);
- $result = $this->db->query($sql);
- if ($result) {
- $this->db->free($result);
- return 1;
- } else {
- $this->error = $this->db->lasterror();
- dol_syslog(get_class($this)."::recalculer: Error ".$this->error, LOG_ERR);
- return -3;
- }
- } else {
- $this->error = $this->db->lasterror();
- dol_syslog(get_class($this)."::recalculer: Error ".$this->error, LOG_ERR);
- return -3;
- }
- }
-
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* fetch_lines
@@ -1024,14 +993,18 @@ class ExpenseReport extends CommonObject
$this->lines = array();
$sql = ' SELECT de.rowid, de.comments, de.qty, de.value_unit, de.date, de.rang,';
- $sql .= ' de.'.$this->fk_element.', de.fk_c_type_fees, de.fk_c_exp_tax_cat, de.fk_projet as fk_project, de.tva_tx, de.fk_ecm_files,';
+ $sql .= ' de.'.$this->fk_element.', de.fk_c_type_fees, de.fk_c_exp_tax_cat, de.fk_projet as fk_project,';
+ $sql .= ' de.tva_tx, de.vat_src_code,';
+ $sql .= ' de.localtax1_tx, de.localtax2_tx, de.localtax1_type, de.localtax2_type,';
+ $sql .= ' de.fk_ecm_files,';
$sql .= ' de.total_ht, de.total_tva, de.total_ttc,';
+ $sql .= ' de.total_localtax1, de.total_localtax2,';
$sql .= ' ctf.code as code_type_fees, ctf.label as libelle_type_fees,';
$sql .= ' p.ref as ref_projet, p.title as title_projet';
$sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element_line.' as de';
$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_type_fees as ctf ON de.fk_c_type_fees = ctf.id';
$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as p ON de.fk_projet = p.rowid';
- $sql .= ' WHERE de.'.$this->fk_element.' = '.$this->id;
+ $sql .= ' WHERE de.'.$this->fk_element.' = '.((int) $this->id);
if (!empty($conf->global->EXPENSEREPORT_LINES_SORTED_BY_ROWID)) {
$sql .= ' ORDER BY de.rang ASC, de.rowid ASC';
} else {
@@ -1065,11 +1038,20 @@ class ExpenseReport extends CommonObject
$deplig->total_ht = $objp->total_ht;
$deplig->total_tva = $objp->total_tva;
$deplig->total_ttc = $objp->total_ttc;
+ $deplig->total_localtax1 = $objp->total_localtax1;
+ $deplig->total_localtax2 = $objp->total_totaltax2;
$deplig->type_fees_code = empty($objp->code_type_fees) ? 'TF_OTHER' : $objp->code_type_fees;
$deplig->type_fees_libelle = $objp->libelle_type_fees;
- $deplig->tva_tx = $objp->tva_tx;
+
+ $deplig->tva_tx = $objp->tva_tx;
$deplig->vatrate = $objp->tva_tx;
+ $deplig->vat_src_code = $objp->vat_src_code;
+ $deplig->localtax1_tx = $objp->localtax1_tx;
+ $deplig->localtax2_tx = $objp->localtax2_tx;
+ $deplig->localtax1_type = $objp->localtax1_type;
+ $deplig->localtax2_type = $objp->localtax2_type;
+
$deplig->projet_ref = $objp->ref_projet;
$deplig->projet_title = $objp->title_projet;
@@ -1757,41 +1739,12 @@ class ExpenseReport extends CommonObject
}
}
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
- /**
- * Update total of an expense report when you delete a line.
- *
- * @param string $ligne_total_ht Amount without taxes
- * @param string $ligne_total_tva Amount of all taxes
- * @return void
- */
- public function update_totaux_del($ligne_total_ht, $ligne_total_tva)
- {
- // phpcs:enable
- $this->total_ht = $this->total_ht - $ligne_total_ht;
- $this->total_tva = $this->total_tva - $ligne_total_tva;
- $this->total_ttc = $this->total_ht + $this->total_tva;
-
- $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
- $sql .= " total_ht = ".$this->total_ht;
- $sql .= " , total_ttc = ".$this->total_ttc;
- $sql .= " , total_tva = ".$this->total_tva;
- $sql .= " WHERE rowid = ".$this->id;
-
- $result = $this->db->query($sql);
- if ($result) {
- return 1;
- } else {
- $this->error = $this->db->error();
- return -1;
- }
- }
/**
* addline
*
* @param float $qty Qty
- * @param double $up Value init
+ * @param double $up Unit price (price with tax)
* @param int $fk_c_type_fees Type payment
* @param string $vatrate Vat rate (Can be '10' or '10 (ABC)')
* @param string $date Date
@@ -1838,7 +1791,14 @@ class ExpenseReport extends CommonObject
$this->line = new ExpenseReportLine($this->db);
- $localtaxes_type = getLocalTaxesFromRate($vatrate, 0, $mysoc, $this->thirdparty);
+ // We don't know seller and buyer for expense reports
+ $seller = $mysoc; // We use same than current company (expense report are often done in same country)
+ $seller->tva_assuj = 1; // Most seller uses vat
+ $seller->localtax1_assuj = $mysoc->localtax1_assuj; // We don't know, we reuse the state of company
+ $seller->localtax2_assuj = $mysoc->localtax1_assuj; // We don't know, we reuse the state of company
+ $buyer = new Societe($this->db);
+
+ $localtaxes_type = getLocalTaxesFromRate($vatrate, 0, $buyer, $seller);
$vat_src_code = '';
$reg = array();
@@ -1848,16 +1808,22 @@ class ExpenseReport extends CommonObject
}
$vatrate = preg_replace('/\*/', '', $vatrate);
- $seller = ''; // seller is unknown
-
- $tmp = calcul_price_total($qty, $up, 0, $vatrate, 0, 0, 0, 'TTC', 0, $type, $seller, $localtaxes_type);
+ $tmp = calcul_price_total($qty, $up, 0, $vatrate, -1, -1, 0, 'TTC', 0, $type, $seller, $localtaxes_type);
$this->line->value_unit = $up;
+
$this->line->vat_src_code = $vat_src_code;
$this->line->vatrate = price2num($vatrate);
+ $this->line->localtax1_tx = $localtaxes_type[1];
+ $this->line->localtax2_tx = $localtaxes_type[3];
+ $this->line->localtax1_type = $localtaxes_type[0];
+ $this->line->localtax2_type = $localtaxes_type[2];
+
$this->line->total_ttc = $tmp[2];
$this->line->total_ht = $tmp[0];
$this->line->total_tva = $tmp[1];
+ $this->line->total_localtax1 = $tmp[9];
+ $this->line->total_localtax2 = $tmp[10];
$this->line->fk_expensereport = $this->id;
$this->line->qty = $qty;
@@ -1899,13 +1865,13 @@ class ExpenseReport extends CommonObject
/**
* Check constraint of rules and update price if needed
*
- * @param int $type type of line
- * @param string $seller seller, but actually he is unknown
- * @return true or false
+ * @param int $type Type of line
+ * @param string $seller Seller, but actually he is unknown
+ * @return true or false
*/
public function checkRules($type = 0, $seller = '')
{
- global $user, $conf, $db, $langs;
+ global $user, $conf, $db, $langs, $mysoc;
$langs->load('trips');
@@ -1913,6 +1879,15 @@ class ExpenseReport extends CommonObject
return true; // if don't use rules
}
+ // We don't know seller and buyer for expense reports
+ if (!is_object($seller)) {
+ $seller = $mysoc; // We use same than current company (expense report are often done in same country)
+ $seller->tva_assuj = 1; // Most seller uses vat
+ $seller->localtax1_assuj = $mysoc->localtax1_assuj; // We don't know, we reuse the state of company
+ $seller->localtax2_assuj = $mysoc->localtax1_assuj; // We don't know, we reuse the state of company
+ }
+ //$buyer = new Societe($this->db);
+
$rulestocheck = ExpenseReportRule::getAllRule($this->line->fk_c_type_fees, $this->line->date, $this->fk_user_author);
$violation = 0;
@@ -1960,6 +1935,8 @@ class ExpenseReport extends CommonObject
$this->line->total_ttc = $tmp[2];
$this->line->total_ht = $tmp[0];
$this->line->total_tva = $tmp[1];
+ $this->line->total_localtax1 = $tmp[9];
+ $this->line->total_localtax2 = $tmp[10];
return false;
} else {
@@ -1970,11 +1947,13 @@ class ExpenseReport extends CommonObject
/**
* Method to apply the offset if needed
*
- * @return boolean true=applied, false=not applied
+ * @param int $type Type of line
+ * @param string $seller Seller, but actually he is unknown
+ * @return boolean True=applied, False=not applied
*/
- public function applyOffset()
+ public function applyOffset($type = 0, $seller = '')
{
- global $conf;
+ global $conf, $mysoc;
if (empty($conf->global->MAIN_USE_EXPENSE_IK)) {
return false;
@@ -1987,6 +1966,15 @@ class ExpenseReport extends CommonObject
return false;
}
+ // We don't know seller and buyer for expense reports
+ if (!is_object($seller)) {
+ $seller = $mysoc; // We use same than current company (expense report are often done in same country)
+ $seller->tva_assuj = 1; // Most seller uses vat
+ $seller->localtax1_assuj = $mysoc->localtax1_assuj; // We don't know, we reuse the state of company
+ $seller->localtax2_assuj = $mysoc->localtax1_assuj; // We don't know, we reuse the state of company
+ }
+ //$buyer = new Societe($this->db);
+
$range = ExpenseReportIk::getRangeByUser($userauthor, $this->line->fk_c_exp_tax_cat);
if (empty($range)) {
@@ -2010,6 +1998,8 @@ class ExpenseReport extends CommonObject
$this->line->total_ttc = $tmp[2];
$this->line->total_ht = $tmp[0];
$this->line->total_tva = $tmp[1];
+ $this->line->total_localtax1 = $tmp[9];
+ $this->line->total_localtax2 = $tmp[10];
return true;
}
@@ -2033,7 +2023,7 @@ class ExpenseReport extends CommonObject
$sql .= ' AND d.rowid <> '.$this->line->id;
}
- dol_syslog(get_class($this)."::offsetAlreadyGiven sql=".$sql);
+ dol_syslog(get_class($this)."::offsetAlreadyGiven");
$resql = $this->db->query($sql);
if ($resql) {
$num = $this->db->num_rows($resql);
@@ -2056,7 +2046,7 @@ class ExpenseReport extends CommonObject
* @param double $vatrate Vat rate. Can be '8.5' or '8.5* (8.5NPROM...)'
* @param string $comments Description
* @param float $qty Qty
- * @param double $value_unit Value init
+ * @param double $value_unit Unit price (with taxes)
* @param int $date Date
* @param int $expensereport_id Expense report id
* @param int $fk_c_exp_tax_cat Id of category of car
@@ -2073,7 +2063,10 @@ class ExpenseReport extends CommonObject
$type = 0; // TODO What if type is service ?
// We don't know seller and buyer for expense reports
- $seller = $mysoc;
+ $seller = $mysoc; // We use same than current company (expense report are often done in same country)
+ $seller->tva_assuj = 1; // Most seller uses vat
+ $seller->localtax1_assuj = $mysoc->localtax1_assuj; // We don't know, we reuse the state of company
+ $seller->localtax2_assuj = $mysoc->localtax1_assuj; // We don't know, we reuse the state of company
$buyer = new Societe($this->db);
$localtaxes_type = getLocalTaxesFromRate($vatrate, 0, $buyer, $seller);
@@ -2087,8 +2080,8 @@ class ExpenseReport extends CommonObject
}
$vatrate = preg_replace('/\*/', '', $vatrate);
- $tmp = calcul_price_total($qty, $value_unit, 0, $vatrate, 0, 0, 0, 'TTC', 0, $type, $seller, $localtaxes_type);
-
+ $tmp = calcul_price_total($qty, $value_unit, 0, $vatrate, -1, -1, 0, 'TTC', 0, $type, $seller, $localtaxes_type);
+ //var_dump($vatrate);var_dump($localtaxes_type);var_dump($tmp);exit;
// calcul total of line
//$total_ttc = price2num($qty*$value_unit, 'MT');
@@ -2109,14 +2102,17 @@ class ExpenseReport extends CommonObject
$this->line->vat_src_code = $vat_src_code;
$this->line->vatrate = price2num($vatrate);
- $this->line->total_ttc = $tmp[2];
- $this->line->total_ht = $tmp[0];
- $this->line->total_tva = $tmp[1];
$this->line->localtax1_tx = $localtaxes_type[1];
$this->line->localtax2_tx = $localtaxes_type[3];
$this->line->localtax1_type = $localtaxes_type[0];
$this->line->localtax2_type = $localtaxes_type[2];
+ $this->line->total_ttc = $tmp[2];
+ $this->line->total_ht = $tmp[0];
+ $this->line->total_tva = $tmp[1];
+ $this->line->total_localtax1 = $tmp[9];
+ $this->line->total_localtax2 = $tmp[10];
+
$this->line->fk_ecm_files = $fk_ecm_files;
$this->line->id = ((int) $rowid);
@@ -2184,6 +2180,8 @@ class ExpenseReport extends CommonObject
return -1;
}
+ $this->update_price();
+
$this->db->commit();
return 1;
@@ -2593,9 +2591,17 @@ class ExpenseReportLine
public $projet_title;
public $vatrate;
+ public $vat_src_code;
+ public $localtax1_tx;
+ public $localtax2_tx;
+ public $localtax1_type;
+ public $localtax2_type;
+
public $total_ht;
public $total_tva;
public $total_ttc;
+ public $total_localtax1;
+ public $total_localtax2;
/**
* @var int ID into llx_ecm_files table to link line to attached file
@@ -2623,6 +2629,7 @@ class ExpenseReportLine
{
$sql = 'SELECT fde.rowid, fde.fk_expensereport, fde.fk_c_type_fees, fde.fk_c_exp_tax_cat, fde.fk_projet as fk_project, fde.date,';
$sql .= ' fde.tva_tx as vatrate, fde.vat_src_code, fde.comments, fde.qty, fde.value_unit, fde.total_ht, fde.total_tva, fde.total_ttc, fde.fk_ecm_files,';
+ $sql .= ' fde.localtax1_tx, fde.localtax2_tx, fde.localtax1_type, fde.localtax2_type, fde.total_localtax1, fde.total_localtax2,';
$sql .= ' ctf.code as type_fees_code, ctf.label as type_fees_libelle,';
$sql .= ' pjt.rowid as projet_id, pjt.title as projet_title, pjt.ref as projet_ref';
$sql .= ' FROM '.MAIN_DB_PREFIX.'expensereport_det as fde';
@@ -2652,11 +2659,20 @@ class ExpenseReportLine
$this->type_fees_libelle = $objp->type_fees_libelle;
$this->projet_ref = $objp->projet_ref;
$this->projet_title = $objp->projet_title;
+
$this->vatrate = $objp->vatrate;
$this->vat_src_code = $objp->vat_src_code;
+ $this->localtax1_tx = $objp->localtax1_tx;
+ $this->localtax2_tx = $objp->localtax2_tx;
+ $this->localtax1_type = $objp->localtax1_type;
+ $this->localtax2_type = $objp->localtax2_type;
+
$this->total_ht = $objp->total_ht;
$this->total_tva = $objp->total_tva;
$this->total_ttc = $objp->total_ttc;
+ $this->total_localtax1 = $objp->total_localtax1;
+ $this->total_localtax2 = $objp->total_localtax2;
+
$this->fk_ecm_files = $objp->fk_ecm_files;
$this->db->free($result);
@@ -2695,18 +2711,29 @@ class ExpenseReportLine
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'expensereport_det';
$sql .= ' (fk_expensereport, fk_c_type_fees, fk_projet,';
- $sql .= ' tva_tx, vat_src_code, comments, qty, value_unit, total_ht, total_tva, total_ttc, date, rule_warning_message, fk_c_exp_tax_cat, fk_ecm_files)';
+ $sql .= ' tva_tx, vat_src_code,';
+ $sql .= ' localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
+ $sql .= ' comments, qty, value_unit,';
+ $sql .= ' total_ht, total_tva, total_ttc,';
+ $sql .= ' total_localtax1, total_localtax2,';
+ $sql .= ' date, rule_warning_message, fk_c_exp_tax_cat, fk_ecm_files)';
$sql .= " VALUES (".$this->db->escape($this->fk_expensereport).",";
$sql .= " ".((int) $this->fk_c_type_fees).",";
$sql .= " ".((int) (!empty($this->fk_project) && $this->fk_project > 0) ? $this->fk_project : ((!empty($this->fk_projet) && $this->fk_projet > 0) ? $this->fk_projet : 'null')).",";
$sql .= " ".((float) $this->vatrate).",";
$sql .= " '".$this->db->escape(empty($this->vat_src_code) ? '' : $this->vat_src_code)."',";
+ $sql .= " ".((float) price2num($this->localtax1_tx)).",";
+ $sql .= " ".((float) price2num($this->localtax2_tx)).",";
+ $sql .= " '".$this->db->escape($this->localtax1_type)."',";
+ $sql .= " '".$this->db->escape($this->localtax2_type)."',";
$sql .= " '".$this->db->escape($this->comments)."',";
$sql .= " ".((float) $this->qty).",";
$sql .= " ".((float) $this->value_unit).",";
$sql .= " ".((float) price2num($this->total_ht)).",";
$sql .= " ".((float) price2num($this->total_tva)).",";
$sql .= " ".((float) price2num($this->total_ttc)).",";
+ $sql .= " ".((float) price2num($this->total_localtax1)).",";
+ $sql .= " ".((float) price2num($this->total_localtax2)).",";
$sql .= " '".$this->db->idate($this->date)."',";
$sql .= " ".(empty($this->rule_warning_message) ? 'null' : "'".$this->db->escape($this->rule_warning_message)."'").",";
$sql .= " ".((int) $this->fk_c_exp_tax_cat).",";
@@ -2814,11 +2841,17 @@ class ExpenseReportLine
$sql .= ", value_unit = ".((float) $this->value_unit);
$sql .= ", qty=".((float) $this->qty);
$sql .= ", date='".$this->db->idate($this->date)."'";
- $sql .= ", total_ht=".((float) price2num($this->total_ht, 'MT'))."";
- $sql .= ", total_tva=".((float) price2num($this->total_tva, 'MT'))."";
- $sql .= ", total_ttc=".((float) price2num($this->total_ttc, 'MT'))."";
+ $sql .= ", total_ht=".((float) price2num($this->total_ht, 'MT'));
+ $sql .= ", total_tva=".((float) price2num($this->total_tva, 'MT'));
+ $sql .= ", total_ttc=".((float) price2num($this->total_ttc, 'MT'));
+ $sql .= ", total_localtax1=".((float) price2num($this->total_localtax1, 'MT'));
+ $sql .= ", total_localtax2=".((float) price2num($this->total_localtax2, 'MT'));
$sql .= ", tva_tx=".((float) $this->vatrate);
$sql .= ", vat_src_code='".$this->db->escape($this->vat_src_code)."'";
+ $sql .= ", localtax1_tx=".((float) $this->localtax1_tx);
+ $sql .= ", localtax2_tx=".((float) $this->localtax2_tx);
+ $sql .= ", localtax1_type='".$this->db->escape($this->localtax1_type)."'";
+ $sql .= ", localtax2_type='".$this->db->escape($this->localtax2_type)."'";
$sql .= ", rule_warning_message='".$this->db->escape($this->rule_warning_message)."'";
$sql .= ", fk_c_exp_tax_cat=".$this->db->escape($this->fk_c_exp_tax_cat);
$sql .= ", fk_ecm_files=".($this->fk_ecm_files > 0 ? ((int) $this->fk_ecm_files) : 'null');