fournisseurs.php 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. <?php
  2. /* Copyright (C) 2001-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2021 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2010-2012 Juanjo Menent <jmenent@2byte.es>
  7. * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
  8. * Copyright (C) 2014 Ion Agorria <ion@agorria.com>
  9. * Copyright (C) 2015 Alexandre Spangaro <aspangaro@open-dsi.fr>
  10. * Copyright (C) 2016 Ferran Marcet <fmarcet@2byte.es>
  11. * Copyright (C) 2019 Frédéric France <frederic.france@netlogic.fr>
  12. * Copyright (C) 2019 Tim Otte <otte@meuser.it>
  13. * Copyright (C) 2020 Pierre Ardoin <mapiolca@me.com>
  14. * Copyright (C) 2023 Joachim Kueter <git-jk@bloxera.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  28. */
  29. /**
  30. * \file htdocs/product/fournisseurs.php
  31. * \ingroup product
  32. * \brief Page of tab suppliers for products
  33. */
  34. // Load Dolibarr environment
  35. require '../main.inc.php';
  36. require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  37. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  38. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  39. require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  40. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
  41. require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_expression.class.php';
  42. require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
  43. if (isModEnabled('barcode')) {
  44. dol_include_once('/core/class/html.formbarcode.class.php');
  45. }
  46. // Load translation files required by the page
  47. $langs->loadLangs(array('products', 'suppliers', 'bills', 'margins', 'stocks'));
  48. $id = GETPOST('id', 'int');
  49. $ref = GETPOST('ref', 'alpha');
  50. $rowid = GETPOST('rowid', 'int');
  51. $action = GETPOST('action', 'aZ09');
  52. $cancel = GETPOST('cancel', 'alpha');
  53. $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'pricesuppliercard';
  54. $socid = GETPOST('socid', 'int');
  55. $cost_price = price2num(GETPOST('cost_price', 'alpha'), '', 2);
  56. $pmp = price2num(GETPOST('pmp', 'alpha'), '', 2);
  57. $backtopage = GETPOST('backtopage', 'alpha');
  58. $error = 0;
  59. $extrafields = new ExtraFields($db);
  60. // If socid provided by ajax company selector
  61. if (GETPOST('search_fourn_id', 'int')) {
  62. $_GET['id_fourn'] = GETPOST('search_fourn_id', 'int');
  63. $_POST['id_fourn'] = GETPOST('search_fourn_id', 'int');
  64. }
  65. // Security check
  66. $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
  67. $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
  68. if ($user->socid) {
  69. $socid = $user->socid;
  70. }
  71. if (empty($user->rights->fournisseur->lire) && (empty($conf->margin->enabled) && !$user->hasRight("margin", "liretous"))) {
  72. accessforbidden();
  73. }
  74. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  75. $sortfield = GETPOST('sortfield', 'aZ09comma');
  76. $sortorder = GETPOST('sortorder', 'aZ09comma');
  77. $page = (GETPOST("page", 'int') ?GETPOST("page", 'int') : 0);
  78. if (empty($page) || $page == -1) {
  79. $page = 0;
  80. } // If $page is not defined, or '' or -1
  81. $offset = $limit * $page;
  82. $pageprev = $page - 1;
  83. $pagenext = $page + 1;
  84. if (!$sortfield) {
  85. $sortfield = "s.nom";
  86. }
  87. if (!$sortorder) {
  88. $sortorder = "ASC";
  89. }
  90. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  91. $hookmanager->initHooks(array('pricesuppliercard', 'globalcard'));
  92. $object = new ProductFournisseur($db);
  93. if ($id > 0 || $ref) {
  94. $object->fetch($id, $ref);
  95. }
  96. $usercanread = (($object->type == Product::TYPE_PRODUCT && $user->rights->produit->lire) || ($object->type == Product::TYPE_SERVICE && $user->rights->service->lire));
  97. $usercancreate = (($object->type == Product::TYPE_PRODUCT && $user->rights->produit->creer) || ($object->type == Product::TYPE_SERVICE && $user->rights->service->creer));
  98. if ($object->id > 0) {
  99. if ($object->type == $object::TYPE_PRODUCT) {
  100. restrictedArea($user, 'produit', $object->id, 'product&product', '', '');
  101. }
  102. if ($object->type == $object::TYPE_SERVICE) {
  103. restrictedArea($user, 'service', $object->id, 'product&product', '', '');
  104. }
  105. } else {
  106. restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
  107. }
  108. /*
  109. * Actions
  110. */
  111. if ($cancel) {
  112. $action = '';
  113. }
  114. $parameters = array('socid'=>$socid, 'id_prod'=>$id);
  115. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  116. if ($reshook < 0) {
  117. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  118. }
  119. if (empty($reshook)) {
  120. if ($action == 'setcost_price') {
  121. if ($id) {
  122. $result = $object->fetch($id);
  123. $object->cost_price = price2num($cost_price);
  124. $result = $object->update($object->id, $user);
  125. if ($result > 0) {
  126. setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
  127. $action = '';
  128. } else {
  129. $error++;
  130. setEventMessages($object->error, $object->errors, 'errors');
  131. }
  132. }
  133. }
  134. if ($action == 'setpmp') {
  135. if ($id) {
  136. $result = $object->fetch($id);
  137. $object->pmp = price2num($pmp);
  138. $sql = "UPDATE ".MAIN_DB_PREFIX."product SET pmp = ".((float) $object->pmp)." WHERE rowid = ".((int) $id);
  139. $resql = $db->query($sql);
  140. //$result = $object->update($object->id, $user);
  141. if ($resql) {
  142. setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
  143. $action = '';
  144. } else {
  145. $error++;
  146. setEventMessages($object->error, $object->errors, 'errors');
  147. }
  148. }
  149. }
  150. if ($action == 'confirm_remove_pf') {
  151. if ($rowid) { // id of product supplier price to remove
  152. $action = '';
  153. $result = $object->remove_product_fournisseur_price($rowid);
  154. if ($result > 0) {
  155. $db->query("DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price_extrafields WHERE fk_object = ".((int) $rowid));
  156. setEventMessages($langs->trans("PriceRemoved"), null, 'mesgs');
  157. } else {
  158. $error++;
  159. setEventMessages($object->error, $object->errors, 'errors');
  160. }
  161. }
  162. }
  163. if ($action == 'save_price') {
  164. $id_fourn = GETPOST("id_fourn");
  165. if (empty($id_fourn)) {
  166. $id_fourn = GETPOST("search_id_fourn");
  167. }
  168. $ref_fourn = GETPOST("ref_fourn");
  169. if (empty($ref_fourn)) {
  170. $ref_fourn = GETPOST("search_ref_fourn");
  171. }
  172. $ref_fourn_old = GETPOST("ref_fourn_old");
  173. if (empty($ref_fourn_old)) {
  174. $ref_fourn_old = $ref_fourn;
  175. }
  176. $quantity = price2num(GETPOST("qty", 'alphanohtml'), 'MS');
  177. $remise_percent = price2num(GETPOST('remise_percent', 'alpha'));
  178. $npr = preg_match('/\*/', GETPOST('tva_tx', 'alpha')) ? 1 : 0;
  179. $tva_tx = str_replace('*', '', GETPOST('tva_tx', 'alpha'));
  180. if (!preg_match('/\((.*)\)/', $tva_tx)) {
  181. $tva_tx = price2num($tva_tx);
  182. }
  183. $price_expression = GETPOST('eid', 'int') ? GETPOST('eid', 'int') : ''; // Discard expression if not in expression mode
  184. $delivery_time_days = GETPOST('delivery_time_days', 'int') ? GETPOST('delivery_time_days', 'int') : '';
  185. $supplier_reputation = GETPOST('supplier_reputation');
  186. $supplier_description = GETPOST('supplier_description', 'restricthtml');
  187. $barcode = GETPOST('barcode', 'alpha');
  188. $fk_barcode_type = GETPOST('fk_barcode_type', 'int');
  189. $packaging = price2num(GETPOST("packaging", 'alphanohtml'), 'MS');
  190. if ($tva_tx == '') {
  191. $error++;
  192. $langs->load("errors");
  193. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("VATRateForSupplierProduct")), null, 'errors');
  194. }
  195. if (!is_numeric($tva_tx)) {
  196. $error++;
  197. $langs->load("errors");
  198. setEventMessages($langs->trans("ErrorFieldMustBeANumeric", $langs->transnoentities("VATRateForSupplierProduct")), null, 'errors');
  199. }
  200. if (empty($quantity)) {
  201. $error++;
  202. $langs->load("errors");
  203. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Qty")), null, 'errors');
  204. }
  205. if (empty($ref_fourn)) {
  206. $error++;
  207. $langs->load("errors");
  208. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("RefSupplier")), null, 'errors');
  209. }
  210. if ($id_fourn <= 0) {
  211. $error++;
  212. $langs->load("errors");
  213. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Supplier")), null, 'errors');
  214. }
  215. if (price2num(GETPOST("price")) < 0 || GETPOST("price") == '') {
  216. if ($price_expression === '') { // Return error of missing price only if price_expression not set
  217. $error++;
  218. $langs->load("errors");
  219. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Price")), null, 'errors');
  220. } else {
  221. $_POST["price"] = 0;
  222. }
  223. }
  224. if (isModEnabled("multicurrency")) {
  225. if (!GETPOST("multicurrency_code")) {
  226. $error++;
  227. $langs->load("errors");
  228. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Currency")), null, 'errors');
  229. }
  230. if (price2num(GETPOST("multicurrency_tx")) <= 0 || GETPOST("multicurrency_tx") == '') {
  231. $error++;
  232. $langs->load("errors");
  233. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("CurrencyRate")), null, 'errors');
  234. }
  235. if (price2num(GETPOST("multicurrency_price")) < 0 || GETPOST("multicurrency_price") == '') {
  236. $error++;
  237. $langs->load("errors");
  238. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PriceCurrency")), null, 'errors');
  239. }
  240. }
  241. if (!$error) {
  242. $db->begin();
  243. if (!$error) {
  244. $ret = $object->add_fournisseur($user, $id_fourn, $ref_fourn_old, $quantity); // This insert record with no value for price. Values are update later with update_buyprice
  245. if ($ret == -3) {
  246. $error++;
  247. $tmpobject = new Product($db);
  248. $tmpobject->fetch($object->product_id_already_linked);
  249. $productLink = $tmpobject->getNomUrl(1, 'supplier');
  250. $texttoshow = $langs->trans("ReferenceSupplierIsAlreadyAssociatedWithAProduct", '{s1}');
  251. $texttoshow = str_replace('{s1}', $productLink, $texttoshow);
  252. setEventMessages($texttoshow, null, 'errors');
  253. } elseif ($ret < 0) {
  254. $error++;
  255. setEventMessages($object->error, $object->errors, 'errors');
  256. }
  257. }
  258. if (!$error) {
  259. $supplier = new Fournisseur($db);
  260. $result = $supplier->fetch($id_fourn);
  261. if (GETPOSTISSET('ref_fourn_price_id')) {
  262. $object->fetch_product_fournisseur_price(GETPOST('ref_fourn_price_id', 'int'));
  263. }
  264. $extralabels = $extrafields->fetch_name_optionals_label("product_fournisseur_price");
  265. $extrafield_values = $extrafields->getOptionalsFromPost("product_fournisseur_price");
  266. $newprice = price2num(GETPOST("price", "alpha"));
  267. if (empty($packaging)) {
  268. $packaging = 1;
  269. }
  270. /* We can have a puchase ref that need to buy 100 min for a given price and with a packaging of 50.
  271. if ($packaging < $quantity) {
  272. $packaging = $quantity;
  273. }*/
  274. $object->packaging = $packaging;
  275. if (isModEnabled("multicurrency")) {
  276. $multicurrency_tx = price2num(GETPOST("multicurrency_tx", 'alpha'));
  277. $multicurrency_price = price2num(GETPOST("multicurrency_price", 'alpha'));
  278. $multicurrency_code = GETPOST("multicurrency_code", 'alpha');
  279. $ret = $object->update_buyprice($quantity, $newprice, $user, GETPOST("price_base_type"), $supplier, GETPOST("oselDispo"), $ref_fourn, $tva_tx, GETPOST("charges"), $remise_percent, 0, $npr, $delivery_time_days, $supplier_reputation, array(), '', $multicurrency_price, GETPOST("multicurrency_price_base_type"), $multicurrency_tx, $multicurrency_code, $supplier_description, $barcode, $fk_barcode_type, $extrafield_values);
  280. } else {
  281. $ret = $object->update_buyprice($quantity, $newprice, $user, GETPOST("price_base_type"), $supplier, GETPOST("oselDispo"), $ref_fourn, $tva_tx, GETPOST("charges"), $remise_percent, 0, $npr, $delivery_time_days, $supplier_reputation, array(), '', 0, 'HT', 1, '', $supplier_description, $barcode, $fk_barcode_type, $extrafield_values);
  282. }
  283. if ($ret < 0) {
  284. $error++;
  285. setEventMessages($object->error, $object->errors, 'errors');
  286. } else {
  287. if (isModEnabled('dynamicprices') && $price_expression !== '') {
  288. //Check the expression validity by parsing it
  289. require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
  290. $priceparser = new PriceParser($db);
  291. $object->fk_supplier_price_expression = $price_expression;
  292. $price_result = $priceparser->parseProductSupplier($object);
  293. if ($price_result < 0) { //Expression is not valid
  294. $error++;
  295. setEventMessages($priceparser->translatedError(), null, 'errors');
  296. }
  297. }
  298. if (!$error && isModEnabled('dynamicprices')) {
  299. //Set the price expression for this supplier price
  300. $ret = $object->setSupplierPriceExpression($price_expression);
  301. if ($ret < 0) {
  302. $error++;
  303. setEventMessages($object->error, $object->errors, 'errors');
  304. }
  305. }
  306. }
  307. }
  308. if (!$error) {
  309. $db->commit();
  310. $action = '';
  311. } else {
  312. $db->rollback();
  313. }
  314. } else {
  315. $action = 'add_price';
  316. }
  317. }
  318. }
  319. /*
  320. * view
  321. */
  322. $form = new Form($db);
  323. $title = $langs->trans('ProductServiceCard');
  324. $helpurl = '';
  325. $shortlabel = dol_trunc($object->label, 16);
  326. if (GETPOST("type") == '0' || ($object->type == Product::TYPE_PRODUCT)) {
  327. $title = $langs->trans('Product')." ".$shortlabel." - ".$langs->trans('BuyingPrices');
  328. $helpurl = 'EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos|DE:Modul_Produkte';
  329. }
  330. if (GETPOST("type") == '1' || ($object->type == Product::TYPE_SERVICE)) {
  331. $title = $langs->trans('Service')." ".$shortlabel." - ".$langs->trans('BuyingPrices');
  332. $helpurl = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios|DE:Modul_Lesitungen';
  333. }
  334. llxHeader('', $title, $helpurl, '', 0, 0, '', '', '', 'classforhorizontalscrolloftabs');
  335. if ($id > 0 || $ref) {
  336. if ($result) {
  337. if ($action == 'ask_remove_pf') {
  338. $form = new Form($db);
  339. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$id.'&rowid='.$rowid, $langs->trans('DeleteProductBuyPrice'), $langs->trans('ConfirmDeleteProductBuyPrice'), 'confirm_remove_pf', '', 0, 1);
  340. echo $formconfirm;
  341. }
  342. if ($action != 'edit' && $action != 're-edit') {
  343. $head = product_prepare_head($object);
  344. $titre = $langs->trans("CardProduct".$object->type);
  345. $picto = ($object->type == Product::TYPE_SERVICE ? 'service' : 'product');
  346. print dol_get_fiche_head($head, 'suppliers', $titre, -1, $picto);
  347. $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  348. $object->next_prev_filter = " fk_product_type = ".$object->type;
  349. $shownav = 1;
  350. if ($user->socid && !in_array('product', explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL))) {
  351. $shownav = 0;
  352. }
  353. dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
  354. print '<div class="fichecenter">';
  355. print '<div class="underbanner clearboth"></div>';
  356. print '<table class="border tableforfield centpercent">';
  357. // Type
  358. if (isModEnabled("product") && isModEnabled("service")) {
  359. $typeformat = 'select;0:'.$langs->trans("Product").',1:'.$langs->trans("Service");
  360. print '<tr><td class="">';
  361. print (empty($conf->global->PRODUCT_DENY_CHANGE_PRODUCT_TYPE)) ? $form->editfieldkey("Type", 'fk_product_type', $object->type, $object, 0, $typeformat) : $langs->trans('Type');
  362. print '</td><td>';
  363. print $form->editfieldval("Type", 'fk_product_type', $object->type, $object, 0, $typeformat);
  364. print '</td></tr>';
  365. }
  366. // Cost price. Can be used for margin module for option "calculate margin on explicit cost price
  367. print '<tr><td>';
  368. $textdesc = $langs->trans("CostPriceDescription");
  369. $textdesc .= "<br>".$langs->trans("CostPriceUsage");
  370. $text = $form->textwithpicto($langs->trans("CostPrice"), $textdesc, 1, 'help', '');
  371. print $form->editfieldkey($text, 'cost_price', $object->cost_price, $object, $usercancreate, 'amount:6');
  372. print '</td><td>';
  373. print $form->editfieldval($text, 'cost_price', $object->cost_price, $object, $usercancreate, 'amount:6');
  374. print '</td></tr>';
  375. // PMP
  376. $usercaneditpmp = 0;
  377. if (!empty($conf->global->PRODUCT_CAN_EDIT_WAP)) {
  378. $usercaneditpmp = $usercancreate;
  379. }
  380. print '<tr><td class="titlefieldcreate">';
  381. $textdesc = $langs->trans("AverageUnitPricePMPDesc");
  382. $text = $form->textwithpicto($langs->trans("AverageUnitPricePMPShort"), $textdesc, 1, 'help', '');
  383. print $form->editfieldkey($text, 'pmp', $object->pmp, $object, $usercaneditpmp, 'amount:6');
  384. print '</td><td>';
  385. print $form->editfieldval($text, 'pmp', ($object->pmp > 0 ? $object->pmp : ''), $object, $usercaneditpmp, 'amount:6');
  386. if ($object->pmp > 0) {
  387. print ' '.$langs->trans("HT");
  388. }
  389. /*
  390. .$form->textwithpicto($langs->trans("AverageUnitPricePMPShort"), $langs->trans("AverageUnitPricePMPDesc")).'</td>';
  391. print '<td>';
  392. if ($object->pmp > 0) {
  393. print price($object->pmp).' '.$langs->trans("HT");
  394. }*/
  395. print '</td>';
  396. print '</tr>';
  397. // Best buying Price
  398. print '<tr><td class="titlefieldcreate">'.$langs->trans("BuyingPriceMin").'</td>';
  399. print '<td>';
  400. $product_fourn = new ProductFournisseur($db);
  401. if ($product_fourn->find_min_price_product_fournisseur($object->id) > 0) {
  402. if ($product_fourn->product_fourn_price_id > 0) {
  403. print $product_fourn->display_price_product_fournisseur();
  404. } else {
  405. print $langs->trans("NotDefined");
  406. }
  407. }
  408. print '</td></tr>';
  409. print '</table>';
  410. print '</div>';
  411. print '<div style="clear:both"></div>';
  412. print dol_get_fiche_end();
  413. // Form to add or update a price
  414. if (($action == 'add_price' || $action == 'update_price') && $usercancreate) {
  415. $langs->load("suppliers");
  416. print "<!-- form to add a supplier price -->\n";
  417. print '<br>';
  418. if ($rowid) {
  419. $object->fetch_product_fournisseur_price($rowid, 1); //Ignore the math expression when getting the price
  420. print load_fiche_titre($langs->trans("ChangeSupplierPrice"));
  421. } else {
  422. print load_fiche_titre($langs->trans("AddSupplierPrice"));
  423. }
  424. print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'" method="POST">';
  425. print '<input type="hidden" name="token" value="'.newToken().'">';
  426. print '<input type="hidden" name="action" value="save_price">';
  427. print dol_get_fiche_head();
  428. print '<table class="border centpercent">';
  429. // Supplier
  430. print '<tr><td class="titlefield fieldrequired">'.$langs->trans("Supplier").'</td><td>';
  431. if ($rowid) {
  432. $supplier = new Fournisseur($db);
  433. $supplier->fetch($socid);
  434. print $supplier->getNomUrl(1);
  435. print '<input type="hidden" name="id_fourn" value="'.$socid.'">';
  436. print '<input type="hidden" name="ref_fourn_price_id" value="'.$rowid.'">';
  437. print '<input type="hidden" name="rowid" value="'.$rowid.'">';
  438. print '<input type="hidden" name="socid" value="'.$socid.'">';
  439. } else {
  440. $events = array();
  441. $events[] = array('method' => 'getVatRates', 'url' => dol_buildpath('/core/ajax/vatrates.php', 1), 'htmlname' => 'tva_tx', 'params' => array());
  442. print img_picto('', 'company', 'class="pictofixedwidth"').$form->select_company(GETPOST("id_fourn", 'alpha'), 'id_fourn', 'fournisseur=1', 'SelectThirdParty', 0, 0, $events);
  443. $parameters = array('filtre'=>"fournisseur=1", 'html_name'=>'id_fourn', 'selected'=>GETPOST("id_fourn"), 'showempty'=>1, 'prod_id'=>$object->id);
  444. $reshook = $hookmanager->executeHooks('formCreateThirdpartyOptions', $parameters, $object, $action);
  445. if (empty($reshook)) {
  446. if (empty($form->result)) {
  447. print '<a href="'.DOL_URL_ROOT.'/societe/card.php?action=create&type=f&backtopage='.urlencode($_SERVER["PHP_SELF"].'?id='.$object->id.'&action='.$action).'">';
  448. print img_picto($langs->trans("CreateDolibarrThirdPartySupplier"), 'add', 'class="marginleftonly"');
  449. print '</a>';
  450. }
  451. }
  452. print '<script type="text/javascript">
  453. $(document).ready(function () {
  454. $("#search_id_fourn").change(load_vat)
  455. console.log("Requesting default VAT rate for the supplier...")
  456. });
  457. function load_vat() {
  458. // get soc id
  459. let socid = $("#id_fourn")[0].value
  460. // load available VAT rates
  461. let vat_url = "'.dol_buildpath('/core/ajax/vatrates.php', 1).'"
  462. //Make GET request with params
  463. let options = "";
  464. options += "id=" + socid
  465. options += "&htmlname=tva_tx"
  466. options += "&action=default" // not defined in vatrates.php, default behavior.
  467. var get = $.getJSON(
  468. vat_url,
  469. options,
  470. (data) => {
  471. rate_options = $.parseHTML(data.value)
  472. rate_options.forEach(opt => {
  473. if (opt.selected) {
  474. replaceVATWithSupplierValue(opt.value)
  475. return
  476. }
  477. })
  478. }
  479. );
  480. }
  481. function replaceVATWithSupplierValue(vat_rate) {
  482. console.log("Default VAT rate for the supplier: " + vat_rate + "%")
  483. $("[name=\'tva_tx\']")[0].value = vat_rate;
  484. }
  485. </script>';
  486. }
  487. print '</td></tr>';
  488. // Ref supplier
  489. print '<tr><td class="fieldrequired">'.$langs->trans("SupplierRef").'</td><td>';
  490. if ($rowid) {
  491. print '<input type="hidden" name="ref_fourn_old" value="'.$object->ref_supplier.'">';
  492. print '<input class="flat width150" maxlength="128" name="ref_fourn" value="'.$object->ref_supplier.'">';
  493. } else {
  494. print '<input class="flat width150" maxlength="128" name="ref_fourn" value="'.(GETPOST("ref_fourn") ? GETPOST("ref_fourn") : '').'">';
  495. }
  496. print '</td>';
  497. print '</tr>';
  498. // Availability
  499. if (getDolGlobalInt('FOURN_PRODUCT_AVAILABILITY')) {
  500. $langs->load("propal");
  501. print '<tr><td>'.$langs->trans("Availability").'</td><td>';
  502. $form->selectAvailabilityDelay($object->fk_availability, "oselDispo", 1);
  503. print '</td></tr>'."\n";
  504. }
  505. // Qty min
  506. print '<tr>';
  507. print '<td class="fieldrequired">'.$langs->trans("QtyMin").'</td>';
  508. print '<td>';
  509. $quantity = GETPOSTISSET('qty') ? price2num(GETPOST('qty', 'alphanohtml'), 'MS') : "1";
  510. if ($rowid) {
  511. print '<input type="hidden" name="qty" value="'.$object->fourn_qty.'">';
  512. print $object->fourn_qty;
  513. } else {
  514. print '<input class="flat" name="qty" size="5" value="'.$quantity.'">';
  515. }
  516. // Units
  517. if (!empty($conf->global->PRODUCT_USE_UNITS)) {
  518. $unit = $object->getLabelOfUnit();
  519. if ($unit !== '') {
  520. print '&nbsp;&nbsp;'.$langs->trans($unit);
  521. }
  522. }
  523. print '</td></tr>';
  524. if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
  525. // Packaging/Conditionnement
  526. print '<tr>';
  527. print '<td class="fieldrequired">'.$form->textwithpicto($langs->trans("PackagingForThisProduct"), $langs->trans("PackagingForThisProductDesc")).'</td>';
  528. print '<td>';
  529. $packaging = GETPOSTISSET('packaging') ? price2num(GETPOST('packaging', 'alphanohtml'), 'MS') : ((empty($rowid)) ? "1" : price2num($object->packaging, 'MS'));
  530. print '<input class="flat" name="packaging" size="5" value="'.$packaging.'">';
  531. // Units
  532. if (!empty($conf->global->PRODUCT_USE_UNITS)) {
  533. $unit = $object->getLabelOfUnit();
  534. if ($unit !== '') {
  535. print '&nbsp;&nbsp;'.$langs->trans($unit);
  536. }
  537. }
  538. }
  539. // Vat rate
  540. $default_vat = '';
  541. // We don't have supplier, so we try to guess.
  542. // For this we build a fictive supplier with same properties than user but using vat)
  543. $mysoc2 = clone $mysoc;
  544. $mysoc2->name = 'Fictive seller with same country';
  545. $mysoc2->tva_assuj = 1;
  546. $default_vat = get_default_tva($mysoc2, $mysoc, $object->id, 0);
  547. $default_npr = get_default_npr($mysoc2, $mysoc, $object->id, 0);
  548. if (empty($default_vat)) {
  549. $default_npr = $default_vat;
  550. }
  551. print '<tr><td class="fieldrequired">'.$langs->trans("VATRateForSupplierProduct").'</td>';
  552. print '<td>';
  553. //print $form->load_tva('tva_tx',$object->tva_tx,$supplier,$mysoc); // Do not use list here as it may be any vat rates for any country
  554. if (!empty($rowid)) { // If we have a supplier, it is an update, we must show the vat of current supplier price
  555. $tmpproductsupplier = new ProductFournisseur($db);
  556. $tmpproductsupplier->fetch_product_fournisseur_price($rowid, 1);
  557. $default_vat = $tmpproductsupplier->fourn_tva_tx;
  558. $default_npr = $tmpproductsupplier->fourn_tva_npr;
  559. } else {
  560. if (empty($default_vat)) {
  561. $default_vat = $object->tva_tx;
  562. }
  563. }
  564. $vattosuggest = (GETPOSTISSET("tva_tx") ? vatrate(GETPOST("tva_tx")) : ($default_vat != '' ?vatrate($default_vat) : ''));
  565. $vattosuggest = preg_replace('/\s*\(.*\)$/', '', $vattosuggest);
  566. print '<input type="text" class="flat" size="5" name="tva_tx" value="'.$vattosuggest.'">';
  567. print '</td></tr>';
  568. if (isModEnabled('dynamicprices')) { //Only show price mode and expression selector if module is enabled
  569. // Price mode selector
  570. print '<tr><td class="fieldrequired">'.$langs->trans("PriceMode").'</td><td>';
  571. $price_expression = new PriceExpression($db);
  572. $price_expression_list = array(0 => $langs->trans("PriceNumeric")); //Put the numeric mode as first option
  573. foreach ($price_expression->list_price_expression() as $entry) {
  574. $price_expression_list[$entry->id] = $entry->title;
  575. }
  576. $price_expression_preselection = GETPOST('eid') ? GETPOST('eid') : ($object->fk_supplier_price_expression ? $object->fk_supplier_price_expression : '0');
  577. print $form->selectarray('eid', $price_expression_list, $price_expression_preselection);
  578. print '&nbsp; <div id="expression_editor" class="button">'.$langs->trans("PriceExpressionEditor").'</div>';
  579. print '</td></tr>';
  580. // This code hides the numeric price input if is not selected, loads the editor page if editor button is pressed
  581. print '<script type="text/javascript">
  582. jQuery(document).ready(run);
  583. function run() {
  584. jQuery("#expression_editor").click(on_click);
  585. jQuery("#eid").change(on_change);
  586. on_change();
  587. }
  588. function on_click() {
  589. window.location = "'.DOL_URL_ROOT.'/product/dynamic_price/editor.php?id='.$id.'&tab=fournisseurs&eid=" + $("#eid").val();
  590. }
  591. function on_change() {
  592. if ($("#eid").val() == 0) {
  593. jQuery("#price_numeric").show();
  594. } else {
  595. jQuery("#price_numeric").hide();
  596. }
  597. }
  598. </script>';
  599. }
  600. if (isModEnabled("multicurrency")) {
  601. // Currency
  602. print '<tr><td class="fieldrequired">'.$langs->trans("Currency").'</td>';
  603. print '<td>';
  604. $currencycodetouse = GETPOST('multicurrency_code') ? GETPOST('multicurrency_code') : (isset($object->fourn_multicurrency_code) ? $object->fourn_multicurrency_code : '');
  605. if (empty($currencycodetouse) && $object->fourn_multicurrency_tx == 1) {
  606. $currencycodetouse = $conf->currency;
  607. }
  608. print $form->selectMultiCurrency($currencycodetouse, "multicurrency_code", 1);
  609. print ' &nbsp; &nbsp; '.$langs->trans("CurrencyRate").' ';
  610. print '<input class="flat" name="multicurrency_tx" size="4" value="'.vatrate(GETPOST('multicurrency_tx') ? GETPOST('multicurrency_tx') : (isset($object->fourn_multicurrency_tx) ? $object->fourn_multicurrency_tx : '')).'">';
  611. print '</td>';
  612. print '</tr>';
  613. // Currency price qty min
  614. print '<tr><td class="fieldrequired">'.$form->textwithpicto($langs->trans("PriceQtyMinCurrency"), $langs->transnoentitiesnoconv("WithoutDiscount")).'</td>';
  615. $pricesupplierincurrencytouse = (GETPOST('multicurrency_price') ? GETPOST('multicurrency_price') : (isset($object->fourn_multicurrency_price) ? $object->fourn_multicurrency_price : ''));
  616. print '<td><input class="flat" name="multicurrency_price" size="8" value="'.price($pricesupplierincurrencytouse).'">';
  617. print '&nbsp;';
  618. print $form->selectPriceBaseType((GETPOST('multicurrency_price_base_type') ?GETPOST('multicurrency_price_base_type') : 'HT'), "multicurrency_price_base_type"); // We keep 'HT' here, multicurrency_price_base_type is not yet supported for supplier prices
  619. print '</td></tr>';
  620. // Price qty min
  621. print '<tr><td class="fieldrequired">'.$form->textwithpicto($langs->trans("PriceQtyMin"), $langs->transnoentitiesnoconv("WithoutDiscount")).'</td>';
  622. print '<td><input class="flat" name="disabled_price" size="8" value="">';
  623. print '<input type="hidden" name="price" value="">';
  624. print '<input type="hidden" name="price_base_type" value="">';
  625. print '&nbsp;';
  626. print $form->selectPriceBaseType('', "disabled_price_base_type");
  627. print '</td></tr>';
  628. $currencies = array();
  629. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."multicurrency WHERE entity = ".((int) $conf->entity);
  630. $resql = $db->query($sql);
  631. if ($resql) {
  632. $currency = new MultiCurrency($db);
  633. while ($obj = $db->fetch_object($resql)) {
  634. $currency->fetch($obj->rowid);
  635. $currencies[$currency->code] = ((float) $currency->rate->rate);
  636. }
  637. }
  638. $currencies = json_encode($currencies);
  639. print <<<END
  640. <!-- javascript to autocalculate the minimum price -->
  641. <script type="text/javascript">
  642. function update_price_from_multicurrency() {
  643. console.log("update_price_from_multicurrency");
  644. var multicurrency_price = price2numjs($('input[name="multicurrency_price"]').val());
  645. var multicurrency_tx = price2numjs($('input[name="multicurrency_tx"]').val());
  646. if (multicurrency_tx != 0) {
  647. $('input[name="price"]').val(multicurrency_price / multicurrency_tx);
  648. $('input[name="disabled_price"]').val(multicurrency_price / multicurrency_tx);
  649. } else {
  650. $('input[name="price"]').val('');
  651. $('input[name="disabled_price"]').val('');
  652. }
  653. }
  654. jQuery(document).ready(function () {
  655. $('input[name="disabled_price"]').prop('disabled', true);
  656. $('select[name="disabled_price_base_type"]').prop('disabled', true);
  657. update_price_from_multicurrency();
  658. $('input[name="multicurrency_price"], input[name="multicurrency_tx"]').keyup(function () {
  659. update_price_from_multicurrency();
  660. });
  661. $('input[name="multicurrency_price"], input[name="multicurrency_tx"]').change(function () {
  662. update_price_from_multicurrency();
  663. });
  664. $('input[name="multicurrency_price"], input[name="multicurrency_tx"]').on('paste', function () {
  665. update_price_from_multicurrency();
  666. });
  667. $('select[name="multicurrency_price_base_type"]').change(function () {
  668. $('input[name="price_base_type"]').val($(this).val());
  669. $('select[name="disabled_price_base_type"]').val($(this).val());
  670. });
  671. var currencies_array = $currencies;
  672. $('select[name="multicurrency_code"]').change(function () {
  673. console.log("We change the currency");
  674. $('input[name="multicurrency_tx"]').val(currencies_array[$(this).val()]);
  675. update_price_from_multicurrency();
  676. });
  677. });
  678. </script>
  679. END;
  680. } else {
  681. // Price qty min
  682. print '<tr><td class="fieldrequired">'.$langs->trans("PriceQtyMin").'</td>';
  683. print '<td><input class="flat" name="price" size="8" value="'.(GETPOST('price') ? price(GETPOST('price')) : (isset($object->fourn_price) ? price($object->fourn_price) : '')).'">';
  684. print '&nbsp;';
  685. print $form->selectPriceBaseType((GETPOSTISSET('price_base_type') ? GETPOST('price_base_type') : 'HT'), "price_base_type"); // We keep 'HT' here, price_base_type is not yet supported for supplier prices
  686. print '</td></tr>';
  687. }
  688. // Option to define a transport cost on supplier price
  689. if (!empty($conf->global->PRODUCT_CHARGES)) {
  690. print '<tr>';
  691. print '<td>'.$langs->trans("Charges").'</td>';
  692. print '<td><input class="flat" name="charges" size="8" value="'.(GETPOST('charges') ? price(GETPOST('charges')) : (isset($object->fourn_charges) ? price($object->fourn_charges) : '')).'">';
  693. print '</td>';
  694. print '</tr>';
  695. }
  696. // Discount qty min
  697. print '<tr><td>'.$langs->trans("DiscountQtyMin").'</td>';
  698. print '<td><input class="flat" name="remise_percent" size="4" value="'.(GETPOSTISSET('remise_percent') ? vatrate(price2num(GETPOST('remise_percent'), '', 2)) : (isset($object->fourn_remise_percent) ?vatrate($object->fourn_remise_percent) : '')).'"> %';
  699. print '</td>';
  700. print '</tr>';
  701. // Delivery delay in days
  702. print '<tr>';
  703. print '<td>'.$langs->trans('NbDaysToDelivery').'</td>';
  704. print '<td><input class="flat" name="delivery_time_days" size="4" value="'.($rowid ? $object->delivery_time_days : '').'">&nbsp;'.$langs->trans('days').'</td>';
  705. print '</tr>';
  706. // Reputation
  707. print '<tr><td>'.$langs->trans("ReferenceReputation").'</td><td>';
  708. echo $form->selectarray('supplier_reputation', $object->reputations, !empty($supplier_reputation) ? $supplier_reputation : $object->supplier_reputation);
  709. print '</td></tr>';
  710. // Barcode
  711. if (isModEnabled('barcode')) {
  712. $formbarcode = new FormBarCode($db);
  713. // Barcode type
  714. print '<tr>';
  715. print '<td>'.$langs->trans('GencodBuyPrice').'</td>';
  716. print '<td>';
  717. print img_picto('', 'barcode', 'class="pictofixedwidth"');
  718. print $formbarcode->selectBarcodeType((GETPOSTISSET('fk_barcode_type') ? GETPOST('fk_barcode_type', 'int') : ($rowid ? $object->supplier_fk_barcode_type : getDolGlobalint("PRODUIT_DEFAULT_BARCODE_TYPE"))), 'fk_barcode_type', 1);
  719. print ' <input class="flat" name="barcode" value="'.(GETPOSTISSET('barcode') ? GETPOST('barcode') : ($rowid ? $object->supplier_barcode : '')).'"></td>';
  720. print '</tr>';
  721. }
  722. // Product description of the supplier
  723. if (!empty($conf->global->PRODUIT_FOURN_TEXTS)) {
  724. //WYSIWYG Editor
  725. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  726. print '<tr>';
  727. print '<td>'.$langs->trans('ProductSupplierDescription').'</td>';
  728. print '<td>';
  729. $doleditor = new DolEditor('supplier_description', $object->desc_supplier, '', 160, 'dolibarr_details', '', false, true, getDolGlobalInt('FCKEDITOR_ENABLE_DETAILS'), ROWS_4, '90%');
  730. $doleditor->Create();
  731. print '</td>';
  732. print '</tr>';
  733. }
  734. // Extrafields
  735. $extrafields->fetch_name_optionals_label("product_fournisseur_price");
  736. $extralabels = !empty($extrafields->attributes["product_fournisseur_price"]['label']) ? $extrafields->attributes["product_fournisseur_price"]['label'] : '';
  737. $extrafield_values = $extrafields->getOptionalsFromPost("product_fournisseur_price");
  738. if (!empty($extralabels)) {
  739. if (empty($rowid)) {
  740. foreach ($extralabels as $key => $value) {
  741. if (!empty($extrafields->attributes["product_fournisseur_price"]['list'][$key]) && ($extrafields->attributes["product_fournisseur_price"]['list'][$key] == 1 || $extrafields->attributes["product_fournisseur_price"]['list'][$key] == 3 || ($action == "update_price" && $extrafields->attributes["product_fournisseur_price"]['list'][$key] == 4))) {
  742. if (!empty($extrafields->attributes["product_fournisseur_price"]['langfile'][$key])) {
  743. $langs->load($extrafields->attributes["product_fournisseur_price"]['langfile'][$key]);
  744. }
  745. print '<tr><td'.($extrafields->attributes["product_fournisseur_price"]['required'][$key] ? ' class="fieldrequired"' : '').'>';
  746. if (!empty($extrafields->attributes["product_fournisseur_price"]['help'][$key])) {
  747. print $form->textwithpicto($langs->trans($value), $langs->trans($extrafields->attributes["product_fournisseur_price"]['help'][$key]));
  748. } else {
  749. print $langs->trans($value);
  750. }
  751. print '</td><td>'.$extrafields->showInputField($key, GETPOSTISSET('options_'.$key) ? $extrafield_values['options_'.$key] : '', '', '', '', '', 0, 'product_fournisseur_price').'</td></tr>';
  752. }
  753. }
  754. } else {
  755. $sql = "SELECT";
  756. $sql .= " fk_object";
  757. foreach ($extralabels as $key => $value) {
  758. $sql .= ", ".$key;
  759. }
  760. $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price_extrafields";
  761. $sql .= " WHERE fk_object = ".((int) $rowid);
  762. $resql = $db->query($sql);
  763. if ($resql) {
  764. $obj = $db->fetch_object($resql);
  765. foreach ($extralabels as $key => $value) {
  766. if (!empty($extrafields->attributes["product_fournisseur_price"]['list'][$key]) && ($extrafields->attributes["product_fournisseur_price"]['list'][$key] == 1 || $extrafields->attributes["product_fournisseur_price"]['list'][$key] == 3 || ($action == "update_price" && $extrafields->attributes["product_fournisseur_price"]['list'][$key] == 4))) {
  767. if (!empty($extrafields->attributes["product_fournisseur_price"]['langfile'][$key])) {
  768. $langs->load($extrafields->attributes["product_fournisseur_price"]['langfile'][$key]);
  769. }
  770. print '<tr><td'.($extrafields->attributes["product_fournisseur_price"]['required'][$key] ? ' class="fieldrequired"' : '').'>';
  771. if (!empty($extrafields->attributes["product_fournisseur_price"]['help'][$key])) {
  772. print $form->textwithpicto($langs->trans($value), $langs->trans($extrafields->attributes["product_fournisseur_price"]['help'][$key]));
  773. } else {
  774. print $langs->trans($value);
  775. }
  776. print '</td><td>'.$extrafields->showInputField($key, GETPOSTISSET('options_'.$key) ? $extrafield_values['options_'.$key] : $obj->{$key}, '', '', '', '', 0, 'product_fournisseur_price');
  777. print '</td></tr>';
  778. }
  779. }
  780. $db->free($resql);
  781. }
  782. }
  783. }
  784. if (is_object($hookmanager)) {
  785. $parameters = array('id_fourn'=>!empty($id_fourn) ? $id_fourn : 0, 'prod_id'=>$object->id);
  786. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action);
  787. print $hookmanager->resPrint;
  788. }
  789. print '</table>';
  790. print dol_get_fiche_end();
  791. print '<div class="center">';
  792. print '<input class="button button-save" type="submit" value="'.$langs->trans("Save").'">';
  793. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  794. print '<input class="button button-cancel" type="submit" name="cancel" value="'.$langs->trans("Cancel").'">';
  795. print '</div>';
  796. print '</form>'."\n";
  797. }
  798. // Actions buttons
  799. print '<div class="tabsAction">'."\n";
  800. if ($action != 'add_price' && $action != 'update_price') {
  801. $parameters = array();
  802. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  803. if (empty($reshook)) {
  804. if ($usercancreate) {
  805. print '<a class="butAction" href="'.DOL_URL_ROOT.'/product/fournisseurs.php?id='.$object->id.'&action=add_price&token='.newToken().'">';
  806. print $langs->trans("AddSupplierPrice").'</a>';
  807. }
  808. }
  809. }
  810. print "</div>\n";
  811. if ($user->hasRight("fournisseur", "read")) { // Duplicate ? this check is already in the head of this file
  812. $param = '';
  813. if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
  814. $param .= '&contextpage='.urlencode($contextpage);
  815. }
  816. if ($limit > 0 && $limit != $conf->liste_limit) {
  817. $param .= '&limit='.urlencode($limit);
  818. }
  819. $param .= '&ref='.urlencode($object->ref);
  820. $product_fourn = new ProductFournisseur($db);
  821. $product_fourn_list = $product_fourn->list_product_fournisseur_price($object->id, $sortfield, $sortorder, $limit, $offset);
  822. $product_fourn_list_all = $product_fourn->list_product_fournisseur_price($object->id, $sortfield, $sortorder, 0, 0);
  823. $nbtotalofrecords = count($product_fourn_list_all);
  824. $num = count($product_fourn_list);
  825. if (($num + ($offset * $limit)) < $nbtotalofrecords) {
  826. $num++;
  827. }
  828. print_barre_liste($langs->trans('SupplierPrices'), $page, $_SERVER['PHP_SELF'], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'title_accountancy.png', 0, '', '', $limit, 1);
  829. // Definition of fields for lists
  830. // Some fields are missing because they are not included in the database query
  831. $arrayfields = array(
  832. 'pfp.datec'=>array('label'=>$langs->trans("AppliedPricesFrom"), 'checked'=>1, 'position'=>1),
  833. 's.nom'=>array('label'=>$langs->trans("Suppliers"), 'checked'=>1, 'position'=>2),
  834. 'pfp.fk_availability'=>array('label'=>$langs->trans("Availability"), 'enabled' => getDolGlobalInt('FOURN_PRODUCT_AVAILABILITY'), 'checked'=>0, 'position'=>4),
  835. 'pfp.quantity'=>array('label'=>$langs->trans("QtyMin"), 'checked'=>1, 'position'=>5),
  836. 'pfp.unitprice'=>array('label'=>$langs->trans("UnitPriceHT"), 'checked'=>1, 'position'=>9),
  837. 'pfp.multicurrency_unitprice'=>array('label'=>$langs->trans("UnitPriceHTCurrency"), 'enabled' => isModEnabled('multicurrency'), 'checked'=>0, 'position'=>10),
  838. 'pfp.charges'=>array('label'=>$langs->trans("Charges"), 'enabled' => !empty($conf->global->PRODUCT_CHARGES), 'checked'=>0, 'position'=>11),
  839. 'pfp.delivery_time_days'=>array('label'=>$langs->trans("NbDaysToDelivery"), 'checked'=>-1, 'position'=>13),
  840. 'pfp.supplier_reputation'=>array('label'=>$langs->trans("ReputationForThisProduct"), 'checked'=>-1, 'position'=>14),
  841. 'pfp.fk_barcode_type'=>array('label'=>$langs->trans("BarcodeType"), 'enabled' => isModEnabled('barcode'), 'checked'=>0, 'position'=>15),
  842. 'pfp.barcode'=>array('label'=>$langs->trans("BarcodeValue"), 'enabled' => isModEnabled('barcode'), 'checked'=>0, 'position'=>16),
  843. 'pfp.packaging'=>array('label'=>$langs->trans("PackagingForThisProduct"), 'enabled' => getDolGlobalInt('PRODUCT_USE_SUPPLIER_PACKAGING'), 'checked'=>0, 'position'=>17),
  844. 'pfp.tms'=>array('label'=>$langs->trans("DateModification"), 'enabled' => isModEnabled('barcode'), 'checked'=>1, 'position'=>18),
  845. );
  846. // fetch optionals attributes and labels
  847. $extrafields->fetch_name_optionals_label("product_fournisseur_price");
  848. if ($extrafields->attributes["product_fournisseur_price"] && array_key_exists('label', $extrafields->attributes["product_fournisseur_price"])) {
  849. $extralabels = $extrafields->attributes["product_fournisseur_price"]['label'];
  850. if (!empty($extralabels)) {
  851. foreach ($extralabels as $key => $value) {
  852. // Show field if not hidden
  853. if (!empty($extrafields->attributes["product_fournisseur_price"]['list'][$key]) && $extrafields->attributes["product_fournisseur_price"]['list'][$key] != 3) {
  854. $extratitle = $langs->trans($value);
  855. $arrayfields['ef.' . $key] = array('label' => $extratitle, 'checked' => 0,
  856. 'position' => (end($arrayfields)['position'] + 1),
  857. 'langfile' => $extrafields->attributes["product_fournisseur_price"]['langfile'][$key],
  858. 'help' => $extrafields->attributes["product_fournisseur_price"]['help'][$key]);
  859. }
  860. }
  861. }
  862. }
  863. // Selection of new fields
  864. include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
  865. $varpage = empty($contextpage) ? $_SERVER["PHP_SELF"] : $contextpage;
  866. $selectedfields = $form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
  867. print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="post" name="formulaire">';
  868. print '<input type="hidden" name="token" value="'.newToken().'">';
  869. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  870. print '<input type="hidden" name="action" value="list">';
  871. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  872. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  873. // Suppliers list title
  874. print '<div class="div-table-responsive">';
  875. print '<table class="liste centpercent">';
  876. $param = "&id=".$object->id;
  877. $nbfields = 0;
  878. print '<tr class="liste_titre">';
  879. if (!empty($arrayfields['pfp.datec']['checked'])) {
  880. print_liste_field_titre("AppliedPricesFrom", $_SERVER["PHP_SELF"], "pfp.datec", "", $param, "", $sortfield, $sortorder, '', '', 1);
  881. $nbfields++;
  882. }
  883. if (!empty($arrayfields['s.nom']['checked'])) {
  884. print_liste_field_titre("Suppliers", $_SERVER["PHP_SELF"], "s.nom", "", $param, "", $sortfield, $sortorder, '', '', 1);
  885. $nbfields++;
  886. }
  887. print_liste_field_titre("SupplierRef", $_SERVER["PHP_SELF"], "", "", $param, "", $sortfield, $sortorder, '', '', 1);
  888. $nbfields++;
  889. if (!empty($arrayfields['pfp.fk_availability']['checked'])) {
  890. print_liste_field_titre("Availability", $_SERVER["PHP_SELF"], "pfp.fk_availability", "", $param, "", $sortfield, $sortorder);
  891. $nbfields++;
  892. }
  893. if (!empty($arrayfields['pfp.quantity']['checked'])) {
  894. print_liste_field_titre("QtyMin", $_SERVER["PHP_SELF"], "pfp.quantity", "", $param, '', $sortfield, $sortorder, 'right ');
  895. $nbfields++;
  896. }
  897. print_liste_field_titre("VATRate", $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'right ');
  898. $nbfields++;
  899. print_liste_field_titre("PriceQtyMinHT", $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'right ');
  900. $nbfields++;
  901. if (isModEnabled("multicurrency")) {
  902. print_liste_field_titre("PriceQtyMinHTCurrency", $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'right ');
  903. $nbfields++;
  904. }
  905. if (!empty($arrayfields['pfp.unitprice']['checked'])) {
  906. print_liste_field_titre("UnitPriceHT", $_SERVER["PHP_SELF"], "pfp.unitprice", "", $param, '', $sortfield, $sortorder, 'right ');
  907. $nbfields++;
  908. }
  909. if (!empty($arrayfields['pfp.multicurrency_unitprice']['checked'])) {
  910. print_liste_field_titre("UnitPriceHTCurrency", $_SERVER["PHP_SELF"], "pfp.multicurrency_unitprice", "", $param, '', $sortfield, $sortorder, 'right ');
  911. $nbfields++;
  912. }
  913. if (isModEnabled("multicurrency")) {
  914. print_liste_field_titre("Currency", $_SERVER["PHP_SELF"], "", "", $param, '', $sortfield, $sortorder, 'right ');
  915. $nbfields++;
  916. }
  917. if (!empty($arrayfields['pfp.charges']['checked'])) {
  918. print_liste_field_titre("Charges", $_SERVER["PHP_SELF"], "pfp.charges", "", $param, '', $sortfield, $sortorder, 'right ');
  919. $nbfields++;
  920. }
  921. print_liste_field_titre("DiscountQtyMin", $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'right ');
  922. $nbfields++;
  923. if (!empty($arrayfields['pfp.delivery_time_days']['checked'])) {
  924. print_liste_field_titre("NbDaysToDelivery", $_SERVER["PHP_SELF"], "pfp.delivery_time_days", "", $param, '', $sortfield, $sortorder, 'right ');
  925. $nbfields++;
  926. }
  927. if (!empty($arrayfields['pfp.supplier_reputation']['checked'])) {
  928. print_liste_field_titre("ReputationForThisProduct", $_SERVER["PHP_SELF"], "pfp.supplier_reputation", "", $param, '', $sortfield, $sortorder, 'center ');
  929. $nbfields++;
  930. }
  931. if (!empty($arrayfields['pfp.fk_barcode_type']['checked'])) {
  932. print_liste_field_titre("BarcodeType", $_SERVER["PHP_SELF"], "pfp.fk_barcode_type", "", $param, '', $sortfield, $sortorder, 'center ');
  933. $nbfields++;
  934. }
  935. if (!empty($arrayfields['pfp.barcode']['checked'])) {
  936. print_liste_field_titre("BarcodeValue", $_SERVER["PHP_SELF"], "pfp.barcode", "", $param, '', $sortfield, $sortorder, 'center ');
  937. $nbfields++;
  938. }
  939. if (!empty($arrayfields['pfp.packaging']['checked'])) {
  940. print_liste_field_titre("PackagingForThisProduct", $_SERVER["PHP_SELF"], "pfp.packaging", "", $param, 'align="center"', $sortfield, $sortorder);
  941. $nbfields++;
  942. }
  943. if (!empty($arrayfields['pfp.tms']['checked'])) {
  944. print_liste_field_titre("DateModification", $_SERVER["PHP_SELF"], "pfp.tms", "", $param, '', $sortfield, $sortorder, 'right ', '', 1);
  945. $nbfields++;
  946. }
  947. // fetch optionals attributes and labels
  948. $extrafields->fetch_name_optionals_label("product_fournisseur_price");
  949. if ($extrafields->attributes["product_fournisseur_price"] && array_key_exists('label', $extrafields->attributes["product_fournisseur_price"])) {
  950. $extralabels = $extrafields->attributes["product_fournisseur_price"]['label'];
  951. if (!empty($extralabels)) {
  952. foreach ($extralabels as $key => $value) {
  953. // Show field if not hidden
  954. if (!empty($extrafields->attributes["product_fournisseur_price"]['list'][$key]) && $extrafields->attributes["product_fournisseur_price"]['list'][$key] != 3) {
  955. if (!empty($extrafields->attributes["product_fournisseur_price"]['langfile'][$key])) {
  956. $langs->load($extrafields->attributes["product_fournisseur_price"]['langfile'][$key]);
  957. }
  958. if (!empty($extrafields->attributes["product_fournisseur_price"]['help'][$key])) {
  959. $extratitle = $form->textwithpicto($langs->trans($value), $langs->trans($extrafields->attributes["product_fournisseur_price"]['help'][$key]));
  960. } else {
  961. $extratitle = $langs->trans($value);
  962. }
  963. if (!empty($arrayfields['ef.' . $key]['checked'])) {
  964. print_liste_field_titre($extratitle, $_SERVER["PHP_SELF"], 'ef.' . $key, '', $param, '', $sortfield, $sortorder, 'right ');
  965. $nbfields++;
  966. }
  967. }
  968. }
  969. }
  970. }
  971. if (is_object($hookmanager)) {
  972. $parameters = array('id_fourn'=>(!empty($id_fourn)?$id_fourn:''), 'prod_id'=>$object->id, 'nbfields'=>$nbfields);
  973. $reshook = $hookmanager->executeHooks('printFieldListTitle', $parameters, $object, $action);
  974. }
  975. print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'center maxwidthsearch ');
  976. $nbfields++;
  977. print "</tr>\n";
  978. if (is_array($product_fourn_list)) {
  979. foreach ($product_fourn_list as $productfourn) {
  980. print '<tr class="oddeven">';
  981. // Date from
  982. if (!empty($arrayfields['pfp.datec']['checked'])) {
  983. print '<td>'.dol_print_date(($productfourn->fourn_date_creation ? $productfourn->fourn_date_creation : $productfourn->date_creation), 'dayhour', 'tzuserrel').'</td>';
  984. }
  985. // Supplier
  986. if (!empty($arrayfields['s.nom']['checked'])) {
  987. print '<td class="tdoverflowmax150">'.$productfourn->getSocNomUrl(1, 'supplier').'</td>';
  988. }
  989. // Supplier ref
  990. if ($usercancreate) { // change required right here
  991. print '<td class="tdoverflowmax150">'.$productfourn->getNomUrl().'</td>';
  992. } else {
  993. print '<td class="tdoverflowmax150">'.dol_escape_htmltag($productfourn->fourn_ref).'</td>';
  994. }
  995. // Availability
  996. if (!empty($arrayfields['pfp.fk_availability']['checked'])) {
  997. $form->load_cache_availability();
  998. $availability = $form->cache_availability[$productfourn->fk_availability]['label'];
  999. print '<td class="left">'.$availability.'</td>';
  1000. }
  1001. // Quantity
  1002. if (!empty($arrayfields['pfp.quantity']['checked'])) {
  1003. print '<td class="right">';
  1004. print $productfourn->fourn_qty;
  1005. // Units
  1006. if (!empty($conf->global->PRODUCT_USE_UNITS)) {
  1007. $unit = $object->getLabelOfUnit();
  1008. if ($unit !== '') {
  1009. print '&nbsp;&nbsp;'.$langs->trans($unit);
  1010. }
  1011. }
  1012. print '</td>';
  1013. }
  1014. // VAT rate
  1015. print '<td class="right">';
  1016. print vatrate($productfourn->fourn_tva_tx, true);
  1017. print '</td>';
  1018. // Price for the quantity
  1019. print '<td class="right">';
  1020. print $productfourn->fourn_price ? '<span class="amount">'.price($productfourn->fourn_price).'</span>' : "";
  1021. print '</td>';
  1022. if (isModEnabled("multicurrency")) {
  1023. // Price for the quantity in currency
  1024. print '<td class="right">';
  1025. print $productfourn->fourn_multicurrency_price ? '<span class="amount">'.price($productfourn->fourn_multicurrency_price).'</span>' : "";
  1026. print '</td>';
  1027. }
  1028. // Unit price
  1029. if (!empty($arrayfields['pfp.unitprice']['checked'])) {
  1030. print '<td class="right">';
  1031. print price($productfourn->fourn_unitprice);
  1032. //print $objp->unitprice? price($objp->unitprice) : ($objp->quantity?price($objp->price/$objp->quantity):"&nbsp;");
  1033. print '</td>';
  1034. }
  1035. // Unit price in currency
  1036. if (!empty($arrayfields['pfp.multicurrency_unitprice']['checked'])) {
  1037. print '<td class="right">';
  1038. print price($productfourn->fourn_multicurrency_unitprice);
  1039. print '</td>';
  1040. }
  1041. // Currency
  1042. if (isModEnabled("multicurrency")) {
  1043. print '<td class="right nowraponall">';
  1044. print $productfourn->fourn_multicurrency_code ? currency_name($productfourn->fourn_multicurrency_code) : '';
  1045. print '</td>';
  1046. }
  1047. // Charges
  1048. if (!empty($arrayfields['pfp.charges']['checked'])) {
  1049. print '<td class="right">';
  1050. print price($productfourn->fourn_charges);
  1051. print '</td>';
  1052. }
  1053. // Discount
  1054. print '<td class="right">';
  1055. print price2num($productfourn->fourn_remise_percent).'%';
  1056. print '</td>';
  1057. // Delivery delay
  1058. if (!empty($arrayfields['pfp.delivery_time_days']['checked'])) {
  1059. print '<td class="right">';
  1060. print $productfourn->delivery_time_days;
  1061. print '</td>';
  1062. }
  1063. // Reputation
  1064. if (!empty($arrayfields['pfp.supplier_reputation']['checked'])) {
  1065. print '<td class="center">';
  1066. if (!empty($productfourn->supplier_reputation) && !empty($object->reputations[$productfourn->supplier_reputation])) {
  1067. print $object->reputations[$productfourn->supplier_reputation];
  1068. }
  1069. print'</td>';
  1070. }
  1071. // Barcode type
  1072. if (!empty($arrayfields['pfp.fk_barcode_type']['checked'])) {
  1073. print '<td class="center">';
  1074. $productfourn->barcode_type = !empty($productfourn->supplier_fk_barcode_type) ? $productfourn->supplier_fk_barcode_type : 0;
  1075. $productfourn->fetch_barcode();
  1076. print $productfourn->barcode_type_label ? $productfourn->barcode_type_label : ($productfourn->supplier_barcode ? '<div class="warning">'.$langs->trans("SetDefaultBarcodeType").'<div>' : '');
  1077. print '</td>';
  1078. }
  1079. // Barcode
  1080. if (!empty($arrayfields['pfp.barcode']['checked'])) {
  1081. print '<td align="right">';
  1082. print $productfourn->supplier_barcode;
  1083. print '</td>';
  1084. }
  1085. // Packaging
  1086. if (!empty($arrayfields['pfp.packaging']['checked'])) {
  1087. print '<td align="center">';
  1088. print price2num($productfourn->packaging);
  1089. print '</td>';
  1090. }
  1091. // Date modification
  1092. if (!empty($arrayfields['pfp.tms']['checked'])) {
  1093. print '<td class="right nowraponall">';
  1094. print dol_print_date(($productfourn->fourn_date_modification ? $productfourn->fourn_date_modification : $productfourn->date_modification), "dayhour");
  1095. print '</td>';
  1096. }
  1097. // Extrafields
  1098. if (!empty($extralabels)) {
  1099. $sql = "SELECT";
  1100. $sql .= " fk_object";
  1101. foreach ($extralabels as $key => $value) {
  1102. $sql .= ", ".$key;
  1103. }
  1104. $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price_extrafields";
  1105. $sql .= " WHERE fk_object = ".((int) $productfourn->product_fourn_price_id);
  1106. $resql = $db->query($sql);
  1107. if ($resql) {
  1108. if ($db->num_rows($resql) != 1) {
  1109. foreach ($extralabels as $key => $value) {
  1110. if (!empty($arrayfields['ef.'.$key]['checked']) && !empty($extrafields->attributes["product_fournisseur_price"]['list'][$key]) && $extrafields->attributes["product_fournisseur_price"]['list'][$key] != 3) {
  1111. print "<td></td>";
  1112. }
  1113. }
  1114. } else {
  1115. $obj = $db->fetch_object($resql);
  1116. foreach ($extralabels as $key => $value) {
  1117. if (!empty($arrayfields['ef.'.$key]['checked']) && !empty($extrafields->attributes["product_fournisseur_price"]['list'][$key]) && $extrafields->attributes["product_fournisseur_price"]['list'][$key] != 3) {
  1118. print '<td align="right">'.$extrafields->showOutputField($key, $obj->{$key}, '', 'product_fournisseur_price')."</td>";
  1119. }
  1120. }
  1121. }
  1122. $db->free($resql);
  1123. }
  1124. }
  1125. if (is_object($hookmanager)) {
  1126. $parameters = array('id_pfp'=>$productfourn->product_fourn_price_id, 'id_fourn'=>(!empty($id_fourn)?$id_fourn:''), 'prod_id'=>$object->id);
  1127. $reshook = $hookmanager->executeHooks('printFieldListValue', $parameters, $object, $action);
  1128. }
  1129. // Modify-Remove
  1130. print '<td class="center nowraponall">';
  1131. if ($usercancreate) {
  1132. print '<a class="editfielda" href="'.$_SERVER['PHP_SELF'].'?id='.((int) $object->id).'&socid='.((int) $productfourn->fourn_id).'&action=update_price&token='.newToken().'&rowid='.((int) $productfourn->product_fourn_price_id).'">'.img_edit()."</a>";
  1133. print ' &nbsp; ';
  1134. print '<a href="'.$_SERVER['PHP_SELF'].'?id='.((int) $object->id).'&socid='.((int) $productfourn->fourn_id).'&action=ask_remove_pf&token='.newToken().'&rowid='.((int) $productfourn->product_fourn_price_id).'">'.img_picto($langs->trans("Remove"), 'delete').'</a>';
  1135. }
  1136. print '</td>';
  1137. print '</tr>';
  1138. }
  1139. if (empty($product_fourn_list)) {
  1140. print '<tr><td colspan="'.$nbfields.'"><span class="opacitymedium">'.$langs->trans("None").'</span></td></tr>';
  1141. }
  1142. } else {
  1143. dol_print_error($db);
  1144. }
  1145. print '</table>';
  1146. print '</div>';
  1147. print '</form>';
  1148. }
  1149. }
  1150. }
  1151. } else {
  1152. print $langs->trans("ErrorUnknown");
  1153. }
  1154. // End of page
  1155. llxFooter();
  1156. $db->close();