GhsClass.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace bizGhs\ghs\classes;
  3. use bizGhs\base\classes\BaseClass;
  4. use bizGhs\custom\classes\AccountMoneyClass;
  5. use bizGhs\shop\classes\ShopClass;
  6. use common\components\imgUtil;
  7. use Yii;
  8. class GhsClass extends BaseClass
  9. {
  10. public static $baseFile = '\bizGhs\ghs\models\Ghs';
  11. //供货商列表 ssh 20221220
  12. public static function getGhsList($where, $sortType = 'all')
  13. {
  14. $order = 'inTurn DESC,addTime DESC';
  15. if ($sortType === 'debt') {
  16. $order = 'balance ASC,inTurn DESC,addTime DESC';
  17. } elseif ($sortType === 'expend') {
  18. $order = 'expendAmount DESC,inTurn DESC,addTime DESC';
  19. }
  20. $data = self::getList('*', $where, $order);
  21. $data['list'] = self::groupBaseInfo($data['list']);
  22. return $data;
  23. }
  24. /**
  25. * 批发店采购:按 xhGhs 净 balance 汇总总待结(balance<0 部分之和,合并后模型)
  26. */
  27. /** 供货商列表 Tab 数量(一次返回,避免前端多次 list 请求) */
  28. public static function getSupplierTabCounts($ownShopId)
  29. {
  30. if (empty($ownShopId)) {
  31. return ['all' => 0, 'debtRank' => 0, 'expendRank' => 0];
  32. }
  33. $base = ['ownShopId' => $ownShopId, 'delStatus' => 0];
  34. return [
  35. 'all' => (int)self::getCount($base),
  36. 'debtRank' => (int)self::getCount(array_merge($base, ['balance<' => 0])),
  37. 'expendRank' => (int)self::getCount($base),
  38. ];
  39. }
  40. /**
  41. * 【用途】批发店采购列表页「总待结」:一次查库同时汇总金额与笔数。
  42. * 【口径】仅净 balance 为负(仍有待结金额)的供货商行计入。
  43. * @return array{amount:string,num:int}
  44. */
  45. public static function sumTotalOutstandingDebtStats($shopId, $delStatus = 0)
  46. {
  47. if (empty($shopId)) {
  48. return ['amount' => '0.00', 'num' => 0];
  49. }
  50. $rows = self::getAllByCondition(
  51. ['ownShopId' => $shopId, 'delStatus' => $delStatus],
  52. null,
  53. 'balance,debtAmount,balanceMerged,debtNum'
  54. );
  55. $totalAmount = '0.00';
  56. $totalNum = 0;
  57. if (!empty($rows)) {
  58. foreach ($rows as $row) {
  59. $outstanding = AccountMoneyClass::getOutstandingDebt($row);
  60. if (bccomp($outstanding, '0', 2) <= 0) {
  61. continue;
  62. }
  63. $totalAmount = bcadd($totalAmount, $outstanding, 2);
  64. $totalNum += intval($row['debtNum'] ?? 0);
  65. }
  66. }
  67. return ['amount' => $totalAmount, 'num' => $totalNum];
  68. }
  69. /** @deprecated 请用 sumTotalOutstandingDebtStats */
  70. public static function sumTotalOutstandingDebt($shopId, $delStatus = 0)
  71. {
  72. $stats = self::sumTotalOutstandingDebtStats($shopId, $delStatus);
  73. return $stats['amount'];
  74. }
  75. /** @deprecated 请用 sumTotalOutstandingDebtStats */
  76. public static function sumTotalOutstandingDebtNum($shopId, $delStatus = 0)
  77. {
  78. $stats = self::sumTotalOutstandingDebtStats($shopId, $delStatus);
  79. return $stats['num'];
  80. }
  81. public static function groupBaseInfo($list)
  82. {
  83. if (empty($list)) {
  84. return $list;
  85. }
  86. $ids = array_column($list, 'shopId');
  87. $shopInfo = ShopClass::getByIds($ids, null, 'id');
  88. foreach ($list as $key => $val) {
  89. $avatar = $val['avatar'] ?? '';
  90. $list[$key]['shortAvatar'] = $avatar;
  91. $list[$key]['avatar'] = imgUtil::groupImg($avatar);
  92. $currentShopId = $val['shopId'] ?? 0;
  93. //新人福利和推荐福利
  94. $xrFl = $shopInfo[$currentShopId]['xrFl'] ?? 0;
  95. $xrFlAmount = $shopInfo[$currentShopId]['xrFlAmount'] ?? 0;
  96. $tjFl = $shopInfo[$currentShopId]['tjFl'] ?? 0;
  97. $tjFlAmount = $shopInfo[$currentShopId]['tjFlAmount'] ?? 0;
  98. $list[$key]['xrFl'] = $xrFl;
  99. $list[$key]['xrFlAmount'] = $xrFlAmount;
  100. $list[$key]['tjFl'] = $tjFl;
  101. $list[$key]['tjFlAmount'] = $tjFlAmount;
  102. $list[$key]['miniKilo'] = $shopInfo[$currentShopId]['miniKilo'] ?? 0;
  103. $list[$key]['kiloFee'] = $shopInfo[$currentShopId]['kiloFee'] ?? 0;
  104. $list[$key]['presell'] = $shopInfo[$currentShopId]['presell'] ?? 0;
  105. $isOpen = 0;
  106. $list[$key]['isOpen'] = $isOpen;
  107. $list[$key]['openStartTime'] = $shopInfo[$currentShopId]['openStartTime'] ?? '';
  108. $list[$key]['openEndTime'] = $shopInfo[$currentShopId]['openEndTime'] ?? '';
  109. // 待结笔数/金额:合并后直接使用 xhGhs.debtNum 与净 balance,不再查采购单表
  110. $list[$key] = AccountMoneyClass::formatMoneyForClient($list[$key]);
  111. }
  112. return $list;
  113. }
  114. //下载供货商 ssh 20240506
  115. public static function getGhsFile($mainId)
  116. {
  117. $ghsList = GhsClass::getAllByCondition(['ownMainId' => $mainId], null, '*');
  118. if (!empty($ghsList)) {
  119. foreach ($ghsList as $key => $ghs) {
  120. $ghsList[$key]['debtNum'] = intval($ghs['debtNum'] ?? 0);
  121. $ghsList[$key]['debtAmount'] = AccountMoneyClass::getOutstandingDebt($ghs);
  122. }
  123. }
  124. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  125. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  126. $objPHPExcel = new \PHPExcel();
  127. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  128. ->setLastModifiedBy("Maarten Balliauw")
  129. ->setTitle("Office 2007 XLSX Document")
  130. ->setSubject("Office 2007 XLSX Document")
  131. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  132. ->setKeywords("office 2007 openxml php")
  133. ->setCategory("file");
  134. $ghsTitle = '供货商';
  135. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  136. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  137. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  138. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  139. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  140. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  141. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  142. $objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID');
  143. $objPHPExcel->getActiveSheet()->setCellValue('B1', '供货商');
  144. $objPHPExcel->getActiveSheet()->setCellValue('C1', '欠款');
  145. $objPHPExcel->getActiveSheet()->setCellValue('D1', '笔数');
  146. //设置宽度
  147. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(40);
  148. //加粗
  149. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  150. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  151. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  152. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  153. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  154. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  155. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  156. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  157. $baseRow = 2;
  158. foreach ($ghsList as $key => $ghs) {
  159. $i = $baseRow + $key;
  160. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $ghs['id']);
  161. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $ghs['name']);
  162. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $ghs['debtAmount']);
  163. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $ghs['debtNum']);
  164. //居左
  165. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  166. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  167. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  168. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  169. }
  170. $fileName = '供货商-' . date("m-d");
  171. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  172. $objPHPExcel->setActiveSheetIndex(0);
  173. $dir = './priceTable/' . $mainId;
  174. if (file_exists($dir) == false) {
  175. mkdir($dir, 0777, true);
  176. }
  177. $date = $fileName;
  178. $file = $date . '.xls';
  179. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  180. if (file_exists($dir . '/' . $file)) {
  181. unlink($dir . '/' . $file);
  182. }
  183. $objWriter->save($dir . '/' . $file);
  184. return ['file' => '/priceTable/' . $mainId . '/' . $file, 'shortFile' => $file];
  185. }
  186. }