PageRenderTime 69ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/product/fournisseurs.php

https://bitbucket.org/speedealing/speedealing
PHP | 531 lines | 370 code | 88 blank | 73 comment | 76 complexity | 772744c09e1407a5419a1fffc4277e9b MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2001-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2012 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@capnetworks.com>
  6. * Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
  7. * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/product/fournisseurs.php
  24. * \ingroup product
  25. * \brief Page of tab suppliers for products
  26. */
  27. require '../main.inc.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
  31. $langs->load("products");
  32. $langs->load("suppliers");
  33. $langs->load("bills");
  34. $id = GETPOST('id', 'int');
  35. $ref = GETPOST('ref', 'alpha');
  36. $rowid=GETPOST('rowid','int');
  37. $action=GETPOST('action', 'alpha');
  38. $socid=GETPOST('socid', 'int');
  39. $error=0; $mesg = '';
  40. // If socid provided by ajax company selector
  41. if (! empty($_REQUEST['search_fourn_id']))
  42. {
  43. $_GET['id_fourn'] = $_GET['search_fourn_id'];
  44. $_POST['id_fourn'] = $_POST['search_fourn_id'];
  45. $_REQUEST['id_fourn'] = $_REQUEST['search_fourn_id'];
  46. }
  47. // Security check
  48. $fieldvalue = (! empty($id) ? $id : (! empty($ref) ? $ref : ''));
  49. $fieldtype = (! empty($ref) ? 'ref' : 'rowid');
  50. if ($user->societe_id) $socid=$user->societe_id;
  51. $result=restrictedArea($user,'produit|service&fournisseur',$fieldvalue,'product&product','','',$fieldtype);
  52. // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
  53. $hookmanager->initHooks(array('pricesuppliercard'));
  54. $sortfield = GETPOST("sortfield",'alpha');
  55. $sortorder = GETPOST("sortorder",'alpha');
  56. if (! $sortfield) $sortfield="s.nom";
  57. if (! $sortorder) $sortorder="ASC";
  58. /*
  59. * Actions
  60. */
  61. if ($action == 'remove_pf')
  62. {
  63. $product = new ProductFournisseur($db);
  64. if ($product->fetch($id) > 0)
  65. {
  66. if ($rowid)
  67. {
  68. $result=$product->remove_product_fournisseur_price($rowid);
  69. $action = '';
  70. $mesg = '<div class="ok">'.$langs->trans("PriceRemoved").'.</div>';
  71. }
  72. }
  73. }
  74. if ($action == 'updateprice' && GETPOST('cancel') <> $langs->trans("Cancel"))
  75. {
  76. $id_fourn=GETPOST("id_fourn");
  77. if (empty($id_fourn)) $id_fourn=GETPOST("search_id_fourn");
  78. $ref_fourn=GETPOST("ref_fourn");
  79. if (empty($ref_fourn)) $ref_fourn=GETPOST("search_ref_fourn");
  80. $quantity=GETPOST("qty");
  81. $remise_percent=price2num(GETPOST('remise_percent','alpha'));
  82. $tva_tx=price2num(GETPOST('tva_tx','alpha'));
  83. if (empty($quantity))
  84. {
  85. $error++;
  86. $mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Qty")).'</div>';
  87. }
  88. if (empty($ref_fourn))
  89. {
  90. $error++;
  91. $mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("RefSupplier")).'</div>';
  92. }
  93. if ($id_fourn <= 0)
  94. {
  95. $error++;
  96. $mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Supplier")).'</div>';
  97. }
  98. if ($_POST["price"] < 0 || $_POST["price"] == '')
  99. {
  100. $error++;
  101. $mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Price")).'</div>';
  102. }
  103. $product = new ProductFournisseur($db);
  104. $result=$product->fetch($id);
  105. if ($result <= 0)
  106. {
  107. $error++;
  108. $mesg=$product->error;
  109. }
  110. if (! $error)
  111. {
  112. $db->begin();
  113. if (! $error)
  114. {
  115. $ret=$product->add_fournisseur($user, $id_fourn, $ref_fourn, $quantity); // This insert record with no value for price. Values are update later with update_buyprice
  116. if ($ret == -3)
  117. {
  118. $error++;
  119. $product->fetch($product->product_id_already_linked);
  120. $productLink = $product->getNomUrl(1,'supplier');
  121. $mesg='<div class="error">'.$langs->trans("ReferenceSupplierIsAlreadyAssociatedWithAProduct",$productLink).'</div>';
  122. }
  123. else if ($ret < 0)
  124. {
  125. $error++;
  126. $mesg='<div class="error">'.$product->error.'</div>';
  127. }
  128. }
  129. if (! $error)
  130. {
  131. $supplier=new Fournisseur($db);
  132. $result=$supplier->fetch($id_fourn);
  133. if (isset($_POST['ref_fourn_price_id']))
  134. $product->fetch_product_fournisseur_price($_POST['ref_fourn_price_id']);
  135. $ret=$product->update_buyprice($quantity, $_POST["price"], $user, $_POST["price_base_type"], $supplier, $_POST["oselDispo"], $ref_fourn, $tva_tx, $_POST["charges"], $remise_percent);
  136. if ($ret < 0)
  137. {
  138. $error++;
  139. $mesg='<div class="error">'.$product->error.'</div>';
  140. }
  141. }
  142. if (! $error)
  143. {
  144. $db->commit();
  145. $action='';
  146. }
  147. else
  148. {
  149. $db->rollback();
  150. }
  151. }
  152. }
  153. if (GETPOST('cancel') == $langs->trans("Cancel"))
  154. {
  155. $action = '';
  156. header("Location: fournisseurs.php?id=".$_GET["id"]);
  157. exit;
  158. }
  159. /*
  160. * view
  161. */
  162. $form = new Form($db);
  163. if ($id || $ref)
  164. {
  165. if ($action <> 're-edit')
  166. {
  167. $product = new ProductFournisseur($db);
  168. $result = $product->fetch($id,$ref);
  169. //$result = $product->fetch_fourn_data($_REQUEST["id_fourn"]);
  170. llxHeader("","",$langs->trans("CardProduct".$product->type));
  171. }
  172. if ($result)
  173. {
  174. if ($action <> 'edit' && $action <> 're-edit')
  175. {
  176. /*
  177. * En mode visu
  178. */
  179. $head=product_prepare_head($product, $user);
  180. $titre=$langs->trans("CardProduct".$product->type);
  181. $picto=($product->type==1?'service':'product');
  182. dol_fiche_head($head, 'suppliers', $titre, 0, $picto);
  183. print '<table class="border" width="100%">';
  184. // Reference
  185. print '<tr>';
  186. print '<td width="15%">'.$langs->trans("Ref").'</td><td colspan="2">';
  187. print $form->showrefnav($product,'ref','',1,'ref');
  188. print '</td>';
  189. print '</tr>';
  190. // Label
  191. print '<tr><td>'.$langs->trans("Label").'</td><td colspan="2">'.$product->libelle.'</td></tr>';
  192. // Minimum Price
  193. print '<tr><td>'.$langs->trans("BuyingPriceMin").'</td>';
  194. print '<td colspan="2">';
  195. $product_fourn = new ProductFournisseur($db);
  196. if ($product_fourn->find_min_price_product_fournisseur($product->id) > 0)
  197. {
  198. if ($product_fourn->product_fourn_price_id > 0) print $product_fourn->display_price_product_fournisseur();
  199. else print $langs->trans("NotDefined");
  200. }
  201. print '</td></tr>';
  202. // Status (to buy)
  203. print '<tr><td>'.$langs->trans("Status").' ('.$langs->trans("Buy").')</td><td>';
  204. print $product->getLibStatut(2,1);
  205. print '</td></tr>';
  206. print '</table>';
  207. print "</div>\n";
  208. dol_htmloutput_mesg($mesg);
  209. // Form to add or update a price
  210. if (($action == 'add_price' || $action == 'updateprice' ) && ($user->rights->produit->creer || $user->rights->service->creer))
  211. {
  212. $langs->load("suppliers");
  213. if ($rowid)
  214. {
  215. $product->fetch_product_fournisseur_price($rowid);
  216. print_fiche_titre($langs->trans("ChangeSupplierPrice"));
  217. }
  218. else
  219. {
  220. print_fiche_titre($langs->trans("AddSupplierPrice"));
  221. }
  222. print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$product->id.'" method="POST">';
  223. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  224. print '<input type="hidden" name="action" value="updateprice">';
  225. print '<table class="border" width="100%">';
  226. print '<tr><td class="fieldrequired" width="25%">'.$langs->trans("Supplier").'</td><td>';
  227. if ($rowid)
  228. {
  229. $supplier=new Fournisseur($db);
  230. $supplier->fetch($socid);
  231. print $supplier->getNomUrl(1);
  232. print '<input type="hidden" name="id_fourn" value="'.$socid.'">';
  233. print '<input type="hidden" name="ref_fourn" value="'.$product->fourn_ref.'">';
  234. print '<input type="hidden" name="ref_fourn_price_id" value="'.$rowid.'">';
  235. }
  236. else
  237. {
  238. $events=array();
  239. $events[]=array('method' => 'getVatRates', 'url' => dol_buildpath('/core/ajax/vatrates.php',1), 'htmlname' => 'tva_tx', 'params' => array());
  240. print $form->select_company(GETPOST("id_fourn"),'id_fourn','fournisseur=1',1,0,0,$events);
  241. if (is_object($hookmanager))
  242. {
  243. $parameters=array('filtre'=>"fournisseur=1",'html_name'=>'id_fourn','selected'=>GETPOST("id_fourn"),'showempty'=>1,'prod_id'=>$product->id);
  244. $reshook=$hookmanager->executeHooks('formCreateThirdpartyOptions',$parameters,$object,$action);
  245. }
  246. }
  247. print '</td></tr>';
  248. // Ref supplier
  249. print '<tr><td class="fieldrequired">'.$langs->trans("SupplierRef").'</td><td>';
  250. if ($rowid)
  251. {
  252. print $product->fourn_ref;
  253. }
  254. else
  255. {
  256. print '<input class="flat" name="ref_fourn" size="12" value="'.(GETPOST("ref_fourn")?GETPOST("ref_fourn"):'').'">';
  257. }
  258. print '</td>';
  259. print '</tr>';
  260. // Availability
  261. if (! empty($conf->global->FOURN_PRODUCT_AVAILABILITY))
  262. {
  263. $langs->load("propal");
  264. print '<tr><td>'.$langs->trans("Availability").'</td><td>';
  265. $form->select_availability($product->fk_availability,"oselDispo",1);
  266. print '</td></tr>'."\n";
  267. }
  268. // Qty min
  269. print '<tr>';
  270. print '<td class="fieldrequired">'.$langs->trans("QtyMin").'</td>';
  271. print '<td>';
  272. $quantity = GETPOST('qty') ? GETPOST('qty') : "1";
  273. if ($rowid)
  274. {
  275. print '<input type="hidden" name="qty" value="'.$product->fourn_qty.'">';
  276. print $product->fourn_qty;
  277. }
  278. else
  279. {
  280. print '<input class="flat" name="qty" size="5" value="'.$quantity.'">';
  281. }
  282. print '</td></tr>';
  283. // Vat rate
  284. $default_vat='';
  285. // We don't have supplier, so we try to guess.
  286. // For this we build a fictive supplier with same properties than user but using vat)
  287. $mysoc2=dol_clone($mysoc);
  288. $mysoc2->tva_assuj=1;
  289. $default_vat=get_default_tva($mysoc2, $mysoc, 0, $product->id);
  290. print '<tr><td class="fieldrequired">'.$langs->trans("VATRateForSupplierProduct").'</td>';
  291. print '<td>';
  292. //print $form->load_tva('tva_tx',$product->tva_tx,$supplier,$mysoc); // Do not use list here as it may be any vat rates for any country
  293. if (! empty($socid)) // When update
  294. {
  295. $supplierselected=new Societe($db);
  296. $supplierselected->fetch($socid);
  297. $default_vat=get_default_tva($supplier, $mysoc, $product->id);
  298. }
  299. if ($action == 'add_price' && $socid) $default_vat=$product->tva_tx; // If editing product-fourn
  300. print '<input type="text" class="flat" size="5" name="tva_tx" value="'.(GETPOST("tva_tx")?vatrate(GETPOST("tva_tx")):($default_vat!=''?vatrate($default_vat):'')).'">';
  301. print '</td></tr>';
  302. // Price qty min
  303. print '<tr><td class="fieldrequired">'.$langs->trans("PriceQtyMin").'</td>';
  304. print '<td><input class="flat" name="price" size="8" value="'.(GETPOST('price')?price(GETPOST('price')):(isset($product->fourn_price)?price($product->fourn_price):'')).'">';
  305. print '&nbsp;';
  306. print $form->select_PriceBaseType((GETPOST('price_base_type')?GETPOST('price_base_type'):$product->price_base_type), "price_base_type");
  307. print '</td></tr>';
  308. // Discount qty min
  309. print '<tr><td>'.$langs->trans("DiscountQtyMin").'</td>';
  310. print '<td><input class="flat" name="remise_percent" size="4" value="'.(GETPOST('remise_percent')?vatrate(GETPOST('remise_percent')):(isset($product->fourn_remise_percent)?vatrate($product->fourn_remise_percent):'')).'"> %';
  311. print '</td>';
  312. print '</tr>';
  313. // Charges ????
  314. if (! empty($conf->margin->enabled))
  315. {
  316. print '<tr>';
  317. print '<td>'.$langs->trans("Charges").'</td>';
  318. print '<td><input class="flat" name="charges" size="8" value="'.(GETPOST('charges')?price(GETPOST('charges')):(isset($product->fourn_charges)?price($product->fourn_charges):'')).'">';
  319. print '</td>';
  320. print '</tr>';
  321. }
  322. print '</table>';
  323. print '<br><center><input class="button" type="submit" value="'.$langs->trans("Save").'">';
  324. print '&nbsp; &nbsp;';
  325. print '<input class="button" type="submit" name="cancel" value="'.$langs->trans("Cancel").'"></center>';
  326. print '</form>';
  327. }
  328. /* ************************************************************************** */
  329. /* */
  330. /* Barre d'action */
  331. /* */
  332. /* ************************************************************************** */
  333. print "\n<div class=\"tabsAction\">\n";
  334. if ($action != 'add_price' && $action != 'updateprice')
  335. {
  336. if ($user->rights->produit->creer || $user->rights->service->creer)
  337. {
  338. print '<a class="butAction" href="'.DOL_URL_ROOT.'/product/fournisseurs.php?id='.$product->id.'&amp;action=add_price">';
  339. print $langs->trans("AddSupplierPrice").'</a>';
  340. }
  341. }
  342. print "\n</div>\n";
  343. print '<br>';
  344. if ($user->rights->fournisseur->lire)
  345. {
  346. // Suppliers list title
  347. print '<table class="noborder" width="100%">';
  348. if ($product->isproduct()) $nblignefour=4;
  349. else $nblignefour=4;
  350. $param="&id=".$product->id;
  351. print '<tr class="liste_titre">';
  352. print_liste_field_titre($langs->trans("Suppliers"),$_SERVER["PHP_SELF"],"s.nom","",$param,"",$sortfield,$sortorder);
  353. print '<td class="liste_titre">'.$langs->trans("SupplierRef").'</td>';
  354. if (!empty($conf->global->FOURN_PRODUCT_AVAILABILITY)) print_liste_field_titre($langs->trans("Availability"),$_SERVER["PHP_SELF"],"pfp.fk_availability","",$param,"",$sortfield,$sortorder);
  355. print_liste_field_titre($langs->trans("QtyMin"),$_SERVER["PHP_SELF"],"pfp.quantity","",$param,'align="right"',$sortfield,$sortorder);
  356. print '<td class="liste_titre" align="right">'.$langs->trans("VATRate").'</td>';
  357. print '<td class="liste_titre" align="right">'.$langs->trans("PriceQtyMinHT").'</td>';
  358. // Charges ????
  359. if (! empty($conf->margin->enabled)) print '<td align="right">'.$langs->trans("Charges").'</td>';
  360. print_liste_field_titre($langs->trans("UnitPriceHT"),$_SERVER["PHP_SELF"],"pfp.unitprice","",$param,'align="right"',$sortfield,$sortorder);
  361. print '<td class="liste_titre" align="right">'.$langs->trans("DiscountQtyMin").'</td>';
  362. // Charges ????
  363. if (! empty($conf->margin->enabled)) print '<td align="right">'.$langs->trans("UnitCharges").'</td>';
  364. print '<td class="liste_titre"></td>';
  365. print "</tr>\n";
  366. $product_fourn = new ProductFournisseur($db);
  367. $product_fourn_list = $product_fourn->list_product_fournisseur_price($product->id, $sortfield, $sortorder);
  368. if (count($product_fourn_list)>0)
  369. {
  370. $var=true;
  371. foreach($product_fourn_list as $productfourn)
  372. {
  373. $var=!$var;
  374. print "<tr ".$bc[$var].">";
  375. print '<td>'.$productfourn->getSocNomUrl(1,'supplier').'</td>';
  376. // Supplier
  377. print '<td align="left">'.$productfourn->fourn_ref.'</td>';
  378. //Availability
  379. if(!empty($conf->global->FOURN_PRODUCT_AVAILABILITY))
  380. {
  381. $form->load_cache_availability();
  382. $availability= $form->cache_availability[$productfourn->fk_availability]['label'];
  383. print '<td align="left">'.$availability.'</td>';
  384. }
  385. // Quantity
  386. print '<td align="right">';
  387. print $productfourn->fourn_qty;
  388. print '</td>';
  389. // VAT rate
  390. print '<td align="right">';
  391. print vatrate($productfourn->fourn_tva_tx,true);
  392. print '</td>';
  393. // Price for the quantity
  394. print '<td align="right">';
  395. print $productfourn->fourn_price?price($productfourn->fourn_price):"";
  396. print '</td>';
  397. // Charges ????
  398. if (! empty($conf->margin->enabled))
  399. {
  400. print '<td align="right">';
  401. print $productfourn->fourn_charges?price($productfourn->fourn_charges):"";
  402. print '</td>';
  403. }
  404. // Unit price
  405. print '<td align="right">';
  406. print price($productfourn->fourn_unitprice);
  407. //print $objp->unitprice? price($objp->unitprice) : ($objp->quantity?price($objp->price/$objp->quantity):"&nbsp;");
  408. print '</td>';
  409. // Discount
  410. print '<td align="right">';
  411. print price2num($productfourn->fourn_remise_percent).'%';
  412. print '</td>';
  413. // Unit Charges ???
  414. if (! empty($conf->margin->enabled))
  415. {
  416. print '<td align="right">';
  417. print $productfourn->fourn_unitcharges?price($productfourn->fourn_unitcharges) : ($productfourn->fourn_qty?price($productfourn->fourn_charges/$productfourn->fourn_qty):"&nbsp;");
  418. print '</td>';
  419. }
  420. // Modify-Remove
  421. print '<td align="center">';
  422. if ($user->rights->produit->creer || $user->rights->service->creer)
  423. {
  424. print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$product->id.'&amp;socid='.$productfourn->fourn_id.'&amp;action=add_price&amp;rowid='.$productfourn->product_fourn_price_id.'">'.img_edit()."</a>";
  425. print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$product->id.'&amp;socid='.$productfourn->fourn_id.'&amp;action=remove_pf&amp;rowid='.$productfourn->product_fourn_price_id.'">'.img_picto($langs->trans("Remove"),'disable.png').'</a>';
  426. }
  427. print '</td>';
  428. print '</tr>';
  429. }
  430. }
  431. print '</table>';
  432. }
  433. }
  434. }
  435. }
  436. else
  437. {
  438. print $langs->trans("ErrorUnknown");
  439. }
  440. // End of page
  441. llxFooter();
  442. $db->close();
  443. ?>