, 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
340 |
--------------------------------------------------------------------------------
/src/NotificationTargetPurchaseRequest.php:
--------------------------------------------------------------------------------
1 | .
19 | --------------------------------------------------------------------------
20 | @package purchaserequest
21 | @author the purchaserequest plugin team
22 | @copyright Copyright (c) 2015-2022 Purchaserequest plugin team
23 | @license GPLv2+
24 | http://www.gnu.org/licenses/gpl.txt
25 | @link https://github.com/InfotelGLPI/purchaserequest
26 | @link http://www.glpi-project.org/
27 | @since 2009
28 | ---------------------------------------------------------------------- */
29 |
30 | namespace GlpiPlugin\Purchaserequest;
31 |
32 | use CommonITILValidation;
33 | use DbUtils;
34 | use Dropdown;
35 | use Html;
36 | use Migration;
37 | use Notification;
38 | use Notification_NotificationTemplate;
39 | use NotificationTarget;
40 | use NotificationTemplate;
41 | use NotificationTemplateTranslation;
42 | use User;
43 |
44 | if (!defined('GLPI_ROOT')) {
45 | die("Sorry. You can't access directly to this file");
46 | }
47 |
48 | // Class NotificationTargetPurchaseRequest
49 | class NotificationTargetPurchaseRequest extends NotificationTarget {
50 | const PURCHASE_VALIDATOR = 30;
51 | const PURCHASE_AUTHOR = 31;
52 |
53 |
54 | public function getEvents() {
55 | return [
56 | 'ask_purchaserequest' => __("Request for validation of the purchase request", "purchaserequest"),
57 | 'no_validation_purchaserequest' => __("Refusal of validation request", "purchaserequest"),
58 | 'validation_purchaserequest' => __("Purchase request validation", "purchaserequest"),
59 | ];
60 | }
61 |
62 | public function addDataForTemplate($event, $options = []) {
63 | global $CFG_GLPI;
64 |
65 | $dbu = new DbUtils();
66 | $events = $this->getAllEvents();
67 |
68 | $this->data['##purchaserequest.action##'] = $events[$event];
69 |
70 | $this->data['##lang.purchaserequest.title##'] = $events[$event];
71 |
72 | $this->data['##lang.purchaserequest.entity##'] = __("Entity");
73 | $this->data['##purchaserequest.entity##'] = Dropdown::getDropdownName('glpi_entities',
74 | $this->obj->getField('entities_id'));
75 |
76 | $this->data['##lang.purchaserequest.name##'] = __("Name");
77 | $this->data['##purchaserequest.name##'] = $this->obj->getField("name");
78 |
79 | $this->data['##lang.purchaserequest.amount##'] = __("Amount", "purchaserequest");
80 | $this->data['##purchaserequest.amount##'] = Dropdown::getValueWithUnit($this->obj->getField("amount"), "€");
81 |
82 | $this->data['##lang.purchaserequest.rebill##'] = __("To be rebilled to the customer", "purchaserequest");
83 | $this->data['##purchaserequest.rebill##'] = Dropdown::getYesNo($this->obj->getField("invoice_customer"));
84 |
85 | $this->data['##lang.purchaserequest.requester##'] = __("Requester");
86 | $this->data['##purchaserequest.requester##'] = getUserName($this->obj->getField('users_id'));
87 |
88 | $this->data['##lang.purchaserequest.group##'] = __("Requester group");
89 | $this->data['##purchaserequest.group##'] = Dropdown::getDropdownName('glpi_groups',
90 | $this->obj->getField('groups_id'));
91 |
92 | $this->data['##lang.purchaserequest.duedate##'] = __("Due date", "purchaserequest");
93 | $this->data['##purchaserequest.duedate##'] = Html::convDate($this->obj->getField("due_date"));
94 |
95 | $this->data['##lang.purchaserequest.comment##'] = __("Description");
96 |
97 | // $comment = Toolbox::stripslashes_deep(str_replace(['\r\n', '\n', '\r'], "
", $this->obj->getField('comment')));
98 | // $comment = html_entity_decode(stripslashes($this->obj->fields['comment']));
99 | $this->data['##purchaserequest.comment##'] = $this->obj->fields['comment'];
100 |
101 | $itemtype = $this->obj->getField("itemtype");
102 |
103 | $this->data['##lang.purchaserequest.itemtype##'] = __("Item type");
104 | if (file_exists(GLPI_ROOT . "/src/" . $itemtype . "Type.php")) {
105 | $this->data['##purchaserequest.itemtype##'] = Dropdown::getDropdownName($dbu->getTableForItemType($itemtype . "Type"),
106 | $this->obj->getField("types_id"));
107 | } else if ($itemtype == "PluginOrderOther") {
108 | $this->data['##purchaserequest.itemtype##'] = $this->obj->getField('othertypename');
109 | }
110 |
111 | $this->data['##lang.purchaserequest.type##'] = __("Type");
112 | $item = new $itemtype();
113 | $this->data['##purchaserequest.type##'] = $item->getTypeName();
114 |
115 | switch ($event) {
116 | case "ask_purchaserequest" :
117 | $this->data['##lang.purchaserequest.users_validation##'] = __("Purchase request validation", "purchaserequest")
118 | . " " . __("By");
119 | break;
120 | case "validation_purchaserequest" :
121 | $this->data['##lang.purchaserequest.users_validation##'] = __("Purchase request is validated", "purchaserequest")
122 | . " " . __("By");
123 | break;
124 | case "no_validation_purchaserequest" :
125 | $this->data['##lang.purchaserequest.users_validation##'] = __("Purchase request canceled", "purchaserequest")
126 | . " " . __("By");
127 | break;
128 |
129 | }
130 | $this->data['##purchaserequest.users_validation##'] = getUserName($this->obj->getField('users_id_validation'));
131 |
132 | $restrict = ['plugin_purchaserequest_purchaserequests_id' => $this->obj->getField("id")];
133 | $dbu = new DbUtils();
134 | $validations = $dbu->getAllDataFromTable('glpi_plugin_purchaserequest_validations', $restrict);
135 | $data['validations'] = [];
136 | if (count($validations)) {
137 | $this->data['##lang.validation.state##'] = _x('item', 'State');
138 | $this->data['##lang.validation.datesubmit##'] = __('Request date');
139 | $this->data['##lang.validation.requester##'] = __('Approval requester');
140 | $this->data['##lang.validation.approver##'] = __('Approver');
141 | $this->data['##lang.validation.comment##'] = __('Approval comments');
142 | $this->data['##lang.validation.datevalidation##'] = __('Approval date');
143 |
144 | $validation = new Validation();
145 | foreach ($validations as $row) {
146 |
147 | $tmp = [];
148 |
149 | $tmp['##validation.state##']
150 | = CommonITILValidation::getStatus($row['status']);
151 | $tmp['##validation.datesubmit##']
152 | = Html::convDateTime($row["submission_date"]);
153 | $tmp['##validation.requester##']
154 | = getUserName($row["users_id"]);
155 | $tmp['##validation.approver##']
156 | = getUserName($row["users_id_validate"]);
157 | $tmp['##validation.comment##']
158 | = $row["comment_validation"];
159 | $tmp['##validation.datevalidation##']
160 | = Html::convDateTime($row["validation_date"]);
161 | $data['validations'][] = $tmp;
162 |
163 | }
164 | $this->data["validations"] = $data["validations"];
165 | }
166 | $this->data['##lang.purchaserequest.url##'] = "URL";
167 |
168 | $url = $CFG_GLPI["url_base"] . "/index.php?redirect=GlpiPlugin\Purchaserequest\Purchaserequest_" . $this->obj->getField("id");
169 | $this->data['##purchaserequest.url##'] = urldecode($url);
170 |
171 | }
172 |
173 | public function getTags() {
174 | $tags = [
175 | 'purchaserequest.name' => __("Name"),
176 | 'purchaserequest.requester' => __("Requester"),
177 | 'purchaserequest.group' => __("Requester group"),
178 | 'purchaserequest.duedate' => __("Due date", "purchaserequest"),
179 | 'purchaserequest.comment' => __("Description"),
180 | 'purchaserequest.itemtype' => __("Item type"),
181 | 'purchaserequest.type' => __("Type"),
182 | 'purchaserequest.amount' => __("Amount", "purchaserequest"),
183 | 'purchaserequest.rebill' => __("To be rebilled to the customer", "purchaserequest"),
184 | 'purchaserequest.users_validation' => __("Editor of validation", "purchaserequest"),
185 | 'validation.state' => _x('item', 'State'),
186 | 'validation.datesubmit' => __('Request date'),
187 | 'validation.requester' => __('Approval requester'),
188 | 'validation.approver' => __('Approver'),
189 | 'validation.comment' => __('Approval comments'),
190 | 'validation.datevalidation' => __('Approval date'),
191 |
192 | ];
193 |
194 | foreach ($tags as $tag => $label) {
195 | $this->addTagToList([
196 | 'tag' => $tag,
197 | 'label' => $label,
198 | 'value' => true,
199 | ]);
200 | }
201 |
202 | //Foreach global tags
203 | $tags = ['validations' => __('Approval')];
204 |
205 | foreach ($tags as $tag => $label) {
206 | $this->addTagToList(['tag' => $tag,
207 | 'label' => $label,
208 | 'value' => false,
209 | 'foreach' => true]);
210 | }
211 | asort($this->tag_descriptions);
212 | }
213 |
214 | public static function install(Migration $migration) {
215 | global $DB;
216 |
217 | $migration->displayMessage("Migrate Purchaserequest notifications");
218 |
219 | $template = new NotificationTemplate();
220 | $templates_id = false;
221 | $query_id = "SELECT `id`
222 | FROM `glpi_notificationtemplates`
223 | WHERE `itemtype`='GlpiPlugin\\Purchaserequest\\PurchaseRequest'
224 | AND `name` = 'Purchase Request Validation'";
225 | $result = $DB->doQuery($query_id) or die ($DB->error());
226 |
227 | if ($DB->numrows($result) > 0) {
228 | $templates_id = $DB->result($result, 0, 'id');
229 | } else {
230 | $tmp = [
231 | 'name' => 'Purchase Request Validation',
232 | 'itemtype' => PurchaseRequest::class,
233 | 'date_mod' => $_SESSION['glpi_currenttime'],
234 | 'comment' => '',
235 | 'css' => '',
236 | ];
237 | $templates_id = $template->add($tmp);
238 | }
239 |
240 | if ($templates_id) {
241 | $translation = new NotificationTemplateTranslation();
242 | $dbu = new DbUtils();
243 | if (!$dbu->countElementsInTable($translation->getTable(), ["notificationtemplates_id" => $templates_id])) {
244 | $tmp['notificationtemplates_id'] = $templates_id;
245 | $tmp['language'] = '';
246 | $tmp['subject'] = '##lang.purchaserequest.title##';
247 | $tmp['content_text'] = '##lang.purchaserequest.url## : ##purchaserequest.url##
248 | ##lang.purchaserequest.entity## : ##purchaserequest.entity##
249 | ##IFpurchaserequest.name####lang.purchaserequest.name## : ##purchaserequest.name##
250 | ##ENDIFpurchaserequest.name##
251 | ##IFpurchaserequest.requester####lang.purchaserequest.requester## : ##purchaserequest.requester##
252 | ##ENDIFpurchaserequest.requester##
253 | ##IFpurchaserequest.group####lang.purchaserequest.group## : ##purchaserequest.group##
254 | ##ENDIFpurchaserequest.group##
255 | ##IFpurchaserequest.due_date####lang.purchaserequest.due_date## : ##purchaserequest.due_date####ENDIFpurchaserequest.due_date##
256 | ##IFpurchaserequest.itemtype####lang.purchaserequest.itemtype## : ##purchaserequest.itemtype####ENDIFpurchaserequest.itemtype##
257 | ##IFpurchaserequest.type####lang.purchaserequest.type## : ##purchaserequest.type####ENDIFpurchaserequest.type##
258 |
259 | ##IFpurchaserequest.comment####lang.purchaserequest.comment## : ##purchaserequest.comment####ENDIFpurchaserequest.comment##';
260 |
261 | $tmp['content_html'] = '<p><strong>##lang.purchaserequest.url##</strong> : ' .
262 | '<a href=\"##purchaserequest.url##\">##purchaserequest.url##</a><br />' .
263 | '<br /><strong>##lang.purchaserequest.entity##</strong> : ##purchaserequest.entity##<br />' .
264 | ' ##IFpurchaserequest.name##<strong>##lang.purchaserequest.name##</strong>' .
265 | ' : ##purchaserequest.name####ENDIFpurchaserequest.name##<br />' .
266 | '##IFpurchaserequest.requester##<strong>##lang.purchaserequest.requester##</strong>' .
267 | ' : ##purchaserequest.requester####ENDIFpurchaserequest.requester##<br />' .
268 | '##IFpurchaserequest.group##<strong>##lang.purchaserequest.group##</strong>' .
269 | ' : ##purchaserequest.group####ENDIFpurchaserequest.group##<br />' .
270 | '##IFpurchaserequest.due_date##<strong>##lang.purchaserequest.due_date##</strong>' .
271 | ' : ##purchaserequest.due_date####ENDIFpurchaserequest.due_date##<br />' .
272 | '##IFpurchaserequest.itemtype##<strong>##lang.purchaserequest.itemtype##</strong>' .
273 | ' : ##purchaserequest.itemtype####ENDIFpurchaserequest.itemtype##<br />' .
274 | '##IFpurchaserequest.type##<strong>##lang.purchaserequest.type##</strong>' .
275 | ' : ##purchaserequest.type####ENDIFpurchaserequest.type##<br /><br />' .
276 | '##IFpurchaserequest.comment##<strong>##lang.purchaserequest.comment##</strong> :' .
277 | '##purchaserequest.comment####ENDIFpurchaserequest.comment##</p>';
278 | $translation->add($tmp);
279 | }
280 |
281 | $notifs = [
282 | 'New Purchase Request Validation' => 'ask_purchaserequest',
283 | 'Confirm Purchase Request Validation' => 'validation_purchaserequest',
284 | 'Cancel Purchase Request Validation' => 'no_validation_purchaserequest',
285 | ];
286 | $notification = new Notification();
287 | $notificationtemplate = new Notification_NotificationTemplate();
288 | foreach ($notifs as $label => $name) {
289 | if (!$dbu->countElementsInTable("glpi_notifications",
290 | ["itemtype" => Purchaserequest::class,
291 | "event" => $name])) {
292 | $tmp = [
293 | 'name' => $label,
294 | 'entities_id' => 0,
295 | 'itemtype' => PurchaseRequest::class,
296 | 'event' => $name,
297 | 'comment' => '',
298 | 'is_recursive' => 1,
299 | 'is_active' => 1,
300 | 'date_mod' => $_SESSION['glpi_currenttime'],
301 | ];
302 | $notification_id = $notification->add($tmp);
303 |
304 | $notificationtemplate->add(['notificationtemplates_id' => $templates_id,
305 | 'mode' => 'mailing',
306 | 'notifications_id' => $notification_id]);
307 | }
308 | }
309 | }
310 |
311 | }
312 |
313 | public static function uninstall() {
314 | global $DB;
315 |
316 | $notif = new Notification();
317 | $options = ['itemtype' => PurchaseRequest::class];
318 | foreach ($DB->request([
319 | 'FROM' => 'glpi_notifications',
320 | 'WHERE' => $options]) as $data) {
321 | $notif->delete($data);
322 | }
323 |
324 | //templates
325 | $template = new NotificationTemplate();
326 | $translation = new NotificationTemplateTranslation();
327 | $notif_template = new Notification_NotificationTemplate();
328 | $options = ['itemtype' => PurchaseRequest::class];
329 | foreach ($DB->request([
330 | 'FROM' => 'glpi_notificationtemplates',
331 | 'WHERE' => $options]) as $data) {
332 | $options_template = [
333 | 'notificationtemplates_id' => $data['id']
334 | ];
335 |
336 | foreach ($DB->request([
337 | 'FROM' => 'glpi_notificationtemplatetranslations',
338 | 'WHERE' => $options_template]) as $data_template) {
339 | $translation->delete($data_template);
340 | }
341 | $template->delete($data);
342 |
343 | foreach ($DB->request([
344 | 'FROM' => 'glpi_notifications_notificationtemplates',
345 | 'WHERE' => $options_template]) as $data_template) {
346 | $notif_template->delete($data_template);
347 | }
348 | }
349 | }
350 |
351 | /**
352 | * Get additionnals targets for Tickets
353 | **/
354 | public function addAdditionalTargets($event = '') {
355 | $this->addTarget(self::PURCHASE_VALIDATOR, __("Validator of the purchase request", "purchaserequest"));
356 | $this->addTarget(self::PURCHASE_AUTHOR, __("Author of the purchase request", "purchaserequest"));
357 |
358 | }
359 |
360 | public function addSpecificTargets($data, $options) {
361 | switch ($data['items_id']) {
362 | case self::PURCHASE_VALIDATOR:
363 | // $this->addUserByField ("users_id_validate");
364 | $this->addValidationApprover($options);
365 | break;
366 | case self::PURCHASE_AUTHOR:
367 | $this->addUserByField("users_id_creator");
368 | break;
369 |
370 | }
371 | }
372 |
373 |
374 | /**
375 | * Add approver related to the ITIL object validation
376 | *
377 | * @param $options array
378 | *
379 | * @return void
380 | */
381 | function addValidationApprover($options = []) {
382 | global $DB;
383 |
384 | if (isset($options['validation_id'])) {
385 | $validationtable = getTableForItemType(Validation::class);
386 |
387 | $criteria = ['LEFT JOIN' => [
388 | User::getTable() => [
389 | 'ON' => [
390 | $validationtable => 'users_id_validate',
391 | User::getTable() => 'id'
392 | ]
393 | ]
394 | ]] + $this->getDistinctUserCriteria() + $this->getProfileJoinCriteria();
395 | $criteria['FROM'] = $validationtable;
396 | $criteria['WHERE']["$validationtable.id"] = $options['validation_id'];
397 |
398 | $iterator = $DB->request($criteria);
399 | foreach ($iterator as $data) {
400 | $this->addToRecipientsList($data);
401 | $iterator->next();
402 | }
403 | }
404 | }
405 | }
406 |
--------------------------------------------------------------------------------
/src/Validation.php:
--------------------------------------------------------------------------------
1 | .
20 | --------------------------------------------------------------------------
21 | @package purchaserequest
22 | @author the purchaserequest plugin team
23 | @copyright Copyright (c) 2015-2022 Purchaserequest plugin team
24 | @license GPLv2+
25 | http://www.gnu.org/licenses/gpl.txt
26 | @link https://github.com/InfotelGLPI/purchaserequest
27 | @link http://www.glpi-project.org/
28 | @since 2009
29 | ---------------------------------------------------------------------- */
30 |
31 | namespace GlpiPlugin\Purchaserequest;
32 |
33 | use Ajax;
34 | use CommonDBTM;
35 | use CommonGLPI;
36 | use CommonITILValidation;
37 | use DbUtils;
38 | use Dropdown;
39 | use Glpi\RichText\RichText;
40 | use Html;
41 | use Log;
42 | use Migration;
43 | use NotificationEvent;
44 | use Plugin;
45 | use PluginOrderOrder;
46 | use PluginOrderReference;
47 | use Session;
48 | use Ticket;
49 | use Toolbox;
50 | use User;
51 |
52 | if (!defined('GLPI_ROOT')) {
53 | die("Sorry. You can't access directly to this file");
54 | }
55 |
56 | /**
57 | * Class Validation
58 | */
59 | class Validation extends CommonDBTM
60 | {
61 | public static $rightname = 'plugin_purchaserequest_validate';
62 | public $dohistory = true;
63 |
64 | public const HISTORY_ADDLINK = 50;
65 | public const HISTORY_DELLINK = 51;
66 |
67 | /**
68 | * @param int $nb
69 | *
70 | * @return string|\translated
71 | */
72 | public static function getTypeName($nb = 0)
73 | {
74 | return _n("Validation", "Validations", $nb, "purchaserequest");
75 | }
76 |
77 | /**
78 | * @return bool
79 | */
80 | public static function canValidation()
81 | {
82 | return Session::haveRight("plugin_purchaserequest_validate", 1);
83 | }
84 |
85 | /**
86 | * @param array $options
87 | *
88 | * @return array
89 | */
90 | public function defineTabs($options = [])
91 | {
92 | $ong = [];
93 | $this->addDefaultFormTab($ong);
94 | return $ong;
95 | }
96 |
97 | /**
98 | * @param CommonGLPI $item
99 | * @param int $withtemplate
100 | *
101 | * @return string|\translated
102 | */
103 | public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
104 | {
105 |
106 | if ($item->getType() == PurchaseRequest::class) {
107 | return self::createTabEntry(__('Approval'));
108 | }
109 |
110 | return '';
111 | }
112 |
113 | public static function getIcon()
114 | {
115 | return "ti ti-check";
116 | }
117 |
118 |
119 | /**
120 | * @param CommonGLPI $item
121 | * @param int $tabnum
122 | * @param int $withtemplate
123 | *
124 | * @return bool
125 | */
126 | public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0)
127 | {
128 |
129 | if ($item->getType() == PurchaseRequest::class) {
130 | self::showValidation($item);
131 | }
132 |
133 | return true;
134 | }
135 |
136 | /**
137 | * @param array|\datas $input
138 | *
139 | * @return array|bool|\datas
140 | */
141 | public function prepareInputForAdd($input)
142 | {
143 |
144 |
145 | $input['status'] = CommonITILValidation::WAITING;
146 |
147 | return $input;
148 | }
149 |
150 | /**
151 | * Prepare input datas for updating the item
152 | *
153 | * @param $input datas used to update the item
154 | *
155 | * @return the modified $input array
156 | **/
157 | public function prepareInputForUpdate($input)
158 | {
159 | global $CFG_GLPI;
160 |
161 | if (isset($input['refuse_purchaserequest']) && $input['refuse_purchaserequest'] == 1) {
162 | $input['status'] = CommonITILValidation::REFUSED;
163 | }
164 |
165 | if (isset($input['accept_purchaserequest']) && $input['accept_purchaserequest'] == 1) {
166 | $input['status'] = CommonITILValidation::ACCEPTED;
167 | }
168 |
169 | if (isset($input['update_status'])) {
170 | $input['validation_date'] = $_SESSION["glpi_currenttime"];
171 | }
172 |
173 | return $input;
174 | }
175 |
176 | /**
177 | * Actions done after the ADD of the item in the database
178 | *
179 | * @return nothing
180 | **/
181 | public function post_addItem()
182 | {
183 | global $CFG_GLPI;
184 |
185 | if ($CFG_GLPI["notifications_mailing"]) {
186 | if (isset($this->input["first"])
187 | && $this->input["first"] == true) {
188 | $purchase_request = new PurchaseRequest();
189 | $purchase_request->getFromDB($this->fields["plugin_purchaserequest_purchaserequests_id"]);
190 | $options = ['validation_id' => $this->fields["id"],
191 | 'validation_status' => $this->fields["status"]];
192 | NotificationEvent::raiseEvent('ask_purchaserequest', $purchase_request, $options);
193 | }
194 | }
195 |
196 | if (isset($this->fields['tickets_id'])) {
197 | $changes[0] = 0;
198 | $changes[1] = '';
199 | $changes[2] = $this->fields["id"];
200 | Log::history(
201 | $this->input['tickets_id'],
202 | 'Ticket',
203 | $changes,
204 | __CLASS__,
205 | Log::HISTORY_PLUGIN + self::HISTORY_ADDLINK
206 | );
207 | }
208 | }
209 |
210 | /**
211 | * Actions done after the UPDATE of the item in the database
212 | *
213 | * @return nothing
214 | **/
215 | public function post_updateItem($history = 1)
216 | {
217 | global $CFG_GLPI;
218 | if (isset($this->oldvalues['tickets_id'])) {
219 | if ($this->oldvalues['tickets_id'] != 0) {
220 | $changes[0] = 0;
221 | $changes[1] = $this->input['id'];
222 | $changes[2] = '';
223 | Log::history(
224 | $this->oldvalues['tickets_id'],
225 | 'Ticket',
226 | $changes,
227 | __CLASS__,
228 | Log::HISTORY_PLUGIN + self::HISTORY_DELLINK
229 | );
230 | }
231 | if (!empty($this->fields['tickets_id'])) {
232 | {
233 | $changes[0] = 0;
234 | $changes[1] = '';
235 | $changes[2] = $this->fields["id"];
236 | Log::history(
237 | $this->fields['tickets_id'],
238 | 'Ticket',
239 | $changes,
240 | __CLASS__,
241 | Log::HISTORY_PLUGIN + self::HISTORY_ADDLINK
242 | );
243 | }
244 | }
245 | }
246 | if (isset($this->input["update_status"])) {
247 | $purchase_request = new PurchaseRequest();
248 | if (isset($this->input['status'])
249 | && $this->input['status'] == CommonITILValidation::REFUSED) {
250 | $input["status"] = CommonITILValidation::REFUSED;
251 | $input["id"] = $this->fields["plugin_purchaserequest_purchaserequests_id"];
252 | $purchase_request->update($input);
253 | } elseif (isset($this->input['status'])
254 | && $this->input['status'] == CommonITILValidation::ACCEPTED) {
255 | $input["id"] = $this->fields["plugin_purchaserequest_purchaserequests_id"];
256 | $items = $this->find(["plugin_purchaserequest_purchaserequests_id" => $this->fields["plugin_purchaserequest_purchaserequests_id"]]);
257 | $validation = true;
258 | foreach ($items as $item) {
259 | if ($item["status"] != CommonITILValidation::ACCEPTED) {
260 | $validation = false;
261 | }
262 | }
263 |
264 | if ($validation == true) {
265 | $input["status"] = CommonITILValidation::ACCEPTED;
266 | $purchase_request->update($input);
267 | }
268 | }
269 | if ($CFG_GLPI["notifications_mailing"]) {
270 | $purchase_request->getFromDB($this->fields["plugin_purchaserequest_purchaserequests_id"]);
271 |
272 | if (count($this->updates)) {
273 | $options = ['validation_id' => $this->fields["id"],
274 | 'validation_status' => $this->fields["status"]];
275 | // NotificationEvent::raiseEvent('validation_answer', $purchase_request, $options);
276 | if (isset($this->input['status'])
277 | && $this->input['status'] == CommonITILValidation::ACCEPTED) {
278 | if ($validation == true && $purchase_request->fields["status"] == CommonITILValidation::ACCEPTED) {
279 | NotificationEvent::raiseEvent('validation_purchaserequest', $purchase_request, $options);
280 | } elseif ($purchase_request->fields["status"] == CommonITILValidation::WAITING) {
281 | $items = $this->find(["plugin_purchaserequest_purchaserequests_id" => $this->fields["plugin_purchaserequest_purchaserequests_id"]]);
282 |
283 | foreach ($items as $item) {
284 | if ($item["status"] == CommonITILValidation::WAITING) {
285 | $options = ['validation_id' => $item["id"],
286 | 'validation_status' => $item["status"]];
287 | NotificationEvent::raiseEvent('ask_purchaserequest', $purchase_request, $options);
288 | }
289 | }
290 | }
291 | } elseif (isset($this->input['status'])
292 | && $this->input['status'] == CommonITILValidation::REFUSED) {
293 | NotificationEvent::raiseEvent('no_validation_purchaserequest', $purchase_request, $options);
294 | }
295 | }
296 | }
297 | }
298 | }
299 |
300 |
301 | /**
302 | * @param $ID
303 | * @param array $options
304 | *
305 | * @return bool
306 | */
307 | public function showForm($ID, $options = [])
308 | {
309 | global $CFG_GLPI;
310 |
311 | $dbu = new DbUtils();
312 | $this->initForm($ID, $options);
313 | $this->showFormHeader($options);
314 |
315 | $canedit = $this->can($ID, UPDATE);
316 | $options['canedit'] = $canedit;
317 |
318 | // Data saved in session
319 | if (isset($_SESSION['glpi_plugin_purchaserequests_fields'])) {
320 | foreach ($_SESSION['glpi_plugin_purchaserequests_fields'] as $key => $value) {
321 | if ($key == "comment") {
322 | $this->fields[$key] = RichText::getEnhancedHtml($value);
323 | } else {
324 | $this->fields[$key] = $value;
325 | }
326 | }
327 | unset($_SESSION['glpi_plugin_purchaserequests_fields']);
328 | }
329 |
330 | /* title */
331 | echo "";
332 | echo "| " . __("Name") . " | ";
333 | if ($canedit) {
334 | echo Html::input('name', ['value' => $this->fields['name'], 'size' => 40]);
335 | } else {
336 | echo $this->fields["name"];
337 | }
338 | echo " | |
";
339 |
340 | echo "";
341 | /* requester */
342 | echo "| " . __("Requester") . " * | ";
343 | if ($canedit) {
344 | $rand_user = User::dropdown(['name' => "users_id",
345 | 'value' => $this->fields["users_id"],
346 | 'entity' => $this->fields["entities_id"],
347 | 'on_change' => "PurchaserequestLoadGroups();",
348 | 'right' => 'all']);
349 | } else {
350 | echo Dropdown::getDropdownName($dbu->getTableForItemType('User'), $this->fields["users_id"]);
351 | }
352 | echo " | ";
353 |
354 | /* requester group */
355 | echo "" . __("Requester group");
356 | echo " | ";
357 |
358 | if ($canedit) {
359 | if ($this->fields['users_id']) {
360 | PurchaseRequest::displayGroup($this->fields['users_id']);
361 | }
362 |
363 | $JS = "function PurchaserequestLoadGroups(){";
364 | $params = ['users_id' => '__VALUE__',
365 | 'entity' => $this->fields["entities_id"]];
366 | $JS .= Ajax::updateItemJsCode(
367 | "plugin_purchaserequest_group",
368 | PLUGIN_PURCHASEREQUEST_WEBDIR . "/ajax/dropdownGroup.php",
369 | $params,
370 | 'dropdown_users_id' . $rand_user,
371 | false
372 | );
373 | $JS .= "}";
374 | echo Html::scriptBlock($JS);
375 | } else {
376 | echo Dropdown::getDropdownName($dbu->getTableForItemType('Group'), $this->fields["groups_id"]);
377 | }
378 | echo " |
";
379 |
380 | /* location */
381 | echo "| " . __("Location") . " | ";
382 | echo "";
383 | Dropdown::show('Location', ['value' => $this->fields["locations_id"],
384 | 'entity' => $this->fields["entities_id"]]);
385 | echo " | ";
386 | echo "" . __("Status") . " | ";
387 | echo "";
388 | Dropdown::show(
389 | PurchaseRequestState::class,
390 | ['value' => $this->fields["plugin_purchaserequest_purchaserequeststates_id"],
391 | 'entity' => $this->fields["entities_id"]]
392 | );
393 | echo " |
";
394 |
395 | /* description */
396 | echo "| " . __("Description") . " * | ";
397 | echo "";
398 | Html::textarea(['name' => 'comment',
399 | 'value' => stripslashes($this->fields['comment']),
400 | 'enable_richtext' => false,
401 | 'cols' => '100',
402 | 'rows' => '4']);
403 | echo " |
";
404 |
405 | /* type */
406 | $reference = new PluginOrderReference();
407 | echo "| " . __("Item type");
408 | echo " * | ";
409 | echo "";
410 | $params = [
411 | 'myname' => 'itemtype',
412 | 'value' => $this->fields["itemtype"],
413 | 'entity' => $_SESSION["glpiactive_entity"],
414 | 'ajax_page' => $CFG_GLPI['root_doc'] . '/plugins/order/ajax/referencespecifications.php',
415 | 'class' => __CLASS__,
416 | ];
417 |
418 | $reference->dropdownAllItems($params);
419 | echo " | ";
420 |
421 | echo "" . __("Type") . " * | ";
422 | echo "";
423 | echo "";
424 | if ($this->fields['itemtype']) {
425 | if ($this->fields['itemtype'] == 'PluginOrderOther') {
426 | $file = 'other';
427 | } else {
428 | $file = $this->fields['itemtype'];
429 | }
430 | $core_typefilename = GLPI_ROOT . "/src/" . $file . "Type.php";
431 | $plugin_typefilename = $CFG_GLPI['root_doc'] . "/plugins/order/inc/" . strtolower($file) . "type.class.php";
432 | $itemtypeclass = $this->fields['itemtype'] . "Type";
433 |
434 | if (file_exists($core_typefilename)
435 | || file_exists($plugin_typefilename)) {
436 | Dropdown::show(
437 | $itemtypeclass,
438 | [
439 | 'name' => "types_id",
440 | 'value' => $this->fields["types_id"],
441 | ]
442 | );
443 | }
444 | }
445 | echo "";
446 | echo " |
";
447 |
448 | echo "| " . __("Due date", "purchaserequest") . " | ";
449 | echo "";
450 | Html::showDateField("due_date", ['value' => $this->fields["due_date"]]);
451 | echo " | ";
452 |
453 | echo "" . __("To be validated by", "purchaserequest") . " * | ";
454 | echo "";
455 | User::dropdown(['name' => "users_id_validate",
456 | 'value' => $this->fields["users_id_validate"],
457 | 'entity' => $this->fields["entities_id"],
458 | 'right' => 'plugin_purchaserequest_validate']);
459 | echo " |
";
460 | echo "| " . __("Amount", "purchaserequest") . " | ";
461 | echo "";
462 | $params = [
463 | 'type' => 'text',
464 | 'value' => number_format($this->fields['amount'], 2, '.', ' '),
465 | ];
466 | echo Html::input('amount', $params);
467 | echo " | ";
468 |
469 | echo "" . __("To be rebilled to the customer", "purchaserequest") . " | ";
470 | echo "";
471 | Html::showCheckbox(['name' => "invoice_customer",
472 | 'checked' => $this->fields["invoice_customer"],
473 | ]);
474 |
475 | echo " | ";
476 | echo "
";
477 | echo "";
478 | $order = new PluginOrderOrder();
479 | echo "| " . __("Linked to the order", "purchaserequest") . " | ";
480 | echo "";
481 |
482 | $options = [];
483 | if ($order->getFromDB($this->fields['plugin_order_orders_id'])) {
484 | $options['value'] = $this->fields['plugin_order_orders_id'];
485 | }
486 | PluginOrderOrder::dropdown($options);
487 | echo " | ";
488 | $ticket = new Ticket();
489 | echo "" . __("Linked to ticket", "purchaserequest") . " | ";
490 | echo "";
491 | $options = [];
492 | if ($ticket->getFromDB($this->fields['tickets_id'])) {
493 | $options['value'] = $this->fields['tickets_id'];
494 | }
495 | $options['entity'] = $this->fields["entities_id"];
496 | Ticket::dropdown($options);
497 | echo " | ";
498 | echo "
";
499 |
500 | if ($ID > 0) {
501 | echo "";
502 |
503 | if ($this->fields['processing_date'] == null) {
504 | echo "| " . __("Treated", "purchaserequest") . " | ";
505 | echo "";
506 | Html::showCheckbox(['name' => 'is_process']);
507 | echo " | ";
508 | echo " | ";
509 | } else {
510 | echo "" . __("Treated on", "purchaserequest");
511 | echo " " . Html::convDateTime($this->fields['processing_date']);
512 | echo " | ";
513 | }
514 | echo "
";
515 | }
516 |
517 | echo Html::hidden('users_id_creator', ['value' => $_SESSION['glpiID']]);
518 |
519 | if ($canedit) {
520 | $this->showFormButtons($options);
521 | } else {
522 | echo "";
523 | Html::closeForm();
524 | }
525 |
526 | return true;
527 | }
528 |
529 |
530 | /**
531 | * Display list of purchase request linked to the order
532 | *
533 | * @param $item
534 | */
535 | public static function showForOrder($item)
536 | {
537 | global $CFG_GLPI;
538 |
539 | $dbu = new DbUtils();
540 | if (isset($_REQUEST["start"])) {
541 | $start = $_REQUEST["start"];
542 | } else {
543 | $start = 0;
544 | }
545 |
546 | $purchase_request = new PurchaseRequest();
547 | $data = $purchase_request->find(['plugin_order_orders_id' => $item->fields['id']]);
548 |
549 | $rows = count($data);
550 |
551 | $canread = Session::haveRight(self::$rightname, READ);
552 |
553 | if (!$rows || !$canread) {
554 | echo __('No item to display');
555 | } else {
556 | $canedit = Session::haveRightsOr(self::$rightname, [CREATE, UPDATE, PURGE]);
557 | $rand = mt_rand();
558 |
559 | echo "";
560 |
561 | Html::printAjaxPager(PurchaseRequest::getTypeName(2), $start, $rows);
562 | echo "