MSupplierBalanceDetail.Class.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * 供应商余额明细管理模块
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/10/30
  7. * Time: 14:02
  8. */
  9. namespace JinDouYun\Model\Finance;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\StatusCode;
  12. use Mall\Framework\Core\ResultWrapper;
  13. use JinDouYun\Model\MBaseModel;
  14. use JinDouYun\Dao\Finance\DSupplierBalanceDetail;
  15. use JinDouYun\Dao\Finance\DSupplierBalanceDetailIndex;
  16. class MSupplierBalanceDetail extends MBaseModel
  17. {
  18. private $objDSupplierBalanceDetail;
  19. private $objDSupplierBalanceDetailIndex;
  20. private $objMSupplierBalance;
  21. private $enterpriseId;
  22. private $userCenterId;
  23. public function __construct($enterpriseId, $userCenterId)
  24. {
  25. $this->userCenterId = $userCenterId;
  26. $this->enterpriseId = $enterpriseId;
  27. parent::__construct($enterpriseId, $userCenterId);
  28. $this->objDSupplierBalanceDetail = new DSupplierBalanceDetail('finance');
  29. $this->objDSupplierBalanceDetailIndex = new DSupplierBalanceDetailIndex('finance');
  30. $this->objMSupplierBalance = new MSupplierBalance($enterpriseId, $userCenterId);
  31. $this->objDSupplierBalanceDetail->setTable('qianniao_supplier_balance_detail_' . $enterpriseId . '_' . date('Y') . '_' . ceil(date('m') / 3));
  32. $this->objDSupplierBalanceDetailIndex->setTable('qianniao_supplier_balance_detail_index_' . $enterpriseId);
  33. }
  34. /**
  35. * 添加供应商余额明细
  36. *
  37. * @param array $params 供应商余额明细数据
  38. *
  39. * @return ResultWrapper
  40. */
  41. public function addSupplierBalanceDetail($params)
  42. {
  43. $beginTransactionStatus = $this->objDSupplierBalanceDetail->beginTransaction();
  44. $SupplierBalanceDetailId = $this->objDSupplierBalanceDetail->insert($params);
  45. if ($SupplierBalanceDetailId === false) {
  46. return ResultWrapper::fail($this->objDSupplierBalanceDetail->error(), ErrorCode::$dberror);
  47. }
  48. //添加索引表
  49. $indexData = [
  50. 'detailId' => $SupplierBalanceDetailId,
  51. 'supplierId' => $params['supplierId'],
  52. 'receiptTime' => $params['receiptTime'],
  53. 'no' => $params['no'],
  54. 'createTime' => $params['createTime'],
  55. 'updateTime' => $params['updateTime'],
  56. ];
  57. $result = $this->objDSupplierBalanceDetailIndex->insert($indexData);
  58. if ($result === false) {
  59. $this->objDSupplierBalanceDetail->rollBack();
  60. return ResultWrapper::fail($this->objDSupplierBalanceDetailIndex->error(), ErrorCode::$dberror);
  61. }
  62. if($beginTransactionStatus){
  63. $this->objDSupplierBalanceDetail->commit();
  64. }
  65. return ResultWrapper::success($SupplierBalanceDetailId);
  66. }
  67. /**
  68. * 获取所有供应商余额明细数据
  69. * @param array $selectParams 过滤条件
  70. * @return ResultWrapper
  71. * @throws \Exception
  72. */
  73. public function getAllSupplierBalanceDetail($selectParams,$export = 0)
  74. {
  75. $limit = $selectParams['limit'];
  76. unset($selectParams['limit']);
  77. $offset = $selectParams['offset'];
  78. unset($selectParams['offset']);
  79. $sort = $selectParams['sort'];
  80. unset($selectParams['sort']);
  81. if($export){
  82. $limit = null;
  83. $offset = null;
  84. }
  85. if (!in_array($sort,['ASC','DESC'])){
  86. return ResultWrapper::fail('sort参数异常',ErrorCode::$paramError);
  87. }
  88. $supplierId = $selectParams['supplierId'];
  89. unset($selectParams['supplierId']);
  90. $start = $selectParams['start'];
  91. unset($selectParams['start']);
  92. $end = $selectParams['end'];
  93. unset($selectParams['end']);
  94. $where = "supplierId = " . $supplierId . ' AND createTime>=' . $start . ' AND createTime<=' . $end;
  95. $supplierBalanceDetailIndexResult = $this->objDSupplierBalanceDetailIndex->select($where, '*', 'createTime asc', $limit, $offset);
  96. $tableSuffix = [];
  97. foreach ($supplierBalanceDetailIndexResult as $supplierBalanceDetailIndex) {
  98. $k = date('Y', $supplierBalanceDetailIndex['createTime']) . '_' . ceil(date('m', $supplierBalanceDetailIndex['createTime']) / 3);
  99. $tableSuffix[$k][] = $supplierBalanceDetailIndex['detailId'];
  100. }
  101. $detailResult = [];
  102. $orderSort = 'createTime '.$sort;
  103. foreach ($tableSuffix as $suffix => $detailIds) {
  104. $this->objDSupplierBalanceDetail->setTable('qianniao_supplier_balance_detail_' . $this->enterpriseId . '_' . $suffix);
  105. $dbResult = $this->objDSupplierBalanceDetail->select($detailIds, '*', $orderSort);
  106. if ($dbResult === false) {
  107. return ResultWrapper::fail($this->objDSupplierBalanceDetail->error(), ErrorCode::$dberror);
  108. }
  109. //如果是采购付款,采购预付 ,
  110. foreach ($dbResult as $v => $k){
  111. $dbResult[$v]['salesAmount'] = $k['financeType'] == '采购付款' || $k['financeType'] == '采购预付' ? '--' : $k['salesAmount'];
  112. $dbResult[$v]['discountMoney'] = $k['financeType'] == '采购付款' || $k['financeType'] == '采购预付' ? '--' : $k['discountMoney'];
  113. $dbResult[$v]['shouldPayAmount'] = $k['financeType'] == '采购付款' || $k['financeType'] == '采购预付' ? '--' : $k['shouldPayAmount'];
  114. }
  115. $detailResult = array_merge($detailResult, $dbResult);
  116. }
  117. $total = $this->objDSupplierBalanceDetailIndex->count($where);
  118. //期初余额
  119. $start = $this->objMSupplierBalance->getShouldPayMoneyByTime($start, $supplierId);
  120. //期末余额
  121. $end = $this->objMSupplierBalance->getShouldPayMoneyByTime($end, $supplierId);
  122. //应付款余额总计
  123. $shouldPayTotal = $this->objMSupplierBalance->getSupplierBalance($supplierId);
  124. //实际付款金额总计
  125. $actualPayTotal = $this->objMSupplierBalance->getSupplierBalance($supplierId, 'totalReceiveMoney');
  126. $return = [
  127. 'data' => $detailResult,
  128. 'total' => ($total) ? intval($total) : 0,
  129. 'openingBalance' => $start ? $start : 0,
  130. 'endingBalance' => $end ? $end : 0,
  131. 'shouldPayTotal' => $shouldPayTotal ? $shouldPayTotal : 0,
  132. 'actualPayTotal' => $actualPayTotal ? $actualPayTotal : 0,
  133. ];
  134. //导出
  135. if($export){
  136. self::exportSupplierBalanceDetail($detailResult);
  137. exit;
  138. }
  139. return ResultWrapper::success($return);
  140. }
  141. /**
  142. * 导出方法
  143. * @param $result
  144. * @return void
  145. * @throws Exception
  146. */
  147. public function exportSupplierBalanceDetail($result)
  148. {
  149. //导出到本地
  150. header("Content-type:application/vnd.ms-excel");
  151. header("Content-Disposition:filename=供应商来往明细记录表.csv");
  152. header('Cache-Control: max-age=0');
  153. $fp = fopen('php://output', 'a');
  154. $head = ['订单日期','订单编号','原订单销货号','业务类别','采购金额','优惠金额','应付金额','实付金额','应付余额','备注']; //定义标题
  155. foreach ($head as $i => $v) {
  156. $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8'); //将中文标题转换编码,否则乱码
  157. }
  158. fputcsv($fp, $head);
  159. $limit = 10000;
  160. $num = 0; //计数器
  161. foreach ($result as $v) { //循环数据
  162. $num++;
  163. if ($num == $limit) {
  164. ob_flush(); //释放内存
  165. flush();
  166. }
  167. $rows['receiptTime'] = isset($v['receiptTime']) ? date("Y-m-d",$v['receiptTime']) : '';//订单日期
  168. $rows['no'] = isset($v['no']) ? $v['no'] : '';//订单编号
  169. $rows['sourceNo'] = isset($v['sourceNo']) ? $v['sourceNo'] : '';//原订单销货号
  170. $rows['financeType'] = isset($v['financeType']) ? $v['financeType'] : '';//业务类别
  171. $rows['salesAmount'] = isset($v['salesAmount']) ? $v['salesAmount'] : '';//采购金额
  172. $rows['discountMoney'] = isset($v['discountMoney']) ? $v['discountMoney'] : '';//优惠金额
  173. $rows['shouldPayAmount'] = isset($v['shouldPayAmount']) ? $v['shouldPayAmount'] : '';//应付金额
  174. $rows['actualPaidAmount'] = isset($v['actualPaidAmount']) ? $v['actualPaidAmount'] : '';//实付金额
  175. $rows['shouldPayBalance'] = isset($v['shouldPayBalance']) ? $v['shouldPayBalance'] : '';//应付余额
  176. $rows['remark'] = isset($v['remark']) ? $v['remark'] : '';//备注
  177. foreach ($rows as $kk => $vv) {
  178. $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码
  179. }
  180. fputcsv($fp, $rs);
  181. $rows = [];
  182. }
  183. }
  184. }