From 3ad815ec7f2f96f50636fe9faa5a2230978a75c4 Mon Sep 17 00:00:00 2001 From: Babak Alizadeh Date: Mon, 21 Apr 2025 23:39:33 +0000 Subject: [PATCH] bug fix in commodity import/export --- .../src/Controller/CommodityController.php | 28 +++++++- hesabixCore/src/Service/Provider.php | 66 ++++++++++++++++++- 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/hesabixCore/src/Controller/CommodityController.php b/hesabixCore/src/Controller/CommodityController.php index cb9e1ec..43896e2 100644 --- a/hesabixCore/src/Controller/CommodityController.php +++ b/hesabixCore/src/Controller/CommodityController.php @@ -497,7 +497,31 @@ class CommodityController extends AbstractController throw new \Exception('هیچ کالایی برای خروجی یافت نشد'); } - $filePath = $provider->createExcell($items); + $array = []; + foreach ($items as $item) { + $temp = []; + $temp[] = $item->isKhadamat() ? '0' : '1'; + $temp[] = $item->isSpeedAccess() ? '1' : '0'; + $temp[] = $item->getName(); + $temp[] = $item->getPriceSell(); + $temp[] = $item->getPriceBuy(); + $temp[] = $item->getMinOrderCount(); + $temp[] = $item->getDes(); + $temp[] = $item->getUnit()->getName(); + $temp[] = $item->getCat()->getName(); + $array[] = $temp; + } + $filePath = $provider->createExcellFromArray($array, [ + 'کالا(۱) خدمات (۰)', + 'دسترسی سریع ۱ فعال', + 'نام کالا', + 'قیمت فروش', + 'قیمت خرید', + 'حداقل سفارش', + 'توضیحات', + 'واحد شمارش', + 'دسته بندی', + ]); $response = new BinaryFileResponse($filePath); $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'commodities.xlsx'); $response->deleteFileAfterSend(true); @@ -756,7 +780,6 @@ class CommodityController extends AbstractController 'Success' => true, 'result' => 1, ]); - } #[Route('/api/commodity/mod/{code}', name: 'app_commodity_mod')] public function app_commodity_mod(Provider $provider, Request $request, Access $access, Log $log, EntityManagerInterface $entityManager, $code = 0): JsonResponse @@ -1499,5 +1522,4 @@ class CommodityController extends AbstractController $log->insert('کالا/خدمات', 'قیمت تعدادی از کالا‌ها به صورت گروهی ویرایش شد.', $this->getUser(), $acc['bid']->getId()); return $this->json($extractor->operationSuccess()); } - } diff --git a/hesabixCore/src/Service/Provider.php b/hesabixCore/src/Service/Provider.php index a450a44..3fc16f7 100644 --- a/hesabixCore/src/Service/Provider.php +++ b/hesabixCore/src/Service/Provider.php @@ -189,12 +189,72 @@ class Provider */ public function createExcellFromArray(array $entities, array $headers = null) { - $spreadsheet = new Spreadsheet(); $activeWorksheet = $spreadsheet->getActiveSheet(); - $activeWorksheet->fromArray($entities, null, 'A1'); + + // تنظیم هدرها اگر وجود داشته باشند + if ($headers !== null) { + $activeWorksheet->fromArray($headers, null, 'A1'); + + // استایل‌دهی به هدرها + $headerStyle = [ + 'font' => [ + 'bold' => true, + 'color' => ['rgb' => 'FFFFFF'], + 'size' => 12, + ], + 'fill' => [ + 'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID, + 'startColor' => ['rgb' => '4472C4'], + ], + 'alignment' => [ + 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, + 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER, + ], + 'borders' => [ + 'allBorders' => [ + 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN, + 'color' => ['rgb' => '000000'], + ], + ], + ]; + + $activeWorksheet->getStyle('A1:' . $activeWorksheet->getHighestColumn() . '1')->applyFromArray($headerStyle); + + // اضافه کردن داده‌ها از ردیف دوم + $activeWorksheet->fromArray($entities, null, 'A2'); + } else { + $activeWorksheet->fromArray($entities, null, 'A1'); + } + + // تنظیم جهت راست به چپ $activeWorksheet->setRightToLeft(true); - $activeWorksheet->getHeaderFooter()->setOddHeader('&CHeader of the Document'); + + // تنظیم عرض ستون‌ها به صورت خودکار + foreach (range('A', $activeWorksheet->getHighestColumn()) as $column) { + $activeWorksheet->getColumnDimension($column)->setAutoSize(true); + } + + // فریز کردن ردیف هدر + $activeWorksheet->freezePane('A2'); + + // تنظیم استایل برای سلول‌های داده + $dataStyle = [ + 'alignment' => [ + 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, + 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER, + ], + 'borders' => [ + 'allBorders' => [ + 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN, + 'color' => ['rgb' => '000000'], + ], + ], + ]; + + $dataRange = 'A1:' . $activeWorksheet->getHighestColumn() . $activeWorksheet->getHighestRow(); + $activeWorksheet->getStyle($dataRange)->applyFromArray($dataStyle); + $writer = new Xlsx($spreadsheet); $filePath = __DIR__ . '/../../var/' . $this->RandomString(12) . '.xlsx'; $writer->save($filePath);