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