fiche-rec.php 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  1. <?php
  2. /* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
  5. * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
  6. * Copyright (C) 2013 Juanjo Menent <jmenent@2byte.es>
  7. * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  8. * Copyright (C) 2012 Cedric Salvador <csalvador@gpcsolutions.fr>
  9. * Copyright (C) 2015 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
  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/compta/facture/fiche-rec.php
  26. * \ingroup facture
  27. * \brief Page to show predefined invoice
  28. */
  29. require '../../main.inc.php';
  30. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture-rec.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  35. $langs->load('bills');
  36. $langs->load('compta');
  37. // Security check
  38. $id=(GETPOST('facid','int')?GETPOST('facid','int'):GETPOST('id','int'));
  39. $confirm = GETPOST('confirm', 'alpha');
  40. $cancel = GETPOST('cancel', 'alpha');
  41. $lineid=GETPOST('lineid','int');
  42. $ref=GETPOST('ref','alpha');
  43. $action=GETPOST('action', 'alpha');
  44. if ($user->societe_id) $socid=$user->societe_id;
  45. $objecttype = 'facture_rec';
  46. if ($action == "create" || $action == "add") $objecttype = '';
  47. $result = restrictedArea($user, 'facture', $id, $objecttype);
  48. $projectid = GETPOST('projectid','int');
  49. $search_ref=GETPOST('search_ref');
  50. $search_societe=GETPOST('search_societe');
  51. $search_montant_ht=GETPOST('search_montant_ht');
  52. $search_montant_vat=GETPOST('search_montant_vat');
  53. $search_montant_ttc=GETPOST('search_montant_ttc');
  54. $day=GETPOST('day');
  55. $year=GETPOST('year');
  56. $month=GETPOST('month');
  57. $day_date_when=GETPOST('day_date_when');
  58. $year_date_when=GETPOST('year_date_when');
  59. $month_date_when=GETPOST('month_date_when');
  60. $search_frequency=GETPOST('search_frequency');
  61. $limit = GETPOST('limit')?GETPOST('limit','int'):$conf->liste_limit;
  62. $sortfield = GETPOST("sortfield",'alpha');
  63. $sortorder = GETPOST("sortorder",'alpha');
  64. $page = GETPOST("page",'int');
  65. if ($page == -1) { $page = 0; }
  66. $offset = $limit * $page;
  67. if (! $sortorder) $sortorder='DESC';
  68. if (! $sortfield) $sortfield='f.titre';
  69. $pageprev = $page - 1;
  70. $pagenext = $page + 1;
  71. $object = new FactureRec($db);
  72. if (($id > 0 || $ref) && $action != 'create' && $action != 'add')
  73. {
  74. $ret = $object->fetch($id, $ref);
  75. if (!$ret)
  76. {
  77. setEventMessages($langs->trans("ErrorRecordNotFound"), null, 'errors');
  78. }
  79. }
  80. // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
  81. $hookmanager->initHooks(array('invoicecard','globalcard'));
  82. $extrafields = new ExtraFields($db);
  83. // fetch optionals attributes and labels
  84. $extralabels = $extrafields->fetch_name_optionals_label('facturerec');
  85. $search_array_options=$extrafields->getOptionalsFromPost($extralabels,'','search_');
  86. $permissionnote = $user->rights->facture->creer; // Used by the include of actions_setnotes.inc.php
  87. $permissiondellink=$user->rights->facture->creer; // Used by the include of actions_dellink.inc.php
  88. $permissiontoedit = $user->rights->facture->creer; // Used by the include of actions_lineupdonw.inc.php
  89. $arrayfields=array(
  90. 'f.titre'=>array('label'=>$langs->trans("Ref"), 'checked'=>1),
  91. 's.nom'=>array('label'=>$langs->trans("ThirdParty"), 'checked'=>1),
  92. 'f.total'=>array('label'=>$langs->trans("AmountHT"), 'checked'=>1),
  93. 'f.tva'=>array('label'=>$langs->trans("AmountVAT"), 'checked'=>1),
  94. 'f.total_ttc'=>array('label'=>$langs->trans("AmountTTC"), 'checked'=>1),
  95. 'f.frequency'=>array('label'=>$langs->trans("RecurringInvoiceTemplate"), 'checked'=>1),
  96. 'f.date_last_gen'=>array('label'=>$langs->trans("DateLastGeneration"), 'checked'=>1),
  97. 'f.date_when'=>array('label'=>$langs->trans("NextDateToExecution"), 'checked'=>1),
  98. 'f.datec'=>array('label'=>$langs->trans("DateCreation"), 'checked'=>0, 'position'=>500),
  99. 'f.tms'=>array('label'=>$langs->trans("DateModificationShort"), 'checked'=>0, 'position'=>500),
  100. );
  101. // Extra fields
  102. if (is_array($extrafields->attribute_label) && count($extrafields->attribute_label))
  103. {
  104. foreach($extrafields->attribute_label as $key => $val)
  105. {
  106. $arrayfields["ef.".$key]=array('label'=>$extrafields->attribute_label[$key], 'checked'=>$extrafields->attribute_list[$key], 'position'=>$extrafields->attribute_pos[$key], 'enabled'=>$extrafields->attribute_perms[$key]);
  107. }
  108. }
  109. /*
  110. * Actions
  111. */
  112. // Set note
  113. include DOL_DOCUMENT_ROOT.'/core/actions_setnotes.inc.php'; // Must be include, not include_once
  114. include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php'; // Must be include, not include_once
  115. // Create predefined invoice
  116. if ($action == 'add')
  117. {
  118. if (! GETPOST('titre'))
  119. {
  120. setEventMessages($langs->transnoentities("ErrorFieldRequired",$langs->trans("Title")), null, 'errors');
  121. $action = "create";
  122. $error++;
  123. }
  124. $frequency=GETPOST('frequency', 'int');
  125. $reyear=GETPOST('reyear');
  126. $remonth=GETPOST('remonth');
  127. $reday=GETPOST('reday');
  128. $rehour=GETPOST('rehour');
  129. $remin=GETPOST('remin');
  130. $nb_gen_max=GETPOST('nb_gen_max', 'int');
  131. //if (empty($nb_gen_max)) $nb_gen_max =0;
  132. if (GETPOST('frequency'))
  133. {
  134. if (empty($reyear) || empty($remonth) || empty($reday))
  135. {
  136. setEventMessages($langs->transnoentities("ErrorFieldRequired",$langs->trans("Date")), null, 'errors');
  137. $action = "create";
  138. $error++;
  139. }
  140. if ($nb_gen_max === '')
  141. {
  142. setEventMessages($langs->transnoentities("ErrorFieldRequired",$langs->trans("MaxPeriodNumber")), null, 'errors');
  143. $action = "create";
  144. $error++;
  145. }
  146. }
  147. if (! $error)
  148. {
  149. $object->titre = GETPOST('titre', 'alpha');
  150. $object->note_private = GETPOST('note_private');
  151. $object->note_public = GETPOST('note_public');
  152. $object->usenewprice = GETPOST('usenewprice');
  153. $object->frequency = $frequency;
  154. $object->unit_frequency = GETPOST('unit_frequency', 'alpha');
  155. $object->nb_gen_max = $nb_gen_max;
  156. $object->auto_validate = GETPOST('auto_validate', 'int');
  157. $object->fk_project = $projectid;
  158. $date_next_execution = dol_mktime($rehour, $remin, 0, $remonth, $reday, $reyear);
  159. $object->date_when = $date_next_execution;
  160. // Get first contract linked to invoice used to generate template
  161. if ($id > 0)
  162. {
  163. $srcObject = new Facture($db);
  164. $srcObject->fetch(GETPOST('facid','int'));
  165. $srcObject->fetchObjectLinked();
  166. if (! empty($srcObject->linkedObjectsIds['contrat']))
  167. {
  168. $contractidid = reset($srcObject->linkedObjectsIds['contrat']);
  169. $object->origin = 'contrat';
  170. $object->origin_id = $contractidid;
  171. $object->linked_objects[$object->origin] = $object->origin_id;
  172. }
  173. }
  174. $db->begin();
  175. $oldinvoice = new Facture($db);
  176. $oldinvoice->fetch($id);
  177. $result = $object->create($user, $oldinvoice->id);
  178. if ($result > 0)
  179. {
  180. $result=$oldinvoice->delete(0, 1);
  181. if ($result < 0)
  182. {
  183. $error++;
  184. setEventMessages($oldinvoice->error, $oldinvoice->errors, 'errors');
  185. $action = "create";
  186. }
  187. }
  188. else
  189. {
  190. $error++;
  191. setEventMessages($object->error, $object->errors, 'errors');
  192. $action = "create";
  193. }
  194. if (! $error)
  195. {
  196. $db->commit();
  197. header("Location: " . $_SERVER['PHP_SELF'] . '?facid=' . $object->id);
  198. exit;
  199. }
  200. else
  201. {
  202. $db->rollback();
  203. $error++;
  204. setEventMessages($object->error, $object->errors, 'errors');
  205. $action = "create";
  206. }
  207. }
  208. }
  209. // Delete
  210. if ($action == 'delete' && $user->rights->facture->supprimer)
  211. {
  212. $object->delete();
  213. header("Location: " . $_SERVER['PHP_SELF'] );
  214. exit;
  215. }
  216. // Update field
  217. // Set condition
  218. if ($action == 'setconditions' && $user->rights->facture->creer)
  219. {
  220. $result=$object->setPaymentTerms(GETPOST('cond_reglement_id', 'int'));
  221. }
  222. // Set mode
  223. elseif ($action == 'setmode' && $user->rights->facture->creer)
  224. {
  225. $result=$object->setPaymentMethods(GETPOST('mode_reglement_id', 'int'));
  226. }
  227. // Set project
  228. elseif ($action == 'classin' && $user->rights->facture->creer)
  229. {
  230. $object->setProject(GETPOST('projectid', 'int'));
  231. }
  232. // Set bank account
  233. elseif ($action == 'setbankaccount' && $user->rights->facture->creer)
  234. {
  235. $result=$object->setBankAccount(GETPOST('fk_account', 'int'));
  236. }
  237. // Set frequency and unit frequency
  238. elseif ($action == 'setfrequency' && $user->rights->facture->creer)
  239. {
  240. $object->setFrequencyAndUnit(GETPOST('frequency', 'int'), GETPOST('unit_frequency', 'alpha'));
  241. }
  242. // Set next date of execution
  243. elseif ($action == 'setdate_when' && $user->rights->facture->creer)
  244. {
  245. $date = dol_mktime(GETPOST('date_whenhour'), GETPOST('date_whenmin'), 0, GETPOST('date_whenmonth'), GETPOST('date_whenday'), GETPOST('date_whenyear'));
  246. if (!empty($date)) $object->setNextDate($date);
  247. }
  248. // Set max period
  249. elseif ($action == 'setnb_gen_max' && $user->rights->facture->creer)
  250. {
  251. $object->setMaxPeriod(GETPOST('nb_gen_max', 'int'));
  252. }
  253. // Set auto validate
  254. elseif ($action == 'setauto_validate' && $user->rights->facture->creer)
  255. {
  256. $object->setAutoValidate(GETPOST('auto_validate', 'int'));
  257. }
  258. // Delete line
  259. if ($action == 'confirm_deleteline' && $confirm == 'yes' && $user->rights->facture->creer)
  260. {
  261. $object->fetch($id);
  262. $object->fetch_thirdparty();
  263. $db->begin();
  264. $line=new FactureLigneRec($db);
  265. // For triggers
  266. $line->id = $lineid;
  267. if ($line->delete() > 0)
  268. {
  269. $result=$object->update_price(1);
  270. if ($result > 0)
  271. {
  272. $db->commit();
  273. $object->fetch($object->id); // Reload lines
  274. }
  275. else
  276. {
  277. $db->rollback();
  278. setEventMessages($db->lasterror(), null, 'errors');
  279. }
  280. }
  281. else
  282. {
  283. $db->rollback();
  284. setEventMessages($line->error, $line->errors, 'errors');
  285. }
  286. }
  287. // Add a new line
  288. if ($action == 'addline' && $user->rights->facture->creer)
  289. {
  290. $langs->load('errors');
  291. $error = 0;
  292. // Set if we used free entry or predefined product
  293. $predef='';
  294. $product_desc=(GETPOST('dp_desc')?GETPOST('dp_desc'):'');
  295. $price_ht = GETPOST('price_ht');
  296. if (GETPOST('prod_entry_mode') == 'free')
  297. {
  298. $idprod=0;
  299. $tva_tx = (GETPOST('tva_tx') ? GETPOST('tva_tx') : 0);
  300. }
  301. else
  302. {
  303. $idprod=GETPOST('idprod', 'int');
  304. $tva_tx = '';
  305. }
  306. $qty = GETPOST('qty' . $predef);
  307. $remise_percent = GETPOST('remise_percent' . $predef);
  308. // Extrafields
  309. $extrafieldsline = new ExtraFields($db);
  310. $extralabelsline = $extrafieldsline->fetch_name_optionals_label($object->table_element_line);
  311. $array_options = $extrafieldsline->getOptionalsFromPost($extralabelsline, $predef);
  312. // Unset extrafield
  313. if (is_array($extralabelsline)) {
  314. // Get extra fields
  315. foreach ($extralabelsline as $key => $value) {
  316. unset($_POST["options_" . $key . $predef]);
  317. }
  318. }
  319. if (empty($idprod) && ($price_ht < 0) && ($qty < 0)) {
  320. setEventMessages($langs->trans('ErrorBothFieldCantBeNegative', $langs->transnoentitiesnoconv('UnitPriceHT'), $langs->transnoentitiesnoconv('Qty')), null, 'errors');
  321. $error ++;
  322. }
  323. if (GETPOST('prod_entry_mode') == 'free' && empty($idprod) && GETPOST('type') < 0) {
  324. setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Type')), null, 'errors');
  325. $error ++;
  326. }
  327. if (GETPOST('prod_entry_mode') == 'free' && empty($idprod) && (! ($price_ht >= 0) || $price_ht == '')) // Unit price can be 0 but not ''
  328. {
  329. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("UnitPriceHT")), null, 'errors');
  330. $error ++;
  331. }
  332. if ($qty == '') {
  333. setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Qty')), null, 'errors');
  334. $error ++;
  335. }
  336. if (GETPOST('prod_entry_mode') == 'free' && empty($idprod) && empty($product_desc)) {
  337. setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Description')), null, 'errors');
  338. $error ++;
  339. }
  340. if ($qty < 0) {
  341. $langs->load("errors");
  342. setEventMessages($langs->trans('ErrorQtyForCustomerInvoiceCantBeNegative'), null, 'errors');
  343. $error ++;
  344. }
  345. if (! $error && ($qty >= 0) && (! empty($product_desc) || ! empty($idprod))) {
  346. $ret = $object->fetch($id);
  347. if ($ret < 0) {
  348. dol_print_error($db, $object->error);
  349. exit();
  350. }
  351. $ret = $object->fetch_thirdparty();
  352. // Clean parameters
  353. $date_start = dol_mktime(GETPOST('date_start' . $predef . 'hour'), GETPOST('date_start' . $predef . 'min'), GETPOST('date_start' . $predef . 'sec'), GETPOST('date_start' . $predef . 'month'), GETPOST('date_start' . $predef . 'day'), GETPOST('date_start' . $predef . 'year'));
  354. $date_end = dol_mktime(GETPOST('date_end' . $predef . 'hour'), GETPOST('date_end' . $predef . 'min'), GETPOST('date_end' . $predef . 'sec'), GETPOST('date_end' . $predef . 'month'), GETPOST('date_end' . $predef . 'day'), GETPOST('date_end' . $predef . 'year'));
  355. $price_base_type = (GETPOST('price_base_type', 'alpha') ? GETPOST('price_base_type', 'alpha') : 'HT');
  356. // Define special_code for special lines
  357. $special_code = 0;
  358. // if (empty($_POST['qty'])) $special_code=3; // Options should not exists on invoices
  359. // Ecrase $pu par celui du produit
  360. // Ecrase $desc par celui du produit
  361. // Ecrase $txtva par celui du produit
  362. // Ecrase $base_price_type par celui du produit
  363. // Replaces $fk_unit with the product's
  364. if (! empty($idprod))
  365. {
  366. $prod = new Product($db);
  367. $prod->fetch($idprod);
  368. $label = ((GETPOST('product_label') && GETPOST('product_label') != $prod->label) ? GETPOST('product_label') : '');
  369. // Update if prices fields are defined
  370. $tva_tx = get_default_tva($mysoc, $object->thirdparty, $prod->id);
  371. $tva_npr = get_default_npr($mysoc, $object->thirdparty, $prod->id);
  372. if (empty($tva_tx)) $tva_npr=0;
  373. $pu_ht = $prod->price;
  374. $pu_ttc = $prod->price_ttc;
  375. $price_min = $prod->price_min;
  376. $price_base_type = $prod->price_base_type;
  377. // We define price for product
  378. if (! empty($conf->global->PRODUIT_MULTIPRICES) && ! empty($object->thirdparty->price_level))
  379. {
  380. $pu_ht = $prod->multiprices[$object->thirdparty->price_level];
  381. $pu_ttc = $prod->multiprices_ttc[$object->thirdparty->price_level];
  382. $price_min = $prod->multiprices_min[$object->thirdparty->price_level];
  383. $price_base_type = $prod->multiprices_base_type[$object->thirdparty->price_level];
  384. if (! empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) // using this option is a bug. kept for backward compatibility
  385. {
  386. if (isset($prod->multiprices_tva_tx[$object->thirdparty->price_level])) $tva_tx=$prod->multiprices_tva_tx[$object->thirdparty->price_level];
  387. if (isset($prod->multiprices_recuperableonly[$object->thirdparty->price_level])) $tva_npr=$prod->multiprices_recuperableonly[$object->thirdparty->price_level];
  388. if (empty($tva_tx)) $tva_npr=0;
  389. }
  390. }
  391. elseif (! empty($conf->global->PRODUIT_CUSTOMER_PRICES))
  392. {
  393. require_once DOL_DOCUMENT_ROOT . '/product/class/productcustomerprice.class.php';
  394. $prodcustprice = new Productcustomerprice($db);
  395. $filter = array('t.fk_product' => $prod->id,'t.fk_soc' => $object->thirdparty->id);
  396. $result = $prodcustprice->fetch_all('', '', 0, 0, $filter);
  397. if ($result) {
  398. if (count($prodcustprice->lines) > 0) {
  399. $pu_ht = price($prodcustprice->lines[0]->price);
  400. $pu_ttc = price($prodcustprice->lines[0]->price_ttc);
  401. $price_base_type = $prodcustprice->lines[0]->price_base_type;
  402. $prod->tva_tx = $prodcustprice->lines[0]->tva_tx;
  403. }
  404. }
  405. }
  406. // if price ht was forced (ie: from gui when calculated by margin rate and cost price)
  407. if (! empty($price_ht))
  408. {
  409. $pu_ht = price2num($price_ht, 'MU');
  410. $pu_ttc = price2num($pu_ht * (1 + ($tva_tx / 100)), 'MU');
  411. }
  412. // On reevalue prix selon taux tva car taux tva transaction peut etre different
  413. // de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur).
  414. elseif ($tva_tx != $prod->tva_tx)
  415. {
  416. if ($price_base_type != 'HT')
  417. {
  418. $pu_ht = price2num($pu_ttc / (1 + ($tva_tx / 100)), 'MU');
  419. }
  420. else
  421. {
  422. $pu_ttc = price2num($pu_ht * (1 + ($tva_tx / 100)), 'MU');
  423. }
  424. }
  425. $desc = '';
  426. // Define output language
  427. if (! empty($conf->global->MAIN_MULTILANGS) && ! empty($conf->global->PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE)) {
  428. $outputlangs = $langs;
  429. $newlang = '';
  430. if (empty($newlang) && GETPOST('lang_id'))
  431. $newlang = GETPOST('lang_id');
  432. if (empty($newlang))
  433. $newlang = $object->thirdparty->default_lang;
  434. if (! empty($newlang)) {
  435. $outputlangs = new Translate("", $conf);
  436. $outputlangs->setDefaultLang($newlang);
  437. }
  438. $desc = (! empty($prod->multilangs [$outputlangs->defaultlang] ["description"])) ? $prod->multilangs [$outputlangs->defaultlang] ["description"] : $prod->description;
  439. } else {
  440. $desc = $prod->description;
  441. }
  442. $desc = dol_concatdesc($desc, $product_desc);
  443. // Add custom code and origin country into description
  444. if (empty($conf->global->MAIN_PRODUCT_DISABLE_CUSTOMCOUNTRYCODE) && (! empty($prod->customcode) || ! empty($prod->country_code))) {
  445. $tmptxt = '(';
  446. if (! empty($prod->customcode))
  447. $tmptxt .= $langs->transnoentitiesnoconv("CustomCode") . ': ' . $prod->customcode;
  448. if (! empty($prod->customcode) && ! empty($prod->country_code))
  449. $tmptxt .= ' - ';
  450. if (! empty($prod->country_code))
  451. $tmptxt .= $langs->transnoentitiesnoconv("CountryOrigin") . ': ' . getCountry($prod->country_code, 0, $db, $langs, 0);
  452. $tmptxt .= ')';
  453. $desc = dol_concatdesc($desc, $tmptxt);
  454. }
  455. $type = $prod->type;
  456. $fk_unit = $prod->fk_unit;
  457. } else {
  458. $pu_ht = price2num($price_ht, 'MU');
  459. $pu_ttc = price2num(GETPOST('price_ttc'), 'MU');
  460. $tva_npr = (preg_match('/\*/', $tva_tx) ? 1 : 0);
  461. $tva_tx = str_replace('*', '', $tva_tx);
  462. if (empty($tva_tx)) $tva_npr=0;
  463. $label = (GETPOST('product_label') ? GETPOST('product_label') : '');
  464. $desc = $product_desc;
  465. $type = GETPOST('type');
  466. $fk_unit= GETPOST('units', 'alpha');
  467. }
  468. // Margin
  469. $fournprice = price2num(GETPOST('fournprice' . $predef) ? GETPOST('fournprice' . $predef) : '');
  470. $buyingprice = price2num(GETPOST('buying_price' . $predef) != '' ? GETPOST('buying_price' . $predef) : ''); // If buying_price is '0', we must keep this value
  471. // Local Taxes
  472. $localtax1_tx = get_localtax($tva_tx, 1, $object->thirdparty, $mysoc, $tva_npr);
  473. $localtax2_tx = get_localtax($tva_tx, 2, $object->thirdparty, $mysoc, $tva_npr);
  474. $info_bits = 0;
  475. if ($tva_npr)
  476. $info_bits |= 0x01;
  477. if (! empty($price_min) && (price2num($pu_ht) * (1 - price2num($remise_percent) / 100) < price2num($price_min))) {
  478. $mesg = $langs->trans("CantBeLessThanMinPrice", price(price2num($price_min, 'MU'), 0, $langs, 0, 0, - 1, $conf->currency));
  479. setEventMessages($mesg, null, 'errors');
  480. } else {
  481. // Insert line
  482. $result = $object->addline($desc, $pu_ht, $qty, $tva_tx, $idprod, $remise_percent, $price_base_type, $info_bits, '', $pu_ttc, $type, - 1, $special_code, $label, $fk_unit);
  483. if ($result > 0)
  484. {
  485. /*if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  486. {
  487. // Define output language
  488. $outputlangs = $langs;
  489. $newlang = '';
  490. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
  491. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
  492. if (! empty($newlang)) {
  493. $outputlangs = new Translate("", $conf);
  494. $outputlangs->setDefaultLang($newlang);
  495. }
  496. $model=$object->modelpdf;
  497. $ret = $object->fetch($id); // Reload to get new records
  498. $result = $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  499. if ($result < 0) setEventMessages($object->error, $object->errors, 'errors');
  500. }*/
  501. $object->fetch($object->id); // Reload lines
  502. unset($_POST['prod_entry_mode']);
  503. unset($_POST['qty']);
  504. unset($_POST['type']);
  505. unset($_POST['remise_percent']);
  506. unset($_POST['price_ht']);
  507. unset($_POST['multicurrency_price_ht']);
  508. unset($_POST['price_ttc']);
  509. unset($_POST['tva_tx']);
  510. unset($_POST['product_ref']);
  511. unset($_POST['product_label']);
  512. unset($_POST['product_desc']);
  513. unset($_POST['fournprice']);
  514. unset($_POST['buying_price']);
  515. unset($_POST['np_marginRate']);
  516. unset($_POST['np_markRate']);
  517. unset($_POST['dp_desc']);
  518. unset($_POST['idprod']);
  519. unset($_POST['units']);
  520. unset($_POST['date_starthour']);
  521. unset($_POST['date_startmin']);
  522. unset($_POST['date_startsec']);
  523. unset($_POST['date_startday']);
  524. unset($_POST['date_startmonth']);
  525. unset($_POST['date_startyear']);
  526. unset($_POST['date_endhour']);
  527. unset($_POST['date_endmin']);
  528. unset($_POST['date_endsec']);
  529. unset($_POST['date_endday']);
  530. unset($_POST['date_endmonth']);
  531. unset($_POST['date_endyear']);
  532. unset($_POST['situations']);
  533. unset($_POST['progress']);
  534. } else {
  535. setEventMessages($object->error, $object->errors, 'errors');
  536. }
  537. $action = '';
  538. }
  539. }
  540. }
  541. elseif ($action == 'updateligne' && $user->rights->facture->creer && ! GETPOST('cancel'))
  542. {
  543. if (! $object->fetch($id) > 0) dol_print_error($db);
  544. $object->fetch_thirdparty();
  545. // Clean parameters
  546. $date_start = '';
  547. $date_end = '';
  548. //$date_start = dol_mktime(GETPOST('date_starthour'), GETPOST('date_startmin'), GETPOST('date_startsec'), GETPOST('date_startmonth'), GETPOST('date_startday'), GETPOST('date_startyear'));
  549. //$date_end = dol_mktime(GETPOST('date_endhour'), GETPOST('date_endmin'), GETPOST('date_endsec'), GETPOST('date_endmonth'), GETPOST('date_endday'), GETPOST('date_endyear'));
  550. $description = dol_htmlcleanlastbr(GETPOST('product_desc') ? GETPOST('product_desc') : GETPOST('desc'));
  551. $pu_ht = GETPOST('price_ht');
  552. $vat_rate = (GETPOST('tva_tx') ? GETPOST('tva_tx') : 0);
  553. $qty = GETPOST('qty');
  554. // Define info_bits
  555. $info_bits = 0;
  556. if (preg_match('/\*/', $vat_rate))
  557. $info_bits |= 0x01;
  558. // Define vat_rate
  559. $vat_rate = str_replace('*', '', $vat_rate);
  560. $localtax1_rate = get_localtax($vat_rate, 1, $object->thirdparty);
  561. $localtax2_rate = get_localtax($vat_rate, 2, $object->thirdparty);
  562. // Add buying price
  563. $fournprice = price2num(GETPOST('fournprice') ? GETPOST('fournprice') : '');
  564. $buyingprice = price2num(GETPOST('buying_price') != '' ? GETPOST('buying_price') : ''); // If buying_price is '0', we muste keep this value
  565. // Extrafields
  566. $extrafieldsline = new ExtraFields($db);
  567. $extralabelsline = $extrafieldsline->fetch_name_optionals_label($object->table_element_line);
  568. $array_options = $extrafieldsline->getOptionalsFromPost($extralabelsline);
  569. // Unset extrafield
  570. if (is_array($extralabelsline)) {
  571. // Get extra fields
  572. foreach ($extralabelsline as $key => $value) {
  573. unset($_POST["options_" . $key]);
  574. }
  575. }
  576. // Define special_code for special lines
  577. $special_code=GETPOST('special_code');
  578. if (! GETPOST('qty')) $special_code=3;
  579. /*$line = new FactureLigne($db);
  580. $line->fetch(GETPOST('lineid'));
  581. $percent = $line->get_prev_progress($object->id);
  582. if (GETPOST('progress') < $percent)
  583. {
  584. $mesg = '<div class="warning">' . $langs->trans("CantBeLessThanMinPercent") . '</div>';
  585. setEventMessages($mesg, null, 'warnings');
  586. $error++;
  587. $result = -1;
  588. }*/
  589. // Check minimum price
  590. $productid = GETPOST('productid', 'int');
  591. if (! empty($productid))
  592. {
  593. $product = new Product($db);
  594. $product->fetch($productid);
  595. $type = $product->type;
  596. $price_min = $product->price_min;
  597. if (! empty($conf->global->PRODUIT_MULTIPRICES) && ! empty($object->thirdparty->price_level))
  598. $price_min = $product->multiprices_min [$object->thirdparty->price_level];
  599. $label = ((GETPOST('update_label') && GETPOST('product_label')) ? GETPOST('product_label') : '');
  600. // Check price is not lower than minimum (check is done only for standard or replacement invoices)
  601. if (($object->type == Facture::TYPE_STANDARD || $object->type == Facture::TYPE_REPLACEMENT) && $price_min && (price2num($pu_ht) * (1 - price2num(GETPOST('remise_percent')) / 100) < price2num($price_min))) {
  602. setEventMessages($langs->trans("CantBeLessThanMinPrice", price(price2num($price_min, 'MU'), 0, $langs, 0, 0, - 1, $conf->currency)), null, 'errors');
  603. $error ++;
  604. }
  605. } else {
  606. $type = GETPOST('type');
  607. $label = (GETPOST('product_label') ? GETPOST('product_label') : '');
  608. // Check parameters
  609. if (GETPOST('type') < 0) {
  610. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type")), null, 'errors');
  611. $error ++;
  612. }
  613. }
  614. if ($qty < 0) {
  615. $langs->load("errors");
  616. setEventMessages($langs->trans('ErrorQtyForCustomerInvoiceCantBeNegative'), null, 'errors');
  617. $error ++;
  618. }
  619. // Update line
  620. if (! $error) {
  621. $result = $object->updateline(GETPOST('lineid'), $description, $pu_ht, $qty,
  622. $vat_rate, GETPOST('productid'), GETPOST('remise_percent'), 'HT', $info_bits, 0, 0, $type,
  623. 0, $special_code, $label, GETPOST('units'));
  624. if ($result >= 0) {
  625. /*if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  626. // Define output language
  627. $outputlangs = $langs;
  628. $newlang = '';
  629. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id'))
  630. $newlang = GETPOST('lang_id');
  631. if ($conf->global->MAIN_MULTILANGS && empty($newlang))
  632. $newlang = $object->thirdparty->default_lang;
  633. if (! empty($newlang)) {
  634. $outputlangs = new Translate("", $conf);
  635. $outputlangs->setDefaultLang($newlang);
  636. }
  637. $ret = $object->fetch($id); // Reload to get new records
  638. $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  639. }*/
  640. $object->fetch($object->id); // Reload lines
  641. unset($_POST['qty']);
  642. unset($_POST['type']);
  643. unset($_POST['productid']);
  644. unset($_POST['remise_percent']);
  645. unset($_POST['price_ht']);
  646. unset($_POST['multicurrency_price_ht']);
  647. unset($_POST['price_ttc']);
  648. unset($_POST['tva_tx']);
  649. unset($_POST['product_ref']);
  650. unset($_POST['product_label']);
  651. unset($_POST['product_desc']);
  652. unset($_POST['fournprice']);
  653. unset($_POST['buying_price']);
  654. unset($_POST['np_marginRate']);
  655. unset($_POST['np_markRate']);
  656. unset($_POST['dp_desc']);
  657. unset($_POST['idprod']);
  658. unset($_POST['units']);
  659. unset($_POST['date_starthour']);
  660. unset($_POST['date_startmin']);
  661. unset($_POST['date_startsec']);
  662. unset($_POST['date_startday']);
  663. unset($_POST['date_startmonth']);
  664. unset($_POST['date_startyear']);
  665. unset($_POST['date_endhour']);
  666. unset($_POST['date_endmin']);
  667. unset($_POST['date_endsec']);
  668. unset($_POST['date_endday']);
  669. unset($_POST['date_endmonth']);
  670. unset($_POST['date_endyear']);
  671. unset($_POST['situations']);
  672. unset($_POST['progress']);
  673. } else {
  674. setEventMessages($object->error, $object->errors, 'errors');
  675. }
  676. }
  677. }
  678. // Do we click on purge search criteria ?
  679. if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter.x") || GETPOST("button_removefilter")) // All test are required to be compatible with all browsers
  680. {
  681. $search_ref='';
  682. $search_societe='';
  683. $search_montant_ht='';
  684. $search_montant_vat='';
  685. $search_montant_ttc='';
  686. $day='';
  687. $year='';
  688. $month='';
  689. $day_date_when='';
  690. $year_date_when='';
  691. $month_date_when='';
  692. $search_frequency='';
  693. $search_array_options=array();
  694. }
  695. /*
  696. * View
  697. */
  698. llxHeader('',$langs->trans("RepeatableInvoices"),'ch-facture.html#s-fac-facture-rec');
  699. $form = new Form($db);
  700. $formother = new FormOther($db);
  701. $companystatic = new Societe($db);
  702. $now = dol_now();
  703. $tmparray=dol_getdate($now);
  704. $today = dol_mktime(23,59,59,$tmparray['mon'],$tmparray['mday'],$tmparray['year']); // Today is last second of current day
  705. /*
  706. * Create mode
  707. */
  708. if ($action == 'create')
  709. {
  710. print load_fiche_titre($langs->trans("CreateRepeatableInvoice"),'','title_accountancy.png');
  711. $object = new Facture($db); // Source invoice
  712. $product_static = new Product($db);
  713. $formproject = new FormProjets($db);
  714. if ($object->fetch($id, $ref) > 0)
  715. {
  716. $result = $object->getLinesArray();
  717. print '<form action="fiche-rec.php" method="post">';
  718. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  719. print '<input type="hidden" name="action" value="add">';
  720. print '<input type="hidden" name="facid" value="'.$object->id.'">';
  721. dol_fiche_head();
  722. $rowspan=4;
  723. if (! empty($conf->projet->enabled)) $rowspan++;
  724. if ($object->fk_account > 0) $rowspan++;
  725. print '<table class="border" width="100%">';
  726. $object->fetch_thirdparty();
  727. // Third party
  728. print '<tr><td class="titlefieldcreate">'.$langs->trans("Customer").'</td><td>'.$object->thirdparty->getNomUrl(1,'customer').'</td>';
  729. print '</tr>';
  730. // Title
  731. print '<tr><td class="fieldrequired">'.$langs->trans("Title").'</td><td>';
  732. print '<input class="flat quatrevingtpercent" type="text" name="titre" value="'.$_POST["titre"].'">';
  733. print '</td></tr>';
  734. // Note public
  735. print '<tr><td>'.$langs->trans("NotePublic").'</td><td valign="top">';
  736. print '<textarea class="flat centpercent" name="note_public" wrap="soft" rows="'.ROWS_4.'"></textarea>';
  737. print '</td></tr>';
  738. // Note private
  739. print '<tr><td>'.$langs->trans("NotePrivate").'</td><td valign="top">';
  740. print '<textarea class="flat centpercent" name="note_private" wrap="soft" rows="'.ROWS_4.'"></textarea>';
  741. print '</td></tr>';
  742. // Author
  743. print "<tr><td>".$langs->trans("Author")."</td><td>".$user->getFullName($langs)."</td></tr>";
  744. // Payment term
  745. print "<tr><td>".$langs->trans("PaymentConditions")."</td><td>";
  746. $form->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->cond_reglement_id, 'none');
  747. print "</td></tr>";
  748. // Payment mode
  749. print "<tr><td>".$langs->trans("PaymentMode")."</td><td>";
  750. $form->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->mode_reglement_id, 'none');
  751. print "</td></tr>";
  752. // Project
  753. if (! empty($conf->projet->enabled) && is_object($object->thirdparty) && $object->thirdparty->id > 0)
  754. {
  755. $projectid = $object->fk_project;
  756. $langs->load('projects');
  757. print '<tr><td>' . $langs->trans('Project') . '</td><td>';
  758. $numprojet = $formproject->select_projects($socid, $projectid, 'projectid', 0);
  759. print ' &nbsp; <a href="'.DOL_URL_ROOT.'/projet/card.php?socid=' . $soc->id . '&action=create&status=1&backtopage='.urlencode($_SERVER["PHP_SELF"].'?action=create&socid='.$soc->id).'">' . $langs->trans("AddProject") . '</a>';
  760. print '</td></tr>';
  761. }
  762. // Bank account
  763. if ($object->fk_account > 0)
  764. {
  765. print "<tr><td>".$langs->trans('BankAccount')."</td><td>";
  766. $form->formSelectAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->fk_account, 'none');
  767. print "</td></tr>";
  768. }
  769. print "</table>";
  770. print '<br><br>';
  771. // Autogeneration
  772. $title = $langs->trans("Recurrence");
  773. print load_fiche_titre($title, '', 'calendar');
  774. print '<table class="border" width="100%">';
  775. // Frequency
  776. print '<tr><td class="titlefieldcreate">'.$form->textwithpicto($langs->trans("Frequency"), $langs->transnoentitiesnoconv('toolTipFrequency'))."</td><td>";
  777. print "<input type='text' name='frequency' value='".GETPOST('frequency', 'int')."' size='5' />&nbsp;".$form->selectarray('unit_frequency', array('d'=>$langs->trans('Day'), 'm'=>$langs->trans('Month'), 'y'=>$langs->trans('Year')), (GETPOST('unit_frequency')?GETPOST('unit_frequency'):'m'));
  778. print "</td></tr>";
  779. // First date of execution for cron
  780. print "<tr><td>".$langs->trans('NextDateToExecution')."</td><td>";
  781. $date_next_execution = isset($date_next_execution) ? $date_next_execution : (GETPOST('remonth') ? dol_mktime(12, 0, 0, GETPOST('remonth'), GETPOST('reday'), GETPOST('reyear')) : -1);
  782. print $form->select_date($date_next_execution, '', 1, 1, '', "add", 1, 1, 1);
  783. print "</td></tr>";
  784. // Number max of generation
  785. print "<tr><td>".$langs->trans("MaxPeriodNumber")."</td><td>";
  786. print '<input type="text" name="nb_gen_max" value="'.GETPOST('nb_gen_max').'" size="5" />';
  787. print "</td></tr>";
  788. // Auto validate the invoice
  789. print "<tr><td>".$langs->trans("StatusOfGeneratedInvoices")."</td><td>";
  790. $select = array('0'=>$langs->trans('BillStatusDraft'),'1'=>$langs->trans('BillStatusValidated'));
  791. print $form->selectarray('auto_validate', $select, GETPOST('auto_validate'));
  792. print "</td></tr>";
  793. print "</table>";
  794. print '<br><br>';
  795. $title = $langs->trans("ProductsAndServices");
  796. if (empty($conf->service->enabled))
  797. $title = $langs->trans("Products");
  798. else if (empty($conf->product->enabled))
  799. $title = $langs->trans("Services");
  800. print load_fiche_titre($title, '', '');
  801. /*
  802. * Invoice lines
  803. */
  804. print '<table id="tablelines" class="noborder noshadow" width="100%">';
  805. // Show object lines
  806. if (! empty($object->lines))
  807. {
  808. $disableedit=1;
  809. $disablemove=1;
  810. $disableremove=1;
  811. $ret = $object->printObjectLines('', $mysoc, $soc, $lineid, 0); // No date selector for template invoice
  812. }
  813. print "</table>\n";
  814. /*
  815. print '<table class="notopnoleftnoright" width="100%">';
  816. print '<tr><td colspan="3">';
  817. $sql = 'SELECT l.fk_product, l.product_type, l.label as custom_label, l.description, l.qty, l.rowid, l.tva_tx,';
  818. $sql.= ' l.fk_remise_except,';
  819. $sql.= ' l.remise_percent, l.subprice, l.info_bits,';
  820. $sql.= ' l.total_ht, l.total_tva as total_vat, l.total_ttc,';
  821. $sql.= ' l.date_start,';
  822. $sql.= ' l.date_end,';
  823. $sql.= ' l.product_type,';
  824. $sql.= ' l.fk_unit,';
  825. $sql.= ' p.ref, p.fk_product_type, p.label as product_label,';
  826. $sql.= ' p.description as product_desc';
  827. $sql.= " FROM ".MAIN_DB_PREFIX."facturedet as l";
  828. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON l.fk_product = p.rowid";
  829. $sql.= " WHERE l.fk_facture = ".$object->id;
  830. $sql.= " ORDER BY l.rowid";
  831. $result = $db->query($sql);
  832. if ($result)
  833. {
  834. $num = $db->num_rows($result);
  835. $i = 0; $total = 0;
  836. echo '<table class="noborder" width="100%">';
  837. if ($num)
  838. {
  839. print '<tr class="liste_titre">';
  840. print '<td>'.$langs->trans("Description").'</td>';
  841. print '<td align="center">'.$langs->trans("VAT").'</td>';
  842. print '<td align="center">'.$langs->trans("Qty").'</td>';
  843. if ($conf->global->PRODUCT_USE_UNITS) {
  844. print '<td width="8%" align="left">'.$langs->trans("Unit").'</td>';
  845. }
  846. print '<td>'.$langs->trans("ReductionShort").'</td>';
  847. print '<td align="right">'.$langs->trans("TotalHT").'</td>';
  848. print '<td align="right">'.$langs->trans("TotalVAT").'</td>';
  849. print '<td align="right">'.$langs->trans("TotalTTC").'</td>';
  850. print '<td align="right">'.$langs->trans("PriceUHT").'</td>';
  851. if (empty($conf->global->PRODUIT_MULTIPRICES)) print '<td align="right">'.$langs->trans("CurrentProductPrice").'</td>';
  852. print "</tr>\n";
  853. }
  854. $var=true;
  855. while ($i < $num)
  856. {
  857. $objp = $db->fetch_object($result);
  858. if ($objp->fk_product > 0)
  859. {
  860. $product = New Product($db);
  861. $product->fetch($objp->fk_product);
  862. }
  863. $var=!$var;
  864. print "<tr ".$bc[$var].">";
  865. // Show product and description
  866. $type=(isset($objp->product_type)?$objp->product_type:$objp->fk_product_type);
  867. $product_static->fk_unit=$objp->fk_unit;
  868. if ($objp->fk_product > 0)
  869. {
  870. print '<td>';
  871. print '<a name="'.$objp->rowid.'"></a>'; // ancre pour retourner sur la ligne
  872. // Show product and description
  873. $product_static->fetch($objp->fk_product); // We need all information later
  874. $text=$product_static->getNomUrl(1);
  875. $text.= ' - '.(! empty($objp->custom_label)?$objp->custom_label:$objp->product_label);
  876. $description=(! empty($conf->global->PRODUIT_DESC_IN_FORM)?'':dol_htmlentitiesbr($objp->description));
  877. print $form->textwithtooltip($text,$description,3,'','',$i);
  878. // Show range
  879. print_date_range($db->jdate($objp->date_start), $db->jdate($objp->date_end));
  880. // Add description in form
  881. if (! empty($conf->global->PRODUIT_DESC_IN_FORM))
  882. print (! empty($objp->description) && $objp->description!=$objp->product_label)?'<br>'.dol_htmlentitiesbr($objp->description):'';
  883. print '</td>';
  884. }
  885. else
  886. {
  887. print '<td>';
  888. print '<a name="'.$objp->rowid.'"></a>'; // ancre pour retourner sur la ligne
  889. if ($type==1) $text = img_object($langs->trans('Service'),'service');
  890. else $text = img_object($langs->trans('Product'),'product');
  891. if (! empty($objp->custom_label)) {
  892. $text.= ' <strong>'.$objp->custom_label.'</strong>';
  893. print $form->textwithtooltip($text,dol_htmlentitiesbr($objp->description),3,'','',$i);
  894. } else {
  895. print $text.' '.nl2br($objp->description);
  896. }
  897. // Show range
  898. print_date_range($db->jdate($objp->date_start), $db->jdate($objp->date_end));
  899. print "</td>\n";
  900. }
  901. // Vat rate
  902. print '<td align="center">'.vatrate($objp->tva_tx).'%</td>';
  903. // Qty
  904. print '<td align="center">'.$objp->qty.'</td>';
  905. if ($conf->global->PRODUCT_USE_UNITS) {
  906. print '<td align="left">'.$product_static->getLabelOfUnit().'</td>';
  907. }
  908. // Percent
  909. if ($objp->remise_percent > 0)
  910. {
  911. print '<td align="right">'.$objp->remise_percent." %</td>\n";
  912. }
  913. else
  914. {
  915. print '<td>&nbsp;</td>';
  916. }
  917. // Total HT
  918. print '<td align="right">'.price($objp->total_ht)."</td>\n";
  919. // Total VAT
  920. print '<td align="right">'.price($objp->total_vat)."</td>\n";
  921. // Total TTC
  922. print '<td align="right">'.price($objp->total_ttc)."</td>\n";
  923. // Total Unit price
  924. print '<td align="right">'.price($objp->subprice)."</td>\n";
  925. // Current price of product
  926. if (empty($conf->global->PRODUIT_MULTIPRICES))
  927. {
  928. if ($objp->fk_product > 0)
  929. {
  930. $flag_price_may_change++;
  931. $prodprice=$product_static->price; // price HT
  932. print '<td align="right">'.price($prodprice)."</td>\n";
  933. }
  934. else
  935. {
  936. print '<td>&nbsp;</td>';
  937. }
  938. }
  939. print "</tr>";
  940. $i++;
  941. }
  942. $db->free($result);
  943. }
  944. else
  945. {
  946. print $db->error();
  947. }
  948. print "</table>";
  949. */
  950. print '</td></tr>';
  951. if ($flag_price_may_change)
  952. {
  953. print '<tr><td colspan="3" align="left">';
  954. print '<select name="usenewprice" class="flat">';
  955. print '<option value="0">'.$langs->trans("AlwaysUseFixedPrice").'</option>';
  956. print '<option value="1" disabled>'.$langs->trans("AlwaysUseNewPrice").'</option>';
  957. print '</select>';
  958. print '</td></tr>';
  959. }
  960. print "</table>\n";
  961. dol_fiche_end();
  962. print '<div align="center"><input type="submit" class="button" value="'.$langs->trans("Create").'">';
  963. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  964. print '<input type="button" class="button" value="' . $langs->trans("Cancel") . '" onClick="javascript:history.go(-1)">';
  965. print '</div>';
  966. print "</form>\n";
  967. }
  968. else
  969. {
  970. dol_print_error('',"Error, no invoice ".$object->id);
  971. }
  972. }
  973. else
  974. {
  975. /*
  976. * View mode
  977. */
  978. if ($object->id > 0)
  979. {
  980. $object->fetch_thirdparty();
  981. // Confirmation de la suppression d'une ligne produit
  982. if ($action == 'ask_deleteline') {
  983. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id . '&lineid=' . $lineid, $langs->trans('DeleteProductLine'), $langs->trans('ConfirmDeleteProductLine'), 'confirm_deleteline', '', 'no', 1);
  984. }
  985. print $formconfirm;
  986. $author = new User($db);
  987. $author->fetch($object->user_author);
  988. $head=array();
  989. $h=0;
  990. $head[$h][0] = $_SERVER["PHP_SELF"].'?id='.$object->id;
  991. $head[$h][1] = $langs->trans("CardBill");
  992. $head[$h][2] = 'card';
  993. dol_fiche_head($head, 'card', $langs->trans("RepeatableInvoice"),0,'bill'); // Add a div
  994. print '<table class="border" width="100%">';
  995. $linkback = '<a href="' . DOL_URL_ROOT . '/compta/facture/fiche-rec.php' . (! empty($socid) ? '?socid=' . $socid : '') . '">' . $langs->trans("BackToList") . '</a>';
  996. // Ref
  997. print '<tr><td width="20%">' . $langs->trans('Ref') . '</td>';
  998. print '<td colspan="5">';
  999. $morehtmlref = '';
  1000. /*
  1001. require_once DOL_DOCUMENT_ROOT . '/core/class/discount.class.php';
  1002. $discount = new DiscountAbsolute($db);
  1003. $result = $discount->fetch(0, $object->id);
  1004. if ($result > 0) {
  1005. $morehtmlref = ' (' . $langs->trans("CreditNoteConvertedIntoDiscount", $discount->getNomUrl(1, 'discount')) . ')';
  1006. }
  1007. if ($result < 0) {
  1008. dol_print_error('', $discount->error);
  1009. }*/
  1010. print $form->showrefnav($object, 'ref', $linkback, 1, 'titre', 'titre', $morehtmlref);
  1011. print '</td></tr>';
  1012. print '<tr><td>'.$langs->trans("Customer").'</td>';
  1013. print '<td colspan="3">'.$object->thirdparty->getNomUrl(1,'customer').'</td></tr>';
  1014. print "<tr><td>".$langs->trans("Author").'</td><td colspan="3">'.$author->getFullName($langs)."</td></tr>";
  1015. print '<tr><td>'.$langs->trans("AmountHT").'</td>';
  1016. print '<td colspan="3">'.price($object->total_ht,'',$langs,1,-1,-1,$conf->currency).'</td>';
  1017. print '</tr>';
  1018. print '<tr><td>'.$langs->trans("AmountVAT").'</td><td colspan="3">'.price($object->total_tva,'',$langs,1,-1,-1,$conf->currency).'</td>';
  1019. print '</tr>';
  1020. print '<tr><td>'.$langs->trans("AmountTTC").'</td><td colspan="3">'.price($object->total_ttc,'',$langs,1,-1,-1,$conf->currency).'</td>';
  1021. print '</tr>';
  1022. // Payment term
  1023. print '<tr><td>';
  1024. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1025. print $langs->trans('PaymentConditionsShort');
  1026. print '</td>';
  1027. if ($object->type != Facture::TYPE_CREDIT_NOTE && $action != 'editconditions' && ! empty($object->brouillon) && $user->rights->facture->creer)
  1028. print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editconditions&amp;facid=' . $object->id . '">' . img_edit($langs->trans('SetConditions'), 1) . '</a></td>';
  1029. print '</tr></table>';
  1030. print '</td><td colspan="3">';
  1031. if ($object->type != Facture::TYPE_CREDIT_NOTE)
  1032. {
  1033. if ($action == 'editconditions') {
  1034. $form->form_conditions_reglement($_SERVER['PHP_SELF'] . '?facid=' . $object->id, $object->cond_reglement_id, 'cond_reglement_id');
  1035. } else {
  1036. $form->form_conditions_reglement($_SERVER['PHP_SELF'] . '?facid=' . $object->id, $object->cond_reglement_id, 'none');
  1037. }
  1038. } else {
  1039. print '&nbsp;';
  1040. }
  1041. print '</td></tr>';
  1042. // Payment mode
  1043. print '<tr><td>';
  1044. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1045. print $langs->trans('PaymentMode');
  1046. print '</td>';
  1047. if ($action != 'editmode' && ! empty($object->brouillon) && $user->rights->facture->creer)
  1048. print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editmode&amp;facid=' . $object->id . '">' . img_edit($langs->trans('SetMode'), 1) . '</a></td>';
  1049. print '</tr></table>';
  1050. print '</td><td colspan="3">';
  1051. if ($action == 'editmode')
  1052. {
  1053. $form->form_modes_reglement($_SERVER['PHP_SELF'].'?facid='.$object->id, $object->mode_reglement_id, 'mode_reglement_id', 'CRDT');
  1054. }
  1055. else
  1056. {
  1057. $form->form_modes_reglement($_SERVER['PHP_SELF'].'?facid='.$object->id, $object->mode_reglement_id, 'none', 'CRDT');
  1058. }
  1059. print '</td></tr>';
  1060. // Note public
  1061. print '<tr><td>';
  1062. print $form->editfieldkey($langs->trans("NotePublic"), 'note_public', $object->note_public, $object, $user->rights->facture->creer);
  1063. print '</td><td colspan="5">';
  1064. print $form->editfieldval($langs->trans("NotePublic"), 'note_public', $object->note_public, $object, $user->rights->facture->creer, 'textarea:'.ROWS_4.':60');
  1065. print '</td>';
  1066. print '</tr>';
  1067. // Note private
  1068. print '<tr><td>';
  1069. print $form->editfieldkey($langs->trans("NotePrivate"), 'note_private', $object->note_private, $object, $user->rights->facture->creer);
  1070. print '</td><td colspan="5">';
  1071. print $form->editfieldval($langs->trans("NotePrivate"), 'note_private', $object->note_private, $object, $user->rights->facture->creer, 'textarea:'.ROWS_4.':60');
  1072. print '</td>';
  1073. print '</tr>';
  1074. // Project
  1075. if (! empty($conf->projet->enabled)) {
  1076. $langs->load('projects');
  1077. print '<tr>';
  1078. print '<td>';
  1079. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1080. print $langs->trans('Project');
  1081. print '</td>';
  1082. if ($action != 'classify') {
  1083. print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=classify&amp;facid=' . $object->id . '">';
  1084. print img_edit($langs->trans('SetProject'), 1);
  1085. print '</a></td>';
  1086. }
  1087. print '</tr></table>';
  1088. print '</td><td colspan="3">';
  1089. if ($action == 'classify') {
  1090. $form->form_project($_SERVER['PHP_SELF'] . '?facid=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1);
  1091. } else {
  1092. $form->form_project($_SERVER['PHP_SELF'] . '?facid=' . $object->id, $object->socid, $object->fk_project, 'none', 0, 0);
  1093. }
  1094. print '</td>';
  1095. print '</tr>';
  1096. }
  1097. // Bank Account
  1098. print '<tr><td class="nowrap">';
  1099. print '<table width="100%" class="nobordernopadding"><tr><td class="nowrap">';
  1100. print $langs->trans('BankAccount');
  1101. print '<td>';
  1102. if (($action != 'editbankaccount') && $user->rights->commande->creer && ! empty($object->brouillon))
  1103. print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=editbankaccount&amp;id='.$object->id.'">'.img_edit($langs->trans('SetBankAccount'),1).'</a></td>';
  1104. print '</tr></table>';
  1105. print '</td><td colspan="3">';
  1106. if ($action == 'editbankaccount')
  1107. {
  1108. $form->formSelectAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->fk_account, 'fk_account', 1);
  1109. }
  1110. else
  1111. {
  1112. $form->formSelectAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->fk_account, 'none');
  1113. }
  1114. print "</td>";
  1115. print '</tr>';
  1116. print "</table>";
  1117. print '<br>';
  1118. /*
  1119. * Recurrence
  1120. */
  1121. $title = $langs->trans("Recurrence");
  1122. print load_fiche_titre($title, '', 'calendar');
  1123. print '<table class="border" width="100%">';
  1124. // if "frequency" is empty or = 0, the reccurence is disabled
  1125. print '<tr><td width="20%">';
  1126. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1127. print $langs->trans('Frequency');
  1128. print '</td>';
  1129. if ($action != 'editfrequency' && ! empty($object->brouillon) && $user->rights->facture->creer)
  1130. print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editfrequency&amp;facid=' . $object->id . '">' . img_edit($langs->trans('Edit'), 1) . '</a></td>';
  1131. print '</tr></table>';
  1132. print '</td><td colspan="5">';
  1133. if ($action == 'editfrequency')
  1134. {
  1135. print '<form method="post" action="'.$_SERVER["PHP_SELF"] . '?facid=' . $object->id.'">';
  1136. print '<input type="hidden" name="action" value="setfrequency">';
  1137. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  1138. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  1139. print '<tr><td>';
  1140. print "<input type='text' name='frequency' value='".$object->frequency."' size='5' />&nbsp;".$form->selectarray('unit_frequency', array('d'=>$langs->trans('Day'), 'm'=>$langs->trans('Month'), 'y'=>$langs->trans('Year')), ($object->unit_frequency?$object->unit_frequency:'m'));
  1141. print '</td>';
  1142. print '<td align="left"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td>';
  1143. print '</tr></table></form>';
  1144. }
  1145. else
  1146. {
  1147. if ($object->frequency > 0)
  1148. {
  1149. print $langs->trans('FrequencyPer_'.$object->unit_frequency, $object->frequency);
  1150. }
  1151. else
  1152. {
  1153. print $langs->trans("NotARecurringInvoiceTemplate");
  1154. }
  1155. }
  1156. print '</td></tr>';
  1157. // Date when
  1158. print '<tr><td>';
  1159. if ($action == 'date_when' || $object->frequency > 0)
  1160. {
  1161. print $form->editfieldkey($langs->trans("NextDateToExecution"), 'date_when', $object->date_when, $object, $user->rights->facture->creer, 'day');
  1162. }
  1163. else
  1164. {
  1165. print $langs->trans("NextDateToExecution");
  1166. }
  1167. print '</td><td colspan="5">';
  1168. if ($action == 'date_when' || $object->frequency > 0)
  1169. {
  1170. print $form->editfieldval($langs->trans("NextDateToExecution"), 'date_when', $object->date_when, $object, $user->rights->facture->creer, 'day');
  1171. }
  1172. print '</td>';
  1173. print '</tr>';
  1174. // Max period / Rest period
  1175. print '<tr><td>';
  1176. if ($action == 'nb_gen_max' || $object->frequency > 0)
  1177. {
  1178. print $form->editfieldkey($langs->trans("MaxPeriodNumber"), 'nb_gen_max', $object->nb_gen_max, $object, $user->rights->facture->creer);
  1179. }
  1180. else
  1181. {
  1182. print $langs->trans("MaxPeriodNumber");
  1183. }
  1184. print '</td><td colspan="5">';
  1185. if ($action == 'nb_gen_max' || $object->frequency > 0)
  1186. {
  1187. print $form->editfieldval($langs->trans("MaxPeriodNumber"), 'nb_gen_max', $object->nb_gen_max?$object->nb_gen_max:'', $object, $user->rights->facture->creer);
  1188. }
  1189. else
  1190. {
  1191. print '';
  1192. }
  1193. print '</td>';
  1194. print '</tr>';
  1195. // Status of generated invoices
  1196. print '<tr><td>';
  1197. if ($action == 'auto_validate' || $object->frequency > 0)
  1198. print $form->editfieldkey($langs->trans("StatusOfGeneratedInvoices"), 'auto_validate', $object->auto_validate, $object, $user->rights->facture->creer);
  1199. else
  1200. print $langs->trans("StatusOfGeneratedInvoices");
  1201. print '</td><td colspan="5">';
  1202. $select = 'select;0:'.$langs->trans('BillStatusDraft').',1:'.$langs->trans('BillStatusValidated');
  1203. if ($action == 'auto_validate' || $object->frequency > 0)
  1204. {
  1205. print $form->editfieldval($langs->trans("StatusOfGeneratedInvoices"), 'auto_validate', $object->auto_validate, $object, $user->rights->facture->creer, $select);
  1206. }
  1207. print '</td>';
  1208. print '</tr>';
  1209. print '</table>';
  1210. print '<br>';
  1211. // Frequencry/Recurring section
  1212. if ($object->frequency > 0)
  1213. {
  1214. if (empty($conf->cron->enabled))
  1215. {
  1216. print info_admin($langs->trans("EnableAndSetupModuleCron", $langs->transnoentitiesnoconv("Module2300Name")));
  1217. }
  1218. print '<table class="border" width="100%">';
  1219. // Nb of generation already done
  1220. print '<tr><td width="20%">'.$langs->trans("NbOfGenerationDone").'</td>';
  1221. print '<td>';
  1222. print $object->nb_gen_done?$object->nb_gen_done:'0';
  1223. print '</td>';
  1224. print '</tr>';
  1225. // Date last
  1226. print '<tr><td>';
  1227. print $langs->trans("DateLastGeneration");
  1228. print '</td><td colspan="5">';
  1229. print dol_print_date($object->date_last_gen, 'dayhour');
  1230. print '</td>';
  1231. print '</tr>';
  1232. print '</table>';
  1233. print '<br>';
  1234. }
  1235. // Lines
  1236. print ' <form name="addproduct" id="addproduct" action="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . (($action != 'editline') ? '#add' : '#line_' . GETPOST('lineid')) . '" method="POST">
  1237. <input type="hidden" name="token" value="' . $_SESSION ['newtoken'] . '">
  1238. <input type="hidden" name="action" value="' . (($action != 'editline') ? 'addline' : 'updateligne') . '">
  1239. <input type="hidden" name="mode" value="">
  1240. <input type="hidden" name="id" value="' . $object->id . '">
  1241. ';
  1242. if (! empty($conf->use_javascript_ajax) && $object->statut == 0) {
  1243. include DOL_DOCUMENT_ROOT . '/core/tpl/ajaxrow.tpl.php';
  1244. }
  1245. print '<table id="tablelines" class="noborder noshadow" width="100%">';
  1246. // Show object lines
  1247. if (! empty($object->lines))
  1248. {
  1249. //$disableedit=1;
  1250. //$disablemove=1;
  1251. $ret = $object->printObjectLines($action, $mysoc, $soc, $lineid, 0); // No date selector for template invoice
  1252. }
  1253. // Form to add new line
  1254. if ($object->statut == 0 && $user->rights->facture->creer && $action != 'valid' && $action != 'editline')
  1255. {
  1256. if ($action != 'editline')
  1257. {
  1258. $var = true;
  1259. // Add free products/services
  1260. $object->formAddObjectLine(0, $mysoc, $soc); // No date selector for template invoice
  1261. $parameters = array();
  1262. $reshook = $hookmanager->executeHooks('formAddObjectLine', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  1263. }
  1264. }
  1265. print "</table>\n";
  1266. print "</form>\n";
  1267. dol_fiche_end();
  1268. /**
  1269. * Barre d'actions
  1270. */
  1271. print '<div class="tabsAction">';
  1272. //if ($object->statut == Facture::STATUS_DRAFT) // there is no draft status on templates.
  1273. //{
  1274. if ($user->rights->facture->creer)
  1275. {
  1276. if (! empty($object->frequency) && $object->nb_gen_max > 0 && ($object->nb_gen_done >= $object->nb_gen_max))
  1277. {
  1278. print '<div class="inline-block divButAction"><a class="butActionRefused" href="#" title="'.dol_escape_htmltag($langs->trans("MaxGenerationReached")).'">'.$langs->trans("CreateBill").'</a></div>';
  1279. }
  1280. else
  1281. {
  1282. if (empty($object->frequency) || $object->date_when <= $today)
  1283. {
  1284. print '<div class="inline-block divButAction"><a class="butAction" href="'.DOL_URL_ROOT.'/compta/facture.php?action=create&amp;socid='.$object->thirdparty->id.'&amp;fac_rec='.$object->id.'">'.$langs->trans("CreateBill").'</a></div>';
  1285. }
  1286. else
  1287. {
  1288. print '<div class="inline-block divButAction"><a class="butActionRefused" href="#" title="'.dol_escape_htmltag($langs->trans("DateIsNotEnough")).'">'.$langs->trans("CreateBill").'</a></div>';
  1289. }
  1290. }
  1291. }
  1292. else
  1293. {
  1294. print '<div class="inline-block divButAction"><a class="butActionRefused" href="#">'.$langs->trans("CreateBill").'</a></div>';
  1295. }
  1296. //}
  1297. //if ($object->statut == Facture::STATUS_DRAFT && $user->rights->facture->supprimer)
  1298. if ($user->rights->facture->supprimer)
  1299. {
  1300. print '<div class="inline-block divButAction"><a class="butActionDelete" href="'.$_SERVER['PHP_SELF'].'?action=delete&id='.$object->id.'">'.$langs->trans('Delete').'</a></div>';
  1301. }
  1302. print '</div>';
  1303. print '<div class="fichecenter"><div class="fichehalfleft">';
  1304. print '<a name="builddoc"></a>'; // ancre
  1305. // Linked object block
  1306. $somethingshown = $form->showLinkedObjectBlock($object);
  1307. print '</div></div>';
  1308. }
  1309. else
  1310. {
  1311. /*
  1312. * List mode
  1313. */
  1314. $sql = "SELECT s.nom as name, s.rowid as socid, f.rowid as facid, f.titre, f.total, f.tva as total_vat, f.total_ttc, f.frequency,";
  1315. $sql.= " f.date_last_gen, f.date_when";
  1316. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture_rec as f";
  1317. $sql.= " WHERE f.fk_soc = s.rowid";
  1318. $sql.= " AND f.entity = ".$conf->entity;
  1319. if ($search_ref) $sql .= natural_search('f.titre', $search_ref);
  1320. if ($search_societe) $sql .= natural_search('s.nom', $search_societe);
  1321. if ($search_frequency) $sql .= natural_search('f.frequency', $search_frequency);
  1322. if ($search_montant_ht != '') $sql.= natural_search('f.total', $search_montant_ht, 1);
  1323. if ($search_montant_vat != '') $sql.= natural_search('f.tva', $search_montant_vat, 1);
  1324. if ($search_montant_ttc != '') $sql.= natural_search('f.total_ttc', $search_montant_ttc, 1);
  1325. if ($month > 0)
  1326. {
  1327. if ($year > 0 && empty($day))
  1328. $sql.= " AND f.date_last_gen BETWEEN '".$db->idate(dol_get_first_day($year,$month,false))."' AND '".$db->idate(dol_get_last_day($year,$month,false))."'";
  1329. else if ($year > 0 && ! empty($day))
  1330. $sql.= " AND f.date_last_gen BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $month, $day, $year))."' AND '".$db->idate(dol_mktime(23, 59, 59, $month, $day, $year))."'";
  1331. else
  1332. $sql.= " AND date_format(f.date_last_gen, '%m') = '".$month."'";
  1333. }
  1334. else if ($year > 0)
  1335. {
  1336. $sql.= " AND f.date_last_gen BETWEEN '".$db->idate(dol_get_first_day($year,1,false))."' AND '".$db->idate(dol_get_last_day($year,12,false))."'";
  1337. }
  1338. if ($month_date_when > 0)
  1339. {
  1340. if ($year_date_when > 0 && empty($day_date_when))
  1341. $sql.= " AND f.date_when BETWEEN '".$db->idate(dol_get_first_day($year_date_when,$month_date_when,false))."' AND '".$db->idate(dol_get_last_day($year_date_when,$month_date_when,false))."'";
  1342. else if ($year_date_when > 0 && ! empty($day_date_when))
  1343. $sql.= " AND f.date_date_when_reglement BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $month_date_when, $day_date_when, $year_date_when))."' AND '".$db->idate(dol_mktime(23, 59, 59, $month_date_when, $day_date_when, $year_date_when))."'";
  1344. else
  1345. $sql.= " AND date_format(f.date_when, '%m') = '".$month_date_when."'";
  1346. }
  1347. else if ($year_date_when > 0)
  1348. {
  1349. $sql.= " AND f.date_when BETWEEN '".$db->idate(dol_get_first_day($year_date_when,1,false))."' AND '".$db->idate(dol_get_last_day($year_date_when,12,false))."'";
  1350. }
  1351. $nbtotalofrecords = 0;
  1352. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  1353. {
  1354. $result = $db->query($sql);
  1355. $nbtotalofrecords = $db->num_rows($result);
  1356. }
  1357. $sql.= $db->order($sortfield, $sortorder);
  1358. $sql.= $db->plimit($limit+1,$offset);
  1359. $resql = $db->query($sql);
  1360. if ($resql)
  1361. {
  1362. $num = $db->num_rows($resql);
  1363. $param='&socid='.$socid;
  1364. if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.$contextpage;
  1365. if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.$limit;
  1366. if ($day) $param.='&day='.$day;
  1367. if ($month) $param.='&month='.$month;
  1368. if ($year) $param.='&year=' .$year;
  1369. if ($day_date_when) $param.='&day_date_when='.$day_date_when;
  1370. if ($month_date_when) $param.='&month_date_when='.$month_date_when;
  1371. if ($year_date_when) $param.='&year_date_when=' .$year_date_when;
  1372. if ($search_ref) $param.='&search_ref=' .$search_ref;
  1373. if ($search_societe) $param.='&search_societe=' .$search_societe;
  1374. if ($search_montant_ht != '') $param.='&search_montant_ht='.$search_montant_ht;
  1375. if ($search_montant_vat != '') $param.='&search_montant_vat='.$search_montant_vat;
  1376. if ($search_montant_ttc != '') $param.='&search_montant_ttc='.$search_montant_ttc;
  1377. if ($search_frequency) $param.='&search_frequency=' .$search_frequency;
  1378. if ($option) $param.="&option=".$option;
  1379. if ($optioncss != '') $param.='&optioncss='.$optioncss;
  1380. // Add $param from extra fields
  1381. foreach ($search_array_options as $key => $val)
  1382. {
  1383. $crit=$val;
  1384. $tmpkey=preg_replace('/search_options_/','',$key);
  1385. if ($val != '') $param.='&search_options_'.$tmpkey.'='.urlencode($val);
  1386. }
  1387. $massactionbutton=$form->selectMassAction('', $massaction == 'presend' ? array() : array('presend'=>$langs->trans("SendByMail"), 'builddoc'=>$langs->trans("PDFMerge")));
  1388. print '<form method="POST" name="searchFormList" action="'.$_SERVER["PHP_SELF"].'">'."\n";
  1389. if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  1390. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  1391. print '<input type="hidden" name="action" value="list">';
  1392. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  1393. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  1394. print '<input type="hidden" name="viewstatut" value="'.$viewstatut.'">';
  1395. print_barre_liste($langs->trans("RepeatableInvoices"),$page,$_SERVER['PHP_SELF'],$param,$sortfield,$sortorder,'',$num,$nbtotalofrecords,'title_accountancy.png',0,'','',$limit);
  1396. print $langs->trans("ToCreateAPredefinedInvoice", $langs->transnoentitiesnoconv("ChangeIntoRepeatableInvoice")).'<br><br>';
  1397. $i = 0;
  1398. print '<table class="noborder" width="100%">';
  1399. print '<tr class="liste_titre">';
  1400. print_liste_field_titre($langs->trans("Ref"),$_SERVER['PHP_SELF'],"f.titre","",$param,"",$sortfield,$sortorder);
  1401. print_liste_field_titre($langs->trans("ThirdParty"),$_SERVER['PHP_SELF'],"s.nom","",$param,"",$sortfield,$sortorder);
  1402. print_liste_field_titre($langs->trans("AmountHT"),$_SERVER['PHP_SELF'],"f.total","",$param,'align="right"',$sortfield,$sortorder);
  1403. print_liste_field_titre($langs->trans("AmountVAT"),$_SERVER['PHP_SELF'],"f.tva","",$param,'align="right"',$sortfield,$sortorder);
  1404. print_liste_field_titre($langs->trans("AmountTTC"),$_SERVER['PHP_SELF'],"f.total_ttc","",$param,'align="right"',$sortfield,$sortorder);
  1405. print_liste_field_titre($langs->trans("RecurringInvoiceTemplate"),$_SERVER['PHP_SELF'],"f.frequency","",$param,'align="center"',$sortfield,$sortorder);
  1406. print_liste_field_titre($langs->trans("DateLastGeneration"),$_SERVER['PHP_SELF'],"f.date_last_gen","",$param,'align="center"',$sortfield,$sortorder);
  1407. print_liste_field_titre($langs->trans("NextDateToExecution"),$_SERVER['PHP_SELF'],"f.date_when","",$param,'align="center"',$sortfield,$sortorder);
  1408. print_liste_field_titre(''); // Field may contains ling text
  1409. print "</tr>\n";
  1410. // Filters lines
  1411. print '<tr class="liste_titre">';
  1412. // Ref
  1413. if (! empty($arrayfields['f.titre']['checked']))
  1414. {
  1415. print '<td class="liste_titre" align="left">';
  1416. print '<input class="flat" size="6" type="text" name="search_ref" value="'.$search_ref.'">';
  1417. print '</td>';
  1418. }
  1419. // Thirpdarty
  1420. if (! empty($arrayfields['s.nom']['checked']))
  1421. {
  1422. print '<td class="liste_titre" align="left"><input class="flat" type="text" size="8" name="search_societe" value="'.$search_societe.'"></td>';
  1423. }
  1424. if (! empty($arrayfields['f.total']['checked']))
  1425. {
  1426. // Amount
  1427. print '<td class="liste_titre" align="right">';
  1428. print '<input class="flat" type="text" size="5" name="search_montant_ht" value="'.$search_montant_ht.'">';
  1429. print '</td>';
  1430. }
  1431. if (! empty($arrayfields['f.tva']['checked']))
  1432. {
  1433. // Amount
  1434. print '<td class="liste_titre" align="right">';
  1435. print '<input class="flat" type="text" size="5" name="search_montant_vat" value="'.$search_montant_vat.'">';
  1436. print '</td>';
  1437. }
  1438. if (! empty($arrayfields['f.total_ttc']['checked']))
  1439. {
  1440. // Amount
  1441. print '<td class="liste_titre" align="right">';
  1442. print '<input class="flat" type="text" size="5" name="search_montant_ttc" value="'.$search_montant_ttc.'">';
  1443. print '</td>';
  1444. }
  1445. if (! empty($arrayfields['f.frequency']['checked']))
  1446. {
  1447. // Amount
  1448. print '<td class="liste_titre" align="right">';
  1449. //print '<input class="flat" type="text" size="5" name="search_frequency" value="'.$search_frequency.'">';
  1450. print '</td>';
  1451. }
  1452. // Date invoice
  1453. if (! empty($arrayfields['f.date_last_gen']['checked']))
  1454. {
  1455. print '<td class="liste_titre" align="center">';
  1456. if (! empty($conf->global->MAIN_LIST_FILTER_ON_DAY)) print '<input class="flat" type="text" size="1" maxlength="2" name="day" value="'.$day.'">';
  1457. print '<input class="flat" type="text" size="1" maxlength="2" name="month" value="'.$month.'">';
  1458. $formother->select_year($year?$year:-1,'year',1, 20, 5);
  1459. print '</td>';
  1460. }
  1461. // Date due
  1462. if (! empty($arrayfields['f.date_when']['checked']))
  1463. {
  1464. print '<td class="liste_titre" align="center">';
  1465. if (! empty($conf->global->MAIN_LIST_FILTER_ON_DAY)) print '<input class="flat" type="text" size="1" maxlength="2" name="day_date_when" value="'.$day_date_when.'">';
  1466. print '<input class="flat" type="text" size="1" maxlength="2" name="month_date_when" value="'.$month_date_when.'">';
  1467. $formother->select_year($year_date_when?$year_date_when:-1,'year_date_when',1, 20, 5);
  1468. print '</td>';
  1469. }
  1470. // Extra fields
  1471. if (is_array($extrafields->attribute_label) && count($extrafields->attribute_label))
  1472. {
  1473. foreach($extrafields->attribute_label as $key => $val)
  1474. {
  1475. if (! empty($arrayfields["ef.".$key]['checked']))
  1476. {
  1477. $align=$extrafields->getAlignFlag($key);
  1478. $typeofextrafield=$extrafields->attribute_type[$key];
  1479. print '<td class="liste_titre'.($align?' '.$align:'').'">';
  1480. if (in_array($typeofextrafield, array('varchar', 'int', 'double', 'select')))
  1481. {
  1482. $crit=$val;
  1483. $tmpkey=preg_replace('/search_options_/','',$key);
  1484. $searchclass='';
  1485. if (in_array($typeofextrafield, array('varchar', 'select'))) $searchclass='searchstring';
  1486. if (in_array($typeofextrafield, array('int', 'double'))) $searchclass='searchnum';
  1487. print '<input class="flat'.($searchclass?' '.$searchclass:'').'" size="4" type="text" name="search_options_'.$tmpkey.'" value="'.dol_escape_htmltag($search_array_options['search_options_'.$tmpkey]).'">';
  1488. }
  1489. print '</td>';
  1490. }
  1491. }
  1492. }
  1493. // Fields from hook
  1494. $parameters=array('arrayfields'=>$arrayfields);
  1495. $reshook=$hookmanager->executeHooks('printFieldListOption',$parameters); // Note that $action and $object may have been modified by hook
  1496. print $hookmanager->resPrint;
  1497. // Date creation
  1498. if (! empty($arrayfields['f.datec']['checked']))
  1499. {
  1500. print '<td class="liste_titre">';
  1501. print '</td>';
  1502. }
  1503. // Date modification
  1504. if (! empty($arrayfields['f.tms']['checked']))
  1505. {
  1506. print '<td class="liste_titre">';
  1507. print '</td>';
  1508. }
  1509. // Action column
  1510. print '<td class="liste_titre" align="middle">';
  1511. $searchpitco=$form->showFilterAndCheckAddButtons(0, 'checkforselect', 1);
  1512. print $searchpitco;
  1513. print '</td>';
  1514. print "</tr>\n";
  1515. if ($num > 0)
  1516. {
  1517. $var=true;
  1518. while ($i < min($num,$limit))
  1519. {
  1520. $objp = $db->fetch_object($resql);
  1521. $var=!$var;
  1522. print "<tr ".$bc[$var].">";
  1523. print '<td><a href="'.$_SERVER['PHP_SELF'].'?id='.$objp->facid.'">'.img_object($langs->trans("ShowBill"),"bill").' '.$objp->titre;
  1524. print "</a></td>\n";
  1525. $companystatic->id=$objp->socid;
  1526. $companystatic->name=$objp->name;
  1527. print '<td>'.$companystatic->getNomUrl(1,'customer').'</td>';
  1528. print '<td align="right">'.price($objp->total).'</td>'."\n";
  1529. print '<td align="right">'.price($objp->total_vat).'</td>'."\n";
  1530. print '<td align="right">'.price($objp->total_ttc).'</td>'."\n";
  1531. print '<td align="center">'.yn($objp->frequency?1:0).'</td>';
  1532. print '<td align="center">'.($objp->frequency ? dol_print_date($objp->date_last_gen,'day') : '').'</td>';
  1533. print '<td align="center">'.($objp->frequency ? dol_print_date($objp->date_when,'day') : '').'</td>';
  1534. print '<td align="center">';
  1535. if ($user->rights->facture->creer)
  1536. {
  1537. if (empty($objp->frequency) || $db->jdate($objp->date_when) <= $today)
  1538. {
  1539. print '<a href="'.DOL_URL_ROOT.'/compta/facture.php?action=create&amp;socid='.$objp->socid.'&amp;fac_rec='.$objp->facid.'">';
  1540. print $langs->trans("CreateBill").'</a>';
  1541. }
  1542. else
  1543. {
  1544. print $langs->trans("DateIsNotEnough");
  1545. }
  1546. }
  1547. else
  1548. {
  1549. print "&nbsp;";
  1550. }
  1551. print "</td>";
  1552. print "</tr>\n";
  1553. $i++;
  1554. }
  1555. }
  1556. else print '<tr '.$bc[false].'><td colspan="9">'.$langs->trans("NoneF").'</td></tr>';
  1557. print "</table>";
  1558. $db->free($resql);
  1559. }
  1560. else
  1561. {
  1562. dol_print_error($db);
  1563. }
  1564. }
  1565. }
  1566. llxFooter();
  1567. $db->close();