fiche-rec.php 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  1. <?php
  2. /* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 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@open-dsi.fr>
  10. * Copyright (C) 2016 Meziane Sof <virtualsof@yahoo.fr>
  11. * Copyright (C) 2017-2018 Frédéric France <frederic.france@netlogic.fr>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26. /**
  27. * \file htdocs/compta/facture/fiche-rec.php
  28. * \ingroup facture
  29. * \brief Page to show predefined invoice
  30. */
  31. require '../../main.inc.php';
  32. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture-rec.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  35. if (! empty($conf->projet->enabled)) {
  36. include_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php';
  37. //include_once DOL_DOCUMENT_ROOT . '/core/class/html.formprojet.class.php';
  38. }
  39. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formprojet.class.php';
  40. require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
  41. require_once DOL_DOCUMENT_ROOT . '/core/lib/invoice.lib.php';
  42. require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php';
  43. // Load translation files required by the page
  44. $langs->loadLangs(array('bills', 'compta', 'admin', 'other', 'products'));
  45. $action = GETPOST('action','alpha');
  46. $massaction = GETPOST('massaction','alpha');
  47. $show_files = GETPOST('show_files','int');
  48. $confirm = GETPOST('confirm','alpha');
  49. $cancel = GETPOST('cancel', 'alpha');
  50. $toselect = GETPOST('toselect', 'array');
  51. $contextpage= GETPOST('contextpage','aZ')?GETPOST('contextpage','aZ'):'invoicetemplatelist'; // To manage different context of search
  52. // Security check
  53. $id=(GETPOST('facid','int')?GETPOST('facid','int'):GETPOST('id','int'));
  54. $lineid=GETPOST('lineid','int');
  55. $ref=GETPOST('ref','alpha');
  56. if ($user->societe_id) $socid=$user->societe_id;
  57. $objecttype = 'facture_rec';
  58. if ($action == "create" || $action == "add") $objecttype = '';
  59. $result = restrictedArea($user, 'facture', $id, $objecttype);
  60. $projectid = GETPOST('projectid','int');
  61. $year_date_when=GETPOST('year_date_when');
  62. $month_date_when=GETPOST('month_date_when');
  63. $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
  64. $sortfield = GETPOST("sortfield",'alpha');
  65. $sortorder = GETPOST("sortorder",'alpha');
  66. $page = GETPOST("page",'int');
  67. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  68. $offset = $limit * $page;
  69. if (! $sortorder) $sortorder='DESC';
  70. if (! $sortfield) $sortfield='f.titre';
  71. $pageprev = $page - 1;
  72. $pagenext = $page + 1;
  73. $object = new FactureRec($db);
  74. if (($id > 0 || $ref) && $action != 'create' && $action != 'add')
  75. {
  76. $ret = $object->fetch($id, $ref);
  77. if (!$ret)
  78. {
  79. setEventMessages($langs->trans("ErrorRecordNotFound"), null, 'errors');
  80. }
  81. }
  82. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  83. $hookmanager->initHooks(array('invoicereccard','globalcard'));
  84. $extrafields = new ExtraFields($db);
  85. // fetch optionals attributes and labels
  86. $extralabels = $extrafields->fetch_name_optionals_label('facture_rec');
  87. $search_array_options=$extrafields->getOptionalsFromPost($object->table_element,'','search_');
  88. $permissionnote = $user->rights->facture->creer; // Used by the include of actions_setnotes.inc.php
  89. $permissiondellink=$user->rights->facture->creer; // Used by the include of actions_dellink.inc.php
  90. $permissiontoedit = $user->rights->facture->creer; // Used by the include of actions_lineupdonw.inc.php
  91. $now = dol_now();
  92. /*
  93. * Actions
  94. */
  95. if (GETPOST('cancel','alpha')) { $action='list'; $massaction=''; }
  96. if (! GETPOST('confirmmassaction','alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') { $massaction=''; }
  97. $parameters = array('socid' => $socid);
  98. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  99. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  100. if (empty($reshook))
  101. {
  102. if (GETPOST('cancel','alpha')) $action='';
  103. // Selection of new fields
  104. include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
  105. // Set note
  106. include DOL_DOCUMENT_ROOT.'/core/actions_setnotes.inc.php'; // Must be include, not include_once
  107. include DOL_DOCUMENT_ROOT.'/core/actions_dellink.inc.php'; // Must be include, not include_once
  108. include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php'; // Must be include, not include_once
  109. // Mass actions
  110. /*$objectclass='MyObject';
  111. $objectlabel='MyObject';
  112. $permtoread = $user->rights->mymodule->read;
  113. $permtodelete = $user->rights->mymodule->delete;
  114. $uploaddir = $conf->mymodule->dir_output;
  115. include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php';*/
  116. // Create predefined invoice
  117. if ($action == 'add')
  118. {
  119. if (! GETPOST('titre'))
  120. {
  121. setEventMessages($langs->transnoentities("ErrorFieldRequired",$langs->trans("Title")), null, 'errors');
  122. $action = "create";
  123. $error++;
  124. }
  125. $frequency=GETPOST('frequency', 'int');
  126. $reyear=GETPOST('reyear');
  127. $remonth=GETPOST('remonth');
  128. $reday=GETPOST('reday');
  129. $rehour=GETPOST('rehour');
  130. $remin=GETPOST('remin');
  131. $nb_gen_max=GETPOST('nb_gen_max', 'int');
  132. //if (empty($nb_gen_max)) $nb_gen_max =0;
  133. if (GETPOST('frequency'))
  134. {
  135. if (empty($reyear) || empty($remonth) || empty($reday))
  136. {
  137. setEventMessages($langs->transnoentities("ErrorFieldRequired",$langs->trans("Date")), null, 'errors');
  138. $action = "create";
  139. $error++;
  140. }
  141. if ($nb_gen_max === '')
  142. {
  143. setEventMessages($langs->transnoentities("ErrorFieldRequired",$langs->trans("MaxPeriodNumber")), null, 'errors');
  144. $action = "create";
  145. $error++;
  146. }
  147. }
  148. if (! $error)
  149. {
  150. $object->titre = GETPOST('titre', 'alpha');
  151. $object->note_private = GETPOST('note_private','none');
  152. $object->note_public = GETPOST('note_public','none');
  153. $object->modelpdf = GETPOST('modelpdf', 'alpha');
  154. $object->usenewprice = GETPOST('usenewprice');
  155. $object->frequency = $frequency;
  156. $object->unit_frequency = GETPOST('unit_frequency', 'alpha');
  157. $object->nb_gen_max = $nb_gen_max;
  158. $object->auto_validate = GETPOST('auto_validate', 'int');
  159. $object->generate_pdf = GETPOST('generate_pdf', 'int');
  160. $object->fk_project = $projectid;
  161. $date_next_execution = dol_mktime($rehour, $remin, 0, $remonth, $reday, $reyear);
  162. $object->date_when = $date_next_execution;
  163. // Get first contract linked to invoice used to generate template (facid is id of source invoice)
  164. if (GETPOST('facid','int') > 0)
  165. {
  166. $srcObject = new Facture($db);
  167. $srcObject->fetch(GETPOST('facid','int'));
  168. $srcObject->fetchObjectLinked();
  169. if (! empty($srcObject->linkedObjectsIds['contrat']))
  170. {
  171. $contractidid = reset($srcObject->linkedObjectsIds['contrat']);
  172. $object->origin = 'contrat';
  173. $object->origin_id = $contractidid;
  174. $object->linked_objects[$object->origin] = $object->origin_id;
  175. }
  176. }
  177. $db->begin();
  178. $oldinvoice = new Facture($db);
  179. $oldinvoice->fetch(GETPOST('facid','int'));
  180. $result = $object->create($user, $oldinvoice->id);
  181. if ($result > 0)
  182. {
  183. $result=$oldinvoice->delete($user, 1);
  184. if ($result < 0)
  185. {
  186. $error++;
  187. setEventMessages($oldinvoice->error, $oldinvoice->errors, 'errors');
  188. $action = "create";
  189. }
  190. }
  191. else
  192. {
  193. $error++;
  194. setEventMessages($object->error, $object->errors, 'errors');
  195. $action = "create";
  196. }
  197. if (! $error)
  198. {
  199. $db->commit();
  200. header("Location: " . $_SERVER['PHP_SELF'] . '?facid=' . $object->id);
  201. exit;
  202. }
  203. else
  204. {
  205. $db->rollback();
  206. $error++;
  207. setEventMessages($object->error, $object->errors, 'errors');
  208. $action = "create";
  209. }
  210. }
  211. }
  212. // Delete
  213. if ($action == 'confirm_deleteinvoice' && $confirm == 'yes' && $user->rights->facture->supprimer)
  214. {
  215. $object->delete($user);
  216. header("Location: " . DOL_URL_ROOT.'/compta/facture/invoicetemplate_list.php');
  217. exit;
  218. }
  219. // Update field
  220. // Set condition
  221. if ($action == 'setconditions' && $user->rights->facture->creer)
  222. {
  223. $result=$object->setPaymentTerms(GETPOST('cond_reglement_id', 'int'));
  224. }
  225. // Set mode
  226. elseif ($action == 'setmode' && $user->rights->facture->creer)
  227. {
  228. $result=$object->setPaymentMethods(GETPOST('mode_reglement_id', 'int'));
  229. }
  230. // Set project
  231. elseif ($action == 'classin' && $user->rights->facture->creer)
  232. {
  233. $object->setProject(GETPOST('projectid', 'int'));
  234. }
  235. // Set bank account
  236. elseif ($action == 'setref' && $user->rights->facture->creer)
  237. {
  238. //var_dump(GETPOST('ref', 'alpha'));exit;
  239. $result=$object->setValueFrom('titre', GETPOST('ref', 'alpha'), '', null, 'text', '', $user, 'BILLREC_MODIFY');
  240. if ($result > 0)
  241. {
  242. $object->titre = GETPOST('ref', 'alpha');
  243. $object->ref = $object->titre;
  244. }
  245. else dol_print_error($db, $object->error, $object->errors);
  246. }
  247. // Set bank account
  248. elseif ($action == 'setbankaccount' && $user->rights->facture->creer)
  249. {
  250. $result=$object->setBankAccount(GETPOST('fk_account', 'int'));
  251. }
  252. // Set frequency and unit frequency
  253. elseif ($action == 'setfrequency' && $user->rights->facture->creer)
  254. {
  255. $object->setFrequencyAndUnit(GETPOST('frequency', 'int'), GETPOST('unit_frequency', 'alpha'));
  256. }
  257. // Set next date of execution
  258. elseif ($action == 'setdate_when' && $user->rights->facture->creer)
  259. {
  260. $date = dol_mktime(GETPOST('date_whenhour'), GETPOST('date_whenmin'), 0, GETPOST('date_whenmonth'), GETPOST('date_whenday'), GETPOST('date_whenyear'));
  261. if (!empty($date)) $object->setNextDate($date);
  262. }
  263. // Set max period
  264. elseif ($action == 'setnb_gen_max' && $user->rights->facture->creer)
  265. {
  266. $object->setMaxPeriod(GETPOST('nb_gen_max', 'int'));
  267. }
  268. // Set auto validate
  269. elseif ($action == 'setauto_validate' && $user->rights->facture->creer)
  270. {
  271. $object->setAutoValidate(GETPOST('auto_validate', 'int'));
  272. }
  273. // Set generate pdf
  274. elseif ($action == 'setgenerate_pdf' && $user->rights->facture->creer)
  275. {
  276. $object->setGeneratepdf(GETPOST('generate_pdf', 'int'));
  277. }
  278. // Set model pdf
  279. elseif ($action == 'setmodelpdf' && $user->rights->facture->creer)
  280. {
  281. $object->setModelpdf(GETPOST('modelpdf', 'alpha'));
  282. }
  283. // Set status disabled
  284. elseif ($action == 'disable' && $user->rights->facture->creer)
  285. {
  286. $db->begin();
  287. $object->fetch($id);
  288. $res = $object->setValueFrom('suspended', 1);
  289. if ($res <= 0)
  290. {
  291. $error++;
  292. }
  293. if (! $error)
  294. {
  295. $db->commit();
  296. }
  297. else
  298. {
  299. $db->rollback();
  300. setEventMessages($object->error, $object->errors, 'errors');
  301. }
  302. }
  303. // Set status enabled
  304. elseif ($action == 'enable' && $user->rights->facture->creer)
  305. {
  306. $db->begin();
  307. $object->fetch($id);
  308. $res = $object->setValueFrom('suspended', 0);
  309. if ($res <= 0)
  310. {
  311. $error++;
  312. }
  313. if (! $error)
  314. {
  315. $db->commit();
  316. }
  317. else
  318. {
  319. $db->rollback();
  320. setEventMessages($object->error, $object->errors, 'errors');
  321. }
  322. }
  323. // Delete line
  324. if ($action == 'confirm_deleteline' && $confirm == 'yes' && $user->rights->facture->creer)
  325. {
  326. $object->fetch($id);
  327. $object->fetch_thirdparty();
  328. $db->begin();
  329. $line=new FactureLigneRec($db);
  330. // For triggers
  331. $line->id = $lineid;
  332. if ($line->delete($user) > 0)
  333. {
  334. $result=$object->update_price(1);
  335. if ($result > 0)
  336. {
  337. $db->commit();
  338. $object->fetch($object->id); // Reload lines
  339. }
  340. else
  341. {
  342. $db->rollback();
  343. setEventMessages($db->lasterror(), null, 'errors');
  344. }
  345. }
  346. else
  347. {
  348. $db->rollback();
  349. setEventMessages($line->error, $line->errors, 'errors');
  350. }
  351. }
  352. elseif ($action == 'update_extras')
  353. {
  354. $object->oldcopy = dol_clone($object);
  355. // Fill array 'array_options' with data from update form
  356. $extralabels = $extrafields->fetch_name_optionals_label($object->table_element);
  357. $ret = $extrafields->setOptionalsFromPost($extralabels, $object, GETPOST('attribute','none'));
  358. if ($ret < 0) $error++;
  359. if (! $error)
  360. {
  361. $result = $object->insertExtraFields('BILLREC_MODIFY');
  362. if ($result < 0)
  363. {
  364. setEventMessages($object->error, $object->errors, 'errors');
  365. $error++;
  366. }
  367. }
  368. }
  369. // Add a new line
  370. if ($action == 'addline' && $user->rights->facture->creer)
  371. {
  372. $langs->load('errors');
  373. $error = 0;
  374. // Set if we used free entry or predefined product
  375. $predef='';
  376. $product_desc=(GETPOST('dp_desc')?GETPOST('dp_desc'):'');
  377. $price_ht = GETPOST('price_ht');
  378. if (GETPOST('prod_entry_mode') == 'free')
  379. {
  380. $idprod=0;
  381. $tva_tx = (GETPOST('tva_tx') ? GETPOST('tva_tx') : 0);
  382. }
  383. else
  384. {
  385. $idprod=GETPOST('idprod', 'int');
  386. $tva_tx = '';
  387. }
  388. $qty = GETPOST('qty' . $predef);
  389. $remise_percent = GETPOST('remise_percent' . $predef);
  390. // Extrafields
  391. $extrafieldsline = new ExtraFields($db);
  392. $extralabelsline = $extrafieldsline->fetch_name_optionals_label($object->table_element_line);
  393. $array_options = $extrafieldsline->getOptionalsFromPost($extralabelsline, $predef);
  394. // Unset extrafield
  395. if (is_array($extralabelsline))
  396. {
  397. // Get extra fields
  398. foreach ($extralabelsline as $key => $value) {
  399. unset($_POST["options_" . $key . $predef]);
  400. }
  401. }
  402. if (empty($idprod) && ($price_ht < 0) && ($qty < 0)) {
  403. setEventMessages($langs->trans('ErrorBothFieldCantBeNegative', $langs->transnoentitiesnoconv('UnitPriceHT'), $langs->transnoentitiesnoconv('Qty')), null, 'errors');
  404. $error ++;
  405. }
  406. if (GETPOST('prod_entry_mode') == 'free' && empty($idprod) && GETPOST('type') < 0) {
  407. setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Type')), null, 'errors');
  408. $error ++;
  409. }
  410. if (GETPOST('prod_entry_mode') == 'free' && empty($idprod) && (! ($price_ht >= 0) || $price_ht == '')) // Unit price can be 0 but not ''
  411. {
  412. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("UnitPriceHT")), null, 'errors');
  413. $error ++;
  414. }
  415. if ($qty == '') {
  416. setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Qty')), null, 'errors');
  417. $error ++;
  418. }
  419. if (GETPOST('prod_entry_mode') == 'free' && empty($idprod) && empty($product_desc)) {
  420. setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Description')), null, 'errors');
  421. $error ++;
  422. }
  423. if ($qty < 0) {
  424. $langs->load("errors");
  425. setEventMessages($langs->trans('ErrorQtyForCustomerInvoiceCantBeNegative'), null, 'errors');
  426. $error ++;
  427. }
  428. if (! $error && ($qty >= 0) && (! empty($product_desc) || ! empty($idprod)))
  429. {
  430. $ret = $object->fetch($id);
  431. if ($ret < 0) {
  432. dol_print_error($db, $object->error);
  433. exit();
  434. }
  435. $ret = $object->fetch_thirdparty();
  436. // Clean parameters
  437. $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'));
  438. $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'));
  439. $price_base_type = (GETPOST('price_base_type', 'alpha') ? GETPOST('price_base_type', 'alpha') : 'HT');
  440. // Define special_code for special lines
  441. $special_code = 0;
  442. // if (empty($_POST['qty'])) $special_code=3; // Options should not exists on invoices
  443. // Ecrase $pu par celui du produit
  444. // Ecrase $desc par celui du produit
  445. // Ecrase $tva_tx par celui du produit
  446. // Ecrase $base_price_type par celui du produit
  447. // Replaces $fk_unit with the product's
  448. if (! empty($idprod))
  449. {
  450. $prod = new Product($db);
  451. $prod->fetch($idprod);
  452. $label = ((GETPOST('product_label') && GETPOST('product_label') != $prod->label) ? GETPOST('product_label') : '');
  453. // Update if prices fields are defined
  454. $tva_tx = get_default_tva($mysoc, $object->thirdparty, $prod->id);
  455. $tva_npr = get_default_npr($mysoc, $object->thirdparty, $prod->id);
  456. if (empty($tva_tx)) $tva_npr=0;
  457. $pu_ht = $prod->price;
  458. $pu_ttc = $prod->price_ttc;
  459. $price_min = $prod->price_min;
  460. $price_base_type = $prod->price_base_type;
  461. // We define price for product
  462. if (! empty($conf->global->PRODUIT_MULTIPRICES) && ! empty($object->thirdparty->price_level))
  463. {
  464. $pu_ht = $prod->multiprices[$object->thirdparty->price_level];
  465. $pu_ttc = $prod->multiprices_ttc[$object->thirdparty->price_level];
  466. $price_min = $prod->multiprices_min[$object->thirdparty->price_level];
  467. $price_base_type = $prod->multiprices_base_type[$object->thirdparty->price_level];
  468. if (! empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) // using this option is a bug. kept for backward compatibility
  469. {
  470. if (isset($prod->multiprices_tva_tx[$object->thirdparty->price_level])) $tva_tx=$prod->multiprices_tva_tx[$object->thirdparty->price_level];
  471. if (isset($prod->multiprices_recuperableonly[$object->thirdparty->price_level])) $tva_npr=$prod->multiprices_recuperableonly[$object->thirdparty->price_level];
  472. if (empty($tva_tx)) $tva_npr=0;
  473. }
  474. }
  475. elseif (! empty($conf->global->PRODUIT_CUSTOMER_PRICES))
  476. {
  477. include_once DOL_DOCUMENT_ROOT . '/product/class/productcustomerprice.class.php';
  478. $prodcustprice = new Productcustomerprice($db);
  479. $filter = array('t.fk_product' => $prod->id,'t.fk_soc' => $object->thirdparty->id);
  480. $result = $prodcustprice->fetch_all('', '', 0, 0, $filter);
  481. if ($result)
  482. {
  483. if (count($prodcustprice->lines) > 0)
  484. {
  485. $pu_ht = price($prodcustprice->lines[0]->price);
  486. $pu_ttc = price($prodcustprice->lines[0]->price_ttc);
  487. $price_base_type = $prodcustprice->lines[0]->price_base_type;
  488. $tva_tx = $prodcustprice->lines[0]->tva_tx;
  489. if ($prodcustprice->lines[0]->default_vat_code && ! preg_match('/\(.*\)/', $tva_tx)) $tva_tx.= ' ('.$prodcustprice->lines[0]->default_vat_code.')';
  490. $tva_npr = $prodcustprice->lines[0]->recuperableonly;
  491. if (empty($tva_tx)) $tva_npr=0;
  492. }
  493. }
  494. }
  495. $tmpvat = price2num(preg_replace('/\s*\(.*\)/', '', $tva_tx));
  496. $tmpprodvat = price2num(preg_replace('/\s*\(.*\)/', '', $prod->tva_tx));
  497. // if price ht was forced (ie: from gui when calculated by margin rate and cost price). TODO Why this ?
  498. if (! empty($price_ht))
  499. {
  500. $pu_ht = price2num($price_ht, 'MU');
  501. $pu_ttc = price2num($pu_ht * (1 + ($tmpvat / 100)), 'MU');
  502. }
  503. // On reevalue prix selon taux tva car taux tva transaction peut etre different
  504. // de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur).
  505. elseif ($tmpvat != $tmpprodvat)
  506. {
  507. if ($price_base_type != 'HT')
  508. {
  509. $pu_ht = price2num($pu_ttc / (1 + ($tmpvat / 100)), 'MU');
  510. }
  511. else
  512. {
  513. $pu_ttc = price2num($pu_ht * (1 + ($tmpvat / 100)), 'MU');
  514. }
  515. }
  516. $desc = '';
  517. // Define output language
  518. if (! empty($conf->global->MAIN_MULTILANGS) && ! empty($conf->global->PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE))
  519. {
  520. $outputlangs = $langs;
  521. $newlang = '';
  522. if (empty($newlang) && GETPOST('lang_id','aZ09'))
  523. $newlang = GETPOST('lang_id','aZ09');
  524. if (empty($newlang))
  525. $newlang = $object->thirdparty->default_lang;
  526. if (! empty($newlang))
  527. {
  528. $outputlangs = new Translate("", $conf);
  529. $outputlangs->setDefaultLang($newlang);
  530. }
  531. $desc = (! empty($prod->multilangs [$outputlangs->defaultlang] ["description"])) ? $prod->multilangs [$outputlangs->defaultlang] ["description"] : $prod->description;
  532. }
  533. else
  534. {
  535. $desc = $prod->description;
  536. }
  537. $desc = dol_concatdesc($desc, $product_desc);
  538. // Add custom code and origin country into description
  539. if (empty($conf->global->MAIN_PRODUCT_DISABLE_CUSTOMCOUNTRYCODE) && (! empty($prod->customcode) || ! empty($prod->country_code)))
  540. {
  541. $tmptxt = '(';
  542. if (! empty($prod->customcode))
  543. $tmptxt .= $langs->transnoentitiesnoconv("CustomCode") . ': ' . $prod->customcode;
  544. if (! empty($prod->customcode) && ! empty($prod->country_code))
  545. $tmptxt .= ' - ';
  546. if (! empty($prod->country_code))
  547. $tmptxt .= $langs->transnoentitiesnoconv("CountryOrigin") . ': ' . getCountry($prod->country_code, 0, $db, $langs, 0);
  548. $tmptxt .= ')';
  549. $desc = dol_concatdesc($desc, $tmptxt);
  550. }
  551. $type = $prod->type;
  552. $fk_unit = $prod->fk_unit;
  553. }
  554. else
  555. {
  556. $pu_ht = price2num($price_ht, 'MU');
  557. $pu_ttc = price2num(GETPOST('price_ttc'), 'MU');
  558. $tva_npr = (preg_match('/\*/', $tva_tx) ? 1 : 0);
  559. $tva_tx = str_replace('*', '', $tva_tx);
  560. if (empty($tva_tx)) $tva_npr=0;
  561. $label = (GETPOST('product_label') ? GETPOST('product_label') : '');
  562. $desc = $product_desc;
  563. $type = GETPOST('type');
  564. $fk_unit= GETPOST('units', 'alpha');
  565. }
  566. $date_start_fill = GETPOST('date_start_fill','int');
  567. $date_end_fill = GETPOST('date_end_fill','int');
  568. // Margin
  569. $fournprice = price2num(GETPOST('fournprice' . $predef) ? GETPOST('fournprice' . $predef) : '');
  570. $buyingprice = price2num(GETPOST('buying_price' . $predef) != '' ? GETPOST('buying_price' . $predef) : ''); // If buying_price is '0', we must keep this value
  571. // Local Taxes
  572. $localtax1_tx = get_localtax($tva_tx, 1, $object->thirdparty, $mysoc, $tva_npr);
  573. $localtax2_tx = get_localtax($tva_tx, 2, $object->thirdparty, $mysoc, $tva_npr);
  574. $info_bits = 0;
  575. if ($tva_npr)
  576. $info_bits |= 0x01;
  577. if (((!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->produit->ignore_price_min_advance)) || empty($conf->global->MAIN_USE_ADVANCED_PERMS) )&& (! empty($price_min) && (price2num($pu_ht) * (1 - price2num($remise_percent) / 100) < price2num($price_min))))
  578. {
  579. $mesg = $langs->trans("CantBeLessThanMinPrice", price(price2num($price_min, 'MU'), 0, $langs, 0, 0, - 1, $conf->currency));
  580. setEventMessages($mesg, null, 'errors');
  581. }
  582. else
  583. {
  584. // Insert line
  585. $result = $object->addline($desc, $pu_ht, $qty, $tva_tx,$localtax1_tx, $localtax2_tx, $idprod, $remise_percent, $price_base_type, $info_bits, '', $pu_ttc, $type, - 1, $special_code, $label, $fk_unit, 0, $date_start_fill, $date_end_fill);
  586. if ($result > 0)
  587. {
  588. /*if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  589. {
  590. // Define output language
  591. $outputlangs = $langs;
  592. $newlang = '';
  593. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id','aZ09')) $newlang = GETPOST('lang_id','aZ09');
  594. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
  595. if (! empty($newlang)) {
  596. $outputlangs = new Translate("", $conf);
  597. $outputlangs->setDefaultLang($newlang);
  598. }
  599. $model=$object->modelpdf;
  600. $ret = $object->fetch($id); // Reload to get new records
  601. $result = $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  602. if ($result < 0) setEventMessages($object->error, $object->errors, 'errors');
  603. }*/
  604. $object->fetch($object->id); // Reload lines
  605. unset($_POST['prod_entry_mode']);
  606. unset($_POST['qty']);
  607. unset($_POST['type']);
  608. unset($_POST['remise_percent']);
  609. unset($_POST['price_ht']);
  610. unset($_POST['multicurrency_price_ht']);
  611. unset($_POST['price_ttc']);
  612. unset($_POST['tva_tx']);
  613. unset($_POST['product_ref']);
  614. unset($_POST['product_label']);
  615. unset($_POST['product_desc']);
  616. unset($_POST['fournprice']);
  617. unset($_POST['buying_price']);
  618. unset($_POST['np_marginRate']);
  619. unset($_POST['np_markRate']);
  620. unset($_POST['dp_desc']);
  621. unset($_POST['idprod']);
  622. unset($_POST['units']);
  623. unset($_POST['date_starthour']);
  624. unset($_POST['date_startmin']);
  625. unset($_POST['date_startsec']);
  626. unset($_POST['date_startday']);
  627. unset($_POST['date_startmonth']);
  628. unset($_POST['date_startyear']);
  629. unset($_POST['date_endhour']);
  630. unset($_POST['date_endmin']);
  631. unset($_POST['date_endsec']);
  632. unset($_POST['date_endday']);
  633. unset($_POST['date_endmonth']);
  634. unset($_POST['date_endyear']);
  635. unset($_POST['date_start_fill']);
  636. unset($_POST['date_end_fill']);
  637. unset($_POST['situations']);
  638. unset($_POST['progress']);
  639. }
  640. else
  641. {
  642. setEventMessages($object->error, $object->errors, 'errors');
  643. }
  644. $action = '';
  645. }
  646. }
  647. }
  648. elseif ($action == 'updateline' && $user->rights->facture->creer && ! GETPOST('cancel','alpha'))
  649. {
  650. if (! $object->fetch($id) > 0) dol_print_error($db);
  651. $object->fetch_thirdparty();
  652. // Clean parameters
  653. $date_start = '';
  654. $date_end = '';
  655. //$date_start = dol_mktime(GETPOST('date_starthour'), GETPOST('date_startmin'), GETPOST('date_startsec'), GETPOST('date_startmonth'), GETPOST('date_startday'), GETPOST('date_startyear'));
  656. //$date_end = dol_mktime(GETPOST('date_endhour'), GETPOST('date_endmin'), GETPOST('date_endsec'), GETPOST('date_endmonth'), GETPOST('date_endday'), GETPOST('date_endyear'));
  657. $description = dol_htmlcleanlastbr(GETPOST('product_desc','none') ? GETPOST('product_desc','none') : GETPOST('desc','none'));
  658. $pu_ht = GETPOST('price_ht');
  659. $vat_rate = (GETPOST('tva_tx') ? GETPOST('tva_tx') : 0);
  660. $qty = GETPOST('qty');
  661. $pu_ht_devise = GETPOST('multicurrency_subprice');
  662. // Define info_bits
  663. $info_bits = 0;
  664. if (preg_match('/\*/', $vat_rate)) $info_bits |= 0x01;
  665. // Define vat_rate
  666. $vat_rate = str_replace('*', '', $vat_rate);
  667. $localtax1_rate = get_localtax($vat_rate, 1, $object->thirdparty);
  668. $localtax2_rate = get_localtax($vat_rate, 2, $object->thirdparty);
  669. // Add buying price
  670. $fournprice = price2num(GETPOST('fournprice') ? GETPOST('fournprice') : '');
  671. $buyingprice = price2num(GETPOST('buying_price') != '' ? GETPOST('buying_price') : ''); // If buying_price is '0', we muste keep this value
  672. // Extrafields
  673. $extrafieldsline = new ExtraFields($db);
  674. $extralabelsline = $extrafieldsline->fetch_name_optionals_label($object->table_element_line);
  675. $array_options = $extrafieldsline->getOptionalsFromPost($extralabelsline);
  676. $objectline = new FactureLigneRec($db);
  677. if ($objectline->fetch(GETPOST('lineid')))
  678. {
  679. $objectline->array_options=$array_options;
  680. $result=$objectline->insertExtraFields();
  681. if ($result < 0)
  682. {
  683. setEventMessages($langs->trans('Error').$result, null, 'errors');
  684. }
  685. }
  686. // Unset extrafield
  687. if (is_array($extralabelsline))
  688. {
  689. // Get extra fields
  690. foreach ($extralabelsline as $key => $value)
  691. {
  692. unset($_POST["options_" . $key]);
  693. }
  694. }
  695. // Define special_code for special lines
  696. $special_code=GETPOST('special_code');
  697. if (! GETPOST('qty')) $special_code=3;
  698. /*$line = new FactureLigne($db);
  699. $line->fetch(GETPOST('lineid'));
  700. $percent = $line->get_prev_progress($object->id);
  701. if (GETPOST('progress') < $percent)
  702. {
  703. $mesg = '<div class="warning">' . $langs->trans("CantBeLessThanMinPercent") . '</div>';
  704. setEventMessages($mesg, null, 'warnings');
  705. $error++;
  706. $result = -1;
  707. }*/
  708. // Check minimum price
  709. $productid = GETPOST('productid', 'int');
  710. if (! empty($productid))
  711. {
  712. $product = new Product($db);
  713. $product->fetch($productid);
  714. $type = $product->type;
  715. $price_min = $product->price_min;
  716. if (! empty($conf->global->PRODUIT_MULTIPRICES) && ! empty($object->thirdparty->price_level))
  717. $price_min = $product->multiprices_min[$object->thirdparty->price_level];
  718. $label = ((GETPOST('update_label') && GETPOST('product_label')) ? GETPOST('product_label') : '');
  719. // Check price is not lower than minimum (check is done only for standard or replacement invoices)
  720. if (((!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->produit->ignore_price_min_advance)) || empty($conf->global->MAIN_USE_ADVANCED_PERMS) )&& (($object->type == Facture::TYPE_STANDARD || $object->type == Facture::TYPE_REPLACEMENT) && $price_min && (price2num($pu_ht) * (1 - price2num(GETPOST('remise_percent')) / 100) < price2num($price_min))))
  721. {
  722. setEventMessages($langs->trans("CantBeLessThanMinPrice", price(price2num($price_min, 'MU'), 0, $langs, 0, 0, - 1, $conf->currency)), null, 'errors');
  723. $error ++;
  724. }
  725. } else {
  726. $type = GETPOST('type');
  727. $label = (GETPOST('product_label') ? GETPOST('product_label') : '');
  728. // Check parameters
  729. if (GETPOST('type') < 0) {
  730. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type")), null, 'errors');
  731. $error ++;
  732. }
  733. }
  734. if ($qty < 0) {
  735. $langs->load("errors");
  736. setEventMessages($langs->trans('ErrorQtyForCustomerInvoiceCantBeNegative'), null, 'errors');
  737. $error ++;
  738. }
  739. $date_start_fill = GETPOST('date_start_fill','int');
  740. $date_end_fill = GETPOST('date_end_fill','int');
  741. // Update line
  742. if (! $error)
  743. {
  744. $result = $object->updateline(
  745. GETPOST('lineid'),
  746. $description,
  747. $pu_ht,
  748. $qty,
  749. $vat_rate,
  750. $localtax1_rate,
  751. $localtax1_rate,
  752. GETPOST('productid'),
  753. GETPOST('remise_percent'),
  754. 'HT',
  755. $info_bits,
  756. 0,
  757. 0,
  758. $type,
  759. 0,
  760. $special_code,
  761. $label,
  762. GETPOST('units'),
  763. $pu_ht_devise,
  764. 0,
  765. $date_start_fill,
  766. $date_end_fill
  767. );
  768. if ($result >= 0)
  769. {
  770. /*if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  771. // Define output language
  772. $outputlangs = $langs;
  773. $newlang = '';
  774. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id','aZ09'))
  775. $newlang = GETPOST('lang_id','aZ09');
  776. if ($conf->global->MAIN_MULTILANGS && empty($newlang))
  777. $newlang = $object->thirdparty->default_lang;
  778. if (! empty($newlang)) {
  779. $outputlangs = new Translate("", $conf);
  780. $outputlangs->setDefaultLang($newlang);
  781. }
  782. $ret = $object->fetch($id); // Reload to get new records
  783. $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  784. }*/
  785. $object->fetch($object->id); // Reload lines
  786. unset($_POST['qty']);
  787. unset($_POST['type']);
  788. unset($_POST['productid']);
  789. unset($_POST['remise_percent']);
  790. unset($_POST['price_ht']);
  791. unset($_POST['multicurrency_price_ht']);
  792. unset($_POST['price_ttc']);
  793. unset($_POST['tva_tx']);
  794. unset($_POST['product_ref']);
  795. unset($_POST['product_label']);
  796. unset($_POST['product_desc']);
  797. unset($_POST['fournprice']);
  798. unset($_POST['buying_price']);
  799. unset($_POST['np_marginRate']);
  800. unset($_POST['np_markRate']);
  801. unset($_POST['dp_desc']);
  802. unset($_POST['idprod']);
  803. unset($_POST['units']);
  804. unset($_POST['date_starthour']);
  805. unset($_POST['date_startmin']);
  806. unset($_POST['date_startsec']);
  807. unset($_POST['date_startday']);
  808. unset($_POST['date_startmonth']);
  809. unset($_POST['date_startyear']);
  810. unset($_POST['date_endhour']);
  811. unset($_POST['date_endmin']);
  812. unset($_POST['date_endsec']);
  813. unset($_POST['date_endday']);
  814. unset($_POST['date_endmonth']);
  815. unset($_POST['date_endyear']);
  816. unset($_POST['situations']);
  817. unset($_POST['progress']);
  818. }
  819. else
  820. {
  821. setEventMessages($object->error, $object->errors, 'errors');
  822. }
  823. }
  824. }
  825. }
  826. /*
  827. * View
  828. */
  829. llxHeader('',$langs->trans("RepeatableInvoices"),'ch-facture.html#s-fac-facture-rec');
  830. $form = new Form($db);
  831. $formother = new FormOther($db);
  832. if (! empty($conf->projet->enabled)) { $formproject = new FormProjets($db); }
  833. $companystatic = new Societe($db);
  834. $invoicerectmp = new FactureRec($db);
  835. $now = dol_now();
  836. $tmparray=dol_getdate($now);
  837. $today = dol_mktime(23,59,59,$tmparray['mon'],$tmparray['mday'],$tmparray['year']); // Today is last second of current day
  838. /*
  839. * Create mode
  840. */
  841. if ($action == 'create')
  842. {
  843. print load_fiche_titre($langs->trans("CreateRepeatableInvoice"),'','title_accountancy.png');
  844. $object = new Facture($db); // Source invoice
  845. $product_static = new Product($db);
  846. if ($object->fetch($id, $ref) > 0)
  847. {
  848. $result = $object->getLinesArray();
  849. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  850. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  851. print '<input type="hidden" name="action" value="add">';
  852. print '<input type="hidden" name="facid" value="'.$object->id.'">';
  853. dol_fiche_head(null, '', '', 0);
  854. $rowspan=4;
  855. if (! empty($conf->projet->enabled)) $rowspan++;
  856. if ($object->fk_account > 0) $rowspan++;
  857. print '<table class="border" width="100%">';
  858. $object->fetch_thirdparty();
  859. // Title
  860. print '<tr><td class="titlefieldcreate fieldrequired">'.$langs->trans("Title").'</td><td>';
  861. print '<input class="flat quatrevingtpercent" type="text" name="titre" value="'.$_POST["titre"].'">';
  862. print '</td></tr>';
  863. // Third party
  864. print '<tr><td class="titlefieldcreate">'.$langs->trans("Customer").'</td><td>'.$object->thirdparty->getNomUrl(1,'customer').'</td>';
  865. print '</tr>';
  866. $note_public=GETPOST('note_public','none')?GETPOST('note_public','none'):$object->note_public;
  867. $note_private=GETPOST('note_private','none')?GETPOST('note_private','none'):$object->note_private;
  868. // Help of substitution key
  869. $substitutionarray = getCommonSubstitutionArray($langs, 2, null, $object);
  870. $substitutionarray['__INVOICE_PREVIOUS_MONTH__'] = $langs->trans("PreviousMonthOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($object->date, -1, 'm'),'%m').')';
  871. $substitutionarray['__INVOICE_MONTH__'] = $langs->trans("MonthOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date($object->date,'%m').')';
  872. $substitutionarray['__INVOICE_NEXT_MONTH__'] = $langs->trans("NextMonthOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($object->date, 1, 'm'),'%m').')';
  873. $substitutionarray['__INVOICE_PREVIOUS_MONTH_TEXT__'] = $langs->trans("TextPreviousMonthOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($object->date, -1, 'm'),'%B').')';
  874. $substitutionarray['__INVOICE_MONTH_TEXT__'] = $langs->trans("TextMonthOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date($object->date,'%B').')';
  875. $substitutionarray['__INVOICE_NEXT_MONTH_TEXT__'] = $langs->trans("TextNextMonthOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($object->date, 1, 'm'), '%B').')';
  876. $substitutionarray['__INVOICE_PREVIOUS_YEAR__'] = $langs->trans("YearOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($object->date, -1, 'y'),'%Y').')';
  877. $substitutionarray['__INVOICE_YEAR__'] = $langs->trans("PreviousYearOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date($object->date,'%Y').')';
  878. $substitutionarray['__INVOICE_NEXT_YEAR__'] = $langs->trans("NextYearOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($object->date, 1, 'y'),'%Y').')';
  879. // Only on template invoices
  880. $substitutionarray['__INVOICE_DATE_NEXT_INVOICE_BEFORE_GEN__'] = $langs->trans("DateNextInvoiceBeforeGen").' ('.$langs->trans("Example").': '.dol_print_date($object->date_when, 'dayhour').')';
  881. $substitutionarray['__INVOICE_DATE_NEXT_INVOICE_AFTER_GEN__'] = $langs->trans("DateNextInvoiceAfterGen").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($object->date_when, $object->frequency, $object->unit_frequency),'dayhour').')';
  882. $htmltext = '<i>'.$langs->trans("FollowingConstantsWillBeSubstituted").':<br>';
  883. foreach($substitutionarray as $key => $val)
  884. {
  885. $htmltext.=$key.' = '.$langs->trans($val).'<br>';
  886. }
  887. $htmltext.='</i>';
  888. // Public note
  889. print '<tr>';
  890. print '<td class="tdtop">';
  891. print $form->textwithpicto($langs->trans('NotePublic'), $htmltext, 1, 'help', '', 0, 2, 'notepublic');
  892. print '</td>';
  893. print '<td>';
  894. $doleditor = new DolEditor('note_public', $note_public, '', 80, 'dolibarr_notes', 'In', 0, false, true, ROWS_3, '90%');
  895. print $doleditor->Create(1);
  896. // Private note
  897. if (empty($user->societe_id))
  898. {
  899. print '<tr>';
  900. print '<td class="tdtop">';
  901. print $form->textwithpicto($langs->trans('NotePrivate'), $htmltext, 1, 'help', '', 0, 2, 'noteprivate');
  902. print '</td>';
  903. print '<td>';
  904. $doleditor = new DolEditor('note_private', $note_private, '', 80, 'dolibarr_notes', 'In', 0, false, true, ROWS_3, '90%');
  905. print $doleditor->Create(1);
  906. // print '<textarea name="note_private" wrap="soft" cols="70" rows="'.ROWS_3.'">'.$note_private.'.</textarea>
  907. print '</td></tr>';
  908. }
  909. // Author
  910. print "<tr><td>".$langs->trans("Author")."</td><td>".$user->getFullName($langs)."</td></tr>";
  911. // Payment term
  912. print "<tr><td>".$langs->trans("PaymentConditions")."</td><td>";
  913. $form->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->cond_reglement_id, 'none');
  914. print "</td></tr>";
  915. // Payment mode
  916. print "<tr><td>".$langs->trans("PaymentMode")."</td><td>";
  917. $form->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->mode_reglement_id, 'none', '', 1);
  918. print "</td></tr>";
  919. // Project
  920. if (! empty($conf->projet->enabled) && is_object($object->thirdparty) && $object->thirdparty->id > 0)
  921. {
  922. $projectid = GETPOST('projectid')?GETPOST('projectid'):$object->fk_project;
  923. $langs->load('projects');
  924. print '<tr><td>' . $langs->trans('Project') . '</td><td>';
  925. $numprojet = $formproject->select_projects($object->thirdparty->id, $projectid, 'projectid', 0, 0, 1, 0, 0, 0, 0, '', 0, 0, '');
  926. print ' &nbsp; <a href="'.DOL_URL_ROOT.'/projet/card.php?socid=' . $object->thirdparty->id . '&action=create&status=1&backtopage='.urlencode($_SERVER["PHP_SELF"].'?action=create&socid='.$object->thirdparty->id.(!empty($id)?'&id='.$id:'')).'">' . $langs->trans("AddProject") . '</a>';
  927. print '</td></tr>';
  928. }
  929. // Bank account
  930. if ($object->fk_account > 0)
  931. {
  932. print "<tr><td>".$langs->trans('BankAccount')."</td><td>";
  933. $form->formSelectAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->fk_account, 'none');
  934. print "</td></tr>";
  935. }
  936. // Model pdf
  937. print "<tr><td>".$langs->trans('Model')."</td><td>";
  938. include_once DOL_DOCUMENT_ROOT . '/core/modules/facture/modules_facture.php';
  939. $list = ModelePDFFactures::liste_modeles($db);
  940. print $form->selectarray('modelpdf', $list, $conf->global->FACTURE_ADDON_PDF);
  941. print "</td></tr>";
  942. print "</table>";
  943. dol_fiche_end();
  944. // Autogeneration
  945. $title = $langs->trans("Recurrence");
  946. print load_fiche_titre('<span class="fa fa-calendar"></span> '.$title, '', '');
  947. dol_fiche_head(null, '', '', 0);
  948. print '<table class="border" width="100%">';
  949. // Frequency + unit
  950. print '<tr><td class="titlefieldcreate">'.$form->textwithpicto($langs->trans("Frequency"), $langs->transnoentitiesnoconv('toolTipFrequency'))."</td><td>";
  951. print "<input type='text' name='frequency' value='".GETPOST('frequency', 'int')."' size='4' />&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'));
  952. print "</td></tr>";
  953. // Date next run
  954. print "<tr><td>".$langs->trans('NextDateToExecution')."</td><td>";
  955. $date_next_execution = isset($date_next_execution) ? $date_next_execution : (GETPOST('remonth') ? dol_mktime(12, 0, 0, GETPOST('remonth'), GETPOST('reday'), GETPOST('reyear')) : -1);
  956. print $form->selectDate($date_next_execution, '', 1, 1, '', "add", 1, 1);
  957. print "</td></tr>";
  958. // Number max of generation
  959. print "<tr><td>".$langs->trans("MaxPeriodNumber")."</td><td>";
  960. print '<input type="text" name="nb_gen_max" value="'.GETPOST('nb_gen_max').'" size="5" />';
  961. print "</td></tr>";
  962. // Auto validate the invoice
  963. print "<tr><td>".$langs->trans("StatusOfGeneratedInvoices")."</td><td>";
  964. $select = array('0'=>$langs->trans('BillStatusDraft'),'1'=>$langs->trans('BillStatusValidated'));
  965. print $form->selectarray('auto_validate', $select, GETPOST('auto_validate'));
  966. print "</td></tr>";
  967. // Auto generate document
  968. if (! empty($conf->global->INVOICE_REC_CAN_DISABLE_DOCUMENT_FILE_GENERATION))
  969. {
  970. print "<tr><td>".$langs->trans("StatusOfGeneratedDocuments")."</td><td>";
  971. $select = array('0'=>$langs->trans('DoNotGenerateDoc'),'1'=>$langs->trans('AutoGenerateDoc'));
  972. print $form->selectarray('generate_pdf', $select, GETPOST('generate_pdf'));
  973. print "</td></tr>";
  974. }
  975. else
  976. {
  977. print '<input type="hidden" name="generate_pdf" value="1">';
  978. }
  979. print "</table>";
  980. dol_fiche_end();
  981. $title = $langs->trans("ProductsAndServices");
  982. if (empty($conf->service->enabled))
  983. $title = $langs->trans("Products");
  984. elseif (empty($conf->product->enabled))
  985. $title = $langs->trans("Services");
  986. print load_fiche_titre($title, '', '');
  987. /*
  988. * Invoice lines
  989. */
  990. print '<div class="div-table-responsive-no-min">';
  991. print '<table id="tablelines" class="noborder noshadow" width="100%">';
  992. // Show object lines
  993. if (! empty($object->lines))
  994. {
  995. $disableedit=1;
  996. $disablemove=1;
  997. $disableremove=1;
  998. $object->printObjectLines('', $mysoc, $object->thirdparty, $lineid, 0); // No date selector for template invoice
  999. }
  1000. print "</table>\n";
  1001. print '<div>';
  1002. print '</td></tr>';
  1003. if ($flag_price_may_change)
  1004. {
  1005. print '<tr><td colspan="3" align="left">';
  1006. print '<select name="usenewprice" class="flat">';
  1007. print '<option value="0">'.$langs->trans("AlwaysUseFixedPrice").'</option>';
  1008. print '<option value="1" disabled>'.$langs->trans("AlwaysUseNewPrice").'</option>';
  1009. print '</select>';
  1010. print '</td></tr>';
  1011. }
  1012. print "</table>\n";
  1013. print '<div align="center"><input type="submit" class="button" value="'.$langs->trans("Create").'">';
  1014. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  1015. print '<input type="button" class="button" value="' . $langs->trans("Cancel") . '" onClick="javascript:history.go(-1)">';
  1016. print '</div>';
  1017. print "</form>\n";
  1018. }
  1019. else
  1020. {
  1021. dol_print_error('',"Error, no invoice ".$object->id);
  1022. }
  1023. }
  1024. else
  1025. {
  1026. /*
  1027. * View mode
  1028. */
  1029. if ($object->id > 0)
  1030. {
  1031. $object->fetch_thirdparty();
  1032. // Confirmation de la suppression d'une ligne produit
  1033. if ($action == 'ask_deleteline') {
  1034. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id . '&lineid=' . $lineid, $langs->trans('DeleteProductLine'), $langs->trans('ConfirmDeleteProductLine'), 'confirm_deleteline', '', 'no', 1);
  1035. }
  1036. // Confirm delete of repeatable invoice
  1037. if ($action == 'ask_deleteinvoice') {
  1038. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('DeleteRepeatableInvoice'), $langs->trans('ConfirmDeleteRepeatableInvoice'), 'confirm_deleteinvoice', '', 'no', 1);
  1039. }
  1040. print $formconfirm;
  1041. $author = new User($db);
  1042. $author->fetch($object->user_author);
  1043. $head=invoice_rec_prepare_head($object);
  1044. dol_fiche_head($head, 'card', $langs->trans("RepeatableInvoice"), -1, 'bill'); // Add a div
  1045. // Recurring invoice content
  1046. $linkback = '<a href="' . DOL_URL_ROOT . '/compta/facture/invoicetemplate_list.php?restore_lastsearch_values=1' . (! empty($socid) ? '&socid=' . $socid : '') . '">' . $langs->trans("BackToList") . '</a>';
  1047. $morehtmlref='';
  1048. if ($action != 'editref') $morehtmlref.=$form->editfieldkey($object->ref, 'ref', $object->ref, $object, $user->rights->facture->creer, '', '', 0, 2);
  1049. else $morehtmlref.= $form->editfieldval('', 'ref', $object->ref, $object, $user->rights->facture->creer, 'string');
  1050. $morehtmlref.='<div class="refidno">';
  1051. // Ref customer
  1052. //$morehtmlref.=$form->editfieldkey("RefCustomer", 'ref_client', $object->ref_client, $object, $user->rights->facture->creer, 'string', '', 0, 1);
  1053. //$morehtmlref.=$form->editfieldval("RefCustomer", 'ref_client', $object->ref_client, $object, $user->rights->facture->creer, 'string', '', null, null, '', 1);
  1054. // Thirdparty
  1055. $morehtmlref.=$langs->trans('ThirdParty') . ' : ' . $object->thirdparty->getNomUrl(1);
  1056. // Project
  1057. if (! empty($conf->projet->enabled))
  1058. {
  1059. $langs->load("projects");
  1060. $morehtmlref.='<br>'.$langs->trans('Project') . ' ';
  1061. if ($user->rights->facture->creer)
  1062. {
  1063. if ($action != 'classify')
  1064. $morehtmlref.='<a href="' . $_SERVER['PHP_SELF'] . '?action=classify&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetProject')) . '</a> : ';
  1065. if ($action == 'classify') {
  1066. //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
  1067. $morehtmlref.='<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
  1068. $morehtmlref.='<input type="hidden" name="action" value="classin">';
  1069. $morehtmlref.='<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  1070. $morehtmlref.=$formproject->select_projects($object->socid, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
  1071. $morehtmlref.='<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
  1072. $morehtmlref.='</form>';
  1073. } else {
  1074. $morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'none', 0, 0, 0, 1);
  1075. }
  1076. } else {
  1077. if (! empty($object->fk_project)) {
  1078. $proj = new Project($db);
  1079. $proj->fetch($object->fk_project);
  1080. $morehtmlref.='<a href="'.DOL_URL_ROOT.'/projet/card.php?id=' . $object->fk_project . '" title="' . $langs->trans('ShowProject') . '">';
  1081. $morehtmlref.=$proj->ref;
  1082. $morehtmlref.='</a>';
  1083. } else {
  1084. $morehtmlref.='';
  1085. }
  1086. }
  1087. }
  1088. $morehtmlref.='</div>';
  1089. dol_banner_tab($object, 'ref', $linkback, 1, 'titre', 'none', $morehtmlref, '', 0, '', $morehtmlright);
  1090. print '<div class="fichecenter">';
  1091. print '<div class="fichehalfleft">';
  1092. print '<div class="underbanner clearboth"></div>';
  1093. print '<table class="border" width="100%">';
  1094. print '<tr><td class="titlefield">'.$langs->trans("Author").'</td><td>'.$author->getFullName($langs)."</td></tr>";
  1095. print '<tr><td>'.$langs->trans("AmountHT").'</td>';
  1096. print '<td>'.price($object->total_ht,'',$langs,1,-1,-1,$conf->currency).'</td>';
  1097. print '</tr>';
  1098. print '<tr><td>'.$langs->trans("AmountVAT").'</td><td>'.price($object->total_tva,'',$langs,1,-1,-1,$conf->currency).'</td>';
  1099. print '</tr>';
  1100. // Amount Local Taxes
  1101. if (($mysoc->localtax1_assuj == "1" && $mysoc->useLocalTax(1)) || $object->total_localtax1 != 0) // Localtax1
  1102. {
  1103. print '<tr><td>' . $langs->transcountry("AmountLT1", $mysoc->country_code) . '</td>';
  1104. print '<td class="nowrap">' . price($object->total_localtax1, 1, '', 1, - 1, - 1, $conf->currency) . '</td></tr>';
  1105. }
  1106. if (($mysoc->localtax2_assuj == "1" && $mysoc->useLocalTax(2)) || $object->total_localtax2 != 0) // Localtax2
  1107. {
  1108. print '<tr><td>' . $langs->transcountry("AmountLT2", $mysoc->country_code) . '</td>';
  1109. print '<td class=nowrap">' . price($object->total_localtax2, 1, '', 1, - 1, - 1, $conf->currency) . '</td></tr>';
  1110. }
  1111. print '<tr><td>'.$langs->trans("AmountTTC").'</td><td colspan="3">'.price($object->total_ttc,'',$langs,1,-1,-1,$conf->currency).'</td>';
  1112. print '</tr>';
  1113. // Payment term
  1114. print '<tr><td>';
  1115. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1116. print $langs->trans('PaymentConditionsShort');
  1117. print '</td>';
  1118. if ($object->type != Facture::TYPE_CREDIT_NOTE && $action != 'editconditions' && ! empty($object->brouillon) && $user->rights->facture->creer)
  1119. print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editconditions&amp;facid=' . $object->id . '">' . img_edit($langs->trans('SetConditions'), 1) . '</a></td>';
  1120. print '</tr></table>';
  1121. print '</td><td>';
  1122. if ($object->type != Facture::TYPE_CREDIT_NOTE)
  1123. {
  1124. if ($action == 'editconditions')
  1125. {
  1126. $form->form_conditions_reglement($_SERVER['PHP_SELF'] . '?facid=' . $object->id, $object->cond_reglement_id, 'cond_reglement_id');
  1127. }
  1128. else
  1129. {
  1130. $form->form_conditions_reglement($_SERVER['PHP_SELF'] . '?facid=' . $object->id, $object->cond_reglement_id, 'none');
  1131. }
  1132. } else {
  1133. print '&nbsp;';
  1134. }
  1135. print '</td></tr>';
  1136. // Payment mode
  1137. print '<tr><td>';
  1138. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1139. print $langs->trans('PaymentMode');
  1140. print '</td>';
  1141. if ($action != 'editmode' && ! empty($object->brouillon) && $user->rights->facture->creer)
  1142. print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editmode&amp;facid=' . $object->id . '">' . img_edit($langs->trans('SetMode'), 1) . '</a></td>';
  1143. print '</tr></table>';
  1144. print '</td><td>';
  1145. if ($action == 'editmode')
  1146. {
  1147. $form->form_modes_reglement($_SERVER['PHP_SELF'].'?facid='.$object->id, $object->mode_reglement_id, 'mode_reglement_id', 'CRDT', 1, 1);
  1148. }
  1149. else
  1150. {
  1151. $form->form_modes_reglement($_SERVER['PHP_SELF'].'?facid='.$object->id, $object->mode_reglement_id, 'none');
  1152. }
  1153. print '</td></tr>';
  1154. // Help of substitution key
  1155. $dateexample=dol_now();
  1156. if (! empty($object->frequency) && ! empty($object->date_when)) $dateexample=$object->date_when;
  1157. $substitutionarray = getCommonSubstitutionArray($langs, 2, null, $object);
  1158. $substitutionarray['__INVOICE_PREVIOUS_MONTH__'] = $langs->trans("PreviousMonthOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($dateexample, -1, 'm'),'%m').')';
  1159. $substitutionarray['__INVOICE_MONTH__'] = $langs->trans("MonthOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date($dateexample,'%m').')';
  1160. $substitutionarray['__INVOICE_NEXT_MONTH__'] = $langs->trans("NextMonthOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($dateexample, 1, 'm'),'%m').')';
  1161. $substitutionarray['__INVOICE_PREVIOUS_MONTH_TEXT__'] = $langs->trans("TextPreviousMonthOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($dateexample, -1, 'm'),'%B').')';
  1162. $substitutionarray['__INVOICE_MONTH_TEXT__'] = $langs->trans("TextMonthOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date($dateexample,'%B').')';
  1163. $substitutionarray['__INVOICE_NEXT_MONTH_TEXT__'] = $langs->trans("TextNextMonthOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($dateexample, 1, 'm'), '%B').')';
  1164. $substitutionarray['__INVOICE_PREVIOUS_YEAR__'] = $langs->trans("YearOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($dateexample, -1, 'y'),'%Y').')';
  1165. $substitutionarray['__INVOICE_YEAR__'] = $langs->trans("PreviousYearOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date($dateexample,'%Y').')';
  1166. $substitutionarray['__INVOICE_NEXT_YEAR__'] = $langs->trans("NextYearOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($dateexample, 1, 'y'),'%Y').')';
  1167. // Only on template invoices
  1168. $substitutionarray['__INVOICE_DATE_NEXT_INVOICE_BEFORE_GEN__'] = $langs->trans("DateNextInvoiceBeforeGen").' ('.$langs->trans("Example").': '.dol_print_date(($object->date_when?$object->date_when:dol_now()), 'dayhour').')';
  1169. $substitutionarray['__INVOICE_DATE_NEXT_INVOICE_AFTER_GEN__'] = $langs->trans("DateNextInvoiceAfterGen").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree(($object->date_when?$object->date_when:dol_now()), $object->frequency, $object->unit_frequency),'dayhour').')';
  1170. $htmltext = '<i>'.$langs->trans("FollowingConstantsWillBeSubstituted").':<br>';
  1171. foreach($substitutionarray as $key => $val)
  1172. {
  1173. $htmltext.=$key.' = '.$langs->trans($val).'<br>';
  1174. }
  1175. $htmltext.='</i>';
  1176. // Note public
  1177. print '<tr><td>';
  1178. print $form->editfieldkey($form->textwithpicto($langs->trans('NotePublic'), $htmltext, 1, 'help', '', 0, 2, 'notepublic'), 'note_public', $object->note_public, $object, $user->rights->facture->creer);
  1179. print '</td><td class="wordbreak">';
  1180. print $form->editfieldval($langs->trans("NotePublic"), 'note_public', $object->note_public, $object, $user->rights->facture->creer, 'textarea:'.ROWS_4.':90%', '', null, null, '', 1);
  1181. print '</td>';
  1182. print '</tr>';
  1183. // Note private
  1184. print '<tr><td>';
  1185. print $form->editfieldkey($form->textwithpicto($langs->trans("NotePrivate"), $htmltext, 1, 'help', '', 0, 2, 'noteprivate'), 'note_private', $object->note_private, $object, $user->rights->facture->creer);
  1186. print '</td><td class="wordbreak">';
  1187. print $form->editfieldval($langs->trans("NotePrivate"), 'note_private', $object->note_private, $object, $user->rights->facture->creer, 'textarea:'.ROWS_4.':90%', '', null, null, '', 1);
  1188. print '</td>';
  1189. print '</tr>';
  1190. // Bank Account
  1191. $langs->load('banks');
  1192. print '<tr><td class="nowrap">';
  1193. print '<table width="100%" class="nobordernopadding"><tr><td class="nowrap">';
  1194. print $langs->trans('RIB');
  1195. print '<td>';
  1196. if (($action != 'editbankaccount') && $user->rights->facture->creer && ! empty($object->brouillon))
  1197. print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=editbankaccount&amp;id='.$object->id.'">'.img_edit($langs->trans('SetBankAccount'),1).'</a></td>';
  1198. print '</tr></table>';
  1199. print '</td><td>';
  1200. if ($action == 'editbankaccount')
  1201. {
  1202. $form->formSelectAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->fk_account, 'fk_account', 1);
  1203. }
  1204. else
  1205. {
  1206. $form->formSelectAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->fk_account, 'none');
  1207. }
  1208. print "</td>";
  1209. print '</tr>';
  1210. // Model pdf
  1211. $langs->load('banks');
  1212. print '<tr><td class="nowrap">';
  1213. print '<table width="100%" class="nobordernopadding"><tr><td class="nowrap">';
  1214. print $langs->trans('Model');
  1215. print '<td>';
  1216. if (($action != 'editmodelpdf') && $user->rights->facture->creer && ! empty($object->brouillon))
  1217. print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=editmodelpdf&amp;id='.$object->id.'">'.img_edit($langs->trans('SetModel'),1).'</a></td>';
  1218. print '</tr></table>';
  1219. print '</td><td>';
  1220. if ($action == 'editmodelpdf')
  1221. {
  1222. include_once DOL_DOCUMENT_ROOT . '/core/modules/facture/modules_facture.php';
  1223. $list = array();
  1224. $models = ModelePDFFactures::liste_modeles($db);
  1225. foreach ($models as $model) {
  1226. $list[] = $model . ':' . $model;
  1227. }
  1228. $select = 'select;'.implode(',', $list);
  1229. print $form->editfieldval($langs->trans("Model"), 'modelpdf', $object->modelpdf, $object, $user->rights->facture->creer, $select);
  1230. }
  1231. else
  1232. {
  1233. print $object->modelpdf;
  1234. }
  1235. print "</td>";
  1236. print '</tr>';
  1237. // Other attributes
  1238. $cols = 2;
  1239. include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_view.tpl.php';
  1240. print '</table>';
  1241. print '</div>';
  1242. print '<div class="fichehalfright">';
  1243. print '<div class="ficheaddleft">';
  1244. print '<div class="underbanner clearboth"></div>';
  1245. /*
  1246. * Recurrence
  1247. */
  1248. $title = $langs->trans("Recurrence");
  1249. //print load_fiche_titre($title, '', 'calendar');
  1250. print '<table class="border" width="100%">';
  1251. print '<tr><td colspan="2"><span class="fa fa-calendar"></span> '.$title.'</td></tr>';
  1252. // if "frequency" is empty or = 0, the reccurence is disabled
  1253. print '<tr><td style="width: 50%">';
  1254. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1255. print $langs->trans('Frequency');
  1256. print '</td>';
  1257. if ($action != 'editfrequency' && ! empty($object->brouillon) && $user->rights->facture->creer)
  1258. print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editfrequency&amp;facid=' . $object->id . '">' . img_edit($langs->trans('Edit'), 1) . '</a></td>';
  1259. print '</tr></table>';
  1260. print '</td><td>';
  1261. if ($action == 'editfrequency')
  1262. {
  1263. print '<form method="post" action="'.$_SERVER["PHP_SELF"] . '?facid=' . $object->id.'">';
  1264. print '<input type="hidden" name="action" value="setfrequency">';
  1265. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  1266. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  1267. print '<tr><td>';
  1268. 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'));
  1269. print '</td>';
  1270. print '<td class="left"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td>';
  1271. print '</tr></table></form>';
  1272. }
  1273. else
  1274. {
  1275. if ($object->frequency > 0)
  1276. {
  1277. print $langs->trans('FrequencyPer_'.$object->unit_frequency, $object->frequency);
  1278. }
  1279. else
  1280. {
  1281. print $langs->trans("NotARecurringInvoiceTemplate");
  1282. }
  1283. }
  1284. print '</td></tr>';
  1285. // Date when (next invoice generation)
  1286. print '<tr><td>';
  1287. if ($action == 'date_when' || $object->frequency > 0)
  1288. {
  1289. print $form->editfieldkey($langs->trans("NextDateToExecution"), 'date_when', $object->date_when, $object, $user->rights->facture->creer, 'day');
  1290. }
  1291. else
  1292. {
  1293. print $langs->trans("NextDateToExecution");
  1294. }
  1295. print '</td><td>';
  1296. if ($action == 'date_when' || $object->frequency > 0)
  1297. {
  1298. print $form->editfieldval($langs->trans("NextDateToExecution"), 'date_when', $object->date_when, $object, $user->rights->facture->creer, 'day', $object->date_when, null, '', '', 0, 'strikeIfMaxNbGenReached');
  1299. }
  1300. //var_dump(dol_print_date($object->date_when+60, 'dayhour').' - '.dol_print_date($now, 'dayhour'));
  1301. if (! $object->isMaxNbGenReached())
  1302. {
  1303. if (! $object->suspended && $action != 'editdate_when' && $object->frequency > 0 && $object->date_when && $object->date_when < $now) print img_warning($langs->trans("Late"));
  1304. }
  1305. else
  1306. {
  1307. print img_info($langs->trans("MaxNumberOfGenerationReached"));
  1308. }
  1309. print '</td>';
  1310. print '</tr>';
  1311. // Max period / Rest period
  1312. print '<tr><td>';
  1313. if ($action == 'nb_gen_max' || $object->frequency > 0)
  1314. {
  1315. print $form->editfieldkey($langs->trans("MaxPeriodNumber"), 'nb_gen_max', $object->nb_gen_max, $object, $user->rights->facture->creer);
  1316. }
  1317. else
  1318. {
  1319. print $langs->trans("MaxPeriodNumber");
  1320. }
  1321. print '</td><td>';
  1322. if ($action == 'nb_gen_max' || $object->frequency > 0)
  1323. {
  1324. print $form->editfieldval($langs->trans("MaxPeriodNumber"), 'nb_gen_max', $object->nb_gen_max?$object->nb_gen_max:'', $object, $user->rights->facture->creer);
  1325. }
  1326. else
  1327. {
  1328. print '';
  1329. }
  1330. print '</td>';
  1331. print '</tr>';
  1332. // Status of generated invoices
  1333. print '<tr><td>';
  1334. if ($action == 'auto_validate' || $object->frequency > 0)
  1335. print $form->editfieldkey($langs->trans("StatusOfGeneratedInvoices"), 'auto_validate', $object->auto_validate, $object, $user->rights->facture->creer);
  1336. else
  1337. print $langs->trans("StatusOfGeneratedInvoices");
  1338. print '</td><td>';
  1339. $select = 'select;0:'.$langs->trans('BillStatusDraft').',1:'.$langs->trans('BillStatusValidated');
  1340. if ($action == 'auto_validate' || $object->frequency > 0)
  1341. {
  1342. print $form->editfieldval($langs->trans("StatusOfGeneratedInvoices"), 'auto_validate', $object->auto_validate, $object, $user->rights->facture->creer, $select);
  1343. }
  1344. print '</td>';
  1345. // Auto generate documents
  1346. if (! empty($conf->global->INVOICE_REC_CAN_DISABLE_DOCUMENT_FILE_GENERATION))
  1347. {
  1348. print '<tr>';
  1349. print '<td>';
  1350. if ($action == 'generate_pdf' || $object->frequency > 0)
  1351. print $form->editfieldkey($langs->trans("StatusOfGeneratedDocuments"), 'generate_pdf', $object->generate_pdf, $object, $user->rights->facture->creer);
  1352. else
  1353. print $langs->trans("StatusOfGeneratedDocuments");
  1354. print '</td>';
  1355. print '<td>';
  1356. $select = 'select;0:'.$langs->trans('DoNotGenerateDoc').',1:'.$langs->trans('AutogenerateDoc');
  1357. if ($action == 'generate_pdf' || $object->frequency > 0)
  1358. {
  1359. print $form->editfieldval($langs->trans("StatusOfGeneratedDocuments"), 'generate_pdf', $object->generate_pdf, $object, $user->rights->facture->creer, $select);
  1360. }
  1361. print '</td>';
  1362. print '</tr>';
  1363. }
  1364. else
  1365. {
  1366. print '<input type="hidden" name="generate_pdf" value="1">';
  1367. }
  1368. print '</table>';
  1369. // Frequencry/Recurring section
  1370. if ($object->frequency > 0)
  1371. {
  1372. print '<br>';
  1373. if (empty($conf->cron->enabled))
  1374. {
  1375. print info_admin($langs->trans("EnableAndSetupModuleCron", $langs->transnoentitiesnoconv("Module2300Name")));
  1376. }
  1377. print '<div class="underbanner clearboth"></div>';
  1378. print '<table class="border centpercent">';
  1379. // Nb of generation already done
  1380. print '<tr><td style="width: 50%">'.$langs->trans("NbOfGenerationDone").'</td>';
  1381. print '<td>';
  1382. print $object->nb_gen_done?$object->nb_gen_done:'0';
  1383. print '</td>';
  1384. print '</tr>';
  1385. // Date last
  1386. print '<tr><td>';
  1387. print $langs->trans("DateLastGeneration");
  1388. print '</td><td>';
  1389. print dol_print_date($object->date_last_gen, 'dayhour');
  1390. print '</td>';
  1391. print '</tr>';
  1392. print '</table>';
  1393. print '<br>';
  1394. }
  1395. print '</div>';
  1396. print '</div>';
  1397. print '</div>';
  1398. print '<div class="clearboth"></div><br>';
  1399. // Lines
  1400. print ' <form name="addproduct" id="addproduct" action="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . (($action != 'editline') ? '#add' : '#line_' . GETPOST('lineid')) . '" method="POST">
  1401. <input type="hidden" name="token" value="' . $_SESSION ['newtoken'] . '">
  1402. <input type="hidden" name="action" value="' . (($action != 'editline') ? 'addline' : 'updateline') . '">
  1403. <input type="hidden" name="mode" value="">
  1404. <input type="hidden" name="id" value="' . $object->id . '">
  1405. ';
  1406. if (! empty($conf->use_javascript_ajax) && $object->statut == 0) {
  1407. include DOL_DOCUMENT_ROOT . '/core/tpl/ajaxrow.tpl.php';
  1408. }
  1409. print '<div class="div-table-responsive-no-min">';
  1410. print '<table id="tablelines" class="noborder noshadow" width="100%">';
  1411. // Show object lines
  1412. if (! empty($object->lines))
  1413. {
  1414. //$disableedit=1;
  1415. //$disablemove=1;
  1416. $ret = $object->printObjectLines($action, $mysoc, $object->thirdparty, $lineid, 0); // No date selector for template invoice
  1417. }
  1418. // Form to add new line
  1419. if ($object->statut == 0 && $user->rights->facture->creer && $action != 'valid' && $action != 'editline')
  1420. {
  1421. if ($action != 'editline')
  1422. {
  1423. // Add free products/services
  1424. $object->formAddObjectLine(0, $mysoc, $object->thirdparty); // No date selector for template invoice
  1425. $parameters = array();
  1426. $reshook = $hookmanager->executeHooks('formAddObjectLine', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  1427. }
  1428. }
  1429. print "</table>\n";
  1430. print '</div>';
  1431. print "</form>\n";
  1432. dol_fiche_end();
  1433. /**
  1434. * Barre d'actions
  1435. */
  1436. print '<div class="tabsAction">';
  1437. if (empty($object->suspended))
  1438. {
  1439. if ($user->rights->facture->creer)
  1440. {
  1441. if (! empty($object->frequency) && $object->nb_gen_max > 0 && ($object->nb_gen_done >= $object->nb_gen_max))
  1442. {
  1443. print '<div class="inline-block divButAction"><a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("MaxGenerationReached")).'">'.$langs->trans("CreateBill").'</a></div>';
  1444. }
  1445. else
  1446. {
  1447. if (empty($object->frequency) || $object->date_when <= $today)
  1448. {
  1449. print '<div class="inline-block divButAction"><a class="butAction" href="'.DOL_URL_ROOT.'/compta/facture/card.php?action=create&socid='.$object->thirdparty->id.'&fac_rec='.$object->id.'">'.$langs->trans("CreateBill").'</a></div>';
  1450. }
  1451. else
  1452. {
  1453. print '<div class="inline-block divButAction"><a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("DateIsNotEnough")).'">'.$langs->trans("CreateBill").'</a></div>';
  1454. }
  1455. }
  1456. }
  1457. else
  1458. {
  1459. print '<div class="inline-block divButAction"><a class="butActionRefused classfortooltip" href="#">'.$langs->trans("CreateBill").'</a></div>';
  1460. }
  1461. }
  1462. if ($user->rights->facture->creer)
  1463. {
  1464. if (empty($object->suspended))
  1465. {
  1466. print '<div class="inline-block divButAction"><a class="butActionDelete" href="'.DOL_URL_ROOT.'/compta/facture/fiche-rec.php?action=disable&id='.$object->id.'">'.$langs->trans("Disable").'</a></div>';
  1467. }
  1468. else
  1469. {
  1470. print '<div class="inline-block divButAction"><a class="butAction" href="'.DOL_URL_ROOT.'/compta/facture/fiche-rec.php?action=enable&id='.$object->id.'">'.$langs->trans("Enable").'</a></div>';
  1471. }
  1472. }
  1473. //if ($object->statut == Facture::STATUS_DRAFT && $user->rights->facture->supprimer)
  1474. if ($user->rights->facture->supprimer)
  1475. {
  1476. print '<div class="inline-block divButAction"><a class="butActionDelete" href="'.$_SERVER['PHP_SELF'].'?action=ask_deleteinvoice&id='.$object->id.'">'.$langs->trans('Delete').'</a></div>';
  1477. }
  1478. print '</div>';
  1479. print '<div class="fichecenter"><div class="fichehalfleft">';
  1480. print '<a name="builddoc"></a>'; // ancre
  1481. // Show links to link elements
  1482. $linktoelem = $form->showLinkToObjectBlock($object, null, array('invoice'));
  1483. $somethingshown = $form->showLinkedObjectBlock($object, $linktoelem);
  1484. print '</div></div>';
  1485. }
  1486. }
  1487. // End of page
  1488. llxFooter();
  1489. $db->close();