MPaid.Class.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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 JinDouYun\Dao\Finance\DPay;
  11. use JinDouYun\Dao\Finance\DPayReceiptIndex;
  12. use Mall\Framework\Core\ErrorCode;
  13. use Mall\Framework\Core\StatusCode;
  14. use Mall\Framework\Core\ResultWrapper;
  15. use JinDouYun\Model\MBaseModel;
  16. use JinDouYun\Model\Finance\MSupplierBalanceDetail;
  17. use JinDouYun\Model\Finance\MSupplierBalance;
  18. use JinDouYun\Model\Finance\MAccount;
  19. use JinDouYun\Model\Finance\MAccountDetail;
  20. use JinDouYun\Model\Finance\MFinanceType;
  21. use JinDouYun\Model\Finance\MPay;
  22. use JinDouYun\Dao\Finance\DPaidOffset;
  23. use JinDouYun\Dao\Finance\DPaid;
  24. use JinDouYun\Dao\Finance\DPaidIndex;
  25. use JinDouYun\Dao\Finance\DPaidAccount;
  26. class MPaid extends MBaseModel
  27. {
  28. private $objDay;
  29. private $objDPaid;
  30. private $objDPaidIndex;
  31. private $objDPaidAccount;
  32. private $objMSupplierBalanceDetail;
  33. private $objMSupplierBalance;
  34. private $objMAccount;
  35. private $objMAccountDetail;
  36. private $enterpriseId;
  37. private $userCenterId;
  38. private $objMFinanceType;
  39. private $objDPaidOffset;
  40. private $objDPayReceiptIndex;
  41. public function __construct($enterpriseId, $userCenterId)
  42. {
  43. $this->userCenterId = $userCenterId;
  44. $this->enterpriseId = $enterpriseId;
  45. parent::__construct($enterpriseId, $userCenterId);
  46. $this->objMSupplierBalanceDetail = new MSupplierBalanceDetail($enterpriseId, $userCenterId);
  47. $this->objMSupplierBalance = new MSupplierBalance($enterpriseId, $userCenterId);
  48. $this->objMAccount = new MAccount($enterpriseId, $userCenterId);
  49. $this->objMAccountDetail = new MAccountDetail($enterpriseId, $userCenterId);
  50. $this->objMFinanceType = new MFinanceType($enterpriseId, $userCenterId);
  51. $this->objDay = new DPay('finance');
  52. $this->objDPaid = new DPaid('finance');
  53. $this->objDPaidIndex = new DPaidIndex('finance');
  54. $this->objDPaidAccount = new DPaidAccount('finance');
  55. $this->objDPaidOffset = new DPaidOffset('finance');
  56. $this->objDPayReceiptIndex = new DPayReceiptIndex('finance');
  57. $this->objDPaidOffset->setTable('qianniao_paid_offset_'. $enterpriseId);
  58. $this->objDPaidIndex->setTable('qianniao_paid_index_' . $this->enterpriseId);
  59. $this->objDPayReceiptIndex->setTable('qianniao_pay_receipt_index_'.$this->enterpriseId);
  60. //$this->objDPaid->setSearchIndex('paid_receipt_search')->setType('paid_receipt');
  61. }
  62. /**
  63. * 添加付款单
  64. *
  65. * @param array $params 付款单数据
  66. *
  67. * @return ResultWrapper
  68. */
  69. public function addPaid($params)
  70. {
  71. $offsetDate = $params['receiptOffsetData'];
  72. unset($params['receiptOffsetData']);
  73. $accountList = $params['accountList'];
  74. unset($params['accountList']);
  75. $beginTransactionStatus = $this->objDPaid->beginTransaction();
  76. // 生成编号
  77. $this->objDPaid->setTable('qianniao_paid_' . $this->enterpriseId . '_' . date('Y') . '_' . ceil(date('m') / 3));
  78. $dbResult = $this->objDPaid->get('createTime >='.strtotime(date('Ymd'.'0:0:0')), 'no', 'createTime desc');
  79. if ($dbResult === false) {
  80. return ResultWrapper::fail($this->objDPaid->error(), ErrorCode::$dberror);
  81. }
  82. if(empty($dbResult)){
  83. $params['no'] = createSerialNumberByDate('');
  84. }else{
  85. $params['no'] = createSerialNumberByDate($dbResult['no']);
  86. }
  87. //sourceNo字段剔除前缀
  88. if(isset($params['sourceNo']) && !empty($params['sourceNo'])){
  89. $tmpSourceNo = explode('-',$params['sourceNo']);
  90. if(count($tmpSourceNo) == 3){
  91. $params['sourceNo'] = $tmpSourceNo[1].'-'.$tmpSourceNo[2];
  92. }
  93. }
  94. //索引表数据
  95. $indexData = [
  96. 'no' => $params['no'],
  97. 'paidId' => 0,
  98. 'supplierId' => $params['supplierId'],
  99. 'currentAccountName' => $params['currentAccountName'],
  100. 'sourceNo' => $params['sourceNo'],
  101. 'auditStatus' => $params['auditStatus'],
  102. 'financeTypeId' => $params['financeTypeId'],
  103. 'financeType' => $params['financeType'],
  104. 'paidType' => empty($offsetDate) ? StatusCode::$standard : StatusCode::$delete,
  105. 'createTime' => $params['createTime'],
  106. 'updateTime' => $params['updateTime'],
  107. 'receiptTime' => $params['receiptTime'],
  108. ];
  109. $PaidId = $this->objDPaidIndex->insert($indexData);
  110. if ($PaidId === false) {
  111. $this->objDPaid->rollBack();
  112. return ResultWrapper::fail($this->objDPaidIndex->error(), ErrorCode::$dberror);
  113. }
  114. $params['id'] = $PaidId;
  115. $params['totalMoney'] = 0;
  116. $params['totalDiscountMoney'] = 0;
  117. $params['totalFinalMoney'] = 0;
  118. foreach ($accountList as $account) {
  119. $account['discountMoney'] = !empty($account['discountMoney']) ? $account['discountMoney'] : 0;
  120. $params['totalMoney'] += $account['money'];
  121. $params['totalDiscountMoney'] += $account['discountMoney'];
  122. $params['totalFinalMoney'] += ($account['money'] - $account['discountMoney']);
  123. $params['offsetMoney'] = 0;
  124. $params['notOffsetMoney'] = $params['totalFinalMoney'];
  125. $params['offsetStatus'] = StatusCode::$delete;
  126. }
  127. $dbResult = $this->objDPaid->insert($params);
  128. if ($dbResult === false) {
  129. $this->objDPaid->rollBack();
  130. return ResultWrapper::fail($this->objDPaid->error(), ErrorCode::$dberror);
  131. }
  132. //收款账户数据
  133. foreach ($accountList as $account) {
  134. if (isset($account['id'])) {
  135. unset($account['id']);
  136. }
  137. $account['paidId'] = $PaidId;
  138. $account['operatorId'] = $this->userCenterId;
  139. $account['createTime'] = $params['createTime'];
  140. $account['updateTime'] = $params['updateTime'];
  141. $this->objDPaidAccount->setTable('qianniao_paid_account_' . $this->enterpriseId . '_' . date('Y') . '_' . ceil(date('m') / 3));
  142. $result = $this->objDPaidAccount->insert($account);
  143. if ($result === false) {
  144. $this->objDPaid->rollBack();
  145. return ResultWrapper::fail($this->objDPaidAccount->error(), ErrorCode::$dberror);
  146. }
  147. }
  148. //记录核销明细
  149. if(empty($offsetDate)){
  150. if ($beginTransactionStatus){
  151. $this->objDPaid->commit();
  152. }
  153. return ResultWrapper::success($PaidId);
  154. }
  155. if(!empty($offsetDate)){
  156. foreach ($offsetDate as $key => $value){
  157. $offsetDate[$key]['paidId'] = $PaidId;
  158. }
  159. }
  160. unset($result);
  161. $result = $this->objDPaidOffset->insert($offsetDate,true);
  162. if ($result === false) {
  163. $this->objDPaid->rollBack();
  164. return ResultWrapper::fail($this->objDPaidOffset->error(), ErrorCode::$dberror);
  165. }
  166. if ($beginTransactionStatus){
  167. $this->objDPaid->commit();
  168. }
  169. /*
  170. $_id = self::createEsDocumentId($PaidId, $params['createTime']);
  171. $esData = $params;
  172. $esData['id'] = $PaidId;
  173. $esData['enterpriseId'] = $this->enterpriseId;
  174. $result = $this->objDPaid->addUpSearchIndexDocument($esData, $_id);
  175. if (isset($result['_shards']) && isset($result['_shards']['successful']) && $result['_shards']['successful'] == 1) {
  176. //echo "es操作成功";die;
  177. }else {
  178. file_put_contents('/www/wwwroot/logs/apiqnys.liuniukj.com/elasticsearch.log',date('Y-m-d H:i:s').'生成付款单es错误,错误原因'.var_export($result,true).PHP_EOL,FILE_APPEND);
  179. }
  180. */
  181. return ResultWrapper::success($PaidId);
  182. }
  183. private function createEsDocumentId($paidId, $time)
  184. {
  185. $t = date('Y', $time) . '_' . ceil(date('m', $time) / 3);
  186. return 'EnterpriseId_' . $this->enterpriseId . '_' . $t .'_paidId_' . $paidId;
  187. }
  188. /**
  189. * 修改付款单
  190. * @param array $params 付款单数据
  191. * @return ResultWrapper
  192. */
  193. public function editPaid($params)
  194. {
  195. $beginTransactionStatus = $this->objDPaid->beginTransaction();
  196. $accountList = $params['accountList'];
  197. unset($params['accountList']);
  198. $offsetDate = $params['receiptOffsetData'];
  199. unset($params['receiptOffsetData']);
  200. if(isset($params['sourceNo']) && !empty($params['sourceNo'])){
  201. unset($params['sourceNo']);
  202. }
  203. $params['totalMoney'] = 0;
  204. $params['totalDiscountMoney'] = 0;
  205. $params['totalFinalMoney'] = 0;
  206. foreach ($accountList as $account) {
  207. $account['discountMoney'] = !empty($account['discountMoney']) ? $account['discountMoney'] : 0;
  208. $params['totalMoney'] += $account['money'];
  209. $params['totalDiscountMoney'] += $account['discountMoney'];
  210. $params['totalFinalMoney'] += ($account['money'] - $account['discountMoney']);
  211. }
  212. $paidId = $params['id'];
  213. unset($params['id']);
  214. // 根据单据创建时间切表
  215. $this->objDPaid->setTable('qianniao_paid_' . $this->enterpriseId . '_' . date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3));
  216. $dbResult = $this->objDPaid->update($params,$paidId);
  217. if ($dbResult === false) {
  218. $this->objDPaid->rollBack();
  219. return ResultWrapper::fail($this->objDPaid->error(), ErrorCode::$dberror);
  220. }
  221. //索引表数据
  222. $indexData = [
  223. 'supplierId' => $params['supplierId'],
  224. 'currentAccountName' => $params['currentAccountName'],
  225. 'sourceNo' => $params['sourceNo'],
  226. 'auditStatus' => $params['auditStatus'],
  227. 'financeTypeId' => $params['financeTypeId'],
  228. 'financeType' => $params['financeType'],
  229. 'paidType' => empty($offsetDate) ? StatusCode::$standard : StatusCode::$delete,
  230. 'createTime' => $params['createTime'],
  231. 'updateTime' => $params['updateTime'],
  232. 'receiptTime' => $params['receiptTime'],
  233. ];
  234. $result = $this->objDPaidIndex->update($indexData,['id'=>$paidId]);
  235. if ($result === false) {
  236. $this->objDPaid->rollBack();
  237. return ResultWrapper::fail($this->objDPaidIndex->error(), ErrorCode::$dberror);
  238. }
  239. //收款账户数据
  240. // 根据单据创建时间切表
  241. $this->objDPaidAccount->setTable('qianniao_paid_account_' . $this->enterpriseId . '_' . date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3));
  242. foreach ($accountList as $account) {
  243. if (isset($account['id'])) {
  244. $account['paidId'] = $paidId;
  245. $account['operatorId'] = $this->userCenterId;
  246. $account['createTime'] = $params['createTime'];
  247. $account['updateTime'] = $params['updateTime'];
  248. $result = $this->objDPaidAccount->update($account,$account['id']);
  249. }else{
  250. $account['paidId'] = $paidId;
  251. $account['operatorId'] = $this->userCenterId;
  252. $account['createTime'] = $params['createTime'];
  253. $result = $this->objDPaidAccount->insert($account);
  254. }
  255. if ($result === false) {
  256. $this->objDPaid->rollBack();
  257. return ResultWrapper::fail($this->objDPaidAccount->error(), ErrorCode::$dberror);
  258. }
  259. }
  260. //更新核销明细
  261. if(empty($offsetDate)){
  262. if ($beginTransactionStatus){
  263. $this->objDPaid->commit();
  264. }
  265. return ResultWrapper::success($dbResult);
  266. }
  267. if(!empty($offsetDate)){
  268. foreach ($offsetDate as $key => $value){
  269. $offsetDate[$key]['paidId'] = $paidId;
  270. unset($result);
  271. $result = $this->objDPaidOffset->update($value, ['paidId'=>$paidId]);
  272. }
  273. }
  274. if ($result === false) {
  275. $this->objDPaid->rollBack();
  276. return ResultWrapper::fail($this->objDPaidOffset->error(), ErrorCode::$dberror);
  277. }
  278. $this->objDPaid->commit();
  279. // $_id = self::createEsDocumentId($paidId, $params['createTime']);
  280. // $esData = $params;
  281. // $esData['id'] = $paidId;
  282. // $esData['enterpriseId'] = $this->enterpriseId;
  283. // $this->objDPaid->esupdateTypeFieldVaule($esData, $_id);
  284. return ResultWrapper::success($dbResult);
  285. }
  286. /**
  287. * 获取指定付款单信息
  288. */
  289. public function getPaidInfo($paidParams)
  290. {
  291. // 根据单据创建时间切表
  292. $this->objDPaid->setTable('qianniao_paid_' . $this->enterpriseId . '_' . date('Y', $paidParams['createTime']) . '_' . ceil(date('m', $paidParams['createTime']) / 3));
  293. $dbResult = $this->objDPaid->get($paidParams['id']);
  294. if ($dbResult === false) {
  295. return ResultWrapper::fail($this->objDPaid->error(), ErrorCode::$dberror);
  296. }
  297. //渲染前缀
  298. $dbResult['no'] = StatusCode::$noPrefix[19] . '-' . $dbResult['no'];
  299. // 根据单据创建时间切表
  300. $this->objDPaidAccount->setTable('qianniao_paid_account_' . $this->enterpriseId . '_' . date('Y', $paidParams['createTime']) . '_' . ceil(date('m', $paidParams['createTime']) / 3));
  301. $dbResult['accountList'] = $this->objDPaidAccount->select(['paidId' => $paidParams['id']]);
  302. //查出供应商当前余额
  303. $dbResult['currentBalanceMoney'] = !empty($dbResult['supplierId']) ? $this->objMSupplierBalance->getSupplierBalance($dbResult['supplierId']) : 0;
  304. //核销明细
  305. $dbResult['offsetDate']= [];
  306. $offsetdbResult = $this->objDPaidOffset->select(['paidId' => $paidParams['id']]);
  307. if ( $offsetdbResult === false ) {
  308. return ResultWrapper::fail($this->objDPaidOffset->error(), ErrorCode::$dberror);
  309. }
  310. if ( !empty($offsetdbResult) ){
  311. $payReceiptIdBindOffsetMoney = [];
  312. foreach ($offsetdbResult as $key => $value){
  313. $payReceiptIdBindOffsetMoney[$value['payReceiptId']] = $value['offsetMoney'];
  314. }
  315. $payReceiptIds = array_column($offsetdbResult,'payReceiptId');
  316. $objMPay = new MPay($this->enterpriseId, $this->userCenterId);
  317. $selectParams = [
  318. 'limit' => 200,
  319. 'offset' => 0,
  320. 'payReceiptIds' => $payReceiptIds,
  321. ];
  322. $payReceiptData = $objMPay->getAllPay($selectParams);
  323. if(!$payReceiptData->isSuccess()){
  324. return ResultWrapper::fail($payReceiptData->getData(), $payReceiptData->getErrorCode());
  325. }
  326. $payReceiptData = $payReceiptData->getData();
  327. if(!empty($payReceiptData['total'])){
  328. foreach ($payReceiptData['data'] as $key => $value){
  329. $payReceiptData['data'][$key]['offsetMoney'] = getArrayItem($payReceiptIdBindOffsetMoney, $value['id'], 0);
  330. }
  331. $dbResult['offsetDate'] = $payReceiptData['data'];
  332. }
  333. }
  334. return ResultWrapper::success($dbResult);
  335. }
  336. /**
  337. * 付款单审核
  338. * @param array $params
  339. * @return ResultWrapper
  340. * @throws \Exception
  341. */
  342. public function updatePaidStatus($params)
  343. {
  344. // 根据单据创建时间切表
  345. $this->objDPaid->setTable('qianniao_paid_' . $this->enterpriseId . '_' . date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3));
  346. // 获取付款单数据
  347. $paidData = $this->objDPaid->get(['id' => $params['id']]);
  348. if ($paidData === false) {
  349. return ResultWrapper::fail($this->objDPaid->error(), ErrorCode::$dberror);
  350. }
  351. if( empty($paidData) ){
  352. return ResultWrapper::fail('要审核的付款单据不存在', ErrorCode::$contentNotExists);
  353. }
  354. // 判断是否已经审核过了
  355. if($paidData['auditStatus'] == StatusCode::$auditStatus['auditPass']){
  356. return ResultWrapper::fail('该单据已经审核过了', ErrorCode::$notAllowAccess);
  357. }
  358. // 修改付款单审核状态
  359. $beginTransactionStatus = $this->objDPaid->beginTransaction();
  360. $dbResult = $this->objDPaid->update(['auditStatus' => StatusCode::$auditStatus['auditPass']], ['id' => $params['id']]);
  361. if ($dbResult === false) {
  362. $this->objDPaid->rollBack();
  363. return ResultWrapper::fail($this->objDPaid->error(), ErrorCode::$dberror);
  364. }
  365. $dbResult = $this->objDPaidIndex->update(['auditStatus' => StatusCode::$auditStatus['auditPass']], ['id' => $params['id']]);
  366. if ($dbResult === false) {
  367. $this->objDPaid->rollBack();
  368. return ResultWrapper::fail($this->objDPaidIndex->error(), ErrorCode::$dberror);
  369. }
  370. $supplierMoney = $this->objMSupplierBalance->getSupplierBalance($paidData['supplierId']);
  371. $remark = '';
  372. //查出源单据已支付过的金额
  373. if (!empty($paidData['sourceNo'])) {
  374. $beforePaidMoney = 0;
  375. $where = 'sourceNo = '. $paidData['sourceNo'] . ' AND auditStatus = '.StatusCode::$auditStatus['auditPass'].' AND id !='.$params['id'];
  376. $sourceNoPaidResult = $this->objDPaidIndex->get($where);
  377. if(!empty($sourceNoPaidResult)) {
  378. $this->objDPaid->setTable('qianniao_paid_' . $this->enterpriseId . '_' . date('Y', $sourceNoPaidResult['createTime']) . '_' . ceil(date('m', $sourceNoPaidResult['createTime']) / 3));
  379. $beforePaidMoney = $this->objDPaid->get_field('totalMoney',$sourceNoPaidResult['paidId']);
  380. }
  381. $remark = ",剩余应付".($paidData['sourceNoMoney'] - $beforePaidMoney - $paidData['totalMoney']).'元';
  382. }
  383. $detailData = [
  384. 'supplierId' => $paidData['supplierId'],
  385. 'receiptTime' => $paidData['receiptTime'],//'单据日期',
  386. 'no' => $paidData['no'],//'单据编号',
  387. 'financeType' => $paidData['financeType'],//'财务类型名称',
  388. 'financeTypeId' => $paidData['financeTypeId'],//'财务类型id',
  389. 'sourceNo' => $paidData['sourceNo'],//'源订单销货号',
  390. 'salesAmount' => 0.00,//销售金额
  391. 'discountMoney' => $paidData['totalDiscountMoney'],// '优惠金额',
  392. 'supplierAmount' => 0.00,//供应商承担金额
  393. 'shouldPayAmount' => 0.00,
  394. 'actualPaidAmount' => $paidData['totalFinalMoney'],//'实际付款金额',
  395. 'shouldPayBalance' => bcsub($supplierMoney,$paidData['totalMoney'],2), //'应付款余额' 当前余额 - 付款金额
  396. 'remark' => '付款单审核通过,本次付给供应商'.$paidData['totalFinalMoney'].'元,优惠'. $paidData['totalDiscountMoney'] .'元'.$remark,//'备注',
  397. 'createTime' => time(),//'创建日期',
  398. 'updateTime' => time(),//'修改日期',
  399. ];
  400. //供应商余额明细
  401. $result = $this->objMSupplierBalanceDetail->addSupplierBalanceDetail($detailData);
  402. if (!$result->isSuccess()) {
  403. $this->objDPaid->rollBack();
  404. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  405. }
  406. //供应商余额
  407. $result = $this->objMSupplierBalance->addSupplierBalance($paidData['supplierId'], -1*$paidData['totalMoney']);
  408. if (!$result->isSuccess()) {
  409. $this->objDPaid->rollBack();
  410. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  411. }
  412. // 查询核销表
  413. $offsetDbResult = $this->objDPaidOffset->select(['paidId'=>$paidData['id']]);
  414. if ($offsetDbResult === false) {
  415. return ResultWrapper::fail($this->objDPaidOffset->error(), ErrorCode::$dberror);
  416. }
  417. //如果是负收款不核销
  418. if($paidData['totalFinalMoney'] > 0){
  419. // 收款单审核自动核销
  420. if (!empty($offsetDbResult)){
  421. foreach ($offsetDbResult as $offsetKey =>$offsetValue){
  422. //说明此条应收单需要优先核销
  423. $sql = 'select * from ' . $this->objDPayReceiptIndex->get_Table() . ' where id='.$offsetValue['payReceiptId'];
  424. $firstOffset = $this->objDPayReceiptIndex->query($sql);
  425. if ($firstOffset === false) {
  426. $this->objDPaid->rollBack();
  427. return ResultWrapper::fail($this->objDPayReceiptIndex->error(), ErrorCode::$dberror);
  428. }
  429. if(empty($firstOffset)){
  430. $this->objDPaid->rollBack();
  431. return ResultWrapper::fail('要操作得应收单不存在', ErrorCode::$contentNotExists);
  432. }
  433. if( $firstOffset[0]['offsetStatus'] == StatusCode::$standard ){
  434. $this->objDPaid->rollBack();
  435. continue;
  436. }
  437. $autoCustomerMoney = self::autoSupplierMoney($paidData['supplierId'],$paidData['totalFinalMoney'],$paidData['id'],$paidData['createTime'],$firstOffset,$offsetValue['payReceiptId']);
  438. if(!$autoCustomerMoney->isSuccess()){
  439. $this->objDPaid->rollBack();
  440. return ResultWrapper::fail($autoCustomerMoney->getData(),$autoCustomerMoney->getErrorCode());
  441. }
  442. }
  443. }else{
  444. if(empty($paidData['sourceNo'])){ // 如果有sourceNo说明需要核销指定应收,这里不需要操作
  445. $autoCustomerMoney = self::autoSupplierMoney($paidData['supplierId'],$paidData['totalFinalMoney'],$paidData['id'],$paidData['createTime'],$firstOffsetDate=[],0);
  446. if(!$autoCustomerMoney->isSuccess()){
  447. $this->objDPaid->rollBack();
  448. return ResultWrapper::fail($autoCustomerMoney->getData(),$autoCustomerMoney->getErrorCode());
  449. }
  450. }
  451. }
  452. }
  453. //更新供应商总收款金额
  454. $result = $this->objMSupplierBalance->updateSupplierTotalReceiveMoney($paidData['supplierId'], $paidData['totalFinalMoney']);
  455. if (!$result->isSuccess()) {
  456. $this->objDPaid->rollBack();
  457. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  458. }
  459. // 根据单据创建时间切表
  460. $this->objDPaidAccount->setTable('qianniao_paid_account_' . $this->enterpriseId . '_' . date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3));
  461. $accounts = $this->objDPaidAccount->select(['paidId'=>$paidData['id']]);
  462. foreach ($accounts as $account) {
  463. //获取账户信息
  464. $accountResult = $this->objMAccount->getAccountInfo($account['accountId']);
  465. if(!$accountResult->isSuccess()) {
  466. $this->objDPaid->rollBack();
  467. return ResultWrapper::fail($accountResult->getData(), $accountResult->getErrorCode());
  468. }
  469. $accountInfo = $accountResult->getData();
  470. //账户明细
  471. $accountDetail = [
  472. 'accountId'=> $accountInfo['id'],
  473. 'accountCode'=> $accountInfo['accountCode'],
  474. 'accountName'=> $accountInfo['name'],
  475. 'accountNumber'=> $accountInfo['accountNumber'],
  476. 'sourceNo'=>$paidData['no'],
  477. 'sourceId'=>$paidData['id'],
  478. 'financeType'=>$paidData['financeType'],
  479. 'beginBalance'=>$accountInfo['money'],
  480. 'shopId'=>$paidData['shopId'],
  481. 'shopName'=>$paidData['shopName'],
  482. 'income'=>$paidData['totalFinalMoney']<0 ? -1 * $paidData['totalFinalMoney'] : 0,
  483. 'expend'=>$paidData['totalFinalMoney']>0 ? $paidData['totalFinalMoney'] : 0,
  484. 'endBalance'=>$accountInfo['money'] - $paidData['totalFinalMoney'],
  485. 'contactUnit'=>'供应商名称:'.$paidData['supplierName'],
  486. 'supplierId'=>$paidData['supplierId'],
  487. 'customerId'=>0,
  488. 'operatorId'=>$this->userCenterId,
  489. 'receiveOrPayPerson'=>$paidData['currentAccountName'],
  490. 'remark'=>'付款单审核通过,本次付给供应商'.$paidData['totalMoney'].'元,优惠'.$paidData['totalDiscountMoney'].'元,实际付款'.$paidData['totalFinalMoney'].'元',
  491. 'createTime'=>time(),
  492. 'updateTime'=>time(),
  493. ];
  494. $result = $this->objMAccountDetail->addAccountDetail($accountDetail);
  495. if (!$result->isSuccess()) {
  496. $this->objDPaid->rollBack();
  497. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  498. }
  499. //给账户减钱
  500. $changedMoney = $paidData['totalFinalMoney'] > 0 ? -1 * $paidData['totalFinalMoney'] : abs($paidData['totalFinalMoney']);
  501. $result = $this->objMAccount->updateMoney($account['accountId'], $changedMoney);
  502. if (!$result->isSuccess()) {
  503. $this->objDPaid->rollBack();
  504. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  505. }
  506. }
  507. //审核通过记录核销金额,未核销金额,核销状态。
  508. // $objMPay = new MPay($this->enterpriseId, $this->userCenterId);
  509. // $offsetDbResult = $this->objDPaidOffset->select(['paidId'=>$paidData['id']]);
  510. // if ($offsetDbResult === false) {
  511. // return ResultWrapper::fail($this->objDPaidOffset->error(), ErrorCode::$dberror);
  512. // }
  513. // if( !empty($offsetDbResult) ){
  514. // foreach ($offsetDbResult as $key =>$value){
  515. // $updateOffsetDbResult = $objMPay->updateOffsetStatus($offsetDbResult[$key]['payReceiptId'],$offsetDbResult[$key]['offsetMoney'],$offsetDbResult[$key]['payCreateTime']);
  516. // if(!$updateOffsetDbResult->isSuccess()){
  517. // $this->objDPaid->rollBack();
  518. // return ResultWrapper::fail($updateOffsetDbResult->getData(), $updateOffsetDbResult->getErrorCode());
  519. // }
  520. // }
  521. // }
  522. if ($beginTransactionStatus){
  523. $this->objDPaid->commit();
  524. }
  525. /*
  526. $_id = self::createEsDocumentId($paidData['id'], $paidData['createTime']);
  527. $this->objDPaid->esupdateTypeFieldVaule(['auditStatus'=>StatusCode::$auditStatus['auditPass']], $_id);*/
  528. return ResultWrapper::success($dbResult);
  529. }
  530. /**
  531. * 获取所有付款单数据
  532. * @param array $selectParams 过滤条件
  533. * @return ResultWrapper
  534. * @throws \Exception
  535. */
  536. public function getAllPaid($selectParams,$export = 0)
  537. {
  538. $limit = $selectParams['limit'];
  539. unset($selectParams['limit']);
  540. $offset = $selectParams['offset'];
  541. unset($selectParams['offset']);
  542. if($export){
  543. $offset = 0;
  544. $limit = 9999;
  545. }
  546. //组装sql where条件
  547. $whereSql = '';
  548. if (isset($selectParams['no']) && !empty($selectParams['no'])) {
  549. //no编号切割
  550. $explodeNo = explode("-",$selectParams['no']);
  551. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  552. $whereSql .= $where . ' no = "' . $explodeNo[1] . '-' . $explodeNo[2] . '"';
  553. }
  554. if (isset($selectParams['financeTypeId']) && !empty($selectParams['financeTypeId'])) {
  555. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  556. $whereSql .= $where . ' financeTypeId = ' . $selectParams['financeTypeId'];
  557. }
  558. if (isset($selectParams['auditStatus']) && !empty($selectParams['auditStatus'])) {
  559. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  560. $whereSql .= $where . ' auditStatus = ' . $selectParams['auditStatus'];
  561. }
  562. if (isset($selectParams['supplierId']) && !empty($selectParams['supplierId'])) {
  563. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  564. $whereSql .= $where . ' supplierId = ' . $selectParams['supplierId'];
  565. }
  566. if ( (isset($selectParams['start']) && !empty($selectParams['start']))&&(isset($selectParams['end']) && !empty($selectParams['end'])) ) {
  567. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  568. $whereSql .= $where . ' createTime BETWEEN ' . $selectParams['start'] . ' AND '. $selectParams['end'];
  569. }
  570. $sql = 'SELECT * FROM ' .$this->objDPaidIndex->get_Table(). $whereSql . ' ORDER BY createTime DESC LIMIT ' . $offset . ' , ' . $limit;
  571. $paidIndexResult = $this->objDPaidIndex->query($sql);
  572. // $paidIndexResult = $this->objDPaidIndex->select($selectParams, '*', 'createTime desc', $limit, $offset);
  573. $tableSuffix = [];
  574. foreach ($paidIndexResult as $paidIndex) {
  575. $k = date('Y', $paidIndex['createTime']) . '_' . ceil(date('m', $paidIndex['createTime']) / 3);
  576. $tableSuffix[$k][] = $paidIndex['id'];
  577. }
  578. $paidResult = [];
  579. foreach ($tableSuffix as $suffix => $paidIds) {
  580. $this->objDPaid->setTable('qianniao_paid_' . $this->enterpriseId . '_' . $suffix);
  581. $dbResult = $this->objDPaid->select($paidIds, '*', 'createTime desc');
  582. if ($dbResult === false) {
  583. return ResultWrapper::fail($this->objDPaid->error(), ErrorCode::$dberror);
  584. }
  585. //渲染前缀
  586. if (!empty($dbResult)){
  587. foreach ($dbResult as $key => $value){
  588. $dbResult[$key]['no'] = StatusCode::$noPrefix[19] . '-' . $value['no'];
  589. if( !empty($value['sourceNo']) ){
  590. $dbResult[$key]['sourceNo'] = StatusCode::$noPrefix[2] . '-' . $value['sourceNo'];
  591. }
  592. }
  593. }
  594. $paidResult = array_merge($paidResult, $dbResult);
  595. }
  596. $totalSql = 'SELECT COUNT(1) as count FROM ' .$this->objDPaidIndex->get_Table() . $whereSql;
  597. $dbTotalResult = $this->objDPaidIndex->query($totalSql);
  598. //$total = $this->objDPaidIndex->count($selectParams);
  599. $return = [
  600. 'data' => self::format($paidResult),
  601. 'total' => $dbTotalResult[0]['count'],
  602. ];
  603. //导出
  604. if($export){
  605. self::exportPaid($return['data']);
  606. exit;
  607. }
  608. return ResultWrapper::success($return);
  609. }
  610. /**
  611. * 自动核销供应商余额
  612. * 客户id,收款单总的money进行自动核销
  613. */
  614. public function autoSupplierMoney($supplierId,$money,$id,$createTime,$firstOffset,$payReceiptId)
  615. {
  616. $paidDbResult = $this->objDPaid->get($id);
  617. if ($paidDbResult === false) {
  618. $this->objDPaid->rollBack();
  619. return ResultWrapper::fail($this->objDPaid->error(), ErrorCode::$dberror);
  620. }
  621. //根据客户id查询客户所有核销状态不为5的应收单
  622. if ($payReceiptId == 0){//说明没有优先核销的
  623. $sql = 'select * from ' . $this->objDPayReceiptIndex->get_Table() . ' where supplierId='.$supplierId.' and offsetStatus != '. StatusCode::$standard;
  624. $dbResultOffset = $this->objDPayReceiptIndex->query($sql);
  625. }else{
  626. $sql = 'select * from ' . $this->objDPayReceiptIndex->get_Table() . ' where supplierId='.$supplierId.' and offsetStatus != '. StatusCode::$standard.' and id !='. $payReceiptId;
  627. $dbResultOffset = $this->objDPayReceiptIndex->query($sql);
  628. }
  629. $dbResultIndex = array_merge($firstOffset,$dbResultOffset);
  630. if ($dbResultIndex === false) {
  631. return ResultWrapper::fail($this->objDPayReceiptIndex->error(), ErrorCode::$dberror);
  632. }
  633. if(empty($dbResultIndex)){//如果为空,说明当前单子只做付款,预付,不做核销.
  634. return ResultWrapper::success([]);
  635. }
  636. $surplusMoney = $money; // 剩余金额
  637. $payOffsetMoney = 0; // 收款单累计核销金额
  638. $payNotOffsetMoney = $money; // 收款累计未核销金额
  639. foreach ($dbResultIndex as $key => $value){
  640. // 付款单钱核销完了,则不再进行核销
  641. if($surplusMoney <=0){
  642. break;
  643. }
  644. //根据index表获取季度表信息
  645. $suffix = date('Y', $value['createTime']) . '_' . ceil(date('m', $value['createTime']) / 3);
  646. $this->objDay->setTable('qianniao_pay_receipt_' . $this->enterpriseId . '_' . $suffix);
  647. //查出当前应收单的信息
  648. $payDate = $this->objDay->get($value['id']);
  649. if ($payDate === false) {
  650. return ResultWrapper::fail($this->objDay->error(), ErrorCode::$dberror);
  651. }
  652. if(empty($payDate)){
  653. continue;
  654. }
  655. //只核销金额大于0的,小于0是退款单
  656. if ($payDate['payMoney']<0) {
  657. continue;
  658. }
  659. switch (true)
  660. {
  661. case $payDate['notOffsetMoney'] <= $surplusMoney; //说明当前单子刚好核销完
  662. $offsetMoney = $payDate['notOffsetMoney'];
  663. $notOffsetMoney = 0;
  664. $offsetStatus = StatusCode::$standard;
  665. $payOffsetMoney = bcadd($payOffsetMoney, $payDate['notOffsetMoney'], 2);
  666. $payNotOffsetMoney = bcsub($payNotOffsetMoney, $payDate['notOffsetMoney'], 2);
  667. $updatePay = [
  668. 'offsetMoney' => $payOffsetMoney,
  669. 'notOffsetMoney' => $payNotOffsetMoney,
  670. 'offsetStatus' => ($payNotOffsetMoney ==0 ) ? StatusCode::$standard : StatusCode::$partion,
  671. ];
  672. $updatePaidIndex = [
  673. 'offsetStatus' => ($payNotOffsetMoney ==0 ) ? StatusCode::$standard : StatusCode::$partion,
  674. ];
  675. break;
  676. case $payDate['notOffsetMoney'] > $surplusMoney;
  677. $offsetMoney = $surplusMoney;
  678. $notOffsetMoney = bcsub($payDate['notOffsetMoney'],$offsetMoney,2);
  679. $offsetStatus = StatusCode::$partion;
  680. $payOffsetMoney = bcadd($payOffsetMoney, $surplusMoney, 2);
  681. $updatePay = [
  682. 'offsetMoney' => $payOffsetMoney,
  683. 'notOffsetMoney' => 0,
  684. 'offsetStatus' => StatusCode::$partion,
  685. ];
  686. $updatePaidIndex = [
  687. 'offsetStatus' => StatusCode::$partion,
  688. ];
  689. break;
  690. }
  691. $surplusMoney = bcsub($surplusMoney, $payDate['notOffsetMoney'], 2);
  692. //判断该单据是否已经审核
  693. if($payDate['auditStatus'] == StatusCode:: $auditStatus['auditPass']){
  694. $update = [
  695. 'offsetMoney' => bcadd($payDate['offsetMoney'],$offsetMoney,2),
  696. 'notOffsetMoney' => $notOffsetMoney,
  697. 'offsetStatus' => $offsetStatus,
  698. ];
  699. }else{
  700. $update = [
  701. 'offsetMoney' => bcadd($payDate['offsetMoney'],$offsetMoney,2),
  702. 'notOffsetMoney' => $notOffsetMoney,
  703. 'offsetStatus' => $offsetStatus,
  704. 'auditStatus' =>StatusCode:: $auditStatus['auditPass']
  705. ];
  706. }
  707. //判断index单据是否已经审核
  708. if($value['auditStatus'] == StatusCode:: $auditStatus['auditPass']){
  709. $updateIndex = [
  710. 'offsetStatus' => $offsetStatus,
  711. ];
  712. }else{
  713. $updateIndex = [
  714. 'offsetStatus' => $offsetStatus,
  715. 'auditStatus' =>StatusCode:: $auditStatus['auditPass']
  716. ];
  717. }
  718. $updateDPay = $this->objDay->update($update,['id'=>$value['id']]);
  719. if ($updateDPay === false) {
  720. $this->objDPaid->rollBack();
  721. return ResultWrapper::fail($this->objDay->error(), ErrorCode::$dberror);
  722. }
  723. $updateDReceiveReceiptIndex = $this->objDPayReceiptIndex->update($updateIndex,['id'=>$value['id']]);
  724. if ($updateDReceiveReceiptIndex === false) {
  725. $this->objDPaid->rollBack();
  726. return ResultWrapper::fail($this->objDPayReceiptIndex->error(), ErrorCode::$dberror);
  727. }
  728. //应收核销完成,记录核销信息
  729. $this->objDPaid->setTable('qianniao_paid_' . $this->enterpriseId . '_' . date('Y', $createTime) . '_' . ceil(date('m', $createTime) / 3));
  730. $updatePayResult = $this->objDPaid->update($updatePay,['id'=>$id]);
  731. if ($updatePayResult === false) {
  732. $this->objDPaid->rollBack();
  733. return ResultWrapper::fail($this->objDPaid->error(), ErrorCode::$dberror);
  734. }
  735. //核销完成更新index表的核销状态
  736. $updatePayIndexResult = $this->objDPaidIndex->update($updatePaidIndex, ['id' => $id]);
  737. if ($updatePayIndexResult === false) {
  738. $this->objDPaid->rollBack();
  739. return ResultWrapper::fail($this->objDPaidIndex->error(), ErrorCode::$dberror);
  740. }
  741. //如果已经记过,需要剔除
  742. if (!empty($firstOffset) && count($firstOffset) - 1 >= $key) {//剔除
  743. continue;
  744. } else {
  745. //核销完成,记录核销明细
  746. $updateOffset = [
  747. 'payReceiptId' => $value['id'],
  748. 'paidId' => $id,
  749. 'offsetMoney' => $offsetMoney,
  750. 'payCreateTime' => $value['createTime']
  751. ];
  752. $result = $this->objDPaidOffset->insert($updateOffset);
  753. if ($result === false) {
  754. $this->objDPaid->rollBack();
  755. return ResultWrapper::fail($this->objDPaidOffset->error(), ErrorCode::$dberror);
  756. }
  757. }
  758. }
  759. return ResultWrapper::success($updateDPay);
  760. }
  761. public function format($data)
  762. {
  763. $financeTypeIds = [];
  764. foreach ($data as $key => $paid) {
  765. $financeTypeIds[] = $paid['financeTypeId'];
  766. }
  767. $financeTypeArr = [];
  768. if ($financeTypeIds) {
  769. $financeTypeResult = $this->objMFinanceType->getAllFinanceTypeNoPage(['id'=>array_unique(array_filter($financeTypeIds))]);
  770. if ($financeTypeResult->isSuccess()) {
  771. $financeTypeData = $financeTypeResult->getData();
  772. if (!empty($financeTypeData)) {
  773. foreach ($financeTypeData as $financeType) {
  774. $financeTypeArr[$financeType['id']] = $financeType['name'];
  775. }
  776. }
  777. }
  778. }
  779. foreach ($data as $key => &$paid) {
  780. $paid['financeType'] = isset($financeTypeArr[$paid['financeTypeId']]) ? $financeTypeArr[$paid['financeTypeId']] : $paid['financeType'];
  781. }
  782. return $data;
  783. }
  784. //搜索的where条件拼接
  785. public function setWhere($selectParams)
  786. {
  787. $defaultDSL = [
  788. 'from' => $selectParams['offset'],
  789. 'size' => $selectParams['limit'],
  790. 'sort' => [
  791. 'createTime' => [
  792. 'order' => 'desc'
  793. ],
  794. ],
  795. ];
  796. $dsl = [];
  797. $dsl['query']['bool']['must'][] = [
  798. 'term' => ['enterpriseId' => $this->enterpriseId],
  799. ];
  800. if (isset($selectParams['keyword'])) {
  801. if (!empty($selectParams['keyword'])) {
  802. $dsl['query']['bool']['must'][] = [
  803. 'multi_match' => [
  804. 'fields' => ['no', 'sourceNo', 'currentAccountName'],
  805. 'query' => $selectParams['keyword'],
  806. 'fuzziness' => 'AUTO',
  807. ]
  808. ];
  809. }
  810. }
  811. if (!empty($selectParams['start']) && !empty($selectParams['end'])) {
  812. $dsl['query']['bool']['must'][] = [
  813. 'range' => [
  814. 'receiptTime' => [
  815. 'gte' => $selectParams['start'],
  816. 'lte' => strtotime(date('Y-m-d',$selectParams['end']).'23:59:59'),
  817. ]
  818. ]
  819. ];
  820. } else {
  821. if (!empty($selectParams['start'])) {
  822. $dsl['query']['bool']['must'][] = [
  823. 'range' => [
  824. 'receiptTime' => [
  825. 'gte' => $selectParams['start'],
  826. ]
  827. ]
  828. ];
  829. }
  830. if (!empty($selectParams['end'])) {
  831. $dsl['query']['bool']['must'][] = [
  832. 'range' => [
  833. 'receiptTime' => [
  834. 'lte' => strtotime(date('Y-m-d',$selectParams['end']).'23:59:59'),
  835. ]
  836. ]
  837. ];
  838. }
  839. }
  840. if (isset($selectParams['customerId'])) {
  841. if (!empty($selectParams['customerId'])) {
  842. $dsl['query']['bool']['filter'][] =
  843. ['term' => ['customerId' => $selectParams['customerId']]];
  844. }
  845. }
  846. if (isset($selectParams['auditStatus'])) {
  847. if (!empty($selectParams['auditStatus'])) {
  848. $dsl['query']['bool']['filter'][] =
  849. ['term' => ['auditStatus' => $selectParams['auditStatus']]];
  850. }
  851. }
  852. if (isset($selectParams['financeTypeId'])) {
  853. if (!empty($selectParams['financeTypeId'])) {
  854. $dsl['query']['bool']['filter'][] =
  855. ['term' => ['financeTypeId' => $selectParams['financeTypeId']]];
  856. }
  857. }
  858. $dsl = array_merge($defaultDSL, $dsl);
  859. return $dsl;
  860. }
  861. public function search($selectParams)
  862. {
  863. $dsl = $this->setWhere($selectParams);
  864. $result = $this->objDPaid->getSearchQueryDsl($dsl);
  865. if (isset($result['status']) && $result['status'] == 400) {
  866. if ($result['error']['reason'] == 'all shards failed') {
  867. return ResultWrapper::success([
  868. 'data' => [],
  869. 'total' => 0
  870. ]);
  871. }
  872. return ResultWrapper::fail('获取应收数据失败' . $result['error']['reason'], ErrorCode::$apiNotResult);
  873. }
  874. if (!isset($result['hits']) || $result['hits']['total'] == 0) {
  875. return ResultWrapper::success([
  876. 'data' => [],
  877. 'total' => 0
  878. ]);
  879. }
  880. $total = $result['hits']['total'];
  881. $dbResult = $result['hits']['hits'];
  882. $list = [];
  883. foreach ($dbResult as $key => &$value) {
  884. $data = [];
  885. $data = $value['_source'];
  886. $list[] = $data;
  887. }
  888. $return = [
  889. 'data' => self::format($list),
  890. 'total' => ($total) ? intval($total) : 0,
  891. ];
  892. return ResultWrapper::success($return);
  893. }
  894. /**
  895. * 付款单列表导出方法
  896. * @param $result
  897. * @return void
  898. * @throws Exception
  899. */
  900. public function exportPaid($result)
  901. {
  902. //导出到本地
  903. header("Content-type:application/vnd.ms-excel");
  904. header("Content-Disposition:filename=付款单记录表.csv");
  905. header('Cache-Control: max-age=0');
  906. $fp = fopen('php://output', 'a');
  907. $head = ['单据号','原单据号','单据日期','供应商','付款类型','金额','优惠金额','实付金额','相关店铺','审核状态']; //定义标题
  908. foreach ($head as $i => $v) {
  909. $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8'); //将中文标题转换编码,否则乱码
  910. }
  911. fputcsv($fp, $head);
  912. $limit = 10000;
  913. $num = 0; //计数器
  914. foreach ($result as $v) { //循环数据
  915. $num++;
  916. if ($num == $limit) {
  917. ob_flush(); //释放内存
  918. flush();
  919. }
  920. $rows['no'] = isset($v['no']) ? $v['no'] : '';//单据号
  921. $rows['sourceNo'] = isset($v['sourceNo']) ? $v['sourceNo'] : '';//原单据号
  922. $rows['createTime'] = isset($v['createTime']) ? date('Y-m-d',$v['createTime']): '';//单据日期
  923. $rows['supplierName'] = isset($v['supplierName']) ? $v['supplierName'] : '';//供应商
  924. $rows['financeType'] = isset($v['financeType']) ? $v['financeType'] : '';//付款类型
  925. $rows['totalMoney'] = isset($v['totalMoney']) ? $v['totalMoney'] : '';//金额
  926. $rows['totalDiscountMoney'] = isset($v['totalDiscountMoney']) ? '¥'.$v['totalDiscountMoney'] : '';//优惠金额
  927. $rows['totalFinalMoney'] = isset($v['totalFinalMoney']) ? '¥'.$v['totalFinalMoney'] : '';//实付金额
  928. $rows['shopName'] = isset($v['shopName']) ? '¥'.$v['shopName'] : '';//相关店铺
  929. $rows['auditStatus'] = isset($v['auditStatus']) === 1 ? '待审核' : '已审核';//审核状态
  930. foreach ($rows as $kk => $vv) {
  931. $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码
  932. }
  933. fputcsv($fp, $rs);
  934. $rows = [];
  935. }
  936. }
  937. /**
  938. * 付款查询核销记录
  939. */
  940. public function getAllPaidOffset($params)
  941. {
  942. $paId = $params['paidId'];
  943. $dbResult = $this->objDPaidOffset->select(['paidId'=>$paId]);
  944. if($dbResult === false){
  945. return ResultWrapper::fail($this->objDPaidOffset->error(), ErrorCode::$dberror);
  946. }
  947. if(empty($dbResult)){
  948. return ResultWrapper::success([]);
  949. }
  950. foreach ($dbResult as $key => $value){
  951. $suffix = date('Y', $value['payCreateTime']) . '_' . ceil(date('m', $value['payCreateTime']) / 3);
  952. $this->objDay->setTable('qianniao_pay_receipt_' . $this->enterpriseId . '_' . $suffix);
  953. $this->objDPaid->setTable('qianniao_paid_' . $this->enterpriseId . '_' . date('Y', $value['createTime']) . '_' . ceil(date('m', $value['createTime']) / 3));
  954. // 查询应付
  955. $payDate = $this->objDay->get($value['payReceiptId']);
  956. if($payDate === false){
  957. return ResultWrapper::fail($this->objDay->error(), ErrorCode::$dberror);
  958. }
  959. // 查询付款
  960. $paidDate = $this->objDPaid->get($value['paidId']);
  961. if($paidDate === false){
  962. return ResultWrapper::fail($this->objDPaid->error(), ErrorCode::$dberror);
  963. }
  964. if(!empty($payDate)){
  965. $dbResult[$key]['payId'] = $value['payReceiptId'];
  966. $dbResult[$key]['no'] = StatusCode::$noPrefix[18].'-'.$payDate['no'];
  967. $dbResult[$key]['offsetMoney'] = $value['offsetMoney'];
  968. $dbResult[$key]['notOffsetMoney'] = $payDate['notOffsetMoney'];
  969. $dbResult[$key]['money'] = $paidDate['totalFinalMoney'];
  970. $dbResult[$key]['purchaseId'] = $payDate['purchaseId'];
  971. $dbResult[$key]['purchaseNo'] = StatusCode::$noPrefix[2].'-'.$payDate['purchaseNo'];
  972. $dbResult[$key]['supplierName'] = $payDate['supplierName'];
  973. }
  974. }
  975. return ResultWrapper::success($dbResult);
  976. }
  977. }