MCustomerBalanceDetail.Class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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\DCustomerBalanceDetail;
  15. use JinDouYun\Dao\Finance\DCustomerBalanceDetailIndex;
  16. class MCustomerBalanceDetail extends MBaseModel
  17. {
  18. private $objDCustomerBalanceDetail;
  19. private $objDCustomerBalanceDetailIndex;
  20. private $enterpriseId;
  21. private $userCenterId;
  22. private $objMCustomerBalance;
  23. public function __construct($enterpriseId, $userCenterId)
  24. {
  25. $this->userCenterId = $userCenterId;
  26. $this->enterpriseId = $enterpriseId;
  27. parent::__construct($enterpriseId, $userCenterId);
  28. $this->objDCustomerBalanceDetail = new DCustomerBalanceDetail('finance');
  29. $this->objDCustomerBalanceDetailIndex = new DCustomerBalanceDetailIndex('finance');
  30. $this->objMCustomerBalance = new MCustomerBalance($enterpriseId, $userCenterId);
  31. $this->objDCustomerBalanceDetail->setTable('qianniao_customer_balance_detail_' . $enterpriseId . '_' . date('Y') . '_' . ceil(date('m') / 3));
  32. $this->objDCustomerBalanceDetailIndex->setTable('qianniao_customer_balance_detail_index_' . $enterpriseId);
  33. }
  34. /**
  35. * 添加客户余额明细
  36. * @param array $params 客户余额明细数据
  37. * @return ResultWrapper
  38. * @throws \Exception
  39. */
  40. public function addCustomerBalanceDetail($params)
  41. {
  42. $beginTransactionStatus = $this->objDCustomerBalanceDetail->beginTransaction();
  43. $CustomerBalanceDetailId = $this->objDCustomerBalanceDetail->insert($params);
  44. if ($CustomerBalanceDetailId === false) {
  45. $this->objDCustomerBalanceDetail->rollBack();
  46. return ResultWrapper::fail($this->objDCustomerBalanceDetail->error(), ErrorCode::$dberror);
  47. }
  48. $params['no'] = empty($params['no'])?$params['sourceNo']:$params['no'];
  49. //添加索引表
  50. $indexData = [
  51. 'detailId' => $CustomerBalanceDetailId,
  52. 'customerId' => $params['customerId'],
  53. 'receiptTime' => $params['receiptTime'],
  54. 'no' => $params['no'],
  55. 'createTime' => $params['createTime'],
  56. 'updateTime' => $params['updateTime'],
  57. ];
  58. $result = $this->objDCustomerBalanceDetailIndex->insert($indexData);
  59. if ($result === false) {
  60. $this->objDCustomerBalanceDetail->rollBack();
  61. return ResultWrapper::fail($this->objDCustomerBalanceDetailIndex->error(), ErrorCode::$dberror);
  62. }
  63. if($beginTransactionStatus){
  64. $this->objDCustomerBalanceDetail->commit();
  65. }
  66. return ResultWrapper::success($CustomerBalanceDetailId);
  67. }
  68. /**
  69. * 获取所有客户余额明细数据
  70. *
  71. * @param array $selectParams 过滤条件
  72. *
  73. * @return ResultWrapper
  74. * @throws \Exception
  75. */
  76. public function getAllCustomerBalanceDetail($selectParams,$export = 0)
  77. {
  78. $limit = $selectParams['limit'];
  79. unset($selectParams['limit']);
  80. $offset = $selectParams['offset'];
  81. unset($selectParams['offset']);
  82. if($export){
  83. $limit = null;
  84. $offset = null;
  85. }
  86. $customerId = $selectParams['customerId'];
  87. unset($selectParams['customerId']);
  88. $start = $selectParams['start'];
  89. unset($selectParams['start']);
  90. $end = $selectParams['end'];
  91. unset($selectParams['end']);
  92. // 先从客户余额明细主索引表查询客户数据
  93. $where = "customerId = " . $customerId . ' AND createTime>=' . $start . ' AND createTime<=' . $end;
  94. $customerBalanceDetailIndexResult = $this->objDCustomerBalanceDetailIndex->select($where, '*', 'createTime asc', $limit, $offset);
  95. if($customerBalanceDetailIndexResult === false) {
  96. return ResultWrapper::fail($this->objDCustomerBalanceDetailIndex->error(), ErrorCode::$dberror);
  97. }
  98. // 计算明细数据都落在那几张分表上
  99. $tableSuffix = [];
  100. foreach ($customerBalanceDetailIndexResult as $customerBalanceDetailIndex) {
  101. $k = date('Y', $customerBalanceDetailIndex['createTime']) . '_' . ceil(date('m', $customerBalanceDetailIndex['createTime']) / 3);
  102. $tableSuffix[$k][] = $customerBalanceDetailIndex['detailId'];
  103. }
  104. // 切换分表获取单据明细数据
  105. $detailResult = [];
  106. foreach ($tableSuffix as $suffix => $detailIds) {
  107. $this->objDCustomerBalanceDetail->setTable('qianniao_customer_balance_detail_' . $this->enterpriseId . '_' . $suffix);
  108. $dbResult = $this->objDCustomerBalanceDetail->select($detailIds, '*', 'createTime asc');
  109. if ($dbResult === false) {
  110. return ResultWrapper::fail($this->objDCustomerBalanceDetail->error(), ErrorCode::$dberror);
  111. }
  112. $detailResult = array_merge($detailResult, self::format($dbResult));
  113. }
  114. if(!empty($customerBalanceDetailIndexResult)){
  115. $total = $this->objDCustomerBalanceDetailIndex->count($where);
  116. }
  117. //期初余额
  118. $start = $this->objMCustomerBalance->getShouldReceiveMoneyByTime($start, $customerId);
  119. //期末余额
  120. $end = $this->objMCustomerBalance->getShouldReceiveMoneyByTime($end, $customerId);
  121. //应收款余额总计
  122. $shouldReceiveTotal = $this->objMCustomerBalance->getCustomerBalance($customerId);
  123. //实际收款金额总计
  124. $actualReceiveTotal = $this->objMCustomerBalance->getCustomerBalance($customerId, 'totalPayMoney');
  125. $return = [
  126. 'data' => $detailResult,
  127. 'total' => (isset($total)&&!empty($total)) ? intval($total) : 0,
  128. 'openingBalance' => $start ? $start : 0,
  129. 'endingBalance' => $end ? $end : 0,
  130. 'shouldReceiveTotal' => $shouldReceiveTotal ? $shouldReceiveTotal : 0,
  131. 'actualReceiveTotal' => $actualReceiveTotal ? $actualReceiveTotal : 0
  132. ];
  133. //导出
  134. if($export){
  135. self::exportCustomerBalanceDetail($detailResult);
  136. exit;
  137. }
  138. return ResultWrapper::success($return);
  139. }
  140. /**
  141. * 公共的格式化方法
  142. */
  143. public function format($data)
  144. {
  145. foreach ($data as $k => $v){
  146. //如果是销售单,销售收款,只展示应收金额
  147. $data[$k]['salesAmount'] = $v['financeType'] == '预存收款' || $v['financeType'] == '销售收款' ? '--' : $v['salesAmount'];
  148. $data[$k]['discountMoney'] = $v['financeType'] == '预存收款' || $v['financeType'] == '销售收款' ? '--' : $v['discountMoney'];
  149. $data[$k]['receivableAmount'] = $v['financeType'] == '预存收款' || $v['financeType'] == '销售收款' ? '--' : $v['receivableAmount'];
  150. switch ($v['financeType']){
  151. case '销售单':
  152. $data[$k]['sourceNo'] = StatusCode::$noPrefix[16].'-'.$v['sourceNo'];
  153. $data[$k]['originNo'] = StatusCode::$noPrefix[1].'-'.$v['originNo'];
  154. break;
  155. case '销售收款':
  156. $data[$k]['sourceNo'] = StatusCode::$noPrefix[17].'-'.$v['sourceNo'];
  157. $data[$k]['originNo'] = ($data[$k]['originNo']) ? StatusCode::$noPrefix[16].'-'.$v['originNo'] : '';
  158. break;
  159. case '线上支付收款':
  160. case '预存收款':
  161. case '银行打款收款':
  162. $data[$k]['sourceNo'] = StatusCode::$noPrefix[17].'-'.$v['sourceNo'];
  163. $data[$k]['originNo'] = ($data[$k]['originNo']) ? StatusCode::$noPrefix[1].'-'.$v['originNo'] : '';
  164. break;
  165. case '销售退货单':
  166. $data[$k]['sourceNo'] = StatusCode::$noPrefix[16].'-'.$v['sourceNo'];
  167. $data[$k]['originNo'] = ($data[$k]['originNo']) ? StatusCode::$noPrefix[6].'-'.$v['originNo'] : '';
  168. break;
  169. case '订单退货退款':
  170. $data[$k]['sourceNo'] = StatusCode::$noPrefix[20].'-'.$v['sourceNo'];
  171. $data[$k]['originNo'] = ($data[$k]['originNo']) ? StatusCode::$noPrefix[15].'-'.$v['originNo'] : '';
  172. break;
  173. case '订单完结退款':
  174. $data[$k]['sourceNo'] = StatusCode::$noPrefix[20].'-'.$v['sourceNo'];
  175. $data[$k]['originNo'] = ($data[$k]['originNo']) ? StatusCode::$noPrefix[1].'-'.$v['originNo'] : '';
  176. break;
  177. }
  178. }
  179. return $data;
  180. }
  181. /**
  182. * 导出方法
  183. * @param $result
  184. * @return void
  185. * @throws Exception
  186. */
  187. public function exportCustomerBalanceDetail($result)
  188. {
  189. //导出到本地
  190. header("Content-type:application/vnd.ms-excel");
  191. header("Content-Disposition:filename=客户往来明细记录表.csv");
  192. header('Cache-Control: max-age=0');
  193. $fp = fopen('php://output', 'a');
  194. $head = ['单据日期','单据编号','业务类别','源销货订单号','销售金额','优惠金额','应收金额','实收金额','应收余额','备注']; //定义标题
  195. foreach ($head as $i => $v) {
  196. $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8'); //将中文标题转换编码,否则乱码
  197. }
  198. fputcsv($fp, $head);
  199. $limit = 10000;
  200. $num = 0; //计数器
  201. foreach ($result as $v) { //循环数据
  202. $num++;
  203. if ($num == $limit) {
  204. ob_flush(); //释放内存
  205. flush();
  206. }
  207. $rows['receiptTime'] = isset($v['receiptTime']) ? date('Y-m-d', $v['receiptTime']) : '';//单据日期
  208. $rows['no'] = isset($v['no']) ? $v['no'] : '';//单据编号
  209. $rows['financeType'] = isset($v['financeType']) ? $v['financeType'] : '';//业务类别
  210. $rows['sourceNo'] = isset($v['sourceNo']) ? $v['sourceNo'] : '';//源销货订单号
  211. $rows['salesAmount'] = isset($v['salesAmount']) ? $v['salesAmount'] : '';//销售金额
  212. $rows['discountMoney'] = isset($v['discountMoney']) ? $v['discountMoney'] : '';//优惠金额
  213. $rows['receivableAmount'] = isset($v['receivableAmount']) ? $v['receivableAmount'] : '';//应收金额
  214. $rows['actualReceivableAmount'] = isset($v['actualReceivableAmount']) ? $v['actualReceivableAmount'] : '';//实收金额
  215. $rows['receivableBalance'] = isset($v['receivableBalance']) ? $v['receivableBalance'] : '';//应收余额
  216. $rows['remark'] = isset($v['remark']) ? $v['remark'] : '';//备注
  217. foreach ($rows as $kk => $vv) {
  218. $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码
  219. }
  220. fputcsv($fp, $rs);
  221. $rows = [];
  222. }
  223. }
  224. }