forked from hiqdev/hipanel-module-stock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPart.php
378 lines (333 loc) · 13 KB
/
Part.php
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
<?php
/*
* Stock Module for Hipanel
*
* @link https://github.com/hiqdev/hipanel-module-stock
* @package hipanel-module-stock
* @license BSD-3-Clause
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
*/
namespace hipanel\modules\stock\models;
use hipanel\base\ModelTrait;
use hipanel\helpers\ArrayHelper;
use hipanel\helpers\StringHelper;
use hipanel\models\Ref;
use hipanel\modules\finance\models\Sale;
use hipanel\modules\stock\models\query\PartQuery;
use hiqdev\hiart\ActiveQuery;
use Yii;
/**
* Class Part
*
* @property Model $model
* @property string $currency
* @property-read array $extractedSerials
* @property-read mixed $title
* @property-read ActiveQuery $sale
* @property-read PartWithProfit[] $profit
*/
class Part extends \hipanel\base\Model
{
use ModelTrait;
public const STATE_OK = 'ok';
public const STATE_DELETED = 'deleted';
/** @inheritdoc */
public static $i18nDictionary = 'hipanel:stock';
public function rules()
{
return [
[
[
'model_label',
'model_type_label',
'model_type',
'model_brand',
'model_brand_label',
'src_name',
'src_type',
'src_type_label',
'dst_name',
'dst_type',
'dst_type_label',
'first_move',
'order_name',
'order_data',
'dst_ids',
'model_ids',
'reserve',
'serial',
'serials',
'partno',
'create_time',
'place',
'count',
'move_type',
'move_type_label',
'move_time',
'move_descr',
'move_remote_ticket',
'remote_ticket',
'move_hm_ticket',
'hm_ticket',
'move_remotehands_label',
'remotehands',
'show_deleted',
'show_groups',
'limit',
'descr',
'price',
'currency',
'client',
'supplier',
'type',
'state',
'selling_currency',
'selling_price',
'selling_time',
'buyer',
'order_id',
'order_name',
'company',
'device_location',
'warranty_till',
],
'safe',
],
[['sale_id'], 'integer'],
[['dst_name_in', 'src_name_in'], 'filter', 'filter' => 'trim', 'on' => 'search'],
[
['dst_name_in', 'src_name_in'],
function ($attribute) {
$required = StringHelper::mexplode($this->{$attribute});
$searchParams = [
'limit' => 'all',
'name_inilike' => $this->{$attribute},
'show_deleted' => true,
];
if (str_contains($attribute, 'dst_name')) {
$searchParams['types'] = self::getDestinationSubTypes();
}
$directions = Move::batchPerform('get-directions', $searchParams);
$diff = array_diff($required, ArrayHelper::getColumn($directions, 'name'));
if (!empty($diff)) {
$this->addError(
$attribute,
Yii::t('hipanel:stock', "No {0} were found for: {1}", [
$this->getAttributeLabel($attribute),
implode(', ', $diff)
])
);
}
},
'on' => 'search',
],
[['price'], 'number'],
[['id', 'company_id', 'dst_id', 'model_id', 'client_id', 'buyer_id', 'last_move_id', 'order_id'], 'integer'],
// Create and copy
[['partno', 'src_id', 'dst_id', 'serials', 'move_descr', 'price', 'currency', 'company_id'], 'required', 'on' => ['create', 'copy']],
[['dst_ids'], 'required', 'when' => function ($model) {
return empty($model->dst_id);
}, 'on' => ['create']],
[['serials'], 'unique', 'on' => ['create', 'copy']],
// Move by one
[['id', 'dst_id', 'src_id', 'partno', 'serial'], 'required', 'on' => 'move-by-one'],
// Trash
[['id', 'dst_id', 'src_id', 'partno', 'serial', 'move_descr'], 'required', 'on' => 'trash'],
// Replace
[['id', 'src_id', 'dst_id', 'move_type', 'serial', 'partno'], 'required', 'on' => 'replace'],
[['serial'], 'unique', 'on' => 'replace'],
[
['serial'],
'match',
'pattern' => '/^([a-zA-Z0-9-]+|_)$/u',
'on' => 'replace',
'message' => Yii::t('hipanel:stock', 'The input format must match /^[a-zA-Z0-9-]+$/ or the _ character, which will create a random serial.'),
],
[['disposal_id'], 'string', 'on' => 'replace'],
// Repair
[['id', 'src_id', 'dst_id', 'move_type'], 'required', 'on' => 'repair'],
// Update
[['id', 'model_id', 'serial', 'price', 'currency', 'company_id'], 'required', 'on' => 'update'],
// Move / Bulk-move
[['src_id', 'dst_id', 'type'], 'required', 'on' => 'move'],
// Reserve / Unreserve
[['id'], 'required', 'on' => 'unreserve'],
[['id', 'reserve'], 'required', 'on' => 'reserve'],
// Bulk set price
[['id', 'price'], 'required', 'on' => 'set-price'],
// Set serial
[['id', 'serial'], 'required', 'on' => 'set-serial'],
[['serial'], 'filter', 'filter' => 'trim', 'on' => 'set-serial'],
// Set real serials
[['ids', 'serials'], 'required', 'on' => 'set-real-serials'],
[['serials'], 'validateRealSerials', 'on' => 'set-real-serials'],
// Unique serial for update, set-serial
[['serial'], 'unique', 'on' => ['set-serial', 'update'], 'when' => function ($model) {
if ($model->isAttributeChanged('serial')) {
return static::findOne($model->id)->serial !== $model->serial;
}
return false;
}],
// Update Order No.
[['order_id', 'first_move_id'], 'required', 'on' => 'update-order-no'],
[['id', 'first_move_id'], 'integer', 'on' => 'update-order-no'],
[['order_id'], 'string', 'on' => 'update-order-no'],
// Change model
[['id', 'model_id'], 'required', 'on' => 'change-model'],
// Delete
[['id'], 'required', 'on' => ['delete']],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return $this->mergeAttributeLabels([
'partno' => Yii::t('hipanel:stock', 'Part No.'),
'model_id' => Yii::t('hipanel:stock', 'Part No.'),
'model_brand' => Yii::t('hipanel:stock', 'Manufacturer'),
'model_brands' => Yii::t('hipanel:stock', 'Manufacturers'),
'model_brand_label' => Yii::t('hipanel:stock', 'Manufacturer'),
'model_type' => Yii::t('hipanel', 'Type'),
'model_types' => Yii::t('hipanel', 'Types'),
'model_type_label' => Yii::t('hipanel', 'Type'),
'create_time' => Yii::t('hipanel', 'Created'),
'create_date' => Yii::t('hipanel', 'Created'),
'move_date' => Yii::t('hipanel:stock', 'Moved'),
'move_time' => Yii::t('hipanel:stock', 'Moved'),
'move_type_label' => Yii::t('hipanel:stock', 'Move type'),
'move_descr' => Yii::t('hipanel:stock', 'Move description'),
'move_type' => Yii::t('hipanel:stock', 'Type'),
'order_name' => Yii::t('hipanel:stock', 'Order'),
'order_data' => Yii::t('hipanel:stock', 'Order'),
'src_id' => Yii::t('hipanel:stock', 'Source'),
'src_name' => Yii::t('hipanel:stock', 'Source'),
'dst_id' => Yii::t('hipanel:stock', 'Destination'),
'dst_ids' => Yii::t('hipanel:stock', 'Destination'),
'dst_name' => Yii::t('hipanel:stock', 'Destination'),
'supplier' => Yii::t('hipanel:stock', 'Supplier'),
'currency' => Yii::t('hipanel:stock', 'Currency'),
'dc_ticket' => Yii::t('hipanel:stock', 'DC ticket'),
'company_id' => Yii::t('hipanel:stock', 'Company'),
'buyer' => Yii::t('hipanel:stock', 'Buyer'),
'buyer_id' => Yii::t('hipanel:stock', 'Buyer'),
'selling_currency' => Yii::t('hipanel:stock', 'Selling currency'),
'selling_price' => Yii::t('hipanel:stock', 'Selling price'),
'selling_time' => Yii::t('hipanel:stock', 'Selling time'),
'price' => Yii::t('hipanel:stock', 'Purchase price'),
'order_id' => Yii::t('hipanel:stock', 'Order'),
'device_location' => Yii::t('hipanel:stock', 'DC location'),
'disposal_id' => Yii::t('hipanel:stock', 'Disposal'),
]);
}
public function validateRealSerials(string $attribute): void
{
if (count($this->getExtractedSerials()) !== count($this->ids ?? [])) {
$this->addError($attribute, Yii::t('hipanel:stock', 'Serial numbers should have been put in the same amount as the selected parts'));
}
}
public function transformToSymbols($currencyCodes = [])
{
$result = [];
foreach ($currencyCodes as $code) {
$result[] = StringHelper::getCurrencySymbol($code);
}
return $result;
}
public function getModel()
{
return $this->hasOne(Model::class, ['id' => 'model_id']);
}
public function scenarioActions(): array
{
return [
'repair' => 'move',
'copy' => 'create',
'trash' => 'move',
'move-by-one' => 'move',
'change-model'=> 'update',
];
}
public function isTrashed(): bool
{
return !empty($this->dst_name) && in_array(mb_strtolower($this->dst_name), ['trash', 'trash_rma'], true);
}
public function isDeletable(): bool
{
return $this->first_move_id === $this->last_move_id;
}
public function getProfit(): ActiveQuery
{
return $this->hasMany(PartWithProfit::class, ['id' => 'id'])->indexBy('currency');
}
public function getSale(): ActiveQuery
{
return $this->hasOne(Sale::class, ['id' => 'object_id']);
}
/**
* @param $types array
* @param $scenario string
* @return array
*/
public function filterTypes($types, $scenario)
{
$result = [];
$matches = [
'copy' => ['order', 'direct', 'outdated'],
'trash' => ['died', 'outdated'],
];
if (key_exists($scenario, $matches)) {
foreach ($matches[$scenario] as $key) {
$result[$key] = $types[$key];
}
} else {
$result = $types;
}
return $result;
}
public function getTitle()
{
return sprintf('%s %s %s #%s', Yii::t('hipanel:stock', $this->model_type_label),
Yii::t('hipanel:stock', $this->model_brand_label),
Yii::t('hipanel:stock', $this->partno),
Yii::t('hipanel:stock', $this->serial)
);
}
public static function getDestinationBasicTypes()
{
return array_keys(Ref::getList('type,device'));
}
public static function getDestinationSubTypes($subType = null)
{
$types = Ref::getList("type,device", null, ['with_recursive' => true]);
foreach ($types as $type => $name) {
if (strpos($type, ",") !== false) {
[$base, $type] = explode(",", $type, 2);
$type = self::getRecursiveSubType($type);
$baseSubTypes[$base][] = $type;
}
$subTypes[] = $type;
}
if (empty($subType)) {
return $subTypes;
}
return empty($baseSubTypes[$subType]) ? [] : $baseSubTypes[$subType];
}
public static function getRecursiveSubType($type)
{
if (str_contains($type, ",")) {
$type = explode(",", $type);
return end($type);
}
return $type;
}
public function getExtractedSerials(): array
{
return preg_split("/[\s,;]+/", $this->serials ?? []);
}
public static function find(): PartQuery
{
return new PartQuery(static::class);
}
}