card.php 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751
  1. <?php
  2. /* Copyright (C) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
  5. * Copyright (C) 2005-2015 Regis Houssin <regis.houssin@capnetworks.com>
  6. * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
  7. * Copyright (C) 2010-2013 Juanjo Menent <jmenent@2byte.es>
  8. * Copyright (C) 2011-2016 Philippe Grand <philippe.grand@atoo-net.com>
  9. * Copyright (C) 2012-2013 Christophe Battarel <christophe.battarel@altairis.fr>
  10. * Copyright (C) 2012 Marcos García <marcosgdf@gmail.com>
  11. * Copyright (C) 2012 Cedric Salvador <csalvador@gpcsolutions.fr>
  12. * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
  13. * Copyright (C) 2014 Ferran Marcet <fmarcet@2byte.es>
  14. * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. */
  29. /**
  30. * \file htdocs/commande/card.php
  31. * \ingroup commande
  32. * \brief Page to show customer order
  33. */
  34. require '../main.inc.php';
  35. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formfile.class.php';
  36. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formorder.class.php';
  37. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formmargin.class.php';
  38. require_once DOL_DOCUMENT_ROOT . '/core/modules/commande/modules_commande.php';
  39. require_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
  40. require_once DOL_DOCUMENT_ROOT . '/comm/action/class/actioncomm.class.php';
  41. require_once DOL_DOCUMENT_ROOT . '/core/lib/order.lib.php';
  42. require_once DOL_DOCUMENT_ROOT . '/core/lib/functions2.lib.php';
  43. require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php';
  44. if (! empty($conf->propal->enabled))
  45. require DOL_DOCUMENT_ROOT . '/comm/propal/class/propal.class.php';
  46. if (! empty($conf->projet->enabled)) {
  47. require DOL_DOCUMENT_ROOT . '/projet/class/project.class.php';
  48. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formprojet.class.php';
  49. }
  50. require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
  51. $langs->load('orders');
  52. $langs->load('sendings');
  53. $langs->load('companies');
  54. $langs->load('bills');
  55. $langs->load('propal');
  56. $langs->load('deliveries');
  57. $langs->load('sendings');
  58. $langs->load('products');
  59. if (!empty($conf->incoterm->enabled)) $langs->load('incoterm');
  60. if (! empty($conf->margin->enabled))
  61. $langs->load('margins');
  62. $id = (GETPOST('id', 'int') ? GETPOST('id', 'int') : GETPOST('orderid', 'int'));
  63. $ref = GETPOST('ref', 'alpha');
  64. $socid = GETPOST('socid', 'int');
  65. $action = GETPOST('action', 'alpha');
  66. $confirm = GETPOST('confirm', 'alpha');
  67. $lineid = GETPOST('lineid', 'int');
  68. $origin = GETPOST('origin', 'alpha');
  69. $originid = (GETPOST('originid', 'int') ? GETPOST('originid', 'int') : GETPOST('origin_id', 'int')); // For backward compatibility
  70. // PDF
  71. $hidedetails = (GETPOST('hidedetails', 'int') ? GETPOST('hidedetails', 'int') : (! empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0));
  72. $hidedesc = (GETPOST('hidedesc', 'int') ? GETPOST('hidedesc', 'int') : (! empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0));
  73. $hideref = (GETPOST('hideref', 'int') ? GETPOST('hideref', 'int') : (! empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0));
  74. // Security check
  75. if (! empty($user->societe_id))
  76. $socid = $user->societe_id;
  77. $result = restrictedArea($user, 'commande', $id);
  78. $object = new Commande($db);
  79. $extrafields = new ExtraFields($db);
  80. // fetch optionals attributes and labels
  81. $extralabels = $extrafields->fetch_name_optionals_label($object->table_element);
  82. // Load object
  83. include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once
  84. // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
  85. $hookmanager->initHooks(array('ordercard','globalcard'));
  86. $permissionnote = $user->rights->commande->creer; // Used by the include of actions_setnotes.inc.php
  87. $permissiondellink = $user->rights->commande->creer; // Used by the include of actions_dellink.inc.php
  88. $permissionedit = $user->rights->commande->creer; // Used by the include of actions_lineupdown.inc.php
  89. /*
  90. * Actions
  91. */
  92. $parameters = array('socid' => $socid);
  93. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  94. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  95. if (empty($reshook))
  96. {
  97. if ($cancel) $action='';
  98. include DOL_DOCUMENT_ROOT.'/core/actions_setnotes.inc.php'; // Must be include, not include_once
  99. include DOL_DOCUMENT_ROOT.'/core/actions_dellink.inc.php'; // Must be include, not include_once
  100. include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php'; // Must be include, not include_once
  101. // Action clone object
  102. if ($action == 'confirm_clone' && $confirm == 'yes' && $user->rights->commande->creer)
  103. {
  104. if (1==0 && ! GETPOST('clone_content') && ! GETPOST('clone_receivers'))
  105. {
  106. setEventMessages($langs->trans("NoCloneOptionsSpecified"), null, 'errors');
  107. }
  108. else
  109. {
  110. if ($object->id > 0)
  111. {
  112. // Because createFromClone modifies the object, we must clone it so that we can restore it later
  113. $orig = clone $object;
  114. $result=$object->createFromClone($socid);
  115. if ($result > 0)
  116. {
  117. header("Location: ".$_SERVER['PHP_SELF'].'?id='.$result);
  118. exit;
  119. }
  120. else
  121. {
  122. setEventMessages($object->error, $object->errors, 'errors');
  123. $object = $orig;
  124. $action='';
  125. }
  126. }
  127. }
  128. }
  129. // Reopen a closed order
  130. else if ($action == 'reopen' && $user->rights->commande->creer)
  131. {
  132. if ($object->statut == Commande::STATUS_CANCELED || $object->statut == Commande::STATUS_CLOSED)
  133. {
  134. $result = $object->set_reopen($user);
  135. if ($result > 0)
  136. {
  137. setEventMessages($langs->trans('OrderReopened', $object->ref), null);
  138. }
  139. else
  140. {
  141. setEventMessages($object->error, $object->errors, 'errors');
  142. }
  143. }
  144. }
  145. // Remove order
  146. else if ($action == 'confirm_delete' && $confirm == 'yes' && $user->rights->commande->supprimer)
  147. {
  148. $result = $object->delete($user);
  149. if ($result > 0)
  150. {
  151. header('Location: index.php');
  152. exit;
  153. }
  154. else
  155. {
  156. setEventMessages($object->error, $object->errors, 'errors');
  157. }
  158. }
  159. // Remove a product line
  160. else if ($action == 'confirm_deleteline' && $confirm == 'yes' && $user->rights->commande->creer)
  161. {
  162. $result = $object->deleteline($lineid);
  163. if ($result > 0)
  164. {
  165. // Define output language
  166. $outputlangs = $langs;
  167. $newlang = '';
  168. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id'))
  169. $newlang = GETPOST('lang_id');
  170. if ($conf->global->MAIN_MULTILANGS && empty($newlang))
  171. $newlang = $object->thirdparty->default_lang;
  172. if (! empty($newlang)) {
  173. $outputlangs = new Translate("", $conf);
  174. $outputlangs->setDefaultLang($newlang);
  175. }
  176. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  177. $ret = $object->fetch($object->id); // Reload to get new records
  178. $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  179. }
  180. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id);
  181. exit;
  182. }
  183. else
  184. {
  185. setEventMessages($object->error, $object->errors, 'errors');
  186. }
  187. }
  188. // Categorisation dans projet
  189. else if ($action == 'classin' && $user->rights->commande->creer)
  190. {
  191. $object->setProject(GETPOST('projectid'));
  192. }
  193. // Add order
  194. else if ($action == 'add' && $user->rights->commande->creer)
  195. {
  196. $datecommande = dol_mktime(12, 0, 0, GETPOST('remonth'), GETPOST('reday'), GETPOST('reyear'));
  197. $datelivraison = dol_mktime(12, 0, 0, GETPOST('liv_month'), GETPOST('liv_day'), GETPOST('liv_year'));
  198. if ($datecommande == '') {
  199. setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentities('Date')), null, 'errors');
  200. $action = 'create';
  201. $error++;
  202. }
  203. if ($socid < 1) {
  204. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Customer")), null, 'errors');
  205. $action = 'create';
  206. $error++;
  207. }
  208. if (! $error) {
  209. $object->socid = $socid;
  210. $object->fetch_thirdparty();
  211. $db->begin();
  212. $object->date_commande = $datecommande;
  213. $object->note_private = GETPOST('note_private');
  214. $object->note_public = GETPOST('note_public');
  215. $object->source = GETPOST('source_id');
  216. $object->fk_project = GETPOST('projectid');
  217. $object->ref_client = GETPOST('ref_client');
  218. $object->modelpdf = GETPOST('model');
  219. $object->cond_reglement_id = GETPOST('cond_reglement_id');
  220. $object->mode_reglement_id = GETPOST('mode_reglement_id');
  221. $object->fk_account = GETPOST('fk_account', 'int');
  222. $object->availability_id = GETPOST('availability_id');
  223. $object->demand_reason_id = GETPOST('demand_reason_id');
  224. $object->date_livraison = $datelivraison;
  225. $object->shipping_method_id = GETPOST('shipping_method_id', 'int');
  226. $object->warehouse_id = GETPOST('warehouse_id', 'int');
  227. $object->fk_delivery_address = GETPOST('fk_address');
  228. $object->contactid = GETPOST('contactid');
  229. $object->fk_incoterms = GETPOST('incoterm_id', 'int');
  230. $object->location_incoterms = GETPOST('location_incoterms', 'alpha');
  231. $object->multicurrency_code = GETPOST('multicurrency_code', 'alpha');
  232. $object->multicurrency_tx = GETPOST('originmulticurrency_tx', 'int');
  233. // Fill array 'array_options' with data from add form
  234. if (! $error)
  235. {
  236. $ret = $extrafields->setOptionalsFromPost($extralabels, $object);
  237. if ($ret < 0) $error++;
  238. }
  239. // If creation from another object of another module (Example: origin=propal, originid=1)
  240. if (! empty($origin) && ! empty($originid))
  241. {
  242. // Parse element/subelement (ex: project_task)
  243. $element = $subelement = $origin;
  244. if (preg_match('/^([^_]+)_([^_]+)/i', $origin, $regs)) {
  245. $element = $regs [1];
  246. $subelement = $regs [2];
  247. }
  248. // For compatibility
  249. if ($element == 'order') {
  250. $element = $subelement = 'commande';
  251. }
  252. if ($element == 'propal') {
  253. $element = 'comm/propal';
  254. $subelement = 'propal';
  255. }
  256. if ($element == 'contract') {
  257. $element = $subelement = 'contrat';
  258. }
  259. $object->origin = $origin;
  260. $object->origin_id = $originid;
  261. // Possibility to add external linked objects with hooks
  262. $object->linked_objects [$object->origin] = $object->origin_id;
  263. $other_linked_objects = GETPOST('other_linked_objects', 'array');
  264. if (! empty($other_linked_objects)) {
  265. $object->linked_objects = array_merge($object->linked_objects, $other_linked_objects);
  266. }
  267. if (! $error)
  268. {
  269. $object_id = $object->create($user);
  270. if ($object_id > 0)
  271. {
  272. dol_include_once('/' . $element . '/class/' . $subelement . '.class.php');
  273. $classname = ucfirst($subelement);
  274. $srcobject = new $classname($db);
  275. dol_syslog("Try to find source object origin=" . $object->origin . " originid=" . $object->origin_id . " to add lines");
  276. $result = $srcobject->fetch($object->origin_id);
  277. if ($result > 0)
  278. {
  279. $lines = $srcobject->lines;
  280. if (empty($lines) && method_exists($srcobject, 'fetch_lines'))
  281. {
  282. $srcobject->fetch_lines();
  283. $lines = $srcobject->lines;
  284. }
  285. $fk_parent_line = 0;
  286. $num = count($lines);
  287. for($i = 0; $i < $num; $i ++)
  288. {
  289. $label = (! empty($lines[$i]->label) ? $lines[$i]->label : '');
  290. $desc = (! empty($lines[$i]->desc) ? $lines[$i]->desc : '');
  291. $product_type = (! empty($lines[$i]->product_type) ? $lines[$i]->product_type : 0);
  292. // Dates
  293. // TODO mutualiser
  294. $date_start = $lines[$i]->date_debut_prevue;
  295. if ($lines[$i]->date_debut_reel)
  296. $date_start = $lines[$i]->date_debut_reel;
  297. if ($lines[$i]->date_start)
  298. $date_start = $lines[$i]->date_start;
  299. $date_end = $lines[$i]->date_fin_prevue;
  300. if ($lines[$i]->date_fin_reel)
  301. $date_end = $lines[$i]->date_fin_reel;
  302. if ($lines[$i]->date_end)
  303. $date_end = $lines[$i]->date_end;
  304. // Reset fk_parent_line for no child products and special product
  305. if (($lines[$i]->product_type != 9 && empty($lines[$i]->fk_parent_line)) || $lines[$i]->product_type == 9) {
  306. $fk_parent_line = 0;
  307. }
  308. // Extrafields
  309. if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED) && method_exists($lines[$i], 'fetch_optionals')) // For avoid conflicts if
  310. // trigger used
  311. {
  312. $lines[$i]->fetch_optionals($lines[$i]->rowid);
  313. $array_options = $lines[$i]->array_options;
  314. }
  315. $result = $object->addline($desc, $lines[$i]->subprice, $lines[$i]->qty, $lines[$i]->tva_tx, $lines[$i]->localtax1_tx, $lines[$i]->localtax2_tx, $lines[$i]->fk_product, $lines[$i]->remise_percent, $lines[$i]->info_bits, $lines[$i]->fk_remise_except, 'HT', 0, $date_start, $date_end, $product_type, $lines[$i]->rang, $lines[$i]->special_code, $fk_parent_line, $lines[$i]->fk_fournprice, $lines[$i]->pa_ht, $label, $array_options, $lines[$i]->fk_unit, $object->origin, $lines[$i]->rowid);
  316. if ($result < 0) {
  317. $error++;
  318. break;
  319. }
  320. // Defined the new fk_parent_line
  321. if ($result > 0 && $lines[$i]->product_type == 9) {
  322. $fk_parent_line = $result;
  323. }
  324. }
  325. } else {
  326. setEventMessages($srcobject->error, $srcobject->errors, 'errors');
  327. $error++;
  328. }
  329. // Now we create same links to contact than the ones found on origin object
  330. if (! empty($conf->global->MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN))
  331. {
  332. $originforcontact = $object->origin;
  333. $originidforcontact = $object->origin_id;
  334. if ($originforcontact == 'shipping') // shipment and order share the same contacts. If creating from shipment we take data of order
  335. {
  336. $originforcontact=$srcobject->origin;
  337. $originidforcontact=$srcobject->origin_id;
  338. }
  339. $sqlcontact = "SELECT code, fk_socpeople FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as ctc";
  340. $sqlcontact.= " WHERE element_id = ".$originidforcontact." AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$originforcontact."'";
  341. $resqlcontact = $db->query($sqlcontact);
  342. if ($resqlcontact)
  343. {
  344. while($objcontact = $db->fetch_object($resqlcontact))
  345. {
  346. //print $objcontact->code.'-'.$objcontact->fk_socpeople."\n";
  347. $object->add_contact($objcontact->fk_socpeople, $objcontact->code);
  348. }
  349. }
  350. else dol_print_error($resqlcontact);
  351. }
  352. // Hooks
  353. $parameters = array('objFrom' => $srcobject);
  354. $reshook = $hookmanager->executeHooks('createFrom', $parameters, $object, $action); // Note that $action and $object may have been
  355. // modified by hook
  356. if ($reshook < 0)
  357. $error++;
  358. } else {
  359. setEventMessages($object->error, $object->errors, 'errors');
  360. $error++;
  361. }
  362. } else {
  363. // Required extrafield left blank, error message already defined by setOptionalsFromPost()
  364. $action = 'create';
  365. }
  366. } else {
  367. if (! $error)
  368. {
  369. $object_id = $object->create($user);
  370. // If some invoice's lines already known
  371. $NBLINES = 8;
  372. for($i = 1; $i <= $NBLINES; $i ++) {
  373. if ($_POST['idprod' . $i]) {
  374. $xid = 'idprod' . $i;
  375. $xqty = 'qty' . $i;
  376. $xremise = 'remise_percent' . $i;
  377. $object->add_product($_POST[$xid], $_POST[$xqty], $_POST[$xremise]);
  378. }
  379. }
  380. }
  381. }
  382. // Insert default contacts if defined
  383. if ($object_id > 0)
  384. {
  385. if (GETPOST('contactid'))
  386. {
  387. $result = $object->add_contact(GETPOST('contactid'), 'CUSTOMER', 'external');
  388. if ($result < 0) {
  389. setEventMessages($langs->trans("ErrorFailedToAddContact"), null, 'errors');
  390. $error++;
  391. }
  392. }
  393. $id = $object_id;
  394. $action = '';
  395. }
  396. // End of object creation, we show it
  397. if ($object_id > 0 && ! $error)
  398. {
  399. $db->commit();
  400. header('Location: ' . $_SERVER["PHP_SELF"] . '?id=' . $object_id);
  401. exit();
  402. } else {
  403. $db->rollback();
  404. $action = 'create';
  405. setEventMessages($object->error, $object->errors, 'errors');
  406. }
  407. }
  408. }
  409. else if ($action == 'classifybilled' && $user->rights->commande->creer)
  410. {
  411. $ret=$object->classifyBilled($user);
  412. if ($ret < 0) {
  413. setEventMessages($object->error, $object->errors, 'errors');
  414. }
  415. }
  416. else if ($action == 'classifyunbilled' && $user->rights->commande->creer)
  417. {
  418. $ret=$object->classifyUnBilled();
  419. if ($ret < 0) {
  420. setEventMessages($object->error, $object->errors, 'errors');
  421. }
  422. }
  423. // Positionne ref commande client
  424. else if ($action == 'set_ref_client' && $user->rights->commande->creer) {
  425. $object->set_ref_client($user, GETPOST('ref_client'));
  426. }
  427. else if ($action == 'setremise' && $user->rights->commande->creer) {
  428. $object->set_remise($user, GETPOST('remise'));
  429. }
  430. else if ($action == 'setabsolutediscount' && $user->rights->commande->creer) {
  431. if (GETPOST('remise_id')) {
  432. if ($object->id > 0) {
  433. $object->insert_discount(GETPOST('remise_id'));
  434. } else {
  435. dol_print_error($db, $object->error);
  436. }
  437. }
  438. }
  439. else if ($action == 'setdate' && $user->rights->commande->creer) {
  440. // print "x ".$_POST['liv_month'].", ".$_POST['liv_day'].", ".$_POST['liv_year'];
  441. $date = dol_mktime(0, 0, 0, GETPOST('order_month'), GETPOST('order_day'), GETPOST('order_year'));
  442. $result = $object->set_date($user, $date);
  443. if ($result < 0) {
  444. setEventMessages($object->error, $object->errors, 'errors');
  445. }
  446. }
  447. else if ($action == 'setdate_livraison' && $user->rights->commande->creer) {
  448. // print "x ".$_POST['liv_month'].", ".$_POST['liv_day'].", ".$_POST['liv_year'];
  449. $datelivraison = dol_mktime(0, 0, 0, GETPOST('liv_month'), GETPOST('liv_day'), GETPOST('liv_year'));
  450. $result = $object->set_date_livraison($user, $datelivraison);
  451. if ($result < 0) {
  452. setEventMessages($object->error, $object->errors, 'errors');
  453. }
  454. }
  455. else if ($action == 'setmode' && $user->rights->commande->creer) {
  456. $result = $object->setPaymentMethods(GETPOST('mode_reglement_id', 'int'));
  457. if ($result < 0)
  458. setEventMessages($object->error, $object->errors, 'errors');
  459. }
  460. // Multicurrency Code
  461. else if ($action == 'setmulticurrencycode' && $user->rights->commande->creer) {
  462. $result = $object->setMulticurrencyCode(GETPOST('multicurrency_code', 'alpha'));
  463. }
  464. // Multicurrency rate
  465. else if ($action == 'setmulticurrencyrate' && $user->rights->commande->creer) {
  466. $result = $object->setMulticurrencyRate(GETPOST('multicurrency_tx', 'int'));
  467. }
  468. else if ($action == 'setavailability' && $user->rights->commande->creer) {
  469. $result = $object->availability(GETPOST('availability_id'));
  470. if ($result < 0)
  471. setEventMessages($object->error, $object->errors, 'errors');
  472. }
  473. else if ($action == 'setdemandreason' && $user->rights->commande->creer) {
  474. $result = $object->demand_reason(GETPOST('demand_reason_id'));
  475. if ($result < 0)
  476. setEventMessages($object->error, $object->errors, 'errors');
  477. }
  478. else if ($action == 'setconditions' && $user->rights->commande->creer) {
  479. $result = $object->setPaymentTerms(GETPOST('cond_reglement_id', 'int'));
  480. if ($result < 0) {
  481. dol_print_error($db, $object->error);
  482. } else {
  483. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  484. // Define output language
  485. $outputlangs = $langs;
  486. $newlang = GETPOST('lang_id', 'alpha');
  487. if ($conf->global->MAIN_MULTILANGS && empty($newlang))
  488. $newlang = $object->thirdparty->default_lang;
  489. if (! empty($newlang)) {
  490. $outputlangs = new Translate("", $conf);
  491. $outputlangs->setDefaultLang($newlang);
  492. }
  493. $ret = $object->fetch($object->id); // Reload to get new records
  494. $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  495. }
  496. }
  497. }
  498. // Set incoterm
  499. elseif ($action == 'set_incoterms' && !empty($conf->incoterm->enabled))
  500. {
  501. $result = $object->setIncoterms(GETPOST('incoterm_id', 'int'), GETPOST('location_incoterms', 'alpha'));
  502. if ($result < 0) {
  503. setEventMessages($object->error, $object->errors, 'errors');
  504. }
  505. }
  506. // bank account
  507. else if ($action == 'setbankaccount' && $user->rights->commande->creer) {
  508. $result=$object->setBankAccount(GETPOST('fk_account', 'int'));
  509. if ($result < 0) {
  510. setEventMessages($object->error, $object->errors, 'errors');
  511. }
  512. }
  513. // shipping method
  514. else if ($action == 'setshippingmethod' && $user->rights->commande->creer) {
  515. $result = $object->setShippingMethod(GETPOST('shipping_method_id', 'int'));
  516. if ($result < 0) {
  517. setEventMessages($object->error, $object->errors, 'errors');
  518. }
  519. }
  520. // warehouse
  521. else if ($action == 'setwarehouse' && $user->rights->commande->creer) {
  522. $result = $object->setWarehouse(GETPOST('warehouse_id', 'int'));
  523. if ($result < 0) {
  524. setEventMessages($object->error, $object->errors, 'errors');
  525. }
  526. }
  527. else if ($action == 'setremisepercent' && $user->rights->commande->creer) {
  528. $result = $object->set_remise($user, GETPOST('remise_percent'));
  529. }
  530. else if ($action == 'setremiseabsolue' && $user->rights->commande->creer) {
  531. $result = $object->set_remise_absolue($user, GETPOST('remise_absolue'));
  532. }
  533. // Add a new line
  534. else if ($action == 'addline' && $user->rights->commande->creer)
  535. {
  536. $langs->load('errors');
  537. $error = 0;
  538. // Set if we used free entry or predefined product
  539. $predef='';
  540. $product_desc=(GETPOST('dp_desc')?GETPOST('dp_desc'):'');
  541. $price_ht = GETPOST('price_ht');
  542. if (GETPOST('prod_entry_mode') == 'free')
  543. {
  544. $idprod=0;
  545. $tva_tx = (GETPOST('tva_tx') ? GETPOST('tva_tx') : 0);
  546. }
  547. else
  548. {
  549. $idprod=GETPOST('idprod', 'int');
  550. $tva_tx = '';
  551. }
  552. $qty = GETPOST('qty' . $predef);
  553. $remise_percent = GETPOST('remise_percent' . $predef);
  554. // Extrafields
  555. $extrafieldsline = new ExtraFields($db);
  556. $extralabelsline = $extrafieldsline->fetch_name_optionals_label($object->table_element_line);
  557. $array_options = $extrafieldsline->getOptionalsFromPost($extralabelsline, $predef);
  558. // Unset extrafield
  559. if (is_array($extralabelsline)) {
  560. // Get extra fields
  561. foreach ($extralabelsline as $key => $value) {
  562. unset($_POST["options_" . $key]);
  563. }
  564. }
  565. if (empty($idprod) && ($price_ht < 0) && ($qty < 0)) {
  566. setEventMessages($langs->trans('ErrorBothFieldCantBeNegative', $langs->transnoentitiesnoconv('UnitPriceHT'), $langs->transnoentitiesnoconv('Qty')), null, 'errors');
  567. $error++;
  568. }
  569. if (GETPOST('prod_entry_mode') == 'free' && empty($idprod) && GETPOST('type') < 0) {
  570. setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Type')), null, 'errors');
  571. $error++;
  572. }
  573. if (GETPOST('prod_entry_mode') == 'free' && empty($idprod) && (! ($price_ht >= 0) || $price_ht == '')) // Unit price can be 0 but not ''
  574. {
  575. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("UnitPriceHT")), null, 'errors');
  576. $error++;
  577. }
  578. if ($qty == '') {
  579. setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Qty')), null, 'errors');
  580. $error++;
  581. }
  582. if (GETPOST('prod_entry_mode') == 'free' && empty($idprod) && empty($product_desc)) {
  583. setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Description')), null, 'errors');
  584. $error++;
  585. }
  586. if (! $error && ($qty >= 0) && (! empty($product_desc) || ! empty($idprod))) {
  587. // Clean parameters
  588. $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'));
  589. $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'));
  590. $price_base_type = (GETPOST('price_base_type', 'alpha')?GETPOST('price_base_type', 'alpha'):'HT');
  591. // Ecrase $pu par celui du produit
  592. // Ecrase $desc par celui du produit
  593. // Ecrase $txtva par celui du produit
  594. // Ecrase $base_price_type par celui du produit
  595. if (! empty($idprod)) {
  596. $prod = new Product($db);
  597. $prod->fetch($idprod);
  598. $label = ((GETPOST('product_label') && GETPOST('product_label') != $prod->label) ? GETPOST('product_label') : '');
  599. // Update if prices fields are defined
  600. $tva_tx = get_default_tva($mysoc, $object->thirdparty, $prod->id);
  601. $tva_npr = get_default_npr($mysoc, $object->thirdparty, $prod->id);
  602. if (empty($tva_tx)) $tva_npr=0;
  603. $pu_ht = $prod->price;
  604. $pu_ttc = $prod->price_ttc;
  605. $price_min = $prod->price_min;
  606. $price_base_type = $prod->price_base_type;
  607. // multiprix
  608. if (! empty($conf->global->PRODUIT_MULTIPRICES) && ! empty($object->thirdparty->price_level))
  609. {
  610. $pu_ht = $prod->multiprices[$object->thirdparty->price_level];
  611. $pu_ttc = $prod->multiprices_ttc[$object->thirdparty->price_level];
  612. $price_min = $prod->multiprices_min[$object->thirdparty->price_level];
  613. $price_base_type = $prod->multiprices_base_type[$object->thirdparty->price_level];
  614. if (! empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) // using this option is a bug. kept for backward compatibility
  615. {
  616. if (isset($prod->multiprices_tva_tx[$object->thirdparty->price_level])) $tva_tx=$prod->multiprices_tva_tx[$object->thirdparty->price_level];
  617. if (isset($prod->multiprices_recuperableonly[$object->thirdparty->price_level])) $tva_npr=$prod->multiprices_recuperableonly[$object->thirdparty->price_level];
  618. }
  619. }
  620. elseif (! empty($conf->global->PRODUIT_CUSTOMER_PRICES))
  621. {
  622. require_once DOL_DOCUMENT_ROOT . '/product/class/productcustomerprice.class.php';
  623. $prodcustprice = new Productcustomerprice($db);
  624. $filter = array('t.fk_product' => $prod->id,'t.fk_soc' => $object->thirdparty->id);
  625. $result = $prodcustprice->fetch_all('', '', 0, 0, $filter);
  626. if ($result >= 0)
  627. {
  628. if (count($prodcustprice->lines) > 0)
  629. {
  630. $pu_ht = price($prodcustprice->lines [0]->price);
  631. $pu_ttc = price($prodcustprice->lines [0]->price_ttc);
  632. $price_base_type = $prodcustprice->lines [0]->price_base_type;
  633. $prod->tva_tx = $prodcustprice->lines [0]->tva_tx;
  634. }
  635. }
  636. else
  637. {
  638. setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors');
  639. }
  640. }
  641. // if price ht is forced (ie: calculated by margin rate and cost price)
  642. if (! empty($price_ht)) {
  643. $pu_ht = price2num($price_ht, 'MU');
  644. $pu_ttc = price2num($pu_ht * (1 + ($tva_tx / 100)), 'MU');
  645. }
  646. // On reevalue prix selon taux tva car taux tva transaction peut etre different
  647. // de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur).
  648. elseif ($tva_tx != $prod->tva_tx) {
  649. if ($price_base_type != 'HT') {
  650. $pu_ht = price2num($pu_ttc / (1 + ($tva_tx / 100)), 'MU');
  651. } else {
  652. $pu_ttc = price2num($pu_ht * (1 + ($tva_tx / 100)), 'MU');
  653. }
  654. }
  655. $desc = '';
  656. // Define output language
  657. if (! empty($conf->global->MAIN_MULTILANGS) && ! empty($conf->global->PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE)) {
  658. $outputlangs = $langs;
  659. $newlang = '';
  660. if (empty($newlang) && GETPOST('lang_id'))
  661. $newlang = GETPOST('lang_id');
  662. if (empty($newlang))
  663. $newlang = $object->thirdparty->default_lang;
  664. if (! empty($newlang)) {
  665. $outputlangs = new Translate("", $conf);
  666. $outputlangs->setDefaultLang($newlang);
  667. }
  668. $desc = (! empty($prod->multilangs [$outputlangs->defaultlang] ["description"])) ? $prod->multilangs [$outputlangs->defaultlang] ["description"] : $prod->description;
  669. } else {
  670. $desc = $prod->description;
  671. }
  672. $desc = dol_concatdesc($desc, $product_desc);
  673. // Add custom code and origin country into description
  674. if (empty($conf->global->MAIN_PRODUCT_DISABLE_CUSTOMCOUNTRYCODE) && (! empty($prod->customcode) || ! empty($prod->country_code))) {
  675. $tmptxt = '(';
  676. if (! empty($prod->customcode))
  677. $tmptxt .= $langs->transnoentitiesnoconv("CustomCode") . ': ' . $prod->customcode;
  678. if (! empty($prod->customcode) && ! empty($prod->country_code))
  679. $tmptxt .= ' - ';
  680. if (! empty($prod->country_code))
  681. $tmptxt .= $langs->transnoentitiesnoconv("CountryOrigin") . ': ' . getCountry($prod->country_code, 0, $db, $langs, 0);
  682. $tmptxt .= ')';
  683. $desc = dol_concatdesc($desc, $tmptxt);
  684. }
  685. $type = $prod->type;
  686. $fk_unit = $prod->fk_unit;
  687. } else {
  688. $pu_ht = price2num($price_ht, 'MU');
  689. $pu_ttc = price2num(GETPOST('price_ttc'), 'MU');
  690. $tva_npr = (preg_match('/\*/', $tva_tx) ? 1 : 0);
  691. $tva_tx = str_replace('*', '', $tva_tx);
  692. $label = (GETPOST('product_label') ? GETPOST('product_label') : '');
  693. $desc = $product_desc;
  694. $type = GETPOST('type');
  695. $fk_unit=GETPOST('units', 'alpha');
  696. }
  697. // Margin
  698. $fournprice = price2num(GETPOST('fournprice' . $predef) ? GETPOST('fournprice' . $predef) : '');
  699. $buyingprice = price2num(GETPOST('buying_price' . $predef) != '' ? GETPOST('buying_price' . $predef) : ''); // If buying_price is '0', we muste keep this value
  700. // Local Taxes
  701. $localtax1_tx = get_localtax($tva_tx, 1, $object->thirdparty);
  702. $localtax2_tx = get_localtax($tva_tx, 2, $object->thirdparty);
  703. $desc = dol_htmlcleanlastbr($desc);
  704. $info_bits = 0;
  705. if ($tva_npr)
  706. $info_bits |= 0x01;
  707. if (! empty($price_min) && (price2num($pu_ht) * (1 - price2num($remise_percent) / 100) < price2num($price_min))) {
  708. $mesg = $langs->trans("CantBeLessThanMinPrice", price(price2num($price_min, 'MU'), 0, $langs, 0, 0, - 1, $conf->currency));
  709. setEventMessages($mesg, null, 'errors');
  710. } else {
  711. // Insert line
  712. $result = $object->addline($desc, $pu_ht, $qty, $tva_tx, $localtax1_tx, $localtax2_tx, $idprod, $remise_percent, $info_bits, 0, $price_base_type, $pu_ttc, $date_start, $date_end, $type, - 1, 0, GETPOST('fk_parent_line'), $fournprice, $buyingprice, $label, $array_options, $fk_unit);
  713. if ($result > 0) {
  714. $ret = $object->fetch($object->id); // Reload to get new records
  715. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  716. // Define output language
  717. $outputlangs = $langs;
  718. $newlang = GETPOST('lang_id', 'alpha');
  719. if (! empty($conf->global->MAIN_MULTILANGS) && empty($newlang))
  720. $newlang = $object->thirdparty->default_lang;
  721. if (! empty($newlang)) {
  722. $outputlangs = new Translate("", $conf);
  723. $outputlangs->setDefaultLang($newlang);
  724. }
  725. $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  726. }
  727. unset($_POST['prod_entry_mode']);
  728. unset($_POST['qty']);
  729. unset($_POST['type']);
  730. unset($_POST['remise_percent']);
  731. unset($_POST['price_ht']);
  732. unset($_POST['multicurrency_price_ht']);
  733. unset($_POST['price_ttc']);
  734. unset($_POST['tva_tx']);
  735. unset($_POST['product_ref']);
  736. unset($_POST['product_label']);
  737. unset($_POST['product_desc']);
  738. unset($_POST['fournprice']);
  739. unset($_POST['buying_price']);
  740. unset($_POST['np_marginRate']);
  741. unset($_POST['np_markRate']);
  742. unset($_POST['dp_desc']);
  743. unset($_POST['idprod']);
  744. unset($_POST['units']);
  745. unset($_POST['date_starthour']);
  746. unset($_POST['date_startmin']);
  747. unset($_POST['date_startsec']);
  748. unset($_POST['date_startday']);
  749. unset($_POST['date_startmonth']);
  750. unset($_POST['date_startyear']);
  751. unset($_POST['date_endhour']);
  752. unset($_POST['date_endmin']);
  753. unset($_POST['date_endsec']);
  754. unset($_POST['date_endday']);
  755. unset($_POST['date_endmonth']);
  756. unset($_POST['date_endyear']);
  757. } else {
  758. setEventMessages($object->error, $object->errors, 'errors');
  759. }
  760. }
  761. }
  762. }
  763. /*
  764. * Update a line
  765. */
  766. else if ($action == 'updateline' && $user->rights->commande->creer && GETPOST('save'))
  767. {
  768. // Clean parameters
  769. $date_start='';
  770. $date_end='';
  771. $date_start=dol_mktime(GETPOST('date_starthour'), GETPOST('date_startmin'), GETPOST('date_startsec'), GETPOST('date_startmonth'), GETPOST('date_startday'), GETPOST('date_startyear'));
  772. $date_end=dol_mktime(GETPOST('date_endhour'), GETPOST('date_endmin'), GETPOST('date_endsec'), GETPOST('date_endmonth'), GETPOST('date_endday'), GETPOST('date_endyear'));
  773. $description=dol_htmlcleanlastbr(GETPOST('product_desc'));
  774. $pu_ht=GETPOST('price_ht');
  775. $vat_rate=(GETPOST('tva_tx')?GETPOST('tva_tx'):0);
  776. // Define info_bits
  777. $info_bits = 0;
  778. if (preg_match('/\*/', $vat_rate))
  779. $info_bits |= 0x01;
  780. // Define vat_rate
  781. $vat_rate = str_replace('*', '', $vat_rate);
  782. $localtax1_rate = get_localtax($vat_rate, 1, $object->thirdparty, $mysoc);
  783. $localtax2_rate = get_localtax($vat_rate, 2, $object->thirdparty, $mysoc);
  784. // Add buying price
  785. $fournprice = price2num(GETPOST('fournprice') ? GETPOST('fournprice') : '');
  786. $buyingprice = price2num(GETPOST('buying_price') != '' ? GETPOST('buying_price') : ''); // If buying_price is '0', we muste keep this value
  787. // Extrafields Lines
  788. $extrafieldsline = new ExtraFields($db);
  789. $extralabelsline = $extrafieldsline->fetch_name_optionals_label($object->table_element_line);
  790. $array_options = $extrafieldsline->getOptionalsFromPost($extralabelsline);
  791. // Unset extrafield POST Data
  792. if (is_array($extralabelsline)) {
  793. foreach ($extralabelsline as $key => $value) {
  794. unset($_POST["options_" . $key]);
  795. }
  796. }
  797. // Define special_code for special lines
  798. $special_code=GETPOST('special_code');
  799. if (! GETPOST('qty')) $special_code=3;
  800. // Check minimum price
  801. $productid = GETPOST('productid', 'int');
  802. if (! empty($productid)) {
  803. $product = new Product($db);
  804. $product->fetch($productid);
  805. $type = $product->type;
  806. $price_min = $product->price_min;
  807. if (! empty($conf->global->PRODUIT_MULTIPRICES) && ! empty($object->thirdparty->price_level))
  808. $price_min = $product->multiprices_min [$object->thirdparty->price_level];
  809. $label = ((GETPOST('update_label') && GETPOST('product_label')) ? GETPOST('product_label') : '');
  810. if ($price_min && (price2num($pu_ht) * (1 - price2num(GETPOST('remise_percent')) / 100) < price2num($price_min))) {
  811. setEventMessages($langs->trans("CantBeLessThanMinPrice", price(price2num($price_min, 'MU'), 0, $langs, 0, 0, - 1, $conf->currency)), null, 'errors');
  812. $error++;
  813. }
  814. } else {
  815. $type = GETPOST('type');
  816. $label = (GETPOST('product_label') ? GETPOST('product_label') : '');
  817. // Check parameters
  818. if (GETPOST('type') < 0) {
  819. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type")), null, 'errors');
  820. $error++;
  821. }
  822. }
  823. if (! $error) {
  824. $result = $object->updateline(GETPOST('lineid'), $description, $pu_ht, GETPOST('qty'), GETPOST('remise_percent'), $vat_rate, $localtax1_rate, $localtax2_rate, 'HT', $info_bits, $date_start, $date_end, $type, GETPOST('fk_parent_line'), 0, $fournprice, $buyingprice, $label, $special_code, $array_options, GETPOST('units'));
  825. if ($result >= 0) {
  826. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  827. // Define output language
  828. $outputlangs = $langs;
  829. $newlang = '';
  830. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id'))
  831. $newlang = GETPOST('lang_id');
  832. if ($conf->global->MAIN_MULTILANGS && empty($newlang))
  833. $newlang = $object->thirdparty->default_lang;
  834. if (! empty($newlang)) {
  835. $outputlangs = new Translate("", $conf);
  836. $outputlangs->setDefaultLang($newlang);
  837. }
  838. $ret = $object->fetch($object->id); // Reload to get new records
  839. $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  840. }
  841. unset($_POST['qty']);
  842. unset($_POST['type']);
  843. unset($_POST['productid']);
  844. unset($_POST['remise_percent']);
  845. unset($_POST['price_ht']);
  846. unset($_POST['multicurrency_price_ht']);
  847. unset($_POST['price_ttc']);
  848. unset($_POST['tva_tx']);
  849. unset($_POST['product_ref']);
  850. unset($_POST['product_label']);
  851. unset($_POST['product_desc']);
  852. unset($_POST['fournprice']);
  853. unset($_POST['buying_price']);
  854. unset($_POST['date_starthour']);
  855. unset($_POST['date_startmin']);
  856. unset($_POST['date_startsec']);
  857. unset($_POST['date_startday']);
  858. unset($_POST['date_startmonth']);
  859. unset($_POST['date_startyear']);
  860. unset($_POST['date_endhour']);
  861. unset($_POST['date_endmin']);
  862. unset($_POST['date_endsec']);
  863. unset($_POST['date_endday']);
  864. unset($_POST['date_endmonth']);
  865. unset($_POST['date_endyear']);
  866. } else {
  867. setEventMessages($object->error, $object->errors, 'errors');
  868. }
  869. }
  870. }
  871. else if ($action == 'updateline' && $user->rights->commande->creer && GETPOST('cancel') == $langs->trans('Cancel')) {
  872. header('Location: ' . $_SERVER['PHP_SELF'] . '?id=' . $object->id); // Pour reaffichage de la fiche en cours d'edition
  873. exit();
  874. }
  875. else if ($action == 'confirm_validate' && $confirm == 'yes' &&
  876. ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->commande->creer))
  877. || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->commande->order_advance->validate)))
  878. )
  879. {
  880. $idwarehouse = GETPOST('idwarehouse');
  881. $qualified_for_stock_change=0;
  882. if (empty($conf->global->STOCK_SUPPORTS_SERVICES))
  883. {
  884. $qualified_for_stock_change=$object->hasProductsOrServices(2);
  885. }
  886. else
  887. {
  888. $qualified_for_stock_change=$object->hasProductsOrServices(1);
  889. }
  890. // Check parameters
  891. if (! empty($conf->stock->enabled) && ! empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $qualified_for_stock_change)
  892. {
  893. if (! $idwarehouse || $idwarehouse == -1)
  894. {
  895. $error++;
  896. setEventMessages($langs->trans('ErrorFieldRequired',$langs->transnoentitiesnoconv("Warehouse")), null, 'errors');
  897. $action='';
  898. }
  899. }
  900. if (! $error) {
  901. $result = $object->valid($user, $idwarehouse);
  902. if ($result >= 0)
  903. {
  904. // Define output language
  905. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  906. {
  907. $outputlangs = $langs;
  908. $newlang = '';
  909. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
  910. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
  911. if (! empty($newlang)) {
  912. $outputlangs = new Translate("", $conf);
  913. $outputlangs->setDefaultLang($newlang);
  914. }
  915. $model=$object->modelpdf;
  916. $ret = $object->fetch($id); // Reload to get new records
  917. $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  918. }
  919. }
  920. else
  921. {
  922. setEventMessages($object->error, $object->errors, 'errors');
  923. }
  924. }
  925. }
  926. // Go back to draft status
  927. else if ($action == 'confirm_modif' && $user->rights->commande->creer) {
  928. $idwarehouse = GETPOST('idwarehouse');
  929. $qualified_for_stock_change=0;
  930. if (empty($conf->global->STOCK_SUPPORTS_SERVICES))
  931. {
  932. $qualified_for_stock_change=$object->hasProductsOrServices(2);
  933. }
  934. else
  935. {
  936. $qualified_for_stock_change=$object->hasProductsOrServices(1);
  937. }
  938. // Check parameters
  939. if (! empty($conf->stock->enabled) && ! empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $qualified_for_stock_change)
  940. {
  941. if (! $idwarehouse || $idwarehouse == -1)
  942. {
  943. $error++;
  944. setEventMessages($langs->trans('ErrorFieldRequired',$langs->transnoentitiesnoconv("Warehouse")), null, 'errors');
  945. $action='';
  946. }
  947. }
  948. if (! $error) {
  949. $result = $object->set_draft($user, $idwarehouse);
  950. if ($result >= 0)
  951. {
  952. // Define output language
  953. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  954. {
  955. $outputlangs = $langs;
  956. $newlang = '';
  957. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
  958. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
  959. if (! empty($newlang)) {
  960. $outputlangs = new Translate("", $conf);
  961. $outputlangs->setDefaultLang($newlang);
  962. }
  963. $model=$object->modelpdf;
  964. $ret = $object->fetch($id); // Reload to get new records
  965. $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  966. }
  967. }
  968. }
  969. }
  970. else if ($action == 'confirm_shipped' && $confirm == 'yes' && $user->rights->commande->cloturer) {
  971. $result = $object->cloture($user);
  972. if ($result < 0) {
  973. setEventMessages($object->error, $object->errors, 'errors');
  974. }
  975. }
  976. else if ($action == 'confirm_cancel' && $confirm == 'yes' &&
  977. ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->commande->creer))
  978. || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->commande->order_advance->validate)))
  979. )
  980. {
  981. $idwarehouse = GETPOST('idwarehouse');
  982. $qualified_for_stock_change=0;
  983. if (empty($conf->global->STOCK_SUPPORTS_SERVICES))
  984. {
  985. $qualified_for_stock_change=$object->hasProductsOrServices(2);
  986. }
  987. else
  988. {
  989. $qualified_for_stock_change=$object->hasProductsOrServices(1);
  990. }
  991. // Check parameters
  992. if (! empty($conf->stock->enabled) && ! empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $qualified_for_stock_change)
  993. {
  994. if (! $idwarehouse || $idwarehouse == -1)
  995. {
  996. $error++;
  997. setEventMessages($langs->trans('ErrorFieldRequired',$langs->transnoentitiesnoconv("Warehouse")), null, 'errors');
  998. $action='';
  999. }
  1000. }
  1001. if (! $error) {
  1002. $result = $object->cancel($idwarehouse);
  1003. if ($result < 0) {
  1004. setEventMessages($object->error, $object->errors, 'errors');
  1005. }
  1006. }
  1007. }
  1008. if ($action == 'builddoc') // In get or post
  1009. {
  1010. // Save last template used to generate document
  1011. if (GETPOST('model'))
  1012. $object->setDocModel($user, GETPOST('model', 'alpha'));
  1013. if (GETPOST('fk_bank')) { // this field may come from an external module
  1014. $object->fk_bank = GETPOST('fk_bank');
  1015. } else {
  1016. $object->fk_bank = $object->fk_account;
  1017. }
  1018. // Define output language
  1019. $outputlangs = $langs;
  1020. $newlang = '';
  1021. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($_REQUEST['lang_id']))
  1022. $newlang = $_REQUEST['lang_id'];
  1023. if ($conf->global->MAIN_MULTILANGS && empty($newlang))
  1024. $newlang = $object->thirdparty->default_lang;
  1025. if (! empty($newlang)) {
  1026. $outputlangs = new Translate("", $conf);
  1027. $outputlangs->setDefaultLang($newlang);
  1028. }
  1029. $result = $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  1030. if ($result <= 0)
  1031. {
  1032. setEventMessages($object->error, $object->errors, 'errors');
  1033. $action='';
  1034. }
  1035. }
  1036. // Remove file in doc form
  1037. if ($action == 'remove_file')
  1038. {
  1039. if ($object->id > 0)
  1040. {
  1041. require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
  1042. $langs->load("other");
  1043. $upload_dir = $conf->commande->dir_output;
  1044. $file = $upload_dir . '/' . GETPOST('file');
  1045. $ret = dol_delete_file($file, 0, 0, 0, $object);
  1046. if ($ret)
  1047. setEventMessages($langs->trans("FileWasRemoved", GETPOST('file')), null, 'mesgs');
  1048. else
  1049. setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('file')), null, 'errors');
  1050. $action = '';
  1051. }
  1052. }
  1053. if ($action == 'update_extras')
  1054. {
  1055. // Fill array 'array_options' with data from update form
  1056. $extralabels = $extrafields->fetch_name_optionals_label($object->table_element);
  1057. $ret = $extrafields->setOptionalsFromPost($extralabels, $object, GETPOST('attribute'));
  1058. if ($ret < 0) $error++;
  1059. if (! $error)
  1060. {
  1061. // Actions on extra fields (by external module or standard code)
  1062. // TODO le hook fait double emploi avec le trigger !!
  1063. $hookmanager->initHooks(array('orderdao'));
  1064. $parameters = array('id' => $object->id);
  1065. $reshook = $hookmanager->executeHooks('insertExtraFields', $parameters, $object, $action); // Note that $action and $object may have been modified by
  1066. // some hooks
  1067. if (empty($reshook)) {
  1068. $result = $object->insertExtraFields();
  1069. if ($result < 0) {
  1070. $error++;
  1071. }
  1072. } else if ($reshook < 0)
  1073. $error++;
  1074. }
  1075. if ($error)
  1076. $action = 'edit_extras';
  1077. }
  1078. if ($action == 'set_thirdparty' && $user->rights->commande->creer)
  1079. {
  1080. $object->fetch($id);
  1081. $object->setValueFrom('fk_soc', $socid);
  1082. header('Location: ' . $_SERVER["PHP_SELF"] . '?id=' . $id);
  1083. exit();
  1084. }
  1085. include DOL_DOCUMENT_ROOT.'/core/actions_printing.inc.php';
  1086. /*
  1087. * Send mail
  1088. */
  1089. // Actions to send emails
  1090. $actiontypecode='AC_COM';
  1091. $trigger_name='ORDER_SENTBYMAIL';
  1092. $paramname='id';
  1093. $mode='emailfromorder';
  1094. include DOL_DOCUMENT_ROOT.'/core/actions_sendmails.inc.php';
  1095. if (! $error && ! empty($conf->global->MAIN_DISABLE_CONTACTS_TAB) && $user->rights->commande->creer)
  1096. {
  1097. if ($action == 'addcontact')
  1098. {
  1099. if ($object->id > 0) {
  1100. $contactid = (GETPOST('userid') ? GETPOST('userid') : GETPOST('contactid'));
  1101. $result = $object->add_contact($contactid, GETPOST('type'), GETPOST('source'));
  1102. }
  1103. if ($result >= 0) {
  1104. header("Location: " . $_SERVER['PHP_SELF'] . "?id=" . $object->id);
  1105. exit();
  1106. } else {
  1107. if ($object->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  1108. $langs->load("errors");
  1109. setEventMessages($langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType"), null, 'errors');
  1110. } else {
  1111. setEventMessages($object->error, $object->errors, 'errors');
  1112. }
  1113. }
  1114. }
  1115. // bascule du statut d'un contact
  1116. else if ($action == 'swapstatut')
  1117. {
  1118. if ($object->id > 0) {
  1119. $result = $object->swapContactStatus(GETPOST('ligne'));
  1120. } else {
  1121. dol_print_error($db);
  1122. }
  1123. }
  1124. // Efface un contact
  1125. else if ($action == 'deletecontact')
  1126. {
  1127. $result = $object->delete_contact($lineid);
  1128. if ($result >= 0) {
  1129. header("Location: " . $_SERVER['PHP_SELF'] . "?id=" . $object->id);
  1130. exit();
  1131. } else {
  1132. dol_print_error($db);
  1133. }
  1134. }
  1135. }
  1136. }
  1137. /*
  1138. * View
  1139. */
  1140. llxHeader('', $langs->trans('Order'), 'EN:Customers_Orders|FR:Commandes_Clients|ES:Pedidos de clientes');
  1141. $form = new Form($db);
  1142. $formfile = new FormFile($db);
  1143. $formorder = new FormOrder($db);
  1144. $formmargin = new FormMargin($db);
  1145. if (! empty($conf->projet->enabled)) { $formproject = new FormProjets($db); }
  1146. /**
  1147. * *******************************************************************
  1148. *
  1149. * Mode creation
  1150. *
  1151. * *******************************************************************
  1152. */
  1153. if ($action == 'create' && $user->rights->commande->creer)
  1154. {
  1155. print load_fiche_titre($langs->trans('CreateOrder'),'','title_commercial.png');
  1156. $soc = new Societe($db);
  1157. if ($socid > 0)
  1158. $res = $soc->fetch($socid);
  1159. $projectid = 0;
  1160. $remise_absolue = 0;
  1161. $currency_code = $conf->currency;
  1162. if (! empty($origin) && ! empty($originid)) {
  1163. // Parse element/subelement (ex: project_task)
  1164. $element = $subelement = $origin;
  1165. if (preg_match('/^([^_]+)_([^_]+)/i', $origin, $regs)) {
  1166. $element = $regs [1];
  1167. $subelement = $regs [2];
  1168. }
  1169. if ($element == 'project') {
  1170. $projectid = $originid;
  1171. if (!$cond_reglement_id) {
  1172. $cond_reglement_id = $soc->cond_reglement_id;
  1173. }
  1174. if (!$mode_reglement_id) {
  1175. $mode_reglement_id = $soc->mode_reglement_id;
  1176. }
  1177. if (!$remise_percent) {
  1178. $remise_percent = $soc->remise_percent;
  1179. }
  1180. if (!$dateorder) {
  1181. // Do not set 0 here (0 for a date is 1970)
  1182. $dateorder = (empty($dateinvoice)?(empty($conf->global->MAIN_AUTOFILL_DATE_ODER)?-1:''):$dateorder);
  1183. }
  1184. } else {
  1185. // For compatibility
  1186. if ($element == 'order' || $element == 'commande') {
  1187. $element = $subelement = 'commande';
  1188. }
  1189. if ($element == 'propal') {
  1190. $element = 'comm/propal';
  1191. $subelement = 'propal';
  1192. }
  1193. if ($element == 'contract') {
  1194. $element = $subelement = 'contrat';
  1195. }
  1196. dol_include_once('/' . $element . '/class/' . $subelement . '.class.php');
  1197. $classname = ucfirst($subelement);
  1198. $objectsrc = new $classname($db);
  1199. $objectsrc->fetch($originid);
  1200. if (empty($objectsrc->lines) && method_exists($objectsrc, 'fetch_lines'))
  1201. $objectsrc->fetch_lines();
  1202. $objectsrc->fetch_thirdparty();
  1203. // Replicate extrafields
  1204. $objectsrc->fetch_optionals($originid);
  1205. $object->array_options = $objectsrc->array_options;
  1206. $projectid = (! empty($objectsrc->fk_project) ? $objectsrc->fk_project : '');
  1207. $ref_client = (! empty($objectsrc->ref_client) ? $objectsrc->ref_client : '');
  1208. $soc = $objectsrc->thirdparty;
  1209. $cond_reglement_id = (!empty($objectsrc->cond_reglement_id)?$objectsrc->cond_reglement_id:(!empty($soc->cond_reglement_id)?$soc->cond_reglement_id:1));
  1210. $mode_reglement_id = (!empty($objectsrc->mode_reglement_id)?$objectsrc->mode_reglement_id:(!empty($soc->mode_reglement_id)?$soc->mode_reglement_id:0));
  1211. $fk_account = (! empty($objectsrc->fk_account)?$objectsrc->fk_account:(! empty($soc->fk_account)?$soc->fk_account:0));
  1212. $availability_id = (!empty($objectsrc->availability_id)?$objectsrc->availability_id:(!empty($soc->availability_id)?$soc->availability_id:0));
  1213. $shipping_method_id = (! empty($objectsrc->shipping_method_id)?$objectsrc->shipping_method_id:(! empty($soc->shipping_method_id)?$soc->shipping_method_id:0));
  1214. $warehouse_id = (! empty($objectsrc->warehouse_id)?$objectsrc->warehouse_id:(! empty($soc->warehouse_id)?$soc->warehouse_id:0));
  1215. $demand_reason_id = (!empty($objectsrc->demand_reason_id)?$objectsrc->demand_reason_id:(!empty($soc->demand_reason_id)?$soc->demand_reason_id:0));
  1216. $remise_percent = (!empty($objectsrc->remise_percent)?$objectsrc->remise_percent:(!empty($soc->remise_percent)?$soc->remise_percent:0));
  1217. $remise_absolue = (!empty($objectsrc->remise_absolue)?$objectsrc->remise_absolue:(!empty($soc->remise_absolue)?$soc->remise_absolue:0));
  1218. $dateorder = empty($conf->global->MAIN_AUTOFILL_DATE_ORDER)?-1:'';
  1219. $datedelivery = (! empty($objectsrc->date_livraison) ? $objectsrc->date_livraison : '');
  1220. if (!empty($conf->multicurrency->enabled))
  1221. {
  1222. if (!empty($objectsrc->multicurrency_code)) $currency_code = $objectsrc->multicurrency_code;
  1223. if (!empty($conf->global->MULTICURRENCY_USE_ORIGIN_TX) && !empty($objectsrc->multicurrency_tx)) $currency_tx = $objectsrc->multicurrency_tx;
  1224. }
  1225. $note_private = $object->getDefaultCreateValueFor('note_private', (! empty($objectsrc->note_private) ? $objectsrc->note_private : null));
  1226. $note_public = $object->getDefaultCreateValueFor('note_public', (! empty($objectsrc->note_public) ? $objectsrc->note_public : null));
  1227. // Object source contacts list
  1228. $srccontactslist = $objectsrc->liste_contact(- 1, 'external', 1);
  1229. }
  1230. }
  1231. else
  1232. {
  1233. $cond_reglement_id = $soc->cond_reglement_id;
  1234. $mode_reglement_id = $soc->mode_reglement_id;
  1235. $fk_account = $soc->fk_account;
  1236. $availability_id = $soc->availability_id;
  1237. $shipping_method_id = $soc->shipping_method_id;
  1238. $warehouse_id = $soc->warehouse_id;
  1239. $demand_reason_id = $soc->demand_reason_id;
  1240. $remise_percent = $soc->remise_percent;
  1241. $remise_absolue = 0;
  1242. $dateorder = empty($conf->global->MAIN_AUTOFILL_DATE_ORDER)?-1:'';
  1243. $projectid = 0;
  1244. if (!empty($conf->multicurrency->enabled) && !empty($soc->multicurrency_code)) $currency_code = $soc->multicurrency_code;
  1245. $note_private = $object->getDefaultCreateValueFor('note_private');
  1246. $note_public = $object->getDefaultCreateValueFor('note_public');
  1247. }
  1248. $absolute_discount=$soc->getAvailableDiscounts();
  1249. $nbrow = 10;
  1250. print '<form name="crea_commande" action="' . $_SERVER["PHP_SELF"] . '" method="POST">';
  1251. print '<input type="hidden" name="token" value="' . $_SESSION ['newtoken'] . '">';
  1252. print '<input type="hidden" name="action" value="add">';
  1253. print '<input type="hidden" name="socid" value="' . $soc->id . '">' . "\n";
  1254. print '<input type="hidden" name="remise_percent" value="' . $soc->remise_percent . '">';
  1255. print '<input type="hidden" name="origin" value="' . $origin . '">';
  1256. print '<input type="hidden" name="originid" value="' . $originid . '">';
  1257. if (!empty($currency_tx)) print '<input type="hidden" name="originmulticurrency_tx" value="' . $currency_tx . '">';
  1258. dol_fiche_head('');
  1259. print '<table class="border" width="100%">';
  1260. // Reference
  1261. print '<tr><td class="titlefieldcreate fieldrequired">' . $langs->trans('Ref') . '</td><td colspan="2">' . $langs->trans("Draft") . '</td></tr>';
  1262. // Reference client
  1263. print '<tr><td>' . $langs->trans('RefCustomer') . '</td><td colspan="2">';
  1264. if (!empty($conf->global->MAIN_USE_PROPAL_REFCLIENT_FOR_ORDER) && ! empty($origin) && ! empty($originid))
  1265. print '<input type="text" name="ref_client" value="'.$ref_client.'"></td>';
  1266. else
  1267. print '<input type="text" name="ref_client" value="'.GETPOST('ref_client').'"></td>';
  1268. print '</tr>';
  1269. // Client
  1270. print '<tr>';
  1271. print '<td class="fieldrequired">' . $langs->trans('Customer') . '</td>';
  1272. if ($socid > 0) {
  1273. print '<td colspan="2">';
  1274. print $soc->getNomUrl(1);
  1275. print '<input type="hidden" name="socid" value="' . $soc->id . '">';
  1276. print '</td>';
  1277. } else {
  1278. print '<td colspan="2">';
  1279. print $form->select_company('', 'socid', 's.client = 1 OR s.client = 3', 'SelectThirdParty');
  1280. // reload page to retrieve customer informations
  1281. if (!empty($conf->global->RELOAD_PAGE_ON_CUSTOMER_CHANGE))
  1282. {
  1283. print '<script type="text/javascript">
  1284. $(document).ready(function() {
  1285. $("#socid").change(function() {
  1286. var socid = $(this).val();
  1287. // reload page
  1288. window.location.href = "'.$_SERVER["PHP_SELF"].'?action=create&socid="+socid+"&ref_client="+$("input[name=ref_client]").val();
  1289. });
  1290. });
  1291. </script>';
  1292. }
  1293. print '</td>';
  1294. }
  1295. print '</tr>' . "\n";
  1296. /*
  1297. * Contact de la commande
  1298. */
  1299. if ($socid > 0) {
  1300. print "<tr><td>" . $langs->trans("DefaultContact") . '</td><td colspan="2">';
  1301. $form->select_contacts($soc->id, $setcontact, 'contactid', 1, $srccontactslist);
  1302. print '</td></tr>';
  1303. // Ligne info remises tiers
  1304. print '<tr><td>' . $langs->trans('Discounts') . '</td><td colspan="2">';
  1305. if ($soc->remise_percent)
  1306. print $langs->trans("CompanyHasRelativeDiscount", $soc->remise_percent);
  1307. else
  1308. print $langs->trans("CompanyHasNoRelativeDiscount");
  1309. print '. ';
  1310. $absolute_discount = $soc->getAvailableDiscounts();
  1311. if ($absolute_discount)
  1312. print $langs->trans("CompanyHasAbsoluteDiscount", price($absolute_discount), $langs->trans("Currency" . $conf->currency));
  1313. else
  1314. print $langs->trans("CompanyHasNoAbsoluteDiscount");
  1315. print '.';
  1316. print '</td></tr>';
  1317. }
  1318. // Date
  1319. print '<tr><td class="fieldrequired">' . $langs->trans('Date') . '</td><td colspan="2">';
  1320. $form->select_date('', 're', '', '', '', "crea_commande", 1, 1); // Always autofill date with current date
  1321. print '</td></tr>';
  1322. // Delivery date planed
  1323. print "<tr><td>".$langs->trans("DateDeliveryPlanned").'</td><td colspan="2">';
  1324. if (empty($datedelivery))
  1325. {
  1326. if (! empty($conf->global->DATE_LIVRAISON_WEEK_DELAY)) $datedelivery = time() + ((7*$conf->global->DATE_LIVRAISON_WEEK_DELAY) * 24 * 60 * 60);
  1327. else $datedelivery=empty($conf->global->MAIN_AUTOFILL_DATE_DELIVERY)?-1:'';
  1328. }
  1329. $form->select_date($datedelivery, 'liv_', '', '', '', "crea_commande", 1, 1);
  1330. print "</td></tr>";
  1331. // Conditions de reglement
  1332. print '<tr><td class="nowrap">' . $langs->trans('PaymentConditionsShort') . '</td><td colspan="2">';
  1333. $form->select_conditions_paiements($cond_reglement_id, 'cond_reglement_id', - 1, 1);
  1334. print '</td></tr>';
  1335. // Mode de reglement
  1336. print '<tr><td>' . $langs->trans('PaymentMode') . '</td><td colspan="2">';
  1337. $form->select_types_paiements($mode_reglement_id, 'mode_reglement_id');
  1338. print '</td></tr>';
  1339. // Bank Account
  1340. if (! empty($conf->global->BANK_ASK_PAYMENT_BANK_DURING_ORDER) && ! empty($conf->banque->enabled))
  1341. {
  1342. print '<tr><td>' . $langs->trans('BankAccount') . '</td><td colspan="2">';
  1343. $form->select_comptes($fk_account, 'fk_account', 0, '', 1);
  1344. print '</td></tr>';
  1345. }
  1346. // Delivery delay
  1347. print '<tr class="fielddeliverydelay"><td>' . $langs->trans('AvailabilityPeriod') . '</td><td colspan="2">';
  1348. $form->selectAvailabilityDelay($availability_id, 'availability_id', '', 1);
  1349. print '</td></tr>';
  1350. // Shipping Method
  1351. if (! empty($conf->expedition->enabled)) {
  1352. print '<tr><td>' . $langs->trans('SendingMethod') . '</td><td colspan="2">';
  1353. print $form->selectShippingMethod($shipping_method_id, 'shipping_method_id', '', 1);
  1354. print '</td></tr>';
  1355. }
  1356. // Warehouse
  1357. if (! empty($conf->expedition->enabled) && ! empty($conf->global->WAREHOUSE_ASK_WAREHOUSE_DURING_ORDER)) {
  1358. require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
  1359. $formproduct=new FormProduct($db);
  1360. print '<tr><td>' . $langs->trans('Warehouse') . '</td><td colspan="2">';
  1361. print $formproduct->selectWarehouses($warehouse_id, 'warehouse_id', '', 1);
  1362. print '</td></tr>';
  1363. }
  1364. // What trigger creation
  1365. print '<tr><td>' . $langs->trans('Source') . '</td><td colspan="2">';
  1366. $form->selectInputReason($demand_reason_id, 'demand_reason_id', '', 1);
  1367. print '</td></tr>';
  1368. // TODO How record was recorded OrderMode (llx_c_input_method)
  1369. // Project
  1370. if (! empty($conf->projet->enabled) && $socid > 0)
  1371. {
  1372. $projectid = GETPOST('projectid')?GETPOST('projectid'):0;
  1373. if ($origin == 'project') $projectid = ($originid ? $originid : 0);
  1374. $langs->load("projects");
  1375. print '<tr>';
  1376. print '<td>' . $langs->trans("Project") . '</td><td colspan="2">';
  1377. $numprojet = $formproject->select_projects($soc->id, $projectid, 'projectid', 0);
  1378. print ' &nbsp; <a href="'.DOL_URL_ROOT.'/projet/card.php?socid=' . $soc->id . '&action=create&status=1&backtopage='.urlencode($_SERVER["PHP_SELF"].'?action=create&socid='.$soc->id).'">' . $langs->trans("AddProject") . '</a>';
  1379. print '</td>';
  1380. print '</tr>';
  1381. }
  1382. // Incoterms
  1383. if (!empty($conf->incoterm->enabled))
  1384. {
  1385. print '<tr>';
  1386. print '<td><label for="incoterm_id">'.$form->textwithpicto($langs->trans("IncotermLabel"), $objectsrc->libelle_incoterms, 1).'</label></td>';
  1387. print '<td colspan="3" class="maxwidthonsmartphone">';
  1388. print $form->select_incoterms((!empty($objectsrc->fk_incoterms) ? $objectsrc->fk_incoterms : ''), (!empty($objectsrc->location_incoterms)?$objectsrc->location_incoterms:''));
  1389. print '</td></tr>';
  1390. }
  1391. // Other attributes
  1392. $parameters = array('objectsrc' => $objectsrc, 'colspan' => ' colspan="3"', 'socid'=>$socid);
  1393. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by
  1394. // hook
  1395. if (empty($reshook) && ! empty($extrafields->attribute_label)) {
  1396. print $object->showOptionals($extrafields, 'edit');
  1397. }
  1398. // Template to use by default
  1399. print '<tr><td>' . $langs->trans('Model') . '</td>';
  1400. print '<td colspan="2">';
  1401. include_once DOL_DOCUMENT_ROOT . '/core/modules/commande/modules_commande.php';
  1402. $liste = ModelePDFCommandes::liste_modeles($db);
  1403. print $form->selectarray('model', $liste, $conf->global->COMMANDE_ADDON_PDF);
  1404. print "</td></tr>";
  1405. // Multicurrency
  1406. if (! empty($conf->multicurrency->enabled))
  1407. {
  1408. print '<tr>';
  1409. print '<td>'.fieldLabel('Currency','multicurrency_code').'</td>';
  1410. print '<td colspan="3" class="maxwidthonsmartphone">';
  1411. print $form->selectMultiCurrency($currency_code, 'multicurrency_code');
  1412. print '</td></tr>';
  1413. }
  1414. // Note public
  1415. print '<tr>';
  1416. print '<td class="border" valign="top">' . $langs->trans('NotePublic') . '</td>';
  1417. print '<td valign="top" colspan="2">';
  1418. $doleditor = new DolEditor('note_public', $note_public, '', 80, 'dolibarr_notes', 'In', 0, false, true, ROWS_3, '90%');
  1419. print $doleditor->Create(1);
  1420. // print '<textarea name="note_public" wrap="soft" cols="70" rows="'.ROWS_3.'">'.$note_public.'</textarea>';
  1421. print '</td></tr>';
  1422. // Note private
  1423. if (empty($user->societe_id)) {
  1424. print '<tr>';
  1425. print '<td class="border" valign="top">' . $langs->trans('NotePrivate') . '</td>';
  1426. print '<td valign="top" colspan="2">';
  1427. $doleditor = new DolEditor('note_private', $note_private, '', 80, 'dolibarr_notes', 'In', 0, false, true, ROWS_3, '90%');
  1428. print $doleditor->Create(1);
  1429. // print '<textarea name="note" wrap="soft" cols="70" rows="'.ROWS_3.'">'.$note_private.'</textarea>';
  1430. print '</td></tr>';
  1431. }
  1432. if (! empty($origin) && ! empty($originid) && is_object($objectsrc))
  1433. {
  1434. // TODO for compatibility
  1435. if ($origin == 'contrat') {
  1436. // Calcul contrat->price (HT), contrat->total (TTC), contrat->tva
  1437. $objectsrc->remise_absolue = $remise_absolue;
  1438. $objectsrc->remise_percent = $remise_percent;
  1439. $objectsrc->update_price(1);
  1440. }
  1441. print "\n<!-- " . $classname . " info -->";
  1442. print "\n";
  1443. print '<input type="hidden" name="amount" value="' . $objectsrc->total_ht . '">' . "\n";
  1444. print '<input type="hidden" name="total" value="' . $objectsrc->total_ttc . '">' . "\n";
  1445. print '<input type="hidden" name="tva" value="' . $objectsrc->total_tva . '">' . "\n";
  1446. print '<input type="hidden" name="origin" value="' . $objectsrc->element . '">';
  1447. print '<input type="hidden" name="originid" value="' . $objectsrc->id . '">';
  1448. switch ($classname) {
  1449. case 'Propal':
  1450. $newclassname = 'CommercialProposal';
  1451. break;
  1452. case 'Commande':
  1453. $newclassname = 'Order';
  1454. break;
  1455. case 'Expedition':
  1456. $newclassname = 'Sending';
  1457. break;
  1458. case 'Contrat':
  1459. $newclassname = 'Contract';
  1460. break;
  1461. default:
  1462. $newclassname = $classname;
  1463. }
  1464. print '<tr><td>' . $langs->trans($newclassname) . '</td><td colspan="2">' . $objectsrc->getNomUrl(1) . '</td></tr>';
  1465. print '<tr><td>' . $langs->trans('TotalHT') . '</td><td colspan="2">' . price($objectsrc->total_ht) . '</td></tr>';
  1466. print '<tr><td>' . $langs->trans('TotalVAT') . '</td><td colspan="2">' . price($objectsrc->total_tva) . "</td></tr>";
  1467. if ($mysoc->localtax1_assuj == "1" || $objectsrc->total_localtax1 != 0) // Localtax1 RE
  1468. {
  1469. print '<tr><td>' . $langs->transcountry("AmountLT1", $mysoc->country_code) . '</td><td colspan="2">' . price($objectsrc->total_localtax1) . "</td></tr>";
  1470. }
  1471. if ($mysoc->localtax2_assuj == "1" || $objectsrc->total_localtax2 != 0) // Localtax2 IRPF
  1472. {
  1473. print '<tr><td>' . $langs->transcountry("AmountLT2", $mysoc->country_code) . '</td><td colspan="2">' . price($objectsrc->total_localtax2) . "</td></tr>";
  1474. }
  1475. print '<tr><td>' . $langs->trans('TotalTTC') . '</td><td colspan="2">' . price($objectsrc->total_ttc) . "</td></tr>";
  1476. if (!empty($conf->multicurrency->enabled))
  1477. {
  1478. print '<tr><td>' . $langs->trans('MulticurrencyTotalHT') . '</td><td colspan="2">' . price($objectsrc->multicurrency_total_ht) . '</td></tr>';
  1479. print '<tr><td>' . $langs->trans('MulticurrencyTotalVAT') . '</td><td colspan="2">' . price($objectsrc->multicurrency_total_tva) . "</td></tr>";
  1480. print '<tr><td>' . $langs->trans('MulticurrencyTotalTTC') . '</td><td colspan="2">' . price($objectsrc->multicurrency_total_ttc) . "</td></tr>";
  1481. }
  1482. }
  1483. else
  1484. {
  1485. if (! empty($conf->global->PRODUCT_SHOW_WHEN_CREATE))
  1486. {
  1487. /*
  1488. * Services/produits predefinis
  1489. */
  1490. $NBLINES = 8;
  1491. print '<tr><td colspan="3">';
  1492. print '<table class="noborder">';
  1493. print '<tr><td>' . $langs->trans('ProductsAndServices') . '</td>';
  1494. print '<td>' . $langs->trans('Qty') . '</td>';
  1495. print '<td>' . $langs->trans('ReductionShort') . '</td>';
  1496. print '</tr>';
  1497. for($i = 1; $i <= $NBLINES; $i ++) {
  1498. print '<tr><td>';
  1499. // multiprix
  1500. if (! empty($conf->global->PRODUIT_MULTIPRICES))
  1501. print $form->select_produits('', 'idprod' . $i, '', $conf->product->limit_size, $soc->price_level);
  1502. else
  1503. print $form->select_produits('', 'idprod' . $i, '', $conf->product->limit_size);
  1504. print '</td>';
  1505. print '<td><input type="text" size="3" name="qty' . $i . '" value="1"></td>';
  1506. print '<td><input type="text" size="3" name="remise_percent' . $i . '" value="' . $soc->remise_percent . '">%</td></tr>';
  1507. }
  1508. print '</table>';
  1509. print '</td></tr>';
  1510. }
  1511. }
  1512. print '</table>';
  1513. dol_fiche_end();
  1514. // Button "Create Draft"
  1515. print '<div class="center">';
  1516. print '<input type="submit" class="button" name="bouton" value="' . $langs->trans('CreateDraft') . '">';
  1517. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  1518. print '<input type="button" class="button" value="' . $langs->trans("Cancel") . '" onClick="javascript:history.go(-1)">';
  1519. print '</div>';
  1520. print '</form>';
  1521. // Show origin lines
  1522. if (! empty($origin) && ! empty($originid) && is_object($objectsrc)) {
  1523. $title = $langs->trans('ProductsAndServices');
  1524. print load_fiche_titre($title);
  1525. print '<table class="noborder" width="100%">';
  1526. $objectsrc->printOriginLinesList();
  1527. print '</table>';
  1528. }
  1529. } else {
  1530. /* *************************************************************************** */
  1531. /* */
  1532. /* Mode vue et edition */
  1533. /* */
  1534. /* *************************************************************************** */
  1535. $now = dol_now();
  1536. if ($object->id > 0) {
  1537. $product_static = new Product($db);
  1538. $soc = new Societe($db);
  1539. $soc->fetch($object->socid);
  1540. $author = new User($db);
  1541. $author->fetch($object->user_author_id);
  1542. $res = $object->fetch_optionals($object->id, $extralabels);
  1543. $head = commande_prepare_head($object);
  1544. dol_fiche_head($head, 'order', $langs->trans("CustomerOrder"), 0, 'order');
  1545. $formconfirm = '';
  1546. /*
  1547. * Confirmation de la suppression de la commande
  1548. */
  1549. if ($action == 'delete') {
  1550. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('DeleteOrder'), $langs->trans('ConfirmDeleteOrder'), 'confirm_delete', '', 0, 1);
  1551. }
  1552. /*
  1553. * Confirmation de la validation
  1554. */
  1555. if ($action == 'validate')
  1556. {
  1557. // on verifie si l'objet est en numerotation provisoire
  1558. $ref = substr($object->ref, 1, 4);
  1559. if ($ref == 'PROV') {
  1560. $numref = $object->getNextNumRef($soc);
  1561. } else {
  1562. $numref = $object->ref;
  1563. }
  1564. $text = $langs->trans('ConfirmValidateOrder', $numref);
  1565. if (! empty($conf->notification->enabled))
  1566. {
  1567. require_once DOL_DOCUMENT_ROOT . '/core/class/notify.class.php';
  1568. $notify = new Notify($db);
  1569. $text .= '<br>';
  1570. $text .= $notify->confirmMessage('ORDER_VALIDATE', $object->socid, $object);
  1571. }
  1572. $qualified_for_stock_change=0;
  1573. if (empty($conf->global->STOCK_SUPPORTS_SERVICES))
  1574. {
  1575. $qualified_for_stock_change=$object->hasProductsOrServices(2);
  1576. }
  1577. else
  1578. {
  1579. $qualified_for_stock_change=$object->hasProductsOrServices(1);
  1580. }
  1581. $formquestion=array();
  1582. if (! empty($conf->stock->enabled) && ! empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $qualified_for_stock_change)
  1583. {
  1584. $langs->load("stocks");
  1585. require_once DOL_DOCUMENT_ROOT . '/product/class/html.formproduct.class.php';
  1586. $formproduct = new FormProduct($db);
  1587. $formquestion = array(
  1588. // 'text' => $langs->trans("ConfirmClone"),
  1589. // array('type' => 'checkbox', 'name' => 'clone_content', 'label' => $langs->trans("CloneMainAttributes"), 'value'
  1590. // => 1),
  1591. // array('type' => 'checkbox', 'name' => 'update_prices', 'label' => $langs->trans("PuttingPricesUpToDate"),
  1592. // 'value' => 1),
  1593. array('type' => 'other','name' => 'idwarehouse','label' => $langs->trans("SelectWarehouseForStockDecrease"),'value' => $formproduct->selectWarehouses(GETPOST('idwarehouse')?GETPOST('idwarehouse'):'ifone', 'idwarehouse', '', 1)));
  1594. }
  1595. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('ValidateOrder'), $text, 'confirm_validate', $formquestion, 0, 1, 220);
  1596. }
  1597. // Confirm back to draft status
  1598. if ($action == 'modif')
  1599. {
  1600. $qualified_for_stock_change=0;
  1601. if (empty($conf->global->STOCK_SUPPORTS_SERVICES))
  1602. {
  1603. $qualified_for_stock_change=$object->hasProductsOrServices(2);
  1604. }
  1605. else
  1606. {
  1607. $qualified_for_stock_change=$object->hasProductsOrServices(1);
  1608. }
  1609. $text=$langs->trans('ConfirmUnvalidateOrder',$object->ref);
  1610. $formquestion=array();
  1611. if (! empty($conf->stock->enabled) && ! empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $qualified_for_stock_change)
  1612. {
  1613. $langs->load("stocks");
  1614. require_once DOL_DOCUMENT_ROOT . '/product/class/html.formproduct.class.php';
  1615. $formproduct = new FormProduct($db);
  1616. $formquestion = array(
  1617. // 'text' => $langs->trans("ConfirmClone"),
  1618. // array('type' => 'checkbox', 'name' => 'clone_content', 'label' => $langs->trans("CloneMainAttributes"), 'value'
  1619. // => 1),
  1620. // array('type' => 'checkbox', 'name' => 'update_prices', 'label' => $langs->trans("PuttingPricesUpToDate"),
  1621. // 'value' => 1),
  1622. array('type' => 'other','name' => 'idwarehouse','label' => $langs->trans("SelectWarehouseForStockIncrease"),'value' => $formproduct->selectWarehouses(GETPOST('idwarehouse')?GETPOST('idwarehouse'):'ifone', 'idwarehouse', '', 1)));
  1623. }
  1624. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('UnvalidateOrder'), $text, 'confirm_modif', $formquestion, "yes", 1, 220);
  1625. }
  1626. /*
  1627. * Confirmation de la cloture
  1628. */
  1629. if ($action == 'shipped') {
  1630. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('CloseOrder'), $langs->trans('ConfirmCloseOrder'), 'confirm_shipped', '', 0, 1);
  1631. }
  1632. /*
  1633. * Confirmation de l'annulation
  1634. */
  1635. if ($action == 'cancel')
  1636. {
  1637. $qualified_for_stock_change=0;
  1638. if (empty($conf->global->STOCK_SUPPORTS_SERVICES))
  1639. {
  1640. $qualified_for_stock_change=$object->hasProductsOrServices(2);
  1641. }
  1642. else
  1643. {
  1644. $qualified_for_stock_change=$object->hasProductsOrServices(1);
  1645. }
  1646. $text=$langs->trans('ConfirmCancelOrder',$object->ref);
  1647. $formquestion=array();
  1648. if (! empty($conf->stock->enabled) && ! empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $qualified_for_stock_change)
  1649. {
  1650. $langs->load("stocks");
  1651. require_once DOL_DOCUMENT_ROOT . '/product/class/html.formproduct.class.php';
  1652. $formproduct = new FormProduct($db);
  1653. $formquestion = array(
  1654. // 'text' => $langs->trans("ConfirmClone"),
  1655. // array('type' => 'checkbox', 'name' => 'clone_content', 'label' => $langs->trans("CloneMainAttributes"), 'value'
  1656. // => 1),
  1657. // array('type' => 'checkbox', 'name' => 'update_prices', 'label' => $langs->trans("PuttingPricesUpToDate"),
  1658. // 'value' => 1),
  1659. array('type' => 'other','name' => 'idwarehouse','label' => $langs->trans("SelectWarehouseForStockIncrease"),'value' => $formproduct->selectWarehouses(GETPOST('idwarehouse')?GETPOST('idwarehouse'):'ifone', 'idwarehouse', '', 1)));
  1660. }
  1661. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('Cancel'), $text, 'confirm_cancel', $formquestion, 0, 1);
  1662. }
  1663. // Confirmation to delete line
  1664. if ($action == 'ask_deleteline')
  1665. {
  1666. $formconfirm=$form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&lineid='.$lineid, $langs->trans('DeleteProductLine'), $langs->trans('ConfirmDeleteProductLine'), 'confirm_deleteline', '', 0, 1);
  1667. }
  1668. // Clone confirmation
  1669. if ($action == 'clone') {
  1670. // Create an array for form
  1671. $formquestion = array(
  1672. // 'text' => $langs->trans("ConfirmClone"),
  1673. // array('type' => 'checkbox', 'name' => 'clone_content', 'label' => $langs->trans("CloneMainAttributes"), 'value' =>
  1674. // 1),
  1675. // array('type' => 'checkbox', 'name' => 'update_prices', 'label' => $langs->trans("PuttingPricesUpToDate"), 'value'
  1676. // => 1),
  1677. array('type' => 'other','name' => 'socid','label' => $langs->trans("SelectThirdParty"),'value' => $form->select_company(GETPOST('socid', 'int'), 'socid', '(s.client=1 OR s.client=3)')));
  1678. // Paiement incomplet. On demande si motif = escompte ou autre
  1679. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('CloneOrder'), $langs->trans('ConfirmCloneOrder', $object->ref), 'confirm_clone', $formquestion, 'yes', 1);
  1680. }
  1681. if (! $formconfirm) {
  1682. $parameters = array('lineid' => $lineid);
  1683. $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  1684. if (empty($reshook)) $formconfirm.=$hookmanager->resPrint;
  1685. elseif ($reshook > 0) $formconfirm=$hookmanager->resPrint;
  1686. }
  1687. // Print form confirm
  1688. print $formconfirm;
  1689. /*
  1690. * Commande
  1691. */
  1692. $nbrow = 9;
  1693. if (! empty($conf->projet->enabled))
  1694. $nbrow ++;
  1695. // Local taxes
  1696. if ($mysoc->localtax1_assuj == "1" || $object->total_localtax1 != 0)
  1697. $nbrow++;
  1698. if ($mysoc->localtax2_assuj == "1" || $object->total_localtax2 != 0 )
  1699. $nbrow++;
  1700. print '<table class="border" width="100%">';
  1701. $linkback = '<a href="' . DOL_URL_ROOT . '/commande/list.php' . (! empty($socid) ? '?socid=' . $socid : '') . '">' . $langs->trans("BackToList") . '</a>';
  1702. // Ref
  1703. print '<tr><td class="titlefield">' . $langs->trans('Ref') . '</td>';
  1704. print '<td colspan="3">';
  1705. print $form->showrefnav($object, 'ref', $linkback, 1, 'ref', 'ref');
  1706. print '</td>';
  1707. print '</tr>';
  1708. // Ref commande client
  1709. print '<tr><td>';
  1710. print '<table class="nobordernopadding" width="100%"><tr><td class="nowrap">';
  1711. print $langs->trans('RefCustomer') . '</td><td align="left">';
  1712. print '</td>';
  1713. if ($action != 'refcustomer' && $object->brouillon)
  1714. print '<td align="right"><a href="' . $_SERVER['PHP_SELF'] . '?action=refcustomer&amp;id=' . $object->id . '">' . img_edit($langs->trans('Modify')) . '</a></td>';
  1715. print '</tr></table>';
  1716. print '</td><td colspan="3">';
  1717. if ($user->rights->commande->creer && $action == 'refcustomer') {
  1718. print '<form action="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '" method="post">';
  1719. print '<input type="hidden" name="token" value="' . $_SESSION ['newtoken'] . '">';
  1720. print '<input type="hidden" name="action" value="set_ref_client">';
  1721. print '<input type="text" class="flat" size="20" name="ref_client" value="' . $object->ref_client . '">';
  1722. print ' <input type="submit" class="button" value="' . $langs->trans('Modify') . '">';
  1723. print '</form>';
  1724. } else {
  1725. print $object->ref_client;
  1726. }
  1727. print '</td>';
  1728. print '</tr>';
  1729. // Third party
  1730. print '<tr><td>';
  1731. print '<table class="nobordernopadding" width="100%">';
  1732. print '<tr><td>' . $langs->trans('Company') . '</td>';
  1733. print '</td><td colspan="5">';
  1734. if (! empty($conf->global->COMMANDE_CHANGE_THIRDPARTY) && $action != 'editthirdparty' && $object->brouillon && $user->rights->commande->creer)
  1735. print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editthirdparty&amp;id=' . $object->id . '">' . img_edit($langs->trans('SetLinkToThirdParty'), 1) . '</a></td>';
  1736. print '</tr></table>';
  1737. print '</td><td colspan="5">';
  1738. if ($action == 'editthirdparty') {
  1739. $form->form_thirdparty($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, 'socid','client>0');
  1740. } else {
  1741. print $soc->getNomUrl(1, 'compta');
  1742. }
  1743. print '</tr>';
  1744. if ($soc->outstanding_limit)
  1745. {
  1746. // Outstanding Bill
  1747. print '<tr><td>';
  1748. print $langs->trans('OutstandingBill');
  1749. print '</td><td colspan="3">';
  1750. print price($soc->get_OutstandingBill()) . ' / ';
  1751. print price($soc->outstanding_limit, 0, '', 1, - 1, - 1, $conf->currency);
  1752. print '</td>';
  1753. print '</tr>';
  1754. }
  1755. // Relative and absolute discounts
  1756. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  1757. $filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final
  1758. // invoice
  1759. $filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
  1760. } else {
  1761. $filterabsolutediscount = "fk_facture_source IS NULL OR (fk_facture_source IS NOT NULL AND description LIKE '(DEPOSIT)%')";
  1762. $filtercreditnote = "fk_facture_source IS NOT NULL AND description NOT LIKE '(DEPOSIT)%'";
  1763. }
  1764. $addrelativediscount = '<a href="' . DOL_URL_ROOT . '/comm/remise.php?id=' . $soc->id . '&backtopage=' . urlencode($_SERVER["PHP_SELF"]) . '?facid=' . $object->id . '">' . $langs->trans("EditRelativeDiscounts") . '</a>';
  1765. $addabsolutediscount = '<a href="' . DOL_URL_ROOT . '/comm/remx.php?id=' . $soc->id . '&backtopage=' . urlencode($_SERVER["PHP_SELF"]) . '?facid=' . $object->id . '">' . $langs->trans("EditGlobalDiscounts") . '</a>';
  1766. $addcreditnote = '<a href="' . DOL_URL_ROOT . '/compta/facture.php?action=create&socid=' . $soc->id . '&type=2&backtopage=' . urlencode($_SERVER["PHP_SELF"]) . '?facid=' . $object->id . '">' . $langs->trans("AddCreditNote") . '</a>';
  1767. print '<tr><td>' . $langs->trans('Discounts') . '</td><td colspan="3">';
  1768. if ($soc->remise_percent)
  1769. print $langs->trans("CompanyHasRelativeDiscount", $soc->remise_percent);
  1770. else
  1771. print $langs->trans("CompanyHasNoRelativeDiscount");
  1772. print '. ';
  1773. $absolute_discount = $soc->getAvailableDiscounts('', 'fk_facture_source IS NULL');
  1774. $absolute_creditnote = $soc->getAvailableDiscounts('', 'fk_facture_source IS NOT NULL');
  1775. $absolute_discount = price2num($absolute_discount, 'MT');
  1776. $absolute_creditnote = price2num($absolute_creditnote, 'MT');
  1777. if ($absolute_discount) {
  1778. if ($object->statut > Commande::STATUS_DRAFT) {
  1779. print $langs->trans("CompanyHasAbsoluteDiscount", price($absolute_discount), $langs->transnoentities("Currency" . $conf->currency));
  1780. } else {
  1781. // Remise dispo de type remise fixe (not credit note)
  1782. print '<br>';
  1783. $form->form_remise_dispo($_SERVER["PHP_SELF"] . '?id=' . $object->id, 0, 'remise_id', $soc->id, $absolute_discount, $filterabsolutediscount);
  1784. }
  1785. }
  1786. if ($absolute_creditnote) {
  1787. print $langs->trans("CompanyHasCreditNote", price($absolute_creditnote), $langs->transnoentities("Currency" . $conf->currency)) . '. ';
  1788. }
  1789. if (! $absolute_discount && ! $absolute_creditnote)
  1790. print $langs->trans("CompanyHasNoAbsoluteDiscount") . '.';
  1791. print '</td></tr>';
  1792. // Date
  1793. print '<tr><td>';
  1794. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1795. print $langs->trans('Date');
  1796. print '</td>';
  1797. if ($action != 'editdate' && $object->brouillon)
  1798. print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editdate&amp;id=' . $object->id . '">' . img_edit($langs->trans('SetDate'), 1) . '</a></td>';
  1799. print '</tr></table>';
  1800. print '</td><td colspan="3">';
  1801. if ($action == 'editdate') {
  1802. print '<form name="setdate" action="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '" method="post">';
  1803. print '<input type="hidden" name="token" value="' . $_SESSION ['newtoken'] . '">';
  1804. print '<input type="hidden" name="action" value="setdate">';
  1805. $form->select_date($object->date, 'order_', '', '', '', "setdate");
  1806. print '<input type="submit" class="button" value="' . $langs->trans('Modify') . '">';
  1807. print '</form>';
  1808. } else {
  1809. print $object->date ? dol_print_date($object->date, 'daytext') : '&nbsp;';
  1810. if ($object->hasDelay() && empty($object->date_livraison)) {
  1811. print ' '.img_picto($langs->trans("Late").' : '.$object->showDelay(), "warning");
  1812. }
  1813. }
  1814. print '</td>';
  1815. print '</tr>';
  1816. // Delivery date planed
  1817. print '<tr><td height="10">';
  1818. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1819. print $langs->trans('DateDeliveryPlanned');
  1820. print '</td>';
  1821. if ($action != 'editdate_livraison')
  1822. print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editdate_livraison&amp;id=' . $object->id . '">' . img_edit($langs->trans('SetDeliveryDate'), 1) . '</a></td>';
  1823. print '</tr></table>';
  1824. print '</td><td colspan="3">';
  1825. if ($action == 'editdate_livraison') {
  1826. print '<form name="setdate_livraison" action="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '" method="post">';
  1827. print '<input type="hidden" name="token" value="' . $_SESSION ['newtoken'] . '">';
  1828. print '<input type="hidden" name="action" value="setdate_livraison">';
  1829. $form->select_date($object->date_livraison ? $object->date_livraison : - 1, 'liv_', '', '', '', "setdate_livraison");
  1830. print '<input type="submit" class="button" value="' . $langs->trans('Modify') . '">';
  1831. print '</form>';
  1832. } else {
  1833. print $object->date_livraison ? dol_print_date($object->date_livraison, 'daytext') : '&nbsp;';
  1834. if ($object->hasDelay() && ! empty($object->date_livraison)) {
  1835. print ' '.img_picto($langs->trans("Late").' : '.$object->showDelay(), "warning");
  1836. }
  1837. }
  1838. print '</td>';
  1839. print '</tr>';
  1840. // Shipping Method
  1841. if (! empty($conf->expedition->enabled)) {
  1842. print '<tr><td height="10">';
  1843. print '<table width="100%" class="nobordernopadding"><tr><td>';
  1844. print $langs->trans('SendingMethod');
  1845. print '</td>';
  1846. if ($action != 'editshippingmethod' && $user->rights->commande->creer)
  1847. print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=editshippingmethod&amp;id='.$object->id.'">'.img_edit($langs->trans('SetShippingMode'),1).'</a></td>';
  1848. print '</tr></table>';
  1849. print '</td><td colspan="3">';
  1850. if ($action == 'editshippingmethod') {
  1851. $form->formSelectShippingMethod($_SERVER['PHP_SELF'].'?id='.$object->id, $object->shipping_method_id, 'shipping_method_id', 1);
  1852. } else {
  1853. $form->formSelectShippingMethod($_SERVER['PHP_SELF'].'?id='.$object->id, $object->shipping_method_id, 'none');
  1854. }
  1855. print '</td>';
  1856. print '</tr>';
  1857. }
  1858. // Warehouse
  1859. if (! empty($conf->expedition->enabled) && ! empty($conf->global->WAREHOUSE_ASK_WAREHOUSE_DURING_ORDER)) {
  1860. require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
  1861. $formproduct=new FormProduct($db);
  1862. print '<tr><td>';
  1863. print '<table width="100%" class="nobordernopadding"><tr><td>';
  1864. print $langs->trans('Warehouse');
  1865. print '</td>';
  1866. if ($action != 'editwarehouse' && $user->rights->commande->creer)
  1867. print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=editwarehouse&amp;id='.$object->id.'">'.img_edit($langs->trans('SetWarehouse'),1).'</a></td>';
  1868. print '</tr></table>';
  1869. print '</td><td colspan="3">';
  1870. if ($action == 'editwarehouse') {
  1871. $formproduct->formSelectWarehouses($_SERVER['PHP_SELF'].'?id='.$object->id, $object->warehouse_id, 'warehouse_id', 1);
  1872. } else {
  1873. $formproduct->formSelectWarehouses($_SERVER['PHP_SELF'].'?id='.$object->id, $object->warehouse_id, 'none');
  1874. }
  1875. print '</td>';
  1876. print '</tr>';
  1877. }
  1878. // Terms of payment
  1879. print '<tr><td height="10">';
  1880. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1881. print $langs->trans('PaymentConditionsShort');
  1882. print '</td>';
  1883. if ($action != 'editconditions')
  1884. print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editconditions&amp;id=' . $object->id . '">' . img_edit($langs->trans('SetConditions'), 1) . '</a></td>';
  1885. print '</tr></table>';
  1886. print '</td><td colspan="3">';
  1887. if ($action == 'editconditions') {
  1888. $form->form_conditions_reglement($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->cond_reglement_id, 'cond_reglement_id', 1);
  1889. } else {
  1890. $form->form_conditions_reglement($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->cond_reglement_id, 'none', 1);
  1891. }
  1892. print '</td>';
  1893. print '</tr>';
  1894. // Mode of payment
  1895. print '<tr><td height="10">';
  1896. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1897. print $langs->trans('PaymentMode');
  1898. print '</td>';
  1899. if ($action != 'editmode')
  1900. print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editmode&amp;id=' . $object->id . '">' . img_edit($langs->trans('SetMode'), 1) . '</a></td>';
  1901. print '</tr></table>';
  1902. print '</td><td colspan="3">';
  1903. if ($action == 'editmode') {
  1904. $form->form_modes_reglement($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->mode_reglement_id, 'mode_reglement_id');
  1905. } else {
  1906. $form->form_modes_reglement($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->mode_reglement_id, 'none');
  1907. }
  1908. print '</td></tr>';
  1909. // Multicurrency
  1910. if (! empty($conf->multicurrency->enabled))
  1911. {
  1912. // Multicurrency code
  1913. print '<tr>';
  1914. print '<td width="25%">';
  1915. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1916. print fieldLabel('Currency','multicurrency_code');
  1917. print '</td>';
  1918. if ($action != 'editmulticurrencycode' && ! empty($object->brouillon))
  1919. print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editmulticurrencycode&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetMultiCurrencyCode'), 1) . '</a></td>';
  1920. print '</tr></table>';
  1921. print '</td><td colspan="5">';
  1922. if ($action == 'editmulticurrencycode') {
  1923. $form->form_multicurrency_code($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->multicurrency_code, 'multicurrency_code');
  1924. } else {
  1925. $form->form_multicurrency_code($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->multicurrency_code, 'none');
  1926. }
  1927. print '</td></tr>';
  1928. // Multicurrency rate
  1929. print '<tr>';
  1930. print '<td width="25%">';
  1931. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1932. print fieldLabel('Rate','multicurrency_tx');
  1933. print '</td>';
  1934. if ($action != 'editmulticurrencyrate' && ! empty($object->brouillon))
  1935. print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editmulticurrencyrate&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetMultiCurrencyCode'), 1) . '</a></td>';
  1936. print '</tr></table>';
  1937. print '</td><td colspan="5">';
  1938. if ($action == 'editmulticurrencyrate') {
  1939. $form->form_multicurrency_rate($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->multicurrency_tx, 'multicurrency_tx', $object->multicurrency_code);
  1940. } else {
  1941. $form->form_multicurrency_rate($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->multicurrency_tx, 'none', $object->multicurrency_code);
  1942. }
  1943. print '</td></tr>';
  1944. }
  1945. // Delivery delay
  1946. print '<tr class="fielddeliverydelay"><td height="10">';
  1947. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1948. print $langs->trans('AvailabilityPeriod');
  1949. print '</td>';
  1950. if ($action != 'editavailability')
  1951. print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editavailability&amp;id=' . $object->id . '">' . img_edit($langs->trans('SetAvailability'), 1) . '</a></td>';
  1952. print '</tr></table>';
  1953. print '</td><td colspan="3">';
  1954. if ($action == 'editavailability') {
  1955. $form->form_availability($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->availability_id, 'availability_id', 1);
  1956. } else {
  1957. $form->form_availability($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->availability_id, 'none', 1);
  1958. }
  1959. print '</td></tr>';
  1960. // Origin
  1961. print '<tr><td height="10">';
  1962. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1963. print $langs->trans('Source');
  1964. print '</td>';
  1965. if ($action != 'editdemandreason')
  1966. print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editdemandreason&amp;id=' . $object->id . '">' . img_edit($langs->trans('SetDemandReason'), 1) . '</a></td>';
  1967. print '</tr></table>';
  1968. print '</td><td colspan="3">';
  1969. if ($action == 'editdemandreason') {
  1970. $form->formInputReason($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->demand_reason_id, 'demand_reason_id', 1);
  1971. } else {
  1972. $form->formInputReason($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->demand_reason_id, 'none');
  1973. }
  1974. // Removed because using dictionary is an admin feature, not a user feature. There is already the "star" to show info to admin users.
  1975. // This is to avoid too heavy screens and have an uniform look and feel for all screens.
  1976. // print '</td><td>';
  1977. // print '<a href="'.DOL_URL_ROOT.'/admin/dict.php?id=22&origin=order&originid='.$object->id.'">'.$langs->trans("DictionarySource").'</a>';
  1978. print '</td></tr>';
  1979. $tmparray=$object->getTotalWeightVolume();
  1980. $totalWeight=$tmparray['weight'];
  1981. $totalVolume=$tmparray['volume'];
  1982. if ($totalWeight || $totalVolume)
  1983. {
  1984. print '<tr><td>'.$langs->trans("CalculatedWeight").' / '.$langs->trans("CalculatedVolume").'</td>';
  1985. print '<td colspan="3">';
  1986. print showDimensionInBestUnit($totalWeight, 0, "weight", $langs, isset($conf->global->MAIN_WEIGHT_DEFAULT_ROUND)?$conf->global->MAIN_WEIGHT_DEFAULT_ROUND:-1, isset($conf->global->MAIN_WEIGHT_DEFAULT_UNIT)?$conf->global->MAIN_WEIGHT_DEFAULT_UNIT:'no');
  1987. print ' / ';
  1988. print showDimensionInBestUnit($totalVolume, 0, "volume", $langs, isset($conf->global->MAIN_VOLUME_DEFAULT_ROUND)?$conf->global->MAIN_VOLUME_DEFAULT_ROUND:-1, isset($conf->global->MAIN_VOLUME_DEFAULT_UNIT)?$conf->global->MAIN_VOLUME_DEFAULT_UNIT:'no');
  1989. print '</td></tr>';
  1990. }
  1991. // TODO How record was recorded OrderMode (llx_c_input_method)
  1992. // Project
  1993. if (! empty($conf->projet->enabled))
  1994. {
  1995. $langs->load('projects');
  1996. print '<tr><td height="10">';
  1997. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1998. print $langs->trans('Project');
  1999. print '</td>';
  2000. if ($action != 'classify')
  2001. print '<td align="right"><a href="' . $_SERVER['PHP_SELF'] . '?action=classify&amp;id=' . $object->id . '">' . img_edit($langs->trans('SetProject')) . '</a></td>';
  2002. print '</tr></table>';
  2003. print '</td><td colspan="3">';
  2004. // print "$object->id, $object->socid, $object->fk_project";
  2005. if ($action == 'classify') {
  2006. $form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1);
  2007. } else {
  2008. $form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'none', 0, 0);
  2009. }
  2010. print '</td></tr>';
  2011. }
  2012. // Incoterms
  2013. if (!empty($conf->incoterm->enabled))
  2014. {
  2015. print '<tr><td>';
  2016. print '<table width="100%" class="nobordernopadding"><tr><td>';
  2017. print $langs->trans('IncotermLabel');
  2018. print '<td><td align="right">';
  2019. if ($user->rights->commande->creer) print '<a href="'.DOL_URL_ROOT.'/commande/card.php?id='.$object->id.'&action=editincoterm">'.img_edit().'</a>';
  2020. else print '&nbsp;';
  2021. print '</td></tr></table>';
  2022. print '</td>';
  2023. print '<td colspan="3">';
  2024. if ($action != 'editincoterm')
  2025. {
  2026. print $form->textwithpicto($object->display_incoterms(), $object->libelle_incoterms, 1);
  2027. }
  2028. else
  2029. {
  2030. print $form->select_incoterms((!empty($object->fk_incoterms) ? $object->fk_incoterms : ''), (!empty($object->location_incoterms)?$object->location_incoterms:''), $_SERVER['PHP_SELF'].'?id='.$object->id);
  2031. }
  2032. print '</td></tr>';
  2033. }
  2034. // Other attributes
  2035. $cols = 3;
  2036. include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_view.tpl.php';
  2037. $rowspan = 4;
  2038. if ($mysoc->localtax1_assuj == "1" || $object->total_localtax1 != 0)
  2039. $rowspan ++;
  2040. if ($mysoc->localtax2_assuj == "1" || $object->total_localtax2 != 0)
  2041. $rowspan ++;
  2042. // Bank Account
  2043. if (! empty($conf->global->BANK_ASK_PAYMENT_BANK_DURING_ORDER) && ! empty($conf->banque->enabled))
  2044. {
  2045. print '<tr><td class="nowrap">';
  2046. print '<table width="100%" class="nobordernopadding"><tr><td class="nowrap">';
  2047. print $langs->trans('BankAccount');
  2048. print '<td>';
  2049. if ($action != 'editbankaccount' && $user->rights->commande->creer)
  2050. print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=editbankaccount&amp;id='.$object->id.'">'.img_edit($langs->trans('SetBankAccount'),1).'</a></td>';
  2051. print '</tr></table>';
  2052. print '</td><td colspan="3">';
  2053. if ($action == 'editbankaccount') {
  2054. $form->formSelectAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->fk_account, 'fk_account', 1);
  2055. } else {
  2056. $form->formSelectAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->fk_account, 'none');
  2057. }
  2058. print '</td>';
  2059. print '</tr>';
  2060. }
  2061. // Total HT
  2062. print '<tr><td>' . $langs->trans('AmountHT') . '</td>';
  2063. print '<td>' . price($object->total_ht, 1, '', 1, - 1, - 1, $conf->currency) . '</td>';
  2064. // Margin Infos
  2065. if (! empty($conf->margin->enabled)) {
  2066. print '<td valign="top" width="50%" colspan="2" rowspan="' . $rowspan . '">';
  2067. $formmargin->displayMarginInfos($object);
  2068. print '</td>';
  2069. } else
  2070. print '<td width="50%" colspan="2" rowspan="' . $rowspan . '"></td>';
  2071. print '</tr>';
  2072. // Total VAT
  2073. print '<tr><td>' . $langs->trans('AmountVAT') . '</td><td>' . price($object->total_tva, 1, '', 1, - 1, - 1, $conf->currency) . '</td></tr>';
  2074. // Amount Local Taxes
  2075. if ($mysoc->localtax1_assuj == "1" || $object->total_localtax1 != 0) // Localtax1
  2076. {
  2077. print '<tr><td>' . $langs->transcountry("AmountLT1", $mysoc->country_code) . '</td>';
  2078. print '<td>' . price($object->total_localtax1, 1, '', 1, - 1, - 1, $conf->currency) . '</td></tr>';
  2079. }
  2080. if ($mysoc->localtax2_assuj == "1" || $object->total_localtax2 != 0) // Localtax2 IRPF
  2081. {
  2082. print '<tr><td>' . $langs->transcountry("AmountLT2", $mysoc->country_code) . '</td>';
  2083. print '<td>' . price($object->total_localtax2, 1, '', 1, - 1, - 1, $conf->currency) . '</td></tr>';
  2084. }
  2085. // Total TTC
  2086. print '<tr><td>' . $langs->trans('AmountTTC') . '</td><td>' . price($object->total_ttc, 1, '', 1, - 1, - 1, $conf->currency) . '</td></tr>';
  2087. if (!empty($conf->multicurrency->enabled))
  2088. {
  2089. // Multicurrency Amount HT
  2090. print '<tr><td height="10">' . fieldLabel('MulticurrencyAmountHT','multicurrency_total_ht') . '</td>';
  2091. 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>';
  2092. print '</tr>';
  2093. // Multicurrency Amount VAT
  2094. print '<tr><td height="10">' . fieldLabel('MulticurrencyAmountVAT','multicurrency_total_tva') . '</td>';
  2095. 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>';
  2096. print '</tr>';
  2097. // Multicurrency Amount TTC
  2098. print '<tr><td height="10">' . fieldLabel('MulticurrencyAmountTTC','multicurrency_total_ttc') . '</td>';
  2099. 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>';
  2100. print '</tr>';
  2101. }
  2102. // Statut
  2103. print '<tr><td>' . $langs->trans('Status') . '</td><td>' . $object->getLibStatut(4) . '</td></tr>';
  2104. print '</table><br>';
  2105. print "\n";
  2106. if (! empty($conf->global->MAIN_DISABLE_CONTACTS_TAB)) {
  2107. $blocname = 'contacts';
  2108. $title = $langs->trans('ContactsAddresses');
  2109. include DOL_DOCUMENT_ROOT . '/core/tpl/bloc_showhide.tpl.php';
  2110. }
  2111. if (! empty($conf->global->MAIN_DISABLE_NOTES_TAB)) {
  2112. $blocname = 'notes';
  2113. $title = $langs->trans('Notes');
  2114. include DOL_DOCUMENT_ROOT . '/core/tpl/bloc_showhide.tpl.php';
  2115. }
  2116. /*
  2117. * Lines
  2118. */
  2119. $result = $object->getLinesArray();
  2120. print ' <form name="addproduct" id="addproduct" action="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . (($action != 'editline') ? '#add' : '#line_' . GETPOST('lineid')) . '" method="POST">
  2121. <input type="hidden" name="token" value="' . $_SESSION ['newtoken'] . '">
  2122. <input type="hidden" name="action" value="' . (($action != 'editline') ? 'addline' : 'updateline') . '">
  2123. <input type="hidden" name="mode" value="">
  2124. <input type="hidden" name="id" value="' . $object->id . '">
  2125. ';
  2126. if (! empty($conf->use_javascript_ajax) && $object->statut == Commande::STATUS_DRAFT) {
  2127. include DOL_DOCUMENT_ROOT . '/core/tpl/ajaxrow.tpl.php';
  2128. }
  2129. print '<table id="tablelines" class="noborder noshadow" width="100%">';
  2130. // Show object lines
  2131. if (! empty($object->lines))
  2132. $ret = $object->printObjectLines($action, $mysoc, $soc, $lineid, 1);
  2133. $numlines = count($object->lines);
  2134. /*
  2135. * Form to add new line
  2136. */
  2137. if ($object->statut == Commande::STATUS_DRAFT && $user->rights->commande->creer)
  2138. {
  2139. if ($action != 'editline')
  2140. {
  2141. $var = true;
  2142. // Add free products/services
  2143. $object->formAddObjectLine(1, $mysoc, $soc);
  2144. $parameters = array();
  2145. $reshook = $hookmanager->executeHooks('formAddObjectLine', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  2146. }
  2147. }
  2148. print '</table>';
  2149. print "</form>\n";
  2150. dol_fiche_end();
  2151. /*
  2152. * Boutons actions
  2153. */
  2154. if ($action != 'presend' && $action != 'editline') {
  2155. print '<div class="tabsAction">';
  2156. $parameters = array();
  2157. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been
  2158. // modified by hook
  2159. if (empty($reshook)) {
  2160. // Send
  2161. if ($object->statut > Commande::STATUS_DRAFT) {
  2162. if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) || $user->rights->commande->order_advance->send)) {
  2163. print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&amp;action=presend&amp;mode=init">' . $langs->trans('SendByMail') . '</a></div>';
  2164. } else
  2165. print '<div class="inline-block divButAction"><a class="butActionRefused" href="#">' . $langs->trans('SendByMail') . '</a></div>';
  2166. }
  2167. // Valid
  2168. if ($object->statut == Commande::STATUS_DRAFT && $object->total_ttc >= 0 && $numlines > 0 &&
  2169. ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->commande->creer))
  2170. || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->commande->order_advance->validate)))
  2171. )
  2172. {
  2173. print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&amp;action=validate">' . $langs->trans('Validate') . '</a></div>';
  2174. }
  2175. // Edit
  2176. if ($object->statut == Commande::STATUS_VALIDATED && $user->rights->commande->creer) {
  2177. print '<div class="inline-block divButAction"><a class="butAction" href="card.php?id=' . $object->id . '&amp;action=modif">' . $langs->trans('Modify') . '</a></div>';
  2178. }
  2179. // Create event
  2180. if ($conf->agenda->enabled && ! empty($conf->global->MAIN_ADD_EVENT_ON_ELEMENT_CARD)) // Add hidden condition because this is not a
  2181. // "workflow" action so should appears somewhere else on
  2182. // page.
  2183. {
  2184. print '<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>';
  2185. }
  2186. // Create intervention
  2187. if ($conf->ficheinter->enabled) {
  2188. $langs->load("interventions");
  2189. if ($object->statut > Commande::STATUS_DRAFT && $object->statut < Commande::STATUS_CLOSED && $object->getNbOfServicesLines() > 0) {
  2190. if ($user->rights->ficheinter->creer) {
  2191. print '<div class="inline-block divButAction"><a class="butAction" href="' . DOL_URL_ROOT . '/fichinter/card.php?action=create&amp;origin=' . $object->element . '&amp;originid=' . $object->id . '&amp;socid=' . $object->socid . '">' . $langs->trans('AddIntervention') . '</a></div>';
  2192. } else {
  2193. print '<div class="inline-block divButAction"><a class="butActionRefused" href="#" title="' . dol_escape_htmltag($langs->trans("NotAllowed")) . '">' . $langs->trans('AddIntervention') . '</a></div>';
  2194. }
  2195. }
  2196. }
  2197. // Create contract
  2198. if ($conf->contrat->enabled && ($object->statut == Commande::STATUS_VALIDATED || $object->statut == Commande::STATUS_ACCEPTED)) {
  2199. $langs->load("contracts");
  2200. if ($user->rights->contrat->creer) {
  2201. print '<div class="inline-block divButAction"><a class="butAction" href="' . DOL_URL_ROOT . '/contrat/card.php?action=create&amp;origin=' . $object->element . '&amp;originid=' . $object->id . '&amp;socid=' . $object->socid . '">' . $langs->trans('AddContract') . '</a></div>';
  2202. }
  2203. }
  2204. // Ship
  2205. $numshipping = 0;
  2206. if (! empty($conf->expedition->enabled)) {
  2207. $numshipping = $object->nb_expedition();
  2208. if ($object->statut > Commande::STATUS_DRAFT && $object->statut < Commande::STATUS_CLOSED && $object->getNbOfProductsLines() > 0) {
  2209. if (($conf->expedition_bon->enabled && $user->rights->expedition->creer) || ($conf->livraison_bon->enabled && $user->rights->expedition->livraison->creer)) {
  2210. if ($user->rights->expedition->creer) {
  2211. print '<div class="inline-block divButAction"><a class="butAction" href="' . DOL_URL_ROOT . '/expedition/shipment.php?id=' . $object->id . '">' . $langs->trans('ShipProduct') . '</a></div>';
  2212. } else {
  2213. print '<div class="inline-block divButAction"><a class="butActionRefused" href="#" title="' . dol_escape_htmltag($langs->trans("NotAllowed")) . '">' . $langs->trans('ShipProduct') . '</a></div>';
  2214. }
  2215. } else {
  2216. $langs->load("errors");
  2217. print '<div class="inline-block divButAction"><a class="butActionRefused" href="#" title="' . dol_escape_htmltag($langs->trans("ErrorModuleSetupNotComplete")) . '">' . $langs->trans('ShipProduct') . '</a></div>';
  2218. }
  2219. }
  2220. }
  2221. // Reopen a closed order
  2222. if (($object->statut == Commande::STATUS_CLOSED || $object->statut == Commande::STATUS_CANCELED) && $user->rights->commande->creer) {
  2223. print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&amp;action=reopen">' . $langs->trans('ReOpen') . '</a></div>';
  2224. }
  2225. // Set to shipped
  2226. if (($object->statut == Commande::STATUS_VALIDATED || $object->statut == Commande::STATUS_ACCEPTED) && $user->rights->commande->cloturer) {
  2227. print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&amp;action=shipped">' . $langs->trans('ClassifyShipped') . '</a></div>';
  2228. }
  2229. // Create bill and Classify billed
  2230. // Note: Even if module invoice is not enabled, we should be able to use button "Classified billed"
  2231. if ($object->statut > Commande::STATUS_DRAFT && ! $object->billed) {
  2232. if (! empty($conf->facture->enabled) && $user->rights->facture->creer && empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) {
  2233. print '<div class="inline-block divButAction"><a class="butAction" href="' . DOL_URL_ROOT . '/compta/facture.php?action=create&amp;origin=' . $object->element . '&amp;originid=' . $object->id . '&amp;socid=' . $object->socid . '">' . $langs->trans("CreateBill") . '</a></div>';
  2234. }
  2235. if ($user->rights->commande->creer && $object->statut >= Commande::STATUS_VALIDATED && empty($conf->global->WORKFLOW_DISABLE_CLASSIFY_BILLED_FROM_ORDER) && empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT)) {
  2236. print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&amp;action=classifybilled">' . $langs->trans("ClassifyBilled") . '</a></div>';
  2237. }
  2238. }
  2239. if ($object->statut > Commande::STATUS_DRAFT && $object->billed) {
  2240. if ($user->rights->commande->creer && $object->statut >= Commande::STATUS_VALIDATED && empty($conf->global->WORKFLOW_DISABLE_CLASSIFY_BILLED_FROM_ORDER) && empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT)) {
  2241. print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&amp;action=classifyunbilled">' . $langs->trans("ClassifyUnBilled") . '</a></div>';
  2242. }
  2243. }
  2244. // Clone
  2245. if ($user->rights->commande->creer) {
  2246. print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&amp;socid=' . $object->socid . '&amp;action=clone&amp;object=order">' . $langs->trans("ToClone") . '</a></div>';
  2247. }
  2248. // Cancel order
  2249. if ($object->statut == Commande::STATUS_VALIDATED &&
  2250. ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->commande->cloturer))
  2251. || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->commande->order_advance->annuler)))
  2252. )
  2253. {
  2254. print '<div class="inline-block divButAction"><a class="butActionDelete" href="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&amp;action=cancel">' . $langs->trans('Cancel') . '</a></div>';
  2255. }
  2256. // Delete order
  2257. if ($user->rights->commande->supprimer) {
  2258. if ($numshipping == 0) {
  2259. print '<div class="inline-block divButAction"><a class="butActionDelete" href="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&amp;action=delete">' . $langs->trans('Delete') . '</a></div>';
  2260. } else {
  2261. print '<div class="inline-block divButAction"><a class="butActionRefused" href="#" title="' . $langs->trans("ShippingExist") . '">' . $langs->trans("Delete") . '</a></div>';
  2262. }
  2263. }
  2264. }
  2265. print '</div>';
  2266. }
  2267. // Select mail models is same action as presend
  2268. if (GETPOST('modelselected')) {
  2269. $action = 'presend';
  2270. }
  2271. if ($action != 'presend')
  2272. {
  2273. print '<div class="fichecenter"><div class="fichehalfleft">';
  2274. // Documents
  2275. $comref = dol_sanitizeFileName($object->ref);
  2276. $file = $conf->commande->dir_output . '/' . $comref . '/' . $comref . '.pdf';
  2277. $relativepath = $comref . '/' . $comref . '.pdf';
  2278. $filedir = $conf->commande->dir_output . '/' . $comref;
  2279. $urlsource = $_SERVER["PHP_SELF"] . "?id=" . $object->id;
  2280. $genallowed = $user->rights->commande->creer;
  2281. $delallowed = $user->rights->commande->supprimer;
  2282. $somethingshown = $formfile->show_documents('commande', $comref, $filedir, $urlsource, $genallowed, $delallowed, $object->modelpdf, 1, 0, 0, 28, 0, '', '', '', $soc->default_lang);
  2283. // Linked object block
  2284. $somethingshown = $form->showLinkedObjectBlock($object);
  2285. // Show links to link elements
  2286. //$linktoelem = $form->showLinkToObjectBlock($object);
  2287. //if ($linktoelem) print '<br>'.$linktoelem;
  2288. print '</div><div class="fichehalfright"><div class="ficheaddleft">';
  2289. // List of actions on element
  2290. include_once DOL_DOCUMENT_ROOT . '/core/class/html.formactions.class.php';
  2291. $formactions = new FormActions($db);
  2292. $somethingshown = $formactions->showactions($object, 'order', $socid);
  2293. print '</div></div></div>';
  2294. }
  2295. /*
  2296. * Action presend
  2297. */
  2298. if ($action == 'presend')
  2299. {
  2300. $object->fetch_projet();
  2301. $ref = dol_sanitizeFileName($object->ref);
  2302. include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
  2303. $fileparams = dol_most_recent_file($conf->commande->dir_output . '/' . $ref, preg_quote($ref, '/').'[^\-]+');
  2304. $file = $fileparams['fullname'];
  2305. // Define output language
  2306. $outputlangs = $langs;
  2307. $newlang = '';
  2308. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($_REQUEST['lang_id']))
  2309. $newlang = $_REQUEST['lang_id'];
  2310. if ($conf->global->MAIN_MULTILANGS && empty($newlang))
  2311. $newlang = $object->thirdparty->default_lang;
  2312. if (!empty($newlang))
  2313. {
  2314. $outputlangs = new Translate('', $conf);
  2315. $outputlangs->setDefaultLang($newlang);
  2316. $outputlangs->load('commercial');
  2317. }
  2318. // Build document if it not exists
  2319. if (! $file || ! is_readable($file)) {
  2320. $result = $object->generateDocument(GETPOST('model') ? GETPOST('model') : $object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  2321. if ($result <= 0) {
  2322. dol_print_error($db, $object->error, $object->errors);
  2323. exit();
  2324. }
  2325. $fileparams = dol_most_recent_file($conf->commande->dir_output . '/' . $ref, preg_quote($ref, '/').'[^\-]+');
  2326. $file = $fileparams['fullname'];
  2327. }
  2328. print '<div class="clearboth"></div>';
  2329. print '<br>';
  2330. print load_fiche_titre($langs->trans('SendOrderByMail'));
  2331. dol_fiche_head('');
  2332. // Cree l'objet formulaire mail
  2333. include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
  2334. $formmail = new FormMail($db);
  2335. $formmail->param['langsmodels']=(empty($newlang)?$langs->defaultlang:$newlang);
  2336. $formmail->fromtype = 'user';
  2337. $formmail->fromid = $user->id;
  2338. $formmail->fromname = $user->getFullName($langs);
  2339. $formmail->frommail = $user->email;
  2340. $formmail->trackid='ord'.$object->id;
  2341. if (! empty($conf->global->MAIN_EMAIL_ADD_TRACK_ID) && ($conf->global->MAIN_EMAIL_ADD_TRACK_ID & 2)) // If bit 2 is set
  2342. {
  2343. include DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  2344. $formmail->frommail=dolAddEmailTrackId($formmail->frommail, 'ord'.$object->id);
  2345. }
  2346. $formmail->withfrom = 1;
  2347. $liste = array();
  2348. foreach ($object->thirdparty->thirdparty_and_contact_email_array(1) as $key => $value)
  2349. $liste [$key] = $value;
  2350. $formmail->withto = GETPOST('sendto') ? GETPOST('sendto') : $liste;
  2351. $formmail->withtocc = $liste;
  2352. $formmail->withtoccc = $conf->global->MAIN_EMAIL_USECCC;
  2353. if (empty($object->ref_client)) {
  2354. $formmail->withtopic = $outputlangs->trans('SendOrderRef', '__ORDERREF__');
  2355. } else if (! empty($object->ref_client)) {
  2356. $formmail->withtopic = $outputlangs->trans('SendOrderRef', '__ORDERREF__ (__REFCLIENT__)');
  2357. }
  2358. $formmail->withfile = 2;
  2359. $formmail->withbody = 1;
  2360. $formmail->withdeliveryreceipt = 1;
  2361. $formmail->withcancel = 1;
  2362. // Tableau des substitutions
  2363. $formmail->setSubstitFromObject($object);
  2364. $formmail->substit ['__ORDERREF__'] = $object->ref;
  2365. $custcontact = '';
  2366. $contactarr = array();
  2367. $contactarr = $object->liste_contact(- 1, 'external');
  2368. if (is_array($contactarr) && count($contactarr) > 0)
  2369. {
  2370. foreach ($contactarr as $contact)
  2371. {
  2372. if ($contact['libelle'] == $langs->trans('TypeContact_commande_external_CUSTOMER')) { // TODO Use code and not label
  2373. $contactstatic = new Contact($db);
  2374. $contactstatic->fetch($contact ['id']);
  2375. $custcontact = $contactstatic->getFullName($langs, 1);
  2376. }
  2377. }
  2378. if (! empty($custcontact)) {
  2379. $formmail->substit['__CONTACTCIVNAME__'] = $custcontact;
  2380. }
  2381. }
  2382. // Tableau des parametres complementaires
  2383. $formmail->param['action'] = 'send';
  2384. $formmail->param['models'] = 'order_send';
  2385. $formmail->param['models_id']=GETPOST('modelmailselected','int');
  2386. $formmail->param['orderid'] = $object->id;
  2387. $formmail->param['returnurl'] = $_SERVER["PHP_SELF"] . '?id=' . $object->id;
  2388. // Init list of files
  2389. if (GETPOST("mode") == 'init') {
  2390. $formmail->clear_attached_files();
  2391. $formmail->add_attached_files($file, basename($file), dol_mimetype($file));
  2392. }
  2393. // Show form
  2394. print $formmail->get_form();
  2395. dol_fiche_end();
  2396. }
  2397. }
  2398. }
  2399. llxFooter();
  2400. $db->close();