card.php 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373
  1. <?php
  2. /* Copyright (C) 2002-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Christophe Combelles <ccomb@free.fr>
  5. * Copyright (C) 2005 Marc Barilley <marc@ocebo.fr>
  6. * Copyright (C) 2005-2013 Regis Houssin <regis.houssin@capnetworks.com>
  7. * Copyright (C) 2010-2014 Juanjo Menent <jmenent@2byte.es>
  8. * Copyright (C) 2013-2015 Philippe Grand <philippe.grand@atoo-net.com>
  9. * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
  10. * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. /**
  26. * \file htdocs/fourn/facture/card.php
  27. * \ingroup facture, fournisseur
  28. * \brief Page for supplier invoice card (view, edit, validate)
  29. */
  30. require '../../main.inc.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_invoice/modules_facturefournisseur.php';
  34. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  35. require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
  36. require_once DOL_DOCUMENT_ROOT.'/core/lib/fourn.lib.php';
  37. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  38. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  39. if (!empty($conf->produit->enabled))
  40. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  41. if (!empty($conf->projet->enabled)) {
  42. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  43. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
  44. }
  45. $langs->load('bills');
  46. $langs->load('compta');
  47. $langs->load('suppliers');
  48. $langs->load('companies');
  49. $langs->load('products');
  50. $langs->load('banks');
  51. if (!empty($conf->incoterm->enabled)) $langs->load('incoterm');
  52. $id = (GETPOST('facid','int') ? GETPOST('facid','int') : GETPOST('id','int'));
  53. $action = GETPOST("action");
  54. $confirm = GETPOST("confirm");
  55. $ref = GETPOST('ref','alpha');
  56. $cancel = GETPOST('cancel','alpha');
  57. $lineid = GETPOST('lineid', 'int');
  58. $projectid = GETPOST('projectid','int');
  59. //PDF
  60. $hidedetails = (GETPOST('hidedetails','int') ? GETPOST('hidedetails','int') : (! empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0));
  61. $hidedesc = (GETPOST('hidedesc','int') ? GETPOST('hidedesc','int') : (! empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0));
  62. $hideref = (GETPOST('hideref','int') ? GETPOST('hideref','int') : (! empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0));
  63. // Security check
  64. $socid='';
  65. if (! empty($user->societe_id)) $socid=$user->societe_id;
  66. $result = restrictedArea($user, 'fournisseur', $id, 'facture_fourn', 'facture');
  67. // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
  68. $hookmanager->initHooks(array('invoicesuppliercard','globalcard'));
  69. $object=new FactureFournisseur($db);
  70. $extrafields = new ExtraFields($db);
  71. // fetch optionals attributes and labels
  72. $extralabels=$extrafields->fetch_name_optionals_label($object->table_element);
  73. // Load object
  74. if ($id > 0 || ! empty($ref))
  75. {
  76. $ret=$object->fetch($id, $ref);
  77. $object->fetch_thirdparty();
  78. }
  79. $permissionnote=$user->rights->fournisseur->facture->creer; // Used by the include of actions_setnotes.inc.php
  80. $permissiondellink=$user->rights->fournisseur->facture->creer; // Used by the include of actions_dellink.inc.php
  81. $permissionedit=$user->rights->fournisseur->facture->creer; // Used by the include of actions_lineupdown.inc.php
  82. /*
  83. * Actions
  84. */
  85. $parameters=array('socid'=>$socid);
  86. $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  87. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  88. if (empty($reshook))
  89. {
  90. if ($cancel) $action='';
  91. include DOL_DOCUMENT_ROOT.'/core/actions_setnotes.inc.php'; // Must be include, not include_once
  92. include DOL_DOCUMENT_ROOT.'/core/actions_dellink.inc.php'; // Must be include, not include_once
  93. include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php'; // Must be include, not include_once
  94. // Link invoice to order
  95. if (GETPOST('linkedOrder') && empty($cancel) && $id > 0)
  96. {
  97. $object->fetch($id);
  98. $object->fetch_thirdparty();
  99. $result = $object->add_object_linked('order_supplier', GETPOST('linkedOrder'));
  100. }
  101. // Action clone object
  102. if ($action == 'confirm_clone' && $confirm == 'yes')
  103. {
  104. // if (1==0 && empty($_REQUEST["clone_content"]) && empty($_REQUEST["clone_receivers"]))
  105. // {
  106. // $mesg='<div class="error">'.$langs->trans("NoCloneOptionsSpecified").'</div>';
  107. // }
  108. // else
  109. // {
  110. $result=$object->createFromClone($id);
  111. if ($result > 0)
  112. {
  113. header("Location: ".$_SERVER['PHP_SELF'].'?action=editref_supplier&id='.$result);
  114. exit;
  115. }
  116. else
  117. {
  118. $langs->load("errors");
  119. setEventMessages($langs->trans($object->error), null, 'errors');
  120. $action='';
  121. }
  122. // }
  123. }
  124. elseif ($action == 'confirm_valid' && $confirm == 'yes' &&
  125. ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->fournisseur->facture->creer))
  126. || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->fournisseur->supplier_invoice_advance->validate)))
  127. )
  128. {
  129. $idwarehouse=GETPOST('idwarehouse');
  130. $object->fetch($id);
  131. $object->fetch_thirdparty();
  132. $qualified_for_stock_change=0;
  133. if (empty($conf->global->STOCK_SUPPORTS_SERVICES))
  134. {
  135. $qualified_for_stock_change=$object->hasProductsOrServices(2);
  136. }
  137. else
  138. {
  139. $qualified_for_stock_change=$object->hasProductsOrServices(1);
  140. }
  141. // Check parameters
  142. if (! empty($conf->stock->enabled) && ! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL) && $qualified_for_stock_change)
  143. {
  144. $langs->load("stocks");
  145. if (! $idwarehouse || $idwarehouse == -1)
  146. {
  147. $error++;
  148. setEventMessages($langs->trans('ErrorFieldRequired',$langs->transnoentitiesnoconv("Warehouse")), null, 'errors');
  149. $action='';
  150. }
  151. }
  152. if (! $error)
  153. {
  154. $result = $object->validate($user,'',$idwarehouse);
  155. if ($result < 0)
  156. {
  157. setEventMessages($object->error,$object->errors,'errors');
  158. }
  159. }
  160. }
  161. elseif ($action == 'confirm_delete' && $confirm == 'yes' && $user->rights->fournisseur->facture->supprimer)
  162. {
  163. $object->fetch($id);
  164. $object->fetch_thirdparty();
  165. $result=$object->delete($id);
  166. if ($result > 0)
  167. {
  168. header('Location: list.php');
  169. exit;
  170. }
  171. else
  172. {
  173. setEventMessages($object->error, $object->errors, 'errors');
  174. }
  175. }
  176. // Remove a product line
  177. else if ($action == 'confirm_deleteline' && $confirm == 'yes' && $user->rights->fournisseur->facture->creer)
  178. {
  179. $result = $object->deleteline($lineid);
  180. if ($result > 0)
  181. {
  182. // Define output language
  183. $outputlangs = $langs;
  184. $newlang = '';
  185. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id'))
  186. $newlang = GETPOST('lang_id');
  187. if ($conf->global->MAIN_MULTILANGS && empty($newlang))
  188. $newlang = $object->thirdparty->default_lang;
  189. if (! empty($newlang)) {
  190. $outputlangs = new Translate("", $conf);
  191. $outputlangs->setDefaultLang($newlang);
  192. }
  193. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  194. $ret = $object->fetch($object->id); // Reload to get new records
  195. $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  196. }
  197. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id);
  198. exit;
  199. }
  200. else
  201. {
  202. setEventMessages($object->error, $object->errors, 'errors');
  203. /* Fix bug 1485 : Reset action to avoid asking again confirmation on failure */
  204. $action='';
  205. }
  206. }
  207. elseif ($action == 'confirm_paid' && $confirm == 'yes' && $user->rights->fournisseur->facture->creer)
  208. {
  209. $object->fetch($id);
  210. $result=$object->set_paid($user);
  211. if ($result<0)
  212. {
  213. setEventMessages($object->error, $object->errors, 'errors');
  214. }
  215. }
  216. // Set supplier ref
  217. if ($action == 'setref_supplier' && $user->rights->fournisseur->commande->creer)
  218. {
  219. $object->ref_supplier = GETPOST('ref_supplier', 'alpha');
  220. if ($object->update($user) < 0) {
  221. setEventMessages($object->error, $object->errors, 'errors');
  222. }
  223. }
  224. // payments conditions
  225. if ($action == 'setconditions' && $user->rights->fournisseur->commande->creer)
  226. {
  227. $result=$object->setPaymentTerms(GETPOST('cond_reglement_id','int'));
  228. }
  229. // payment mode
  230. else if ($action == 'setmode' && $user->rights->fournisseur->commande->creer)
  231. {
  232. $result = $object->setPaymentMethods(GETPOST('mode_reglement_id','int'));
  233. }
  234. // Multicurrency Code
  235. else if ($action == 'setmulticurrencycode' && $user->rights->facture->creer) {
  236. $result = $object->setMulticurrencyCode(GETPOST('multicurrency_code', 'alpha'));
  237. }
  238. // Multicurrency rate
  239. else if ($action == 'setmulticurrencyrate' && $user->rights->facture->creer) {
  240. $result = $object->setMulticurrencyRate(GETPOST('multicurrency_tx', 'int'));
  241. }
  242. // bank account
  243. else if ($action == 'setbankaccount' && $user->rights->fournisseur->facture->creer) {
  244. $result=$object->setBankAccount(GETPOST('fk_account', 'int'));
  245. }
  246. // Set label
  247. elseif ($action == 'setlabel' && $user->rights->fournisseur->facture->creer)
  248. {
  249. $object->fetch($id);
  250. $object->label=$_POST['label'];
  251. $result=$object->update($user);
  252. if ($result < 0) dol_print_error($db);
  253. }
  254. elseif ($action == 'setdatef' && $user->rights->fournisseur->facture->creer)
  255. {
  256. $object->fetch($id);
  257. $object->date=dol_mktime(12,0,0,$_POST['datefmonth'],$_POST['datefday'],$_POST['datefyear']);
  258. if ($object->date_echeance && $object->date_echeance < $object->date) $object->date_echeance=$object->date;
  259. $result=$object->update($user);
  260. if ($result < 0) dol_print_error($db,$object->error);
  261. }
  262. elseif ($action == 'setdate_lim_reglement' && $user->rights->fournisseur->facture->creer)
  263. {
  264. $object->fetch($id);
  265. $object->date_echeance=dol_mktime(12,0,0,$_POST['date_lim_reglementmonth'],$_POST['date_lim_reglementday'],$_POST['date_lim_reglementyear']);
  266. if (! empty($object->date_echeance) && $object->date_echeance < $object->date)
  267. {
  268. $object->date_echeance=$object->date;
  269. setEventMessages($langs->trans("DatePaymentTermCantBeLowerThanObjectDate"), null, 'warnings');
  270. }
  271. $result=$object->update($user);
  272. if ($result < 0) dol_print_error($db,$object->error);
  273. }
  274. // Delete payment
  275. elseif ($action == 'deletepaiement' && $user->rights->fournisseur->facture->creer)
  276. {
  277. $object->fetch($id);
  278. if ($object->statut == FactureFournisseur::STATUS_VALIDATED && $object->paye == 0)
  279. {
  280. $paiementfourn = new PaiementFourn($db);
  281. $result=$paiementfourn->fetch(GETPOST('paiement_id'));
  282. if ($result > 0) $result=$paiementfourn->delete(); // If fetch ok and found
  283. if ($result < 0) {
  284. setEventMessages($paiementfourn->error, $paiementfourn->errors, 'errors');
  285. }
  286. }
  287. }
  288. // Create
  289. elseif ($action == 'add' && $user->rights->fournisseur->facture->creer)
  290. {
  291. $error=0;
  292. $datefacture=dol_mktime(12,0,0,$_POST['remonth'],$_POST['reday'],$_POST['reyear']);
  293. $datedue=dol_mktime(12,0,0,$_POST['echmonth'],$_POST['echday'],$_POST['echyear']);
  294. if (GETPOST('socid','int')<1)
  295. {
  296. setEventMessages($langs->trans('ErrorFieldRequired',$langs->transnoentities('Supplier')), null, 'errors');
  297. $action='create';
  298. $error++;
  299. }
  300. if ($datefacture == '')
  301. {
  302. setEventMessages($langs->trans('ErrorFieldRequired',$langs->transnoentities('DateInvoice')), null, 'errors');
  303. $action='create';
  304. $_GET['socid']=$_POST['socid'];
  305. $error++;
  306. }
  307. if (! GETPOST('ref_supplier'))
  308. {
  309. setEventMessages($langs->trans('ErrorFieldRequired',$langs->transnoentities('RefSupplier')), null, 'errors');
  310. $action='create';
  311. $_GET['socid']=$_POST['socid'];
  312. $error++;
  313. }
  314. // Fill array 'array_options' with data from add form
  315. if (! $error)
  316. {
  317. $db->begin();
  318. $extralabels = $extrafields->fetch_name_optionals_label($object->table_element);
  319. $ret = $extrafields->setOptionalsFromPost($extralabels, $object);
  320. if ($ret < 0) $error++;
  321. $tmpproject = GETPOST('projectid', 'int');
  322. // Creation facture
  323. $object->ref = $_POST['ref'];
  324. $object->ref_supplier = $_POST['ref_supplier'];
  325. $object->socid = $_POST['socid'];
  326. $object->libelle = $_POST['libelle'];
  327. $object->date = $datefacture;
  328. $object->date_echeance = $datedue;
  329. $object->note_public = GETPOST('note_public');
  330. $object->note_private = GETPOST('note_private');
  331. $object->cond_reglement_id = GETPOST('cond_reglement_id');
  332. $object->mode_reglement_id = GETPOST('mode_reglement_id');
  333. $object->fk_account = GETPOST('fk_account', 'int');
  334. $object->fk_project = ($tmpproject > 0) ? $tmpproject : null;
  335. $object->fk_incoterms = GETPOST('incoterm_id', 'int');
  336. $object->location_incoterms = GETPOST('location_incoterms', 'alpha');
  337. $object->multicurrency_code = GETPOST('multicurrency_code', 'alpha');
  338. $object->multicurrency_tx = GETPOST('originmulticurrency_tx', 'int');
  339. // Auto calculation of date due if not filled by user
  340. if(empty($object->date_echeance)) $object->date_echeance = $object->calculate_date_lim_reglement();
  341. // If creation from another object of another module
  342. if (! $error && $_POST['origin'] && $_POST['originid'])
  343. {
  344. // Parse element/subelement (ex: project_task)
  345. $element = $subelement = $_POST['origin'];
  346. /*if (preg_match('/^([^_]+)_([^_]+)/i',$_POST['origin'],$regs))
  347. {
  348. $element = $regs[1];
  349. $subelement = $regs[2];
  350. }*/
  351. // For compatibility
  352. if ($element == 'order') {
  353. $element = $subelement = 'commande';
  354. }
  355. if ($element == 'propal') {
  356. $element = 'comm/propal'; $subelement = 'propal';
  357. }
  358. if ($element == 'contract') {
  359. $element = $subelement = 'contrat';
  360. }
  361. if ($element == 'order_supplier') {
  362. $element = 'fourn'; $subelement = 'fournisseur.commande';
  363. }
  364. if ($element == 'project')
  365. {
  366. $element = 'projet';
  367. }
  368. $object->origin = $_POST['origin'];
  369. $object->origin_id = $_POST['originid'];
  370. $id = $object->create($user);
  371. // Add lines
  372. if ($id > 0)
  373. {
  374. require_once DOL_DOCUMENT_ROOT.'/'.$element.'/class/'.$subelement.'.class.php';
  375. $classname = ucfirst($subelement);
  376. if ($classname == 'Fournisseur.commande') $classname='CommandeFournisseur';
  377. $srcobject = new $classname($db);
  378. $result=$srcobject->fetch($_POST['originid']);
  379. if ($result > 0)
  380. {
  381. $lines = $srcobject->lines;
  382. if (empty($lines) && method_exists($srcobject,'fetch_lines'))
  383. {
  384. $srcobject->fetch_lines();
  385. $lines = $srcobject->lines;
  386. }
  387. $num=count($lines);
  388. for ($i = 0; $i < $num; $i++)
  389. {
  390. $desc=($lines[$i]->desc?$lines[$i]->desc:$lines[$i]->libelle);
  391. $product_type=($lines[$i]->product_type?$lines[$i]->product_type:0);
  392. // Dates
  393. // TODO mutualiser
  394. $date_start=$lines[$i]->date_debut_prevue;
  395. if ($lines[$i]->date_debut_reel) $date_start=$lines[$i]->date_debut_reel;
  396. if ($lines[$i]->date_start) $date_start=$lines[$i]->date_start;
  397. $date_end=$lines[$i]->date_fin_prevue;
  398. if ($lines[$i]->date_fin_reel) $date_end=$lines[$i]->date_fin_reel;
  399. if ($lines[$i]->date_end) $date_end=$lines[$i]->date_end;
  400. // FIXME Missing $lines[$i]->ref_supplier and $lines[$i]->label into addline and updateline methods. They are filled when coming from order for example.
  401. $result = $object->addline(
  402. $desc,
  403. $lines[$i]->subprice,
  404. $lines[$i]->tva_tx,
  405. $lines[$i]->localtax1_tx,
  406. $lines[$i]->localtax2_tx,
  407. $lines[$i]->qty,
  408. $lines[$i]->fk_product,
  409. $lines[$i]->remise_percent,
  410. $date_start,
  411. $date_end,
  412. 0,
  413. $lines[$i]->info_bits,
  414. 'HT',
  415. $product_type,
  416. $lines[$i]->rang,
  417. 0,
  418. $lines[$i]->array_options,
  419. $lines[$i]->fk_unit
  420. );
  421. if ($result < 0)
  422. {
  423. $error++;
  424. break;
  425. }
  426. }
  427. // Now reload line
  428. $object->fetch_lines();
  429. }
  430. else
  431. {
  432. $error++;
  433. }
  434. }
  435. else
  436. {
  437. $error++;
  438. }
  439. }
  440. else if (! $error)
  441. {
  442. $id = $object->create($user);
  443. if ($id < 0)
  444. {
  445. $error++;
  446. }
  447. if (! $error)
  448. {
  449. // If some invoice's lines already known
  450. for ($i = 1 ; $i < 9 ; $i++)
  451. {
  452. $label = $_POST['label'.$i];
  453. $amountht = price2num($_POST['amount'.$i]);
  454. $amountttc = price2num($_POST['amountttc'.$i]);
  455. $tauxtva = price2num($_POST['tauxtva'.$i]);
  456. $qty = $_POST['qty'.$i];
  457. $fk_product = $_POST['fk_product'.$i];
  458. if ($label)
  459. {
  460. if ($amountht)
  461. {
  462. $price_base='HT'; $amount=$amountht;
  463. }
  464. else
  465. {
  466. $price_base='TTC'; $amount=$amountttc;
  467. }
  468. $atleastoneline=1;
  469. $product=new Product($db);
  470. $product->fetch($_POST['idprod'.$i]);
  471. $ret=$object->addline($label, $amount, $tauxtva, $product->localtax1_tx, $product->localtax2_tx, $qty, $fk_product, $remise_percent, '', '', '', 0, $price_base, $_POST['rang'.$i], 1);
  472. if ($ret < 0) $error++;
  473. }
  474. }
  475. }
  476. }
  477. if ($error)
  478. {
  479. $langs->load("errors");
  480. $db->rollback();
  481. setEventMessages($object->error, $object->errors, 'errors');
  482. $action='create';
  483. $_GET['socid']=$_POST['socid'];
  484. }
  485. else
  486. {
  487. $db->commit();
  488. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  489. $outputlangs = $langs;
  490. $result = $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  491. if ($result <= 0)
  492. {
  493. dol_print_error($db,$object->error,$object->errors);
  494. exit;
  495. }
  496. }
  497. header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
  498. exit;
  499. }
  500. }
  501. }
  502. // Edit line
  503. elseif ($action == 'updateline' && $user->rights->fournisseur->facture->creer)
  504. {
  505. $db->begin();
  506. $object->fetch($id);
  507. $object->fetch_thirdparty();
  508. $tva_tx = GETPOST('tva_tx');
  509. if (GETPOST('price_ht') != '')
  510. {
  511. $up = price2num(GETPOST('price_ht'));
  512. $price_base_type = 'HT';
  513. }
  514. else
  515. {
  516. $up = price2num(GETPOST('price_ttc'));
  517. $price_base_type = 'TTC';
  518. }
  519. if (GETPOST('productid'))
  520. {
  521. $prod = new Product($db);
  522. $prod->fetch(GETPOST('productid'));
  523. $label = $prod->description;
  524. if (trim($_POST['product_desc']) != trim($label)) $label=$_POST['product_desc'];
  525. $type = $prod->type;
  526. }
  527. else
  528. {
  529. $label = $_POST['product_desc'];
  530. $type = $_POST["type"]?$_POST["type"]:0;
  531. }
  532. $date_start=dol_mktime(GETPOST('date_starthour'), GETPOST('date_startmin'), GETPOST('date_startsec'), GETPOST('date_startmonth'), GETPOST('date_startday'), GETPOST('date_startyear'));
  533. $date_end=dol_mktime(GETPOST('date_endhour'), GETPOST('date_endmin'), GETPOST('date_endsec'), GETPOST('date_endmonth'), GETPOST('date_endday'), GETPOST('date_endyear'));
  534. $localtax1_tx= get_localtax($_POST['tauxtva'], 1, $mysoc,$object->thirdparty);
  535. $localtax2_tx= get_localtax($_POST['tauxtva'], 2, $mysoc,$object->thirdparty);
  536. $remise_percent=GETPOST('remise_percent');
  537. // Extrafields Lines
  538. $extrafieldsline = new ExtraFields($db);
  539. $extralabelsline = $extrafieldsline->fetch_name_optionals_label($object->table_element_line);
  540. $array_options = $extrafieldsline->getOptionalsFromPost($extralabelsline);
  541. // Unset extrafield POST Data
  542. if (is_array($extralabelsline)) {
  543. foreach ($extralabelsline as $key => $value) {
  544. unset($_POST["options_" . $key]);
  545. }
  546. }
  547. $result=$object->updateline(GETPOST('lineid'), $label, $up, $tva_tx, $localtax1_tx, $localtax2_tx, GETPOST('qty'), GETPOST('productid'), $price_base_type, 0, $type, $remise_percent, 0, $date_start, $date_end, $array_options, $_POST['units']);
  548. if ($result >= 0)
  549. {
  550. unset($_POST['label']);
  551. unset($_POST['date_starthour']);
  552. unset($_POST['date_startmin']);
  553. unset($_POST['date_startsec']);
  554. unset($_POST['date_startday']);
  555. unset($_POST['date_startmonth']);
  556. unset($_POST['date_startyear']);
  557. unset($_POST['date_endhour']);
  558. unset($_POST['date_endmin']);
  559. unset($_POST['date_endsec']);
  560. unset($_POST['date_endday']);
  561. unset($_POST['date_endmonth']);
  562. unset($_POST['date_endyear']);
  563. $db->commit();
  564. }
  565. else
  566. {
  567. $db->rollback();
  568. setEventMessages($object->error, $object->errors, 'errors');
  569. }
  570. }
  571. elseif ($action == 'addline' && $user->rights->fournisseur->facture->creer)
  572. {
  573. $db->begin();
  574. $ret=$object->fetch($id);
  575. if ($ret < 0)
  576. {
  577. dol_print_error($db,$object->error);
  578. exit;
  579. }
  580. $ret=$object->fetch_thirdparty();
  581. $langs->load('errors');
  582. $error=0;
  583. // Set if we used free entry or predefined product
  584. $predef='';
  585. $product_desc=(GETPOST('dp_desc')?GETPOST('dp_desc'):'');
  586. if (GETPOST('prod_entry_mode') == 'free')
  587. {
  588. $idprod=0;
  589. $price_ht = GETPOST('price_ht');
  590. $tva_tx = (GETPOST('tva_tx') ? GETPOST('tva_tx') : 0);
  591. }
  592. else
  593. {
  594. $idprod=GETPOST('idprod', 'int');
  595. $price_ht = '';
  596. $tva_tx = '';
  597. }
  598. $qty = GETPOST('qty'.$predef);
  599. $remise_percent=GETPOST('remise_percent'.$predef);
  600. $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'));
  601. $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'));
  602. // Extrafields
  603. $extrafieldsline = new ExtraFields($db);
  604. $extralabelsline = $extrafieldsline->fetch_name_optionals_label($object->table_element_line);
  605. $array_options = $extrafieldsline->getOptionalsFromPost($extralabelsline, $predef);
  606. // Unset extrafield
  607. if (is_array($extralabelsline)) {
  608. // Get extra fields
  609. foreach ($extralabelsline as $key => $value) {
  610. unset($_POST["options_" . $key]);
  611. }
  612. }
  613. if (GETPOST('prod_entry_mode')=='free' && GETPOST('price_ht') < 0 && $qty < 0)
  614. {
  615. setEventMessages($langs->trans('ErrorBothFieldCantBeNegative', $langs->transnoentitiesnoconv('UnitPrice'), $langs->transnoentitiesnoconv('Qty')), null, 'errors');
  616. $error++;
  617. }
  618. if (GETPOST('prod_entry_mode')=='free' && ! GETPOST('idprodfournprice') && GETPOST('type') < 0)
  619. {
  620. setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Type')), null, 'errors');
  621. $error++;
  622. }
  623. if (GETPOST('prod_entry_mode')=='free' && GETPOST('price_ht')==='' && GETPOST('price_ttc')==='') // Unit price can be 0 but not ''
  624. {
  625. setEventMessages($langs->trans($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('UnitPrice'))), null, 'errors');
  626. $error++;
  627. }
  628. if (GETPOST('prod_entry_mode')=='free' && ! GETPOST('dp_desc'))
  629. {
  630. setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Description')), null, 'errors');
  631. $error++;
  632. }
  633. if (! GETPOST('qty'))
  634. {
  635. setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Qty')), null, 'errors');
  636. $error++;
  637. }
  638. if (GETPOST('prod_entry_mode') != 'free') // With combolist mode idprodfournprice is > 0 or -1. With autocomplete, idprodfournprice is > 0 or ''
  639. {
  640. $idprod=0;
  641. $productsupplier=new ProductFournisseur($db);
  642. if (GETPOST('idprodfournprice') == -1 || GETPOST('idprodfournprice') == '') $idprod=-2; // Same behaviour than with combolist. When not select idprodfournprice is now -2 (to avoid conflict with next action that may return -1)
  643. if (GETPOST('idprodfournprice') > 0)
  644. {
  645. $idprod=$productsupplier->get_buyprice(GETPOST('idprodfournprice'), $qty); // Just to see if a price exists for the quantity. Not used to found vat.
  646. }
  647. //Replaces $fk_unit with the product's
  648. if ($idprod > 0)
  649. {
  650. $result=$productsupplier->fetch($idprod);
  651. $label = $productsupplier->label;
  652. $desc = $productsupplier->description;
  653. if (trim($product_desc) != trim($desc)) $desc = dol_concatdesc($desc, $product_desc);
  654. $tva_tx=get_default_tva($object->thirdparty, $mysoc, $productsupplier->id, $_POST['idprodfournprice']);
  655. $tva_npr = get_default_npr($object->thirdparty, $mysoc, $productsupplier->id, $_POST['idprodfournprice']);
  656. if (empty($tva_tx)) $tva_npr=0;
  657. $localtax1_tx= get_localtax($tva_tx, 1, $mysoc, $object->thirdparty, $tva_npr);
  658. $localtax2_tx= get_localtax($tva_tx, 2, $mysoc, $object->thirdparty, $tva_npr);
  659. $type = $productsupplier->type;
  660. $price_base_type = 'HT';
  661. // TODO Save the product supplier ref into database into field ref_supplier (must rename field ref into ref_supplier first)
  662. $result=$object->addline($desc, $productsupplier->fourn_pu, $tva_tx, $localtax1_tx, $localtax2_tx, $qty, $idprod, $remise_percent, $date_start, $date_end, 0, $tva_npr, $price_base_type, $type, -1, 0, $array_options, $productsupplier->fk_unit);
  663. }
  664. if ($idprod == -2 || $idprod == 0)
  665. {
  666. // Product not selected
  667. $error++;
  668. $langs->load("errors");
  669. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ProductOrService")), null, 'errors');
  670. }
  671. if ($idprod == -1)
  672. {
  673. // Quantity too low
  674. $error++;
  675. $langs->load("errors");
  676. setEventMessages($langs->trans("ErrorQtyTooLowForThisSupplier"), null, 'errors');
  677. }
  678. }
  679. else if( GETPOST('price_ht')!=='' || GETPOST('price_ttc')!=='' )
  680. {
  681. $pu_ht = price2num($price_ht, 'MU');
  682. $pu_ttc = price2num(GETPOST('price_ttc'), 'MU');
  683. $tva_npr = (preg_match('/\*/', $tva_tx) ? 1 : 0);
  684. $tva_tx = str_replace('*', '', $tva_tx);
  685. $label = (GETPOST('product_label') ? GETPOST('product_label') : '');
  686. $desc = $product_desc;
  687. $type = GETPOST('type');
  688. $fk_unit= GETPOST('units', 'alpha');
  689. $tva_tx = price2num($tva_tx); // When vat is text input field
  690. // Local Taxes
  691. $localtax1_tx= get_localtax($tva_tx, 1,$mysoc,$object->thirdparty);
  692. $localtax2_tx= get_localtax($tva_tx, 2,$mysoc,$object->thirdparty);
  693. if (!empty($_POST['price_ht']))
  694. {
  695. $ht = price2num($_POST['price_ht']);
  696. $price_base_type = 'HT';
  697. }
  698. else
  699. {
  700. $ttc = price2num($_POST['price_ttc']);
  701. $ht = $ttc / (1 + ($tva_tx / 100));
  702. $price_base_type = 'HT';
  703. }
  704. $result=$object->addline($product_desc, $ht, $tva_tx, $localtax1_tx, $localtax2_tx, $qty, 0, $remise_percent, $date_start, $date_end, 0, $tva_npr, $price_base_type, $type, -1, 0, $array_options, $fk_unit);
  705. }
  706. //print "xx".$tva_tx; exit;
  707. if (! $error && $result > 0)
  708. {
  709. $db->commit();
  710. // Define output language
  711. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  712. {
  713. $outputlangs = $langs;
  714. $newlang = '';
  715. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
  716. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
  717. if (! empty($newlang)) {
  718. $outputlangs = new Translate("", $conf);
  719. $outputlangs->setDefaultLang($newlang);
  720. }
  721. $model=$object->modelpdf;
  722. $ret = $object->fetch($id); // Reload to get new records
  723. $result=$object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  724. if ($result < 0) dol_print_error($db,$result);
  725. }
  726. unset($_POST ['prod_entry_mode']);
  727. unset($_POST['qty']);
  728. unset($_POST['type']);
  729. unset($_POST['remise_percent']);
  730. unset($_POST['pu']);
  731. unset($_POST['price_ht']);
  732. unset($_POST['multicurrency_price_ht']);
  733. unset($_POST['price_ttc']);
  734. unset($_POST['tva_tx']);
  735. unset($_POST['label']);
  736. unset($localtax1_tx);
  737. unset($localtax2_tx);
  738. unset($_POST['np_marginRate']);
  739. unset($_POST['np_markRate']);
  740. unset($_POST['dp_desc']);
  741. unset($_POST['idprodfournprice']);
  742. unset($_POST['units']);
  743. unset($_POST['date_starthour']);
  744. unset($_POST['date_startmin']);
  745. unset($_POST['date_startsec']);
  746. unset($_POST['date_startday']);
  747. unset($_POST['date_startmonth']);
  748. unset($_POST['date_startyear']);
  749. unset($_POST['date_endhour']);
  750. unset($_POST['date_endmin']);
  751. unset($_POST['date_endsec']);
  752. unset($_POST['date_endday']);
  753. unset($_POST['date_endmonth']);
  754. unset($_POST['date_endyear']);
  755. }
  756. else
  757. {
  758. $db->rollback();
  759. setEventMessages($object->error, $object->errors, 'errors');
  760. }
  761. $action = '';
  762. }
  763. elseif ($action == 'classin')
  764. {
  765. $object->fetch($id);
  766. $result=$object->setProject($projectid);
  767. }
  768. // Set invoice to draft status
  769. elseif ($action == 'edit' && $user->rights->fournisseur->facture->creer)
  770. {
  771. $object->fetch($id);
  772. $totalpaye = $object->getSommePaiement();
  773. $resteapayer = $object->total_ttc - $totalpaye;
  774. // On verifie si les lignes de factures ont ete exportees en compta et/ou ventilees
  775. //$ventilExportCompta = $object->getVentilExportCompta();
  776. // On verifie si aucun paiement n'a ete effectue
  777. if ($resteapayer == $object->total_ttc && $object->paye == 0 && $ventilExportCompta == 0)
  778. {
  779. $object->set_draft($user);
  780. // Define output language
  781. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  782. {
  783. $outputlangs = $langs;
  784. $newlang = '';
  785. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
  786. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
  787. if (! empty($newlang)) {
  788. $outputlangs = new Translate("", $conf);
  789. $outputlangs->setDefaultLang($newlang);
  790. }
  791. $model=$object->modelpdf;
  792. $ret = $object->fetch($id); // Reload to get new records
  793. $result=$object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  794. if ($result < 0) dol_print_error($db,$result);
  795. }
  796. $action='';
  797. }
  798. }
  799. // Set invoice to validated/unpaid status
  800. elseif ($action == 'reopen' && $user->rights->fournisseur->facture->creer)
  801. {
  802. $result = $object->fetch($id);
  803. if ($object->statut == FactureFournisseur::STATUS_CLOSED
  804. || ($object->statut == FactureFournisseur::STATUS_ABANDONED && $object->close_code != 'replaced'))
  805. {
  806. $result = $object->set_unpaid($user);
  807. if ($result > 0)
  808. {
  809. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$id);
  810. exit;
  811. }
  812. else
  813. {
  814. setEventMessages($object->error, $object->errors, 'errors');
  815. }
  816. }
  817. }
  818. /*
  819. * Send mail
  820. */
  821. // Actions to send emails
  822. $actiontypecode='AC_SUP_INV';
  823. $trigger_name='BILL_SUPPLIER_SENTBYMAIL';
  824. $paramname='id';
  825. $mode='emailfromsupplierinvoice';
  826. include DOL_DOCUMENT_ROOT.'/core/actions_sendmails.inc.php';
  827. // Build document
  828. if ($action == 'builddoc')
  829. {
  830. // Save modele used
  831. $object->fetch($id);
  832. $object->fetch_thirdparty();
  833. // Save last template used to generate document
  834. if (GETPOST('model')) $object->setDocModel($user, GETPOST('model','alpha'));
  835. $outputlangs = $langs;
  836. $newlang=GETPOST('lang_id','alpha');
  837. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang=$object->thirdparty->default_lang;
  838. if (! empty($newlang))
  839. {
  840. $outputlangs = new Translate("",$conf);
  841. $outputlangs->setDefaultLang($newlang);
  842. }
  843. $result = $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  844. if ($result <= 0)
  845. {
  846. setEventMessages($object->error, $object->errors, 'errors');
  847. $action='';
  848. }
  849. }
  850. // Make calculation according to calculationrule
  851. if ($action == 'calculate')
  852. {
  853. $calculationrule=GETPOST('calculationrule');
  854. $object->fetch($id);
  855. $object->fetch_thirdparty();
  856. $result=$object->update_price(0, (($calculationrule=='totalofround')?'0':'1'), 0, $object->thirdparty);
  857. if ($result <= 0)
  858. {
  859. dol_print_error($db,$result);
  860. exit;
  861. }
  862. }
  863. // Delete file in doc form
  864. if ($action == 'remove_file')
  865. {
  866. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  867. if ($object->fetch($id))
  868. {
  869. $object->fetch_thirdparty();
  870. $upload_dir = $conf->fournisseur->facture->dir_output . "/";
  871. $file = $upload_dir . '/' . GETPOST('file');
  872. $ret=dol_delete_file($file,0,0,0,$object);
  873. if ($ret) setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
  874. else setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
  875. }
  876. }
  877. if ($action == 'update_extras')
  878. {
  879. // Fill array 'array_options' with data from add form
  880. $extralabels=$extrafields->fetch_name_optionals_label($object->table_element);
  881. $ret = $extrafields->setOptionalsFromPost($extralabels,$object,GETPOST('attribute'));
  882. if ($ret < 0) $error++;
  883. if (!$error)
  884. {
  885. // Actions on extra fields (by external module or standard code)
  886. // TODO le hook fait double emploi avec le trigger !!
  887. $hookmanager->initHooks(array('supplierinvoicedao'));
  888. $parameters=array('id'=>$object->id);
  889. $reshook=$hookmanager->executeHooks('insertExtraFields',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  890. if (empty($reshook))
  891. {
  892. if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
  893. {
  894. $result=$object->insertExtraFields();
  895. if ($result < 0)
  896. {
  897. $error++;
  898. }
  899. }
  900. }
  901. else if ($reshook < 0) $error++;
  902. }
  903. else
  904. {
  905. $action = 'edit_extras';
  906. }
  907. }
  908. if (! empty($conf->global->MAIN_DISABLE_CONTACTS_TAB) && $user->rights->fournisseur->facture->creer)
  909. {
  910. if ($action == 'addcontact')
  911. {
  912. $result = $object->fetch($id);
  913. if ($result > 0 && $id > 0)
  914. {
  915. $contactid = (GETPOST('userid') ? GETPOST('userid') : GETPOST('contactid'));
  916. $result = $object->add_contact($contactid, $_POST["type"], $_POST["source"]);
  917. }
  918. if ($result >= 0)
  919. {
  920. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  921. exit;
  922. }
  923. else
  924. {
  925. if ($object->error == 'DB_ERROR_RECORD_ALREADY_EXISTS')
  926. {
  927. $langs->load("errors");
  928. setEventMessages($langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType"), null, 'errors');
  929. }
  930. else
  931. {
  932. setEventMessages($object->error, $object->errors, 'errors');
  933. }
  934. }
  935. }
  936. // bascule du statut d'un contact
  937. else if ($action == 'swapstatut')
  938. {
  939. if ($object->fetch($id))
  940. {
  941. $result=$object->swapContactStatus(GETPOST('ligne'));
  942. }
  943. else
  944. {
  945. dol_print_error($db);
  946. }
  947. }
  948. // Efface un contact
  949. else if ($action == 'deletecontact')
  950. {
  951. $object->fetch($id);
  952. $result = $object->delete_contact($_GET["lineid"]);
  953. if ($result >= 0)
  954. {
  955. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  956. exit;
  957. }
  958. else {
  959. dol_print_error($db);
  960. }
  961. }
  962. }
  963. }
  964. /*
  965. * View
  966. */
  967. $form = new Form($db);
  968. $formfile = new FormFile($db);
  969. $bankaccountstatic=new Account($db);
  970. $paymentstatic=new PaiementFourn($db);
  971. llxHeader('',$langs->trans('SupplierInvoice'),'');
  972. // Mode creation
  973. if ($action == 'create')
  974. {
  975. $facturestatic = new FactureFournisseur($db);
  976. $extralabels = $extrafields->fetch_name_optionals_label($facturestatic->table_element);
  977. print load_fiche_titre($langs->trans('NewBill'));
  978. dol_htmloutput_events();
  979. $currency_code = $conf->currency;
  980. $societe='';
  981. if (GETPOST('socid') > 0)
  982. {
  983. $societe=new Societe($db);
  984. $societe->fetch(GETPOST('socid','int'));
  985. if (!empty($conf->multicurrency->enabled) && !empty($societe->multicurrency_code)) $currency_code = $societe->multicurrency_code;
  986. }
  987. if (GETPOST('origin') && GETPOST('originid'))
  988. {
  989. // Parse element/subelement (ex: project_task)
  990. $element = $subelement = GETPOST('origin');
  991. if ($element == 'project')
  992. {
  993. $projectid=GETPOST('originid');
  994. $element = 'projet';
  995. }
  996. else if (in_array($element,array('order_supplier')))
  997. {
  998. // For compatibility
  999. if ($element == 'order') {
  1000. $element = $subelement = 'commande';
  1001. }
  1002. if ($element == 'propal') {
  1003. $element = 'comm/propal'; $subelement = 'propal';
  1004. }
  1005. if ($element == 'contract') {
  1006. $element = $subelement = 'contrat';
  1007. }
  1008. if ($element == 'order_supplier') {
  1009. $element = 'fourn'; $subelement = 'fournisseur.commande';
  1010. }
  1011. require_once DOL_DOCUMENT_ROOT.'/'.$element.'/class/'.$subelement.'.class.php';
  1012. $classname = ucfirst($subelement);
  1013. if ($classname == 'Fournisseur.commande') $classname='CommandeFournisseur';
  1014. $objectsrc = new $classname($db);
  1015. $objectsrc->fetch(GETPOST('originid'));
  1016. $objectsrc->fetch_thirdparty();
  1017. $projectid = (!empty($objectsrc->fk_project)?$objectsrc->fk_project:'');
  1018. //$ref_client = (!empty($objectsrc->ref_client)?$object->ref_client:'');
  1019. $soc = $objectsrc->thirdparty;
  1020. $cond_reglement_id = (!empty($objectsrc->cond_reglement_id)?$objectsrc->cond_reglement_id:(!empty($soc->cond_reglement_supplier_id)?$soc->cond_reglement_supplier_id:1));
  1021. $mode_reglement_id = (!empty($objectsrc->mode_reglement_id)?$objectsrc->mode_reglement_id:(!empty($soc->mode_reglement_supplier_id)?$soc->mode_reglement_supplier_id:0));
  1022. $fk_account = (! empty($objectsrc->fk_account)?$objectsrc->fk_account:(! empty($soc->fk_account)?$soc->fk_account:0));
  1023. $remise_percent = (!empty($objectsrc->remise_percent)?$objectsrc->remise_percent:(!empty($soc->remise_percent)?$soc->remise_percent:0));
  1024. $remise_absolue = (!empty($objectsrc->remise_absolue)?$objectsrc->remise_absolue:(!empty($soc->remise_absolue)?$soc->remise_absolue:0));
  1025. $dateinvoice = empty($conf->global->MAIN_AUTOFILL_DATE)?-1:'';
  1026. if (!empty($conf->multicurrency->enabled))
  1027. {
  1028. if (!empty($objectsrc->multicurrency_code)) $currency_code = $objectsrc->multicurrency_code;
  1029. if (!empty($conf->global->MULTICURRENCY_USE_ORIGIN_TX) && !empty($objectsrc->multicurrency_tx)) $currency_tx = $objectsrc->multicurrency_tx;
  1030. }
  1031. $datetmp=dol_mktime(12,0,0,$_POST['remonth'],$_POST['reday'],$_POST['reyear']);
  1032. $dateinvoice=($datetmp==''?(empty($conf->global->MAIN_AUTOFILL_DATE)?-1:''):$datetmp);
  1033. $datetmp=dol_mktime(12,0,0,$_POST['echmonth'],$_POST['echday'],$_POST['echyear']);
  1034. $datedue=($datetmp==''?-1:$datetmp);
  1035. }
  1036. }
  1037. else
  1038. {
  1039. $cond_reglement_id = $societe->cond_reglement_supplier_id;
  1040. $mode_reglement_id = $societe->mode_reglement_supplier_id;
  1041. $fk_account = $societe->fk_account;
  1042. $datetmp=dol_mktime(12,0,0,$_POST['remonth'],$_POST['reday'],$_POST['reyear']);
  1043. $dateinvoice=($datetmp==''?(empty($conf->global->MAIN_AUTOFILL_DATE)?-1:''):$datetmp);
  1044. $datetmp=dol_mktime(12,0,0,$_POST['echmonth'],$_POST['echday'],$_POST['echyear']);
  1045. $datedue=($datetmp==''?-1:$datetmp);
  1046. if (!empty($conf->multicurrency->enabled) && !empty($soc->multicurrency_code)) $currency_code = $soc->multicurrency_code;
  1047. }
  1048. print '<form name="add" action="'.$_SERVER["PHP_SELF"].'" method="post">';
  1049. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  1050. print '<input type="hidden" name="action" value="add">';
  1051. print '<input type="hidden" name="origin" value="'.GETPOST('origin').'">';
  1052. print '<input type="hidden" name="originid" value="'.GETPOST('originid').'">';
  1053. if (!empty($currency_tx)) print '<input type="hidden" name="originmulticurrency_tx" value="' . $currency_tx . '">';
  1054. dol_fiche_head();
  1055. print '<table class="border" width="100%">';
  1056. // Ref
  1057. print '<tr><td class="titlefieldcreate">'.$langs->trans('Ref').'</td><td>'.$langs->trans('Draft').'</td></tr>';
  1058. // Third party
  1059. print '<tr><td class="fieldrequired">'.$langs->trans('Supplier').'</td>';
  1060. print '<td>';
  1061. if (GETPOST('socid') > 0)
  1062. {
  1063. print $societe->getNomUrl(1);
  1064. print '<input type="hidden" name="socid" value="'.GETPOST('socid','int').'">';
  1065. }
  1066. else
  1067. {
  1068. print $form->select_company(GETPOST('socid','int'), 'socid', 's.fournisseur = 1', 'SelectThirdParty');
  1069. }
  1070. print '</td></tr>';
  1071. // Ref supplier
  1072. print '<tr><td class="fieldrequired">'.$langs->trans('RefSupplier').'</td><td><input name="ref_supplier" value="'.(isset($_POST['ref_supplier'])?$_POST['ref_supplier']:'').'" type="text"></td>';
  1073. print '</tr>';
  1074. print '<tr><td valign="top" class="fieldrequired">'.$langs->trans('Type').'</td><td colspan="2">';
  1075. print '<table class="nobordernopadding">'."\n";
  1076. // Standard invoice
  1077. print '<tr height="18"><td width="16px" valign="middle">';
  1078. print '<input type="radio" name="type" value="0"'.($_POST['type']==0?' checked':'').'>';
  1079. print '</td><td valign="middle">';
  1080. $desc=$form->textwithpicto($langs->trans("InvoiceStandardAsk"),$langs->transnoentities("InvoiceStandardDesc"),1);
  1081. print $desc;
  1082. print '</td></tr>'."\n";
  1083. /*
  1084. // Deposit
  1085. print '<tr height="18"><td width="16px" valign="middle">';
  1086. print '<input type="radio" name="type" value="3"'.($_POST['type']==3?' checked':'').'>';
  1087. print '</td><td valign="middle">';
  1088. $desc=$form->textwithpicto($langs->trans("InvoiceDeposit"),$langs->transnoentities("InvoiceDepositDesc"),1);
  1089. print $desc;
  1090. print '</td></tr>'."\n";
  1091. // Proforma
  1092. if (! empty($conf->global->FACTURE_USE_PROFORMAT))
  1093. {
  1094. print '<tr height="18"><td width="16px" valign="middle">';
  1095. print '<input type="radio" name="type" value="4"'.($_POST['type']==4?' checked':'').'>';
  1096. print '</td><td valign="middle">';
  1097. $desc=$form->textwithpicto($langs->trans("InvoiceProForma"),$langs->transnoentities("InvoiceProFormaDesc"),1);
  1098. print $desc;
  1099. print '</td></tr>'."\n";
  1100. }
  1101. // Replacement
  1102. print '<tr height="18"><td valign="middle">';
  1103. print '<input type="radio" name="type" value="1"'.($_POST['type']==1?' checked':'');
  1104. if (! $options) print ' disabled';
  1105. print '>';
  1106. print '</td><td valign="middle">';
  1107. $text=$langs->trans("InvoiceReplacementAsk").' ';
  1108. $text.='<select class="flat" name="fac_replacement"';
  1109. if (! $options) $text.=' disabled';
  1110. $text.='>';
  1111. if ($options)
  1112. {
  1113. $text.='<option value="-1">&nbsp;</option>';
  1114. $text.=$options;
  1115. }
  1116. else
  1117. {
  1118. $text.='<option value="-1">'.$langs->trans("NoReplacableInvoice").'</option>';
  1119. }
  1120. $text.='</select>';
  1121. $desc=$form->textwithpicto($text,$langs->transnoentities("InvoiceReplacementDesc"),1);
  1122. print $desc;
  1123. print '</td></tr>';
  1124. // Credit note
  1125. print '<tr height="18"><td valign="middle">';
  1126. print '<input type="radio" name="type" value="2"'.($_POST['type']==2?' checked':'');
  1127. if (! $optionsav) print ' disabled';
  1128. print '>';
  1129. print '</td><td valign="middle">';
  1130. $text=$langs->transnoentities("InvoiceAvoirAsk").' ';
  1131. // $text.='<input type="text" value="">';
  1132. $text.='<select class="flat" name="fac_avoir"';
  1133. if (! $optionsav) $text.=' disabled';
  1134. $text.='>';
  1135. if ($optionsav)
  1136. {
  1137. $text.='<option value="-1">&nbsp;</option>';
  1138. $text.=$optionsav;
  1139. }
  1140. else
  1141. {
  1142. $text.='<option value="-1">'.$langs->trans("NoInvoiceToCorrect").'</option>';
  1143. }
  1144. $text.='</select>';
  1145. $desc=$form->textwithpicto($text,$langs->transnoentities("InvoiceAvoirDesc"),1);
  1146. print $desc;
  1147. print '</td></tr>'."\n";
  1148. */
  1149. print '</table>';
  1150. print '</td></tr>';
  1151. // Label
  1152. print '<tr><td>'.$langs->trans('Label').'</td><td><input size="30" name="libelle" value="'.(isset($_POST['libelle'])?$_POST['libelle']:'').'" type="text"></td></tr>';
  1153. // Date invoice
  1154. print '<tr><td class="fieldrequired">'.$langs->trans('DateInvoice').'</td><td>';
  1155. $form->select_date($dateinvoice,'','','','',"add",1,1);
  1156. print '</td></tr>';
  1157. // Due date
  1158. print '<tr><td>'.$langs->trans('DateMaxPayment').'</td><td>';
  1159. $form->select_date($datedue,'ech','','','',"add",1,1);
  1160. print '</td></tr>';
  1161. // Payment term
  1162. print '<tr><td class="nowrap">'.$langs->trans('PaymentConditionsShort').'</td><td colspan="2">';
  1163. $form->select_conditions_paiements(isset($_POST['cond_reglement_id'])?$_POST['cond_reglement_id']:$cond_reglement_id, 'cond_reglement_id');
  1164. print '</td></tr>';
  1165. // Payment mode
  1166. print '<tr><td>'.$langs->trans('PaymentMode').'</td><td colspan="2">';
  1167. $form->select_types_paiements(isset($_POST['mode_reglement_id'])?$_POST['mode_reglement_id']:$mode_reglement_id, 'mode_reglement_id', 'DBIT');
  1168. print '</td></tr>';
  1169. // Bank Account
  1170. print '<tr><td>'.$langs->trans('BankAccount').'</td><td colspan="2">';
  1171. $form->select_comptes($fk_account, 'fk_account', 0, '', 1);
  1172. print '</td></tr>';
  1173. // Multicurrency
  1174. if (! empty($conf->multicurrency->enabled))
  1175. {
  1176. print '<tr>';
  1177. print '<td>'.fieldLabel('Currency','multicurrency_code').'</td>';
  1178. print '<td colspan="2" class="maxwidthonsmartphone">';
  1179. print $form->selectMultiCurrency($currency_code, 'multicurrency_code');
  1180. print '</td></tr>';
  1181. }
  1182. // Project
  1183. if (! empty($conf->projet->enabled))
  1184. {
  1185. $formproject = new FormProjets($db);
  1186. $langs->load('projects');
  1187. print '<tr><td>' . $langs->trans('Project') . '</td><td colspan="2">';
  1188. $formproject->select_projects((empty($conf->global->PROJECT_CAN_ALWAYS_LINK_TO_ALL_SUPPLIERS)?$societe->id:-1), $projectid, 'projectid', 0, 0, 1, 1);
  1189. print '</td></tr>';
  1190. }
  1191. // Incoterms
  1192. if (!empty($conf->incoterm->enabled))
  1193. {
  1194. print '<tr>';
  1195. print '<td><label for="incoterm_id">'.$form->textwithpicto($langs->trans("IncotermLabel"), $objectsrc->libelle_incoterms, 1).'</label></td>';
  1196. print '<td colspan="3" class="maxwidthonsmartphone">';
  1197. print $form->select_incoterms((!empty($objectsrc->fk_incoterms) ? $objectsrc->fk_incoterms : ''), (!empty($objectsrc->location_incoterms)?$objectsrc->location_incoterms:''));
  1198. print '</td></tr>';
  1199. }
  1200. // Public note
  1201. print '<tr><td>'.$langs->trans('NotePublic').'</td>';
  1202. print '<td>';
  1203. $note_public = $object->getDefaultCreateValueFor('note_public');
  1204. $doleditor = new DolEditor('note_public', $note_public, '', 80, 'dolibarr_notes', 'In', 0, false, true, ROWS_3, 70);
  1205. print $doleditor->Create(1);
  1206. print '</td>';
  1207. // print '<td><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_5.'"></textarea></td>';
  1208. print '</tr>';
  1209. // Private note
  1210. print '<tr><td>'.$langs->trans('NotePrivate').'</td>';
  1211. print '<td>';
  1212. $note_private = $object->getDefaultCreateValueFor('note_private');
  1213. $doleditor = new DolEditor('note_private', $note_private, '', 80, 'dolibarr_notes', 'In', 0, false, true, ROWS_3, 70);
  1214. print $doleditor->Create(1);
  1215. print '</td>';
  1216. // print '<td><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_5.'"></textarea></td>';
  1217. print '</tr>';
  1218. if (empty($reshook) && ! empty($extrafields->attribute_label))
  1219. {
  1220. print $object->showOptionals($extrafields, 'edit');
  1221. }
  1222. if (is_object($objectsrc))
  1223. {
  1224. print "\n<!-- ".$classname." info -->";
  1225. print "\n";
  1226. print '<input type="hidden" name="amount" value="'.$objectsrc->total_ht.'">'."\n";
  1227. print '<input type="hidden" name="total" value="'.$objectsrc->total_ttc.'">'."\n";
  1228. print '<input type="hidden" name="tva" value="'.$objectsrc->total_tva.'">'."\n";
  1229. print '<input type="hidden" name="origin" value="'.$objectsrc->element.'">';
  1230. print '<input type="hidden" name="originid" value="'.$objectsrc->id.'">';
  1231. $txt=$langs->trans($classname);
  1232. if ($classname=='CommandeFournisseur') {
  1233. $langs->load('orders');
  1234. $txt=$langs->trans("SupplierOrder");
  1235. }
  1236. print '<tr><td>'.$txt.'</td><td colspan="2">'.$objectsrc->getNomUrl(1);
  1237. // We check if Origin document (id and type is known) has already at least one invoice attached to it
  1238. $objectsrc->fetchObjectLinked($originid,$origin,'','invoice_supplier');
  1239. $cntinvoice=count($objectsrc->linkedObjects['invoice_supplier']);
  1240. if ($cntinvoice>=1)
  1241. {
  1242. setEventMessages('WarningBillExist', null, 'warnings');
  1243. echo ' ('.$langs->trans('LatestRelatedBill').end($objectsrc->linkedObjects['invoice_supplier'])->getNomUrl(1).')';
  1244. }
  1245. echo '</td></tr>';
  1246. print '<tr><td>'.$langs->trans('TotalHT').'</td><td colspan="2">'.price($objectsrc->total_ht).'</td></tr>';
  1247. print '<tr><td>'.$langs->trans('TotalVAT').'</td><td colspan="2">'.price($objectsrc->total_tva)."</td></tr>";
  1248. if ($mysoc->country_code=='ES')
  1249. {
  1250. if ($mysoc->localtax1_assuj=="1" || $object->total_localtax1 != 0) //Localtax1
  1251. {
  1252. print '<tr><td>'.$langs->transcountry("AmountLT1",$mysoc->country_code).'</td><td colspan="2">'.price($objectsrc->total_localtax1)."</td></tr>";
  1253. }
  1254. if ($mysoc->localtax2_assuj=="1" || $object->total_localtax2 != 0) //Localtax2
  1255. {
  1256. print '<tr><td>'.$langs->transcountry("AmountLT2",$mysoc->country_code).'</td><td colspan="2">'.price($objectsrc->total_localtax2)."</td></tr>";
  1257. }
  1258. }
  1259. print '<tr><td>'.$langs->trans('TotalTTC').'</td><td colspan="2">'.price($objectsrc->total_ttc)."</td></tr>";
  1260. if (!empty($conf->multicurrency->enabled))
  1261. {
  1262. print '<tr><td>' . $langs->trans('MulticurrencyTotalHT') . '</td><td colspan="2">' . price($objectsrc->multicurrency_total_ht) . '</td></tr>';
  1263. print '<tr><td>' . $langs->trans('MulticurrencyTotalVAT') . '</td><td colspan="2">' . price($objectsrc->multicurrency_total_tva) . "</td></tr>";
  1264. print '<tr><td>' . $langs->trans('MulticurrencyTotalTTC') . '</td><td colspan="2">' . price($objectsrc->multicurrency_total_ttc) . "</td></tr>";
  1265. }
  1266. }
  1267. else
  1268. {
  1269. // TODO more bugs
  1270. if (1==2 && ! empty($conf->global->PRODUCT_SHOW_WHEN_CREATE))
  1271. {
  1272. print '<tr class="liste_titre">';
  1273. print '<td>&nbsp;</td>';
  1274. print '<td>'.$langs->trans('Label').'</td>';
  1275. print '<td align="right">'.$langs->trans('PriceUHT').'</td>';
  1276. print '<td align="right">'.$langs->trans('VAT').'</td>';
  1277. print '<td align="right">'.$langs->trans('Qty').'</td>';
  1278. print '<td align="right">'.$langs->trans('PriceUTTC').'</td>';
  1279. print '</tr>';
  1280. for ($i = 1 ; $i < 9 ; $i++)
  1281. {
  1282. $value_qty = '1';
  1283. $value_tauxtva = '';
  1284. print '<tr><td>'.$i.'</td>';
  1285. print '<td><input size="50" name="label'.$i.'" value="'.$value_label.'" type="text"></td>';
  1286. print '<td align="right"><input type="text" size="8" name="amount'.$i.'" value="'.$value_pu.'"></td>';
  1287. print '<td align="right">';
  1288. print $form->load_tva('tauxtva'.$i,$value_tauxtva,$societe,$mysoc);
  1289. print '</td>';
  1290. print '<td align="right"><input type="text" size="3" name="qty'.$i.'" value="'.$value_qty.'"></td>';
  1291. print '<td align="right"><input type="text" size="8" name="amountttc'.$i.'" value=""></td></tr>';
  1292. }
  1293. }
  1294. }
  1295. // Other options
  1296. $parameters=array('colspan' => ' colspan="6"');
  1297. $reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
  1298. // Bouton "Create Draft"
  1299. print "</table>\n";
  1300. dol_fiche_end();
  1301. print '<div class="center">';
  1302. print '<input type="submit" class="button" name="bouton" value="'.$langs->trans('CreateDraft').'">';
  1303. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  1304. print '<input type="button" class="button" value="' . $langs->trans("Cancel") . '" onClick="javascript:history.go(-1)">';
  1305. print '</div>';
  1306. print "</form>\n";
  1307. // Show origin lines
  1308. if (is_object($objectsrc))
  1309. {
  1310. print '<br>';
  1311. $title=$langs->trans('ProductsAndServices');
  1312. print load_fiche_titre($title);
  1313. print '<table class="noborder" width="100%">';
  1314. $objectsrc->printOriginLinesList();
  1315. print '</table>';
  1316. }
  1317. }
  1318. else
  1319. {
  1320. if ($id > 0 || ! empty($ref))
  1321. {
  1322. /* *************************************************************************** */
  1323. /* */
  1324. /* Fiche en mode visu ou edition */
  1325. /* */
  1326. /* *************************************************************************** */
  1327. $now=dol_now();
  1328. $productstatic = new Product($db);
  1329. $object->fetch($id,$ref);
  1330. $result=$object->fetch_thirdparty();
  1331. if ($result < 0) dol_print_error($db);
  1332. $societe = new Fournisseur($db);
  1333. $result=$societe->fetch($object->socid);
  1334. if ($result < 0) dol_print_error($db);
  1335. // fetch optionals attributes and labels
  1336. $extralabels = $extrafields->fetch_name_optionals_label($object->table_element);
  1337. /*
  1338. * View card
  1339. */
  1340. $head = facturefourn_prepare_head($object);
  1341. $titre=$langs->trans('SupplierInvoice');
  1342. dol_fiche_head($head, 'card', $titre, 0, 'bill');
  1343. // Clone confirmation
  1344. if ($action == 'clone')
  1345. {
  1346. // Create an array for form
  1347. $formquestion=array(
  1348. //'text' => $langs->trans("ConfirmClone"),
  1349. //array('type' => 'checkbox', 'name' => 'clone_content', 'label' => $langs->trans("CloneMainAttributes"), 'value' => 1)
  1350. );
  1351. // Paiement incomplet. On demande si motif = escompte ou autre
  1352. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id,$langs->trans('CloneInvoice'),$langs->trans('ConfirmCloneInvoice',$object->ref),'confirm_clone',$formquestion,'yes', 1);
  1353. }
  1354. // Confirmation de la validation
  1355. if ($action == 'valid')
  1356. {
  1357. // on verifie si l'objet est en numerotation provisoire
  1358. $objectref = substr($object->ref, 1, 4);
  1359. if ($objectref == 'PROV')
  1360. {
  1361. $savdate=$object->date;
  1362. $numref = $object->getNextNumRef($societe);
  1363. }
  1364. else
  1365. {
  1366. $numref = $object->ref;
  1367. }
  1368. $text=$langs->trans('ConfirmValidateBill',$numref);
  1369. /*if (! empty($conf->notification->enabled))
  1370. {
  1371. require_once DOL_DOCUMENT_ROOT .'/core/class/notify.class.php';
  1372. $notify=new Notify($db);
  1373. $text.='<br>';
  1374. $text.=$notify->confirmMessage('BILL_SUPPLIER_VALIDATE',$object->socid, $object);
  1375. }*/
  1376. $formquestion=array();
  1377. $qualified_for_stock_change=0;
  1378. if (empty($conf->global->STOCK_SUPPORTS_SERVICES))
  1379. {
  1380. $qualified_for_stock_change=$object->hasProductsOrServices(2);
  1381. }
  1382. else
  1383. {
  1384. $qualified_for_stock_change=$object->hasProductsOrServices(1);
  1385. }
  1386. if (! empty($conf->stock->enabled) && ! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL) && $qualified_for_stock_change)
  1387. {
  1388. $langs->load("stocks");
  1389. require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
  1390. $formproduct=new FormProduct($db);
  1391. $formquestion=array(
  1392. //'text' => $langs->trans("ConfirmClone"),
  1393. //array('type' => 'checkbox', 'name' => 'clone_content', 'label' => $langs->trans("CloneMainAttributes"), 'value' => 1),
  1394. //array('type' => 'checkbox', 'name' => 'update_prices', 'label' => $langs->trans("PuttingPricesUpToDate"), 'value' => 1),
  1395. array('type' => 'other', 'name' => 'idwarehouse', 'label' => $langs->trans("SelectWarehouseForStockIncrease"), 'value' => $formproduct->selectWarehouses(GETPOST('idwarehouse'),'idwarehouse','',1)));
  1396. }
  1397. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ValidateBill'), $text, 'confirm_valid', $formquestion, 1, 1, 240);
  1398. }
  1399. // Confirmation set paid
  1400. if ($action == 'paid')
  1401. {
  1402. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ClassifyPaid'), $langs->trans('ConfirmClassifyPaidBill', $object->ref), 'confirm_paid', '', 0, 1);
  1403. }
  1404. // Confirmation de la suppression de la facture fournisseur
  1405. if ($action == 'delete')
  1406. {
  1407. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteBill'), $langs->trans('ConfirmDeleteBill'), 'confirm_delete', '', 0, 1);
  1408. }
  1409. // Confirmation to delete line
  1410. if ($action == 'ask_deleteline')
  1411. {
  1412. $formconfirm=$form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&lineid='.$lineid, $langs->trans('DeleteProductLine'), $langs->trans('ConfirmDeleteProductLine'), 'confirm_deleteline', '', 0, 1);
  1413. }
  1414. if (!$formconfirm) {
  1415. $parameters=array('lineid'=>$lineid);
  1416. $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  1417. if (empty($reshook)) $formconfirm.=$hookmanager->resPrint;
  1418. elseif ($reshook > 0) $formconfirm=$hookmanager->resPrint;
  1419. }
  1420. // Print form confirm
  1421. print $formconfirm;
  1422. /**
  1423. * Invoice
  1424. */
  1425. print '<table class="border" width="100%">';
  1426. $linkback = '<a href="'.DOL_URL_ROOT.'/fourn/facture/list.php'.(! empty($socid)?'?socid='.$socid:'').'">'.$langs->trans("BackToList").'</a>';
  1427. // Ref
  1428. print '<tr><td class="nowrap" width="20%">'.$langs->trans("Ref").'</td><td colspan="4">';
  1429. print $form->showrefnav($object, 'ref', $linkback, 1, 'ref', 'ref');
  1430. print '</td>';
  1431. print "</tr>\n";
  1432. // Ref supplier
  1433. print '<tr><td>'.$form->editfieldkey("RefSupplier",'ref_supplier',$object->ref_supplier,$object,($object->statut<FactureFournisseur::STATUS_CLOSED && $user->rights->fournisseur->facture->creer)).'</td><td colspan="4">';
  1434. print $form->editfieldval("RefSupplier",'ref_supplier',$object->ref_supplier,$object,($object->statut<FactureFournisseur::STATUS_CLOSED && $user->rights->fournisseur->facture->creer));
  1435. print '</td></tr>';
  1436. // Third party
  1437. print '<tr><td>'.$langs->trans('Supplier').'</td><td colspan="4">'.$societe->getNomUrl(1,'supplier');
  1438. print ' &nbsp; (<a href="'.DOL_URL_ROOT.'/fourn/facture/list.php?socid='.$object->socid.'">'.$langs->trans('OtherBills').'</a>)</td>';
  1439. print '</tr>';
  1440. // Type
  1441. print '<tr><td>'.$langs->trans('Type').'</td><td colspan="4">';
  1442. print $object->getLibType();
  1443. if ($object->type == FactureFournisseur::TYPE_REPLACEMENT)
  1444. {
  1445. $facreplaced=new FactureFournisseur($db);
  1446. $facreplaced->fetch($object->fk_facture_source);
  1447. print ' ('.$langs->transnoentities("ReplaceInvoice",$facreplaced->getNomUrl(1)).')';
  1448. }
  1449. if ($object->type == FactureFournisseur::TYPE_CREDIT_NOTE)
  1450. {
  1451. $facusing=new FactureFournisseur($db);
  1452. $facusing->fetch($object->fk_facture_source);
  1453. print ' ('.$langs->transnoentities("CorrectInvoice",$facusing->getNomUrl(1)).')';
  1454. }
  1455. $facidavoir=$object->getListIdAvoirFromInvoice();
  1456. if (count($facidavoir) > 0)
  1457. {
  1458. print ' ('.$langs->transnoentities("InvoiceHasAvoir");
  1459. $i=0;
  1460. foreach($facidavoir as $id)
  1461. {
  1462. if ($i==0) print ' ';
  1463. else print ',';
  1464. $facavoir=new FactureFournisseur($db);
  1465. $facavoir->fetch($id);
  1466. print $facavoir->getNomUrl(1);
  1467. }
  1468. print ')';
  1469. }
  1470. if (isset($facidnext) && $facidnext > 0)
  1471. {
  1472. $facthatreplace=new FactureFournisseur($db);
  1473. $facthatreplace->fetch($facidnext);
  1474. print ' ('.$langs->transnoentities("ReplacedByInvoice",$facthatreplace->getNomUrl(1)).')';
  1475. }
  1476. print '</td></tr>';
  1477. // Label
  1478. print '<tr><td>'.$form->editfieldkey("Label",'libelle',$object->label,$object,($user->rights->fournisseur->facture->creer)).'</td>';
  1479. print '<td colspan="3">'.$form->editfieldval("Label",'libelle',$object->label,$object,($user->rights->fournisseur->facture->creer)).'</td>';
  1480. /*
  1481. * List of payments
  1482. */
  1483. $nbrows=9; $nbcols=3;
  1484. if (! empty($conf->projet->enabled)) $nbrows++;
  1485. if (! empty($conf->banque->enabled)) { $nbrows++; $nbcols++; }
  1486. if (! empty($conf->incoterm->enabled)) $nbrows++;
  1487. // Local taxes
  1488. if ($societe->localtax1_assuj=="1") $nbrows++;
  1489. if ($societe->localtax2_assuj=="1") $nbrows++;
  1490. print '<td rowspan="'.$nbrows.'" valign="top">';
  1491. $sql = 'SELECT p.datep as dp, p.ref, p.num_paiement, p.rowid, p.fk_bank,';
  1492. $sql.= ' c.id as paiement_type,';
  1493. $sql.= ' pf.amount,';
  1494. $sql.= ' ba.rowid as baid, ba.ref as baref, ba.label';
  1495. $sql.= ' FROM '.MAIN_DB_PREFIX.'paiementfourn as p';
  1496. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON p.fk_bank = b.rowid';
  1497. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank_account as ba ON b.fk_account = ba.rowid';
  1498. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as c ON p.fk_paiement = c.id';
  1499. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiementfourn_facturefourn as pf ON pf.fk_paiementfourn = p.rowid';
  1500. $sql.= ' WHERE pf.fk_facturefourn = '.$object->id;
  1501. $sql.= ' ORDER BY p.datep, p.tms';
  1502. $result = $db->query($sql);
  1503. if ($result)
  1504. {
  1505. $num = $db->num_rows($result);
  1506. $i = 0; $totalpaye = 0;
  1507. print '<table class="nobordernopadding paymenttable" width="100%">';
  1508. print '<tr class="liste_titre">';
  1509. print '<td>'.$langs->trans('Payments').'</td>';
  1510. print '<td>'.$langs->trans('Date').'</td>';
  1511. print '<td>'.$langs->trans('Type').'</td>';
  1512. if (! empty($conf->banque->enabled)) print '<td align="right">'.$langs->trans('BankAccount').'</td>';
  1513. print '<td align="right">'.$langs->trans('Amount').'</td>';
  1514. print '<td width="18">&nbsp;</td>';
  1515. print '</tr>';
  1516. $var=true;
  1517. if ($num > 0)
  1518. {
  1519. while ($i < $num)
  1520. {
  1521. $objp = $db->fetch_object($result);
  1522. $var=!$var;
  1523. print '<tr '.$bc[$var].'><td>';
  1524. $paymentstatic->id=$objp->rowid;
  1525. $paymentstatic->datepaye=$db->jdate($objp->dp);
  1526. $paymentstatic->ref=$objp->ref;
  1527. $paymentstatic->num_paiement=$objp->num_paiement;
  1528. $paymentstatic->payment_code=$objp->payment_code;
  1529. print $paymentstatic->getNomUrl(1);
  1530. print '</td>';
  1531. print '<td>'.dol_print_date($db->jdate($objp->dp), 'day') . '</td>';
  1532. print '<td>';
  1533. print $form->form_modes_reglement(null, $objp->paiement_type,'none').' '.$objp->num_paiement;
  1534. print '</td>';
  1535. if (! empty($conf->banque->enabled))
  1536. {
  1537. $bankaccountstatic->id=$objp->baid;
  1538. $bankaccountstatic->ref=$objp->baref;
  1539. $bankaccountstatic->label=$objp->baref;
  1540. print '<td align="right">';
  1541. if ($objp->baid > 0) print $bankaccountstatic->getNomUrl(1,'transactions');
  1542. print '</td>';
  1543. }
  1544. print '<td align="right">'.price($objp->amount).'</td>';
  1545. print '<td align="center">';
  1546. if ($object->statut == FactureFournisseur::STATUS_VALIDATED && $object->paye == 0 && $user->societe_id == 0)
  1547. {
  1548. print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=deletepaiement&paiement_id='.$objp->rowid.'">';
  1549. print img_delete();
  1550. print '</a>';
  1551. }
  1552. print '</td>';
  1553. print '</tr>';
  1554. $totalpaye += $objp->amount;
  1555. $i++;
  1556. }
  1557. }
  1558. else
  1559. {
  1560. print '<tr '.$bc[$var].'><td colspan="'.$nbcols.'" class="opacitymedium">'.$langs->trans("None").'</td><td></td><td></td></tr>';
  1561. }
  1562. if ($object->paye == 0)
  1563. {
  1564. print '<tr><td colspan="'.$nbcols.'" align="right">'.$langs->trans('AlreadyPaid').' :</td><td align="right"><b>'.price($totalpaye).'</b></td><td></td></tr>';
  1565. print '<tr><td colspan="'.$nbcols.'" align="right">'.$langs->trans("Billed").' :</td><td align="right" style="border: 1px solid;">'.price($object->total_ttc).'</td><td></td></tr>';
  1566. $resteapayer = $object->total_ttc - $totalpaye;
  1567. print '<tr><td colspan="'.$nbcols.'" align="right">'.$langs->trans('RemainderToPay').' :</td>';
  1568. print '<td align="right" style="border: 1px solid;" bgcolor="#f0f0f0"><b>'.price($resteapayer).'</b></td><td></td></tr>';
  1569. }
  1570. print '</table>';
  1571. $db->free($result);
  1572. }
  1573. else
  1574. {
  1575. dol_print_error($db);
  1576. }
  1577. print '</td>';
  1578. print '</tr>';
  1579. $form_permission = $object->statut<FactureFournisseur::STATUS_CLOSED && $user->rights->fournisseur->facture->creer && $object->getSommePaiement() <= 0;
  1580. // Date
  1581. print '<tr><td>'.$form->editfieldkey("Date",'datef',$object->datep,$object,$form_permission,'datepicker').'</td><td colspan="3">';
  1582. print $form->editfieldval("Date",'datef',$object->datep,$object,$form_permission,'datepicker');
  1583. print '</td>';
  1584. // Due date
  1585. print '<tr><td>'.$form->editfieldkey("DateMaxPayment",'date_lim_reglement',$object->date_echeance,$object,$form_permission,'datepicker').'</td><td colspan="3">';
  1586. print $form->editfieldval("DateMaxPayment",'date_lim_reglement',$object->date_echeance,$object,$form_permission,'datepicker');
  1587. if ($action != 'editdate_lim_reglement' && $object->hasDelay()) {
  1588. print img_warning($langs->trans('Late'));
  1589. }
  1590. print '</td>';
  1591. // Conditions de reglement par defaut
  1592. $langs->load('bills');
  1593. print '<tr><td class="nowrap">';
  1594. print '<table width="100%" class="nobordernopadding"><tr><td class="nowrap">';
  1595. print $langs->trans('PaymentConditions');
  1596. print '<td>';
  1597. if ($action != 'editconditions') print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=editconditions&amp;id='.$object->id.'">'.img_edit($langs->trans('SetConditions'),1).'</a></td>';
  1598. print '</tr></table>';
  1599. print '</td><td colspan="2">';
  1600. if ($action == 'editconditions')
  1601. {
  1602. $form->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->cond_reglement_id,'cond_reglement_id');
  1603. }
  1604. else
  1605. {
  1606. $form->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->cond_reglement_id,'none');
  1607. }
  1608. print "</td>";
  1609. print '</tr>';
  1610. // Mode of payment
  1611. $langs->load('bills');
  1612. print '<tr><td class="nowrap">';
  1613. print '<table width="100%" class="nobordernopadding"><tr><td class="nowrap">';
  1614. print $langs->trans('PaymentMode');
  1615. print '</td>';
  1616. if ($action != 'editmode') print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=editmode&amp;id='.$object->id.'">'.img_edit($langs->trans('SetMode'),1).'</a></td>';
  1617. print '</tr></table>';
  1618. print '</td><td colspan="2">';
  1619. if ($action == 'editmode')
  1620. {
  1621. $form->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->mode_reglement_id, 'mode_reglement_id', 'DBIT');
  1622. }
  1623. else
  1624. {
  1625. $form->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->mode_reglement_id, 'none', 'DBIT');
  1626. }
  1627. print '</td></tr>';
  1628. // Multicurrency
  1629. if (! empty($conf->multicurrency->enabled))
  1630. {
  1631. // Multicurrency code
  1632. print '<tr>';
  1633. print '<td>';
  1634. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1635. print fieldLabel('Currency','multicurrency_code');
  1636. print '</td>';
  1637. if ($action != 'editmulticurrencycode' && ! empty($object->brouillon))
  1638. print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editmulticurrencycode&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetMultiCurrencyCode'), 1) . '</a></td>';
  1639. print '</tr></table>';
  1640. print '</td><td colspan="3">';
  1641. if ($action == 'editmulticurrencycode') {
  1642. $form->form_multicurrency_code($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->multicurrency_code, 'multicurrency_code');
  1643. } else {
  1644. $form->form_multicurrency_code($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->multicurrency_code, 'none');
  1645. }
  1646. print '</td></tr>';
  1647. // Multicurrency rate
  1648. print '<tr>';
  1649. print '<td>';
  1650. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1651. print fieldLabel('Rate','multicurrency_tx');
  1652. print '</td>';
  1653. if ($action != 'editmulticurrencyrate' && ! empty($object->brouillon))
  1654. print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editmulticurrencyrate&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetMultiCurrencyCode'), 1) . '</a></td>';
  1655. print '</tr></table>';
  1656. print '</td><td colspan="3">';
  1657. if ($action == 'editmulticurrencyrate') {
  1658. $form->form_multicurrency_rate($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->multicurrency_tx, 'multicurrency_tx', $object->multicurrency_code);
  1659. } else {
  1660. $form->form_multicurrency_rate($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->multicurrency_tx, 'none', $object->multicurrency_code);
  1661. }
  1662. print '</td></tr>';
  1663. }
  1664. // Bank Account
  1665. print '<tr><td class="nowrap">';
  1666. print '<table width="100%" class="nobordernopadding"><tr><td class="nowrap">';
  1667. print $langs->trans('BankAccount');
  1668. print '<td>';
  1669. if ($action != 'editbankaccount' && $user->rights->fournisseur->facture->creer)
  1670. print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=editbankaccount&amp;id='.$object->id.'">'.img_edit($langs->trans('SetBankAccount'),1).'</a></td>';
  1671. print '</tr></table>';
  1672. print '</td><td colspan="3">';
  1673. if ($action == 'editbankaccount') {
  1674. $form->formSelectAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->fk_account, 'fk_account', 1);
  1675. } else {
  1676. $form->formSelectAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->fk_account, 'none');
  1677. }
  1678. print "</td>";
  1679. print '</tr>';
  1680. // Status
  1681. $alreadypaid=$object->getSommePaiement();
  1682. print '<tr><td>'.$langs->trans('Status').'</td><td colspan="3">'.$object->getLibStatut(4,$alreadypaid).'</td></tr>';
  1683. // Amount
  1684. print '<tr><td>'.$langs->trans('AmountHT').'</td><td colspan="3">'.price($object->total_ht,1,$langs,0,-1,-1,$conf->currency).'</td></tr>';
  1685. print '<tr><td>'.$langs->trans('AmountVAT').'</td><td>'.price($object->total_tva,1,$langs,0,-1,-1,$conf->currency).'</td><td colspan="2" align="left">';
  1686. if (GETPOST('calculationrule')) $calculationrule=GETPOST('calculationrule','alpha');
  1687. else $calculationrule=(empty($conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND)?'totalofround':'roundoftotal');
  1688. if ($calculationrule == 'totalofround') $calculationrulenum=1;
  1689. else $calculationrulenum=2;
  1690. $s=$langs->trans("ReCalculate").' ';
  1691. $s.='<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=calculate&calculationrule=totalofround">'.$langs->trans("Mode1").'</a>';
  1692. $s.=' / ';
  1693. $s.='<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=calculate&calculationrule=roundoftotal">'.$langs->trans("Mode2").'</a>';
  1694. print $form->textwithtooltip($s, $langs->trans("CalculationRuleDesc",$calculationrulenum).'<br>'.$langs->trans("CalculationRuleDescSupplier"), 2, 1, img_picto('','help'));
  1695. print '</td></tr>';
  1696. // Amount Local Taxes
  1697. //TODO: Place into a function to control showing by country or study better option
  1698. if ($societe->localtax1_assuj=="1") //Localtax1
  1699. {
  1700. print '<tr><td>'.$langs->transcountry("AmountLT1",$societe->country_code).'</td>';
  1701. print '<td colspan="3">'.price($object->total_localtax1,1,$langs,0,-1,-1,$conf->currency).'</td>';
  1702. print '</tr>';
  1703. }
  1704. if ($societe->localtax2_assuj=="1") //Localtax2
  1705. {
  1706. print '<tr><td>'.$langs->transcountry("AmountLT2",$societe->country_code).'</td>';
  1707. print '<td colspan="3">'.price($object->total_localtax2,1,$langs,0,-1,-1,$conf->currency).'</td>';
  1708. print '</tr>';
  1709. }
  1710. print '<tr><td>'.$langs->trans('AmountTTC').'</td><td colspan="3">'.price($object->total_ttc,1,$langs,0,-1,-1,$conf->currency).'</td></tr>';
  1711. if (!empty($conf->multicurrency->enabled))
  1712. {
  1713. // Multicurrency Amount HT
  1714. print '<tr><td height="10">' . fieldLabel('MulticurrencyAmountHT','multicurrency_total_ht') . '</td>';
  1715. print '<td class="nowrap" colspan="2">' . price($object->multicurrency_total_ht, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)) . '</td>';
  1716. print '</tr>';
  1717. // Multicurrency Amount VAT
  1718. print '<tr><td height="10">' . fieldLabel('MulticurrencyAmountVAT','multicurrency_total_tva') . '</td>';
  1719. print '<td class="nowrap" colspan="2">' . price($object->multicurrency_total_tva, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)) . '</td>';
  1720. print '</tr>';
  1721. // Multicurrency Amount TTC
  1722. print '<tr><td height="10">' . fieldLabel('MulticurrencyAmountTTC','multicurrency_total_ttc') . '</td>';
  1723. print '<td class="nowrap" colspan="2">' . price($object->multicurrency_total_ttc, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)) . '</td>';
  1724. print '</tr>';
  1725. }
  1726. // Project
  1727. if (! empty($conf->projet->enabled))
  1728. {
  1729. $langs->load('projects');
  1730. print '<tr>';
  1731. print '<td>';
  1732. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1733. print $langs->trans('Project');
  1734. print '</td>';
  1735. if ($action != 'classify')
  1736. {
  1737. print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=classify&amp;id='.$object->id.'">';
  1738. print img_edit($langs->trans('SetProject'),1);
  1739. print '</a></td>';
  1740. }
  1741. print '</tr></table>';
  1742. print '</td><td colspan="3">';
  1743. if ($action == 'classify')
  1744. {
  1745. $form->form_project($_SERVER['PHP_SELF'].'?id='.$object->id, (empty($conf->global->PROJECT_CAN_ALWAYS_LINK_TO_ALL_SUPPLIERS)?$object->socid:-1), $object->fk_project, 'projectid', 0, 0, 1);
  1746. }
  1747. else
  1748. {
  1749. $form->form_project($_SERVER['PHP_SELF'].'?id='.$object->id, $object->socid, $object->fk_project, 'none', 0, 0);
  1750. }
  1751. print '</td>';
  1752. print '</tr>';
  1753. }
  1754. // Incoterms
  1755. if (!empty($conf->incoterm->enabled))
  1756. {
  1757. print '<tr><td>';
  1758. print '<table width="100%" class="nobordernopadding"><tr><td>';
  1759. print $langs->trans('IncotermLabel');
  1760. print '<td><td align="right">';
  1761. if ($user->rights->fournisseur->facture->creer) print '<a href="'.DOL_URL_ROOT.'/fourn/facture/card.php?facid='.$object->id.'&action=editincoterm">'.img_edit().'</a>';
  1762. else print '&nbsp;';
  1763. print '</td></tr></table>';
  1764. print '</td>';
  1765. print '<td colspan="3">';
  1766. if ($action != 'editincoterm')
  1767. {
  1768. print $form->textwithpicto($object->display_incoterms(), $object->libelle_incoterms, 1);
  1769. }
  1770. else
  1771. {
  1772. print $form->select_incoterms((!empty($object->fk_incoterms) ? $object->fk_incoterms : ''), (!empty($object->location_incoterms)?$object->location_incoterms:''), $_SERVER['PHP_SELF'].'?id='.$object->id);
  1773. }
  1774. print '</td></tr>';
  1775. }
  1776. // Other attributes
  1777. $cols = 4;
  1778. include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_view.tpl.php';
  1779. print '</table><br>';
  1780. if (! empty($conf->global->MAIN_DISABLE_CONTACTS_TAB))
  1781. {
  1782. $blocname = 'contacts';
  1783. $title = $langs->trans('ContactsAddresses');
  1784. include DOL_DOCUMENT_ROOT.'/core/tpl/bloc_showhide.tpl.php';
  1785. }
  1786. if (! empty($conf->global->MAIN_DISABLE_NOTES_TAB))
  1787. {
  1788. $colwidth=20;
  1789. $blocname = 'notes';
  1790. $title = $langs->trans('Notes');
  1791. include DOL_DOCUMENT_ROOT.'/core/tpl/bloc_showhide.tpl.php';
  1792. }
  1793. /*
  1794. * Lines
  1795. */
  1796. //$result = $object->getLinesArray();
  1797. print ' <form name="addproduct" id="addproduct" action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.(($action != 'editline')?'#add':'#line_'.GETPOST('lineid')).'" method="POST">
  1798. <input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">
  1799. <input type="hidden" name="action" value="' . (($action != 'editline') ? 'addline' : 'updateline') . '">
  1800. <input type="hidden" name="mode" value="">
  1801. <input type="hidden" name="id" value="'.$object->id.'">
  1802. <input type="hidden" name="socid" value="'.$societe->id.'">
  1803. ';
  1804. if (! empty($conf->use_javascript_ajax) && $object->statut == FactureFournisseur::STATUS_DRAFT) {
  1805. include DOL_DOCUMENT_ROOT . '/core/tpl/ajaxrow.tpl.php';
  1806. }
  1807. print '<table id="tablelines" class="noborder noshadow" width="100%">';
  1808. global $forceall, $senderissupplier, $dateSelector, $inputalsopricewithtax;
  1809. $forceall=1; $senderissupplier=1; $dateSelector=0; $inputalsopricewithtax=1;
  1810. // Show object lines
  1811. if (! empty($object->lines))
  1812. $ret = $object->printObjectLines($action, $societe, $mysoc, $lineid, 1);
  1813. $num=count($object->lines);
  1814. // Form to add new line
  1815. if ($object->statut == FactureFournisseur::STATUS_DRAFT && $user->rights->fournisseur->facture->creer)
  1816. {
  1817. if ($action != 'editline')
  1818. {
  1819. $var = true;
  1820. // Add free products/services
  1821. $object->formAddObjectLine(1, $societe, $mysoc);
  1822. $parameters = array();
  1823. $reshook = $hookmanager->executeHooks('formAddObjectLine', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  1824. }
  1825. }
  1826. print '</table>';
  1827. print '</form>';
  1828. dol_fiche_end();
  1829. if ($action != 'presend')
  1830. {
  1831. /*
  1832. * Boutons actions
  1833. */
  1834. print '<div class="tabsAction">';
  1835. $parameters = array();
  1836. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been
  1837. // modified by hook
  1838. if (empty($reshook))
  1839. {
  1840. // Modify a validated invoice with no payments
  1841. if ($object->statut == FactureFournisseur::STATUS_VALIDATED && $action != 'edit' && $object->getSommePaiement() == 0 && $user->rights->fournisseur->facture->creer)
  1842. {
  1843. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;action=edit">'.$langs->trans('Modify').'</a>';
  1844. }
  1845. // Reopen a standard paid invoice
  1846. if (($object->type == FactureFournisseur::TYPE_STANDARD || $object->type == FactureFournisseur::TYPE_REPLACEMENT) && ($object->statut == 2 || $object->statut == 3)) // A paid invoice (partially or completely)
  1847. {
  1848. if (! $facidnext && $object->close_code != 'replaced') // Not replaced by another invoice
  1849. {
  1850. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;action=reopen">'.$langs->trans('ReOpen').'</a>';
  1851. }
  1852. else
  1853. {
  1854. print '<span class="butActionRefused" title="'.$langs->trans("DisabledBecauseReplacedInvoice").'">'.$langs->trans('ReOpen').'</span>';
  1855. }
  1856. }
  1857. // Send by mail
  1858. if (($object->statut == FactureFournisseur::STATUS_VALIDATED || $object->statut == FactureFournisseur::STATUS_CLOSED))
  1859. {
  1860. if (empty($conf->global->MAIN_USE_ADVANCED_PERMS) || $user->rights->fournisseur->supplier_invoice_advance->send)
  1861. {
  1862. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;action=presend&amp;mode=init">'.$langs->trans('SendByMail').'</a>';
  1863. }
  1864. else print '<a class="butActionRefused" href="#">'.$langs->trans('SendByMail').'</a>';
  1865. }
  1866. // Make payments
  1867. if ($action != 'edit' && $object->statut == FactureFournisseur::STATUS_VALIDATED && $object->paye == 0 && $user->societe_id == 0)
  1868. {
  1869. print '<a class="butAction" href="paiement.php?facid='.$object->id.'&amp;action=create &amp;accountid='.$object->fk_account.'">'.$langs->trans('DoPayment').'</a>'; // must use facid because id is for payment id not invoice
  1870. }
  1871. // Classify paid
  1872. if ($action != 'edit' && $object->statut == FactureFournisseur::STATUS_VALIDATED && $object->paye == 0 && $user->societe_id == 0)
  1873. {
  1874. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=paid"';
  1875. print '>'.$langs->trans('ClassifyPaid').'</a>';
  1876. //print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=paid">'.$langs->trans('ClassifyPaid').'</a>';
  1877. }
  1878. // Validate
  1879. if ($action != 'edit' && $object->statut == FactureFournisseur::STATUS_DRAFT)
  1880. {
  1881. if (count($object->lines))
  1882. {
  1883. if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->fournisseur->facture->creer))
  1884. || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->fournisseur->supplier_invoice_advance->validate)))
  1885. {
  1886. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=valid"';
  1887. print '>'.$langs->trans('Validate').'</a>';
  1888. }
  1889. else
  1890. {
  1891. print '<a class="butActionRefused" href="#" title="'.dol_escape_htmltag($langs->trans("NotAllowed")).'"';
  1892. print '>'.$langs->trans('Validate').'</a>';
  1893. }
  1894. }
  1895. }
  1896. // Create event
  1897. if ($conf->agenda->enabled && ! empty($conf->global->MAIN_ADD_EVENT_ON_ELEMENT_CARD)) // Add hidden condition because this is not a "workflow" action so should appears somewhere else on page.
  1898. {
  1899. print '<div class="inline-block divButAction"><a class="butAction" href="' . DOL_URL_ROOT . '/comm/action/card.php?action=create&amp;origin=' . $object->element . '&amp;originid=' . $object->id . '&amp;socid=' . $object->socid . '">' . $langs->trans("AddAction") . '</a></div>';
  1900. }
  1901. // Clone
  1902. if ($action != 'edit' && $user->rights->fournisseur->facture->creer)
  1903. {
  1904. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=clone&amp;socid='.$object->socid.'">'.$langs->trans('ToClone').'</a>';
  1905. }
  1906. // Delete
  1907. if ($action != 'edit' && $user->rights->fournisseur->facture->supprimer)
  1908. {
  1909. if ($object->getSommePaiement()) {
  1910. print '<div class="inline-block divButAction"><a class="butActionRefused" href="#" title="' . $langs->trans("DisabledBecausePayments") . '">' . $langs->trans('Delete') . '</a></div>';
  1911. } else {
  1912. print '<a class="butActionDelete" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=delete">'.$langs->trans('Delete').'</a>';
  1913. }
  1914. }
  1915. print '</div>';
  1916. print '<br>';
  1917. if ($action != 'edit')
  1918. {
  1919. print '<div class="fichecenter"><div class="fichehalfleft">';
  1920. //print '<table width="100%"><tr><td width="50%" valign="top">';
  1921. //print '<a name="builddoc"></a>'; // ancre
  1922. /*
  1923. * Documents generes
  1924. */
  1925. $ref=dol_sanitizeFileName($object->ref);
  1926. $subdir = get_exdir($object->id,2,0,0,$object,'invoice_supplier').$ref;
  1927. $filedir = $conf->fournisseur->facture->dir_output.'/'.get_exdir($object->id,2,0,0,$object,'invoice_supplier').$ref;
  1928. $urlsource=$_SERVER['PHP_SELF'].'?id='.$object->id;
  1929. $genallowed=$user->rights->fournisseur->facture->creer;
  1930. $delallowed=$user->rights->fournisseur->facture->supprimer;
  1931. $modelpdf=(! empty($object->modelpdf)?$object->modelpdf:(empty($conf->global->INVOICE_SUPPLIER_ADDON_PDF)?'':$conf->global->INVOICE_SUPPLIER_ADDON_PDF));
  1932. print $formfile->showdocuments('facture_fournisseur',$subdir,$filedir,$urlsource,$genallowed,$delallowed,$modelpdf,1,0,0,40,0,'','','',$societe->default_lang);
  1933. $somethingshown=$formfile->numoffiles;
  1934. // Linked object block
  1935. $somethingshown = $form->showLinkedObjectBlock($object);
  1936. // Show links to link elements
  1937. $linktoelem = $form->showLinkToObjectBlock($object,array('supplier_order'));
  1938. if ($linktoelem) print '<br>'.$linktoelem;
  1939. print '</div><div class="fichehalfright"><div class="ficheaddleft">';
  1940. //print '</td><td valign="top" width="50%">';
  1941. //print '<br>';
  1942. // List of actions on element
  1943. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formactions.class.php';
  1944. $formactions=new FormActions($db);
  1945. $somethingshown=$formactions->showactions($object,'invoice_supplier',$socid,0,'listaction'.($genallowed?'largetitle':''));
  1946. print '</div></div></div>';
  1947. //print '</td></tr></table>';
  1948. }
  1949. }
  1950. }
  1951. /*
  1952. * Show mail form
  1953. */
  1954. if (GETPOST('modelselected')) {
  1955. $action = 'presend';
  1956. }
  1957. if ($action == 'presend')
  1958. {
  1959. $ref = dol_sanitizeFileName($object->ref);
  1960. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  1961. $fileparams = dol_most_recent_file($conf->fournisseur->facture->dir_output.'/'.get_exdir($object->id,2,0,0,$object,'invoice_supplier').$ref, preg_quote($ref,'/').'([^\-])+');
  1962. $file=$fileparams['fullname'];
  1963. // Define output language
  1964. $outputlangs = $langs;
  1965. $newlang = '';
  1966. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($_REQUEST['lang_id']))
  1967. $newlang = $_REQUEST['lang_id'];
  1968. if ($conf->global->MAIN_MULTILANGS && empty($newlang))
  1969. $newlang = $object->thirdparty->default_lang;
  1970. if (!empty($newlang))
  1971. {
  1972. $outputlangs = new Translate('', $conf);
  1973. $outputlangs->setDefaultLang($newlang);
  1974. $outputlangs->load('bills');
  1975. }
  1976. // Build document if it not exists
  1977. if (! $file || ! is_readable($file))
  1978. {
  1979. $result = $object->generateDocument(GETPOST('model')?GETPOST('model'):$object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  1980. if ($result <= 0)
  1981. {
  1982. dol_print_error($db,$object->error,$object->errors);
  1983. exit;
  1984. }
  1985. $fileparams = dol_most_recent_file($conf->fournisseur->facture->dir_output.'/'.get_exdir($object->id,2,0,0,$object,'invoice_supplier').$ref, preg_quote($ref,'/').'([^\-])+');
  1986. $file=$fileparams['fullname'];
  1987. }
  1988. print '<div class="clearboth"></div>';
  1989. print '<br>';
  1990. print load_fiche_titre($langs->trans('SendBillByMail'));
  1991. dol_fiche_head('');
  1992. // Cree l'objet formulaire mail
  1993. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
  1994. $formmail = new FormMail($db);
  1995. $formmail->param['langsmodels']=(empty($newlang)?$langs->defaultlang:$newlang);
  1996. $formmail->fromtype = 'user';
  1997. $formmail->fromid = $user->id;
  1998. $formmail->fromname = $user->getFullName($langs);
  1999. $formmail->frommail = $user->email;
  2000. $formmail->trackid='sin'.$object->id;
  2001. if (! empty($conf->global->MAIN_EMAIL_ADD_TRACK_ID) && ($conf->global->MAIN_EMAIL_ADD_TRACK_ID & 2)) // If bit 2 is set
  2002. {
  2003. include DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  2004. $formmail->frommail=dolAddEmailTrackId($formmail->frommail, 'sin'.$object->id);
  2005. }
  2006. $formmail->withfrom=1;
  2007. $liste=array();
  2008. foreach ($object->thirdparty->thirdparty_and_contact_email_array(1) as $key=>$value) $liste[$key]=$value;
  2009. $formmail->withto=GETPOST("sendto")?GETPOST("sendto"):$liste;
  2010. $formmail->withtocc=$liste;
  2011. $formmail->withtoccc=$conf->global->MAIN_EMAIL_USECCC;
  2012. $formmail->withtopic=$outputlangs->trans('SendBillRef','__REF__');
  2013. $formmail->withfile=2;
  2014. $formmail->withbody=1;
  2015. $formmail->withdeliveryreceipt=1;
  2016. $formmail->withcancel=1;
  2017. // Tableau des substitutions
  2018. $formmail->setSubstitFromObject($object);
  2019. $formmail->substit['__SUPPLIERINVREF__']=$object->ref;
  2020. //Find the good contact adress
  2021. $custcontact='';
  2022. $contactarr=array();
  2023. $contactarr=$object->liste_contact(-1,'external');
  2024. if (is_array($contactarr) && count($contactarr)>0) {
  2025. foreach($contactarr as $contact) {
  2026. if ($contact['libelle']==$langs->trans('TypeContact_invoice_supplier_external_BILLING')) {
  2027. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  2028. $contactstatic=new Contact($db);
  2029. $contactstatic->fetch($contact['id']);
  2030. $custcontact=$contactstatic->getFullName($langs,1);
  2031. }
  2032. }
  2033. if (!empty($custcontact)) {
  2034. $formmail->substit['__CONTACTCIVNAME__']=$custcontact;
  2035. }
  2036. }
  2037. // Tableau des parametres complementaires
  2038. $formmail->param['action']='send';
  2039. $formmail->param['models']='invoice_supplier_send';
  2040. $formmail->param['models_id']=GETPOST('modelmailselected','int');
  2041. $formmail->param['facid']=$object->id;
  2042. $formmail->param['returnurl']=$_SERVER["PHP_SELF"].'?id='.$object->id;
  2043. // Init list of files
  2044. if (GETPOST("mode")=='init')
  2045. {
  2046. $formmail->clear_attached_files();
  2047. $formmail->add_attached_files($file,basename($file),dol_mimetype($file));
  2048. }
  2049. // Show form
  2050. print $formmail->get_form();
  2051. dol_fiche_end();
  2052. }
  2053. }
  2054. }
  2055. // End of page
  2056. llxFooter();
  2057. $db->close();