Repository navigation
Expand file tree
/
Copy pathhook.php
More file actions
643 lines (586 loc) · 34.1 KB
/
Copy pathhook.php
File metadata and controls
643 lines (586 loc) · 34.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
<?php
/**
* Установка, обновление и удаление схемы плагина storefront.
*
* Принцип: номенклатура, склады-расположения, цены, права и согласования —
* штатные объекты GLPI. Плагин добавляет только витрину, документ заказа,
* количественный учёт и правила.
*/
function plugin_storefront_install(): bool
{
global $DB;
$migration = new Migration(PLUGIN_STOREFRONT_VERSION);
$charset = 'utf8mb4';
$collate = 'utf8mb4_unicode_ci';
/* ======================================================== витрины */
if (!$DB->tableExists('glpi_plugin_storefront_catalogs')) {
$DB->doQuery("CREATE TABLE `glpi_plugin_storefront_catalogs` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) DEFAULT NULL,
`entities_id` INT UNSIGNED NOT NULL DEFAULT 0,
`is_recursive` TINYINT NOT NULL DEFAULT 1,
`is_active` TINYINT NOT NULL DEFAULT 1,
`icon` VARCHAR(60) NOT NULL DEFAULT 'ti ti-package',
`description` TEXT DEFAULT NULL,
`header` TEXT DEFAULT NULL,
-- кто исполняет и кто согласует
`groups_id_fulfil` INT UNSIGNED NOT NULL DEFAULT 0,
`groups_id_approver` INT UNSIGNED NOT NULL DEFAULT 0,
`approval_mode` VARCHAR(20) NOT NULL DEFAULT 'chain',
`min_title_level` INT NOT NULL DEFAULT 60,
-- автосогласование рутинных заказов
`auto_approve_amount` DECIMAL(12,2) NOT NULL DEFAULT 0,
`auto_approve_lines` INT NOT NULL DEFAULT 0,
-- маршрут этапов, если плагин itilflow установлен
`plugin_itilflow_processes_id` INT UNSIGNED NOT NULL DEFAULT 0,
`itilcategories_id` INT UNSIGNED NOT NULL DEFAULT 0,
-- что видит и что может сотрудник
`show_prices` TINYINT NOT NULL DEFAULT 0,
`show_stock` TINYINT NOT NULL DEFAULT 1,
`allow_free_text` TINYINT NOT NULL DEFAULT 1,
`allow_recipient` TINYINT NOT NULL DEFAULT 1,
-- пояснение «зачем» экономит согласующему переписку
`comment_required` TINYINT NOT NULL DEFAULT 1,
`max_lines` INT NOT NULL DEFAULT 0,
`reserve_mode` VARCHAR(20) NOT NULL DEFAULT 'soft',
-- плитка на главной странице самообслуживания
`show_on_home` TINYINT NOT NULL DEFAULT 1,
-- витрина во всю ширину страницы: выбор администратора витрины
`wide_layout` TINYINT NOT NULL DEFAULT 0,
`illustration` VARCHAR(60) NOT NULL DEFAULT 'request-support',
`tiles_id` INT UNSIGNED NOT NULL DEFAULT 0,
-- доска объявлений над карточками: правила приёма, места выдачи
`announcement` TEXT DEFAULT NULL,
`announcement_level` VARCHAR(20) NOT NULL DEFAULT 'info',
`comment` TEXT DEFAULT NULL,
`date_creation` TIMESTAMP NULL DEFAULT NULL,
`date_mod` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`),
KEY `entities_id` (`entities_id`),
KEY `is_recursive` (`is_recursive`),
KEY `is_active` (`is_active`)
) ENGINE=InnoDB DEFAULT CHARSET={$charset} COLLATE={$collate}");
}
/* ======================================================== склады витрины */
if (!$DB->tableExists('glpi_plugin_storefront_warehouses')) {
$DB->doQuery("CREATE TABLE `glpi_plugin_storefront_warehouses` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) DEFAULT NULL,
`entities_id` INT UNSIGNED NOT NULL DEFAULT 0,
`is_recursive` TINYINT NOT NULL DEFAULT 0,
`plugin_storefront_catalogs_id` INT UNSIGNED NOT NULL DEFAULT 0,
`locations_id` INT UNSIGNED NOT NULL DEFAULT 0,
`users_id_tech` INT UNSIGNED NOT NULL DEFAULT 0,
`groups_id_tech` INT UNSIGNED NOT NULL DEFAULT 0,
`is_default` TINYINT NOT NULL DEFAULT 0,
`is_active` TINYINT NOT NULL DEFAULT 1,
`is_pickup` TINYINT NOT NULL DEFAULT 1,
`comment` TEXT DEFAULT NULL,
`date_creation` TIMESTAMP NULL DEFAULT NULL,
`date_mod` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `catalog` (`plugin_storefront_catalogs_id`,`is_active`),
KEY `locations_id` (`locations_id`),
KEY `entities_id` (`entities_id`)
) ENGINE=InnoDB DEFAULT CHARSET={$charset} COLLATE={$collate}");
}
/* ======================================================== позиции витрины */
// Обёртка над штатной номенклатурой: добавляет то, чего в GLPI нет —
// единицу измерения, признак активности в витрине, размер упаковки.
if (!$DB->tableExists('glpi_plugin_storefront_products')) {
$DB->doQuery("CREATE TABLE `glpi_plugin_storefront_products` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) DEFAULT NULL,
`entities_id` INT UNSIGNED NOT NULL DEFAULT 0,
`is_recursive` TINYINT NOT NULL DEFAULT 1,
`plugin_storefront_catalogs_id` INT UNSIGNED NOT NULL DEFAULT 0,
-- ссылка на штатный объект GLPI: ConsumableItem, CartridgeItem,
-- Computer, Monitor, Peripheral и т. п.
`itemtype` VARCHAR(100) NOT NULL DEFAULT 'ConsumableItem',
`items_id` INT UNSIGNED NOT NULL DEFAULT 0,
-- вид учёта: количественный или экземплярный
`tracking` VARCHAR(20) NOT NULL DEFAULT 'quantity',
`unit` VARCHAR(30) NOT NULL DEFAULT __('шт', 'storefront'),
`pack_size` INT NOT NULL DEFAULT 1,
`is_active` TINYINT NOT NULL DEFAULT 1,
-- своё описание витрины: в карточке номенклатуры GLPI лежит
-- служебный комментарий, а сотруднику нужен человеческий текст
`description` TEXT DEFAULT NULL,
-- платная позиция: цену видно даже там, где витрина цены прячет
`is_chargeable` TINYINT NOT NULL DEFAULT 0,
`price` DECIMAL(12,2) NOT NULL DEFAULT 0,
`use_infocom_price` TINYINT NOT NULL DEFAULT 1,
`max_qty` INT NOT NULL DEFAULT 0,
`ranking` INT NOT NULL DEFAULT 0,
`min_title_level` INT NOT NULL DEFAULT 0,
`comment` TEXT DEFAULT NULL,
`date_creation` TIMESTAMP NULL DEFAULT NULL,
`date_mod` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unicity` (`plugin_storefront_catalogs_id`,`itemtype`,`items_id`),
KEY `catalog_active` (`plugin_storefront_catalogs_id`,`is_active`,`ranking`),
KEY `item` (`itemtype`,`items_id`),
KEY `name` (`name`),
KEY `entities_id` (`entities_id`)
) ENGINE=InnoDB DEFAULT CHARSET={$charset} COLLATE={$collate}");
}
/* ======================================================== остатки */
// Агрегат, а не журнал: одна строка на позицию × склад. На 45 тысячах
// пользователей считать остаток суммой движений на каждый показ витрины
// нельзя, поэтому храним готовое число и правим его вместе с движением.
if (!$DB->tableExists('glpi_plugin_storefront_stocks')) {
$DB->doQuery("CREATE TABLE `glpi_plugin_storefront_stocks` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`plugin_storefront_products_id` INT UNSIGNED NOT NULL DEFAULT 0,
`plugin_storefront_warehouses_id` INT UNSIGNED NOT NULL DEFAULT 0,
`entities_id` INT UNSIGNED NOT NULL DEFAULT 0,
`qty_on_hand` INT NOT NULL DEFAULT 0,
`qty_reserved` INT NOT NULL DEFAULT 0,
`threshold` INT NOT NULL DEFAULT 0,
`target` INT NOT NULL DEFAULT 0,
`date_counted` TIMESTAMP NULL DEFAULT NULL,
`date_mod` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unicity` (`plugin_storefront_products_id`,`plugin_storefront_warehouses_id`),
KEY `warehouse` (`plugin_storefront_warehouses_id`),
KEY `low` (`plugin_storefront_warehouses_id`,`qty_on_hand`)
) ENGINE=InnoDB DEFAULT CHARSET={$charset} COLLATE={$collate}");
}
/* ======================================================== движения */
if (!$DB->tableExists('glpi_plugin_storefront_movements')) {
$DB->doQuery("CREATE TABLE `glpi_plugin_storefront_movements` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`entities_id` INT UNSIGNED NOT NULL DEFAULT 0,
`plugin_storefront_products_id` INT UNSIGNED NOT NULL DEFAULT 0,
`plugin_storefront_warehouses_id` INT UNSIGNED NOT NULL DEFAULT 0,
`plugin_storefront_orders_id` INT UNSIGNED NOT NULL DEFAULT 0,
-- in приход | out выдача | adjust инвентаризация | reserve | unreserve
`type` VARCHAR(20) NOT NULL DEFAULT 'in',
`qty` INT NOT NULL DEFAULT 0,
`qty_before` INT NOT NULL DEFAULT 0,
`qty_after` INT NOT NULL DEFAULT 0,
`users_id` INT UNSIGNED NOT NULL DEFAULT 0,
`users_id_recipient` INT UNSIGNED NOT NULL DEFAULT 0,
`groups_id_recipient` INT UNSIGNED NOT NULL DEFAULT 0,
`entities_id_recipient` INT UNSIGNED NOT NULL DEFAULT 0,
`suppliers_id` INT UNSIGNED NOT NULL DEFAULT 0,
`document_no` VARCHAR(100) DEFAULT NULL,
`unit_price` DECIMAL(12,2) NOT NULL DEFAULT 0,
`comment` TEXT DEFAULT NULL,
`date` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `product_date` (`plugin_storefront_products_id`,`date`),
KEY `warehouse_date` (`plugin_storefront_warehouses_id`,`date`),
KEY `order` (`plugin_storefront_orders_id`),
KEY `type_date` (`type`,`date`),
KEY `recipient_date` (`users_id_recipient`,`date`),
KEY `group_recipient_date` (`groups_id_recipient`,`date`),
KEY `entity_recipient_date` (`entities_id_recipient`,`date`)
) ENGINE=InnoDB DEFAULT CHARSET={$charset} COLLATE={$collate}");
}
/* ======================================================== заказы */
if (!$DB->tableExists('glpi_plugin_storefront_orders')) {
$DB->doQuery("CREATE TABLE `glpi_plugin_storefront_orders` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) DEFAULT NULL,
`entities_id` INT UNSIGNED NOT NULL DEFAULT 0,
`is_recursive` TINYINT NOT NULL DEFAULT 0,
`plugin_storefront_catalogs_id` INT UNSIGNED NOT NULL DEFAULT 0,
`plugin_storefront_warehouses_id` INT UNSIGNED NOT NULL DEFAULT 0,
-- связанная заявка GLPI
`itemtype` VARCHAR(100) NOT NULL DEFAULT 'Ticket',
`items_id` INT UNSIGNED NOT NULL DEFAULT 0,
`users_id_requester` INT UNSIGNED NOT NULL DEFAULT 0,
-- для кого заказ: self себе | user сотруднику | group отделу | entity подразделению
`recipient_type` VARCHAR(20) NOT NULL DEFAULT 'self',
`users_id_recipient` INT UNSIGNED NOT NULL DEFAULT 0,
`groups_id_recipient` INT UNSIGNED NOT NULL DEFAULT 0,
`entities_id_recipient` INT UNSIGNED NOT NULL DEFAULT 0,
`recipient_note` VARCHAR(255) DEFAULT NULL,
-- из какого набора собран заказ: по нему считается «выдан ли
-- стартовый набор этому человеку»
`plugin_storefront_kits_id` INT UNSIGNED NOT NULL DEFAULT 0,
-- draft | approval | queue | approved | ready | issued | cancelled | rejected
`state` VARCHAR(20) NOT NULL DEFAULT 'draft',
`users_id_approver` INT UNSIGNED NOT NULL DEFAULT 0,
`approval_source` VARCHAR(20) DEFAULT NULL,
`approval_comment` TEXT DEFAULT NULL,
`is_auto_approved` TINYINT NOT NULL DEFAULT 0,
`lines_count` INT NOT NULL DEFAULT 0,
`qty_requested` INT NOT NULL DEFAULT 0,
`qty_approved` INT NOT NULL DEFAULT 0,
`qty_issued` INT NOT NULL DEFAULT 0,
`amount` DECIMAL(12,2) NOT NULL DEFAULT 0,
`waybill_no` VARCHAR(60) DEFAULT NULL,
`date_submitted` TIMESTAMP NULL DEFAULT NULL,
`date_approved` TIMESTAMP NULL DEFAULT NULL,
`date_issued` TIMESTAMP NULL DEFAULT NULL,
`comment` TEXT DEFAULT NULL,
`date_creation` TIMESTAMP NULL DEFAULT NULL,
`date_mod` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `state_date` (`state`,`date_creation`),
KEY `requester_state` (`users_id_requester`,`state`),
KEY `recipient` (`users_id_recipient`),
KEY `catalog_state` (`plugin_storefront_catalogs_id`,`state`),
KEY `item` (`itemtype`,`items_id`),
KEY `entities_id` (`entities_id`),
KEY `date_issued` (`date_issued`)
) ENGINE=InnoDB DEFAULT CHARSET={$charset} COLLATE={$collate}");
}
/* ======================================================== строки заказа */
if (!$DB->tableExists('glpi_plugin_storefront_orderitems')) {
$DB->doQuery("CREATE TABLE `glpi_plugin_storefront_orderitems` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`plugin_storefront_orders_id` INT UNSIGNED NOT NULL DEFAULT 0,
`plugin_storefront_products_id` INT UNSIGNED NOT NULL DEFAULT 0,
-- снимок на момент заказа: номенклатуру могут переименовать
`itemtype` VARCHAR(100) DEFAULT NULL,
`items_id` INT UNSIGNED NOT NULL DEFAULT 0,
`name_snapshot` VARCHAR(255) DEFAULT NULL,
`unit_snapshot` VARCHAR(30) DEFAULT NULL,
`price_snapshot` DECIMAL(12,2) NOT NULL DEFAULT 0,
`qty_requested` INT NOT NULL DEFAULT 0,
`qty_approved` INT NOT NULL DEFAULT 0,
`qty_issued` INT NOT NULL DEFAULT 0,
-- сколько единиц этот заказ сейчас держит в резерве склада:
-- единственный источник правды, чтобы снятие резерва не гадало
`qty_reserved` INT NOT NULL DEFAULT 0,
`change_reason` TEXT DEFAULT NULL,
-- позиция «нет в каталоге»
`is_free_text` TINYINT NOT NULL DEFAULT 0,
`free_text` TEXT DEFAULT NULL,
-- инвентарные номера при экземплярной выдаче
`issued_items` TEXT DEFAULT NULL,
`ranking` INT NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
KEY `order_rank` (`plugin_storefront_orders_id`,`ranking`),
KEY `product` (`plugin_storefront_products_id`)
) ENGINE=InnoDB DEFAULT CHARSET={$charset} COLLATE={$collate}");
}
/* ======================================================== лимиты */
if (!$DB->tableExists('glpi_plugin_storefront_limits')) {
$DB->doQuery("CREATE TABLE `glpi_plugin_storefront_limits` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) DEFAULT NULL,
`entities_id` INT UNSIGNED NOT NULL DEFAULT 0,
`is_recursive` TINYINT NOT NULL DEFAULT 1,
`plugin_storefront_catalogs_id` INT UNSIGNED NOT NULL DEFAULT 0,
`is_active` TINYINT NOT NULL DEFAULT 1,
-- на кого: all | user | group | entity | title
`scope` VARCHAR(20) NOT NULL DEFAULT 'all',
`scope_items_id` INT UNSIGNED NOT NULL DEFAULT 0,
-- чья норма: each — у каждого своя, total — одна на всю область
`scope_mode` VARCHAR(10) NOT NULL DEFAULT 'each',
-- на что: catalog | category | product
`target` VARCHAR(20) NOT NULL DEFAULT 'product',
`target_items_id` INT UNSIGNED NOT NULL DEFAULT 0,
`period` VARCHAR(20) NOT NULL DEFAULT 'month',
`max_qty` INT NOT NULL DEFAULT 0,
`max_amount` DECIMAL(12,2) NOT NULL DEFAULT 0,
`is_hard` TINYINT NOT NULL DEFAULT 0,
`comment` TEXT DEFAULT NULL,
`date_creation` TIMESTAMP NULL DEFAULT NULL,
`date_mod` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `catalog_active` (`plugin_storefront_catalogs_id`,`is_active`),
KEY `scope` (`scope`,`scope_items_id`),
KEY `target` (`target`,`target_items_id`)
) ENGINE=InnoDB DEFAULT CHARSET={$charset} COLLATE={$collate}");
}
/* ======================================================== уровни должностей */
// На проде 916 должностей и 77 % заполненности. Размечать руками нельзя,
// поэтому уровень выводится из названия шаблоном, а здесь хранятся
// результат и правки администратора.
if (!$DB->tableExists('glpi_plugin_storefront_titlelevels')) {
$DB->doQuery("CREATE TABLE `glpi_plugin_storefront_titlelevels` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`usertitles_id` INT UNSIGNED NOT NULL DEFAULT 0,
`level` INT NOT NULL DEFAULT 30,
`is_manual` TINYINT NOT NULL DEFAULT 0,
`can_approve` TINYINT NOT NULL DEFAULT 0,
`date_mod` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `usertitles_id` (`usertitles_id`),
KEY `level` (`level`)
) ENGINE=InnoDB DEFAULT CHARSET={$charset} COLLATE={$collate}");
}
/* ======================================================== наборы */
if (!$DB->tableExists('glpi_plugin_storefront_kits')) {
$DB->doQuery("CREATE TABLE `glpi_plugin_storefront_kits` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) DEFAULT NULL,
`entities_id` INT UNSIGNED NOT NULL DEFAULT 0,
`is_recursive` TINYINT NOT NULL DEFAULT 1,
`plugin_storefront_catalogs_id` INT UNSIGNED NOT NULL DEFAULT 0,
`is_active` TINYINT NOT NULL DEFAULT 1,
-- стартовый набор: выдаётся один раз, потом исчезает у сотрудника
`is_once` TINYINT NOT NULL DEFAULT 0,
`icon` VARCHAR(60) NOT NULL DEFAULT 'ti ti-briefcase',
`min_title_level` INT NOT NULL DEFAULT 0,
`comment` TEXT DEFAULT NULL,
`date_creation` TIMESTAMP NULL DEFAULT NULL,
`date_mod` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `catalog_active` (`plugin_storefront_catalogs_id`,`is_active`)
) ENGINE=InnoDB DEFAULT CHARSET={$charset} COLLATE={$collate}");
}
if (!$DB->tableExists('glpi_plugin_storefront_kititems')) {
$DB->doQuery("CREATE TABLE `glpi_plugin_storefront_kititems` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`plugin_storefront_kits_id` INT UNSIGNED NOT NULL DEFAULT 0,
`plugin_storefront_products_id` INT UNSIGNED NOT NULL DEFAULT 0,
`qty` INT NOT NULL DEFAULT 1,
`ranking` INT NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `unicity` (`plugin_storefront_kits_id`,`plugin_storefront_products_id`),
KEY `kit_rank` (`plugin_storefront_kits_id`,`ranking`)
) ENGINE=InnoDB DEFAULT CHARSET={$charset} COLLATE={$collate}");
}
/* ======================================================== оценки позиций */
// Оценку ставит только тот, кто позицию получал: иначе это не отзыв
// о вещи, а голосование за ассортимент.
if (!$DB->tableExists('glpi_plugin_storefront_ratings')) {
$DB->doQuery("CREATE TABLE `glpi_plugin_storefront_ratings` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`plugin_storefront_products_id` INT UNSIGNED NOT NULL DEFAULT 0,
`users_id` INT UNSIGNED NOT NULL DEFAULT 0,
`stars` TINYINT NOT NULL DEFAULT 0,
`comment` TEXT DEFAULT NULL,
`date_creation` TIMESTAMP NULL DEFAULT NULL,
`date_mod` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unicity` (`plugin_storefront_products_id`,`users_id`),
KEY `product_stars` (`plugin_storefront_products_id`,`stars`)
) ENGINE=InnoDB DEFAULT CHARSET={$charset} COLLATE={$collate}");
}
/* ======================================================== разрешения на повтор набора */
// Разовый набор иногда нужен повторно: перевод в другое подразделение,
// утрата. Разрешение выдаёт администратор и оно гасится при выдаче.
if (!$DB->tableExists('glpi_plugin_storefront_kitgrants')) {
$DB->doQuery("CREATE TABLE `glpi_plugin_storefront_kitgrants` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`plugin_storefront_kits_id` INT UNSIGNED NOT NULL DEFAULT 0,
`users_id` INT UNSIGNED NOT NULL DEFAULT 0,
`users_id_author` INT UNSIGNED NOT NULL DEFAULT 0,
`is_used` TINYINT NOT NULL DEFAULT 0,
`reason` VARCHAR(255) DEFAULT NULL,
`date_creation` TIMESTAMP NULL DEFAULT NULL,
`date_mod` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `kit_user` (`plugin_storefront_kits_id`,`users_id`,`is_used`)
) ENGINE=InnoDB DEFAULT CHARSET={$charset} COLLATE={$collate}");
}
/* ======================================================== корзина */
// Черновик заказа держим в своей таблице, а не в сессии: сотрудник должен
// иметь возможность собрать корзину с телефона и оформить с компьютера.
if (!$DB->tableExists('glpi_plugin_storefront_cartitems')) {
$DB->doQuery("CREATE TABLE `glpi_plugin_storefront_cartitems` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`users_id` INT UNSIGNED NOT NULL DEFAULT 0,
`plugin_storefront_catalogs_id` INT UNSIGNED NOT NULL DEFAULT 0,
`plugin_storefront_products_id` INT UNSIGNED NOT NULL DEFAULT 0,
-- из какого набора строка: заказ унаследует это, и разовый набор
-- отметится выданным. В сессии такую метку держать нельзя —
-- корзину собирают с телефона, а оформляют с компьютера.
`plugin_storefront_kits_id` INT UNSIGNED NOT NULL DEFAULT 0,
`qty` INT NOT NULL DEFAULT 1,
`is_free_text` TINYINT NOT NULL DEFAULT 0,
`free_text` TEXT DEFAULT NULL,
`date_mod` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `user_catalog` (`users_id`,`plugin_storefront_catalogs_id`),
KEY `product` (`plugin_storefront_products_id`)
) ENGINE=InnoDB DEFAULT CHARSET={$charset} COLLATE={$collate}");
}
/* ======================================================== права и cron */
// обновление уже установленных копий
$migration->addField('glpi_plugin_storefront_orders', 'recipient_type', 'string',
['value' => 'self']);
// Внешние ключи заводим типом fkey: GLPI требует беззнаковые целые
// и предупреждает при обновлении, если тип обычный integer.
$migration->addField('glpi_plugin_storefront_orders', 'entities_id_recipient', 'fkey');
$migration->addField('glpi_plugin_storefront_orders', 'recipient_note', 'string',
['value' => null]);
$migration->addField('glpi_plugin_storefront_movements', 'groups_id_recipient', 'fkey');
$migration->addField('glpi_plugin_storefront_movements', 'entities_id_recipient', 'fkey');
$migration->addField('glpi_plugin_storefront_orderitems', 'qty_reserved', 'integer',
['value' => 0]);
$migration->addKey('glpi_plugin_storefront_movements',
['groups_id_recipient', 'date'], 'group_recipient_date');
$migration->addKey('glpi_plugin_storefront_movements',
['entities_id_recipient', 'date'], 'entity_recipient_date');
$migration->addField('glpi_plugin_storefront_catalogs', 'show_on_home', 'bool', ['value' => 1]);
$migration->addField('glpi_plugin_storefront_catalogs', 'illustration', 'string',
['value' => 'request-support']);
$migration->addField('glpi_plugin_storefront_catalogs', 'tiles_id', 'fkey');
// Витрина организации видна только внутри неё и ниже: наследование в GLPI
// идёт вниз. Сотрудник самообслуживания организацию не переключает и
// работает из корня, поэтому витрину нужно уметь публиковать вверх.
// Печатная накладная: реквизиты и вид документа настраиваются на витрине,
// потому что у разных складов свои бланки и свои подписанты.
$migration->addField('glpi_plugin_storefront_catalogs', 'waybill_org', 'string');
$migration->addField('glpi_plugin_storefront_catalogs', 'waybill_title', 'string');
$migration->addField('glpi_plugin_storefront_catalogs', 'waybill_footer', 'text');
$migration->addField('glpi_plugin_storefront_catalogs', 'waybill_signatory', 'string');
$migration->addField('glpi_plugin_storefront_catalogs', 'waybill_show_prices', 'bool',
['value' => 1]);
$migration->addField('glpi_plugin_storefront_catalogs', 'waybill_show_requested', 'bool',
['value' => 1]);
// Норма лимита: у каждого своя или одна общая на отдел, подразделение,
// должность или витрину целиком. Умолчание сохраняет прежнее поведение.
$migration->addField('glpi_plugin_storefront_catalogs', 'wide_layout', 'bool',
['value' => 0, 'after' => 'show_on_home']);
$migration->addField('glpi_plugin_storefront_limits', 'scope_mode', 'string',
['value' => 'each', 'after' => 'scope_items_id']);
$migration->addField('glpi_plugin_storefront_catalogs', 'show_to_parents', 'bool',
['value' => 0]);
$migration->addField('glpi_plugin_storefront_catalogs', 'require_approver', 'bool',
['value' => 0]);
$migration->addField('glpi_plugin_storefront_catalogs', 'close_on_reject', 'bool',
['value' => 1]);
$migration->addField('glpi_plugin_storefront_catalogs', 'comment_required', 'bool',
['value' => 1]);
$migration->addField('glpi_plugin_storefront_catalogs', 'announcement', 'text');
$migration->addField('glpi_plugin_storefront_catalogs', 'announcement_level', 'string',
['value' => 'info']);
$migration->addField('glpi_plugin_storefront_products', 'description', 'text');
$migration->addField('glpi_plugin_storefront_products', 'is_chargeable', 'bool',
['value' => 0]);
$migration->addField('glpi_plugin_storefront_kits', 'is_once', 'bool', ['value' => 0]);
$migration->addField('glpi_plugin_storefront_orders', 'plugin_storefront_kits_id', 'fkey');
$migration->addField('glpi_plugin_storefront_cartitems', 'plugin_storefront_kits_id', 'fkey');
// Копии, обновлённые ранней 0.3.0, получили эти поля знаковыми — правим.
foreach ([
'glpi_plugin_storefront_orders' => ['entities_id_recipient'],
'glpi_plugin_storefront_movements' => ['groups_id_recipient', 'entities_id_recipient'],
'glpi_plugin_storefront_catalogs' => ['tiles_id'],
] as $table => $fields) {
foreach ($fields as $field) {
$migration->changeField($table, $field, $field, 'fkey');
}
}
$migration->addRight('plugin_storefront_catalog', ALLSTANDARDRIGHT);
$migration->addRight('plugin_storefront_order', READ | UPDATE);
$migration->addRight('plugin_storefront_stock', READ | UPDATE);
$migration->executeMigration();
CronTask::register(
'GlpiPlugin\\Storefront\\Cron',
'storefront_lowstock',
86400,
[
'comment' => __('Оповещение об остатках ниже порога и расчёт потребности к закупке', 'storefront'),
'mode' => CronTask::MODE_EXTERNAL,
'state' => CronTask::STATE_WAITING,
]
);
CronTask::register(
'GlpiPlugin\\Storefront\\Cron',
'storefront_cartcleanup',
604800,
[
'comment' => __('Очистка брошенных корзин старше 30 дней', 'storefront'),
'mode' => CronTask::MODE_EXTERNAL,
'state' => CronTask::STATE_WAITING,
]
);
CronTask::register(
'GlpiPlugin\\Storefront\\Cron',
'storefront_reserves',
86400,
[
'comment' => __('Возврат в оборот резерва, который не держит ни один заказ', 'storefront'),
'mode' => CronTask::MODE_EXTERNAL,
'state' => CronTask::STATE_WAITING,
]
);
// Шаблоны формы витрины GLPI держит скомпилированными и на рабочем
// контуре их не перечитывает. Установка плагина сбрасывает только кеш
// переводов, поэтому после обновления файлов форма отдавалась бы прежней,
// без новых полей. Убираем скомпилированные шаблоны сами.
plugin_storefront_clearTemplateCache();
return true;
}
/**
* Плагин включают: обновляем скомпилированные шаблоны.
*
* GLPI вызывает эту функцию при каждом включении плагина. Так форма витрины
* обновляется и тогда, когда файлы заменили на ту же версию: миграция в этом
* случае не выполняется, а «выключить и включить» помогает.
*/
function plugin_storefront_activate(): void
{
plugin_storefront_clearTemplateCache();
}
/**
* Сброс скомпилированных шаблонов Twig.
*
* Вызывается из установки и обновления плагина. Ошибка здесь не должна
* ломать установку: в худшем случае кеш чистится командой
* «php bin/console cache:clear».
*
* @return bool удалось ли сбросить кеш
*/
function plugin_storefront_clearTemplateCache(): bool
{
try {
$dir = \Glpi\Kernel\Kernel::getCacheRootDir() . '/templates';
if (!is_dir($dir)) {
return false;
}
$done = false;
foreach ((array) glob($dir . '/*/*.php') as $file) {
if (is_file($file) && @unlink($file)) {
$done = true;
}
}
foreach ((array) glob($dir . '/*', GLOB_ONLYDIR) as $sub) {
@rmdir($sub);
}
return $done;
} catch (\Throwable $e) {
return false;
}
}
function plugin_storefront_uninstall(): bool
{
global $DB;
// История выдач — учётные данные, они не удаляются вместе с плагином.
// Справочники переименовываются с суффиксом даты, движения и заказы остаются.
$keep = [
'glpi_plugin_storefront_orders',
'glpi_plugin_storefront_orderitems',
'glpi_plugin_storefront_movements',
'glpi_plugin_storefront_stocks',
];
$rename = [
'glpi_plugin_storefront_catalogs',
'glpi_plugin_storefront_warehouses',
'glpi_plugin_storefront_products',
'glpi_plugin_storefront_limits',
'glpi_plugin_storefront_kits',
'glpi_plugin_storefront_kititems',
'glpi_plugin_storefront_kitgrants',
'glpi_plugin_storefront_ratings',
'glpi_plugin_storefront_titlelevels',
];
$drop = ['glpi_plugin_storefront_cartitems'];
foreach ($rename as $t) {
if ($DB->tableExists($t)) {
$DB->doQuery("RENAME TABLE `{$t}` TO `{$t}_backup_" . date('Ymd') . "`");
}
}
foreach ($drop as $t) {
if ($DB->tableExists($t)) {
$DB->doQuery("DROP TABLE `{$t}`");
}
}
unset($keep);
ProfileRight::deleteProfileRights([
'plugin_storefront_catalog',
'plugin_storefront_order',
'plugin_storefront_stock',
]);
return true;
}