card.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. /* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/asset/card.php
  20. * \ingroup asset
  21. * \brief Page to create/edit/view asset
  22. */
  23. // Load Dolibarr environment
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/asset.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/asset/class/asset.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  29. // Load translation files required by the page
  30. $langs->loadLangs(array("assets", "other"));
  31. // Get parameters
  32. $id = GETPOST('id', 'int');
  33. $ref = GETPOST('ref', 'alpha');
  34. $action = GETPOST('action', 'aZ09');
  35. $confirm = GETPOST('confirm', 'alpha');
  36. $cancel = GETPOST('cancel', 'aZ09');
  37. $contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'assetcard'; // To manage different context of search
  38. $backtopage = GETPOST('backtopage', 'alpha');
  39. $backtopageforcancel = GETPOST('backtopageforcancel', 'alpha');
  40. // Initialize technical objects
  41. $object = new Asset($db);
  42. $extrafields = new ExtraFields($db);
  43. $diroutputmassaction = $conf->asset->dir_output.'/temp/massgeneration/'.$user->id;
  44. $hookmanager->initHooks(array('assetcard', 'globalcard')); // Note that conf->hooks_modules contains array
  45. // Fetch optionals attributes and labels
  46. $extrafields->fetch_name_optionals_label($object->table_element);
  47. $search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
  48. // Initialize array of search criterias
  49. $search_all = GETPOST("search_all", 'alpha');
  50. $search = array();
  51. foreach ($object->fields as $key => $val) {
  52. if (GETPOST('search_'.$key, 'alpha')) {
  53. $search[$key] = GETPOST('search_'.$key, 'alpha');
  54. }
  55. }
  56. if (empty($action) && empty($id) && empty($ref)) {
  57. $action = 'view';
  58. }
  59. // Load object
  60. include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
  61. $permissiontoread = $user->hasRight('asset', 'read');
  62. $permissiontoadd = $user->hasRight('asset', 'write'); // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
  63. $permissiontodelete = $user->hasRight('asset', 'delete') || ($permissiontoadd && isset($object->status) && $object->status == $object::STATUS_DRAFT);
  64. $permissionnote = $user->hasRight('asset', 'write'); // Used by the include of actions_setnotes.inc.php
  65. $permissiondellink = $user->hasRight('asset', 'write'); // Used by the include of actions_dellink.inc.php
  66. $upload_dir = $conf->asset->multidir_output[isset($object->entity) ? $object->entity : 1];
  67. // Security check (enable the most restrictive one)
  68. if ($user->socid > 0) {
  69. accessforbidden();
  70. }
  71. if ($user->socid > 0) {
  72. $socid = $user->socid;
  73. }
  74. $isdraft = (($object->status == $object::STATUS_DRAFT) ? 1 : 0);
  75. restrictedArea($user, $object->element, $object->id, $object->table_element, '', 'fk_soc', 'rowid', $isdraft);
  76. if (!isModEnabled('asset')) {
  77. accessforbidden();
  78. }
  79. if (!$permissiontoread) {
  80. accessforbidden();
  81. }
  82. /*
  83. * Actions
  84. */
  85. $parameters = array();
  86. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  87. if ($reshook < 0) {
  88. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  89. }
  90. if (empty($reshook)) {
  91. $error = 0;
  92. $backurlforlist = DOL_URL_ROOT.'/asset/list.php';
  93. if (empty($backtopage) || ($cancel && empty($id))) {
  94. if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
  95. if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
  96. $backtopage = $backurlforlist;
  97. } else {
  98. $backtopage = DOL_URL_ROOT.'/asset/card.php?id='.((!empty($id) && $id > 0) ? $id : '__ID__');
  99. }
  100. }
  101. }
  102. $object->oldcopy = dol_clone($object, 2);
  103. $triggermodname = 'ASSET_MODIFY'; // Name of trigger action code to execute when we modify record
  104. // Action dispose object
  105. if ($action == 'confirm_disposal' && $confirm == 'yes' && $permissiontoadd) {
  106. $object->disposal_date = dol_mktime(12, 0, 0, GETPOST('disposal_datemonth', 'int'), GETPOST('disposal_dateday', 'int'), GETPOST('disposal_dateyear', 'int')); // for date without hour, we use gmt
  107. $object->disposal_amount_ht = GETPOST('disposal_amount', 'int');
  108. $object->fk_disposal_type = GETPOST('fk_disposal_type', 'int');
  109. $disposal_invoice_id = GETPOST('disposal_invoice_id', 'int');
  110. $object->disposal_depreciated = ((GETPOST('disposal_depreciated') == '1' || GETPOST('disposal_depreciated') == 'on') ? 1 : 0);
  111. $object->disposal_subject_to_vat = ((GETPOST('disposal_subject_to_vat') == '1' || GETPOST('disposal_subject_to_vat') == 'on') ? 1 : 0);
  112. $result = $object->dispose($user, $disposal_invoice_id);
  113. if ($result < 0) {
  114. setEventMessages($object->error, $object->errors, 'errors');
  115. }
  116. $action = '';
  117. } elseif ($action == "add") {
  118. $object->supplier_invoice_id = GETPOST('supplier_invoice_id', 'int');
  119. }
  120. // Actions cancel, add, update, update_extras, confirm_validate, confirm_delete, confirm_deleteline, confirm_clone, confirm_close, confirm_setdraft, confirm_reopen
  121. include DOL_DOCUMENT_ROOT.'/core/actions_addupdatedelete.inc.php';
  122. // Actions when linking object each other
  123. include DOL_DOCUMENT_ROOT.'/core/actions_dellink.inc.php';
  124. // Actions when printing a doc from card
  125. include DOL_DOCUMENT_ROOT.'/core/actions_printing.inc.php';
  126. // Action to move up and down lines of object
  127. //include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php';
  128. // Action to build doc
  129. include DOL_DOCUMENT_ROOT.'/core/actions_builddoc.inc.php';
  130. // Actions to send emails
  131. $triggersendname = 'ASSET_SENTBYMAIL';
  132. $autocopy = 'MAIN_MAIL_AUTOCOPY_ASSET_TO';
  133. $trackid = 'asset'.$object->id;
  134. include DOL_DOCUMENT_ROOT.'/core/actions_sendmails.inc.php';
  135. }
  136. /*
  137. * View
  138. *
  139. */
  140. $form = new Form($db);
  141. $formfile = new FormFile($db);
  142. $title = $langs->trans("Asset").' - '.$langs->trans("Card");
  143. $help_url = '';
  144. llxHeader('', $title, $help_url);
  145. // Part to create
  146. if ($action == 'create') {
  147. print load_fiche_titre($langs->trans("NewObject", $langs->transnoentitiesnoconv("Asset")), '', 'object_'.$object->picto);
  148. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  149. print '<input type="hidden" name="token" value="'.newToken().'">';
  150. print '<input type="hidden" name="action" value="add">';
  151. if ($backtopage) {
  152. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  153. }
  154. if ($backtopageforcancel) {
  155. print '<input type="hidden" name="backtopageforcancel" value="'.$backtopageforcancel.'">';
  156. }
  157. if (GETPOSTISSET('supplier_invoice_id')) {
  158. $object->fields['supplier_invoice_id'] = array('type' => 'integer:FactureFournisseur:fourn/class/fournisseur.facture.class.php:1:entity IN (__SHARED_ENTITIES__)', 'label' => 'SupplierInvoice', 'enabled' => '1', 'noteditable' => '1', 'position' => 280, 'notnull' => 0, 'visible' => 1, 'index' => 1, 'validate' => '1',);
  159. print '<input type="hidden" name="supplier_invoice_id" value="' . GETPOST('supplier_invoice_id', 'int') . '">';
  160. }
  161. print dol_get_fiche_head(array(), '');
  162. // Set some default values
  163. //if (! GETPOSTISSET('fieldname')) $_POST['fieldname'] = 'myvalue';
  164. print '<table class="border centpercent tableforfieldcreate">'."\n";
  165. // Common attributes
  166. include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_add.tpl.php';
  167. // Other attributes
  168. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_add.tpl.php';
  169. print '</table>'."\n";
  170. print dol_get_fiche_end();
  171. print $form->buttonsSaveCancel("Create");
  172. print '</form>';
  173. //dol_set_focus('input[name="ref"]');
  174. }
  175. // Part to edit record
  176. if (($id || $ref) && $action == 'edit') {
  177. print load_fiche_titre($langs->trans("Asset"), '', 'object_'.$object->picto);
  178. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  179. print '<input type="hidden" name="token" value="'.newToken().'">';
  180. print '<input type="hidden" name="action" value="update">';
  181. print '<input type="hidden" name="id" value="'.$object->id.'">';
  182. if ($backtopage) {
  183. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  184. }
  185. if ($backtopageforcancel) {
  186. print '<input type="hidden" name="backtopageforcancel" value="'.$backtopageforcancel.'">';
  187. }
  188. print dol_get_fiche_head();
  189. print '<table class="border centpercent tableforfieldedit">'."\n";
  190. // Common attributes
  191. include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_edit.tpl.php';
  192. // Other attributes
  193. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_edit.tpl.php';
  194. print '</table>';
  195. print dol_get_fiche_end();
  196. print $form->buttonsSaveCancel();
  197. print '</form>';
  198. }
  199. // Part to show record
  200. if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create'))) {
  201. $res = $object->fetch_optionals();
  202. $head = assetPrepareHead($object);
  203. print dol_get_fiche_head($head, 'card', $langs->trans("Asset"), -1, $object->picto);
  204. $formconfirm = '';
  205. // Confirmation to delete
  206. if ($action == 'delete') {
  207. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteAsset'), $langs->trans('ConfirmDeleteObject'), 'confirm_delete', '', 0, 1);
  208. } elseif ($action == 'disposal') {
  209. // Disposal
  210. $langs->load('bills');
  211. $disposal_date = dol_mktime(12, 0, 0, GETPOST('disposal_datemonth', 'int'), GETPOST('disposal_dateday', 'int'), GETPOST('disposal_dateyear', 'int')); // for date without hour, we use gmt
  212. $disposal_amount = GETPOST('disposal_amount', 'int');
  213. $fk_disposal_type = GETPOST('fk_disposal_type', 'int');
  214. $disposal_invoice_id = GETPOST('disposal_invoice_id', 'int');
  215. $disposal_depreciated = GETPOSTISSET('disposal_depreciated') ? GETPOST('disposal_depreciated') : 1;
  216. $disposal_depreciated = !empty($disposal_depreciated) ? 1 : 0;
  217. $disposal_subject_to_vat = GETPOSTISSET('disposal_subject_to_vat') ? GETPOST('disposal_subject_to_vat') : 1;
  218. $disposal_subject_to_vat = !empty($disposal_subject_to_vat) ? 1 : 0;
  219. $object->fields['fk_disposal_type']['visible'] = 1;
  220. $disposal_type_form = $object->showInputField(null, 'fk_disposal_type', $fk_disposal_type, '', '', '', 0);
  221. $object->fields['fk_disposal_type']['visible'] = -2;
  222. $object->fields['disposal_invoice_id'] = array('type' => 'integer:Facture:compta/facture/class/facture.class.php::entity IN (__SHARED_ENTITIES__)', 'enabled' => '1', 'notnull' => 1, 'visible' => 1, 'index' => 1, 'validate' => '1',);
  223. $disposal_invoice_form = $object->showInputField(null, 'disposal_invoice_id', $disposal_invoice_id, '', '', '', 0);
  224. unset($object->fields['disposal_invoice_id']);
  225. // Create an array for form
  226. $formquestion = array(
  227. array('type' => 'date', 'name' => 'disposal_date', 'tdclass' => 'fieldrequired', 'label' => $langs->trans("AssetDisposalDate"), 'value' => $disposal_date),
  228. array('type' => 'text', 'name' => 'disposal_amount', 'tdclass' => 'fieldrequired', 'label' => $langs->trans("AssetDisposalAmount"), 'value' => $disposal_amount),
  229. array('type' => 'other', 'name' => 'fk_disposal_type', 'tdclass' => 'fieldrequired', 'label' => $langs->trans("AssetDisposalType"), 'value' => $disposal_type_form),
  230. array('type' => 'other', 'name' => 'disposal_invoice_id', 'label' => $langs->trans("InvoiceCustomer"), 'value' => $disposal_invoice_form),
  231. array('type' => 'checkbox', 'name' => 'disposal_depreciated', 'label' => $langs->trans("AssetDisposalDepreciated"), 'value' => $disposal_depreciated),
  232. array('type' => 'checkbox', 'name' => 'disposal_subject_to_vat', 'label' => $langs->trans("AssetDisposalSubjectToVat"), 'value' => $disposal_subject_to_vat),
  233. );
  234. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('AssetDisposal'), $langs->trans('AssetConfirmDisposalAsk', $object->ref . ' - ' . $object->label), 'confirm_disposal', $formquestion, 'yes', 1);
  235. } elseif ($action == 'reopen') {
  236. // Re-open
  237. // Create an array for form
  238. $formquestion = array();
  239. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ReOpen'), $langs->trans('AssetConfirmReOpenAsk', $object->ref), 'confirm_reopen', $formquestion, 'yes', 1);
  240. }
  241. // Clone confirmation
  242. /* elseif ($action == 'clone') {
  243. // Create an array for form
  244. $formquestion = array();
  245. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ToClone'), $langs->trans('ConfirmCloneAsk', $object->ref), 'confirm_clone', $formquestion, 'yes', 1);
  246. }*/
  247. // Call Hook formConfirm
  248. $parameters = array('formConfirm' => $formconfirm);
  249. $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  250. if (empty($reshook)) {
  251. $formconfirm .= $hookmanager->resPrint;
  252. } elseif ($reshook > 0) {
  253. $formconfirm = $hookmanager->resPrint;
  254. }
  255. // Print form confirm
  256. print $formconfirm;
  257. // Object card
  258. // ------------------------------------------------------------
  259. $linkback = '<a href="'.DOL_URL_ROOT.'/asset/list.php?restore_lastsearch_values=1'.(!empty($socid) ? '&socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
  260. $morehtmlref = '<div class="refidno">';
  261. $morehtmlref .= '</div>';
  262. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
  263. print '<div class="fichecenter">';
  264. print '<div class="fichehalfleft">';
  265. print '<div class="underbanner clearboth"></div>';
  266. print '<table class="border centpercent tableforfield">'."\n";
  267. // Common attributes
  268. $keyforbreak='date_acquisition'; // We change column just before this field
  269. //unset($object->fields['fk_project']); // Hide field already shown in banner
  270. //unset($object->fields['fk_soc']); // Hide field already shown in banner
  271. include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_view.tpl.php';
  272. // Other attributes. Fields from hook formObjectOptions and Extrafields.
  273. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
  274. print '</table>';
  275. print '</div>';
  276. print '</div>';
  277. print '<div class="clearboth"></div>';
  278. print dol_get_fiche_end();
  279. // Buttons for actions
  280. if ($action != 'presend' && $action != 'editline') {
  281. print '<div class="tabsAction">' . "\n";
  282. $parameters = array();
  283. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  284. if ($reshook < 0) {
  285. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  286. }
  287. if (empty($reshook)) {
  288. // Send
  289. if (empty($user->socid)) {
  290. print dolGetButtonAction('', $langs->trans('SendMail'), 'default', $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&action=presend&mode=init&token=' . newToken() . '#formmailbeforetitle');
  291. }
  292. if ($object->status == $object::STATUS_DRAFT) {
  293. print dolGetButtonAction($langs->trans('Modify'), '', 'default', $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&action=edit&token=' . newToken(), '', $permissiontoadd);
  294. }
  295. // Clone
  296. //print dolGetButtonAction($langs->trans('ToClone'), '', 'default', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=clone&token=' . newToken(), '', false && $permissiontoadd);
  297. if ($object->status == $object::STATUS_DRAFT) {
  298. print dolGetButtonAction($langs->trans('AssetDisposal'), '', 'default', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=disposal&token=' . newToken(), '', $permissiontoadd);
  299. } else {
  300. print dolGetButtonAction($langs->trans('ReOpen'), '', 'default', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=reopen&token=' . newToken(), '', $permissiontoadd);
  301. }
  302. // Delete (need delete permission, or if draft, just need create/modify permission)
  303. print dolGetButtonAction($langs->trans('Delete'), '', 'delete', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=delete&token=' . newToken(), '', $permissiontodelete || ($object->status == $object::STATUS_DRAFT && $permissiontoadd));
  304. }
  305. print '</div>' . "\n";
  306. }
  307. // Select mail models is same action as presend
  308. if (GETPOST('modelselected')) {
  309. $action = 'presend';
  310. }
  311. if ($action != 'presend') {
  312. print '<div class="fichecenter"><div class="fichehalfleft">';
  313. print '<a name="builddoc"></a>'; // ancre
  314. $includedocgeneration = 0;
  315. // Documents
  316. if ($includedocgeneration) {
  317. $objref = dol_sanitizeFileName($object->ref);
  318. $relativepath = $objref.'/'.$objref.'.pdf';
  319. $filedir = $conf->asset->dir_output.'/'.$objref;
  320. $urlsource = $_SERVER["PHP_SELF"]."?id=".$object->id;
  321. $genallowed = $user->hasRight('asset', 'read'); // If you can read, you can build the PDF to read content
  322. $delallowed = $user->hasRight('asset', 'write'); // If you can create/edit, you can remove a file on card
  323. print $formfile->showdocuments('asset:Asset', $objref, $filedir, $urlsource, $genallowed, $delallowed, $object->model_pdf, 1, 0, 0, 28, 0, '', '', '', $langs->defaultlang);
  324. }
  325. // Show links to link elements
  326. $linktoelem = $form->showLinkToObjectBlock($object, null, array('asset'));
  327. $somethingshown = $form->showLinkedObjectBlock($object, $linktoelem);
  328. print '</div><div class="fichehalfright">';
  329. $morehtmlcenter = '';
  330. $MAXEVENT = 10;
  331. $morehtmlcenter = dolGetButtonTitle($langs->trans('SeeAll'), '', 'fa fa-bars imgforviewmode', DOL_URL_ROOT.'/asset/agenda.php?id='.$object->id);
  332. // List of actions on element
  333. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formactions.class.php';
  334. $formactions = new FormActions($db);
  335. $somethingshown = $formactions->showactions($object, $object->element, 0, 1, '', $MAXEVENT, '', $morehtmlcenter);
  336. print '</div></div>';
  337. }
  338. //Select mail models is same action as presend
  339. if (GETPOST('modelselected')) {
  340. $action = 'presend';
  341. }
  342. // Presend form
  343. $modelmail = 'asset';
  344. $defaulttopic = 'InformationMessage';
  345. $diroutput = $conf->asset->dir_output;
  346. $trackid = 'asset'.$object->id;
  347. include DOL_DOCUMENT_ROOT.'/core/tpl/card_presend.tpl.php';
  348. }
  349. // End of page
  350. llxFooter();
  351. $db->close();