PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/product/stock/mouvement.php

https://bitbucket.org/speedealing/speedealing
PHP | 494 lines | 321 code | 65 blank | 108 comment | 49 complexity | 4f139fb1353c57012b4afaec22148997 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2001-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com>
  5. * Copyright (C) 2010-2011 Herve Prot <herve.prot@symeos.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/product/stock/mouvement.php
  22. * \ingroup stock
  23. * \brief Page to list stock movements
  24. */
  25. require '../../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/stock.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  33. $langs->load("products");
  34. $langs->load("stocks");
  35. if (!$user->rights->produit->lire) accessforbidden();
  36. $id=GETPOST('id','int');
  37. $product_id=GETPOST("product_id");
  38. $action=GETPOST('action');
  39. $cancel=GETPOST('cancel');
  40. $idproduct = isset($_GET["idproduct"])?$_GET["idproduct"]:$_PRODUCT["idproduct"];
  41. $year = isset($_GET["year"])?$_GET["year"]:$_POST["year"];
  42. $month = isset($_GET["month"])?$_GET["month"]:$_POST["month"];
  43. $search_movement = isset($_REQUEST["search_movement"])?$_REQUEST["search_movement"]:'';
  44. $search_product = isset($_REQUEST["search_product"])?$_REQUEST["search_product"]:'';
  45. $search_warehouse = isset($_REQUEST["search_warehouse"])?$_REQUEST["search_warehouse"]:'';
  46. $search_user = isset($_REQUEST["search_user"])?$_REQUEST["search_user"]:'';
  47. $page = GETPOST("page",'int');
  48. $sortfield = GETPOST("sortfield",'alpha');
  49. $sortorder = GETPOST("sortorder",'alpha');
  50. if ($page < 0) $page = 0;
  51. $offset = $conf->liste_limit * $page;
  52. if (! $sortfield) $sortfield="m.datem";
  53. if (! $sortorder) $sortorder="DESC";
  54. if (GETPOST("button_removefilter"))
  55. {
  56. $year='';
  57. $month='';
  58. $search_movement="";
  59. $search_product="";
  60. $search_warehouse="";
  61. $search_user="";
  62. $sall="";
  63. }
  64. /*
  65. * Actions
  66. */
  67. if ($cancel) $action='';
  68. // Correct stock
  69. if ($action == "correct_stock" && ! $_POST["cancel"])
  70. {
  71. if (is_numeric($_POST["nbpiece"]) && $product_id)
  72. {
  73. $product = new Product($db);
  74. $result=$product->fetch($product_id);
  75. $result=$product->correct_stock(
  76. $user,
  77. $id,
  78. $_POST["nbpiece"],
  79. $_POST["mouvement"],
  80. $_POST["label"],
  81. 0
  82. ); // We do not change value of stock for a correction
  83. if ($result > 0)
  84. {
  85. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
  86. exit;
  87. }
  88. }
  89. else $action='';
  90. }
  91. /*
  92. * View
  93. */
  94. $productstatic=new Product($db);
  95. $warehousestatic=new Entrepot($db);
  96. $userstatic=new User($db);
  97. $form=new Form($db);
  98. $formother=new FormOther($db);
  99. $formproduct=new FormProduct($db);
  100. $sql = "SELECT p.rowid, p.label as produit, p.fk_product_type as type,";
  101. $sql.= " e.label as stock, e.rowid as entrepot_id,";
  102. $sql.= " m.rowid as mid, m.value, m.datem, m.fk_user_author, m.label,";
  103. $sql.= " u.login";
  104. $sql.= " FROM (".MAIN_DB_PREFIX."entrepot as e,";
  105. $sql.= " ".MAIN_DB_PREFIX."product as p,";
  106. $sql.= " ".MAIN_DB_PREFIX."stock_mouvement as m)";
  107. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON m.fk_user_author = u.rowid";
  108. $sql.= " WHERE m.fk_product = p.rowid";
  109. $sql.= " AND m.fk_entrepot = e.rowid";
  110. $sql.= " AND e.entity = ".$conf->entity;
  111. if (empty($conf->global->STOCK_SUPPORTS_SERVICES)) $sql.= " AND p.fk_product_type = 0";
  112. if ($id)
  113. {
  114. $sql.= " AND e.rowid ='".$id."'";
  115. }
  116. if ($month > 0)
  117. {
  118. if ($year > 0)
  119. $sql.= " AND m.datem BETWEEN '".$db->idate(dol_get_first_day($year,$month,false))."' AND '".$db->idate(dol_get_last_day($year,$month,false))."'";
  120. else
  121. $sql.= " AND date_format(m.datem, '%m') = '$month'";
  122. }
  123. else if ($year > 0)
  124. {
  125. $sql.= " AND m.datem BETWEEN '".$db->idate(dol_get_first_day($year,1,false))."' AND '".$db->idate(dol_get_last_day($year,12,false))."'";
  126. }
  127. if (! empty($search_movement))
  128. {
  129. $sql.= " AND m.label LIKE '%".$db->escape($search_movement)."%'";
  130. }
  131. if (! empty($search_product))
  132. {
  133. $sql.= " AND p.label LIKE '%".$db->escape($search_product)."%'";
  134. }
  135. if (! empty($search_warehouse))
  136. {
  137. $sql.= " AND e.label LIKE '%".$db->escape($search_warehouse)."%'";
  138. }
  139. if (! empty($search_user))
  140. {
  141. $sql.= " AND u.login LIKE '%".$db->escape($search_user)."%'";
  142. }
  143. if (! empty($_GET['idproduct']))
  144. {
  145. $sql.= " AND p.rowid = '".$idproduct."'";
  146. }
  147. $sql.= $db->order($sortfield,$sortorder);
  148. $sql.= $db->plimit($conf->liste_limit+1, $offset);
  149. //print $sql;
  150. $resql = $db->query($sql);
  151. if ($resql)
  152. {
  153. $num = $db->num_rows($resql);
  154. if ($idproduct)
  155. {
  156. $product = new Product($db);
  157. $product->fetch($idproduct);
  158. }
  159. if ($_GET["id"])
  160. {
  161. $entrepot = new Entrepot($db);
  162. $result = $entrepot->fetch($id);
  163. if ($result < 0)
  164. {
  165. dol_print_error($db);
  166. }
  167. }
  168. $i = 0;
  169. $texte = $langs->trans("ListOfStockMovements");
  170. llxHeader("",$texte);
  171. /*
  172. * Show tab only if we ask a particular warehouse
  173. */
  174. if ($id)
  175. {
  176. $head = stock_prepare_head($entrepot);
  177. dol_fiche_head($head, 'movements', $langs->trans("Warehouse"), 0, 'stock');
  178. print '<table class="border" width="100%">';
  179. $linkback = '<a href="'.DOL_URL_ROOT.'/adherents/liste.php">'.$langs->trans("BackToList").'</a>';
  180. // Ref
  181. print '<tr><td width="25%">'.$langs->trans("Ref").'</td><td colspan="3">';
  182. print $form->showrefnav($entrepot, 'id', $linkback, 1, 'rowid', 'libelle');
  183. print '</td>';
  184. print '<tr><td>'.$langs->trans("LocationSummary").'</td><td colspan="3">'.$entrepot->lieu.'</td></tr>';
  185. // Description
  186. print '<tr><td valign="top">'.$langs->trans("Description").'</td><td colspan="3">'.dol_htmlentitiesbr($entrepot->description).'</td></tr>';
  187. // Address
  188. print '<tr><td>'.$langs->trans('Address').'</td><td colspan="3">';
  189. print $entrepot->address;
  190. print '</td></tr>';
  191. // Town
  192. print '<tr><td width="25%">'.$langs->trans('Zip').'</td><td width="25%">'.$entrepot->zip.'</td>';
  193. print '<td width="25%">'.$langs->trans('Town').'</td><td width="25%">'.$entrepot->town.'</td></tr>';
  194. // Country
  195. print '<tr><td>'.$langs->trans('Country').'</td><td colspan="3">';
  196. $img=picto_from_langcode($entrepot->country_code);
  197. print ($img?$img.' ':'');
  198. print $entrepot->country;
  199. print '</td></tr>';
  200. // Status
  201. print '<tr><td>'.$langs->trans("Status").'</td><td colspan="3">'.$entrepot->getLibStatut(4).'</td></tr>';
  202. $calcproducts=$entrepot->nb_products();
  203. // Nb of products
  204. print '<tr><td valign="top">'.$langs->trans("NumberOfProducts").'</td><td colspan="3">';
  205. print empty($calcproducts['nb'])?'0':$calcproducts['nb'];
  206. print "</td></tr>";
  207. // Value
  208. print '<tr><td valign="top">'.$langs->trans("EstimatedStockValueShort").'</td><td colspan="3">';
  209. print empty($calcproducts['value'])?'0':$calcproducts['value'];
  210. print "</td></tr>";
  211. // Last movement
  212. $sql = "SELECT MAX(m.datem) as datem";
  213. $sql .= " FROM ".MAIN_DB_PREFIX."stock_mouvement as m";
  214. $sql .= " WHERE m.fk_entrepot = '".$entrepot->id."'";
  215. $resqlbis = $db->query($sql);
  216. if ($resqlbis)
  217. {
  218. $obj = $db->fetch_object($resqlbis);
  219. $lastmovementdate=$db->jdate($obj->datem);
  220. }
  221. else
  222. {
  223. dol_print_error($db);
  224. }
  225. print '<tr><td valign="top">'.$langs->trans("LastMovement").'</td><td colspan="3">';
  226. if ($lastmovementdate)
  227. {
  228. print dol_print_date($lastmovementdate,'dayhour');
  229. }
  230. else
  231. {
  232. print $langs->trans("None");
  233. }
  234. print "</td></tr>";
  235. print "</table>";
  236. print '</div>';
  237. }
  238. /*
  239. * Correct stock
  240. */
  241. if ($action == "correction")
  242. {
  243. print_titre($langs->trans("StockCorrection"));
  244. print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" method="post">'."\n";
  245. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  246. print '<input type="hidden" name="action" value="correct_stock">';
  247. print '<table class="border" width="100%">';
  248. // Warehouse
  249. print '<tr>';
  250. print '<td width="20%">'.$langs->trans("Product").'</td>';
  251. print '<td width="20%">';
  252. print $form->select_produits(GETPOST('productid'),'product_id',(empty($conf->global->STOCK_SUPPORTS_SERVICES)?'0':''));
  253. print '</td>';
  254. print '<td width="20%">';
  255. print '<select name="mouvement" class="flat">';
  256. print '<option value="0">'.$langs->trans("Add").'</option>';
  257. print '<option value="1">'.$langs->trans("Delete").'</option>';
  258. print '</select></td>';
  259. print '<td width="20%">'.$langs->trans("NumberOfUnit").'</td><td width="20%"><input class="flat" name="nbpiece" size="10" value=""></td>';
  260. print '</tr>';
  261. // Label
  262. print '<tr>';
  263. print '<td width="20%">'.$langs->trans("Label").'</td>';
  264. print '<td colspan="4">';
  265. print '<input type="text" name="label" size="40" value="">';
  266. print '</td>';
  267. print '</tr>';
  268. print '</table>';
  269. print '<center><input type="submit" class="button" value="'.$langs->trans('Save').'">&nbsp;';
  270. print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'"></center>';
  271. print '</form>';
  272. }
  273. /*
  274. * Transfer of units
  275. */
  276. /*
  277. if ($action == "transfert")
  278. {
  279. print_titre($langs->trans("Transfer"));
  280. print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" method="post">'."\n";
  281. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  282. print '<input type="hidden" name="action" value="transfert_stock">';
  283. print '<table class="border" width="100%">';
  284. print '<tr>';
  285. print '<td width="20%">'.$langs->trans("Product").'</td>';
  286. print '<td width="20%">';
  287. print $form->select_produits(GETPOST('productid'),'product_id');
  288. print '</td>';
  289. print '<td width="20%">'.$langs->trans("WarehouseTarget").'</td><td width="20%">';
  290. print $formproduct->selectWarehouses('','id_entrepot_destination','',1);
  291. print '</td>';
  292. print '<td width="20%">'.$langs->trans("NumberOfUnit").'</td><td width="20%"><input name="nbpiece" size="10" value=""></td>';
  293. print '</tr>';
  294. // Label
  295. print '<tr>';
  296. print '<td width="20%">'.$langs->trans("Label").'</td>';
  297. print '<td colspan="5">';
  298. print '<input type="text" name="label" size="40" value="">';
  299. print '</td>';
  300. print '</tr>';
  301. print '</table>';
  302. print '<center><input type="submit" class="button" value="'.$langs->trans('Save').'">&nbsp;';
  303. print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'"></center>';
  304. print '</form>';
  305. }
  306. */
  307. /* ************************************************************************** */
  308. /* */
  309. /* Barre d'action */
  310. /* */
  311. /* ************************************************************************** */
  312. if (empty($action) && $id)
  313. {
  314. print "<div class=\"tabsAction\">\n";
  315. if ($user->rights->stock->creer)
  316. {
  317. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$id.'&action=correction">'.$langs->trans("StockCorrection").'</a>';
  318. }
  319. /*if ($user->rights->stock->mouvement->creer)
  320. {
  321. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$id.'&action=transfert">'.$langs->trans("StockMovement").'</a>';
  322. }*/
  323. print '</div><br>';
  324. }
  325. $param='';
  326. if ($id) $param.='&id='.$id;
  327. if ($search_movement) $param.='&search_movement='.urlencode($search_movement);
  328. if ($search_product) $param.='&search_product='.urlencode($search_product);
  329. if ($search_warehouse) $param.='&search_warehouse='.urlencode($search_warehouse);
  330. if ($sref) $param.='&sref='.urlencode($sref);
  331. if ($snom) $param.='&snom='.urlencode($snom);
  332. if ($search_user) $param.='&search_user='.urlencode($search_user);
  333. if ($idproduct > 0) $param.='&idproduct='.$idproduct;
  334. if ($id) print_barre_liste($texte, $page, "mouvement.php", $param, $sortfield, $sortorder,'',$num,0,'');
  335. else print_barre_liste($texte, $page, "mouvement.php", $param, $sortfield, $sortorder,'',$num);
  336. print '<table class="noborder" width="100%">';
  337. print "<tr class=\"liste_titre\">";
  338. //print_liste_field_titre($langs->trans("Id"),$_SERVER["PHP_SELF"], "m.rowid","",$param,"",$sortfield,$sortorder);
  339. print_liste_field_titre($langs->trans("Date"),$_SERVER["PHP_SELF"], "m.datem","",$param,"",$sortfield,$sortorder);
  340. print_liste_field_titre($langs->trans("Label"),$_SERVER["PHP_SELF"], "m.label","",$param,"",$sortfield,$sortorder);
  341. print_liste_field_titre($langs->trans("Product"),$_SERVER["PHP_SELF"], "p.ref","",$param,"",$sortfield,$sortorder);
  342. print_liste_field_titre($langs->trans("Warehouse"),$_SERVER["PHP_SELF"], "e.label","",$param,"",$sortfield,$sortorder);
  343. print_liste_field_titre($langs->trans("Author"),$_SERVER["PHP_SELF"], "m.fk_user_author","",$param,"",$sortfield,$sortorder);
  344. print_liste_field_titre($langs->trans("Units"),$_SERVER["PHP_SELF"], "m.value","",$param,'align="right"',$sortfield,$sortorder);
  345. print "</tr>\n";
  346. // Lignes des champs de filtre
  347. print '<form method="get" action="'.$_SERVER["PHP_SELF"].'">';
  348. if ($id) print '<input type="hidden" name="id" value="'.$id.'">';
  349. print '<tr class="liste_titre">';
  350. print '<td class="liste_titre" valign="right">';
  351. print $langs->trans('Month').': <input class="flat" type="text" size="2" maxlength="2" name="month" value="'.$month.'">';
  352. print '&nbsp;'.$langs->trans('Year').': ';
  353. $syear = GETPOST('year')?GETPOST('year'):-1;
  354. $formother->select_year($syear,'year',1, 20, 5);
  355. print '</td>';
  356. // Label of movement
  357. print '<td class="liste_titre" align="left">';
  358. print '<input class="flat" type="text" size="12" name="search_movement" value="'.$search_movement.'">';
  359. print '</td>';
  360. // Product
  361. print '<td class="liste_titre" align="left">';
  362. print '<input class="flat" type="text" size="12" name="search_product" value="'.($idproduct?$product->libelle:$search_product).'">';
  363. print '</td>';
  364. print '<td class="liste_titre" align="left">';
  365. print '<input class="flat" type="text" size="10" name="search_warehouse" value="'.($search_warehouse).'">';
  366. print '</td>';
  367. print '<td class="liste_titre" align="left">';
  368. print '<input class="flat" type="text" size="6" name="search_user" value="'.($search_user).'">';
  369. print '</td>';
  370. print '<td class="liste_titre" align="right">';
  371. print '<input type="image" class="liste_titre" src="'.DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/search.png" name="button_search" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">';
  372. print '&nbsp; ';
  373. print '<input type="image" class="liste_titre" src="'.DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/searchclear.png" name="button_removefilter" value="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'" title="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'">';
  374. print '</td>';
  375. print "</tr>\n";
  376. print '</form>';
  377. $var=True;
  378. while ($i < min($num,$conf->liste_limit))
  379. {
  380. $objp = $db->fetch_object($resql);
  381. $var=!$var;
  382. print "<tr $bc[$var]>";
  383. // Id movement
  384. //print '<td>'.$objp->mid.'</td>'; // This is primary not movement id
  385. // Date
  386. print '<td>'.dol_print_date($db->jdate($objp->datem),'dayhour').'</td>';
  387. // Label of movement
  388. print '<td>'.$objp->label.'</td>';
  389. // Product
  390. print '<td>';
  391. $productstatic->id=$objp->rowid;
  392. $productstatic->ref=$objp->produit;
  393. $productstatic->type=$objp->type;
  394. print $productstatic->getNomUrl(1,'',16);
  395. print "</td>\n";
  396. // Warehouse
  397. print '<td>';
  398. $warehousestatic->id=$objp->entrepot_id;
  399. $warehousestatic->libelle=$objp->stock;
  400. print $warehousestatic->getNomUrl(1);
  401. print "</td>\n";
  402. // Author
  403. print '<td>';
  404. $userstatic->id=$objp->fk_user_author;
  405. $userstatic->lastname=$objp->login;
  406. print $userstatic->getNomUrl(1);
  407. print "</td>\n";
  408. // Value
  409. print '<td align="right">';
  410. if ($objp->value > 0) print '+';
  411. print $objp->value.'</td>';
  412. print "</tr>\n";
  413. $i++;
  414. }
  415. $db->free($resql);
  416. print "</table>";
  417. }
  418. else
  419. {
  420. dol_print_error($db);
  421. }
  422. llxFooter();
  423. $db->close();
  424. ?>