fournisseurs.php 58 KB

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