1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\backend\widgets; |
4
|
|
|
|
5
|
|
|
use devgroup\TagDependencyHelper\ActiveRecordHelper; |
6
|
|
|
use Yii; |
7
|
|
|
use app; |
8
|
|
|
use yii\base\InvalidConfigException; |
9
|
|
|
use yii\base\Widget; |
10
|
|
|
use yii\caching\TagDependency; |
11
|
|
|
use app\modules\shop\models\WarehouseProduct; |
12
|
|
|
use app\modules\shop\models\Warehouse; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Widget WarehousesRemains renders input block for specifying product remains on each of active warehouse |
17
|
|
|
* @package app\backend\widgets |
18
|
|
|
*/ |
19
|
|
|
class WarehousesRemains extends Widget |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var app\modules\shop\models\Product Product model |
23
|
|
|
*/ |
24
|
|
|
public $model = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
|
|
public function run() |
30
|
|
|
{ |
31
|
|
|
if ($this->model === null) { |
32
|
|
|
throw new InvalidConfigException("Model should be set for WarehousesRemains widget"); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$state = $this->model->getWarehousesState(); |
36
|
|
|
|
37
|
|
|
$activeWarehousesIds = Warehouse::activeWarehousesIds(); |
38
|
|
|
$remains = []; |
39
|
|
|
foreach ($state as $remain) { |
40
|
|
|
$remains[$remain->warehouse_id] = $remain; |
41
|
|
|
if(($key = array_search($remain->warehouse_id, $activeWarehousesIds)) !== false) { |
42
|
|
|
unset($activeWarehousesIds[$key]); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// if we have new warehouses that not represented in warehouses state |
47
|
|
|
if (count($activeWarehousesIds) > 0) { |
48
|
|
|
foreach ($activeWarehousesIds as $id) { |
49
|
|
|
// create new record with default values |
50
|
|
|
$remain = new WarehouseProduct(); |
51
|
|
|
$remain->warehouse_id = $id; |
52
|
|
|
$remain->product_id = $this->model->id; |
53
|
|
|
$remain->save(); |
54
|
|
|
|
55
|
|
|
// add to remains |
56
|
|
|
$remains[$remain->warehouse_id] = $remain; |
57
|
|
|
} |
58
|
|
|
TagDependency::invalidate(Yii::$app->cache, ActiveRecordHelper::getObjectTag($this->model->className(), $this->model->id)); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this->render( |
62
|
|
|
'warehouses-remains', |
63
|
|
|
[ |
64
|
|
|
'model' => $this->model, |
65
|
|
|
'remains' => $remains, |
66
|
|
|
] |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
} |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.