GhsClass.php 11 KB

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