$writeOffs = WriteOffs::find()->select(['items', 'type'])
->where(['between', 'date', $weekStart, $weekEnd])
->andWhere(['store_id' => $exportImportTable->export_val])
-// ->indexBy('type')
->asArray()->all();
-// var_dump($writeOffs);
+
$selfCostProduct = SelfCostProduct::find()->select(['price', 'product_guid'])
->where(['between', 'date', $weekStart, $weekEnd])
->andWhere(['store_id' => $store_id])
->asArray()->all();
foreach (MotivationCostsItem::getWriteOffsItems() as $key) {
- $data = $writeOffs[$key] ?? null;
- $sum = 0;
- if (!empty($data)) {
- try {
- $json = Json::decode($data['items'] ?? '[]');
-
- foreach ($json as $itemArr) {
- $sum += floatval(($selfCostProduct[$itemArr['product_id']]["price"] ?? 0)) * ($itemArr['quantity'] ?? 0);
-// var_dump($selfCostProduct[$itemArr['product_id']]["price"] ?? 0);
-// var_dump($itemArr['quantity'] ?? 0);
- }
- } catch (\Exception $ex) {}
- }
- $motivationItemType = MotivationCostsItem::writeOffsToMotivationItemMap($key);
- $motivationCostsItem = MotivationCostsItem::find()->where(['name' => $motivationItemType])->one();
- /** @var $motivationCostsItem MotivationCostsItem */
- if ($motivation && $motivationCostsItem) {
- var_dump($sum);
- self::saveOrUpdateMotivationValue($motivation->id, 'week' . $ind, $motivationCostsItem->code,
- MotivationCostsItem::DATA_TYPE_FLOAT, $sum);
+ foreach($writeOffs as $data) {
+ if (($data['type'] ?? '') != $key) {
+ continue;
+ }
+ $sum = 0;
+ if (!empty($data)) {
+ try {
+ $json = Json::decode($data['items'] ?? '[]');
+
+ foreach ($json as $itemArr) {
+ $sum += floatval(($selfCostProduct[$itemArr['product_id']]["price"] ?? 0)) * ($itemArr['quantity'] ?? 0);
+ }
+ } catch (\Exception $ex) {}
+ }
+ $motivationItemType = MotivationCostsItem::writeOffsToMotivationItemMap($key);
+ $motivationCostsItem = MotivationCostsItem::find()->where(['name' => $motivationItemType])->one();
+ /** @var $motivationCostsItem MotivationCostsItem */
+ if ($motivation && $motivationCostsItem) {
+ self::saveOrUpdateMotivationValue($motivation->id, 'week' . $ind, $motivationCostsItem->code,
+ MotivationCostsItem::DATA_TYPE_FLOAT, $sum);
+ }
}
}
}
-// var_dump("-------");
}
-// die;
+
if ($motivation) {
$motivation->save();
if ($motivation->getErrors()) {