fournisseurs.php 58 KB

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