commande_fournisseur.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. /* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
  7. * Copyright (C) 2023 Gauthier VERDOL <gauthier.verdol@atm-consulting.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 <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/product/stock/stats/commande_fournisseur.php
  24. * \ingroup product service facture
  25. * \brief Page of supplier order statistics for a batch
  26. */
  27. // Load Dolibarr environment
  28. require '../../../main.inc.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  34. // Load translation files required by the page
  35. $langs->loadLangs(array('companies', 'bills', 'products', 'orders'));
  36. $id = GETPOST('id', 'int');
  37. $ref = GETPOST('ref', 'alpha');
  38. $batch = GETPOST('batch', 'alpha');
  39. $objectid = GETPOST('productid', 'int');
  40. // Security check
  41. $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
  42. $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
  43. $socid = '';
  44. if (!empty($user->socid)) {
  45. $socid = $user->socid;
  46. }
  47. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  48. $hookmanager->initHooks(array('batchproductstatssupplierorder'));
  49. $showmessage = GETPOST('showmessage');
  50. // Load variable for pagination
  51. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  52. $sortfield = GETPOST('sortfield', 'aZ09comma');
  53. $sortorder = GETPOST('sortorder', 'aZ09comma');
  54. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  55. if (empty($page) || $page == -1) {
  56. $page = 0;
  57. } // If $page is not defined, or '' or -1
  58. $offset = $limit * $page;
  59. $pageprev = $page - 1;
  60. $pagenext = $page + 1;
  61. if (!$sortorder) {
  62. $sortorder = "DESC";
  63. }
  64. if (!$sortfield) {
  65. $sortfield = "cf.date_commande";
  66. }
  67. $search_month = GETPOST('search_month', 'int');
  68. $search_year = GETPOST('search_year', 'int');
  69. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter', 'alpha')) {
  70. $search_month = '';
  71. $search_year = '';
  72. }
  73. if (!$user->hasRight('produit', 'lire')) accessforbidden();
  74. /*
  75. * View
  76. */
  77. $commandefournisseurstatic = new CommandeFournisseur($db);
  78. $societestatic = new Societe($db);
  79. $form = new Form($db);
  80. $formother = new FormOther($db);
  81. if ($id > 0 || !empty($ref)) {
  82. $object = new Productlot($db);
  83. if ($ref) {
  84. $tmp = explode('_', $ref);
  85. $objectid = $tmp[0];
  86. $batch = $tmp[1];
  87. }
  88. $result = $object->fetch($id, $objectid, $batch);
  89. $parameters = array('id'=>$id);
  90. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  91. if ($reshook < 0) {
  92. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  93. }
  94. $helpurl = '';
  95. $shortlabel = dol_trunc($object->batch, 16);
  96. $title = $langs->trans('Batch')." ".$shortlabel." - ".$langs->trans('Referers');
  97. $helpurl = 'EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
  98. llxHeader('', $title, $helpurl);
  99. if ($result > 0) {
  100. $head = productlot_prepare_head($object);
  101. $titre = $langs->trans("CardProduct".$object->type);
  102. $picto = 'lot';
  103. print dol_get_fiche_head($head, 'referers', $langs->trans("Batch"), -1, $object->picto);
  104. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  105. print $hookmanager->resPrint;
  106. if ($reshook < 0) {
  107. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  108. }
  109. $linkback = '<a href="'.DOL_URL_ROOT.'/product/stock/productlot_list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  110. $shownav = 1;
  111. if ($user->socid && !in_array('product', explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL))) {
  112. $shownav = 0;
  113. }
  114. dol_banner_tab($object, 'id', $linkback, $shownav, 'rowid', 'batch', $morehtmlref);
  115. print '<div class="fichecenter">';
  116. print '<div class="underbanner clearboth"></div>';
  117. print '<table class="border centpercent tableforfield" width="100%">';
  118. // Product
  119. print '<tr><td class="titlefield">'.$langs->trans("Product").'</td><td>';
  120. $producttmp = new Product($db);
  121. $producttmp->fetch($object->fk_product);
  122. print $producttmp->getNomUrl(1, 'stock')." - ".$producttmp->label;
  123. print '</td></tr>';
  124. print "</table>";
  125. echo '<br>';
  126. // // Sell by
  127. // if (empty($conf->global->PRODUCT_DISABLE_SELLBY)) {
  128. // print '<tr><td>';
  129. // print $form->editfieldkey($langs->trans('SellByDate'), 'sellby', $object->sellby, $object, $user->rights->stock->creer, 'datepicker');
  130. // print '</td><td>';
  131. // print $form->editfieldval($langs->trans('SellByDate'), 'sellby', $object->sellby, $object, $user->rights->stock->creer, 'datepicker');
  132. // print '</td>';
  133. // print '</tr>';
  134. // }
  135. //
  136. // // Eat by
  137. // if (empty($conf->global->PRODUCT_DISABLE_EATBY)) {
  138. // print '<tr><td>';
  139. // print $form->editfieldkey($langs->trans('EatByDate'), 'eatby', $object->eatby, $object, $user->rights->stock->creer, 'datepicker');
  140. // print '</td><td>';
  141. // print $form->editfieldval($langs->trans('EatByDate'), 'eatby', $object->eatby, $object, $user->rights->stock->creer, 'datepicker');
  142. // print '</td>';
  143. // print '</tr>';
  144. // }
  145. //
  146. // if (!empty($conf->global->PRODUCT_LOT_ENABLE_TRACEABILITY)) {
  147. // print '<tr><td>'.$form->editfieldkey($langs->trans('ManufacturingDate'), 'manufacturing_date', $object->manufacturing_date, $object, $user->rights->stock->creer).'</td>';
  148. // print '<td>'.$form->editfieldval($langs->trans('ManufacturingDate'), 'manufacturing_date', $object->manufacturing_date, $object, $user->rights->stock->creer, 'datepicker').'</td>';
  149. // print '</tr>';
  150. // // print '<tr><td>'.$form->editfieldkey($langs->trans('FirstUseDate'), 'commissionning_date', $object->commissionning_date, $object, $user->rights->stock->creer).'</td>';
  151. // // print '<td>'.$form->editfieldval($langs->trans('FirstUseDate'), 'commissionning_date', $object->commissionning_date, $object, $user->rights->stock->creer, 'datepicker').'</td>';
  152. // // print '</tr>';
  153. // print '<tr><td>'.$form->editfieldkey($langs->trans('DestructionDate'), 'scrapping_date', $object->scrapping_date, $object, $user->rights->stock->creer).'</td>';
  154. // print '<td>'.$form->editfieldval($langs->trans('DestructionDate'), 'scrapping_date', $object->scrapping_date, $object, $user->rights->stock->creer, 'datepicker').'</td>';
  155. // print '</tr>';
  156. // }
  157. //
  158. // // Quality control
  159. // if (!empty($conf->global->PRODUCT_LOT_ENABLE_QUALITY_CONTROL)) {
  160. // print '<tr><td>'.$form->editfieldkey($langs->trans('EndOfLife'), 'eol_date', $object->eol_date, $object, $user->rights->stock->creer).'</td>';
  161. // print '<td>'.$form->editfieldval($langs->trans('EndOfLife'), 'eol_date', $object->eol_date, $object, $user->rights->stock->creer, 'datepicker').'</td>';
  162. // print '</tr>';
  163. // print '<tr><td>'.$form->editfieldkey($langs->trans('QCFrequency'), 'qc_frequency', $object->qc_frequency, $object, $user->rights->stock->creer).'</td>';
  164. // print '<td>'.$form->editfieldval($langs->trans('QCFrequency'), 'qc_frequency', $object->qc_frequency, $object, $user->rights->stock->creer, 'numeric').'</td>';
  165. // print '</tr>';
  166. // }
  167. //
  168. // // Other attributes
  169. // include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
  170. print '<table class="border centpercent tableforfield" width="100%">';
  171. $nboflines = show_stats_for_batch($object, $socid);
  172. print "</table>";
  173. print '</div>';
  174. print '<div class="clearboth"></div>';
  175. print dol_get_fiche_end();
  176. if ($showmessage && $nboflines > 1) {
  177. print '<span class="opacitymedium">'.$langs->trans("ClinkOnALinkOfColumn", $langs->transnoentitiesnoconv("Referers")).'</span>';
  178. } elseif ($user->hasRight('fournisseur', 'commande', 'lire')) {
  179. $sql = "SELECT DISTINCT s.nom as name, s.rowid as socid, s.code_fournisseur,";
  180. $sql .= " cf.ref, cf.date_commande, cf.date_livraison as delivery_date, cf.fk_statut as statut, cf.rowid as facid,";
  181. $sql .= " cfd.rowid, SUM(cfdi.qty) as qty";
  182. // $sql.= ", cfd.total_ht * SUM(cfdi.qty) / cfd.qty as total_ht_pondere";
  183. if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
  184. $sql .= ", sc.fk_soc, sc.fk_user ";
  185. }
  186. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  187. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."commande_fournisseur as cf ON (cf.fk_soc = s.rowid)";
  188. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."commande_fournisseurdet as cfd ON (cfd.fk_commande = cf.rowid)";
  189. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."commande_fournisseur_dispatch as cfdi ON (cfdi.fk_commandefourndet = cfd.rowid)";
  190. if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
  191. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  192. }
  193. $sql .= " WHERE cf.entity IN (".getEntity('product').")";
  194. $sql .= " AND cfdi.batch = '".($db->escape($object->batch))."'";
  195. if (!empty($search_month)) {
  196. $sql .= ' AND MONTH(cf.date_commande) IN ('.$db->sanitize($search_month).')';
  197. }
  198. if (!empty($search_year)) {
  199. $sql .= ' AND YEAR(cf.date_commande) IN ('.$db->sanitize($search_year).')';
  200. }
  201. if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
  202. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  203. }
  204. if ($socid) {
  205. $sql .= " AND cf.fk_soc = ".((int) $socid);
  206. }
  207. $sql .= " GROUP BY cf.rowid";
  208. $sql .= $db->order($sortfield, $sortorder);
  209. // Calcul total qty and amount for global if full scan list
  210. $total_ht_pondere = 0;
  211. $total_qty = 0;
  212. // Count total nb of records
  213. $totalofrecords = '';
  214. if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
  215. $result = $db->query($sql);
  216. $totalofrecords = $db->num_rows($result);
  217. }
  218. $sql .= $db->plimit($limit + 1, $offset);
  219. $result = $db->query($sql);
  220. if ($result) {
  221. $num = $db->num_rows($result);
  222. $option .= '&id='.$object->id;
  223. if ($limit > 0 && $limit != $conf->liste_limit) {
  224. $option .= '&limit='.((int) $limit);
  225. }
  226. if (!empty($search_month)) {
  227. $option .= '&search_month='.urlencode($search_month);
  228. }
  229. if (!empty($search_year)) {
  230. $option .= '&search_year='.urlencode($search_year);
  231. }
  232. print '<form method="post" action="'.$_SERVER ['PHP_SELF'].'?id='.$object->id.'" name="search_form">'."\n";
  233. print '<input type="hidden" name="token" value="'.newToken().'">';
  234. if (!empty($sortfield)) {
  235. print '<input type="hidden" name="sortfield" value="'.$sortfield.'"/>';
  236. }
  237. if (!empty($sortorder)) {
  238. print '<input type="hidden" name="sortorder" value="'.$sortorder.'"/>';
  239. }
  240. print_barre_liste($langs->trans("SuppliersOrders"), $page, $_SERVER["PHP_SELF"], $option, $sortfield, $sortorder, '', $num, $totalofrecords, '', 0, '', '', $limit, 0, 0, 1);
  241. if (!empty($page)) {
  242. $option .= '&page='.urlencode($page);
  243. }
  244. print '<div class="liste_titre liste_titre_bydiv centpercent">';
  245. print '<div class="divsearchfield">';
  246. print $langs->trans('Period').' ('.$langs->trans("OrderDate").') - ';
  247. print $langs->trans('Month').':<input class="flat" type="text" size="4" name="search_month" value="'.$search_month.'"> ';
  248. print $langs->trans('Year').':'.$formother->selectyear($search_year ? $search_year : - 1, 'search_year', 1, 20, 5);
  249. print '<div style="vertical-align: middle; display: inline-block">';
  250. print '<input type="image" class="liste_titre" name="button_search" src="'.img_picto($langs->trans("Search"), 'search.png', '', '', 1).'" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">';
  251. print '<input type="image" class="liste_titre" name="button_removefilter" src="'.img_picto($langs->trans("Search"), 'searchclear.png', '', '', 1).'" value="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'" title="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'">';
  252. print '</div>';
  253. print '</div>';
  254. print '</div>';
  255. $i = 0;
  256. print '<div class="div-table-responsive">';
  257. print '<table class="tagtable liste listwithfilterbefore" width="100%">';
  258. print '<tr class="liste_titre">';
  259. print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "s.rowid", "", $option, '', $sortfield, $sortorder);
  260. print_liste_field_titre("Company", $_SERVER["PHP_SELF"], "s.nom", "", $option, '', $sortfield, $sortorder);
  261. print_liste_field_titre("SupplierCode", $_SERVER["PHP_SELF"], "s.code_fournisseur", "", $option, '', $sortfield, $sortorder);
  262. print_liste_field_titre("OrderDate", $_SERVER["PHP_SELF"], "cf.date_commande", "", $option, 'align="center"', $sortfield, $sortorder);
  263. print_liste_field_titre("DateDeliveryPlanned", $_SERVER["PHP_SELF"], "cf.date_livraison", "", $option, 'align="center"', $sortfield, $sortorder);
  264. print_liste_field_titre("Qty", $_SERVER["PHP_SELF"], "cfdi.qty", "", $option, 'align="center"', $sortfield, $sortorder);
  265. // print_liste_field_titre("AmountHT", $_SERVER["PHP_SELF"], "total_ht_pondere", "", $option, 'align="right"', $sortfield, $sortorder);
  266. print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "cf.fk_statut", "", $option, 'align="right"', $sortfield, $sortorder);
  267. print "</tr>\n";
  268. if ($num > 0) {
  269. while ($i < min($num, $limit)) {
  270. $objp = $db->fetch_object($result);
  271. if ($objp->type == Facture::TYPE_CREDIT_NOTE) {
  272. $objp->qty = -($objp->qty);
  273. }
  274. // $total_ht_pondere += $objp->total_ht_pondere;
  275. $total_qty += $objp->qty;
  276. $commandefournisseurstatic->id = $objp->facid;
  277. $commandefournisseurstatic->ref = $objp->ref;
  278. $societestatic->fetch($objp->socid);
  279. // $paiement = $commandefournisseurstatic->getSommePaiement();
  280. print '<tr class="oddeven">';
  281. print '<td>';
  282. print $commandefournisseurstatic->getNomUrl(1);
  283. print "</td>\n";
  284. print '<td>'.$societestatic->getNomUrl(1).'</td>';
  285. print "<td>".$objp->code_fournisseur."</td>\n";
  286. print '<td class="center">';
  287. print dol_print_date($db->jdate($objp->date_commande), 'dayhour')."</td>";
  288. print '<td class="center">';
  289. print dol_print_date($db->jdate($objp->delivery_date), 'dayhour')."</td>";
  290. print '<td class="center">'.$objp->qty."</td>\n";
  291. // print '<td align="right">'.price($objp->total_ht_pondere)."</td>\n";
  292. print '<td align="right">'.$commandefournisseurstatic->LibStatut($objp->statut, 5).'</td>';
  293. print "</tr>\n";
  294. $i++;
  295. }
  296. }
  297. print '<tr class="liste_total">';
  298. if ($num < $limit) {
  299. print '<td class="left">'.$langs->trans("Total").'</td>';
  300. } else {
  301. print '<td class="left">'.$langs->trans("Totalforthispage").'</td>';
  302. }
  303. print '<td colspan="2"></td>';
  304. print '<td></td>';
  305. print '<td></td>';
  306. print '<td class="center">'.$total_qty.'</td>';
  307. // print '<td class="right">'.$total_ht_pondere.'</td>';
  308. print '<td></td>';
  309. print "</table>";
  310. print '</div>';
  311. print '</form>';
  312. } else {
  313. dol_print_error($db);
  314. }
  315. $db->free($result);
  316. }
  317. }
  318. } else {
  319. dol_print_error();
  320. }
  321. // End of page
  322. llxFooter();
  323. $db->close();