card.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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) accessforbidden();
  69. if ($user->socid > 0) $socid = $user->socid;
  70. $isdraft = (($object->status == $object::STATUS_DRAFT) ? 1 : 0);
  71. restrictedArea($user, $object->element, $object->id, $object->table_element, '', 'fk_soc', 'rowid', $isdraft);
  72. if (!isModEnabled('asset')) accessforbidden();
  73. if (!$permissiontoread) accessforbidden();
  74. /*
  75. * Actions
  76. */
  77. $parameters = array();
  78. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  79. if ($reshook < 0) {
  80. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  81. }
  82. if (empty($reshook)) {
  83. $error = 0;
  84. $backurlforlist = DOL_URL_ROOT.'/asset/list.php';
  85. if (empty($backtopage) || ($cancel && empty($id))) {
  86. if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
  87. if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
  88. $backtopage = $backurlforlist;
  89. } else {
  90. $backtopage = DOL_URL_ROOT.'/asset/card.php?id='.((!empty($id) && $id > 0) ? $id : '__ID__');
  91. }
  92. }
  93. }
  94. $object->oldcopy = dol_clone($object);
  95. $triggermodname = 'ASSET_MODIFY'; // Name of trigger action code to execute when we modify record
  96. // Action dispose object
  97. if ($action == 'confirm_disposal' && $confirm == 'yes' && $permissiontoadd) {
  98. $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
  99. $object->disposal_amount_ht = GETPOST('disposal_amount', 'int');
  100. $object->fk_disposal_type = GETPOST('fk_disposal_type', 'int');
  101. $disposal_invoice_id = GETPOST('disposal_invoice_id', 'int');
  102. $object->disposal_depreciated = ((GETPOST('disposal_depreciated') == '1' || GETPOST('disposal_depreciated') == 'on') ? 1 : 0);
  103. $object->disposal_subject_to_vat = ((GETPOST('disposal_subject_to_vat') == '1' || GETPOST('disposal_subject_to_vat') == 'on') ? 1 : 0);
  104. $result = $object->dispose($user, $disposal_invoice_id);
  105. if ($result < 0) {
  106. setEventMessages($object->error, $object->errors, 'errors');
  107. }
  108. $action = '';
  109. } elseif ($action == "add") {
  110. $object->supplier_invoice_id = GETPOST('supplier_invoice_id', 'int');
  111. }
  112. // Actions cancel, add, update, update_extras, confirm_validate, confirm_delete, confirm_deleteline, confirm_clone, confirm_close, confirm_setdraft, confirm_reopen
  113. include DOL_DOCUMENT_ROOT.'/core/actions_addupdatedelete.inc.php';
  114. // Actions when linking object each other
  115. include DOL_DOCUMENT_ROOT.'/core/actions_dellink.inc.php';
  116. // Actions when printing a doc from card
  117. include DOL_DOCUMENT_ROOT.'/core/actions_printing.inc.php';
  118. // Action to move up and down lines of object
  119. //include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php';
  120. // Action to build doc
  121. include DOL_DOCUMENT_ROOT.'/core/actions_builddoc.inc.php';
  122. // Actions to send emails
  123. $triggersendname = 'ASSET_SENTBYMAIL';
  124. $autocopy = 'MAIN_MAIL_AUTOCOPY_ASSET_TO';
  125. $trackid = 'asset'.$object->id;
  126. include DOL_DOCUMENT_ROOT.'/core/actions_sendmails.inc.php';
  127. }
  128. /*
  129. * View
  130. *
  131. */
  132. $form = new Form($db);
  133. $formfile = new FormFile($db);
  134. $title = $langs->trans("Asset").' - '.$langs->trans("Card");
  135. $help_url = '';
  136. llxHeader('', $title, $help_url);
  137. // Part to create
  138. if ($action == 'create') {
  139. print load_fiche_titre($langs->trans("NewObject", $langs->transnoentitiesnoconv("Asset")), '', 'object_'.$object->picto);
  140. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  141. print '<input type="hidden" name="token" value="'.newToken().'">';
  142. print '<input type="hidden" name="action" value="add">';
  143. if ($backtopage) {
  144. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  145. }
  146. if ($backtopageforcancel) {
  147. print '<input type="hidden" name="backtopageforcancel" value="'.$backtopageforcancel.'">';
  148. }
  149. if (GETPOSTISSET('supplier_invoice_id')) {
  150. $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',);
  151. print '<input type="hidden" name="supplier_invoice_id" value="' . GETPOST('supplier_invoice_id', 'int') . '">';
  152. }
  153. print dol_get_fiche_head(array(), '');
  154. // Set some default values
  155. //if (! GETPOSTISSET('fieldname')) $_POST['fieldname'] = 'myvalue';
  156. print '<table class="border centpercent tableforfieldcreate">'."\n";
  157. // Common attributes
  158. include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_add.tpl.php';
  159. // Other attributes
  160. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_add.tpl.php';
  161. print '</table>'."\n";
  162. print dol_get_fiche_end();
  163. print $form->buttonsSaveCancel("Create");
  164. print '</form>';
  165. //dol_set_focus('input[name="ref"]');
  166. }
  167. // Part to edit record
  168. if (($id || $ref) && $action == 'edit') {
  169. print load_fiche_titre($langs->trans("Asset"), '', 'object_'.$object->picto);
  170. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  171. print '<input type="hidden" name="token" value="'.newToken().'">';
  172. print '<input type="hidden" name="action" value="update">';
  173. print '<input type="hidden" name="id" value="'.$object->id.'">';
  174. if ($backtopage) {
  175. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  176. }
  177. if ($backtopageforcancel) {
  178. print '<input type="hidden" name="backtopageforcancel" value="'.$backtopageforcancel.'">';
  179. }
  180. print dol_get_fiche_head();
  181. print '<table class="border centpercent tableforfieldedit">'."\n";
  182. // Common attributes
  183. include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_edit.tpl.php';
  184. // Other attributes
  185. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_edit.tpl.php';
  186. print '</table>';
  187. print dol_get_fiche_end();
  188. print $form->buttonsSaveCancel();
  189. print '</form>';
  190. }
  191. // Part to show record
  192. if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create'))) {
  193. $res = $object->fetch_optionals();
  194. $head = assetPrepareHead($object);
  195. print dol_get_fiche_head($head, 'card', $langs->trans("Asset"), -1, $object->picto);
  196. $formconfirm = '';
  197. // Confirmation to delete
  198. if ($action == 'delete') {
  199. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteAsset'), $langs->trans('ConfirmDeleteObject'), 'confirm_delete', '', 0, 1);
  200. } elseif ($action == 'disposal') {
  201. // Disposal
  202. $langs->load('bills');
  203. $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
  204. $disposal_amount = GETPOST('disposal_amount', 'int');
  205. $fk_disposal_type = GETPOST('fk_disposal_type', 'int');
  206. $disposal_invoice_id = GETPOST('disposal_invoice_id', 'int');
  207. $disposal_depreciated = GETPOSTISSET('disposal_depreciated') ? GETPOST('disposal_depreciated') : 1;
  208. $disposal_depreciated = !empty($disposal_depreciated) ? 1 : 0;
  209. $disposal_subject_to_vat = GETPOSTISSET('disposal_subject_to_vat') ? GETPOST('disposal_subject_to_vat') : 1;
  210. $disposal_subject_to_vat = !empty($disposal_subject_to_vat) ? 1 : 0;
  211. $object->fields['fk_disposal_type']['visible'] = 1;
  212. $disposal_type_form = $object->showInputField(null, 'fk_disposal_type', $fk_disposal_type, '', '', '', 0);
  213. $object->fields['fk_disposal_type']['visible'] = -2;
  214. $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',);
  215. $disposal_invoice_form = $object->showInputField(null, 'disposal_invoice_id', $disposal_invoice_id, '', '', '', 0);
  216. unset($object->fields['disposal_invoice_id']);
  217. // Create an array for form
  218. $formquestion = array(
  219. array('type' => 'date', 'name' => 'disposal_date', 'tdclass' => 'fieldrequired', 'label' => $langs->trans("AssetDisposalDate"), 'value' => $disposal_date),
  220. array('type' => 'text', 'name' => 'disposal_amount', 'tdclass' => 'fieldrequired', 'label' => $langs->trans("AssetDisposalAmount"), 'value' => $disposal_amount),
  221. array('type' => 'other', 'name' => 'fk_disposal_type', 'tdclass' => 'fieldrequired', 'label' => $langs->trans("AssetDisposalType"), 'value' => $disposal_type_form),
  222. array('type' => 'other', 'name' => 'disposal_invoice_id', 'label' => $langs->trans("InvoiceCustomer"), 'value' => $disposal_invoice_form),
  223. array('type' => 'checkbox', 'name' => 'disposal_depreciated', 'label' => $langs->trans("AssetDisposalDepreciated"), 'value' => $disposal_depreciated),
  224. array('type' => 'checkbox', 'name' => 'disposal_subject_to_vat', 'label' => $langs->trans("AssetDisposalSubjectToVat"), 'value' => $disposal_subject_to_vat),
  225. );
  226. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('AssetDisposal'), $langs->trans('AssetConfirmDisposalAsk', $object->ref . ' - ' . $object->label), 'confirm_disposal', $formquestion, 'yes', 1);
  227. } elseif ($action == 'reopen') {
  228. // Re-open
  229. // Create an array for form
  230. $formquestion = array();
  231. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ReOpen'), $langs->trans('AssetConfirmReOpenAsk', $object->ref), 'confirm_reopen', $formquestion, 'yes', 1);
  232. }
  233. // Clone confirmation
  234. /* elseif ($action == 'clone') {
  235. // Create an array for form
  236. $formquestion = array();
  237. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ToClone'), $langs->trans('ConfirmCloneAsk', $object->ref), 'confirm_clone', $formquestion, 'yes', 1);
  238. }*/
  239. // Call Hook formConfirm
  240. $parameters = array('formConfirm' => $formconfirm);
  241. $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  242. if (empty($reshook)) {
  243. $formconfirm .= $hookmanager->resPrint;
  244. } elseif ($reshook > 0) {
  245. $formconfirm = $hookmanager->resPrint;
  246. }
  247. // Print form confirm
  248. print $formconfirm;
  249. // Object card
  250. // ------------------------------------------------------------
  251. $linkback = '<a href="'.DOL_URL_ROOT.'/asset/list.php?restore_lastsearch_values=1'.(!empty($socid) ? '&socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
  252. $morehtmlref = '<div class="refidno">';
  253. $morehtmlref .= '</div>';
  254. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
  255. print '<div class="fichecenter">';
  256. print '<div class="fichehalfleft">';
  257. print '<div class="underbanner clearboth"></div>';
  258. print '<table class="border centpercent tableforfield">'."\n";
  259. // Common attributes
  260. $keyforbreak='date_acquisition'; // We change column just before this field
  261. //unset($object->fields['fk_project']); // Hide field already shown in banner
  262. //unset($object->fields['fk_soc']); // Hide field already shown in banner
  263. include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_view.tpl.php';
  264. // Other attributes. Fields from hook formObjectOptions and Extrafields.
  265. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
  266. print '</table>';
  267. print '</div>';
  268. print '</div>';
  269. print '<div class="clearboth"></div>';
  270. print dol_get_fiche_end();
  271. // Buttons for actions
  272. if ($action != 'presend' && $action != 'editline') {
  273. print '<div class="tabsAction">' . "\n";
  274. $parameters = array();
  275. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  276. if ($reshook < 0) {
  277. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  278. }
  279. if (empty($reshook)) {
  280. // Send
  281. if (empty($user->socid)) {
  282. print dolGetButtonAction('', $langs->trans('SendMail'), 'default', $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&action=presend&mode=init&token=' . newToken() . '#formmailbeforetitle');
  283. }
  284. if ($object->status == $object::STATUS_DRAFT) {
  285. print dolGetButtonAction($langs->trans('Modify'), '', 'default', $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&action=edit&token=' . newToken(), '', $permissiontoadd);
  286. }
  287. // Clone
  288. //print dolGetButtonAction($langs->trans('ToClone'), '', 'default', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=clone&token=' . newToken(), '', false && $permissiontoadd);
  289. if ($object->status == $object::STATUS_DRAFT) {
  290. print dolGetButtonAction($langs->trans('AssetDisposal'), '', 'default', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=disposal&token=' . newToken(), '', $permissiontoadd);
  291. } else {
  292. print dolGetButtonAction($langs->trans('ReOpen'), '', 'default', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=reopen&token=' . newToken(), '', $permissiontoadd);
  293. }
  294. // Delete (need delete permission, or if draft, just need create/modify permission)
  295. print dolGetButtonAction($langs->trans('Delete'), '', 'delete', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=delete&token=' . newToken(), '', $permissiontodelete || ($object->status == $object::STATUS_DRAFT && $permissiontoadd));
  296. }
  297. print '</div>' . "\n";
  298. }
  299. // Select mail models is same action as presend
  300. if (GETPOST('modelselected')) {
  301. $action = 'presend';
  302. }
  303. if ($action != 'presend') {
  304. print '<div class="fichecenter"><div class="fichehalfleft">';
  305. print '<a name="builddoc"></a>'; // ancre
  306. $includedocgeneration = 0;
  307. // Documents
  308. if ($includedocgeneration) {
  309. $objref = dol_sanitizeFileName($object->ref);
  310. $relativepath = $objref.'/'.$objref.'.pdf';
  311. $filedir = $conf->asset->dir_output.'/'.$objref;
  312. $urlsource = $_SERVER["PHP_SELF"]."?id=".$object->id;
  313. $genallowed = $user->hasRight('asset', 'read'); // If you can read, you can build the PDF to read content
  314. $delallowed = $user->hasRight('asset', 'write'); // If you can create/edit, you can remove a file on card
  315. print $formfile->showdocuments('asset:Asset', $objref, $filedir, $urlsource, $genallowed, $delallowed, $object->model_pdf, 1, 0, 0, 28, 0, '', '', '', $langs->defaultlang);
  316. }
  317. // Show links to link elements
  318. $linktoelem = $form->showLinkToObjectBlock($object, null, array('asset'));
  319. $somethingshown = $form->showLinkedObjectBlock($object, $linktoelem);
  320. print '</div><div class="fichehalfright">';
  321. $morehtmlcenter = '';
  322. $MAXEVENT = 10;
  323. $morehtmlcenter = dolGetButtonTitle($langs->trans('SeeAll'), '', 'fa fa-bars imgforviewmode', DOL_URL_ROOT.'/asset/agenda.php?id='.$object->id);
  324. // List of actions on element
  325. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formactions.class.php';
  326. $formactions = new FormActions($db);
  327. $somethingshown = $formactions->showactions($object, $object->element, 0, 1, '', $MAXEVENT, '', $morehtmlcenter);
  328. print '</div></div>';
  329. }
  330. //Select mail models is same action as presend
  331. if (GETPOST('modelselected')) {
  332. $action = 'presend';
  333. }
  334. // Presend form
  335. $modelmail = 'asset';
  336. $defaulttopic = 'InformationMessage';
  337. $diroutput = $conf->asset->dir_output;
  338. $trackid = 'asset'.$object->id;
  339. include DOL_DOCUMENT_ROOT.'/core/tpl/card_presend.tpl.php';
  340. }
  341. // End of page
  342. llxFooter();
  343. $db->close();