orderstoinvoice.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. <?php
  2. /* Copyright (C) 2001-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
  6. * Copyright (C) 2012 Andreu Bisquerra Gaya <jove@bisquerra.com>
  7. * Copyright (C) 2012 David Rodriguez Martinez <davidrm146@gmail.com>
  8. * Copyright (C) 2012 Juanjo Menent <jmenent@2byte.es>
  9. * Copyright (C) 2015 Ferran Marcet <fmarcet@2byte.es>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. /**
  25. * \file htdocs/commande/orderstoinvoice.php
  26. * \ingroup commande
  27. * \brief Page to invoice multiple orders
  28. */
  29. require '../main.inc.php';
  30. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  35. require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
  36. if (! empty($conf->projet->enabled)) {
  37. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
  38. }
  39. $langs->load('orders');
  40. $langs->load('deliveries');
  41. $langs->load('companies');
  42. if (! $user->rights->facture->creer)
  43. accessforbidden();
  44. $id = (GETPOST('id')?GETPOST('id','int'):GETPOST("facid")); // For backward compatibility
  45. $ref = GETPOST('ref','alpha');
  46. $action = GETPOST('action','alpha');
  47. $confirm = GETPOST('confirm','alpha');
  48. $sref = GETPOST('sref');
  49. $sref_client = GETPOST('sref_client');
  50. $sall = GETPOST('sall');
  51. $socid = GETPOST('socid','int');
  52. $selected = GETPOST('orders_to_invoice');
  53. $sortfield = GETPOST("sortfield",'alpha');
  54. $sortorder = GETPOST("sortorder",'alpha');
  55. $viewstatut = GETPOST('viewstatut');
  56. $error = 0;
  57. if (! $sortfield) $sortfield='c.rowid';
  58. if (! $sortorder) $sortorder='DESC';
  59. $now = dol_now();
  60. $date_start = dol_mktime(0,0,0,$_REQUEST["date_startmonth"],$_REQUEST["date_startday"],$_REQUEST["date_startyear"]); // Date for local PHP server
  61. $date_end = dol_mktime(23,59,59,$_REQUEST["date_endmonth"],$_REQUEST["date_endday"],$_REQUEST["date_endyear"]);
  62. $date_starty = dol_mktime(0,0,0,$_REQUEST["date_start_delymonth"],$_REQUEST["date_start_delyday"],$_REQUEST["date_start_delyyear"]); // Date for local PHP server
  63. $date_endy = dol_mktime(23,59,59,$_REQUEST["date_end_delymonth"],$_REQUEST["date_end_delyday"],$_REQUEST["date_end_delyyear"]);
  64. if ($action == 'create')
  65. {
  66. if (! is_array($selected))
  67. {
  68. $error++;
  69. setEventMessages($langs->trans('Error_OrderNotChecked'), null, 'errors');
  70. }
  71. else
  72. {
  73. $origin = GETPOST('origin');
  74. $originid = GETPOST('originid');
  75. }
  76. }
  77. // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
  78. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  79. $hookmanager=new HookManager($db);
  80. $hookmanager->initHooks(array('orderstoinvoice'));
  81. /*
  82. * Actions
  83. */
  84. if (($action == 'create' || $action == 'add') && !$error)
  85. {
  86. require_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
  87. require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
  88. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  89. require_once DOL_DOCUMENT_ROOT.'/core/lib/invoice.lib.php';
  90. if (! empty($conf->projet->enabled)) require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  91. $langs->load('bills');
  92. $langs->load('products');
  93. $langs->load('main');
  94. if (isset($_GET['orders_to_invoice']))
  95. {
  96. $orders_id = $_GET['orders_to_invoice'];
  97. $n = count($orders_id);
  98. $i = 0;
  99. $originid=$orders_id[0];
  100. $_GET['originid']=$orders_id[0];
  101. }
  102. if (isset($_POST['orders_to_invoice']))
  103. {
  104. $orders_id = $_POST['orders_to_invoice'];
  105. $nn = count($orders_id);
  106. $ii = 0;
  107. $originid=$orders_id[0];
  108. $_POST['originid']=$orders_id[0];
  109. }
  110. $projectid = GETPOST('projectid','int')?GETPOST('projectid','int'):0;
  111. $lineid = GETPOST('lineid','int');
  112. $userid = GETPOST('userid','int');
  113. $search_ref = GETPOST('sf_ref')?GETPOST('sf_ref'):GETPOST('search_ref');
  114. $closeOrders = GETPOST('autocloseorders') ? true : false;
  115. // Security check
  116. $fieldid = GETPOST('ref','alpha')?'facnumber':'rowid';
  117. if ($user->societe_id) $socid=$user->societe_id;
  118. $result = restrictedArea($user, 'facture', $id,'','','fk_soc',$fieldid);
  119. $usehm=$conf->global->MAIN_USE_HOURMIN_IN_DATE_RANGE;
  120. $object=new Facture($db);
  121. // Insert new invoice in database
  122. if ($action == 'add' && $user->rights->facture->creer)
  123. {
  124. $object->socid=GETPOST('socid');
  125. $db->begin();
  126. $error=0;
  127. // Standard or deposit or proforma invoice
  128. if ($_POST['type'] == 0 )
  129. {
  130. $datefacture = dol_mktime(12, 0, 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']);
  131. if (empty($datefacture))
  132. {
  133. $datefacture = dol_mktime(date("h"), date("M"), 0, date("m"), date("d"), date("Y"));
  134. }
  135. if (! $error)
  136. {
  137. // Si facture standard
  138. $object->socid = $_POST['socid'];
  139. $object->type = $_POST['type'];
  140. $object->number = $_POST['facnumber'];
  141. $object->date = $datefacture;
  142. $object->note_public = trim($_POST['note_public']);
  143. $object->note = trim($_POST['note']);
  144. $object->ref_client = $_POST['ref_client'];
  145. $object->ref_int = $_POST['ref_int'];
  146. $object->modelpdf = $_POST['model'];
  147. $object->fk_project = $_POST['projectid'];
  148. $object->cond_reglement_id = ($_POST['type'] == 3?1:$_POST['cond_reglement_id']);
  149. $object->mode_reglement_id = $_POST['mode_reglement_id'];
  150. $object->amount = $_POST['amount'];
  151. $object->remise_absolue = $_POST['remise_absolue'];
  152. $object->remise_percent = $_POST['remise_percent'];
  153. if ($_POST['origin'] && $_POST['originid'])
  154. {
  155. $object->origin = $_POST['origin'];
  156. $object->origin_id = $orders_id[$ii];
  157. $object->linked_objects = $orders_id;
  158. $id = $object->create($user);
  159. $object->fetch_thirdparty();
  160. if ($id>0)
  161. {
  162. foreach($orders_id as $origin => $origin_id)
  163. {
  164. $origin_id = (! empty($origin_id) ? $origin_id : $object->origin_id);
  165. $db->begin();
  166. $sql = "INSERT INTO ".MAIN_DB_PREFIX."element_element (";
  167. $sql.= "fk_source";
  168. $sql.= ", sourcetype";
  169. $sql.= ", fk_target";
  170. $sql.= ", targettype";
  171. $sql.= ") VALUES (";
  172. $sql.= $origin_id;
  173. $sql.= ", '".$object->origin."'";
  174. $sql.= ", ".$id;
  175. $sql.= ", '".$object->element."'";
  176. $sql.= ")";
  177. if ($db->query($sql))
  178. {
  179. $db->commit();
  180. }
  181. else
  182. {
  183. $db->rollback();
  184. }
  185. }
  186. while ($ii < $nn)
  187. {
  188. include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  189. $objectsrc = new Commande($db);
  190. dol_syslog("Try to find source object origin=".$object->origin." originid=".$object->origin_id." to add lines");
  191. $result=$objectsrc->fetch($orders_id[$ii]);
  192. if ($result > 0)
  193. {
  194. if ($closeOrders)
  195. {
  196. $objectsrc->classifyBilled($user);
  197. $objectsrc->setStatut(3);
  198. }
  199. $lines = $objectsrc->lines;
  200. if (empty($lines) && method_exists($objectsrc, 'fetch_lines'))
  201. {
  202. $objectsrc->fetch_lines();
  203. $lines = $objectsrc->lines;
  204. }
  205. $fk_parent_line=0;
  206. $num=count($lines);
  207. for ($i=0;$i<$num;$i++)
  208. {
  209. $desc=($lines[$i]->desc?$lines[$i]->desc:$lines[$i]->libelle);
  210. if ($lines[$i]->subprice < 0)
  211. {
  212. // Negative line, we create a discount line
  213. $discount = new DiscountAbsolute($db);
  214. $discount->fk_soc=$object->socid;
  215. $discount->amount_ht=abs($lines[$i]->total_ht);
  216. $discount->amount_tva=abs($lines[$i]->total_tva);
  217. $discount->amount_ttc=abs($lines[$i]->total_ttc);
  218. $discount->tva_tx=$lines[$i]->tva_tx;
  219. $discount->fk_user=$user->id;
  220. $discount->description=$desc;
  221. $discountid=$discount->create($user);
  222. if ($discountid > 0)
  223. {
  224. $result=$object->insert_discount($discountid);
  225. //$result=$discount->link_to_invoice($lineid,$id);
  226. }
  227. else
  228. {
  229. setEventMessages($discount->error, $discount->errors, 'errors');
  230. $error++;
  231. break;
  232. }
  233. }
  234. else
  235. {
  236. // Positive line
  237. $product_type=($lines[$i]->product_type?$lines[$i]->product_type:0);
  238. // Date start
  239. $date_start=false;
  240. if ($lines[$i]->date_debut_prevue) $date_start=$lines[$i]->date_debut_prevue;
  241. if ($lines[$i]->date_debut_reel) $date_start=$lines[$i]->date_debut_reel;
  242. if ($lines[$i]->date_start) $date_start=$lines[$i]->date_start;
  243. //Date end
  244. $date_end=false;
  245. if ($lines[$i]->date_fin_prevue) $date_end=$lines[$i]->date_fin_prevue;
  246. if ($lines[$i]->date_fin_reel) $date_end=$lines[$i]->date_fin_reel;
  247. if ($lines[$i]->date_end) $date_end=$lines[$i]->date_end;
  248. // Reset fk_parent_line for no child products and special product
  249. if (($lines[$i]->product_type != 9 && empty($lines[$i]->fk_parent_line)) || $lines[$i]->product_type == 9)
  250. {
  251. $fk_parent_line = 0;
  252. }
  253. $result = $object->addline(
  254. $desc,
  255. $lines[$i]->subprice,
  256. $lines[$i]->qty,
  257. $lines[$i]->tva_tx,
  258. $lines[$i]->localtax1_tx,
  259. $lines[$i]->localtax2_tx,
  260. $lines[$i]->fk_product,
  261. $lines[$i]->remise_percent,
  262. $date_start,
  263. $date_end,
  264. 0,
  265. $lines[$i]->info_bits,
  266. $lines[$i]->fk_remise_except,
  267. 'HT',
  268. 0,
  269. $product_type,
  270. $ii,
  271. $lines[$i]->special_code,
  272. $object->origin,
  273. $lines[$i]->rowid,
  274. $fk_parent_line,
  275. $lines[$i]->fk_fournprice,
  276. $lines[$i]->pa_ht,
  277. $lines[$i]->label
  278. );
  279. if ($result > 0)
  280. {
  281. $lineid=$result;
  282. }
  283. else
  284. {
  285. $lineid=0;
  286. $error++;
  287. break;
  288. }
  289. // Defined the new fk_parent_line
  290. if ($result > 0 && $lines[$i]->product_type == 9)
  291. {
  292. $fk_parent_line = $result;
  293. }
  294. }
  295. }
  296. }
  297. else
  298. {
  299. setEventMessages($objectsrc->error, $objectsrc->errors, 'errors');
  300. $error++;
  301. }
  302. $ii++;
  303. }
  304. }
  305. else
  306. {
  307. setEventMessages($object->error, $object->errors, 'errors');
  308. $error++;
  309. }
  310. }
  311. }
  312. }
  313. // End of object creation, we show it
  314. if ($id > 0 && ! $error)
  315. {
  316. $db->commit();
  317. header('Location: '.DOL_URL_ROOT.'/compta/facture.php?facid='.$id);
  318. exit;
  319. }
  320. else
  321. {
  322. $db->rollback();
  323. $action='create';
  324. $_GET["origin"]=$_POST["origin"];
  325. $_GET["originid"]=$_POST["originid"];
  326. setEventMessages($object->error, $object->errors, 'errors');
  327. $error++;
  328. }
  329. }
  330. }
  331. /*
  332. * View
  333. */
  334. $html = new Form($db);
  335. $htmlother = new FormOther($db);
  336. $formfile = new FormFile($db);
  337. $companystatic = new Societe($db);
  338. // Mode creation
  339. if ($action == 'create' && !$error)
  340. {
  341. $facturestatic=new Facture($db);
  342. llxHeader();
  343. print load_fiche_titre($langs->trans('NewBill'));
  344. $soc = new Societe($db);
  345. if ($socid) $res=$soc->fetch($socid);
  346. if ($res)
  347. {
  348. $cond_reglement_id = $soc->cond_reglement_id;
  349. $mode_reglement_id = $soc->mode_reglement_id;
  350. $remise_percent = $soc->remise_percent;
  351. }
  352. $remise_absolue = 0;
  353. $dateinvoice = empty($conf->global->MAIN_AUTOFILL_DATE)?-1:'';
  354. $absolute_discount=$soc->getAvailableDiscounts();
  355. print '<form name="add" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  356. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  357. print '<input type="hidden" name="action" value="add">';
  358. print '<input type="hidden" name="socid" value="'.$soc->id.'">' ."\n";
  359. print '<input name="facnumber" type="hidden" value="provisoire">';
  360. print '<input name="ref_client" type="hidden" value="'.$ref_client.'">';
  361. print '<input name="ref_int" type="hidden" value="'.$ref_int.'">';
  362. print '<input type="hidden" name="origin" value="'.GETPOST('origin').'">';
  363. print '<input type="hidden" name="originid" value="'.GETPOST('originid').'">';
  364. print '<input type="hidden" name="autocloseorders" value="'.GETPOST('autocloseorders').'">';
  365. dol_fiche_head();
  366. print '<table class="border" width="100%">';
  367. // Ref
  368. print '<tr><td class="fieldrequired">'.$langs->trans('Ref').'</td><td colspan="2">'.$langs->trans('Draft').'</td></tr>';
  369. // Third party
  370. print '<tr><td class="fieldrequired">'.$langs->trans('Customer').'</td><td colspan="2">';
  371. print $soc->getNomUrl(1);
  372. print '<input type="hidden" name="socid" value="'.$soc->id.'">';
  373. print '</td>';
  374. print '</tr>'."\n";
  375. // Type
  376. print '<tr><td valign="top" class="fieldrequired">'.$langs->trans('Type').'</td><td colspan="2">';
  377. print '<table class="nobordernopadding">'."\n";
  378. // Standard invoice
  379. print '<tr height="18"><td width="16px" valign="middle">';
  380. print '<input type="radio" name="type" value="0"'.(GETPOST('type')==0?' checked':'').'>';
  381. print '</td><td valign="middle">';
  382. $desc=$html->textwithpicto($langs->trans("InvoiceStandardAsk"),$langs->transnoentities("InvoiceStandardDesc"),1);
  383. print $desc;
  384. print '</td></tr>'."\n";
  385. print '</table>';
  386. // Date invoice
  387. print '<tr><td class="fieldrequired">'.$langs->trans('Date').'</td><td colspan="2">';
  388. $html->select_date('','','','','',"add",1,1);
  389. print '</td></tr>';
  390. // Payment term
  391. print '<tr><td class="nowrap">'.$langs->trans('PaymentConditionsShort').'</td><td colspan="2">';
  392. $html->select_conditions_paiements(isset($_POST['cond_reglement_id'])?$_POST['cond_reglement_id']:$cond_reglement_id,'cond_reglement_id');
  393. print '</td></tr>';
  394. // Payment mode
  395. print '<tr><td>'.$langs->trans('PaymentMode').'</td><td colspan="2">';
  396. $html->select_types_paiements(isset($_POST['mode_reglement_id'])?$_POST['mode_reglement_id']:$mode_reglement_id,'mode_reglement_id');
  397. print '</td></tr>';
  398. // Project
  399. if (! empty($conf->projet->enabled))
  400. {
  401. $formproject=new FormProjets($db);
  402. $langs->load('projects');
  403. print '<tr><td>'.$langs->trans('Project').'</td><td colspan="2">';
  404. $formproject->select_projects($soc->id, $projectid, 'projectid');
  405. print '</td></tr>';
  406. }
  407. include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  408. $objectsrc = new Commande($db);
  409. $listoforders = array();
  410. foreach ($selected as $sel)
  411. {
  412. $result=$objectsrc->fetch($sel);
  413. if ($result > 0)
  414. {
  415. $listoforders[] = $objectsrc->ref;
  416. }
  417. }
  418. // Other attributes
  419. $parameters=array('objectsrc' => $objectsrc, 'idsrc' => $listoforders, 'colspan' => ' colspan="3"');
  420. $reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
  421. // Modele PDF
  422. print '<tr><td>'.$langs->trans('Model').'</td>';
  423. print '<td>';
  424. include_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
  425. $liste=ModelePDFFactures::liste_modeles($db);
  426. print $html->selectarray('model',$liste,$conf->global->FACTURE_ADDON_PDF);
  427. print "</td></tr>";
  428. // Public note
  429. print '<tr>';
  430. print '<td class="border" valign="top">'.$langs->trans('NotePublic').'</td>';
  431. print '<td valign="top" colspan="2">';
  432. print '<textarea name="note_public" wrap="soft" cols="70" rows="'.ROWS_3.'">';
  433. print $langs->trans("Orders").": ".implode(', ', $listoforders);
  434. print '</textarea></td></tr>';
  435. // Private note
  436. if (empty($user->societe_id))
  437. {
  438. print '<tr>';
  439. print '<td class="border" valign="top">'.$langs->trans('NotePrivate').'</td>';
  440. print '<td valign="top" colspan="2">';
  441. print '<textarea name="note" wrap="soft" cols="70" rows="'.ROWS_3.'">';
  442. print '</textarea></td></tr>';
  443. }
  444. print '</table>';
  445. while ($i < $n)
  446. {
  447. print '<input type="hidden" name="orders_to_invoice[]" value="'.$orders_id[$i].'">';
  448. $i++;
  449. }
  450. dol_fiche_end();
  451. // Button "Create Draft"
  452. print '<div class="center"><input type="submit" class="button" name="bouton" value="'.$langs->trans('CreateDraft').'" /></div>';
  453. print "</form>\n";
  454. print '</td></tr>';
  455. print "</table>\n";
  456. }
  457. // Mode liste
  458. if (($action != 'create' && $action != 'add') || ($action == 'create' && $error))
  459. {
  460. llxHeader();
  461. ?>
  462. <script type="text/javascript">
  463. jQuery(document).ready(function() {
  464. jQuery("#checkall").click(function() {
  465. jQuery(".checkformerge").prop('checked', true);
  466. });
  467. jQuery("#checknone").click(function() {
  468. jQuery(".checkformerge").prop('checked', false);
  469. });
  470. });
  471. </script>
  472. <?php
  473. $sql = 'SELECT s.nom, s.rowid as socid, s.client, c.rowid, c.ref, c.total_ht, c.ref_client,';
  474. $sql.= ' c.date_valid, c.date_commande, c.date_livraison, c.fk_statut, c.facture as facturee';
  475. $sql.= ' FROM '.MAIN_DB_PREFIX.'societe as s';
  476. $sql.= ', '.MAIN_DB_PREFIX.'commande as c';
  477. if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  478. $sql.= ' WHERE c.entity IN ('.getEntity('commande', 1).')';
  479. $sql.= ' AND c.fk_soc = s.rowid';
  480. // Show orders with status validated, shipping started and delivered (well any order we can bill)
  481. $sql.= " AND ((c.fk_statut IN (1,2)) OR (c.fk_statut = 3 AND c.facture = 0))";
  482. if ($socid) $sql.= ' AND s.rowid = '.$socid;
  483. if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
  484. if ($sref)
  485. {
  486. $sql.= " AND c.ref LIKE '%".$db->escape($sref)."%'";
  487. }
  488. if ($sall)
  489. {
  490. $sql.= " AND (c.ref LIKE '%".$db->escape($sall)."%' OR c.note LIKE '%".$db->escape($sall)."%')";
  491. }
  492. //Date filter
  493. if ($date_start && $date_end) $sql.= " AND c.date_commande >= '".$db->idate($date_start)."' AND c.date_commande <= '".$db->idate($date_end)."'";
  494. if ($date_starty && $date_endy) $sql.= " AND c.date_livraison >= '".$db->idate($date_starty)."' AND c.date_livraison <= '".$db->idate($date_endy)."'";
  495. if (!empty($sref_client))
  496. {
  497. $sql.= ' AND c.ref_client LIKE \'%'.$db->escape($sref_client).'%\'';
  498. }
  499. $sql.= ' ORDER BY '.$sortfield.' '.$sortorder;
  500. $resql = $db->query($sql);
  501. if ($resql)
  502. {
  503. if ($socid)
  504. {
  505. $soc = new Societe($db);
  506. $soc->fetch($socid);
  507. }
  508. $title = $langs->trans('ListOfOrders');
  509. $title.=' - '.$langs->trans('StatusOrderValidated').', '.$langs->trans("StatusOrderSent").', '.$langs->trans('StatusOrderToBill');
  510. $num = $db->num_rows($resql);
  511. print load_fiche_titre($title);
  512. $i = 0;
  513. $period=$html->select_date($date_start,'date_start',0,0,1,'',1,0,1).' - '.$html->select_date($date_end,'date_end',0,0,1,'',1,0,1);
  514. $periodely=$html->select_date($date_starty,'date_start_dely',0,0,1,'',1,0,1).' - '.$html->select_date($date_endy,'date_end_dely',0,0,1,'',1,0,1);
  515. if (! empty($socid))
  516. {
  517. // Company
  518. $companystatic->id=$socid;
  519. $companystatic->name=$soc->name;
  520. print '<h3>'.$companystatic->getNomUrl(1,'customer').'</h3>';
  521. }
  522. print '<table class="noborder" width="100%">';
  523. print '<tr class="liste_titre">';
  524. print_liste_field_titre($langs->trans('Ref'),$_SERVER["PHP_SELF"],'c.ref','','&amp;socid='.$socid,'',$sortfield,$sortorder);
  525. print_liste_field_titre($langs->trans('RefCustomerOrder'),$_SERVER["PHP_SELF"],'c.ref_client','','&amp;socid='.$socid,'',$sortfield,$sortorder);
  526. print_liste_field_titre($langs->trans('OrderDate'),$_SERVER["PHP_SELF"],'c.date_commande','','&amp;socid='.$socid, 'align="center"',$sortfield,$sortorder);
  527. print_liste_field_titre($langs->trans('DeliveryDate'),$_SERVER["PHP_SELF"],'c.date_livraison','','&amp;socid='.$socid, 'align="center"',$sortfield,$sortorder);
  528. print_liste_field_titre($langs->trans('Status'),'','','','','align="right"');
  529. print_liste_field_titre($langs->trans('GenerateBill'),'','','','','align="center"');
  530. print '</tr>';
  531. // Lignes des champs de filtre
  532. print '<form method="get" action="orderstoinvoice.php">';
  533. print '<input type="hidden" name="socid" value="'.$socid.'">';
  534. print '<tr class="liste_titre">';
  535. print '<td class="liste_titre">';
  536. //REF
  537. print '<input class="flat" size="10" type="text" name="sref" value="'.$sref.'">';
  538. print '</td>';
  539. print '<td class="liste_titre" align="left">';
  540. print '<input class="flat" type="text" size="10" name="sref_client" value="'.$sref_client.'">';
  541. print '</td>';
  542. //DATE ORDER
  543. print '<td class="liste_titre" align="center">';
  544. print $period;
  545. print '</td>';
  546. //DATE DELIVERY
  547. print '<td class="liste_titre" align="center">';
  548. print $periodely;
  549. print '</td>';
  550. //SEARCH BUTTON
  551. print '<td align="right" class="liste_titre">';
  552. 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")).'">';
  553. print '</td>';
  554. //ALL/NONE
  555. print '<td align="center" class="liste_titre">';
  556. if ($conf->use_javascript_ajax) print '<a href="#" id="checkall">'.$langs->trans("All").'</a> / <a href="#" id="checknone">'.$langs->trans("None").'</a>';
  557. print '</td>';
  558. print '</tr>';
  559. print '</form>';
  560. print '<form name="orders2invoice" action="orderstoinvoice.php" method="GET">';
  561. $var=true;
  562. $generic_commande = new Commande($db);
  563. while ($i < $num)
  564. {
  565. $objp = $db->fetch_object($resql);
  566. $var=!$var;
  567. print '<tr '.$bc[$var].'>';
  568. print '<td class="nowrap">';
  569. $generic_commande->id=$objp->rowid;
  570. $generic_commande->ref=$objp->ref;
  571. $generic_commande->statut = $objp->fk_statut;
  572. $generic_commande->date_commande = $db->jdate($objp->date_commande);
  573. $generic_commande->date_livraison = $db->jdate($objp->date_livraison);
  574. print '<table class="nobordernopadding"><tr class="nocellnopadd">';
  575. print '<td class="nobordernopadding nowrap">';
  576. print $generic_commande->getNomUrl(1,$objp->fk_statut);
  577. print '</td>';
  578. print '<td width="20" class="nobordernopadding nowrap">';
  579. if ($generic_commande->hasDelay()) {
  580. print img_picto($langs->trans("Late"),"warning");
  581. }
  582. print '</td>';
  583. print '<td width="16" align="right" class="nobordernopadding hideonsmartphone">';
  584. $filename=dol_sanitizeFileName($objp->ref);
  585. $filedir=$conf->commande->dir_output . '/' . dol_sanitizeFileName($objp->ref);
  586. $urlsource=$_SERVER['PHP_SELF'].'?id='.$objp->rowid;
  587. print $formfile->getDocumentsLink($generic_commande->element, $filename, $filedir);
  588. print '</td></tr></table>';
  589. print '</td>';
  590. print '<td>'.$objp->ref_client.'</td>';
  591. // Order date
  592. print '<td align="center" class="nowrap">';
  593. print dol_print_date($db->jdate($objp->date_commande),'day');
  594. print '</td>';
  595. //Delivery date
  596. print '<td align="center" class="nowrap">';
  597. print dol_print_date($db->jdate($objp->date_livraison),'day');
  598. print '</td>';
  599. // Statut
  600. print '<td align="right" class="nowrap">'.$generic_commande->LibStatut($objp->fk_statut,$objp->facturee,5).'</td>';
  601. // Checkbox
  602. print '<td align="center">';
  603. print '<input class="flat checkformerge" type="checkbox" name="orders_to_invoice[]" value="'.$objp->rowid.'">';
  604. print '</td>' ;
  605. print '</tr>';
  606. $total = $total + $objp->price;
  607. $subtotal = $subtotal + $objp->price;
  608. $i++;
  609. }
  610. print '</table>';
  611. /*
  612. * Boutons actions
  613. */
  614. print '<br><div class="center"><input type="checkbox" '.(empty($conf->global->INVOICE_CLOSE_ORDERS_OFF_BY_DEFAULT_FORMASSINVOICE)?' checked="checked"':'').' name="autocloseorders"> '.$langs->trans("CloseProcessedOrdersAutomatically");
  615. print '<div align="right">';
  616. print '<input type="hidden" name="socid" value="'.$socid.'">';
  617. print '<input type="hidden" name="action" value="create">';
  618. print '<input type="hidden" name="origin" value="commande"><br>';
  619. //print '<a class="butAction" href="index.php">'.$langs->trans("GoBack").'</a>';
  620. print '<input type="submit" class="butAction" value="'.$langs->trans("GenerateBill").'">';
  621. print '</div>';
  622. print '</div>';
  623. print '</form>';
  624. $db->free($resql);
  625. }
  626. else
  627. {
  628. dol_print_error($db);
  629. }
  630. }
  631. llxFooter();
  632. $db->close();