namespace yii_app\services;
+use yii\helpers\ArrayHelper;
use yii_app\records\Motivation;
use yii_app\records\MotivationBuh;
use yii_app\records\MotivationBuhValue;
$alias = $validate === 'month' ? 'month' : 'week' . $week;
$motivationValueGroup = MotivationValueGroup::findOne(['alias' => $alias]);
+
+ $storeBuhIds = ArrayHelper::map(StoreGuidBuh::find()->select(['store_guid', 'store_id'])->asArray()->all(), 'store_guid', 'store_id');
+ $motivationCostsItems = ArrayHelper::map(MotivationCostsItem::find()->select(['name', 'code'])->asArray()->all(), 'name', 'code');
+
if (!empty($data['cost_items'])) {
foreach ($data['cost_items'] as $stores) {
foreach ($stores as $storeData) {
- $storeBuh = StoreGuidBuh::find()
- ->andWhere(['store_guid' => $storeData['store_guid']])
- ->one();
+ $storeBuh = array_key_exists($storeData['store_guid'], $storeBuhIds);
if (!$storeBuh) {
LogService::apiErrorLog(json_encode(["error_id" => 45, "error" => 'Несуществующий магазин! Название: ' . $storeData['store_name'] . ' , guid: ' . $storeData['store_guid']], JSON_UNESCAPED_UNICODE));
return;
}
- $store = CityStore::getCityStoreById($storeBuh->store_id, true);
+ $storeId = $storeBuhIds[$storeData['store_guid']];
foreach ($storeData['items'] as $items) {
foreach ($items as $item) {
- $value = MotivationCostsItem::findOne(['name' => $item['name']]);
+ $value = array_key_exists($item['name'], $motivationCostsItems);
if (!$value) {
LogService::apiErrorLog(json_encode(["error_id" => 46, "error" => $item['name']], JSON_UNESCAPED_UNICODE));
+ return;
}
$motivationBuh = MotivationBuh::findOne([
$motivationBuhValue = MotivationBuhValue::findOne([
'motivation_buh_id' => $motivationBuh->id,
- 'store_id' => $store['id'],
+ 'store_id' => $storeId,
'motivation_group_id' => $motivationValueGroup->id,
- 'value_id' => $value->id,
+ 'value_id' => $motivationCostsItems[$storeData['items']],
'value_type' => MotivationCostsItem::DATA_TYPE_FLOAT,
]);
$motivationBuhValue = new MotivationBuhValue();
$motivationBuhValue->setAttributes([
'motivation_buh_id' => $motivationBuh->id,
- 'store_id' => $store['id'],
+ 'store_id' => $storeId,
'motivation_group_id' => $motivationValueGroup->id,
- 'value_id' => $value->id,
+ 'value_id' => $motivationCostsItems[$storeData['items']],
'value_type' => MotivationCostsItem::DATA_TYPE_FLOAT,
'value_float' => $item['summ']
]);