| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- <?php
- namespace bizGhs\product\services;
- use bizGhs\base\services\BaseService;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\item\classes\ItemClassClass;
- use bizGhs\product\classes\ProductClass;
- use common\components\barcodeUtil;
- use common\components\dirUtil;
- use common\components\imgUtil;
- use common\services\xhItemService;
- use Yii;
- class ProductService extends BaseService
- {
- public static $baseFile = '\bizGhs\product\classes\ProductClass';
- //特价花材分类
- const TJ_CLASS_ID = '-1';
- //常用花材分类
- const OFTEN_CLASS_ID = '-2';
- //组装分类花材数据
- public static function assembleData($classIds, $itemInfoData, $showTodayDiscount = false)
- {
- //今日特价
- $tjItem = [];
- if ($itemInfoData) {
- foreach ($itemInfoData as $v) {
- $v['classId'] = self::TJ_CLASS_ID;
- if (isset($v['discountPrice']) && $v['discountPrice'] > 0) {
- $tjItem[] = $v;
- }
- }
- }
- $classMapItem = [];
- if ($itemInfoData) {
- foreach ($itemInfoData as $v) {
- $classMapItem[$v['classId']][] = $v['itemId'];
- }
- }
- $classData = [];
- foreach ($classIds as $classVal) {
- $classId = $classVal['id'];
- $className = $classVal['name'] ?? '';
- $cover = imgUtil::groupImg($classVal['cover'])."?x-oss-process=image/resize,m_fill,h_130,w_130";
- if ($classId > 0) {
- $tmp = [];
- $tmp['classId'] = $classId;
- $tmp['className'] = $classVal['name'] ?? '';
- $tmp['classCover'] = $cover;
- $items = $classMapItem[$classId] ?? [];
- if ($items) {
- if ($itemInfoData) {
- foreach ($itemInfoData as $v) {
- if (in_array($v['itemId'], $items)) {
- //冗余给前端使用 2021.1.31 lqh
- $v['classId'] = $classId;
- $v['className'] = $className;
- $tmp['child'][] = $v;
- }
- }
- } else {
- $tmp['child'] = [];
- }
- } else {
- $tmp['child'] = [];
- }
- $classData[] = $tmp;
- }
- }
- if ($showTodayDiscount && !empty($tjItem)) {
- $tjClass = [['classId' => self::TJ_CLASS_ID, 'className' => '今日特价', 'classCover' => imgUtil::groupImg('item_class/tj.jpg'), 'child' => $tjItem,]];
- $classData = array_merge($tjClass, $classData);
- }
- return $classData;
- }
- //根据ghsItemInfo [{productId:0,itemPrice:1,bigNum:1,smallNum:0}] 计算总价格,总重量
- public static function groupCargo($ghsItemInfo)
- {
- $productIds = array_column($ghsItemInfo, 'productId');
- $productData = ProductClass::getProductByIds($productIds);
- $productData = array_column($productData, NULL, 'id');
- $itemIds = array_column($productData, 'itemId');
- $itemData = xhItemService::getByIds($itemIds);
- $itemData = array_column($itemData, NULL, 'id');
- $data = [
- 'totalPrice' => 0,
- 'totalWeight' => 0,
- ];
- foreach ($ghsItemInfo as $v) {
- $productId = $v['productId'];
- $itemId = $productData[$productId]['itemId'];
- $unitNum = $itemData[$itemId]['unitNum'] ?? 0;
- $weight = $itemData[$itemId]['weight'] ?? 0;// 每扎重量
- $itemNum = ProductClass::mergeItemNum($v['bigNum'], $v['smallNum'], $unitNum); // 总共多少扎
- $weight = bcmul($weight, $itemNum, 2);
- $itemPrice = $productData[$productId]['price'] ?? 0; //当时售卖的价格
- $price = bcmul($itemNum, $itemPrice, 2);
- $data['totalWeight'] = bcadd($data['totalWeight'], $weight, 2);
- $data['totalPrice'] = bcadd($data['totalPrice'], $price, 2);
- }
- return $data;
- }
- //生成价格表 ssh 20210922
- public static function generatePriceTable($sjId, $level, $mainId = 0)
- {
- $status = 1;
- //获取所有当前供货商的分类
- //根据分类组装分类下的花材信息
- //中央ID
- $classIds = ItemClassClass::getGhsItemClassAll($mainId);
- $where['mainId'] = $mainId;
- $where['status'] = $status;
- $where['delStatus'] = 0;
- $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
- $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
- $data = self::assembleData($classIds, $itemInfoData, true);
- $formatData = [];
- foreach ($data as $key => $class) {
- $name = $class['className'] ?? '';
- $formatData[] = ['name' => $name, 'price' => ''];
- if (isset($class['child']) && !empty($class['child'])) {
- foreach ($class['child'] as $cKey => $child) {
- $cName = $child['name'] ?? '';
- $price = $child['price'] ?? '';
- $formatData[] = [
- 'name' => $cName,
- 'price' => $price,
- ];
- }
- }
- }
- $map = CustomClass::$nickNameMap;
- $levelName = $map[$level] ?? '';
- $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
- require_once($phpExcelFile . 'Classes/PHPExcel.php');
- $objPHPExcel = new \PHPExcel();
- $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
- ->setLastModifiedBy("Maarten Balliauw")
- ->setTitle("Office 2007 XLSX Test Document")
- ->setSubject("Office 2007 XLSX Test Document")
- ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
- ->setKeywords("office 2007 openxml php")
- ->setCategory("Test result file");
- //$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $levelName . "客户");
- //$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " 编号 " . $level);
- $topLevelName = $levelName;
- if ($level == CustomClass::LEVEL_SK) {
- $topLevelName = '';
- }
- $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $topLevelName);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
- $i = 1;
- $letter = [1 => ['A', 'B'], 2 => ['C', 'D'], 3 => ['E', 'F'], 4 => ['G', 'H'], 5 => ['I', 'J'], 6 => ['K', 'L'], 7 => ['M', 'N'],
- 8 => ['O', 'P'], 9 => ['Q', 'R'], 10 => ['S', 'T'], 11 => ['U', 'V']];
- $lastCell = '';
- //最大行
- $max = 50;
- foreach ($formatData as $key => $product) {
- $currentPage = ceil($i / $max);
- $x = $i % $max == 0 ? $max : $i % $max;
- $before = $letter[$currentPage][0] ?? 'A';
- $after = $letter[$currentPage][1] ?? 'B';
- //行高14
- $objPHPExcel->getActiveSheet()->getRowDimension($x)->setRowHeight(15);
- //设置表格宽度
- $objPHPExcel->getActiveSheet()->getColumnDimension($before)->setWidth(11);
- $objPHPExcel->getActiveSheet()->getColumnDimension($after)->setWidth(5);
- if (isset($product['price']) == false || empty($product['price'])) {
- //没有价格的合并单元格
- $objPHPExcel->getActiveSheet()->mergeCells("{$before}{$x}:{$after}{$x}")->setCellValue($before . $x, $product['name']);
- //加粗
- $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->setName('微软雅黑');
- //居中
- $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- //垂直居中
- $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
- } else {
- $objPHPExcel->getActiveSheet()->setCellValue($before . $x, $product['name'])->setCellValue($after . $x, $product['price']);
- //字体大小
- $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->setSize(9);
- //垂直居中
- $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
- }
- $lastCell = $after . $max;
- $i++;
- }
- //设置边框
- $objPHPExcel->getActiveSheet()->getStyle('A1:' . $lastCell)->applyFromArray(array('borders' => array('allborders' => array('style' => \PHPExcel_Style_Border::BORDER_THIN),)));
- $objPHPExcel->getActiveSheet()->setTitle('价格表');
- $objPHPExcel->setActiveSheetIndex(0);
- $dir = './priceTable/' . $sjId;
- if (file_exists($dir) == false) {
- mkdir($dir, 0777, true);
- }
- //$date = date("m-d") . "-" . $levelName . "-编号" . $level;
- $date = date("m-d") . "-" . $levelName;
- $file = $date . '.xls';
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
- if (file_exists($dir . '/' . $file)) {
- unlink($dir . '/' . $file);
- }
- $objWriter->save($dir . '/' . $file);
- return ['file' => '/priceTable/' . $sjId . '/' . $file, 'shortFile' => $file];
- }
- //生成条形码 lqh 20211227
- public static function generateBarcodeTable($sjId, $shopId, $mainId = 0)
- {
- //中央ID
- $classIds = ItemClassClass::getGhsItemClassAll($mainId);
- // $where['sjId'] = $sjId;
- // $where['shopId'] = $shopId;
- $where['mainId'] = $mainId;
- $where['delStatus'] = 0;
- $product = ProductClass::getAllByCondition($where, null, "*");
- $data = [];
- foreach ($product as $v) {
- $tmp = [];
- $tmp['id'] = $v['id'];
- $tmp['name'] = $v['name'];
- $data[$v['classId']][] = $tmp;
- }
- $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
- require_once($phpExcelFile . 'Classes/PHPExcel.php');
- $objPHPExcel = new \PHPExcel();
- $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
- ->setLastModifiedBy("Maarten Balliauw")
- ->setTitle("Office 2007 XLSX Test Document")
- ->setSubject("Office 2007 XLSX Test Document")
- ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
- ->setKeywords("office 2007 openxml php")
- ->setCategory("Test result file");
- //一行五列
- $column_start = "A";
- $column_end = "F";
- $title_array = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
- 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH'];
- $i = 1;
- $page = 0;
- $objPHPExcel->setActiveSheetIndex($page);
- foreach ($classIds as $k => $v) {
- $productData = $data[$v['id']] ?? [];
- if (empty($productData)) {
- continue;
- }
- $cate_name = $v['name'] ?? '';
- $objPHPExcel->getActiveSheet()->mergeCells("{$column_start}{$i}:{$column_end}{$i}")->setCellValue("A" . $i, $cate_name);
- //居中
- $objPHPExcel->getActiveSheet()->getStyle("{$column_start}{$i}:{$column_end}{$i}")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- //加粗
- $objPHPExcel->getActiveSheet()->getStyle("{$column_start}{$i}:{$column_end}{$i}")->getFont()->setSize(18)->setBold(true);
- $i++;
- if ($productData) {
- $newChunk = array_chunk($productData, 4); //6个一组
- foreach ($newChunk as $c) {
- foreach ($c as $ck => $cv) {
- $objPHPExcel->getActiveSheet()->setCellValue($title_array[$ck] . $i, $cv['name']);
- //居中
- $objPHPExcel->getActiveSheet()->getStyle("$title_array[$ck]$i")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- }
- $i++;
- foreach ($c as $ck => $cv) {
- $objDrawing = new \PHPExcel_Worksheet_Drawing;
- $p = dirUtil::getBarcodeDir() . $cv['id'] . ".jpg";
- if (!file_exists($p)) {
- //生成条形码
- barcodeUtil::generatorProductBarcode($cv['id']);
- }
- $objDrawing->setPath($p);
- //$objDrawing->setHeight(130);//设置图片高度
- $objDrawing->setOffsetX(28);//设置图片水平偏移
- //$objDrawing->setOffsetY(1);//设置图片垂直偏移
- $objPHPExcel->getActiveSheet()->getColumnDimension($title_array[$ck])->setWidth(22);
- $objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(66);
- $objPHPExcel->getActiveSheet()->getStyle("{$title_array[$ck] }{$i}:{$title_array[$ck] }{$i}")->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $objDrawing->setCoordinates($title_array[$ck] . ($i));
- $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
- //居中
- $objPHPExcel->getActiveSheet()->getStyle("$title_array[$ck]$i")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- }
- $i++;
- if ($i >= 18) {
- $i = 1;
- $page++;
- $objPHPExcel->createSheet();
- $objPHPExcel->setActiveSheetIndex($page);
- }
- }
- }
- }
- $dir = './barcodeTable/' . $shopId;
- if (file_exists($dir) == false) {
- mkdir($dir, 0777, true);
- }
- $date = "条形码-" . date("m-d");
- $file = $date . '.xls';
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
- if (file_exists($dir . '/' . $file)) {
- unlink($dir . '/' . $file);
- }
- $objWriter->save($dir . '/' . $file);
- return ['file' => '/barcodeTable/' . $shopId . '/' . $file, 'shortFile' => $file];
- }
- }
|