diff --git a/htdocs/comm/propal/card.php b/htdocs/comm/propal/card.php index fbdb640bf5d..3ffddeb50cb 100644 --- a/htdocs/comm/propal/card.php +++ b/htdocs/comm/propal/card.php @@ -1169,20 +1169,26 @@ if (empty($reshook)) { if ($result) { // If there is some prices specific to the customer if (count($prodcustprice->lines) > 0) { - $pricebycustomerexist = true; - $pu_ht = price($prodcustprice->lines[0]->price); - $pu_ttc = price($prodcustprice->lines[0]->price_ttc); - $price_min = price($prodcustprice->lines[0]->price_min); - $price_min_ttc = price($prodcustprice->lines[0]->price_min_ttc); - $price_base_type = $prodcustprice->lines[0]->price_base_type; - /*$tva_tx = ($prodcustprice->lines[0]->default_vat_code ? $prodcustprice->lines[0]->tva_tx.' ('.$prodcustprice->lines[0]->default_vat_code.' )' : $prodcustprice->lines[0]->tva_tx); - if ($prodcustprice->lines[0]->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) { - $tva_tx .= ' ('.$prodcustprice->lines[0]->default_vat_code.')'; + $date_now = (int) floor(dol_now() / 86400) * 86400; // date without hours + foreach ($prodcustprice->lines as $k => $custprice_line) { + if ($custprice_line->date_begin <= $date_now && (empty($custprice_line->date_end) || $date_now <= $custprice_line->date_end)) { + $pricebycustomerexist = true; + $pu_ht = price($custprice_line->price); + $pu_ttc = price($custprice_line->price_ttc); + $price_min = price($custprice_line->price_min); + $price_min_ttc = price($custprice_line->price_min_ttc); + $price_base_type = $custprice_line->price_base_type; + /*$tva_tx = ($custprice_line->default_vat_code ? $custprice_line->tva_tx.' ('.$custprice_line->default_vat_code.' )' : $custprice_line->tva_tx); + if ($custprice_line->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) { + $tva_tx .= ' ('.$custprice_line->default_vat_code.')'; + } + $tva_npr = $custprice_line->recuperableonly; + if (empty($tva_tx)) { + $tva_npr = 0; + }*/ + break; + } } - $tva_npr = $prodcustprice->lines[0]->recuperableonly; - if (empty($tva_tx)) { - $tva_npr = 0; - }*/ } } @@ -1227,19 +1233,25 @@ if (empty($reshook)) { if ($result) { // If there is some prices specific to the customer if (count($prodcustprice->lines) > 0) { - $pu_ht = price($prodcustprice->lines[0]->price); - $pu_ttc = price($prodcustprice->lines[0]->price_ttc); - $price_min = price($prodcustprice->lines[0]->price_min); - $price_min_ttc = price($prodcustprice->lines[0]->price_min_ttc); - $price_base_type = $prodcustprice->lines[0]->price_base_type; - /*$tva_tx = ($prodcustprice->lines[0]->default_vat_code ? $prodcustprice->lines[0]->tva_tx.' ('.$prodcustprice->lines[0]->default_vat_code.' )' : $prodcustprice->lines[0]->tva_tx); - if ($prodcustprice->lines[0]->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) { - $tva_tx .= ' ('.$prodcustprice->lines[0]->default_vat_code.')'; + $date_now = (int) floor(dol_now() / 86400) * 86400; // date without hours + foreach ($prodcustprice->lines as $k => $custprice_line) { + if ($custprice_line->date_begin <= $date_now && (empty($custprice_line->date_end) || $date_now <= $custprice_line->date_end)) { + $pu_ht = price($custprice_line->price); + $pu_ttc = price($custprice_line->price_ttc); + $price_min = price($custprice_line->price_min); + $price_min_ttc = price($custprice_line->price_min_ttc); + $price_base_type = $custprice_line->price_base_type; + /*$tva_tx = ($custprice_line->default_vat_code ? $custprice_line->tva_tx.' ('.$custprice_line->default_vat_code.' )' : $custprice_line->tva_tx); + if ($custprice_line->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) { + $tva_tx .= ' ('.$custprice_line->default_vat_code.')'; + } + $tva_npr = $custprice_line->recuperableonly; + if (empty($tva_tx)) { + $tva_npr = 0; + }*/ + break; + } } - $tva_npr = $prodcustprice->lines[0]->recuperableonly; - if (empty($tva_tx)) { - $tva_npr = 0; - }*/ } } } elseif (getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY')) { diff --git a/htdocs/comm/propal/class/propal.class.php b/htdocs/comm/propal/class/propal.class.php index 2f8723a7c8b..626d9e315f3 100644 --- a/htdocs/comm/propal/class/propal.class.php +++ b/htdocs/comm/propal/class/propal.class.php @@ -1542,10 +1542,17 @@ class Propal extends CommonObject if ($result) { // If there is some prices specific to the customer if (count($prodcustprice->lines) > 0) { - $pu_ht = price($prodcustprice->lines[0]->price); - $tva_tx = ($prodcustprice->lines[0]->default_vat_code ? $prodcustprice->lines[0]->tva_tx.' ('.$prodcustprice->lines[0]->default_vat_code.' )' : $prodcustprice->lines[0]->tva_tx); - if ($prodcustprice->lines[0]->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) { - $tva_tx .= ' ('.$prodcustprice->lines[0]->default_vat_code.')'; + $date_now = (int) floor(dol_now() / 86400) * 86400; // date without hours + foreach ($prodcustprice->lines as $k => $custprice_line) { + if ($custprice_line->date_begin <= $date_now && (empty($custprice_line->date_end) || $date_now <= $custprice_line->date_end)) { + $pu_ht = price($custprice_line->price); + $tva_tx = ($custprice_line->default_vat_code ? $custprice_line->tva_tx . ' (' . $custprice_line->default_vat_code . ' )' : $custprice_line->tva_tx); + if ($custprice_line->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) { + $tva_tx .= ' (' . $custprice_line->default_vat_code . ')'; + } + $remise_percent = $custprice_line->discount_percent; + break; + } } } } diff --git a/htdocs/commande/card.php b/htdocs/commande/card.php index f10e6e3aa6c..913dc87d333 100644 --- a/htdocs/commande/card.php +++ b/htdocs/commande/card.php @@ -877,19 +877,25 @@ if (empty($reshook)) { $result = $prodcustprice->fetchAll('', '', 0, 0, $filter); if ($result >= 0) { if (count($prodcustprice->lines) > 0) { - $pricebycustomerexist = true; - $pu_ht = price($prodcustprice->lines[0]->price); - $pu_ttc = price($prodcustprice->lines[0]->price_ttc); - $price_min = price($prodcustprice->lines[0]->price_min); - $price_min_ttc = price($prodcustprice->lines[0]->price_min_ttc); - $price_base_type = $prodcustprice->lines[0]->price_base_type; - $tva_tx = $prodcustprice->lines[0]->tva_tx; - if ($prodcustprice->lines[0]->default_vat_code && !preg_match('/\(.*\)/', (string) $tva_tx)) { - $tva_tx .= ' ('.$prodcustprice->lines[0]->default_vat_code.')'; - } - $tva_npr = $prodcustprice->lines[0]->recuperableonly; - if (empty($tva_tx)) { - $tva_npr = 0; + $date_now = (int) floor(dol_now() / 86400) * 86400; // date without hours + foreach ($prodcustprice->lines as $k => $custprice_line) { + if ($custprice_line->date_begin <= $date_now && (empty($custprice_line->date_end) || $date_now <= $custprice_line->date_end)) { + $pricebycustomerexist = true; + $pu_ht = price($custprice_line->price); + $pu_ttc = price($custprice_line->price_ttc); + $price_min = price($custprice_line->price_min); + $price_min_ttc = price($custprice_line->price_min_ttc); + $price_base_type = $custprice_line->price_base_type; + $tva_tx = $custprice_line->tva_tx; + if ($custprice_line->default_vat_code && !preg_match('/\(.*\)/', (string) $tva_tx)) { + $tva_tx .= ' (' . $custprice_line->default_vat_code . ')'; + } + $tva_npr = $custprice_line->recuperableonly; + if (empty($tva_tx)) { + $tva_npr = 0; + } + break; + } } } } else { @@ -938,18 +944,24 @@ if (empty($reshook)) { $result = $prodcustprice->fetchAll('', '', 0, 0, $filter); if ($result >= 0) { if (count($prodcustprice->lines) > 0) { - $pu_ht = price($prodcustprice->lines[0]->price); - $pu_ttc = price($prodcustprice->lines[0]->price_ttc); - $price_min = price($prodcustprice->lines[0]->price_min); - $price_min_ttc = price($prodcustprice->lines[0]->price_min_ttc); - $price_base_type = $prodcustprice->lines[0]->price_base_type; - $tva_tx = $prodcustprice->lines[0]->tva_tx; - if ($prodcustprice->lines[0]->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) { - $tva_tx .= ' ('.$prodcustprice->lines[0]->default_vat_code.')'; - } - $tva_npr = $prodcustprice->lines[0]->recuperableonly; - if (empty($tva_tx)) { - $tva_npr = 0; + $date_now = (int) floor(dol_now() / 86400) * 86400; // date without hours + foreach ($prodcustprice->lines as $k => $custprice_line) { + if ($custprice_line->date_begin <= $date_now && (empty($custprice_line->date_end) || $date_now <= $custprice_line->date_end)) { + $pu_ht = price($custprice_line->price); + $pu_ttc = price($custprice_line->price_ttc); + $price_min = price($custprice_line->price_min); + $price_min_ttc = price($custprice_line->price_min_ttc); + $price_base_type = $custprice_line->price_base_type; + $tva_tx = $custprice_line->tva_tx; + if ($custprice_line->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) { + $tva_tx .= ' (' . $custprice_line->default_vat_code . ')'; + } + $tva_npr = $custprice_line->recuperableonly; + if (empty($tva_tx)) { + $tva_npr = 0; + } + break; + } } } } else { diff --git a/htdocs/contrat/card.php b/htdocs/contrat/card.php index 7102d907a6d..e2f11457698 100644 --- a/htdocs/contrat/card.php +++ b/htdocs/contrat/card.php @@ -572,16 +572,22 @@ if (empty($reshook)) { $result = $prodcustprice->fetchAll('', '', 0, 0, $filter); if ($result) { if (count($prodcustprice->lines) > 0) { - $price_min = price($prodcustprice->lines[0]->price_min); - $price_min_ttc = price($prodcustprice->lines[0]->price_min_ttc); - /*$tva_tx = $prodcustprice->lines[0]->tva_tx; - if ($prodcustprice->lines[0]->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) { - $tva_tx .= ' ('.$prodcustprice->lines[0]->default_vat_code.')'; + $date_now = (int) floor(dol_now() / 86400) * 86400; // date without hours + foreach ($prodcustprice->lines as $k => $custprice_line) { + if ($custprice_line->date_begin <= $date_now && (empty($custprice_line->date_end) || $date_now <= $custprice_line->date_end)) { + $price_min = price($custprice_line->price_min); + $price_min_ttc = price($custprice_line->price_min_ttc); + /*$tva_tx = $custprice_line->tva_tx; + if ($custprice_line->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) { + $tva_tx .= ' ('.$custprice_line->default_vat_code.')'; + } + $tva_npr = $custprice_line->recuperableonly; + if (empty($tva_tx)) { + $tva_npr = 0; + }*/ + break; + } } - $tva_npr = $prodcustprice->lines[0]->recuperableonly; - if (empty($tva_tx)) { - $tva_npr = 0; - }*/ } } } diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 77c8eb3e137..8cd33589397 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -2995,8 +2995,8 @@ class Form //Price by customer if ((getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) && !empty($socid)) { $sql .= ', pcp.rowid as idprodcustprice, pcp.price as custprice, pcp.price_ttc as custprice_ttc,'; - $sql .= ' pcp.price_base_type as custprice_base_type, pcp.tva_tx as custtva_tx, pcp.default_vat_code as custdefault_vat_code, pcp.ref_customer as custref'; - $selectFields .= ", idprodcustprice, custprice, custprice_ttc, custprice_base_type, custtva_tx, custdefault_vat_code, custref"; + $sql .= ' pcp.price_base_type as custprice_base_type, pcp.tva_tx as custtva_tx, pcp.default_vat_code as custdefault_vat_code, pcp.ref_customer as custref, pcp.discount_percent as custdiscount_percent'; + $selectFields .= ", idprodcustprice, custprice, custprice_ttc, custprice_base_type, custtva_tx, custdefault_vat_code, custref, custdiscount_percent"; } // Units if (getDolGlobalInt('PRODUCT_USE_UNITS')) { @@ -3047,7 +3047,20 @@ class Form //Price by customer if ((getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) && !empty($socid)) { - $sql .= " LEFT JOIN " . $this->db->prefix() . "product_customer_price as pcp ON pcp.fk_soc=" . ((int) $socid) . " AND pcp.fk_product=p.rowid"; + $now = dol_now(); + $sql .= " LEFT JOIN ("; + $sql .= " SELECT pcp1.*"; + $sql .= " FROM " . $this->db->prefix() . "product_customer_price AS pcp1"; + $sql .= " LEFT JOIN ("; + $sql .= " SELECT fk_soc, fk_product, MIN(date_begin) AS date_begin"; + $sql .= " FROM " . $this->db->prefix() . "product_customer_price"; + $sql .= " WHERE fk_soc = " . ((int) $socid); + $sql .= " AND date_begin <= '" . $this->db->idate($now) . "'"; + $sql .= " AND (date_end IS NULL OR '" . $this->db->idate($now) . "' <= date_end)"; + $sql .= " GROUP BY fk_soc, fk_product"; + $sql .= " ) AS pcp2 ON pcp1.fk_soc = pcp2.fk_soc AND pcp1.fk_product = pcp2.fk_product AND pcp1.date_begin = pcp2.date_begin"; + $sql .= " WHERE pcp2.fk_soc IS NOT NULL"; + $sql .= " ) AS pcp ON pcp.fk_soc = " . ((int) $socid) . " AND pcp.fk_product = p.rowid"; } // Units if (getDolGlobalInt('PRODUCT_USE_UNITS')) { @@ -3588,6 +3601,7 @@ class Form $outpricebasetype = $objp->custprice_base_type; $outtva_tx = $objp->custtva_tx; $outdefault_vat_code = $objp->custdefault_vat_code; + $outdiscount = $objp->custdiscount_percent; } } diff --git a/htdocs/core/lib/pdf.lib.php b/htdocs/core/lib/pdf.lib.php index 4b11ccc758d..8a2fdc2f978 100644 --- a/htdocs/core/lib/pdf.lib.php +++ b/htdocs/core/lib/pdf.lib.php @@ -1747,9 +1747,18 @@ function pdf_getlinedesc($object, $i, $outputlangs, $hideref = 0, $hidedesc = 0, $nbCustomerPrices = $productCustomerPriceStatic->fetchAll('', '', 1, 0, $filter); if ($nbCustomerPrices > 0) { - $productCustomerPrice = $productCustomerPriceStatic->lines[0]; + $productCustomerPrice = null; + if (count($productCustomerPriceStatic->lines) > 0) { + $date_now = (int) floor(dol_now() / 86400) * 86400; // date without hours + foreach ($productCustomerPriceStatic->lines as $k => $custprice_line) { + if ($custprice_line->date_begin <= $date_now && (empty($custprice_line->date_end) || $date_now <= $custprice_line->date_end)) { + $productCustomerPrice = $custprice_line; + break; + } + } + } - if (!empty($productCustomerPrice->ref_customer)) { + if (isset($productCustomerPrice) && !empty($productCustomerPrice->ref_customer)) { switch ($conf->global->PRODUIT_CUSTOMER_PRICES_PDF_REF_MODE) { case 1: $ref_prodserv = $productCustomerPrice->ref_customer; diff --git a/htdocs/core/modules/modProduct.class.php b/htdocs/core/modules/modProduct.class.php index 166cf89dd38..b2ff61b9e58 100644 --- a/htdocs/core/modules/modProduct.class.php +++ b/htdocs/core/modules/modProduct.class.php @@ -380,11 +380,14 @@ class modProduct extends DolibarrModules $this->export_fields_array[$r] = array('p.rowid' => "Id", 'p.ref' => "Ref", 'p.label' => "Label", 's.nom' => 'ThirdParty', 's.code_client' => 'CodeClient', + 'pr.date_begin' => "AppliedPricesFrom", + 'pr.date_end' => "AppliedPricesTo", 'pr.price_base_type' => "PriceBase", 'pr.price' => "PriceUnitPriceHT", 'pr.price_ttc' => "PriceUnitPriceTTC", 'pr.price_min' => "MinPriceUnitPriceHT", 'pr.price_min_ttc' => "MinPriceUnitPriceTTC", 'pr.tva_tx' => 'PriceVATRate', 'pr.default_vat_code' => 'PriceVATCode', + 'pr.discount_percent' => 'Discount', 'pr.datec' => 'DateCreation'); if (is_object($mysoc) && $usenpr) { $this->export_fields_array[$r]['pr.recuperableonly'] = 'NPR'; @@ -392,11 +395,14 @@ class modProduct extends DolibarrModules $this->export_entities_array[$r] = array('p.rowid' => "product", 'p.ref' => "product", 'p.label' => "Label", 's.nom' => 'company', 's.code_client' => 'company', + 'pr.date_begin' => "product", + 'pr.date_end' => "product", 'pr.price_base_type' => "product", 'pr.price' => "product", 'pr.price_ttc' => "product", 'pr.price_min' => "product", 'pr.price_min_ttc' => "product", 'pr.tva_tx' => 'product', 'pr.default_vat_code' => 'product', + 'pr.discount_percent' => 'product', 'pr.recuperableonly' => 'product', 'pr.datec' => "product"); $this->export_sql_start[$r] = 'SELECT DISTINCT '; diff --git a/htdocs/core/modules/modService.class.php b/htdocs/core/modules/modService.class.php index b93531bdd6e..f4b7592eb13 100644 --- a/htdocs/core/modules/modService.class.php +++ b/htdocs/core/modules/modService.class.php @@ -327,22 +327,28 @@ class modService extends DolibarrModules $this->export_permission[$r] = array(array("service", "export")); $this->export_fields_array[$r] = array('p.rowid' => "Id", 'p.ref' => "Ref", 's.nom' => 'ThirdParty', + 'pr.date_begin' => "AppliedPricesFrom", + 'pr.date_end' => "AppliedPricesTo", 'pr.price_base_type' => "PriceBase", 'pr.price' => "PriceUnitPriceHT", 'pr.price_ttc' => "PriceUnitPriceTTC", 'pr.price_min' => "MinPriceUnitPriceHT", 'pr.price_min_ttc' => "MinPriceUnitPriceTTC", 'pr.tva_tx' => 'PriceVATRate', 'pr.default_vat_code' => 'PriceVATCode', + 'pr.discount_percent' => 'Discount', 'pr.datec' => 'DateCreation'); if (is_object($mysoc) && $usenpr) { $this->export_fields_array[$r]['pr.recuperableonly'] = 'NPR'; } $this->export_entities_array[$r] = array('p.rowid' => "product", 'p.ref' => "product", 's.nom' => 'company', + 'pr.date_begin' => "product", + 'pr.date_end' => "product", 'pr.price_base_type' => "product", 'pr.price' => "product", 'pr.price_ttc' => "product", 'pr.price_min' => "product", 'pr.price_min_ttc' => "product", 'pr.tva_tx' => 'product', 'pr.default_vat_code' => 'product', + 'pr.discount_percent' => 'product', 'pr.recuperableonly' => 'product', 'pr.datec' => "product"); $this->export_sql_start[$r] = 'SELECT DISTINCT '; diff --git a/htdocs/core/tpl/objectline_create.tpl.php b/htdocs/core/tpl/objectline_create.tpl.php index df96090135a..fa0044433ac 100644 --- a/htdocs/core/tpl/objectline_create.tpl.php +++ b/htdocs/core/tpl/objectline_create.tpl.php @@ -938,6 +938,13 @@ if (!empty($usemargins) && $user->hasRight('margins', 'creer')) { $('#tva_tx option[value="'+stringforvatrateselection+'"]').prop('selected', true); socid)) { + ?> + $("#remise_percent").val(data.discount); + var proddesc = data.desc_trans; diff --git a/htdocs/langs/en_US/products.lang b/htdocs/langs/en_US/products.lang index 84cfffe0780..23636fb9779 100644 --- a/htdocs/langs/en_US/products.lang +++ b/htdocs/langs/en_US/products.lang @@ -69,6 +69,8 @@ UpdateVAT=Update VAT UpdateDefaultPrice=Update default price UpdateLevelPrices=Update prices for each level AppliedPricesFrom=Applied from +AppliedPricesTo=Applied to +ErrorAppliedPricesIntersectAnotherPeriod=The begin or end date intersect to another existing period. SellingPrice=Selling price SellingPriceHT=Selling price (excl. tax) SellingPriceTTC=Selling price (inc. tax) diff --git a/htdocs/langs/fr_FR/products.lang b/htdocs/langs/fr_FR/products.lang index 0b5954b3394..53734c04008 100644 --- a/htdocs/langs/fr_FR/products.lang +++ b/htdocs/langs/fr_FR/products.lang @@ -69,6 +69,8 @@ UpdateVAT=Mettre à jour la TVA UpdateDefaultPrice=Modifier prix par défaut UpdateLevelPrices=Modifier prix pour chaque niveau AppliedPricesFrom=Pratiqués à partir du +AppliedPricesTo=Pratiqués jusqu'au +ErrorAppliedPricesIntersectAnotherPeriod=La date de début ou de fin rentre en conflit avec une période de dates existante. SellingPrice=Prix de vente SellingPriceHT=Prix de vente HT SellingPriceTTC=Prix de vente TTC diff --git a/htdocs/product/ajax/products.php b/htdocs/product/ajax/products.php index 274de7cce07..85e9a6fcfc5 100644 --- a/htdocs/product/ajax/products.php +++ b/htdocs/product/ajax/products.php @@ -220,13 +220,20 @@ if ($action == 'fetch' && !empty($id)) { $result = $prodcustprice->fetchAll('', '', 0, 0, $filter); if ($result) { if (count($prodcustprice->lines) > 0) { - $found = true; - $outprice_ht = price($prodcustprice->lines[0]->price); - $outprice_ttc = price($prodcustprice->lines[0]->price_ttc); - $outpricebasetype = $prodcustprice->lines[0]->price_base_type; - $outtva_tx_formated = price($prodcustprice->lines[0]->tva_tx); - $outtva_tx = price2num($prodcustprice->lines[0]->tva_tx); - $outdefault_vat_code = $prodcustprice->lines[0]->default_vat_code; + $date_now = (int) floor(dol_now() / 86400) * 86400; // date without hours + foreach ($prodcustprice->lines as $k => $custprice_line) { + if ($custprice_line->date_begin <= $date_now && (empty($custprice_line->date_end) || $date_now <= $custprice_line->date_end)) { + $found = true; + $outprice_ht = price($custprice_line->price); + $outprice_ttc = price($custprice_line->price_ttc); + $outpricebasetype = $custprice_line->price_base_type; + $outtva_tx_formated = price($custprice_line->tva_tx); + $outtva_tx = price2num($custprice_line->tva_tx); + $outdefault_vat_code = $custprice_line->default_vat_code; + $outdiscount = $custprice_line->discount_percent; + break; + } + } } } } diff --git a/htdocs/product/card.php b/htdocs/product/card.php index f685f012a5b..63bf5565aed 100644 --- a/htdocs/product/card.php +++ b/htdocs/product/card.php @@ -1156,10 +1156,16 @@ if (empty($reshook)) { $result = $prodcustprice->fetchAll('', '', 0, 0, $filter); if ($result) { if (count($prodcustprice->lines) > 0) { - $pu_ht = price($prodcustprice->lines [0]->price); - $pu_ttc = price($prodcustprice->lines [0]->price_ttc); - $price_base_type = $prodcustprice->lines [0]->price_base_type; - $tva_tx = $prodcustprice->lines [0]->tva_tx; + $date_now = (int) floor(dol_now() / 86400) * 86400; // date without hours + foreach ($prodcustprice->lines as $k => $custprice_line) { + if ($custprice_line->date_begin <= $date_now && (empty($custprice_line->date_end) || $date_now <= $custprice_line->date_end)) { + $pu_ht = price($custprice_line->price); + $pu_ttc = price($custprice_line->price_ttc); + $price_base_type = $custprice_line->price_base_type; + $tva_tx = $custprice_line->tva_tx; + break; + } + } } } } diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index b7d714b1722..f06a8fd29ec 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -2354,18 +2354,24 @@ class Product extends CommonObject $result = $prodcustprice->fetchAll('', '', 0, 0, $filter); if ($result) { if (count($prodcustprice->lines) > 0) { - $pricebycustomerexist = true; - $pu_ht = price($prodcustprice->lines[0]->price); - $price_min = price($prodcustprice->lines[0]->price_min); - $pu_ttc = price($prodcustprice->lines[0]->price_ttc); - $price_base_type = $prodcustprice->lines[0]->price_base_type; - $tva_tx = $prodcustprice->lines[0]->tva_tx; - if ($prodcustprice->lines[0]->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) { - $tva_tx .= ' ('.$prodcustprice->lines[0]->default_vat_code.')'; - } - $tva_npr = $prodcustprice->lines[0]->recuperableonly; - if (empty($tva_tx)) { - $tva_npr = 0; + $date_now = (int) floor(dol_now() / 86400) * 86400; // date without hours + foreach ($prodcustprice->lines as $k => $custprice_line) { + if ($custprice_line->date_begin <= $date_now && (empty($custprice_line->date_end) || $date_now <= $custprice_line->date_end)) { + $pricebycustomerexist = true; + $pu_ht = price($custprice_line->price); + $price_min = price($custprice_line->price_min); + $pu_ttc = price($custprice_line->price_ttc); + $price_base_type = $custprice_line->price_base_type; + $tva_tx = $custprice_line->tva_tx; + if ($custprice_line->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) { + $tva_tx .= ' (' . $custprice_line->default_vat_code . ')'; + } + $tva_npr = $custprice_line->recuperableonly; + if (empty($tva_tx)) { + $tva_npr = 0; + } + break; + } } } } @@ -2415,17 +2421,23 @@ class Product extends CommonObject $result = $prodcustprice->fetchAll('', '', 0, 0, $filter); if ($result) { if (count($prodcustprice->lines) > 0) { - $pu_ht = price($prodcustprice->lines[0]->price); - $price_min = price($prodcustprice->lines[0]->price_min); - $pu_ttc = price($prodcustprice->lines[0]->price_ttc); - $price_base_type = $prodcustprice->lines[0]->price_base_type; - $tva_tx = $prodcustprice->lines[0]->tva_tx; - if ($prodcustprice->lines[0]->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) { - $tva_tx .= ' ('.$prodcustprice->lines[0]->default_vat_code.')'; - } - $tva_npr = $prodcustprice->lines[0]->recuperableonly; - if (empty($tva_tx)) { - $tva_npr = 0; + $date_now = (int) floor(dol_now() / 86400) * 86400; // date without hours + foreach ($prodcustprice->lines as $k => $custprice_line) { + if ($custprice_line->date_begin <= $date_now && (empty($custprice_line->date_end) || $date_now <= $custprice_line->date_end)) { + $pu_ht = price($custprice_line->price); + $price_min = price($custprice_line->price_min); + $pu_ttc = price($custprice_line->price_ttc); + $price_base_type = $custprice_line->price_base_type; + $tva_tx = $custprice_line->tva_tx; + if ($custprice_line->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) { + $tva_tx .= ' (' . $custprice_line->default_vat_code . ')'; + } + $tva_npr = $custprice_line->recuperableonly; + if (empty($tva_tx)) { + $tva_npr = 0; + } + break; + } } } } diff --git a/htdocs/product/class/productcustomerprice.class.php b/htdocs/product/class/productcustomerprice.class.php index 4838885cb07..fe073e962f5 100644 --- a/htdocs/product/class/productcustomerprice.class.php +++ b/htdocs/product/class/productcustomerprice.class.php @@ -38,7 +38,8 @@ class ProductCustomerPrice extends CommonObject 'ref' => array('type' => 'varchar(128)', 'label' => 'Ref', 'enabled' => 1, 'visible' => 4, 'position' => 10, 'notnull' => 1, 'default' => '(PROV)', 'index' => 1, 'searchall' => 1, 'comment' => "Reference of object", 'showoncombobox' => 1, 'noteditable' => 1), 'fk_product' => array('type' => 'integer:Product:product/class/product.class.php:0', 'label' => 'Product', 'enabled' => '$conf->product->enabled', 'visible' => 1, 'position' => 35, 'notnull' => 1, 'index' => 1, 'comment' => "Product to produce", 'css' => 'maxwidth300', 'csslist' => 'tdoverflowmax100', 'picto' => 'product'), 'ref_customer' => array('type' => 'varchar(128)', 'label' => 'RefCustomer', 'enabled' => 1, 'visible' => 4, 'position' => 10, 'notnull' => 1,), - 'datec' => array('type' => 'datetime', 'label' => 'AppliedPricesFrom', 'enabled' => 1, 'visible' => 1, 'position' => 500, 'notnull' => 1,), + 'date_begin' => array('type' => 'date', 'label' => 'AppliedPricesFrom', 'enabled' => 1, 'visible' => 1, 'position' => 500, 'notnull' => 1,), + 'date_end' => array('type' => 'date', 'label' => 'AppliedPricesTo', 'enabled' => 1, 'visible' => 1, 'position' => 501, 'notnull' => 1,), 'price_base_type' => array('type' => 'varchar(255)', 'label' => 'PriceBase', 'enabled' => 1, 'visible' => 1, 'position' => 11, 'notnull' => -1, 'comment' => 'Price Base Type'), 'tva_tx' => array('type' => 'decimal(20,6)', 'label' => 'VAT', 'enabled' => 1, 'visible' => 1, 'position' => 12, 'notnull' => -1, 'comment' => 'TVA Tax Rate'), 'price' => array('type' => 'decimal(20,6)', 'label' => 'HT', 'enabled' => 1, 'visible' => 1, 'position' => 8, 'notnull' => -1, 'comment' => 'Price HT'), @@ -46,6 +47,7 @@ class ProductCustomerPrice extends CommonObject 'price_min' => array('type' => 'decimal(20,6)', 'label' => 'MinPriceHT', 'enabled' => 1, 'visible' => 1, 'position' => 9, 'notnull' => -1, 'comment' => 'Minimum Price'), 'price_min_ttc' => array('type' => 'decimal(20,6)', 'label' => 'MinPriceTTC', 'enabled' => 1, 'visible' => 1, 'position' => 10, 'notnull' => -1, 'comment' => 'Minimum Price TTC'), 'price_label' => array('type' => 'varchar(255)', 'label' => 'PriceLabel', 'enabled' => 1, 'visible' => 1, 'position' => 20, 'notnull' => -1, 'comment' => 'Price Label'), + 'discount_percent' => array('type' => 'decimal(20,6)', 'label' => 'Discount', 'enabled' => 1, 'visible' => 1, 'position' => 30, 'notnull' => -1, 'comment' => 'Discount'), 'fk_user' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => 1, 'position' => 510, 'notnull' => 1, 'foreignkey' => 'user.rowid', 'csslist' => 'tdoverflowmax100'), ); @@ -136,6 +138,18 @@ class ProductCustomerPrice extends CommonObject * @var string */ public $price_label; + /** + * @var float|string|'' + */ + public $discount_percent; + /** + * @var string|int + */ + public $date_begin = ''; + /** + * @var string|int + */ + public $date_end = ''; /** * @var int User ID @@ -158,6 +172,46 @@ class ProductCustomerPrice extends CommonObject $this->db = $db; } + /** + * Check if begin and end dates intersect other dates periods + * + * @return int Result <0 if KO, >0 if OK + */ + public function verifyDates() + { + global $langs; + + $sql = "SELECT COUNT(*) AS nb"; + $sql .= " FROM " . $this->db->prefix() . "product_customer_price as t"; + $sql .= " WHERE (t.date_begin = '" . $this->db->idate($this->date_begin) . "' OR t.date_end = '" . $this->db->idate($this->date_begin) . "'"; + $sql .= " OR t.date_begin = '" . $this->db->idate($this->date_end) . "' OR t.date_end = '" . $this->db->idate($this->date_end) . "'"; + $sql .= " OR (t.date_begin <= '" . $this->db->idate($this->date_begin) . "' AND '" . $this->db->idate($this->date_begin) . "' <= t.date_end)"; + $sql .= " OR (t.date_begin <= '" . $this->db->idate($this->date_end) . "' AND '" . $this->db->idate($this->date_end) . "' <= t.date_end))"; + if ($this->fk_product > 0) $sql .= " AND t.fk_product = " . ((int) $this->fk_product); + if ($this->fk_soc > 0) $sql .= " AND t.fk_soc = " . ((int) $this->fk_soc); + if ($this->id > 0) $sql .= " AND t.rowid != " . ((int) $this->id); + + dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); + $resql = $this->db->query($sql); + if (!$resql) { + $this->errors[] = "Error " . $this->db->lasterror(); + return -1; + } + + $nb = 0; + if ($obj = $this->db->fetch_object($resql)) { + $nb = (int) $obj->nb; + } + $this->db->free($resql); + + if ($nb > 0) { + $this->errors[] = $langs->trans('ErrorAppliedPricesIntersectAnotherPeriod'); + return -1; + } + + return 1; + } + /** * Create object into database * @@ -170,6 +224,7 @@ class ProductCustomerPrice extends CommonObject { global $conf, $langs; $error = 0; + $now = dol_now(); // Clean parameters @@ -212,6 +267,12 @@ class ProductCustomerPrice extends CommonObject if (isset($this->localtax2_tx)) { $this->localtax2_tx = trim($this->localtax2_tx); } + if (empty($this->discount_percent) || !is_numeric($this->discount_percent)) { + $this->discount_percent = 0; + } + if (empty($this->date_begin)) { + $this->date_begin = $now; + } if (isset($this->fk_user)) { $this->fk_user = (int) $this->fk_user; } @@ -224,6 +285,10 @@ class ProductCustomerPrice extends CommonObject // Check parameters // Put here code to add control on parameters values + $result = $this->verifyDates(); + if ($result < 0) { + return -1; + } if ($this->price != '' || $this->price == 0) { $vatRate = (float) $this->tva_tx; @@ -276,12 +341,15 @@ class ProductCustomerPrice extends CommonObject $sql .= "localtax1_tx,"; $sql .= "localtax2_type,"; $sql .= "localtax2_tx,"; + $sql .= "discount_percent,"; + $sql .= "date_begin,"; + $sql .= "date_end,"; $sql .= "fk_user,"; $sql .= "price_label,"; $sql .= "import_key"; $sql .= ") VALUES ("; $sql .= " ".((int) $conf->entity).","; - $sql .= " '".$this->db->idate(dol_now())."',"; + $sql .= " '".$this->db->idate($now)."',"; $sql .= " ".(!isset($this->fk_product) ? 'NULL' : ((int) $this->fk_product)).","; $sql .= " ".(!isset($this->fk_soc) ? 'NULL' : ((int) $this->fk_soc)).","; $sql .= " ".(!isset($this->ref_customer) ? 'NULL' : "'".$this->db->escape($this->ref_customer)."'").","; @@ -297,6 +365,9 @@ class ProductCustomerPrice extends CommonObject $sql .= " ".(!isset($this->localtax1_tx) ? 'NULL' : (empty($this->localtax1_tx) ? 0 : $this->localtax1_tx)).","; $sql .= " ".(empty($this->localtax2_type) ? "'0'" : "'".$this->db->escape($this->localtax2_type)."'").","; $sql .= " ".(!isset($this->localtax2_tx) ? 'NULL' : (empty($this->localtax2_tx) ? 0 : $this->localtax2_tx)).","; + $sql .= " ".(empty($this->discount_percent) ? '0' : "'".$this->db->escape(price2num($this->discount_percent))."'").","; + $sql .= " '".$this->db->idate($this->date_begin)."',"; + $sql .= " ".(empty($this->date_end) ? 'NULL' : "'".$this->db->idate($this->date_end)."'").","; $sql .= " ".((int) $user->id).","; $sql .= " ".(!isset($this->price_label) ? 'NULL' : "'".$this->db->escape($this->price_label)."'").","; $sql .= " ".(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'"); @@ -371,6 +442,9 @@ class ProductCustomerPrice extends CommonObject $sql .= " t.recuperableonly,"; $sql .= " t.localtax1_tx,"; $sql .= " t.localtax2_tx,"; + $sql .= " t.discount_percent,"; + $sql .= " t.date_begin,"; + $sql .= " t.date_end,"; $sql .= " t.fk_user,"; $sql .= " t.price_label,"; $sql .= " t.import_key"; @@ -401,6 +475,9 @@ class ProductCustomerPrice extends CommonObject $this->recuperableonly = $obj->recuperableonly; $this->localtax1_tx = $obj->localtax1_tx; $this->localtax2_tx = $obj->localtax2_tx; + $this->discount_percent = $obj->discount_percent; + $this->date_begin = $this->db->jdate($obj->date_begin); + $this->date_end = $this->db->jdate($obj->date_end); $this->fk_user = $obj->fk_user; $this->price_label = $obj->price_label; $this->import_key = $obj->import_key; @@ -433,7 +510,7 @@ class ProductCustomerPrice extends CommonObject public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '') { if (empty($sortfield)) { - $sortfield = "t.rowid"; + $sortfield = "t.date_begin"; } if (empty($sortorder)) { $sortorder = "DESC"; @@ -459,6 +536,9 @@ class ProductCustomerPrice extends CommonObject $sql .= " t.localtax2_tx,"; $sql .= " t.localtax1_type,"; $sql .= " t.localtax2_type,"; + $sql .= " t.discount_percent,"; + $sql .= " t.date_begin,"; + $sql .= " t.date_end,"; $sql .= " t.fk_user,"; $sql .= " t.price_label,"; $sql .= " t.import_key,"; @@ -482,7 +562,7 @@ class ProductCustomerPrice extends CommonObject $sql .= " AND ".$this->db->sanitize($key)." LIKE '%".$this->db->escape($this->db->escapeforlike($value))."%'"; } elseif ($key == 'prod.ref' || $key == 'prod.label') { $sql .= " AND ".$this->db->sanitize($key)." LIKE '%".$this->db->escape($this->db->escapeforlike($value))."%'"; - } elseif ($key == 't.price' || $key == 't.price_ttc') { + } elseif ($key == 't.price' || $key == 't.price_ttc' || $key == 't.discount_percent') { $sql .= " AND ".$this->db->sanitize($key)." = ".((float) price2num($value)); } else { $sql .= " AND ".$this->db->sanitize($key)." = ".((int) $value); @@ -536,6 +616,9 @@ class ProductCustomerPrice extends CommonObject $line->localtax2_tx = $obj->localtax2_tx; $line->localtax1_type = $obj->localtax1_type; $line->localtax2_type = $obj->localtax2_type; + $line->discount_percent = $obj->discount_percent; + $line->date_begin = $this->db->jdate($obj->date_begin); + $line->date_end = $this->db->jdate($obj->date_end); $line->fk_user = $obj->fk_user; $line->price_label = $obj->price_label; $line->import_key = $obj->import_key; @@ -565,10 +648,10 @@ class ProductCustomerPrice extends CommonObject */ public function fetchAllLog($sortorder, $sortfield, $limit, $offset, $filter = array()) { - if (!empty($sortfield)) { - $sortfield = "t.rowid"; + if (empty($sortfield)) { + $sortfield = "t.date_begin"; } - if (!empty($sortorder)) { + if (empty($sortorder)) { $sortorder = "DESC"; } @@ -589,6 +672,9 @@ class ProductCustomerPrice extends CommonObject $sql .= " t.recuperableonly,"; $sql .= " t.localtax1_tx,"; $sql .= " t.localtax2_tx,"; + $sql .= " t.discount_percent,"; + $sql .= " t.date_begin,"; + $sql .= " t.date_end,"; $sql .= " t.fk_user,"; $sql .= " t.price_label,"; $sql .= " t.import_key,"; @@ -645,6 +731,9 @@ class ProductCustomerPrice extends CommonObject $line->recuperableonly = $obj->recuperableonly; $line->localtax1_tx = $obj->localtax1_tx; $line->localtax2_tx = $obj->localtax2_tx; + $line->discount_percent = $obj->discount_percent; + $line->date_begin = $this->db->jdate($obj->date_begin); + $line->date_end = $this->db->jdate($obj->date_end); $line->fk_user = $obj->fk_user; $line->price_label = $obj->price_label; $line->import_key = $obj->import_key; @@ -674,6 +763,7 @@ class ProductCustomerPrice extends CommonObject { global $conf, $langs; $error = 0; + $now = dol_now(); // Clean parameters @@ -716,6 +806,12 @@ class ProductCustomerPrice extends CommonObject if (isset($this->localtax2_tx)) { $this->localtax2_tx = trim((string) $this->localtax2_tx); } + if (empty($this->discount_percent) || !is_numeric($this->discount_percent)) { + $this->discount_percent = 0; + } + if (empty($this->date_begin)) { + $this->date_begin = $now; + } if (isset($this->fk_user)) { $this->fk_user = (int) $this->fk_user; } @@ -728,6 +824,10 @@ class ProductCustomerPrice extends CommonObject // Check parameters // Put here code to add a control on parameters values + $result = $this->verifyDates(); + if ($result < 0) { + return -1; + } if ($this->price != '' || $this->price == 0) { $vatRate = (float) $this->tva_tx; @@ -782,6 +882,9 @@ class ProductCustomerPrice extends CommonObject $sql .= "localtax2_tx,"; $sql .= "localtax1_type,"; $sql .= "localtax2_type,"; + $sql .= "discount_percent,"; + $sql .= "date_begin,"; + $sql .= "date_end,"; $sql .= "fk_user,"; $sql .= "price_label,"; $sql .= "import_key"; @@ -806,6 +909,9 @@ class ProductCustomerPrice extends CommonObject $sql .= " t.localtax2_tx,"; $sql .= " t.localtax1_type,"; $sql .= " t.localtax2_type,"; + $sql .= " t.discount_percent,"; + $sql .= " t.date_begin,"; + $sql .= " t.date_end,"; $sql .= " t.fk_user,"; $sql .= " t.price_label,"; $sql .= " t.import_key"; @@ -825,7 +931,7 @@ class ProductCustomerPrice extends CommonObject $sql = "UPDATE ".$this->db->prefix()."product_customer_price SET"; $sql .= " entity=".((int) $conf->entity).","; - $sql .= " datec='".$this->db->idate(dol_now())."',"; + $sql .= " datec='".$this->db->idate($now)."',"; $sql .= " tms=".(dol_strlen((string) $this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').","; $sql .= " fk_product=".(isset($this->fk_product) ? $this->fk_product : "null").","; $sql .= " fk_soc=".(isset($this->fk_soc) ? $this->fk_soc : "null").","; @@ -842,6 +948,9 @@ class ProductCustomerPrice extends CommonObject $sql .= " localtax2_tx=".(isset($this->localtax2_tx) ? (empty($this->localtax2_tx) ? 0 : $this->localtax2_tx) : "null").","; $sql .= " localtax1_type=".(!empty($this->localtax1_type) ? "'".$this->db->escape($this->localtax1_type)."'" : "'0'").","; $sql .= " localtax2_type=".(!empty($this->localtax2_type) ? "'".$this->db->escape($this->localtax2_type)."'" : "'0'").","; + $sql .= " discount_percent=".(!empty($this->discount_percent) ? "'".price2num($this->discount_percent)."'" : "0").","; + $sql .= " date_begin='".$this->db->idate($this->date_begin)."',"; + $sql .= " date_end=".(!empty($this->date_end) ? "'".$this->db->idate($this->date_end)."'" : "null").","; $sql .= " fk_user=".((int) $user->id).","; $sql .= " price_label=".(isset($this->price_label) ? "'".$this->db->escape($this->price_label)."'" : "null").","; $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null"); @@ -939,6 +1048,9 @@ class ProductCustomerPrice extends CommonObject $prodsocpriceupd->tva_tx = $this->tva_tx; $prodsocpriceupd->recuperableonly = $this->recuperableonly; $prodsocpriceupd->price_label = $this->price_label; + $prodsocpriceupd->discount_percent = $this->discount_percent; + $prodsocpriceupd->date_begin = $this->date_begin; + $prodsocpriceupd->date_end = $this->date_end; $resultupd = $prodsocpriceupd->update($user, 0, $forceupdateaffiliate); if ($resultupd < 0) { @@ -958,6 +1070,9 @@ class ProductCustomerPrice extends CommonObject $prodsocpricenew->tva_tx = $this->tva_tx; $prodsocpricenew->recuperableonly = $this->recuperableonly; $prodsocpricenew->price_label = $this->price_label; + $prodsocpricenew->discount_percent = $this->discount_percent; + $prodsocpricenew->date_begin = $this->date_begin; + $prodsocpricenew->date_end = $this->date_end; $resultupd = $prodsocpricenew->create($user, 0, $forceupdateaffiliate); if ($resultupd < 0) { @@ -1102,6 +1217,9 @@ class ProductCustomerPrice extends CommonObject $this->recuperableonly = ''; $this->localtax1_tx = ''; $this->localtax2_tx = ''; + $this->discount_percent = ''; + $this->date_begin = ''; + $this->date_end = ''; $this->fk_user = 0; $this->price_label = ''; $this->import_key = ''; @@ -1185,6 +1303,18 @@ class PriceByCustomerLine extends CommonObjectLine * @var float */ public $localtax2_tx; + /** + * @var float|string|'' + */ + public $discount_percent; + /** + * @var string|int + */ + public $date_begin = ''; + /** + * @var string|int + */ + public $date_end = ''; /** * @var int User ID diff --git a/htdocs/product/price.php b/htdocs/product/price.php index 6c6a98b126e..eadcd487538 100644 --- a/htdocs/product/price.php +++ b/htdocs/product/price.php @@ -754,6 +754,9 @@ if (empty($reshook)) { $prodcustprice->price_min = price2num(GETPOST("price_min"), 'MU'); $prodcustprice->price_base_type = GETPOST("price_base_type", 'alpha'); $prodcustprice->price_label = GETPOST("price_label", 'alpha'); + $prodcustprice->discount_percent = price2num(GETPOST("discount_percent")); + $prodcustprice->date_begin = dol_mktime(0, 0, 0, GETPOSTINT('date_beginmonth'), GETPOSTINT('date_beginday'), GETPOSTINT('date_beginyear'), 'tzserver'); // If we enter the 02 january, we need to save the 02 january for server; + $prodcustprice->date_end = dol_mktime(0, 0, 0, GETPOSTINT('date_endmonth'), GETPOSTINT('date_endday'), GETPOSTINT('date_endyear'), 'tzserver'); // If we enter the 02 january, we need to save the 02 january for server $extralabels = $extrafields->fetch_name_optionals_label("product_customer_price"); $extrafield_values = $extrafields->getOptionalsFromPost("product_customer_price"); @@ -864,11 +867,11 @@ if (empty($reshook)) { if ($result < 0) { setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors'); + $action = 'add_customer_price'; } else { setEventMessages($langs->trans('RecordSaved'), null, 'mesgs'); + $action = ''; } - - $action = ''; } } @@ -906,6 +909,9 @@ if (empty($reshook)) { $prodcustprice->price_min = price2num(GETPOST("price_min"), 'MU'); $prodcustprice->price_base_type = GETPOST("price_base_type", 'alpha'); $prodcustprice->price_label = GETPOST("price_label", 'alpha'); + $prodcustprice->discount_percent = price2num(GETPOST("discount_percent")); + $prodcustprice->date_begin = dol_mktime(0, 0, 0, GETPOSTINT('date_beginmonth'), GETPOSTINT('date_beginday'), GETPOSTINT('date_beginyear'), 'tzserver'); // If we enter the 02 january, we need to save the 02 january for server; + $prodcustprice->date_end = dol_mktime(0, 0, 0, GETPOSTINT('date_endmonth'), GETPOSTINT('date_endday'), GETPOSTINT('date_endyear'), 'tzserver'); // If we enter the 02 january, we need to save the 02 january for server $extralabels = $extrafields->fetch_name_optionals_label("product_customer_price"); $extrafield_values = $extrafields->getOptionalsFromPost("product_customer_price"); @@ -1009,11 +1015,11 @@ if (empty($reshook)) { if ($result < 0) { setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors'); + $action = 'update_customer_price'; } else { setEventMessages($langs->trans("Save"), null, 'mesgs'); + $action = ''; } - - $action = ''; } } } @@ -2002,10 +2008,10 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT $pageprev = $page - 1; $pagenext = $page + 1; if (!$sortorder) { - $sortorder = "ASC"; + $sortorder = "ASC,ASC"; } if (!$sortfield) { - $sortfield = "soc.nom"; + $sortfield = "soc.nom,t.date_begin"; } // Build filter to display only concerned lines @@ -2034,7 +2040,7 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''.$langs->trans('ThirdParty').''; print ''; $filter = '(s.client:IN:1,2,3)'; - print img_picto('', 'company').$form->select_company('', 'socid', $filter, 'SelectThirdParty', 0, 0, array(), 0, 'minwidth300'); + print img_picto('', 'company').$form->select_company(GETPOSTINT('socid'), 'socid', $filter, 'SelectThirdParty', 0, 0, array(), 0, 'minwidth300'); print ''; print ''; @@ -2042,6 +2048,18 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print '' . $langs->trans('RefCustomer') . ''; print ''; + // Applied Prices From + $date_begin = dol_mktime(0, 0, 0, GETPOSTINT('date_beginmonth'), GETPOSTINT('date_beginday'), GETPOSTINT('date_beginyear'), 'tzserver'); // If we enter the 02 january, we need to save the 02 january for server; + print ''.$langs->trans("AppliedPricesFrom").''; + print $form->selectDate(!empty($date_begin) ? $date_begin : dol_now(), "date_begin", 0, 0, 1, "date_begin"); + print ''; + + // Applied Prices To + $date_end = dol_mktime(0, 0, 0, GETPOSTINT('date_endmonth'), GETPOSTINT('date_endday'), GETPOSTINT('date_endyear'), 'tzserver'); // If we enter the 02 january, we need to save the 02 january for server + print ''.$langs->trans("AppliedPricesTo").''; + print $form->selectDate($date_end, "date_end", 0, 0, 1, "date_end"); + print ''; + // VAT print ''.$langs->trans("DefaultTaxRate").''; print $form->load_tva("tva_tx", $object->default_vat_code ? $object->tva_tx.' ('.$object->default_vat_code.')' : $object->tva_tx, $mysoc, null, $object->id, $object->tva_npr, $object->type, false, 1); @@ -2090,6 +2108,12 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''; print ''; + // Discount + $discount_percent = price2num(GETPOST("discount_percent")); + print ''.$langs->trans("Discount").''; + print ''; + print ''; + // Extrafields $extrafields->fetch_name_optionals_label("product_customer_price"); $extralabels = !empty($extrafields->attributes["product_customer_price"]['label']) ? $extrafields->attributes["product_customer_price"]['label'] : ''; @@ -2159,6 +2183,16 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print '' . $langs->trans('RefCustomer') . ''; print ''; + // Applied Prices From + print ''.$langs->trans("AppliedPricesFrom").''; + print $form->selectDate($prodcustprice->date_begin, "date_begin", 0, 0, 1, "date_begin"); + print ''; + + // Applied Prices To + print ''.$langs->trans("AppliedPricesTo").''; + print $form->selectDate($prodcustprice->date_end, "date_end", 0, 0, 1, "date_end"); + print ''; + // VAT print ''.$langs->trans("DefaultTaxRate").''; print $form->load_tva("tva_tx", $prodcustprice->default_vat_code ? $prodcustprice->tva_tx.' ('.$prodcustprice->default_vat_code.')' : $prodcustprice->tva_tx, $mysoc, null, $object->id, $prodcustprice->recuperableonly, $object->type, false, 1); @@ -2201,8 +2235,6 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT } print ''; - - // Price Label print ''; print $langs->trans('PriceLabel'); @@ -2211,6 +2243,11 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''; print ''; + // Discount + print ''.$langs->trans("Discount").''; + print ''; + print ''; + // Extrafields $extrafields->fetch_name_optionals_label("product_customer_price"); $extralabels = !empty($extrafields->attributes["product_customer_price"]['label']) ? $extrafields->attributes["product_customer_price"]['label'] : ''; @@ -2280,6 +2317,7 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT // List of all log of prices by customers print ''."\n"; + $sortfield = 't.datec'; $filter = array('t.fk_product' => (string) $object->id, 't.fk_soc' => (string) GETPOSTINT('socid')); // Count total nb of records @@ -2318,6 +2356,7 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''.$langs->trans("ThirdParty").''; print ''.$langs->trans('RefCustomer').''; print ''.$langs->trans("AppliedPricesFrom").''; + print ''.$langs->trans("AppliedPricesTo").''; print ''.$langs->trans("PriceBase").''; print ''.$langs->trans("DefaultTaxRate").''; print ''.$langs->trans("HT").''; @@ -2328,8 +2367,9 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''.$langs->trans("MinPrice").' '.$langs->trans("HT").''; print ''.$langs->trans("MinPrice").' '.$langs->trans("TTC").''; print ''.$langs->trans("PriceLabel").''; + print ''.$langs->trans("Discount").''; print ''.$langs->trans("ChangedBy").''; - print ' '; + print ''.$langs->trans("DateCreation").''; print ''; foreach ($prodcustprice->lines as $line) { @@ -2361,7 +2401,8 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''.$staticsoc->getNomUrl(1).""; print ''.$line->ref_customer.''; - print "".dol_print_date($line->datec, "dayhour", 'tzuserrel').""; + print "".dol_print_date($line->date_begin, "day", 'tzuserrel').""; + print "".dol_print_date($line->date_end, "day", 'tzuserrel').""; print ''.$langs->trans($line->price_base_type).""; print ''; @@ -2393,6 +2434,7 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''.price($line->price_min).''; print ''.price($line->price_min_ttc).''; print ''.$line->price_label.''; + print ''.price($line->discount_percent).''; // User $userstatic = new User($db); @@ -2401,6 +2443,7 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print $userstatic->getNomUrl(1, '', 0, 0, 24, 0, 'login'); //print $userstatic->getLoginUrl(1); print ''; + print "".dol_print_date($line->datec, "dayhour", 'tzuserrel').""; print ''; } print ""; @@ -2440,9 +2483,9 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT $extrafields->fetch_name_optionals_label("product_customer_price"); $custom_price_extralabels = !empty($extrafields->attributes["product_customer_price"]['label']) ? $extrafields->attributes["product_customer_price"]['label'] : ''; if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) { - $colspan = 10; - } else { $colspan = 11; + } else { + $colspan = 12; } if ($mysoc->localtax1_assuj == "1" || $mysoc->localtax2_assuj == "1") { $colspan++; @@ -2466,6 +2509,7 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''.$langs->trans("ThirdParty").''; print ''.$langs->trans('RefCustomer').''; print ''.$langs->trans("AppliedPricesFrom").''; + print ''.$langs->trans("AppliedPricesTo").''; print ''.$langs->trans("PriceBase").''; print ''.$langs->trans("DefaultTaxRate").''; print ''.$langs->trans("HT").''; @@ -2476,6 +2520,7 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''.$langs->trans("MinPrice").' '.$langs->trans("HT").''; print ''.$langs->trans("MinPrice").' '.$langs->trans("TTC").''; print ''.$langs->trans("PriceLabel").''; + print ''.$langs->trans("Discount").''; // fetch optionals attributes and labels $extrafields->fetch_name_optionals_label("product_customer_price"); if ($extrafields->attributes["product_customer_price"] && array_key_exists('label', $extrafields->attributes["product_customer_price"])) { @@ -2522,7 +2567,7 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT if (!getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) { print ''."\n"; print ''; - print '' . $langs->trans('Default') . ''; + print '' . $langs->trans('Default') . ''; print ''.$langs->trans($object->price_base_type).""; @@ -2560,6 +2605,7 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''.price($object->price_min_ttc).''; print ''.$object->price_label.''; print ''; + print ''; if (!empty($extralabels)) { foreach ($extralabels as $key) { // Show field if not hidden @@ -2612,7 +2658,8 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''.$staticsoc->getNomUrl(1).""; print ''.dol_escape_htmltag($line->ref_customer).''; - print "".dol_print_date($line->datec, "dayhour", 'tzuserrel').""; + print "".dol_print_date($line->date_begin, "day", 'tzuserrel').""; + print "".dol_print_date($line->date_end, "day", 'tzuserrel').""; print ''.$langs->trans($line->price_base_type).""; // VAT Rate print ''; @@ -2646,6 +2693,7 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''.price($line->price_min).''; print ''.price($line->price_min_ttc).''; print ''.$line->price_label.''; + print ''.price($line->discount_percent).''; // Extrafields $extrafields->fetch_name_optionals_label("product_customer_price"); diff --git a/htdocs/societe/price.php b/htdocs/societe/price.php index 865b774e30c..81d72787cce 100644 --- a/htdocs/societe/price.php +++ b/htdocs/societe/price.php @@ -98,7 +98,7 @@ if (empty($reshook)) { $search_prod = $search_label = $search_price = $search_price_ttc = ''; } - if ($action == 'add_customer_price_confirm' && !$cancel && ($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer'))) { + if ($action == 'add_customer_price_confirm' && $prodcustprice !== null && !$cancel && ($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer'))) { if (!(GETPOSTINT('prodid') > 0)) { $error++; setEventMessages($langs->trans("ErrorFieldRequired", $langs->trans("Product")), null, 'errors'); @@ -115,6 +115,9 @@ if (empty($reshook)) { $prodcustprice->price = price2num(GETPOST("price"), 'MU'); $prodcustprice->price_min = price2num(GETPOST("price_min"), 'MU'); $prodcustprice->price_base_type = GETPOST("price_base_type", 'alpha'); + $prodcustprice->discount_percent = price2num(GETPOST("discount_percent")); + $prodcustprice->date_begin = dol_mktime(0, 0, 0, GETPOSTINT('date_beginmonth'), GETPOSTINT('date_beginday'), GETPOSTINT('date_beginyear'), 'tzserver'); // If we enter the 02 january, we need to save the 02 january for server; + $prodcustprice->date_end = dol_mktime(0, 0, 0, GETPOSTINT('date_endmonth'), GETPOSTINT('date_endday'), GETPOSTINT('date_endyear'), 'tzserver'); // If we enter the 02 january, we need to save the 02 january for server $tva_tx_txt = GETPOST('tva_tx', 'alpha'); // tva_tx can be '8.5' or '8.5*' or '8.5 (XXX)' or '8.5* (XXX)' @@ -180,15 +183,15 @@ if (empty($reshook)) { if ($result < 0) { setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors'); + $action = 'add_customer_price'; } else { setEventMessages($langs->trans("Save"), null, 'mesgs'); + $action = ''; } - - $action = ''; } } - if ($action == 'delete_customer_price' && ($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer'))) { + if ($action == 'delete_customer_price' && $prodcustprice !== null && ($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer'))) { // Delete price by customer $prodcustprice->id = GETPOSTINT('lineid'); $result = $prodcustprice->delete($user); @@ -201,7 +204,7 @@ if (empty($reshook)) { $action = ''; } - if ($action == 'update_customer_price_confirm' && !$cancel && ($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer'))) { + if ($action == 'update_customer_price_confirm' && $prodcustprice !== null && !$cancel && ($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer'))) { $prodcustprice->fetch(GETPOSTINT('lineid')); $update_child_soc = GETPOSTINT('updatechildprice'); @@ -213,6 +216,9 @@ if (empty($reshook)) { $prodcustprice->price_base_type = GETPOST("price_base_type", 'alpha'); $prodcustprice->tva_tx = str_replace('*', '', GETPOST("tva_tx")); $prodcustprice->recuperableonly = (preg_match('/\*/', GETPOST("tva_tx")) ? 1 : 0); + $prodcustprice->discount_percent = price2num(GETPOST("discount_percent")); + $prodcustprice->date_begin = dol_mktime(0, 0, 0, GETPOSTINT('date_beginmonth'), GETPOSTINT('date_beginday'), GETPOSTINT('date_beginyear'), 'tzserver'); // If we enter the 02 january, we need to save the 02 january for server; + $prodcustprice->date_end = dol_mktime(0, 0, 0, GETPOSTINT('date_endmonth'), GETPOSTINT('date_endday'), GETPOSTINT('date_endyear'), 'tzserver'); // If we enter the 02 january, we need to save the 02 january for server $result = $prodcustprice->update($user, 0, $update_child_soc); if ($result > 0) { @@ -237,11 +243,11 @@ if (empty($reshook)) { } if ($result < 0) { setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors'); + $action = 'edit_customer_price'; } else { setEventMessages($langs->trans("Save"), null, 'mesgs'); + $action = ''; } - - $action = ''; } } @@ -323,10 +329,10 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT $pageprev = $page - 1; $pagenext = $page + 1; if (!$sortorder) { - $sortorder = "ASC"; + $sortorder = "ASC,ASC"; } if (!$sortfield) { - $sortfield = "soc.nom"; + $sortfield = "soc.nom,t.date_begin"; } // Build filter to display only related lines @@ -368,7 +374,7 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''; print ''.$langs->trans('Product').''; print ''; - $form->select_produits(0, 'prodid', '', 0); + $form->select_produits(GETPOSTINT('prodid'), 'prodid', '', 0); print ''; print ''; @@ -376,6 +382,18 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''.$langs->trans('RefCustomer').''; print ''; + // Applied Prices From + $date_begin = dol_mktime(0, 0, 0, GETPOSTINT('date_beginmonth'), GETPOSTINT('date_beginday'), GETPOSTINT('date_beginyear'), 'tzserver'); // If we enter the 02 january, we need to save the 02 january for server; + print ''.$langs->trans("AppliedPricesFrom").''; + print $form->selectDate(!empty($date_begin) ? $date_begin : dol_now(), "date_begin", 0, 0, 1, "date_begin"); + print ''; + + // Applied Prices To + $date_end = dol_mktime(0, 0, 0, GETPOSTINT('date_endmonth'), GETPOSTINT('date_endday'), GETPOSTINT('date_endyear'), 'tzserver'); // If we enter the 02 january, we need to save the 02 january for server + print ''.$langs->trans("AppliedPricesTo").''; + print $form->selectDate($date_end, "date_end", 0, 0, 1, "date_end"); + print ''; + // VAT print ''.$langs->trans("VATRate").''; print $form->load_tva("tva_tx", GETPOST("tva_tx", "alpha"), $mysoc, null, $object->id, 0, '', false, 1); @@ -405,6 +423,12 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''; print ''; + // Discount + $discount_percent = price2num(GETPOST("discount_percent")); + print ''.$langs->trans("Discount").''; + print ''; + print ''; + // Update all child soc print ''; print $langs->trans('ForceUpdateChildPriceSoc'); @@ -469,6 +493,16 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''.$langs->trans('RefCustomer').''; print ''; + // Applied Prices From + print ''.$langs->trans("AppliedPricesFrom").''; + print $form->selectDate($prodcustprice->date_begin, "date_begin", 0, 0, 1, "date_begin"); + print ''; + + // Applied Prices To + print ''.$langs->trans("AppliedPricesTo").''; + print $form->selectDate($prodcustprice->date_end, "date_end", 0, 0, 1, "date_end"); + print ''; + // VAT print ''.$langs->trans("VATRate").''; print $form->load_tva("tva_tx", $prodcustprice->tva_tx, $mysoc, null, $staticprod->id, $prodcustprice->recuperableonly); @@ -507,6 +541,11 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT } print ''; + // Discount + print ''.$langs->trans("Discount").''; + print ''; + print ''; + // Extrafields $extrafields->fetch_name_optionals_label("product_customer_price"); $extralabels = !empty($extrafields->attributes["product_customer_price"]['label']) ? $extrafields->attributes["product_customer_price"]['label'] : ''; @@ -577,6 +616,7 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print '
'; print ''."\n"; + $sortfield = 't.datec'; $filter = array( 't.fk_product' => (string) GETPOSTINT('prodid'), 't.fk_soc' => (string) $socid @@ -609,14 +649,16 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''.$langs->trans("Product").''; print ''.$langs->trans('RefCustomer').''; print ''.$langs->trans("AppliedPricesFrom").''; + print ''.$langs->trans("AppliedPricesTo").''; print ''.$langs->trans("PriceBase").''; print ''.$langs->trans("VAT").''; print ''.$langs->trans("HT").''; print ''.$langs->trans("TTC").''; print ''.$langs->trans("MinPrice").' '.$langs->trans("HT").''; print ''.$langs->trans("MinPrice").' '.$langs->trans("TTC").''; + print ''.$langs->trans("Discount").''; print ''.$langs->trans("ChangedBy").''; - print ''; + print ''.$langs->trans("DateCreation").''; print ''; foreach ($prodcustprice->lines as $line) { @@ -630,7 +672,8 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print "".$staticprod->getNomUrl(1).""; print ''.$line->ref_customer.''; - print "".dol_print_date($line->datec, "dayhour").""; + print "".dol_print_date($line->date_begin, "day", 'tzuserrel').""; + print "".dol_print_date($line->date_end, "day", 'tzuserrel').""; print ''.$langs->trans($line->price_base_type).""; print ''.vatrate($line->tva_tx, true, $line->recuperableonly).""; @@ -638,12 +681,13 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''.price($line->price_ttc).""; print ''.price($line->price_min).''; print ''.price($line->price_min_ttc).''; + print ''.price($line->discount_percent).''; // User print ''; print $userstatic->getNomUrl(-1); print ''; - print ''; + print "".dol_print_date($line->datec, "dayhour", 'tzuserrel').""; } print ""; } else { @@ -786,12 +830,14 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''; print ''; print ''; + print ''; print ''; print ''; print ''; print ''; print ''; print ''; + print ''; if (!empty($extralabels)) { foreach ($extralabels as $key) { print ''; @@ -818,7 +864,8 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''.$staticprod->getNomUrl(1).""; print ''.$staticprod->label.""; print ''.$line->ref_customer.''; - print ''.dol_print_date($line->datec, "dayhour").""; + print ''.dol_print_date($line->date_begin, "day", 'tzuserrel').""; + print ''.dol_print_date($line->date_end, "day", 'tzuserrel').""; print ''.$langs->trans($line->price_base_type).""; print ''.vatrate($line->tva_tx.($line->default_vat_code ? ' ('.$line->default_vat_code.')' : ''), true, $line->recuperableonly).""; print ''.price($line->price).""; @@ -826,6 +873,7 @@ if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT print ''.price($line->price_min).''; print ''.price($line->price_min_ttc).''; print ''.$line->price_label.''; + print ''.price($line->discount_percent).''; // User print ''; print $userstatic->getNomUrl(-1);