| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- namespace bizGhs\ghs\classes;
- use bizGhs\base\classes\BaseClass;
- use bizGhs\custom\classes\AccountMoneyClass;
- use bizGhs\shop\classes\ShopClass;
- use common\components\imgUtil;
- use Yii;
- class GhsClass extends BaseClass
- {
- public static $baseFile = '\bizGhs\ghs\models\Ghs';
- //供货商列表 ssh 20221220
- public static function getGhsList($where, $sortType = 'all')
- {
- $order = 'inTurn DESC,addTime DESC';
- if ($sortType === 'debt') {
- $order = 'balance ASC,inTurn DESC,addTime DESC';
- } elseif ($sortType === 'expend') {
- $order = 'expendAmount DESC,inTurn DESC,addTime DESC';
- }
- $data = self::getList('*', $where, $order);
- $data['list'] = self::groupBaseInfo($data['list']);
- return $data;
- }
- /**
- * 批发店采购:按 xhGhs 净 balance 汇总总待结(balance<0 部分之和,合并后模型)
- */
- /** 供货商列表 Tab 数量(一次返回,避免前端多次 list 请求) */
- public static function getSupplierTabCounts($ownShopId)
- {
- if (empty($ownShopId)) {
- return ['all' => 0, 'debtRank' => 0, 'expendRank' => 0];
- }
- $base = ['ownShopId' => $ownShopId, 'delStatus' => 0];
- return [
- 'all' => (int)self::getCount($base),
- 'debtRank' => (int)self::getCount(array_merge($base, ['balance<' => 0])),
- 'expendRank' => (int)self::getCount($base),
- ];
- }
- /**
- * 【用途】批发店采购列表页「总待结」:一次查库同时汇总金额与笔数。
- * 【口径】仅净 balance 为负(仍有待结金额)的供货商行计入。
- * @return array{amount:string,num:int}
- */
- public static function sumTotalOutstandingDebtStats($shopId, $delStatus = 0)
- {
- if (empty($shopId)) {
- return ['amount' => '0.00', 'num' => 0];
- }
- $rows = self::getAllByCondition(
- ['ownShopId' => $shopId, 'delStatus' => $delStatus],
- null,
- 'balance,debtAmount,balanceMerged,debtNum'
- );
- $totalAmount = '0.00';
- $totalNum = 0;
- if (!empty($rows)) {
- foreach ($rows as $row) {
- $outstanding = AccountMoneyClass::getOutstandingDebt($row);
- if (bccomp($outstanding, '0', 2) <= 0) {
- continue;
- }
- $totalAmount = bcadd($totalAmount, $outstanding, 2);
- $totalNum += intval($row['debtNum'] ?? 0);
- }
- }
- return ['amount' => $totalAmount, 'num' => $totalNum];
- }
- /** @deprecated 请用 sumTotalOutstandingDebtStats */
- public static function sumTotalOutstandingDebt($shopId, $delStatus = 0)
- {
- $stats = self::sumTotalOutstandingDebtStats($shopId, $delStatus);
- return $stats['amount'];
- }
- /** @deprecated 请用 sumTotalOutstandingDebtStats */
- public static function sumTotalOutstandingDebtNum($shopId, $delStatus = 0)
- {
- $stats = self::sumTotalOutstandingDebtStats($shopId, $delStatus);
- return $stats['num'];
- }
- public static function groupBaseInfo($list)
- {
- if (empty($list)) {
- return $list;
- }
- $ids = array_column($list, 'shopId');
- $shopInfo = ShopClass::getByIds($ids, null, 'id');
- foreach ($list as $key => $val) {
- $avatar = $val['avatar'] ?? '';
- $list[$key]['shortAvatar'] = $avatar;
- $list[$key]['avatar'] = imgUtil::groupImg($avatar);
- $currentShopId = $val['shopId'] ?? 0;
- //新人福利和推荐福利
- $xrFl = $shopInfo[$currentShopId]['xrFl'] ?? 0;
- $xrFlAmount = $shopInfo[$currentShopId]['xrFlAmount'] ?? 0;
- $tjFl = $shopInfo[$currentShopId]['tjFl'] ?? 0;
- $tjFlAmount = $shopInfo[$currentShopId]['tjFlAmount'] ?? 0;
- $list[$key]['xrFl'] = $xrFl;
- $list[$key]['xrFlAmount'] = $xrFlAmount;
- $list[$key]['tjFl'] = $tjFl;
- $list[$key]['tjFlAmount'] = $tjFlAmount;
- $list[$key]['miniKilo'] = $shopInfo[$currentShopId]['miniKilo'] ?? 0;
- $list[$key]['kiloFee'] = $shopInfo[$currentShopId]['kiloFee'] ?? 0;
- $list[$key]['presell'] = $shopInfo[$currentShopId]['presell'] ?? 0;
- $isOpen = 0;
- $list[$key]['isOpen'] = $isOpen;
- $list[$key]['openStartTime'] = $shopInfo[$currentShopId]['openStartTime'] ?? '';
- $list[$key]['openEndTime'] = $shopInfo[$currentShopId]['openEndTime'] ?? '';
- // 待结笔数/金额:合并后直接使用 xhGhs.debtNum 与净 balance,不再查采购单表
- $list[$key] = AccountMoneyClass::formatMoneyForClient($list[$key]);
- }
- return $list;
- }
- //下载供货商 ssh 20240506
- public static function getGhsFile($mainId)
- {
- $ghsList = GhsClass::getAllByCondition(['ownMainId' => $mainId], null, '*');
- if (!empty($ghsList)) {
- foreach ($ghsList as $key => $ghs) {
- $ghsList[$key]['debtNum'] = intval($ghs['debtNum'] ?? 0);
- $ghsList[$key]['debtAmount'] = AccountMoneyClass::getOutstandingDebt($ghs);
- }
- }
- $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 Document")
- ->setSubject("Office 2007 XLSX Document")
- ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
- ->setKeywords("office 2007 openxml php")
- ->setCategory("file");
- $ghsTitle = '供货商';
- $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
- $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);
- $objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID');
- $objPHPExcel->getActiveSheet()->setCellValue('B1', '供货商');
- $objPHPExcel->getActiveSheet()->setCellValue('C1', '欠款');
- $objPHPExcel->getActiveSheet()->setCellValue('D1', '笔数');
- //设置宽度
- $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(40);
- //加粗
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $baseRow = 2;
- foreach ($ghsList as $key => $ghs) {
- $i = $baseRow + $key;
- $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $ghs['id']);
- $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $ghs['name']);
- $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $ghs['debtAmount']);
- $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $ghs['debtNum']);
- //居左
- $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- }
- $fileName = '供货商-' . date("m-d");
- $objPHPExcel->getActiveSheet()->setTitle($fileName);
- $objPHPExcel->setActiveSheetIndex(0);
- $dir = './priceTable/' . $mainId;
- if (file_exists($dir) == false) {
- mkdir($dir, 0777, true);
- }
- $date = $fileName;
- $file = $date . '.xls';
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
- if (file_exists($dir . '/' . $file)) {
- unlink($dir . '/' . $file);
- }
- $objWriter->save($dir . '/' . $file);
- return ['file' => '/priceTable/' . $mainId . '/' . $file, 'shortFile' => $file];
- }
- }
|