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