card-rec.php 69 KB

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