}
if ($exportImportTable) {
- $writeOffs = WriteOffs::find()->select(['items', 'type', 'date'])
- ->where(['between', 'date', $weekStart, $weekEnd])
- ->andWhere(['store_id' => $exportImportTable->export_val])
+ $writeOffs = WriteOffs::find()->alias('wo')->select(['wo.type', 'wo.date', 'wop.product_id', 'wop.quantity'])
+ ->rightJoin('write_offs_products wop', 'wop.write_offs_id = wo.id')
+ ->where(['between', 'wo.date', $weekStart, $weekEnd])
+ ->andWhere(['wo.store_id' => $exportImportTable->export_val])
->asArray()->all();
- $selfCostProduct = SelfCostProduct::find()->select(['price', 'product_guid'])
+ $selfCostProduct = SelfCostProduct::find()->select(['price', 'product_guid, date'])
->where(['between', 'date', $weekStart, $weekEnd])
->andWhere(['store_id' => $store_id])
- ->indexBy('product_guid')
->asArray()->all();
+ $selfCostProductMap = [];
+ foreach ($selfCostProduct as $scp) {
+ $selfCostProductMap[$scp['date']][$scp['product_guid']] = floatval($scp['price']);
+ }
+
foreach (MotivationCostsItem::getWriteOffsItems() as $key) {
$sum = 0;
foreach($writeOffs as $data) {
if (($data['type'] ?? '') != $key) {
continue;
}
- if (!empty($data)) {
- try {
- $json = Json::decode($data['items'] ?? '[]');
+ $sum += ($selfCostProductMap[$data['date']][$data['product_id']] ?? 0) * ($data['quantity'] ?? 0);
+ }
- 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);
- }
+ $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);
}
}
}