ProductService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. namespace bizGhs\product\services;
  3. use bizGhs\base\services\BaseService;
  4. use bizGhs\custom\classes\CustomClass;
  5. use bizGhs\item\classes\ItemClassClass;
  6. use bizGhs\product\classes\ProductClass;
  7. use common\components\barcodeUtil;
  8. use common\components\dirUtil;
  9. use common\components\imgUtil;
  10. use common\services\xhItemService;
  11. use Yii;
  12. class ProductService extends BaseService
  13. {
  14. public static $baseFile = '\bizGhs\product\classes\ProductClass';
  15. //特价花材分类
  16. const TJ_CLASS_ID = '-1';
  17. //常用花材分类
  18. const OFTEN_CLASS_ID = '-2';
  19. //组装分类花材数据
  20. public static function assembleData($classIds, $itemInfoData, $showTodayDiscount = false)
  21. {
  22. //今日特价
  23. $tjItem = [];
  24. if ($itemInfoData) {
  25. foreach ($itemInfoData as $v) {
  26. $v['classId'] = self::TJ_CLASS_ID;
  27. if (isset($v['discountPrice']) && $v['discountPrice'] > 0) {
  28. $tjItem[] = $v;
  29. }
  30. }
  31. }
  32. $classMapItem = [];
  33. if ($itemInfoData) {
  34. foreach ($itemInfoData as $v) {
  35. $classMapItem[$v['classId']][] = $v['itemId'];
  36. }
  37. }
  38. $classData = [];
  39. foreach ($classIds as $classVal) {
  40. $classId = $classVal['id'];
  41. $className = $classVal['name'] ?? '';
  42. $cover = imgUtil::groupImg($classVal['cover'])."?x-oss-process=image/resize,m_fill,h_130,w_130";
  43. if ($classId > 0) {
  44. $tmp = [];
  45. $tmp['classId'] = $classId;
  46. $tmp['className'] = $classVal['name'] ?? '';
  47. $tmp['classCover'] = $cover;
  48. $items = $classMapItem[$classId] ?? [];
  49. if ($items) {
  50. if ($itemInfoData) {
  51. foreach ($itemInfoData as $v) {
  52. if (in_array($v['itemId'], $items)) {
  53. //冗余给前端使用 2021.1.31 lqh
  54. $v['classId'] = $classId;
  55. $v['className'] = $className;
  56. $tmp['child'][] = $v;
  57. }
  58. }
  59. } else {
  60. $tmp['child'] = [];
  61. }
  62. } else {
  63. $tmp['child'] = [];
  64. }
  65. $classData[] = $tmp;
  66. }
  67. }
  68. if ($showTodayDiscount && !empty($tjItem)) {
  69. $tjClass = [['classId' => self::TJ_CLASS_ID, 'className' => '今日特价', 'classCover' => imgUtil::groupImg('item_class/tj.jpg'), 'child' => $tjItem,]];
  70. $classData = array_merge($tjClass, $classData);
  71. }
  72. return $classData;
  73. }
  74. //根据ghsItemInfo [{productId:0,itemPrice:1,bigNum:1,smallNum:0}] 计算总价格,总重量
  75. public static function groupCargo($ghsItemInfo)
  76. {
  77. $productIds = array_column($ghsItemInfo, 'productId');
  78. $productData = ProductClass::getProductByIds($productIds);
  79. $productData = array_column($productData, NULL, 'id');
  80. $itemIds = array_column($productData, 'itemId');
  81. $itemData = xhItemService::getByIds($itemIds);
  82. $itemData = array_column($itemData, NULL, 'id');
  83. $data = [
  84. 'totalPrice' => 0,
  85. 'totalWeight' => 0,
  86. ];
  87. foreach ($ghsItemInfo as $v) {
  88. $productId = $v['productId'];
  89. $itemId = $productData[$productId]['itemId'];
  90. $unitNum = $itemData[$itemId]['unitNum'] ?? 0;
  91. $weight = $itemData[$itemId]['weight'] ?? 0;// 每扎重量
  92. $itemNum = ProductClass::mergeItemNum($v['bigNum'], $v['smallNum'], $unitNum); // 总共多少扎
  93. $weight = bcmul($weight, $itemNum, 2);
  94. $itemPrice = $productData[$productId]['price'] ?? 0; //当时售卖的价格
  95. $price = bcmul($itemNum, $itemPrice, 2);
  96. $data['totalWeight'] = bcadd($data['totalWeight'], $weight, 2);
  97. $data['totalPrice'] = bcadd($data['totalPrice'], $price, 2);
  98. }
  99. return $data;
  100. }
  101. //生成价格表 ssh 20210922
  102. public static function generatePriceTable($sjId, $level, $mainId = 0)
  103. {
  104. $status = 1;
  105. //获取所有当前供货商的分类
  106. //根据分类组装分类下的花材信息
  107. //中央ID
  108. $classIds = ItemClassClass::getGhsItemClassAll($mainId);
  109. $where['mainId'] = $mainId;
  110. $where['status'] = $status;
  111. $where['delStatus'] = 0;
  112. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  113. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  114. $data = self::assembleData($classIds, $itemInfoData, true);
  115. $formatData = [];
  116. foreach ($data as $key => $class) {
  117. $name = $class['className'] ?? '';
  118. $formatData[] = ['name' => $name, 'price' => ''];
  119. if (isset($class['child']) && !empty($class['child'])) {
  120. foreach ($class['child'] as $cKey => $child) {
  121. $cName = $child['name'] ?? '';
  122. $price = $child['price'] ?? '';
  123. $formatData[] = [
  124. 'name' => $cName,
  125. 'price' => $price,
  126. ];
  127. }
  128. }
  129. }
  130. $map = CustomClass::$nickNameMap;
  131. $levelName = $map[$level] ?? '';
  132. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  133. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  134. $objPHPExcel = new \PHPExcel();
  135. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  136. ->setLastModifiedBy("Maarten Balliauw")
  137. ->setTitle("Office 2007 XLSX Test Document")
  138. ->setSubject("Office 2007 XLSX Test Document")
  139. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  140. ->setKeywords("office 2007 openxml php")
  141. ->setCategory("Test result file");
  142. //$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $levelName . "客户");
  143. //$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " 编号 " . $level);
  144. $topLevelName = $levelName;
  145. if ($level == CustomClass::LEVEL_SK) {
  146. $topLevelName = '';
  147. }
  148. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $topLevelName);
  149. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  150. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  151. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  152. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  153. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  154. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  155. $i = 1;
  156. $letter = [1 => ['A', 'B'], 2 => ['C', 'D'], 3 => ['E', 'F'], 4 => ['G', 'H'], 5 => ['I', 'J'], 6 => ['K', 'L'], 7 => ['M', 'N'],
  157. 8 => ['O', 'P'], 9 => ['Q', 'R'], 10 => ['S', 'T'], 11 => ['U', 'V']];
  158. $lastCell = '';
  159. //最大行
  160. $max = 50;
  161. foreach ($formatData as $key => $product) {
  162. $currentPage = ceil($i / $max);
  163. $x = $i % $max == 0 ? $max : $i % $max;
  164. $before = $letter[$currentPage][0] ?? 'A';
  165. $after = $letter[$currentPage][1] ?? 'B';
  166. //行高14
  167. $objPHPExcel->getActiveSheet()->getRowDimension($x)->setRowHeight(15);
  168. //设置表格宽度
  169. $objPHPExcel->getActiveSheet()->getColumnDimension($before)->setWidth(11);
  170. $objPHPExcel->getActiveSheet()->getColumnDimension($after)->setWidth(5);
  171. if (isset($product['price']) == false || empty($product['price'])) {
  172. //没有价格的合并单元格
  173. $objPHPExcel->getActiveSheet()->mergeCells("{$before}{$x}:{$after}{$x}")->setCellValue($before . $x, $product['name']);
  174. //加粗
  175. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->setSize(9)->setBold(true);
  176. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->setName('微软雅黑');
  177. //居中
  178. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  179. //垂直居中
  180. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  181. } else {
  182. $objPHPExcel->getActiveSheet()->setCellValue($before . $x, $product['name'])->setCellValue($after . $x, $product['price']);
  183. //字体大小
  184. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->setSize(9);
  185. //垂直居中
  186. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  187. }
  188. $lastCell = $after . $max;
  189. $i++;
  190. }
  191. //设置边框
  192. $objPHPExcel->getActiveSheet()->getStyle('A1:' . $lastCell)->applyFromArray(array('borders' => array('allborders' => array('style' => \PHPExcel_Style_Border::BORDER_THIN),)));
  193. $objPHPExcel->getActiveSheet()->setTitle('价格表');
  194. $objPHPExcel->setActiveSheetIndex(0);
  195. $dir = './priceTable/' . $sjId;
  196. if (file_exists($dir) == false) {
  197. mkdir($dir, 0777, true);
  198. }
  199. //$date = date("m-d") . "-" . $levelName . "-编号" . $level;
  200. $date = date("m-d") . "-" . $levelName;
  201. $file = $date . '.xls';
  202. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  203. if (file_exists($dir . '/' . $file)) {
  204. unlink($dir . '/' . $file);
  205. }
  206. $objWriter->save($dir . '/' . $file);
  207. return ['file' => '/priceTable/' . $sjId . '/' . $file, 'shortFile' => $file];
  208. }
  209. //生成条形码 lqh 20211227
  210. public static function generateBarcodeTable($sjId, $shopId, $mainId = 0)
  211. {
  212. //中央ID
  213. $classIds = ItemClassClass::getGhsItemClassAll($mainId);
  214. // $where['sjId'] = $sjId;
  215. // $where['shopId'] = $shopId;
  216. $where['mainId'] = $mainId;
  217. $where['delStatus'] = 0;
  218. $product = ProductClass::getAllByCondition($where, null, "*");
  219. $data = [];
  220. foreach ($product as $v) {
  221. $tmp = [];
  222. $tmp['id'] = $v['id'];
  223. $tmp['name'] = $v['name'];
  224. $data[$v['classId']][] = $tmp;
  225. }
  226. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  227. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  228. $objPHPExcel = new \PHPExcel();
  229. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  230. ->setLastModifiedBy("Maarten Balliauw")
  231. ->setTitle("Office 2007 XLSX Test Document")
  232. ->setSubject("Office 2007 XLSX Test Document")
  233. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  234. ->setKeywords("office 2007 openxml php")
  235. ->setCategory("Test result file");
  236. //一行五列
  237. $column_start = "A";
  238. $column_end = "F";
  239. $title_array = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
  240. 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH'];
  241. $i = 1;
  242. $page = 0;
  243. $objPHPExcel->setActiveSheetIndex($page);
  244. foreach ($classIds as $k => $v) {
  245. $productData = $data[$v['id']] ?? [];
  246. if (empty($productData)) {
  247. continue;
  248. }
  249. $cate_name = $v['name'] ?? '';
  250. $objPHPExcel->getActiveSheet()->mergeCells("{$column_start}{$i}:{$column_end}{$i}")->setCellValue("A" . $i, $cate_name);
  251. //居中
  252. $objPHPExcel->getActiveSheet()->getStyle("{$column_start}{$i}:{$column_end}{$i}")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  253. //加粗
  254. $objPHPExcel->getActiveSheet()->getStyle("{$column_start}{$i}:{$column_end}{$i}")->getFont()->setSize(18)->setBold(true);
  255. $i++;
  256. if ($productData) {
  257. $newChunk = array_chunk($productData, 4); //6个一组
  258. foreach ($newChunk as $c) {
  259. foreach ($c as $ck => $cv) {
  260. $objPHPExcel->getActiveSheet()->setCellValue($title_array[$ck] . $i, $cv['name']);
  261. //居中
  262. $objPHPExcel->getActiveSheet()->getStyle("$title_array[$ck]$i")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  263. }
  264. $i++;
  265. foreach ($c as $ck => $cv) {
  266. $objDrawing = new \PHPExcel_Worksheet_Drawing;
  267. $p = dirUtil::getBarcodeDir() . $cv['id'] . ".jpg";
  268. if (!file_exists($p)) {
  269. //生成条形码
  270. barcodeUtil::generatorProductBarcode($cv['id']);
  271. }
  272. $objDrawing->setPath($p);
  273. //$objDrawing->setHeight(130);//设置图片高度
  274. $objDrawing->setOffsetX(28);//设置图片水平偏移
  275. //$objDrawing->setOffsetY(1);//设置图片垂直偏移
  276. $objPHPExcel->getActiveSheet()->getColumnDimension($title_array[$ck])->setWidth(22);
  277. $objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(66);
  278. $objPHPExcel->getActiveSheet()->getStyle("{$title_array[$ck] }{$i}:{$title_array[$ck] }{$i}")->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  279. $objDrawing->setCoordinates($title_array[$ck] . ($i));
  280. $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
  281. //居中
  282. $objPHPExcel->getActiveSheet()->getStyle("$title_array[$ck]$i")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  283. }
  284. $i++;
  285. if ($i >= 18) {
  286. $i = 1;
  287. $page++;
  288. $objPHPExcel->createSheet();
  289. $objPHPExcel->setActiveSheetIndex($page);
  290. }
  291. }
  292. }
  293. }
  294. $dir = './barcodeTable/' . $shopId;
  295. if (file_exists($dir) == false) {
  296. mkdir($dir, 0777, true);
  297. }
  298. $date = "条形码-" . date("m-d");
  299. $file = $date . '.xls';
  300. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  301. if (file_exists($dir . '/' . $file)) {
  302. unlink($dir . '/' . $file);
  303. }
  304. $objWriter->save($dir . '/' . $file);
  305. return ['file' => '/barcodeTable/' . $shopId . '/' . $file, 'shortFile' => $file];
  306. }
  307. }