orderstoinvoice.php 25 KB

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