use yii\db\Expression;
use yii\db\Query;
use yii\helpers\ArrayHelper;
+use yii_app\records\BouquetComposition;
use yii_app\records\CityStore;
+use yii_app\records\CityStoreParams;
use yii_app\records\PricesDynamic;
+use yii_app\records\Products1c;
use yii_app\records\Products1cNomenclature;
+use yii_app\records\Sales;
use yii_app\records\SalesProducts;
use yii_app\records\SalesWriteOffsPlan;
/**
* @param array|int $storeIds
- * @param string $dateFrom формат 'Y-m-d H:i:s'
- * @param string $dateTo формат 'Y-m-d H:i:s'
+ * @param int $month
+ * @param int $year
* @param int $regionId
* @return array список строк с полями:
* sale_id, sale_date, product_id, product_name,
* component_guid, component_name, component_category,
* quantity, price, cost
*/
- public function getUnmarkedProductsComponents(array|int $storeIds, string $dateFrom, string $dateTo, int $regionId = 52, $typeFilter = null): array
+ public function getUnmarkedProductsComponents(int $storeId, string $month, string $year, int $regionId = null, $typeFilter = null): array
{
- $salesProducts = SalesProducts::find()
- ->alias('sp')
- ->innerJoinWith([
- 'sale s',
- 'product_1c p1c',
- 'product.nomenclature nom',
- ])
- ->andWhere(['s.store_id' => $storeIds])
- ->andWhere(['between', 's.date', $dateFrom, $dateTo])
- ->andWhere(['nom.category' => null]);
+ $date = new \DateTimeImmutable(sprintf('%04d-%02d-01', $year, $month));
+
+
+ $region = CityStoreParams::find()
+ ->where(['store_id' => $storeId])
+ ->one()->address_region;
- if ($typeFilter) {
- $salesProducts->andWhere(['p1c.type' => $typeFilter]);
+ if (!$regionId && !$region) {
+ // определяем регион по городу
+ $cityId = CityStore::find()->select('city_id')->where(['id' => $storeId])->scalar();
+ if ($cityId == 1) {
+ $region = BouquetComposition::REGION_MSK;
+ } else {
+ $region = BouquetComposition::REGION_NN;
}
- $salesProducts->all();
+ }
+ $monthStart = sprintf('%04d-%02d-01 00:00:00', $year, $month);
+ $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
+ $monthEnd = sprintf('%04d-%02d-%02d 23:59:59', $year, $month, $daysInMonth);
+ $salesProducts = Sales::find()
+ ->alias('s')
+ ->select(['s.id', 's.date', 'sp.product_id', 'p1c.type', 'p1c.components' , 'p1c.name'])
+ ->innerJoin(
+ ['sp' => SalesProducts::tableName()],
+ 's.id = sp.check_id'
+ )
+ ->innerJoin(
+ ['p1c' => Products1c::tableName()],
+ 'p1c.id = sp.product_id'
+ )
+ ->leftJoin(
+ ['nom' => Products1cNomenclature::tableName()],
+ 'nom.id = sp.product_id'
+ )
+ ->andWhere(['s.store_id' => $storeId])
+ ->andWhere(['between', 's.date', $monthStart, $monthEnd])
+ ->andWhere(['not', ['p1c.components' => '']])
+ ->andWhere(['nom.category' => null])
+ ->asArray()
+ ->all();
+ // var_dump( $salesProducts); die();
$components = [];
$rows = [];
foreach ($salesProducts as $sp) {
/** @var SalesProducts $sp */
- $sale = $sp->check_id;
- $product = $sp->product_id;
- $js = trim($product->components);
+ $js = trim($sp['components']);
if ($js === '' || $js[0] !== '{') {
continue;
}
$components[$guid] = true;
$rows[] = [
- 'sale_id' => $sale->id,
- 'sale_date' => $sale->date,
- 'product_id' => $product->id,
- 'product_name'=> $product->name,
+ 'sale_id' => $sp['id'],
+ 'sale_date' => $sp['date'],
+ 'product_id' => $sp['product_id'],
+ 'product_name'=> $sp['name'],
'component_guid' => $guid,
'quantity' => $qty,
];
}
}
+
if (empty($rows)) {
return [];
}
$guids = array_keys($components);
$nomenclatures = Products1cNomenclature::find()
- ->andWhere(['guid' => $guids])
- ->indexBy('guid')
+ ->andWhere(['id' => $guids])
+ ->indexBy('id')
->all();
$priceDynamics = PricesDynamic::find()
- ->andWhere(['region_id' => $regionId])
+ ->andWhere(['region_id' => $region])
->andWhere(['product_id' => array_values( ArrayHelper::getColumn($nomenclatures, 'id') )])
- ->andWhere(['<=', 'date_from', $dateTo])
- ->andWhere(['>=', 'date_to', $dateFrom])
+ // ->andWhere(['<=', 'date_from', $monthStart])
+ // ->andWhere(['>=', 'date_to', $monthEnd])
->orderBy(['date_from' => SORT_DESC])
->all();