From 2ad9c8bf7b7dd5530be90faf6d3f8101e2d36d99 Mon Sep 17 00:00:00 2001 From: Babak Alizadeh Date: Tue, 4 Mar 2025 06:34:37 +0000 Subject: [PATCH] bug fix in top commodities chart and doctrine configs --- hesabixCore/config/packages/doctrine.yaml | 2 + hesabixCore/config/services.yaml | 7 +++ .../src/Controller/ReportController.php | 55 ++++++++++++++----- 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/hesabixCore/config/packages/doctrine.yaml b/hesabixCore/config/packages/doctrine.yaml index a8077d1..30a00e1 100644 --- a/hesabixCore/config/packages/doctrine.yaml +++ b/hesabixCore/config/packages/doctrine.yaml @@ -9,6 +9,8 @@ doctrine: enable_lazy_ghost_objects: true naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware auto_mapping: true + controller_resolver: + auto_mapping: true mappings: App: is_bundle: false diff --git a/hesabixCore/config/services.yaml b/hesabixCore/config/services.yaml index 5c9fa8e..0e18016 100644 --- a/hesabixCore/config/services.yaml +++ b/hesabixCore/config/services.yaml @@ -14,6 +14,13 @@ services: _defaults: autowire: true # Automatically injects dependencies in your services. autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. + doctrine.orm.default_attribute_driver: + class: Doctrine\ORM\Mapping\Driver\AttributeDriver + arguments: + - [ '%kernel.project_dir%/src/Entity' ] + - true # reportFieldsWhereDeclared + tags: + - { name: doctrine.orm.mapping_driver } App\Security\AuthenticationFailureHandler: arguments: $captchaService: '@App\Service\CaptchaService' diff --git a/hesabixCore/src/Controller/ReportController.php b/hesabixCore/src/Controller/ReportController.php index b618c0b..e0f819c 100644 --- a/hesabixCore/src/Controller/ReportController.php +++ b/hesabixCore/src/Controller/ReportController.php @@ -338,12 +338,27 @@ class ReportController extends AbstractController $queryBuilder = $entityManager->createQueryBuilder(); $queryBuilder - ->select('c') // Commodity - ->addSelect('SUM(CAST(hr.commdityCount AS INTEGER)) as totalCount') - ->addSelect('hr') // HesabdariRow + ->select('c.id AS id') + ->addSelect('c.code AS code') + ->addSelect('c.name AS name') + ->addSelect('c.des AS des') + ->addSelect('c.priceBuy AS priceBuy') + ->addSelect('c.priceSell AS priceSell') + ->addSelect('c.khadamat AS khadamat') + ->addSelect('c.orderPoint AS orderPoint') + ->addSelect('c.commodityCountCheck AS commodityCountCheck') + ->addSelect('c.minOrderCount AS minOrderCount') + ->addSelect('c.dayLoading AS dayLoading') + ->addSelect('c.speedAccess AS speedAccess') + ->addSelect('c.withoutTax AS withoutTax') + ->addSelect('c.barcodes AS barcodes') + ->addSelect('IDENTITY(c.unit) AS unitId') // ID واحد شمارش + ->addSelect('u.name AS unit') // نام واحد شمارش + ->addSelect('SUM(CAST(hr.commdityCount AS INTEGER)) AS totalCount') // مجموع فروش ->from(HesabdariRow::class, 'hr') ->innerJoin('hr.doc', 'hd') ->innerJoin('hr.commodity', 'c') + ->leftJoin('c.unit', 'u') // برای گرفتن نام واحد شمارش ->where('hd.bid = :business') ->andWhere('hd.type = :type') ->andWhere('hr.year = :year') @@ -353,12 +368,13 @@ class ReportController extends AbstractController ->setParameter('year', $year) ->setParameter('dateStart', $dateStart) ->setParameter('dateEnd', $dateEnd) - ->groupBy('c.id, hr.id') + ->groupBy('c.id') // گروه‌بندی فقط بر اساس شناسه کالا + ->addGroupBy('u.name') // برای اطمینان از گروه‌بندی درست با واحد ->orderBy('totalCount', 'DESC') ->setMaxResults($limit); try { - $results = $queryBuilder->getQuery()->getResult(); + $results = $queryBuilder->getQuery()->getArrayResult(); // نتیجه به صورت آرایه $logger->info('Query executed successfully', [ 'sql' => $queryBuilder->getQuery()->getSQL(), 'params' => $queryBuilder->getQuery()->getParameters()->toArray(), @@ -370,14 +386,27 @@ class ReportController extends AbstractController return $this->json(['message' => 'No data found'], 200); } - $topCommodities = []; - foreach ($results as $result) { - // با توجه به لاگ، اندیس 0 الان HesabdariRow هست - $row = $result[0]; // HesabdariRow - $commodity = $row->getCommodity(); // Commodity از داخل HesabdariRow - $totalCount = (int) $result['totalCount']; - $topCommodities[] = $explore::ExploreCommodity($commodity, $totalCount); - } + // فرمت‌بندی خروجی + $topCommodities = array_map(function ($result) { + return [ + 'id' => $result['id'], + 'code' => $result['code'], + 'name' => $result['name'], + 'des' => $result['des'], + 'priceBuy' => $result['priceBuy'], + 'priceSell' => $result['priceSell'], + 'khadamat' => $result['khadamat'], + 'orderPoint' => $result['orderPoint'], + 'commodityCountCheck' => $result['commodityCountCheck'], + 'minOrderCount' => $result['minOrderCount'], + 'dayLoading' => $result['dayLoading'], + 'speedAccess' => $result['speedAccess'], + 'withoutTax' => $result['withoutTax'], + 'barcodes' => $result['barcodes'], + 'unit' => $result['unit'] ?? '', // نام واحد شمارش + 'count' => (int) $result['totalCount'] // مجموع فروش + ]; + }, $results); return $this->json($topCommodities); } catch (\Exception $e) {