]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Merge branch 'develop' into feature_smirnov_erp-140_fact_motivation
authorAlexander Smirnov <fredeom@mail.ru>
Wed, 4 Sep 2024 12:12:25 +0000 (15:12 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Wed, 4 Sep 2024 12:12:25 +0000 (15:12 +0300)
1  2 
erp24/services/MotivationService.php

index 322f6dd369496a0b55407dfa77f9dbb33dbe90eb,c1b242221fd6c00264e17c58f5d58ac2a3906e29..d7a92f1721254f6a16b9d58ab92a010deef6fd12
@@@ -1787,48 -1359,78 +1787,111 @@@ class MotivationServic
          $endDate = date('Y-m-d', strtotime($endDate));
  
  
-         $sum = (float)SelfCostProduct::find()
-             ->where(['store_id' => $storeId])
+         $salesRecords = Sales::find()
+             ->select(['id', 'date'])
+             ->where(['store_id' => $storeId, 'operation' => 'Продажа'])
              ->andWhere(['>=', 'date', $startDate])
              ->andWhere(['<=', 'date', $endDate])
-             ->sum('price');
+             ->asArray()
+             ->all();
+         $endDatePlusThreeWeeks = date('Y-m-d', strtotime($endDate . ' +3 weeks'));
+         $returnedSales = Sales::find()
+             ->select(['sales_check'])
+             ->where(['store_id' => $storeId, 'operation' => 'Возврат'])
+             ->andWhere(['>=', 'date', $startDate])
+             ->andWhere(['<=', 'date', $endDatePlusThreeWeeks])  // $endDate + 3 недели
+             ->andWhere(['is not', 'sales_check', null])  // Проверяем, что поле sales_check не пустое
+             ->asArray()
+             ->all();
+         $returnedSalesIds = array_column($returnedSales, 'sales_check');
+         $salesRecords = array_filter($salesRecords, function($sale) use ($returnedSalesIds) {
+             return !in_array($sale['id'], $returnedSalesIds);
+         });
+         // Если нет подходящих чеков, сразу возвращаем 0
+         if (empty($salesRecords)) {
+             return 0.0;
+         }
  
-         return $sum;
+         $salesIds = array_column($salesRecords, 'id');
+         $saleDates = array_column($salesRecords, 'date', 'id');
+         $salesProducts = SalesProducts::find()
+             ->select(['check_id', 'product_id', 'quantity'])
+             ->where(['check_id' => $salesIds])
+             ->asArray()
+             ->all();
+         $productIds = array_unique(array_column($salesProducts, 'product_id'));
+         $selfCosts = SelfCostProduct::find()
+             ->select(['product_guid', 'price', 'date'])
+             ->where(['store_id' => $storeId, 'product_guid' => $productIds])
+             ->andWhere(['date' => array_map(fn($date) => date('Y-m-d', strtotime($date)), $saleDates)])
+             ->asArray()
+             ->all();
+         $selfCostsMap = [];
+         foreach ($selfCosts as $cost) {
+             $selfCostsMap[$cost['product_guid']][$cost['date']] = $cost['price'];
+         }
+         $totalSum = 0.0;
+         foreach ($salesProducts as $product) {
+             $productId = $product['product_id'];
+             $quantity = (float)$product['quantity'];
+             $checkDate = date('Y-m-d', strtotime($saleDates[$product['check_id']]));
+             if (isset($selfCostsMap[$productId][$checkDate])) {
+                 $totalSum += $selfCostsMap[$productId][$checkDate] * $quantity;
+             }
+         }
+         return $totalSum;
      }
  
 +    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;
 +    }
 +
      /**
       * Сохраняет мотивацию по себестоимости товара для указанного магазина и месяца.
       *