ProductService.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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\arrayUtil;
  8. use common\components\barcodeUtil;
  9. use common\components\dirUtil;
  10. use common\components\imgUtil;
  11. use common\services\xhItemService;
  12. use Yii;
  13. class ProductService extends BaseService
  14. {
  15. public static $baseFile = '\bizGhs\product\classes\ProductClass';
  16. //特价
  17. const TJ_CLASS_ID = '-1';
  18. //满减活动
  19. const OFTEN_CLASS_ID = '-2';
  20. //预售
  21. const PRESELL_CLASS_ID = '-3';
  22. //组装分类花材数据
  23. public static function assembleItemData($classInfo, $itemInfoData)
  24. {
  25. $itemGroup = [];
  26. $tjItem = [];
  27. $preSellItem = [];
  28. if ($itemInfoData) {
  29. foreach ($itemInfoData as $v) {
  30. $reachNum = $v['reachNum'] ?? 0;
  31. $reachNumDiscount = $v['reachNumDiscount'] ?? 0;
  32. if (isset($v['stock']) && $v['stock'] > 0) {
  33. if (isset($v['presell']) && $v['presell'] == 1) {
  34. $v['classId'] = self::PRESELL_CLASS_ID;;
  35. $v['className'] = '预售专区';
  36. $itemGroup[] = $v;
  37. unset($v['className']);
  38. unset($v['py']);
  39. $preSellItem[] = $v;
  40. } elseif ((isset($v['discountPrice']) && $v['discountPrice'] > 0) || ($reachNum > 0 && $reachNumDiscount > 0)) {
  41. $v['classId'] = self::TJ_CLASS_ID;;
  42. $v['className'] = '今日推荐';
  43. $itemGroup[] = $v;
  44. unset($v['className']);
  45. unset($v['py']);
  46. $tjItem[] = $v;
  47. }
  48. }
  49. }
  50. }
  51. $classMapItem = [];
  52. if ($itemInfoData) {
  53. foreach ($itemInfoData as $v) {
  54. $classMapItem[$v['classId']][] = $v['itemId'];
  55. }
  56. }
  57. $classData = [];
  58. foreach ($classInfo as $classVal) {
  59. $classId = $classVal['id'];
  60. $className = $classVal['name'] ?? '';
  61. $print = $classVal['print'] ?? 1;
  62. $cover = $classVal['cover'] ?? '';
  63. if ($classId > 0) {
  64. $tmp = [];
  65. $tmp['classId'] = $classId;
  66. $tmp['className'] = $classVal['name'] ?? '';
  67. $tmp['classCover'] = $cover;
  68. $tmp['print'] = $print;
  69. $items = $classMapItem[$classId] ?? [];
  70. if ($items) {
  71. if ($itemInfoData) {
  72. foreach ($itemInfoData as $v) {
  73. if (in_array($v['itemId'], $items)) {
  74. //冗余给前端使用 2021.1.31 lqh
  75. $v['classId'] = $classId;
  76. $v['className'] = $className;
  77. $itemGroup[] = $v;
  78. unset($v['className']);
  79. unset($v['py']);
  80. $tmp['child'][] = $v;
  81. }
  82. }
  83. } else {
  84. $tmp['child'] = [];
  85. }
  86. } else {
  87. $tmp['child'] = [];
  88. }
  89. $classData[] = $tmp;
  90. }
  91. }
  92. if (!empty($tjItem)) {
  93. $tjClass = [[
  94. 'classId' => self::TJ_CLASS_ID,
  95. 'className' => '今日推荐',
  96. 'print' => 0,
  97. 'classCover' => imgUtil::groupImg('item_class/tj.jpg'),
  98. 'child' => $tjItem,
  99. ]];
  100. $classData = array_merge($tjClass, $classData);
  101. }
  102. if (!empty($preSellItem)) {
  103. $preSellClass = [[
  104. 'classId' => self::PRESELL_CLASS_ID,
  105. 'className' => '预售专区',
  106. 'print' => 0,
  107. 'classCover' => imgUtil::groupImg('item_class/mrfl.png'),
  108. 'child' => $preSellItem,
  109. ]];
  110. $classData = array_merge($preSellClass, $classData);
  111. }
  112. return ['classItem' => $classData, 'itemList' => $itemGroup];
  113. }
  114. //组装分类花材数据
  115. public static function assembleData($classInfo, $itemInfoData, $showTodayDiscount = false)
  116. {
  117. $tjItem = []; //特价
  118. $mjItem = []; //满减
  119. $itemGroup = [];
  120. $preSellItem = []; //预售
  121. if ($itemInfoData) {
  122. foreach ($itemInfoData as $v) {
  123. $reachNum = $v['reachNum'] ?? 0;
  124. $reachNumDiscount = $v['reachNumDiscount'] ?? 0;
  125. if ($reachNum > 0 && $reachNumDiscount > 0) {
  126. $v['classId'] = self::OFTEN_CLASS_ID;
  127. $v['className'] = '满减活动';
  128. $mjItem[] = $v;
  129. $itemGroup[] = $v;
  130. }
  131. if ((isset($v['discountPrice']) && $v['discountPrice'] > 0)) {
  132. $v['classId'] = self::TJ_CLASS_ID;
  133. $v['className'] = '今日推荐';
  134. $tjItem[] = $v;
  135. $itemGroup[] = $v;
  136. }
  137. if (isset($v['presell']) && $v['presell'] == 1) {
  138. $v['classId'] = self::PRESELL_CLASS_ID;
  139. $v['className'] = '预售专区';
  140. $preSellItem[] = $v;
  141. $itemGroup[] = $v;
  142. }
  143. }
  144. }
  145. $classMapItem = [];
  146. if ($itemInfoData) {
  147. foreach ($itemInfoData as $v) {
  148. $classMapItem[$v['classId']][] = $v['itemId'];
  149. }
  150. }
  151. $classData = [];
  152. foreach ($classInfo as $classVal) {
  153. $classId = $classVal['id'];
  154. $className = $classVal['name'] ?? '';
  155. $print = $classVal['print'] ?? 1;
  156. $cover = $classVal['cover'] ?? '';
  157. if ($classId > 0) {
  158. $tmp = [];
  159. $tmp['classId'] = $classId;
  160. $tmp['className'] = $classVal['name'] ?? '';
  161. $tmp['classCover'] = $cover;
  162. $tmp['print'] = $print;
  163. $items = $classMapItem[$classId] ?? [];
  164. if ($items) {
  165. if ($itemInfoData) {
  166. foreach ($itemInfoData as $v) {
  167. if (in_array($v['itemId'], $items)) {
  168. //冗余给前端使用 2021.1.31 lqh
  169. $v['classId'] = $classId;
  170. $v['className'] = $className;
  171. $v['print'] = $print;
  172. $tmp['child'][] = $v;
  173. $itemGroup[] = $v;
  174. }
  175. }
  176. } else {
  177. $tmp['child'] = [];
  178. }
  179. } else {
  180. $tmp['child'] = [];
  181. }
  182. $classData[] = $tmp;
  183. }
  184. }
  185. if ($showTodayDiscount && !empty($mjItem)) {
  186. $mjClass = [[
  187. 'classId' => self::OFTEN_CLASS_ID,
  188. 'className' => '满减活动',
  189. 'print' => 0,
  190. 'classCover' => imgUtil::groupImg('item_class/mrfl.jpg'),
  191. 'child' => $mjItem,
  192. ]];
  193. $classData = array_merge($mjClass, $classData);
  194. }
  195. if ($showTodayDiscount && !empty($tjItem)) {
  196. //限购的排在最前面,多处同步修改limit_ahead
  197. $tjItem = arrayUtil::arraySort($tjItem, 'limitBuy');
  198. $tjClass = [[
  199. 'classId' => self::TJ_CLASS_ID,
  200. 'className' => '今日推荐',
  201. 'print' => 0,
  202. 'classCover' => imgUtil::groupImg('item_class/tj.jpg'),
  203. 'child' => $tjItem,
  204. ]];
  205. $classData = array_merge($tjClass, $classData);
  206. }
  207. if ($showTodayDiscount && !empty($preSellItem)) {
  208. $preSellClass = [[
  209. 'classId' => self::PRESELL_CLASS_ID,
  210. 'className' => '预售专区',
  211. 'print' => 0,
  212. 'classCover' => imgUtil::groupImg('item_class/mrfl.png'),
  213. 'child' => $preSellItem,
  214. ]];
  215. $classData = array_merge($preSellClass, $classData);
  216. }
  217. return ['classItem' => $classData, 'itemList' => $itemGroup];
  218. }
  219. //根据ghsItemInfo [{productId:0,itemPrice:1,bigNum:1,smallNum:0}] 计算总价格,总重量
  220. public static function groupCargo($ghsItemInfo)
  221. {
  222. $productIds = array_column($ghsItemInfo, 'productId');
  223. $productData = ProductClass::getProductByIds($productIds);
  224. $productData = array_column($productData, NULL, 'id');
  225. $itemIds = array_column($productData, 'itemId');
  226. $itemData = xhItemService::getByIds($itemIds);
  227. $itemData = array_column($itemData, NULL, 'id');
  228. $data = [
  229. 'totalPrice' => 0,
  230. 'totalWeight' => 0,
  231. ];
  232. foreach ($ghsItemInfo as $v) {
  233. $productId = $v['productId'];
  234. $itemId = $productData[$productId]['itemId'];
  235. $unitNum = $itemData[$itemId]['unitNum'] ?? 0;
  236. $weight = $itemData[$itemId]['weight'] ?? 0;// 每扎重量
  237. $itemNum = ProductClass::mergeItemNum($v['bigNum'], $v['smallNum'], $unitNum); // 总共多少扎
  238. $weight = bcmul($weight, $itemNum, 2);
  239. $itemPrice = $productData[$productId]['price'] ?? 0; //当时售卖的价格
  240. $price = bcmul($itemNum, $itemPrice, 2);
  241. $data['totalWeight'] = bcadd($data['totalWeight'], $weight, 2);
  242. $data['totalPrice'] = bcadd($data['totalPrice'], $price, 2);
  243. }
  244. return $data;
  245. }
  246. //下载所有花材的库存 ssh 20250328
  247. public static function generateItemTable($sjId, $level, $mainId = 0)
  248. {
  249. $status = 1;
  250. //获取所有当前供货商的分类
  251. //根据分类组装分类下的花材信息
  252. //中央ID
  253. $classIds = ItemClassClass::getGhsItemClassAll($mainId);
  254. $where['mainId'] = $mainId;
  255. $where['status'] = $status;
  256. $where['delStatus'] = 0;
  257. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  258. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  259. $respond = self::assembleData($classIds, $itemInfoData, true);
  260. $data = $respond['classItem'] ?? [];
  261. $formatData = [];
  262. foreach ($data as $key => $class) {
  263. $print = $class['print'] ?? 1;
  264. if ($print == 0) {
  265. continue;
  266. }
  267. $name = $class['className'] ?? '';
  268. $formatData[] = ['name' => $name, 'price' => '', 'type' => 'className'];
  269. if (isset($class['child']) && !empty($class['child'])) {
  270. foreach ($class['child'] as $cKey => $child) {
  271. $cName = $child['name'] ?? '';
  272. $stock = $child['stock'] ?? '';
  273. $formatData[] = [
  274. 'name' => $cName,
  275. 'price' => $stock,
  276. 'type' => 'item',
  277. ];
  278. }
  279. }
  280. }
  281. $map = CustomClass::$nickNameMap;
  282. $levelName = $map[$level] ?? '';
  283. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  284. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  285. $objPHPExcel = new \PHPExcel();
  286. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  287. ->setLastModifiedBy("Maarten Balliauw")
  288. ->setTitle("Office 2007 XLSX Test Document")
  289. ->setSubject("Office 2007 XLSX Test Document")
  290. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  291. ->setKeywords("office 2007 openxml php")
  292. ->setCategory("Test result file");
  293. //$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $levelName . "客户");
  294. //$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " 编号 " . $level);
  295. $topLevelName = $levelName;
  296. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $topLevelName);
  297. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  298. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  299. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  300. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  301. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  302. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  303. $i = 1;
  304. $letter = [1 => ['A', 'B'], 2 => ['C', 'D'], 3 => ['E', 'F'], 4 => ['G', 'H'], 5 => ['I', 'J'], 6 => ['K', 'L'], 7 => ['M', 'N'],
  305. 8 => ['O', 'P'], 9 => ['Q', 'R'], 10 => ['S', 'T'], 11 => ['U', 'V']];
  306. $lastCell = '';
  307. //最大行
  308. $max = 50;
  309. foreach ($formatData as $key => $product) {
  310. $currentPage = ceil($i / $max);
  311. $x = $i % $max == 0 ? $max : $i % $max;
  312. $before = $letter[$currentPage][0] ?? 'A';
  313. $after = $letter[$currentPage][1] ?? 'B';
  314. //行高14
  315. $objPHPExcel->getActiveSheet()->getRowDimension($x)->setRowHeight(15);
  316. //设置表格宽度
  317. $objPHPExcel->getActiveSheet()->getColumnDimension($before)->setWidth(11);
  318. $objPHPExcel->getActiveSheet()->getColumnDimension($after)->setWidth(5);
  319. if (isset($product['price']) == false || empty($product['price'])) {
  320. //没有价格的合并单元格
  321. $objPHPExcel->getActiveSheet()->mergeCells("{$before}{$x}:{$after}{$x}")->setCellValue($before . $x, $product['name']);
  322. //加粗
  323. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->setSize(9)->setBold(true);
  324. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->setName('微软雅黑');
  325. //居中
  326. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  327. //垂直居中
  328. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  329. } else if (isset($product['type']) && $product['type'] == 'reach') {
  330. $objPHPExcel->getActiveSheet()->setCellValue($before . $x, $product['name'])->setCellValue($after . $x, $product['price']);
  331. //字体大小 加粗
  332. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->setSize(9)->setBold(true);
  333. //红色
  334. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_RED);
  335. //垂直居中
  336. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  337. //字体大小 加粗
  338. $objPHPExcel->getActiveSheet()->getStyle($after . $x)->getFont()->setSize(11)->setBold(true);
  339. //红色
  340. $objPHPExcel->getActiveSheet()->getStyle($after . $x)->getFont()->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_RED);
  341. } else {
  342. $objPHPExcel->getActiveSheet()->setCellValue($before . $x, $product['name'])->setCellValue($after . $x, $product['price']);
  343. //字体大小
  344. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->setSize(9);
  345. //垂直居中
  346. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  347. }
  348. $lastCell = $after . $max;
  349. $i++;
  350. }
  351. //设置边框
  352. $objPHPExcel->getActiveSheet()->getStyle('A1:' . $lastCell)->applyFromArray(array('borders' => array('allborders' => array('style' => \PHPExcel_Style_Border::BORDER_THIN),)));
  353. $objPHPExcel->getActiveSheet()->setTitle('价格表');
  354. $objPHPExcel->setActiveSheetIndex(0);
  355. $dir = './priceTable/' . $sjId;
  356. if (file_exists($dir) == false) {
  357. mkdir($dir, 0777, true);
  358. }
  359. $date = date("H");
  360. $file = $date . '_stock.xls';
  361. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  362. if (file_exists($dir . '/' . $file)) {
  363. unlink($dir . '/' . $file);
  364. }
  365. $objWriter->save($dir . '/' . $file);
  366. return ['file' => '/priceTable/' . $sjId . '/' . $file, 'shortFile' => $file];
  367. }
  368. //生成价格表 ssh 20210922
  369. public static function generatePriceTable($sjId, $level, $mainId = 0)
  370. {
  371. $status = 1;
  372. //获取所有当前供货商的分类
  373. //根据分类组装分类下的花材信息
  374. //中央ID
  375. $classIds = ItemClassClass::getGhsItemClassAll($mainId);
  376. $where['mainId'] = $mainId;
  377. $where['status'] = $status;
  378. $where['delStatus'] = 0;
  379. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  380. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  381. $respond = self::assembleData($classIds, $itemInfoData, true);
  382. $data = $respond['classItem'] ?? [];
  383. $formatData = [];
  384. foreach ($data as $key => $class) {
  385. $print = $class['print'] ?? 1;
  386. if ($print == 0) {
  387. continue;
  388. }
  389. $name = $class['className'] ?? '';
  390. $formatData[] = ['name' => $name, 'price' => '', 'type' => 'className'];
  391. if (isset($class['child']) && !empty($class['child'])) {
  392. foreach ($class['child'] as $cKey => $child) {
  393. $cName = $child['name'] ?? '';
  394. $cName = trim($cName);
  395. $price = $child['price'] ?? '';
  396. $formatData[] = [
  397. 'name' => $cName,
  398. 'price' => $price,
  399. 'type' => 'item',
  400. ];
  401. if (isset($child['reachNum']) && $child['reachNum'] > 0) {
  402. if (isset($child['reachNumDiscount']) && $child['reachNumDiscount'] > 0) {
  403. $bigUnit = $child['bigUnit'] ?? '扎';
  404. $reachPrice = bcsub($price, $child['reachNumDiscount'], 2);
  405. $reachPrice = floatval($reachPrice);
  406. $reachName = ' 满' . $child['reachNum'] . $bigUnit;
  407. $formatData[] = [
  408. 'name' => $reachName,
  409. 'price' => $reachPrice,
  410. //满减活动花材
  411. 'type' => 'reach',
  412. ];
  413. }
  414. }
  415. }
  416. }
  417. }
  418. $map = CustomClass::$nickNameMap;
  419. $levelName = $map[$level] ?? '';
  420. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  421. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  422. $objPHPExcel = new \PHPExcel();
  423. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  424. ->setLastModifiedBy("Maarten Balliauw")
  425. ->setTitle("Office 2007 XLSX Test Document")
  426. ->setSubject("Office 2007 XLSX Test Document")
  427. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  428. ->setKeywords("office 2007 openxml php")
  429. ->setCategory("Test result file");
  430. //$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $levelName . "客户");
  431. //$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " 编号 " . $level);
  432. $topLevelName = $levelName;
  433. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $topLevelName);
  434. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  435. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  436. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  437. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  438. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  439. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  440. $i = 1;
  441. $letter = [1 => ['A', 'B'], 2 => ['C', 'D'], 3 => ['E', 'F'], 4 => ['G', 'H'], 5 => ['I', 'J'], 6 => ['K', 'L'], 7 => ['M', 'N'],
  442. 8 => ['O', 'P'], 9 => ['Q', 'R'], 10 => ['S', 'T'], 11 => ['U', 'V']];
  443. $lastCell = '';
  444. //最大行
  445. $max = 50;
  446. foreach ($formatData as $key => $product) {
  447. $currentPage = ceil($i / $max);
  448. $x = $i % $max == 0 ? $max : $i % $max;
  449. $before = $letter[$currentPage][0] ?? 'A';
  450. $after = $letter[$currentPage][1] ?? 'B';
  451. //行高14
  452. $objPHPExcel->getActiveSheet()->getRowDimension($x)->setRowHeight(15);
  453. //设置表格宽度
  454. $objPHPExcel->getActiveSheet()->getColumnDimension($before)->setWidth(11);
  455. $objPHPExcel->getActiveSheet()->getColumnDimension($after)->setWidth(5);
  456. if (isset($product['price']) == false || empty($product['price'])) {
  457. //没有价格的合并单元格
  458. $objPHPExcel->getActiveSheet()->mergeCells("{$before}{$x}:{$after}{$x}")->setCellValue($before . $x, trim($product['name']));
  459. //加粗
  460. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->setSize(9)->setBold(true);
  461. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->setName('微软雅黑');
  462. //居中
  463. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  464. //垂直居中
  465. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  466. } else if (isset($product['type']) && $product['type'] == 'reach') {
  467. $objPHPExcel->getActiveSheet()->setCellValue($before . $x, trim($product['name']))->setCellValue($after . $x, $product['price']);
  468. //字体大小 加粗
  469. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->setSize(9)->setBold(true);
  470. //红色
  471. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_RED);
  472. //垂直居中
  473. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  474. //字体大小 加粗
  475. $objPHPExcel->getActiveSheet()->getStyle($after . $x)->getFont()->setSize(11)->setBold(true);
  476. //红色
  477. $objPHPExcel->getActiveSheet()->getStyle($after . $x)->getFont()->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_RED);
  478. } else {
  479. $objPHPExcel->getActiveSheet()->setCellValue($before . $x, trim($product['name']))->setCellValue($after . $x, $product['price']);
  480. //字体大小
  481. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->setSize(9);
  482. //垂直居中
  483. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  484. }
  485. $lastCell = $after . $max;
  486. $i++;
  487. }
  488. //设置边框
  489. $objPHPExcel->getActiveSheet()->getStyle('A1:' . $lastCell)->applyFromArray(array('borders' => array('allborders' => array('style' => \PHPExcel_Style_Border::BORDER_THIN),)));
  490. $objPHPExcel->getActiveSheet()->setTitle('价格表');
  491. $objPHPExcel->setActiveSheetIndex(0);
  492. $dir = './priceTable/' . $sjId;
  493. if (file_exists($dir) == false) {
  494. mkdir($dir, 0777, true);
  495. }
  496. //$date = date("m-d") . "-" . $levelName . "-编号" . $level;
  497. $date = date("m-d") . "-" . $levelName;
  498. $file = $date . '.xls';
  499. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  500. if (file_exists($dir . '/' . $file)) {
  501. unlink($dir . '/' . $file);
  502. }
  503. $objWriter->save($dir . '/' . $file);
  504. return ['file' => '/priceTable/' . $sjId . '/' . $file, 'shortFile' => $file];
  505. }
  506. //下载库存表 ssh 20241028
  507. public static function generateStockTable($sjId, $level, $mainId = 0)
  508. {
  509. //获取所有当前供货商的分类
  510. //根据分类组装分类下的花材信息
  511. //中央ID
  512. $classIds = ItemClassClass::getGhsItemClassAll($mainId);
  513. $where['mainId'] = $mainId;
  514. $where['status'] = 1;
  515. $where['delStatus'] = 0;
  516. $where['stock>'] = 0;
  517. $where['virtualStock'] = 0;
  518. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  519. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  520. $respond = self::assembleData($classIds, $itemInfoData, true);
  521. $data = $respond['classItem'] ?? [];
  522. $formatData = [];
  523. // echo "<pre>";
  524. // print_r($where);
  525. // print_r($data);
  526. // die;
  527. foreach ($data as $key => $class) {
  528. $name = $class['className'] ?? '';
  529. $formatData[] = ['name' => $name, 'price' => '', 'type' => 'className'];
  530. if (isset($class['child']) && !empty($class['child'])) {
  531. foreach ($class['child'] as $cKey => $child) {
  532. $cName = $child['name'] ?? '';
  533. $stock = $child['stock'] ?? 0;
  534. $stock = floatval($stock);
  535. $formatData[] = [
  536. 'name' => $cName,
  537. 'price' => $stock,
  538. 'type' => 'item',
  539. ];
  540. }
  541. }
  542. }
  543. $map = CustomClass::$nickNameMap;
  544. $levelName = $map[$level] ?? '';
  545. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  546. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  547. $objPHPExcel = new \PHPExcel();
  548. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  549. ->setLastModifiedBy("Maarten Balliauw")
  550. ->setTitle("Office 2007 XLSX Test Document")
  551. ->setSubject("Office 2007 XLSX Test Document")
  552. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  553. ->setKeywords("office 2007 openxml php")
  554. ->setCategory("Test result file");
  555. //$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $levelName . "客户");
  556. //$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " 编号 " . $level);
  557. $topLevelName = $levelName;
  558. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $topLevelName);
  559. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  560. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  561. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  562. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  563. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  564. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  565. $i = 1;
  566. $letter = [1 => ['A', 'B'], 2 => ['C', 'D'], 3 => ['E', 'F'], 4 => ['G', 'H'], 5 => ['I', 'J'], 6 => ['K', 'L'], 7 => ['M', 'N'],
  567. 8 => ['O', 'P'], 9 => ['Q', 'R'], 10 => ['S', 'T'], 11 => ['U', 'V']];
  568. $lastCell = '';
  569. //最大行
  570. $max = 50;
  571. foreach ($formatData as $key => $product) {
  572. $currentPage = ceil($i / $max);
  573. $x = $i % $max == 0 ? $max : $i % $max;
  574. $before = $letter[$currentPage][0] ?? 'A';
  575. $after = $letter[$currentPage][1] ?? 'B';
  576. //行高14
  577. $objPHPExcel->getActiveSheet()->getRowDimension($x)->setRowHeight(15);
  578. //设置表格宽度
  579. $objPHPExcel->getActiveSheet()->getColumnDimension($before)->setWidth(11);
  580. $objPHPExcel->getActiveSheet()->getColumnDimension($after)->setWidth(5);
  581. if (isset($product['price']) == false || empty($product['price'])) {
  582. //没有价格的合并单元格
  583. $objPHPExcel->getActiveSheet()->mergeCells("{$before}{$x}:{$after}{$x}")->setCellValue($before . $x, $product['name']);
  584. //加粗
  585. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->setSize(9)->setBold(true);
  586. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->setName('微软雅黑');
  587. //居中
  588. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  589. //垂直居中
  590. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  591. } else if (isset($product['type']) && $product['type'] == 'reach') {
  592. $objPHPExcel->getActiveSheet()->setCellValue($before . $x, $product['name'])->setCellValue($after . $x, $product['price']);
  593. //字体大小 加粗
  594. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->setSize(9)->setBold(true);
  595. //红色
  596. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_RED);
  597. //垂直居中
  598. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  599. //字体大小 加粗
  600. $objPHPExcel->getActiveSheet()->getStyle($after . $x)->getFont()->setSize(11)->setBold(true);
  601. //红色
  602. $objPHPExcel->getActiveSheet()->getStyle($after . $x)->getFont()->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_RED);
  603. } else {
  604. $objPHPExcel->getActiveSheet()->setCellValue($before . $x, $product['name'])->setCellValue($after . $x, $product['price']);
  605. //字体大小
  606. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getFont()->setSize(9);
  607. //垂直居中
  608. $objPHPExcel->getActiveSheet()->getStyle($before . $x)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  609. }
  610. $lastCell = $after . $max;
  611. $i++;
  612. }
  613. //设置边框
  614. $objPHPExcel->getActiveSheet()->getStyle('A1:' . $lastCell)->applyFromArray(array('borders' => array('allborders' => array('style' => \PHPExcel_Style_Border::BORDER_THIN),)));
  615. $objPHPExcel->getActiveSheet()->setTitle('库存表');
  616. $objPHPExcel->setActiveSheetIndex(0);
  617. $dir = './priceTable/' . $sjId;
  618. if (file_exists($dir) == false) {
  619. mkdir($dir, 0777, true);
  620. }
  621. $date = date("m-d") . "-剩余库存";
  622. $file = $date . '.xls';
  623. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  624. if (file_exists($dir . '/' . $file)) {
  625. unlink($dir . '/' . $file);
  626. }
  627. $objWriter->save($dir . '/' . $file);
  628. return ['file' => '/priceTable/' . $sjId . '/' . $file, 'shortFile' => $file];
  629. }
  630. //生成条形码 lqh 20211227
  631. public static function generateBarcodeTable($sjId, $shopId, $mainId = 0)
  632. {
  633. //中央ID
  634. $classIds = ItemClassClass::getGhsItemClassAll($mainId);
  635. // $where['sjId'] = $sjId;
  636. // $where['shopId'] = $shopId;
  637. $where['mainId'] = $mainId;
  638. $where['delStatus'] = 0;
  639. $product = ProductClass::getAllByCondition($where, null, "*");
  640. $data = [];
  641. foreach ($product as $v) {
  642. $tmp = [];
  643. $tmp['id'] = $v['id'];
  644. $tmp['name'] = $v['name'];
  645. $data[$v['classId']][] = $tmp;
  646. }
  647. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  648. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  649. $objPHPExcel = new \PHPExcel();
  650. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  651. ->setLastModifiedBy("Maarten Balliauw")
  652. ->setTitle("Office 2007 XLSX Test Document")
  653. ->setSubject("Office 2007 XLSX Test Document")
  654. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  655. ->setKeywords("office 2007 openxml php")
  656. ->setCategory("Test result file");
  657. //一行五列
  658. $column_start = "A";
  659. $column_end = "F";
  660. $title_array = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
  661. 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH'];
  662. $i = 1;
  663. $page = 0;
  664. $objPHPExcel->setActiveSheetIndex($page);
  665. foreach ($classIds as $k => $v) {
  666. $productData = $data[$v['id']] ?? [];
  667. if (empty($productData)) {
  668. continue;
  669. }
  670. $cate_name = $v['name'] ?? '';
  671. $objPHPExcel->getActiveSheet()->mergeCells("{$column_start}{$i}:{$column_end}{$i}")->setCellValue("A" . $i, $cate_name);
  672. //居中
  673. $objPHPExcel->getActiveSheet()->getStyle("{$column_start}{$i}:{$column_end}{$i}")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  674. //加粗
  675. $objPHPExcel->getActiveSheet()->getStyle("{$column_start}{$i}:{$column_end}{$i}")->getFont()->setSize(18)->setBold(true);
  676. $i++;
  677. if ($productData) {
  678. $newChunk = array_chunk($productData, 4); //6个一组
  679. foreach ($newChunk as $c) {
  680. foreach ($c as $ck => $cv) {
  681. $objPHPExcel->getActiveSheet()->setCellValue($title_array[$ck] . $i, $cv['name']);
  682. //居中
  683. $objPHPExcel->getActiveSheet()->getStyle("$title_array[$ck]$i")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  684. }
  685. $i++;
  686. foreach ($c as $ck => $cv) {
  687. $objDrawing = new \PHPExcel_Worksheet_Drawing;
  688. $p = dirUtil::getBarcodeDir() . $cv['id'] . ".jpg";
  689. if (!file_exists($p)) {
  690. //生成条形码
  691. barcodeUtil::generatorProductBarcode($cv['id']);
  692. }
  693. $objDrawing->setPath($p);
  694. //$objDrawing->setHeight(130);//设置图片高度
  695. $objDrawing->setOffsetX(28);//设置图片水平偏移
  696. //$objDrawing->setOffsetY(1);//设置图片垂直偏移
  697. $objPHPExcel->getActiveSheet()->getColumnDimension($title_array[$ck])->setWidth(22);
  698. $objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(66);
  699. $objPHPExcel->getActiveSheet()->getStyle("{$title_array[$ck] }{$i}:{$title_array[$ck] }{$i}")->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  700. $objDrawing->setCoordinates($title_array[$ck] . ($i));
  701. $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
  702. //居中
  703. $objPHPExcel->getActiveSheet()->getStyle("$title_array[$ck]$i")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  704. }
  705. $i++;
  706. if ($i >= 18) {
  707. $i = 1;
  708. $page++;
  709. $objPHPExcel->createSheet();
  710. $objPHPExcel->setActiveSheetIndex($page);
  711. }
  712. }
  713. }
  714. }
  715. $dir = './barcodeTable/' . $shopId;
  716. if (file_exists($dir) == false) {
  717. mkdir($dir, 0777, true);
  718. }
  719. $date = "条形码-" . date("m-d");
  720. $file = $date . '.xls';
  721. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  722. if (file_exists($dir . '/' . $file)) {
  723. unlink($dir . '/' . $file);
  724. }
  725. $objWriter->save($dir . '/' . $file);
  726. return ['file' => '/barcodeTable/' . $shopId . '/' . $file, 'shortFile' => $file];
  727. }
  728. }