fournisseurs.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. <?php
  2. /* Copyright (C) 2001-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2013 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.dolibarr@gmail.com>
  10. * Copyright (C) 2016 Ferran Marcet <fmarcet@2byte.es>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. /**
  26. * \file htdocs/product/fournisseurs.php
  27. * \ingroup product
  28. * \brief Page of tab suppliers for products
  29. */
  30. require '../main.inc.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  33. require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
  35. require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_expression.class.php';
  36. require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
  37. // Load translation files required by the page
  38. $langs->loadLangs(array('products', 'suppliers', 'bills', 'margins'));
  39. $id = GETPOST('id', 'int');
  40. $ref = GETPOST('ref', 'alpha');
  41. $rowid=GETPOST('rowid','int');
  42. $action=GETPOST('action', 'alpha');
  43. $cancel=GETPOST('cancel', 'alpha');
  44. $contextpage=GETPOST('contextpage','aZ')?GETPOST('contextpage','aZ'):'pricesuppliercard';
  45. $socid=GETPOST('socid', 'int');
  46. $cost_price=GETPOST('cost_price', 'alpha');
  47. $backtopage=GETPOST('backtopage','alpha');
  48. $error=0;
  49. // If socid provided by ajax company selector
  50. if (! empty($_REQUEST['search_fourn_id']))
  51. {
  52. $_GET['id_fourn'] = $_GET['search_fourn_id'];
  53. $_POST['id_fourn'] = $_POST['search_fourn_id'];
  54. $_REQUEST['id_fourn'] = $_REQUEST['search_fourn_id'];
  55. }
  56. // Security check
  57. $fieldvalue = (! empty($id) ? $id : (! empty($ref) ? $ref : ''));
  58. $fieldtype = (! empty($ref) ? 'ref' : 'rowid');
  59. if ($user->societe_id) $socid=$user->societe_id;
  60. $result=restrictedArea($user,'produit|service',$fieldvalue,'product&product','','',$fieldtype);
  61. if (empty($user->rights->fournisseur->lire)) accessforbidden();
  62. $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
  63. $sortfield = GETPOST("sortfield",'alpha');
  64. $sortorder = GETPOST("sortorder",'alpha');
  65. $page = (GETPOST("page",'int')?GETPOST("page", 'int'):0);
  66. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  67. $offset = $limit * $page;
  68. $pageprev = $page - 1;
  69. $pagenext = $page + 1;
  70. if (! $sortfield) $sortfield="s.nom";
  71. if (! $sortorder) $sortorder="ASC";
  72. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  73. $hookmanager->initHooks(array('pricesuppliercard','globalcard'));
  74. $object = new ProductFournisseur($db);
  75. if ($id > 0 || $ref)
  76. {
  77. $object->fetch($id,$ref);
  78. }
  79. $sortfield = GETPOST("sortfield",'alpha');
  80. $sortorder = GETPOST("sortorder",'alpha');
  81. if (! $sortfield) $sortfield="s.nom";
  82. if (! $sortorder) $sortorder="ASC";
  83. /*
  84. * Actions
  85. */
  86. if ($cancel) $action='';
  87. $parameters=array('socid'=>$socid, 'id_prod'=>$id);
  88. $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  89. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  90. if (empty($reshook))
  91. {
  92. if ($action == 'setcost_price')
  93. {
  94. if ($id)
  95. {
  96. $result=$object->fetch($id);
  97. $object->cost_price = price2num($cost_price);
  98. $result=$object->update($object->id, $user);
  99. if ($result > 0)
  100. {
  101. setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
  102. $action='';
  103. }
  104. else
  105. {
  106. $error++;
  107. setEventMessages($object->error, $object->errors, 'errors');
  108. }
  109. }
  110. }
  111. if ($action == 'confirm_remove_pf')
  112. {
  113. if ($rowid) // id of product supplier price to remove
  114. {
  115. $action = '';
  116. $result=$object->remove_product_fournisseur_price($rowid);
  117. if($result > 0){
  118. setEventMessages($langs->trans("PriceRemoved"), null, 'mesgs');
  119. }else{
  120. $error++;
  121. setEventMessages($object->error, $object->errors, 'errors');
  122. }
  123. }
  124. }
  125. if ($action == 'updateprice')
  126. {
  127. $id_fourn=GETPOST("id_fourn");
  128. if (empty($id_fourn)) $id_fourn=GETPOST("search_id_fourn");
  129. $ref_fourn=GETPOST("ref_fourn");
  130. if (empty($ref_fourn)) $ref_fourn=GETPOST("search_ref_fourn");
  131. $quantity=GETPOST("qty");
  132. $remise_percent=price2num(GETPOST('remise_percent','alpha'));
  133. $npr = preg_match('/\*/', $_POST['tva_tx']) ? 1 : 0 ;
  134. $tva_tx = str_replace('*','', GETPOST('tva_tx','alpha'));
  135. $tva_tx = price2num($tva_tx);
  136. $price_expression = GETPOST('eid', 'int') ? GETPOST('eid', 'int') : ''; // Discard expression if not in expression mode
  137. $delivery_time_days = GETPOST('delivery_time_days', 'int') ? GETPOST('delivery_time_days', 'int') : '';
  138. $supplier_reputation = GETPOST('supplier_reputation');
  139. $supplier_description = GETPOST('supplier_description', 'alpha');
  140. if ($tva_tx == '')
  141. {
  142. $error++;
  143. $langs->load("errors");
  144. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("VATRateForSupplierProduct")), null, 'errors');
  145. }
  146. if (! is_numeric($tva_tx))
  147. {
  148. $error++;
  149. $langs->load("errors");
  150. setEventMessages($langs->trans("ErrorFieldMustBeANumeric",$langs->transnoentities("VATRateForSupplierProduct")), null, 'errors');
  151. }
  152. if (empty($quantity))
  153. {
  154. $error++;
  155. $langs->load("errors");
  156. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Qty")), null, 'errors');
  157. }
  158. if (empty($ref_fourn))
  159. {
  160. $error++;
  161. $langs->load("errors");
  162. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("RefSupplier")), null, 'errors');
  163. }
  164. if ($id_fourn <= 0)
  165. {
  166. $error++;
  167. $langs->load("errors");
  168. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Supplier")), null, 'errors');
  169. }
  170. if ($_POST["price"] < 0 || $_POST["price"] == '')
  171. {
  172. if ($price_expression === '') // Return error of missing price only if price_expression not set
  173. {
  174. $error++;
  175. $langs->load("errors");
  176. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Price")), null, 'errors');
  177. }
  178. else
  179. {
  180. $_POST["price"] = 0;
  181. }
  182. }
  183. if ($conf->multicurrency->enabled) {
  184. if (empty($_POST["multicurrency_code"])) {
  185. $error++;
  186. $langs->load("errors");
  187. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Currency")), null, 'errors');
  188. }
  189. if ($_POST["multicurrency_tx"] <= 0 || $_POST["multicurrency_tx"] == '') {
  190. $error++;
  191. $langs->load("errors");
  192. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("CurrencyRate")), null, 'errors');
  193. }
  194. if ($_POST["multicurrency_price"] < 0 || $_POST["multicurrency_price"] == '') {
  195. $error++;
  196. $langs->load("errors");
  197. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PriceCurrency")), null, 'errors');
  198. }
  199. }
  200. if (! $error)
  201. {
  202. $db->begin();
  203. if (! $error)
  204. {
  205. $ret=$object->add_fournisseur($user, $id_fourn, $ref_fourn, $quantity); // This insert record with no value for price. Values are update later with update_buyprice
  206. if ($ret == -3)
  207. {
  208. $error++;
  209. $object->fetch($object->product_id_already_linked);
  210. $productLink = $object->getNomUrl(1,'supplier');
  211. setEventMessages($langs->trans("ReferenceSupplierIsAlreadyAssociatedWithAProduct",$productLink), null, 'errors');
  212. }
  213. else if ($ret < 0)
  214. {
  215. $error++;
  216. setEventMessages($object->error, $object->errors, 'errors');
  217. }
  218. }
  219. if (! $error)
  220. {
  221. $supplier=new Fournisseur($db);
  222. $result=$supplier->fetch($id_fourn);
  223. if (isset($_POST['ref_fourn_price_id']))
  224. $object->fetch_product_fournisseur_price($_POST['ref_fourn_price_id']);
  225. $newprice = price2num(GETPOST("price","alpha"));
  226. if ($conf->multicurrency->enabled)
  227. {
  228. $multicurrency_tx = price2num(GETPOST("multicurrency_tx",'alpha'));
  229. $multicurrency_price = price2num(GETPOST("multicurrency_price",'alpha'));
  230. $multicurrency_code = GETPOST("multicurrency_code",'alpha');
  231. $ret = $object->update_buyprice($quantity, $newprice, $user, $_POST["price_base_type"], $supplier, $_POST["oselDispo"], $ref_fourn, $tva_tx, $_POST["charges"], $remise_percent, 0, $npr, $delivery_time_days, $supplier_reputation, array(), '', $multicurrency_price, $_POST["multicurrency_price_base_type"], $multicurrency_tx, $multicurrency_code, $supplier_description);
  232. } else {
  233. $ret = $object->update_buyprice($quantity, $newprice, $user, $_POST["price_base_type"], $supplier, $_POST["oselDispo"], $ref_fourn, $tva_tx, $_POST["charges"], $remise_percent, 0, $npr, $delivery_time_days, $supplier_reputation, array(), '', 0, 'HT', 1, '', $supplier_description);
  234. }
  235. if ($ret < 0)
  236. {
  237. $error++;
  238. setEventMessages($object->error, $object->errors, 'errors');
  239. }
  240. else
  241. {
  242. if (!empty($conf->dynamicprices->enabled) && $price_expression !== '')
  243. {
  244. //Check the expression validity by parsing it
  245. $priceparser = new PriceParser($db);
  246. $object->fk_supplier_price_expression = $price_expression;
  247. $price_result = $priceparser->parseProductSupplier($object);
  248. if ($price_result < 0) { //Expression is not valid
  249. $error++;
  250. setEventMessages($priceparser->translatedError(), null, 'errors');
  251. }
  252. }
  253. if (! $error && ! empty($conf->dynamicprices->enabled))
  254. {
  255. //Set the price expression for this supplier price
  256. $ret=$object->setSupplierPriceExpression($price_expression);
  257. if ($ret < 0)
  258. {
  259. $error++;
  260. setEventMessages($object->error, $object->errors, 'errors');
  261. }
  262. }
  263. }
  264. }
  265. if (! $error)
  266. {
  267. $db->commit();
  268. $action='';
  269. }
  270. else
  271. {
  272. $db->rollback();
  273. }
  274. }
  275. else
  276. {
  277. $action = 'add_price';
  278. }
  279. }
  280. }
  281. /*
  282. * view
  283. */
  284. $title = $langs->trans('ProductServiceCard');
  285. $helpurl = '';
  286. $shortlabel = dol_trunc($object->label,16);
  287. if (GETPOST("type") == '0' || ($object->type == Product::TYPE_PRODUCT))
  288. {
  289. $title = $langs->trans('Product')." ". $shortlabel ." - ".$langs->trans('BuyingPrices');
  290. $helpurl='EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
  291. }
  292. if (GETPOST("type") == '1' || ($object->type == Product::TYPE_SERVICE))
  293. {
  294. $title = $langs->trans('Service')." ". $shortlabel ." - ".$langs->trans('BuyingPrices');
  295. $helpurl='EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
  296. }
  297. llxHeader('', $title, $helpurl);
  298. $form = new Form($db);
  299. if ($id > 0 || $ref)
  300. {
  301. if ($result)
  302. {
  303. if ($action == 'ask_remove_pf') {
  304. $form = new Form($db);
  305. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $id . '&rowid=' . $rowid, $langs->trans('DeleteProductBuyPrice'), $langs->trans('ConfirmDeleteProductBuyPrice'), 'confirm_remove_pf', '', 0, 1);
  306. echo $formconfirm;
  307. }
  308. if ($action <> 'edit' && $action <> 're-edit')
  309. {
  310. $head=product_prepare_head($object);
  311. $titre=$langs->trans("CardProduct".$object->type);
  312. $picto=($object->type== Product::TYPE_SERVICE?'service':'product');
  313. dol_fiche_head($head, 'suppliers', $titre, -1, $picto);
  314. $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  315. $object->next_prev_filter=" fk_product_type = ".$object->type;
  316. $shownav = 1;
  317. if ($user->societe_id && ! in_array('product', explode(',',$conf->global->MAIN_MODULES_FOR_EXTERNAL))) $shownav=0;
  318. dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
  319. print '<div class="fichecenter">';
  320. print '<div class="underbanner clearboth"></div>';
  321. print '<table class="border tableforfield" width="100%">';
  322. // Minimum Price
  323. print '<tr><td class="titlefield">'.$langs->trans("BuyingPriceMin").'</td>';
  324. print '<td colspan="2">';
  325. $product_fourn = new ProductFournisseur($db);
  326. if ($product_fourn->find_min_price_product_fournisseur($object->id) > 0)
  327. {
  328. if ($product_fourn->product_fourn_price_id > 0) print $product_fourn->display_price_product_fournisseur();
  329. else print $langs->trans("NotDefined");
  330. }
  331. print '</td></tr>';
  332. // Cost price. Can be used for margin module for option "calculate margin on explicit cost price
  333. // Accountancy sell code
  334. print '<tr><td>';
  335. $textdesc =$langs->trans("CostPriceDescription");
  336. $textdesc.="<br>".$langs->trans("CostPriceUsage");
  337. $text=$form->textwithpicto($langs->trans("CostPrice"), $textdesc, 1, 'help', '');
  338. print $form->editfieldkey($text,'cost_price',$object->cost_price,$object,$user->rights->produit->creer||$user->rights->service->creer,'amount:6');
  339. print '</td><td colspan="2">';
  340. print $form->editfieldval($text,'cost_price',$object->cost_price,$object,$user->rights->produit->creer||$user->rights->service->creer,'amount:6');
  341. print '</td></tr>';
  342. print '</table>';
  343. print '</div>';
  344. print '<div style="clear:both"></div>';
  345. dol_fiche_end();
  346. // Form to add or update a price
  347. if (($action == 'add_price' || $action == 'updateprice' ) && ($user->rights->produit->creer || $user->rights->service->creer))
  348. {
  349. $langs->load("suppliers");
  350. if ($rowid)
  351. {
  352. $object->fetch_product_fournisseur_price($rowid, 1); //Ignore the math expression when getting the price
  353. print load_fiche_titre($langs->trans("ChangeSupplierPrice"));
  354. }
  355. else
  356. {
  357. print load_fiche_titre($langs->trans("AddSupplierPrice"));
  358. }
  359. print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'" method="POST">';
  360. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  361. print '<input type="hidden" name="action" value="updateprice">';
  362. dol_fiche_head();
  363. print '<table class="border" width="100%">';
  364. // Supplier
  365. print '<tr><td class="titlefield fieldrequired">'.$langs->trans("Supplier").'</td><td>';
  366. if ($rowid)
  367. {
  368. $supplier=new Fournisseur($db);
  369. $supplier->fetch($socid);
  370. print $supplier->getNomUrl(1);
  371. print '<input type="hidden" name="id_fourn" value="'.$socid.'">';
  372. print '<input type="hidden" name="ref_fourn" value="'.$object->fourn_ref.'">';
  373. print '<input type="hidden" name="ref_fourn_price_id" value="'.$rowid.'">';
  374. print '<input type="hidden" name="rowid" value="'.$rowid.'">';
  375. print '<input type="hidden" name="socid" value="'.$socid.'">';
  376. }
  377. else
  378. {
  379. $events=array();
  380. $events[]=array('method' => 'getVatRates', 'url' => dol_buildpath('/core/ajax/vatrates.php',1), 'htmlname' => 'tva_tx', 'params' => array());
  381. print $form->select_company(GETPOST("id_fourn"),'id_fourn','fournisseur=1','SelectThirdParty',0,0,$events);
  382. $parameters=array('filtre'=>"fournisseur=1",'html_name'=>'id_fourn','selected'=>GETPOST("id_fourn"),'showempty'=>1,'prod_id'=>$object->id);
  383. $reshook=$hookmanager->executeHooks('formCreateThirdpartyOptions',$parameters,$object,$action);
  384. if (empty($reshook))
  385. {
  386. if (empty($form->result))
  387. {
  388. print ' - <a href="'.DOL_URL_ROOT.'/societe/card.php?action=create&type=f&backtopage='.urlencode($_SERVER["PHP_SELF"].'?id='.$object->id.'&action='.$action).'">'.$langs->trans("CreateDolibarrThirdPartySupplier").'</a>';
  389. }
  390. }
  391. }
  392. print '</td></tr>';
  393. // Ref supplier
  394. print '<tr><td class="fieldrequired">'.$langs->trans("SupplierRef").'</td><td>';
  395. if ($rowid)
  396. {
  397. print $object->fourn_ref;
  398. }
  399. else
  400. {
  401. print '<input class="flat" name="ref_fourn" size="12" value="'.(GETPOST("ref_fourn")?GETPOST("ref_fourn"):'').'">';
  402. }
  403. print '</td>';
  404. print '</tr>';
  405. // Availability
  406. if (! empty($conf->global->FOURN_PRODUCT_AVAILABILITY))
  407. {
  408. $langs->load("propal");
  409. print '<tr><td>'.$langs->trans("Availability").'</td><td>';
  410. $form->selectAvailabilityDelay($object->fk_availability,"oselDispo",1);
  411. print '</td></tr>'."\n";
  412. }
  413. // Qty min
  414. print '<tr>';
  415. print '<td class="fieldrequired">'.$langs->trans("QtyMin").'</td>';
  416. print '<td>';
  417. $quantity = GETPOST('qty') ? GETPOST('qty') : "1";
  418. if ($rowid)
  419. {
  420. print '<input type="hidden" name="qty" value="'.$object->fourn_qty.'">';
  421. print $object->fourn_qty;
  422. }
  423. else
  424. {
  425. print '<input class="flat" name="qty" size="5" value="'.$quantity.'">';
  426. }
  427. print '</td></tr>';
  428. // Vat rate
  429. $default_vat='';
  430. // We don't have supplier, so we try to guess.
  431. // For this we build a fictive supplier with same properties than user but using vat)
  432. $mysoc2 = clone $mysoc;
  433. $mysoc2->name='Fictive seller with same country';
  434. $mysoc2->tva_assuj=1;
  435. $default_vat=get_default_tva($mysoc2, $mysoc, $object->id, 0);
  436. $default_npr=get_default_npr($mysoc2, $mysoc, $object->id, 0);
  437. if (empty($default_vat)) $default_npr=$default_vat;
  438. print '<tr><td class="fieldrequired">'.$langs->trans("VATRateForSupplierProduct").'</td>';
  439. print '<td>';
  440. //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
  441. if (! empty($rowid)) // If we have a supplier, it is an update, we must show the vat of current supplier price
  442. {
  443. $tmpproductsupplier=new ProductFournisseur($db);
  444. $tmpproductsupplier->fetch_product_fournisseur_price($rowid, 1);
  445. $default_vat=$tmpproductsupplier->fourn_tva_tx;
  446. $default_npr=$tmpproductsupplier->fourn_tva_npr;
  447. }
  448. else
  449. {
  450. if (empty($default_vat))
  451. {
  452. $default_vat=$object->tva_tx;
  453. }
  454. }
  455. $vattosuggest=(GETPOST("tva_tx")?vatrate(GETPOST("tva_tx")):($default_vat!=''?vatrate($default_vat):''));
  456. $vattosuggest=preg_replace('/\s*\(.*\)$/','', $vattosuggest);
  457. print '<input type="text" class="flat" size="5" name="tva_tx" value="'.$vattosuggest.'">';
  458. print '</td></tr>';
  459. if (! empty($conf->dynamicprices->enabled)) //Only show price mode and expression selector if module is enabled
  460. {
  461. // Price mode selector
  462. print '<tr><td class="fieldrequired">'.$langs->trans("PriceMode").'</td><td>';
  463. $price_expression = new PriceExpression($db);
  464. $price_expression_list = array(0 => $langs->trans("PriceNumeric")); //Put the numeric mode as first option
  465. foreach ($price_expression->list_price_expression() as $entry) {
  466. $price_expression_list[$entry->id] = $entry->title;
  467. }
  468. $price_expression_preselection = GETPOST('eid') ? GETPOST('eid') : ($object->fk_supplier_price_expression ? $object->fk_supplier_price_expression : '0');
  469. print $form->selectarray('eid', $price_expression_list, $price_expression_preselection);
  470. print '&nbsp; <div id="expression_editor" class="button">'.$langs->trans("PriceExpressionEditor").'</div>';
  471. print '</td></tr>';
  472. // This code hides the numeric price input if is not selected, loads the editor page if editor button is pressed
  473. print '<script type="text/javascript">
  474. jQuery(document).ready(run);
  475. function run() {
  476. jQuery("#expression_editor").click(on_click);
  477. jQuery("#eid").change(on_change);
  478. on_change();
  479. }
  480. function on_click() {
  481. window.location = "'.DOL_URL_ROOT.'/product/dynamic_price/editor.php?id='.$id.'&tab=fournisseurs&eid=" + $("#eid").val();
  482. }
  483. function on_change() {
  484. if ($("#eid").val() == 0) {
  485. jQuery("#price_numeric").show();
  486. } else {
  487. jQuery("#price_numeric").hide();
  488. }
  489. }
  490. </script>';
  491. }
  492. if ($conf->multicurrency->enabled) {
  493. // Currency
  494. print '<tr><td class="fieldrequired">'.$langs->trans("Currency").'</td>';
  495. print '<td>';
  496. $currencycodetouse = GETPOST('multicurrency_code')?GETPOST('multicurrency_code'):(isset($object->fourn_multicurrency_code)?$object->fourn_multicurrency_code:'');
  497. if (empty($currencycodetouse) && $object->fourn_multicurrency_tx == 1) $currencycodetouse=$conf->currency;
  498. print $form->selectMultiCurrency($currencycodetouse, "multicurrency_code", 1);
  499. print '</td>';
  500. print '</tr>';
  501. // Currency tx
  502. print '<tr><td class="fieldrequired">'.$langs->trans("CurrencyRate").'</td>';
  503. print '<td><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:'')).'">';
  504. print '</td>';
  505. print '</tr>';
  506. // Currency price qty min
  507. print '<tr><td class="fieldrequired">'.$langs->trans("PriceQtyMinCurrency").'</td>';
  508. $pricesupplierincurrencytouse=(GETPOST('multicurrency_price')?GETPOST('multicurrency_price'):(isset($object->fourn_multicurrency_price)?$object->fourn_multicurrency_price:''));
  509. print '<td><input class="flat" name="multicurrency_price" size="8" value="'.price($pricesupplierincurrencytouse).'">';
  510. print '&nbsp;';
  511. 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
  512. print '</td></tr>';
  513. // Price qty min
  514. print '<tr><td class="fieldrequired">' . $langs->trans("PriceQtyMin") . '</td>';
  515. print '<td><input class="flat" name="disabled_price" size="8" value="">';
  516. print '<input type="hidden" name="price" value="">';
  517. print '<input type="hidden" name="price_base_type" value="">';
  518. print '&nbsp;';
  519. print $form->selectPriceBaseType('', "disabled_price_base_type");
  520. print '</td></tr>';
  521. $currencies = array();
  522. $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'multicurrency WHERE entity = '.$conf->entity;
  523. $resql = $db->query($sql);
  524. if ($resql) {
  525. $currency = new MultiCurrency($db);
  526. while ($obj = $db->fetch_object($resql)) {
  527. $currency->fetch($obj->rowid);
  528. $currencies[$currency->code] = $currency->rate->rate;
  529. }
  530. }
  531. $currencies = json_encode($currencies);
  532. print <<<SCRIPT
  533. <script type="text/javascript">
  534. function update_price_from_multicurrency() {
  535. var multicurrency_price = $('input[name="multicurrency_price"]').val();
  536. var multicurrency_tx = $('input[name="multicurrency_tx"]').val();
  537. $('input[name="price"]').val(multicurrency_price / multicurrency_tx);
  538. $('input[name="disabled_price"]').val(multicurrency_price / multicurrency_tx);
  539. }
  540. jQuery(document).ready(function () {
  541. $('input[name="disabled_price"]').prop('disabled', true);
  542. $('select[name="disabled_price_base_type"]').prop('disabled', true);
  543. update_price_from_multicurrency();
  544. $('input[name="multicurrency_price"]').keyup(function () {
  545. update_price_from_multicurrency();
  546. }).change(function () {
  547. update_price_from_multicurrency();
  548. }).on('paste', function () {
  549. update_price_from_multicurrency();
  550. });
  551. $('input[name="multicurrency_tx"]').keyup(function () {
  552. update_price_from_multicurrency();
  553. }).change(function () {
  554. update_price_from_multicurrency();
  555. }).on('paste', function () {
  556. update_price_from_multicurrency();
  557. });
  558. $('select[name="multicurrency_price_base_type"]').change(function () {
  559. $('input[name="price_base_type"]').val($(this).val());
  560. $('select[name="disabled_price_base_type"]').val($(this).val());
  561. });
  562. var currencies_array = $currencies;
  563. $('select[name="multicurrency_code"]').change(function () {
  564. $('input[name="multicurrency_tx"]').val(currencies_array[$(this).val()]);
  565. });
  566. });
  567. </script>
  568. SCRIPT;
  569. } else {
  570. // Price qty min
  571. print '<tr><td class="fieldrequired">' . $langs->trans("PriceQtyMin") . '</td>';
  572. print '<td><input class="flat" name="price" size="8" value="' . (GETPOST('price') ? price(GETPOST('price')) : (isset($object->fourn_price) ? price($object->fourn_price) : '')) . '">';
  573. print '&nbsp;';
  574. print $form->selectPriceBaseType((GETPOST('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
  575. print '</td></tr>';
  576. }
  577. // Discount qty min
  578. print '<tr><td>'.$langs->trans("DiscountQtyMin").'</td>';
  579. print '<td><input class="flat" name="remise_percent" size="4" value="'.(GETPOST('remise_percent')?vatrate(GETPOST('remise_percent')):(isset($object->fourn_remise_percent)?vatrate($object->fourn_remise_percent):'')).'"> %';
  580. print '</td>';
  581. print '</tr>';
  582. // Delivery delay in days
  583. print '<tr>';
  584. print '<td>'.$langs->trans('NbDaysToDelivery').'</td>';
  585. print '<td><input class="flat" name="delivery_time_days" size="4" value="'.($rowid ? $object->delivery_time_days : '').'">&nbsp;'.$langs->trans('days').'</td>';
  586. print '</tr>';
  587. // Reputation
  588. print '<tr><td>'.$langs->trans("SupplierReputation").'</td><td>';
  589. echo $form->selectarray('supplier_reputation', $object->reputations, $supplier_reputation?$supplier_reputation:$object->supplier_reputation);
  590. print '</td></tr>';
  591. // Option to define a transport cost on supplier price
  592. if ($conf->global->PRODUCT_CHARGES)
  593. {
  594. if (! empty($conf->margin->enabled))
  595. {
  596. print '<tr>';
  597. print '<td>'.$langs->trans("Charges").'</td>';
  598. print '<td><input class="flat" name="charges" size="8" value="'.(GETPOST('charges')?price(GETPOST('charges')):(isset($object->fourn_charges)?price($object->fourn_charges):'')).'">';
  599. print '</td>';
  600. print '</tr>';
  601. }
  602. }
  603. // Product description of the supplier
  604. if (! empty($conf->global->PRODUIT_FOURN_TEXTS))
  605. {
  606. //WYSIWYG Editor
  607. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  608. print '<tr>';
  609. print '<td>'.$langs->trans('ProductSupplierDescription').'</td>';
  610. print '<td>';
  611. $doleditor = new DolEditor('supplier_description', $object->desc_supplier, '', 160, 'dolibarr_details', '', false, true, $conf->global->FCKEDITOR_ENABLE_PRODUCTDESC, ROWS_4, '90%');
  612. $doleditor->Create();
  613. print '</td>';
  614. print '</tr>';
  615. }
  616. if (is_object($hookmanager))
  617. {
  618. $parameters=array('id_fourn'=>$id_fourn,'prod_id'=>$object->id);
  619. $reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action);
  620. print $hookmanager->resPrint;
  621. }
  622. print '</table>';
  623. dol_fiche_end();
  624. print '<div class="center">';
  625. print '<input class="button" type="submit" value="'.$langs->trans("Save").'">';
  626. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  627. print '<input class="button" type="submit" name="cancel" value="'.$langs->trans("Cancel").'">';
  628. print '</div>';
  629. print '</form>';
  630. }
  631. // Actions buttons
  632. print "\n<div class=\"tabsAction\">\n";
  633. if ($action != 'add_price' && $action != 'updateprice')
  634. {
  635. $parameters=array();
  636. $reshook=$hookmanager->executeHooks('addMoreActionsButtons',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
  637. if (empty($reshook))
  638. {
  639. if ($user->rights->produit->creer || $user->rights->service->creer)
  640. {
  641. print '<a class="butAction" href="'.DOL_URL_ROOT.'/product/fournisseurs.php?id='.$object->id.'&amp;action=add_price">';
  642. print $langs->trans("AddSupplierPrice").'</a>';
  643. }
  644. }
  645. }
  646. print "\n</div>\n";
  647. print '<br>';
  648. if ($user->rights->fournisseur->lire)
  649. {
  650. $param='';
  651. if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.urlencode($contextpage);
  652. if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.urlencode($limit);
  653. $param.='&ref='.urlencode($object->ref);
  654. $product_fourn = new ProductFournisseur($db);
  655. $product_fourn_list = $product_fourn->list_product_fournisseur_price($object->id, $sortfield, $sortorder, $limit, $offset);
  656. $product_fourn_list_all = $product_fourn->list_product_fournisseur_price($object->id, $sortfield, $sortorder, 0, 0);
  657. $nbtotalofrecords = count($product_fourn_list_all);
  658. $num = count($product_fourn_list);
  659. if (($num + ($offset * $limit)) < $nbtotalofrecords) $num++;
  660. print_barre_liste($langs->trans('SupplierPrices'), $page, $_SERVEUR ['PHP_SELF'], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'title_accountancy.png', 0, '', '', $limit, 1);
  661. // Suppliers list title
  662. print '<div class="div-table-responsive">';
  663. print '<table class="noborder" width="100%">';
  664. if ($object->isProduct()) $nblignefour=4;
  665. else $nblignefour=4;
  666. $param="&id=".$object->id;
  667. print '<tr class="liste_titre">';
  668. print_liste_field_titre("AppliedPricesFrom",$_SERVER["PHP_SELF"],"pfp.datec","",$param,"",$sortfield,$sortorder);
  669. print_liste_field_titre("Suppliers",$_SERVER["PHP_SELF"],"s.nom","",$param,"",$sortfield,$sortorder);
  670. print_liste_field_titre("SupplierRef",$_SERVER["PHP_SELF"],"","",$param,"",$sortfield,$sortorder);
  671. if (!empty($conf->global->FOURN_PRODUCT_AVAILABILITY)) print_liste_field_titre("Availability",$_SERVER["PHP_SELF"],"pfp.fk_availability","",$param,"",$sortfield,$sortorder);
  672. print_liste_field_titre("QtyMin",$_SERVER["PHP_SELF"],"pfp.quantity","",$param,'align="right"',$sortfield,$sortorder);
  673. print_liste_field_titre("VATRate",$_SERVER["PHP_SELF"],'','',$param,'align="right"',$sortfield,$sortorder);
  674. print_liste_field_titre("PriceQtyMinHT",$_SERVER["PHP_SELF"],'','',$param,'align="right"',$sortfield,$sortorder);
  675. if ($conf->multicurrency->enabled) {
  676. print_liste_field_titre("PriceQtyMinHTCurrency", $_SERVER["PHP_SELF"], '', '', $param, 'align="right"', $sortfield, $sortorder);
  677. }
  678. print_liste_field_titre("UnitPriceHT",$_SERVER["PHP_SELF"],"pfp.unitprice","",$param,'align="right"',$sortfield,$sortorder);
  679. if ($conf->multicurrency->enabled) {
  680. print_liste_field_titre("UnitPriceHTCurrency", $_SERVER["PHP_SELF"], "pfp.multicurrency_unitprice", "", $param, 'align="right"', $sortfield, $sortorder);
  681. print_liste_field_titre("Currency", $_SERVER["PHP_SELF"], "", "", $param, 'align="right"', $sortfield, $sortorder);
  682. }
  683. print_liste_field_titre("DiscountQtyMin",$_SERVER["PHP_SELF"],'','',$param,'align="right"',$sortfield,$sortorder);
  684. print_liste_field_titre("NbDaysToDelivery",$_SERVER["PHP_SELF"],"pfp.delivery_time_days","",$param,'align="right"',$sortfield,$sortorder);
  685. print_liste_field_titre("ReputationForThisProduct",$_SERVER["PHP_SELF"],"pfp.supplier_reputation","",$param,'align="center"',$sortfield,$sortorder);
  686. print_liste_field_titre('');
  687. print "</tr>\n";
  688. if (is_array($product_fourn_list))
  689. {
  690. foreach($product_fourn_list as $productfourn)
  691. {
  692. print '<tr class="oddeven">';
  693. // Date from
  694. print '<td>'.dol_print_date($productfourn->date_creation, 'dayhour').'</td>';
  695. // Supplier
  696. print '<td>'.$productfourn->getSocNomUrl(1,'supplier').'</td>';
  697. // Supplier ref
  698. print '<td align="left">'.$productfourn->fourn_ref.'</td>';
  699. // Availability
  700. if(!empty($conf->global->FOURN_PRODUCT_AVAILABILITY))
  701. {
  702. $form->load_cache_availability();
  703. $availability= $form->cache_availability[$productfourn->fk_availability]['label'];
  704. print '<td align="left">'.$availability.'</td>';
  705. }
  706. // Quantity
  707. print '<td align="right">';
  708. print $productfourn->fourn_qty;
  709. print '</td>';
  710. // VAT rate
  711. print '<td align="right">';
  712. print vatrate($productfourn->fourn_tva_tx,true);
  713. print '</td>';
  714. // Price for the quantity
  715. print '<td align="right">';
  716. print $productfourn->fourn_price?price($productfourn->fourn_price):"";
  717. print '</td>';
  718. if ($conf->multicurrency->enabled) {
  719. // Price for the quantity in currency
  720. print '<td align="right">';
  721. print $productfourn->fourn_multicurrency_price ? price($productfourn->fourn_multicurrency_price) : "";
  722. print '</td>';
  723. }
  724. // Unit price
  725. print '<td align="right">';
  726. print price($productfourn->fourn_unitprice);
  727. //print $objp->unitprice? price($objp->unitprice) : ($objp->quantity?price($objp->price/$objp->quantity):"&nbsp;");
  728. print '</td>';
  729. if ($conf->multicurrency->enabled) {
  730. // Unit price in currency
  731. print '<td align="right">';
  732. print price($productfourn->fourn_multicurrency_unitprice);
  733. print '</td>';
  734. // Currency
  735. print '<td align="right">';
  736. print $productfourn->fourn_multicurrency_code ? currency_name($productfourn->fourn_multicurrency_code) : '';
  737. print '</td>';
  738. }
  739. // Discount
  740. print '<td align="right">';
  741. print price2num($productfourn->fourn_remise_percent).'%';
  742. print '</td>';
  743. // Delivery delay
  744. print '<td align="right">';
  745. print $productfourn->delivery_time_days;
  746. print '</td>';
  747. // Reputation
  748. print '<td align="center">';
  749. if (!empty($productfourn->supplier_reputation) && !empty($object->reputations[$productfourn->supplier_reputation])) {
  750. print $object->reputations[$productfourn->supplier_reputation];
  751. }
  752. print'</td>';
  753. if (is_object($hookmanager))
  754. {
  755. $parameters=array('id_pfp'=>$productfourn->product_fourn_price_id,'id_fourn'=>$id_fourn,'prod_id'=>$object->id);
  756. $reshook=$hookmanager->executeHooks('printObjectLine',$parameters,$object,$action);
  757. }
  758. // Modify-Remove
  759. print '<td class="center nowraponall">';
  760. if ($user->rights->produit->creer || $user->rights->service->creer)
  761. {
  762. print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;socid='.$productfourn->fourn_id.'&amp;action=add_price&amp;rowid='.$productfourn->product_fourn_price_id.'">'.img_edit()."</a>";
  763. print ' &nbsp; ';
  764. print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;socid='.$productfourn->fourn_id.'&amp;action=ask_remove_pf&amp;rowid='.$productfourn->product_fourn_price_id.'">'.img_picto($langs->trans("Remove"),'delete').'</a>';
  765. }
  766. print '</td>';
  767. print '</tr>';
  768. }
  769. }
  770. else
  771. {
  772. dol_print_error($db);
  773. }
  774. print '</table>';
  775. print '</div>';
  776. }
  777. }
  778. }
  779. }
  780. else
  781. {
  782. print $langs->trans("ErrorUnknown");
  783. }
  784. // End of page
  785. llxFooter();
  786. $db->close();