}
public static function calculateMonthMaterials($year, $month) {
+ $monthStart = date("Y-m-d 00:00:00", strtotime("$year-$month-01"));
+ $monthEnd = date("Y-m-t 23:59:59", strtotime("$year-$month-01"));
+
$motivations = Motivation::find()
->where(['year' => $year, 'month' => $month])
->all();
foreach ($motivations as $motivation) {
+ /** @var $motivation Motivation */
+
$materials = self::getMotivationValue($motivation->id, 10, self::CODE_CONSUMABLES_SALES_SUPPORT);
+ $consumablesSalesSupport = self::getCostConsumablesSalesSupportByStore($monthStart, $monthEnd, $motivation->store_id);
+
self::saveOrUpdateMotivationValue($motivation->id,
- "fact", self::CODE_CONSUMABLES_SALES_SUPPORT, "float", $materials);
+ "fact", self::CODE_CONSUMABLES_SALES_SUPPORT, "float", $materials + $consumablesSalesSupport);
}
}
return $sum;
}
+ public static function getCostConsumablesSalesSupportByStore($startDate, $endDate, $storeId) {
+ $exportImportTable = ExportImportTable::find()->select(['export_val'])->where(['entity' => 'city_store', 'entity_id' => $storeId, 'export_id' => 1])->one();
+
+ if ($exportImportTable) {
+ $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', $startDate, $endDate])
+ ->andWhere(['wo.store_id' => $exportImportTable->export_val])
+ ->andWhere(['type' => 'Расходные материалы (обеспечение продаж)'])
+ ->asArray()->all();
+
+ $selfCostProduct = SelfCostProduct::find()->select(['price', 'product_guid', 'date'])
+ ->where(['between', 'date', $startDate, $endDate])
+ ->andWhere(['store_id' => $storeId])
+ ->asArray()->all();
+
+ $selfCostProductMap = [];
+ foreach ($selfCostProduct as $scp) {
+ $selfCostProductMap[$scp['date']][$scp['product_guid']] = floatval($scp['price']);
+ }
+
+ $sum = 0;
+ foreach($writeOffs as $data) {
+ $sum += ($selfCostProductMap[date("Y-m-d", strtotime($data['date']))][$data['product_id']] ?? 0)
+ * ($data['quantity'] ?? 0);
+ }
+
+ return $sum;
+ }
+
+ return 0;
+ }
+
/**
* Сохраняет мотивацию по себестоимости товара для указанного магазина и месяца.
*