card.php 108 KB

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