MReceived.Class.php 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  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\Order\DOrder;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\StatusCode;
  13. use Mall\Framework\Core\ResultWrapper;
  14. use JinDouYun\Dao\Finance\DReceived;
  15. use JinDouYun\Dao\Finance\DReceivedIndex;
  16. use JinDouYun\Dao\Finance\DReceivedAccount;
  17. use JinDouYun\Dao\Finance\DReceivedReceiptCheckQueue;
  18. use JinDouYun\Dao\Finance\DReceiveReceiptIndex;
  19. use JinDouYun\Dao\Finance\DReceiveOffset;
  20. use JinDouYun\Dao\Finance\DReceive;
  21. use JinDouYun\Model\Order\MOrder;
  22. use JinDouYun\Model\MBaseModel;
  23. use JinDouYun\Model\Finance\MCustomerBalanceDetail;
  24. use JinDouYun\Model\Finance\MCustomerBalance;
  25. use JinDouYun\Model\Finance\MAccount;
  26. use JinDouYun\Model\Finance\MAccountDetail;
  27. use JinDouYun\Model\Finance\MFinanceType;
  28. use JinDouYun\Cache\OverviewCache;
  29. class MReceived extends MBaseModel
  30. {
  31. private $objDReceive;
  32. private $objDReceived;
  33. private $objDReceivedIndex;
  34. private $objDReceivedAccount;
  35. private $objMCustomerBalanceDetail;
  36. private $objMCustomerBalance;
  37. private $objMAccount;
  38. private $objMAccountDetail;
  39. private $objOverviewCache;
  40. private $objDReceivedReceiptCheckQueue;
  41. private $objDReceiveReceiptIndex;
  42. private $objMFinanceType;
  43. private $objDReceiveOffset;
  44. private $enterpriseId;
  45. private $userCenterId;
  46. /**
  47. * @param $enterpriseId
  48. * @param $userCenterId
  49. * @throws \Exception
  50. */
  51. public function __construct($enterpriseId, $userCenterId)
  52. {
  53. $this->userCenterId = $userCenterId;
  54. $this->enterpriseId = $enterpriseId;
  55. parent::__construct($enterpriseId, $userCenterId);
  56. $this->objMCustomerBalanceDetail = new MCustomerBalanceDetail($enterpriseId, $userCenterId);
  57. $this->objMCustomerBalance = new MCustomerBalance($enterpriseId, $userCenterId);
  58. $this->objMAccount = new MAccount($enterpriseId, $userCenterId);
  59. $this->objMAccountDetail = new MAccountDetail($enterpriseId, $userCenterId);
  60. $this->objMFinanceType = new MFinanceType($enterpriseId, $userCenterId);
  61. $this->objDReceive = new DReceive('finance');
  62. $this->objDReceived = new DReceived('finance');
  63. $this->objDReceivedIndex = new DReceivedIndex('finance');
  64. $this->objDReceivedAccount = new DReceivedAccount('finance');
  65. $this->objOverviewCache = new OverviewCache();
  66. $this->objDReceivedReceiptCheckQueue = new DReceivedReceiptCheckQueue('finance');
  67. $this->objDReceiveReceiptIndex = new DReceiveReceiptIndex('finance');
  68. $this->objDReceiveOffset = new DReceiveOffset('finance');
  69. $this->objDReceiveOffset->setTable('qianniao_receive_offset_'. $enterpriseId);
  70. $this->objDReceived->setTable('qianniao_received_' . $enterpriseId . '_' . date('Y') . '_' . ceil(date('m') / 3));
  71. $this->objDReceivedAccount->setTable('qianniao_received_account_' . $enterpriseId . '_' . date('Y') . '_' . ceil(date('m') / 3));
  72. $this->objDReceivedIndex->setTable('qianniao_received_index_' . $enterpriseId);
  73. $this->objDReceiveReceiptIndex->setTable('qianniao_receive_receipt_index_'.$enterpriseId);
  74. //$this->objDReceived->setSearchIndex('received_receipt_search')->setType('received_receipt');
  75. }
  76. /**
  77. * 添加收款单
  78. *
  79. * @param array $params 收款单数据
  80. * @param bool $autoCheck 是否需要自动审核
  81. * @return ResultWrapper
  82. */
  83. public function addReceived($params, $autoCheck= false)
  84. {
  85. $offsetDate = $params['receiptOffsetData'];
  86. unset($params['receiptOffsetData']);
  87. $accountList = $params['accountList'];
  88. unset($params['accountList']);
  89. $beginTransactionStatus = $this->objDReceived->beginTransaction();
  90. //先款后货查询收款单是否已生成
  91. $params['originId'] = empty($params['originId']) ? 0 : $params['originId'];
  92. if($params['originId'] != 0){
  93. $dbResult = $this->objDReceived->get(['originId'=>$params['originId']]);
  94. if ($dbResult === false) {
  95. return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
  96. }
  97. if(!empty($dbResult)){
  98. // 查询订单是否是先款后货的
  99. $objMOrder = new DOrder();
  100. $orderDbResult = $objMOrder->query('select * from qianniao_order_'.$this->enterpriseId.'_1 where id = '.$params['originId']);
  101. if ($orderDbResult === false) {
  102. return ResultWrapper::fail($objMOrder->error(), ErrorCode::$dberror);
  103. }
  104. if (!empty($orderDbResult) && $orderDbResult[0]['payType'] == StatusCode::$payType['cashBefore']){// 说明该先款后货的单子已经生成过
  105. return ResultWrapper::fail('该订单已经创建收款单,请先审核收款单',ErrorCode::$notAllowAccess);
  106. }
  107. }
  108. }
  109. unset($dbResult);
  110. // 生成编号
  111. $dbResult = $this->objDReceived->get('createTime >='.strtotime(date('Ymd'.'0:0:0')), 'no', 'createTime desc');
  112. if ($dbResult === false) {
  113. return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
  114. }
  115. if(empty($dbResult)){
  116. $params['no'] = createSerialNumberByDate('');
  117. }else{
  118. $params['no'] = createSerialNumberByDate($dbResult['no']);
  119. }
  120. // 索引表数据
  121. $indexData = [
  122. 'no' => $params['no'],
  123. 'receivedId' => 0,
  124. 'customerId' => $params['customerId'],
  125. 'currentAccountName' => $params['currentAccountName'],//制单人
  126. 'sourceNo' => $params['sourceNo'],
  127. 'sourceId' => getArrayItem($params,'sourceId',0),
  128. 'originId' => getArrayItem($params,'originId',0),
  129. 'originNo' => getArrayItem($params,'originNo',''),
  130. 'auditStatus' => $params['auditStatus'],
  131. 'financeTypeId' => $params['financeTypeId'],
  132. 'financeType' => $params['financeType'],
  133. 'receivedType' => empty($offsetDate) ? StatusCode::$standard : StatusCode::$delete,
  134. 'createTime' => $params['createTime'],
  135. 'updateTime' => $params['updateTime'],
  136. 'receiptTime' => $params['receiptTime'],
  137. ];
  138. $ReceivedId = $this->objDReceivedIndex->insert($indexData);
  139. if ($ReceivedId === false) {
  140. $this->objDReceived->rollBack();
  141. return ResultWrapper::fail($this->objDReceivedIndex->error(), ErrorCode::$dberror);
  142. }
  143. // 收款单表数据
  144. $params['id'] = $ReceivedId; // 收款单id = 索引表自动id
  145. $params['totalMoney'] = 0;
  146. $params['totalDiscountMoney'] = 0;
  147. $params['totalFinalMoney'] = 0;
  148. foreach ($accountList as $account) {
  149. $account['discountMoney'] = !empty($account['discountMoney']) ? $account['discountMoney'] : 0;
  150. $params['totalMoney'] = bcadd($params['totalMoney'], $account['money'], 4);//总金额
  151. $params['totalDiscountMoney'] = bcadd($params['totalDiscountMoney'], $account['discountMoney'], 4); //总优惠金额
  152. $params['totalFinalMoney'] = bcadd($params['totalFinalMoney'], bcsub($account['money'] , $account['discountMoney'], 4), 4);//总实付金额
  153. $params['offsetStatus'] = StatusCode::$delete;
  154. }
  155. $params['offsetMoney'] = 0;
  156. $params['notOffsetMoney'] = bcadd($params['totalFinalMoney'],$params['totalDiscountMoney'],2);
  157. $result = $this->objDReceived->insert($params);
  158. if ($result === false) {
  159. $this->objDReceived->rollBack();
  160. return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
  161. }
  162. //收款账户数据
  163. foreach ($accountList as $account) {
  164. $account['operatorId'] = $this->userCenterId;
  165. $account['receivedId'] = $ReceivedId;
  166. $account['finalMoney'] = bcsub($account['money'],$account['discountMoney'],2);
  167. $account = self::buildAccountData($account);
  168. $result = $this->objDReceivedAccount->insert($account);
  169. if ($result === false) {
  170. $this->objDReceived->rollBack();
  171. return ResultWrapper::fail($this->objDReceivedAccount->error(), ErrorCode::$dberror);
  172. }
  173. unset($result);
  174. }
  175. //需要自动审核的单据加到队列表里,通过cron审核
  176. if($autoCheck) {
  177. $data = [
  178. 'enterpriseId'=>$this->enterpriseId,
  179. 'receivedNo'=>$params['no'],
  180. 'createTime'=>$params['createTime']
  181. ];
  182. $result = $this->objDReceivedReceiptCheckQueue->insert($data);
  183. if ($result === false) {
  184. $this->objDReceived->rollBack();
  185. return ResultWrapper::fail($this->objDReceivedReceiptCheckQueue->error(), ErrorCode::$dberror);
  186. }
  187. }
  188. //记录核销明细
  189. if (empty($offsetDate)){
  190. if( $beginTransactionStatus ){
  191. $this->objDReceived->commit();
  192. }
  193. return ResultWrapper::success($ReceivedId);
  194. }
  195. if(!empty($offsetDate)){
  196. foreach ($offsetDate as $key => $value){
  197. $offsetDate[$key]['receivedId'] = $ReceivedId;
  198. }
  199. }
  200. unset($result);
  201. $result = $this->objDReceiveOffset->insert($offsetDate,true);
  202. if ($result === false) {
  203. $this->objDReceived->rollBack();
  204. return ResultWrapper::fail($this->objDReceiveOffset->error(), ErrorCode::$dberror);
  205. }
  206. if( $beginTransactionStatus ){
  207. $this->objDReceived->commit();
  208. }
  209. /*
  210. $_id = self::createEsDocumentId($ReceivedId, $params['createTime']);
  211. $esData = $params;
  212. $esData['id'] = $ReceivedId;
  213. $esData['enterpriseId'] = $this->enterpriseId;
  214. $result = $this->objDReceived->addUpSearchIndexDocument($esData, $_id);
  215. if (isset($result['_shards']) && isset($result['_shards']['successful']) && $result['_shards']['successful'] == 1) {
  216. //echo "es操作成功";die;
  217. }else {
  218. file_put_contents('/www/wwwroot/logs/api.junhailan.com/elasticsearch.log',date('Y-m-d H:i:s').'生成收款单es错误,错误原因'.var_export($result,true).PHP_EOL,FILE_APPEND);
  219. }
  220. */
  221. return ResultWrapper::success($ReceivedId);
  222. }
  223. /**
  224. * Doc: (des="")
  225. * User: XMing
  226. * Date: 2020/11/5
  227. * Time: 7:08 下午
  228. * @param array $params
  229. * @return array
  230. */
  231. public static function buildAccountData(array $params)
  232. {
  233. return [
  234. 'receivedId' => isset($params['receivedId']) ? $params['receivedId'] : 0,
  235. 'accountId' => isset($params['accountId']) ? $params['accountId'] : 0,
  236. 'accountNumber' => isset($params['accountNumber']) ? $params['accountNumber'] : 0,
  237. 'accountName' => getArrayItem($params, 'accountName', ''),
  238. 'money' => isset($params['money']) ? $params['money'] : 0,
  239. 'discountMoney' => isset($params['discountMoney']) ? $params['discountMoney'] : 0,
  240. 'finalMoney' => isset($params['finalMoney']) ? $params['finalMoney'] : 0,
  241. 'payWay' => isset($params['payWay']) ? $params['payWay'] : 0,
  242. 'remark' => isset($params['remark']) ? $params['remark'] : '',
  243. 'createTime' => time(),
  244. 'updateTime' => time(),
  245. 'operatorId' => isset($params['operatorId']) ? $params['operatorId'] : 0,
  246. ];
  247. }
  248. /**
  249. * 编辑收款单
  250. * @param $params
  251. * @return ResultWrapper
  252. * @throws \Exception
  253. */
  254. public function editReceived($params)
  255. {
  256. $this->objDReceived->setTable('qianniao_received_' . $this->enterpriseId . '_' . date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3));
  257. $this->objDReceivedAccount->setTable('qianniao_received_account_' . $this->enterpriseId . '_' . date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3));
  258. $beginTransactionstatus = $this->objDReceived->beginTransaction();
  259. $accountList = $params['accountList'];
  260. unset($params['accountList']);
  261. $offsetDate = $params['receiptOffsetData'];
  262. unset($params['receiptOffsetData']);
  263. $params['totalMoney'] = 0;
  264. $params['totalDiscountMoney'] = 0;
  265. $params['totalFinalMoney'] = 0;
  266. foreach ($accountList as $account) {
  267. $account['discountMoney'] = !empty($account['discountMoney']) ? $account['discountMoney'] : 0;
  268. $params['totalMoney'] += $account['money'];
  269. $params['totalDiscountMoney'] += $account['discountMoney'];
  270. $params['totalFinalMoney'] += ($account['money'] - $account['discountMoney']);
  271. }
  272. $receivedId = $params['id'];
  273. unset($params['id']);
  274. if( isset($params['no']) ){
  275. unset($params['no']);
  276. }
  277. $dbResult = $this->objDReceived->update($params, $receivedId);
  278. if ($dbResult === false) {
  279. $this->objDReceived->rollBack();
  280. return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
  281. }
  282. //索引表数据
  283. $indexData = [
  284. 'customerId' => $params['customerId'],
  285. 'currentAccountName' => $params['currentAccountName'],
  286. 'sourceNo' => $params['sourceNo'],
  287. 'auditStatus' => $params['auditStatus'],
  288. 'financeTypeId' => $params['financeTypeId'],
  289. 'financeType' => $params['financeType'],
  290. 'receivedType' => empty($offsetDate) ? StatusCode::$standard : StatusCode::$delete,
  291. 'createTime' => $params['createTime'],
  292. 'updateTime' => $params['updateTime'],
  293. 'receiptTime' => $params['receiptTime'],
  294. ];
  295. $result = $this->objDReceivedIndex->update($indexData,['id'=>$receivedId]);
  296. if ($result === false) {
  297. $this->objDReceived->rollBack();
  298. return ResultWrapper::fail($this->objDReceivedIndex->error(), ErrorCode::$dberror);
  299. }
  300. unset($result);
  301. //收款账户数据
  302. foreach ($accountList as $account) {
  303. if (isset($account['id'])) {
  304. $account['receivedId'] = $receivedId;
  305. $account['operatorId'] = $this->userCenterId;
  306. $account['updateTime'] = $params['updateTime'];
  307. $result = $this->objDReceivedAccount->update($account, $account['id']);
  308. }else{
  309. $account['receivedId'] = $receivedId;
  310. $account['operatorId'] = $this->userCenterId;
  311. $account['createTime'] = $params['createTime'];
  312. $result = $this->objDReceivedAccount->insert($account);
  313. }
  314. if ($result === false) {
  315. $this->objDReceived->rollBack();
  316. return ResultWrapper::fail($this->objDReceivedAccount->error(), ErrorCode::$dberror);
  317. }
  318. unset($result);
  319. }
  320. //更新核销明细
  321. if (empty($offsetDate)){
  322. if($beginTransactionstatus){
  323. $this->objDReceived->commit();
  324. }
  325. return ResultWrapper::success($dbResult);
  326. }
  327. if(!empty($offsetDate)){
  328. foreach ($offsetDate as $key => $value){
  329. $offsetDate[$key]['receivedId'] = $receivedId;
  330. unset($result);
  331. $result = $this->objDReceiveOffset->update($value, ['receivedId'=>$receivedId]);
  332. }
  333. }
  334. if ($result === false) {
  335. $this->objDReceived->rollBack();
  336. return ResultWrapper::fail($this->objDReceiveOffset->error(), ErrorCode::$dberror);
  337. }
  338. if($beginTransactionstatus){
  339. $this->objDReceived->commit();
  340. }
  341. /*
  342. $_id = self::createEsDocumentId($receivedId, $params['createTime']);
  343. $esData = $params;
  344. $esData['id'] = $receivedId;
  345. $esData['enterpriseId'] = $this->enterpriseId;
  346. $this->objDReceived->esupdateTypeFieldVaule($esData, $_id);
  347. */
  348. return ResultWrapper::success($dbResult);
  349. }
  350. private function createEsDocumentId($receivedId, $time)
  351. {
  352. $t = date('Y', $time) . '_' . ceil(date('m', $time) / 3);
  353. return 'EnterpriseId_' . $this->enterpriseId . '_' . $t . '_receivedId_' . $receivedId;
  354. }
  355. /**
  356. * 获取指定收款单信息
  357. * @param $receivedParams
  358. * @return ResultWrapper
  359. * @throws \Exception
  360. */
  361. public function getReceivedInfo($receivedParams)
  362. {
  363. unset($receivedParams['createTime']);
  364. $receivedIndexData = $this->objDReceivedIndex->get($receivedParams['id']);
  365. if ($receivedIndexData === false) {
  366. return ResultWrapper::fail($this->objDReceivedIndex->error(), ErrorCode::$dberror);
  367. }
  368. if(empty($receivedIndexData)){
  369. return ResultWrapper::fail('要查看的收款单索引数据不存在', ErrorCode::$contentNotExists);
  370. }
  371. $this->objDReceived->setTable('qianniao_received_' . $this->enterpriseId . '_' . date('Y', $receivedIndexData['createTime']) . '_' . ceil(date('m', $receivedIndexData['createTime']) / 3));
  372. $this->objDReceivedAccount->setTable('qianniao_received_account_' . $this->enterpriseId . '_' . date('Y', $receivedIndexData['createTime']) . '_' . ceil(date('m', $receivedIndexData['createTime']) / 3));
  373. $dbResult = $this->objDReceived->get($receivedParams);
  374. if ($dbResult === false) {
  375. return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
  376. }
  377. if(empty($dbResult)){
  378. return ResultWrapper::fail('要查看的收款单不存在', ErrorCode::$contentNotExists);
  379. }
  380. //渲染前缀
  381. $dbResult['no'] = StatusCode::$noPrefix[17] . '-' . $dbResult['no'];
  382. //渲染凭证
  383. if( isset($dbResult['extends'])&& !empty($dbResult['extends']) ){
  384. $dbResult['extends'] = json_decode($dbResult['extends'],true);
  385. }
  386. $accountData = $this->objDReceivedAccount->select(['receivedId' => $receivedParams['id'],'deleteStatus'=>StatusCode::$standard]);
  387. if ($accountData === false) {
  388. return ResultWrapper::fail($this->objDReceivedAccount->error(), ErrorCode::$dberror);
  389. }
  390. $dbResult['accountList'] = $accountData;
  391. //查出客户当前余额
  392. $dbResult['currentBalanceMoney'] = !empty($dbResult['customerId']) ? $this->objMCustomerBalance->getCustomerBalance($dbResult['customerId']) : 0;
  393. // 处理应收和收款单核销关系
  394. $dbResult['offsetDate']= [];
  395. $offsetdbResult = $this->objDReceiveOffset->select(['receivedId' => $receivedParams['id']]);
  396. if ( $offsetdbResult === false ) {
  397. return ResultWrapper::fail($this->objDReceiveOffset->error(), ErrorCode::$dberror);
  398. }
  399. if ( !empty($offsetdbResult) ){
  400. // 映射应收单对应的核销金额
  401. $receiveReceiptIdBindOffsetMoney = [];
  402. foreach ($offsetdbResult as $key => $value){
  403. $receiveReceiptIdBindOffsetMoney[$value['receiveReceiptId']] = $value['offsetMoney'];
  404. }
  405. // 批量查询应收数据
  406. $receiveReceiptIds = array_column($offsetdbResult,'receiveReceiptId');
  407. $objMReceive = new MReceive($this->enterpriseId, $this->userCenterId);
  408. $selectParams = [
  409. 'limit' => 200,
  410. 'offset' => 0,
  411. 'receiveReceiptIds' => $receiveReceiptIds,
  412. 'offsetStatus' =>$offsetdbResult['offsetStatus']
  413. ];
  414. $receiveData = $objMReceive->getAllReceive($selectParams);
  415. if(!$receiveData->isSuccess()){
  416. return ResultWrapper::fail($receiveData->getData(), $receiveData->getErrorCode());
  417. }
  418. $receiveData = $receiveData->getData();
  419. if(!empty($receiveData['total'])){
  420. foreach ($receiveData['data'] as $key => $value){
  421. $receiveData['data'][$key]['offsetMoney'] = getArrayItem($receiveReceiptIdBindOffsetMoney, $value['id'], 0);
  422. }
  423. $dbResult['offsetDate'] = $receiveData['data'];
  424. }
  425. }
  426. return ResultWrapper::success($dbResult);
  427. }
  428. /**
  429. * 删除收款单
  430. */
  431. public function updateReceivedDeleteStatus($where, $createTime)
  432. {
  433. $this->objDReceived->setTable('qianniao_received_' . $this->enterpriseId . '_' . date('Y', $createTime) . '_' . ceil(date('m', $createTime) / 3));
  434. $this->objDReceivedAccount->setTable('qianniao_received_account_' . $this->enterpriseId . '_' . date('Y', $createTime) . '_' . ceil(date('m', $createTime) / 3));
  435. $receivedId = $this->objDReceivedIndex->get($where, 'receivedId');
  436. if ($receivedId === false) {
  437. return ResultWrapper::fail($this->objDReceivedIndex->error(), ErrorCode::$dberror);
  438. }
  439. if(empty($receivedId)){
  440. return ResultWrapper::fail('要删除的收款单不存在', ErrorCode::$contentNotExists);
  441. }
  442. // 删除索引
  443. $dbResult = $this->objDReceivedIndex->update(['deleteStatus'=>StatusCode::$delete,'updateTime'=>time()], ['id'=>$receivedId]);
  444. if ($dbResult === false) {
  445. return ResultWrapper::fail($this->objDReceivedIndex->error(), ErrorCode::$dberror);
  446. }
  447. // 删除收款单单据
  448. $dbResult = $this->objDReceived->update(['deleteStatus'=>StatusCode::$delete,'updateTime'=>time()], ['id'=>$receivedId]);
  449. if ($dbResult === false) {
  450. return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
  451. }
  452. // 删除收款单账号
  453. $dbResult = $this->objDReceivedAccount->update(['deleteStatus'=>StatusCode::$delete,'updateTime'=>time(), ['receivedId'=>$receivedId]]);
  454. if ($dbResult === false) {
  455. return ResultWrapper::fail($this->objDReceivedAccount->error(), ErrorCode::$dberror);
  456. }
  457. return ResultWrapper::success('删除成功');
  458. }
  459. /**
  460. * 收款单审核 (修改 判断账户是否存在)
  461. * @param array $params
  462. * @return ResultWrapper
  463. * @throws \Exception
  464. */
  465. public function updateReceivedStatus($params)
  466. {
  467. // 切换收款单和收款单账户表
  468. $this->objDReceived->setTable('qianniao_received_' . $this->enterpriseId . '_' . date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3));
  469. $this->objDReceivedAccount->setTable('qianniao_received_account_' . $this->enterpriseId . '_' . date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3));
  470. if(empty($params['id']) && empty($params['no'])){
  471. return ResultWrapper::fail('ID和编号不能同时为空', ErrorCode::$paramError);
  472. }
  473. $getWhere=array();
  474. if(empty($params['id'])){
  475. $getWhere["no"] = $params['no'];
  476. }else{
  477. $getWhere["id"] = $params['id'];
  478. }
  479. // 查询收款单数据
  480. $receivedData = $this->objDReceived->get($getWhere);
  481. if ($receivedData === false) {
  482. return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
  483. }
  484. if(empty($receivedData)){
  485. return ResultWrapper::fail('要操作得收款单不存在', ErrorCode::$contentNotExists);
  486. }
  487. if($receivedData['auditStatus'] == StatusCode:: $auditStatus['auditPass']){
  488. return ResultWrapper::fail('当前收款单已经审核过了', ErrorCode::$notAllowAccess);
  489. }
  490. $params['id'] = $receivedData['id'];
  491. $beginTransactionStatus = $this->objDReceived->beginTransaction();
  492. // 更新收款单审核状态
  493. $dbResult = $this->objDReceived->update(['auditStatus' => StatusCode:: $auditStatus['auditPass']], ['id' => $params['id']]);
  494. if ($dbResult === false) {
  495. $this->objDReceived->rollBack();
  496. return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
  497. }
  498. unset($dbResult);
  499. // 更新收款单索引表审核状态
  500. $dbResult = $this->objDReceivedIndex->update(['auditStatus' => StatusCode:: $auditStatus['auditPass']], ['id' => $params['id']]);
  501. if ($dbResult === false) {
  502. $this->objDReceived->rollBack();
  503. return ResultWrapper::fail($this->objDReceivedIndex->error(), ErrorCode::$dberror);
  504. }
  505. unset($dbResult);
  506. // 获取客户往来余额
  507. $customerBalance = $this->objMCustomerBalance->getCustomerBalance($receivedData['customerId']);
  508. // 查出源单据已收过的金额
  509. $remark = '';
  510. if (!empty($receivedData['sourceNo'])) {
  511. $beforeReceivedMoney = 0;
  512. $where = 'sourceNo = ' . $receivedData['sourceNo'] . ' AND auditStatus = ' . StatusCode::$auditStatus['auditPass'] . ' AND receivedId !=' . $params['id'];
  513. $sourceNoReceivedResult = $this->objDReceivedIndex->get($where);
  514. if (!empty($sourceNoReceivedResult)) {
  515. $this->objDReceived->setTable('qianniao_received_' . $this->enterpriseId . '_' . date('Y', $sourceNoReceivedResult['createTime']) . '_' . ceil(date('m', $sourceNoReceivedResult['createTime']) / 3));
  516. $beforeReceivedMoney = $this->objDReceived->get_field('totalMoney', $sourceNoReceivedResult['receivedId']);
  517. }
  518. $remark = ',源单据剩余应收'. bcsub(bcsub($receivedData['sourceNoMoney'] , $beforeReceivedMoney, 4), $receivedData['totalMoney'], 4) . '元';
  519. }
  520. $detailData = [
  521. 'customerId' => $receivedData['customerId'],//'客户id',
  522. 'receiptTime' => $receivedData['receiptTime'],//'单据日期',
  523. 'sourceId' => $receivedData['id'],//'单据编号',
  524. 'sourceNo' => $receivedData['no'],//'单据编号',
  525. 'financeType' => $receivedData['financeType'],//'财务类型名称',
  526. 'financeTypeId' => $receivedData['financeTypeId'],//'财务类型id',
  527. 'originNo' => $receivedData['sourceNo'],//'源订单销货号',
  528. 'originId' => $receivedData['sourceId'],//'源订单销货号',
  529. 'salesAmount' => $receivedData['totalMoney'],//总金额
  530. 'discountMoney' => $receivedData['totalDiscountMoney'],// '优惠金额',
  531. 'customerAmount' => $receivedData['totalFinalMoney'],// '客户承担金额',
  532. 'receivableAmount' => $receivedData['totalFinalMoney'],// '应收金额',
  533. 'actualReceivableAmount' => $receivedData['totalFinalMoney'],//'实际收款金额',
  534. 'receivableBalance' => bcsub($customerBalance , $receivedData['totalFinalMoney'], 4),//'应收后余额',
  535. 'remark' => '收款单审核通过,本次优惠后实收' . $receivedData['totalFinalMoney'] . '元,优惠'.$receivedData['totalDiscountMoney'].'元'.$remark,//'备注',
  536. 'createTime' => time(),//'创建日期',
  537. 'updateTime' => time(),//'修改日期',
  538. ];
  539. // 添加客户往来余额明细,修改客户往来余额
  540. $result = $this->objMCustomerBalanceDetail->addCustomerBalanceDetail($detailData);
  541. if (!$result->isSuccess()) {
  542. $this->objDReceived->rollBack();
  543. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  544. }
  545. // 编辑客户往来余额
  546. $changedMoney = $receivedData['totalMoney'] > 0 ? -1 * $receivedData['totalFinalMoney'] : abs($receivedData['totalFinalMoney']);
  547. $result = $this->objMCustomerBalance->addCustomerBalance($receivedData['customerId'], $changedMoney);
  548. if (!$result->isSuccess()) {
  549. $this->objDReceived->rollBack();
  550. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  551. }
  552. // 查询核销记录表
  553. $offsetDbResult = $this->objDReceiveOffset->select(['receivedId'=>$receivedData['id'],'type'=>StatusCode::$standard]);
  554. if ($offsetDbResult === false) {
  555. return ResultWrapper::fail($this->objDReceiveOffset->error(), ErrorCode::$dberror);
  556. }
  557. // 核销收款单指定的应收单
  558. if (!empty($offsetDbResult)){
  559. // 手动核销指定的应收单数据
  560. foreach ($offsetDbResult as $offsetKey =>$offsetValue){
  561. // 查询应收单数据
  562. $sql = 'select * from ' . $this->objDReceiveReceiptIndex->get_Table() . ' where id='.$offsetValue['receiveReceiptId'];
  563. $firstOffset = $this->objDReceiveReceiptIndex->query($sql);
  564. if ($firstOffset === false) {
  565. $this->objDReceived->rollBack();
  566. return ResultWrapper::fail($this->objDReceiveReceiptIndex->error(), ErrorCode::$dberror);
  567. }
  568. if(empty($firstOffset)){
  569. $this->objDReceived->rollBack();
  570. return ResultWrapper::fail('要操作得应收单不存在', ErrorCode::$contentNotExists);
  571. }
  572. // 当前要处理的应收单已经核销则不做处理
  573. if( $firstOffset[0]['offsetStatus'] == StatusCode::$standard ){
  574. continue;
  575. }
  576. $autoCustomerMoney = self::autoCustomerMoney($receivedData['customerId'],bcadd($receivedData['totalFinalMoney'],$receivedData['totalDiscountMoney'],2),$receivedData['id'],$receivedData['createTime'],$firstOffset,$offsetValue['receiveReceiptId']);
  577. if(!$autoCustomerMoney->isSuccess()){
  578. $this->objDReceived->rollBack();
  579. return ResultWrapper::fail($autoCustomerMoney->getData(),$autoCustomerMoney->getErrorCode());
  580. }
  581. }
  582. }else{ // 从最老单子到最新单子的顺序依次核销
  583. // 核销应收单
  584. if (empty($receivedData['originId'])){
  585. $autoCustomerMoney = self::autoCustomerMoney($receivedData['customerId'],bcadd($receivedData['totalFinalMoney'],$receivedData['totalDiscountMoney'],2),$receivedData['id'],$receivedData['createTime'],$firstOffsetDate=[],0);
  586. if(!$autoCustomerMoney->isSuccess()){
  587. $this->objDReceived->rollBack();
  588. return ResultWrapper::fail($autoCustomerMoney->getData(),$autoCustomerMoney->getErrorCode());
  589. }
  590. }else{ //核销指定订单
  591. //查询应收表
  592. $dbResult = $this->objDReceiveReceiptIndex->get(['originId'=>$receivedData['originId']]);
  593. if ($dbResult === false) {
  594. return ResultWrapper::fail($this->objDReceiveReceiptIndex->error(), ErrorCode::$dberror);
  595. }
  596. if(!empty($dbResult)){
  597. //根据时间切表
  598. $suffix = date('Y', $dbResult['createTime']) . '_' . ceil(date('m', $dbResult['createTime']) / 3);
  599. $this->objDReceive->setTable('qianniao_receive_receipt_' . $this->enterpriseId . '_' . $suffix);
  600. $receiveResult = $this->objDReceive->get(['id'=>$dbResult['id']]);
  601. if ($receiveResult === false) {
  602. return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
  603. }
  604. //核销应收
  605. switch (true){
  606. case bcsub($receivedData['notOffsetMoney'],$receiveResult['notOffsetMoney'],2) == 0;
  607. $receiveOffsetDate = [
  608. 'offsetMoney'=> bcadd($receiveResult['offsetMoney'],$receiveResult['notOffsetMoney'],2),
  609. 'notOffsetMoney'=> 0,
  610. 'offsetStatus'=> StatusCode::$standard,
  611. 'updateTime' => time()
  612. ];
  613. $receiveIndexOffsetDate = [
  614. 'offsetStatus' => StatusCode::$standard,
  615. 'updateTime' => time()
  616. ];
  617. $receivedOffsetDate = [
  618. 'offsetMoney' => !empty($receivedData['offsetMoney']) ? $receivedData['totalFinalMoney'] : $receiveResult['notOffsetMoney'],
  619. 'notOffsetMoney' =>0,
  620. 'offsetStatus' =>StatusCode::$standard,
  621. 'updateTime' => time()
  622. ];
  623. break;
  624. case bcsub($receivedData['notOffsetMoney'],$receiveResult['notOffsetMoney'],2) > 0;
  625. $receiveOffsetDate = [
  626. 'offsetMoney'=> bcadd($receiveResult['offsetMoney'],$receiveResult['notOffsetMoney'],2),
  627. 'notOffsetMoney'=> 0,
  628. 'offsetStatus'=> StatusCode::$standard,
  629. 'updateTime' => time()
  630. ];
  631. $receiveIndexOffsetDate = [
  632. 'offsetStatus' => StatusCode::$standard,
  633. 'updateTime' => time()
  634. ];
  635. $receivedOffsetDate = [
  636. 'offsetMoney' =>$receiveResult['notOffsetMoney'],
  637. 'notOffsetMoney' =>bcsub($receivedData['notOffsetMoney'],$receiveResult['notOffsetMoney'],2),
  638. 'offsetStatus' =>StatusCode::$partion,
  639. 'updateTime' => time()
  640. ];
  641. break;
  642. case bcsub($receivedData['notOffsetMoney'],$receiveResult['notOffsetMoney'],2) < 0;
  643. $receiveOffsetDate = [
  644. 'offsetMoney'=> bcadd($receiveResult['offsetMoney'],$receivedData['notOffsetMoney'],2),
  645. 'notOffsetMoney'=> bcsub($receiveResult['notOffsetMoney'],$receivedData['notOffsetMoney'],2),
  646. 'offsetStatus'=> StatusCode::$partion,
  647. 'updateTime' => time()
  648. ];
  649. $receiveIndexOffsetDate = [
  650. 'offsetStatus' => StatusCode::$partion,
  651. 'updateTime' => time()
  652. ];
  653. $receivedOffsetDate = [
  654. 'offsetMoney' => empty($receivedData['offsetMoney']) ? $receivedData['totalFinalMoney'] :$receivedData['notOffsetMoney'],
  655. 'notOffsetMoney' =>0,
  656. 'offsetStatus' =>StatusCode::$standard,
  657. 'updateTime' => time()
  658. ];
  659. }
  660. $updateOffsetStatus = $this->objDReceive->update($receiveOffsetDate,['id'=>$receiveResult['id']]);
  661. if ($updateOffsetStatus === false) {
  662. $this->objDReceive->rollBack();
  663. return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
  664. }
  665. $updateIndexOffsetStatus = $this->objDReceiveReceiptIndex->update($receiveIndexOffsetDate, ['id' => $receiveResult['id']]);
  666. if ($updateIndexOffsetStatus === false) {
  667. $this->objDReceive->rollBack();
  668. return ResultWrapper::fail($this->objDReceiveReceiptIndex->error(), ErrorCode::$dberror);
  669. }
  670. //在线支付的收款单直接核销
  671. $updateReceivedOffset = $this->objDReceived->update($receivedOffsetDate,['id'=>$receivedData['id']]);
  672. if ($updateReceivedOffset === false) {
  673. $this->objDReceive->rollBack();
  674. return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
  675. }
  676. // 记录核销明细
  677. //核销完成,记录核销明细
  678. $updateOffset = [
  679. 'receiveReceiptId' => $receiveResult['id'],
  680. 'receivedId' => $receivedData['id'],
  681. 'offsetMoney' => $receiveOffsetDate['offsetMoney'],
  682. 'receiveCreateTime' => $receiveResult['createTime'],
  683. 'type' => StatusCode::$delete
  684. ];
  685. $result = $this->objDReceiveOffset->insert($updateOffset);
  686. if ($result === false) {
  687. $this->objDReceived->rollBack();
  688. return ResultWrapper::fail($this->objDReceiveOffset->error(), ErrorCode::$dberror);
  689. }
  690. }
  691. }
  692. }
  693. //更新客户总付款金额
  694. $result = $this->objMCustomerBalance->updateCustomerTotalPayMoney($receivedData['customerId'], $receivedData['totalFinalMoney']);
  695. if (!$result->isSuccess()) {
  696. $this->objDReceived->rollBack();
  697. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  698. }
  699. //给账户加钱
  700. $accounts = $this->objDReceivedAccount->select(['receivedId' => $receivedData['id']]);
  701. foreach ($accounts as $account) {
  702. //获取账户信息
  703. $accountResult = $this->objMAccount->getAccountInfo($account['accountId']);
  704. if (!$accountResult->isSuccess()) {
  705. $this->objDReceived->rollBack();
  706. return ResultWrapper::fail($accountResult->getData(), $accountResult->getErrorCode());
  707. }
  708. $accountInfo = $accountResult->getData();
  709. //账户明细
  710. $accountDetail = [
  711. 'accountId' => $accountInfo['id'],
  712. 'accountCode' => $accountInfo['accountCode'],
  713. 'accountName' => $accountInfo['name'],
  714. 'accountNumber' => $accountInfo['accountNumber'],
  715. 'sourceId' => $receivedData['id'],
  716. 'sourceNo' => $receivedData['no'],
  717. 'financeType' => $receivedData['financeType'],
  718. 'beginBalance' => $accountInfo['money'],
  719. 'shopId' => $receivedData['shopId'],
  720. 'shopName' => $receivedData['shopName'],
  721. 'income' => $receivedData['totalFinalMoney']>0 ? $receivedData['totalFinalMoney'] : 0,
  722. 'expend' => $receivedData['totalFinalMoney']<0 ? -1 * $receivedData['totalFinalMoney'] : 0,
  723. 'endBalance' => $accountInfo['money'] + $receivedData['totalFinalMoney'],
  724. 'contactUnit' => '客户名称:' . $receivedData['customerName'],
  725. 'supplierId' => 0,
  726. 'customerId' => $receivedData['customerId'],
  727. 'operatorId' => $this->userCenterId,
  728. 'receiveOrPayPerson' => $receivedData['currentAccountName'],
  729. 'remark' => '收款单审核通过,本次收客户' . $receivedData['totalMoney'] . '元,优惠' . $receivedData['totalDiscountMoney'] . '元,最终收款' . $receivedData['totalFinalMoney'] . '元',
  730. 'createTime' => time(),
  731. 'updateTime' => time(),
  732. ];
  733. $result = $this->objMAccountDetail->addAccountDetail($accountDetail);
  734. if (!$result->isSuccess()) {
  735. $this->objDReceived->rollBack();
  736. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  737. }
  738. $result = $this->objMAccount->updateMoney($account['accountId'], $receivedData['totalFinalMoney']);
  739. if (!$result->isSuccess()) {
  740. $this->objDReceived->rollBack();
  741. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  742. }
  743. }
  744. //审核通过记录核销金额,未核销金额,核销状态。
  745. /* $objMReceive = new MReceive($this->enterpriseId, $this->userCenterId);
  746. if( !empty($offsetDbResult) ){
  747. foreach ($offsetDbResult as $key =>$value) {
  748. // //如果$customerBalance客户余额是负的,直接自动核销订单。
  749. // if ($customerBalance<0){
  750. // $autoOffset = $objMReceive->autoOffset($customerBalance,$offsetDbResult[$key]['receiveReceiptId'],$offsetDbResult[$key]['receiveCreateTime']);
  751. // if ($autoOffset->isSuccess() === false) {
  752. // $this->objDReceived->rollBack();
  753. // return ResultWrapper::fail($autoOffset->getData(), $autoOffset->getErrorCode());
  754. // }
  755. // }else{
  756. //修改应付单核销金额,索引表核销状态。
  757. $updateOffsetDbResult = $objMReceive->updateOffsetStatus($offsetDbResult[$key]['receiveReceiptId'], $offsetDbResult[$key]['offsetMoney'], $offsetDbResult[$key]['receiveCreateTime']);
  758. if ($updateOffsetDbResult->isSuccess() === false) {
  759. $this->objDReceived->rollBack();
  760. return ResultWrapper::fail($updateOffsetDbResult->getData(), $updateOffsetDbResult->getErrorCode());
  761. }
  762. // }
  763. }
  764. }*/
  765. if ($beginTransactionStatus){
  766. $this->objDReceived->commit();
  767. }
  768. $objMOrder = new MOrder($this->userCenterId, $this->enterpriseId);
  769. //根据orderId修改订单状态
  770. if(!empty($receivedData['originId'])){
  771. //查询所有的收款
  772. $allReceivedMoney = 0;
  773. $allReceiveMoney = 0;
  774. unset($dbResult);
  775. $dbResult = $this->objDReceived->select(['originId' => $receivedData['originId']]);
  776. if ($dbResult === false) {
  777. return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
  778. }
  779. foreach ($dbResult as $receivedKey => $receivedValue){
  780. $allReceivedMoney = bcadd($allReceivedMoney,$receivedValue['totalFinalMoney'],2);
  781. }
  782. unset($dbResult);
  783. //查询订单对应的应收
  784. $dbResult = $this->objDReceiveReceiptIndex->select(['originId'=>$receivedData['originId']]);
  785. foreach ($dbResult as $receiptIndexKey => $receiptIndexValue){
  786. $suffix = date('Y', $receiptIndexValue['createTime']) . '_' . ceil(date('m', $receiptIndexValue['createTime']) / 3);
  787. $this->objDReceive->setTable('qianniao_receive_receipt_' . $this->enterpriseId . '_' . $suffix);
  788. $objReceiveResult = $this->objDReceive->get(['id'=>$receiptIndexValue['id']]);
  789. if ($objReceiveResult === false) {
  790. return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
  791. }
  792. if(!empty($objReceiveResult)){
  793. $allReceiveMoney = $objReceiveResult['offsetMoney'];
  794. }
  795. }
  796. //判断订单状态
  797. $orderTable = $objMOrder->getOrderTable($this->enterpriseId,$this->userCenterId);
  798. $objDOrder = new DOrder();
  799. $statusOrder = $objDOrder->query('select * from '.$orderTable.' where id = '.$receivedData['originId']);
  800. //收银端收款有时候会没有订单
  801. $xpayAmount = empty($statusOrder)?0:$statusOrder[0]['payAmount'];//订单实际需要支付金额
  802. if( bcsub(bcsub($allReceivedMoney,$allReceiveMoney,2),$xpayAmount,2) >= 0 ){//已支付
  803. $orderPayStatus = [
  804. 'payStatus' => StatusCode::$standard,
  805. 'payTime' => time()
  806. ];
  807. }else{
  808. $orderPayStatus = [
  809. 'payStatus' => StatusCode::$partion,
  810. 'payTime' => time()
  811. ];
  812. }
  813. $orderDbResult = $objMOrder->updateOrderPayData($orderPayStatus, ['id' => $receivedData['originId']]);
  814. if ($orderDbResult->isSuccess() === false) {
  815. return ResultWrapper::fail($orderDbResult->getData(), $orderDbResult->getErrorCode());
  816. }
  817. }else{
  818. //查询当前核销表
  819. unset($offsetDbResult);
  820. $offsetDbResult = $this->objDReceiveOffset->select(['receivedId'=>$receivedData['id'],'type'=>StatusCode::$standard]);
  821. if ($offsetDbResult === false) {
  822. return ResultWrapper::fail($this->objDReceiveOffset->error(), ErrorCode::$dberror);
  823. }
  824. //财务完成,修改订单表支付状态
  825. foreach ($offsetDbResult as $key =>$value) {
  826. //根据应付单获取orderId,用来修改订单支付状态
  827. $suffix = date('Y', $offsetDbResult[$key]['receiveCreateTime']) . '_' . ceil(date('m', $offsetDbResult[$key]['receiveCreateTime']) / 3);
  828. $this->objDReceive->setTable('qianniao_receive_receipt_' . $this->enterpriseId . '_' . $suffix);
  829. $orderIdDbResult = $this->objDReceive->get(['id' => $offsetDbResult[$key]['receiveReceiptId']]);
  830. if ($orderIdDbResult === false) {
  831. return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
  832. }
  833. if (empty($orderIdDbResult)) {
  834. return ResultWrapper::fail('应付单不存在', ErrorCode::$notAllowAccess);
  835. }
  836. //判断核销金额,未核销金额,来计算payStatus的状态
  837. $updateOrderPayData1 = ['payStatus' => StatusCode::$standard, 'payTime' => time()];
  838. $updateOrderPayData2 = ['id' => $orderIdDbResult['originId']];
  839. switch (true) {
  840. case $orderIdDbResult['offsetMoney'] == $orderIdDbResult['receiveMoney'];
  841. $updateOrderPayData1['payStatus'] = StatusCode::$standard;
  842. break;
  843. case $orderIdDbResult['offsetMoney'] == 0;
  844. $updateOrderPayData1['payStatus'] = StatusCode::$delete;
  845. break;
  846. case $orderIdDbResult['offsetMoney'] > 0 && $orderIdDbResult['offsetMoney'] < $orderIdDbResult['receiveMoney'];
  847. $updateOrderPayData1['payStatus'] = StatusCode::$partion;
  848. $updateOrderPayData1['notPayMoney'] = $orderIdDbResult['notOffsetMoney'];
  849. }
  850. $orderDbResult = $objMOrder->updateOrderPayData($updateOrderPayData1, $updateOrderPayData2);
  851. if ($orderDbResult->isSuccess() === false) {
  852. return ResultWrapper::fail($orderDbResult->getData(), $orderDbResult->getErrorCode());
  853. }
  854. }
  855. }
  856. //今日收款金额redis
  857. $this->objOverviewCache->saveBusinessOverview($this->enterpriseId, 'todayReceivedMoney', $receivedData['totalFinalMoney']);
  858. $this->objOverviewCache->saveBusinessOverview($this->enterpriseId, 'todayReceivedMoney', $receivedData['totalFinalMoney'], $receivedData['shopId']);
  859. /*
  860. $_id = self::createEsDocumentId($receivedData['id'], $receivedData['createTime']);
  861. $this->objDReceived->esupdateTypeFieldVaule(['auditStatus' => StatusCode::$auditStatus['auditPass']], $_id);
  862. */
  863. // return ResultWrapper::success($dbResult);
  864. return ResultWrapper::success(null);
  865. }
  866. /**
  867. * 自动核销客户余额
  868. * 客户id,收款单总的money进行自动核销
  869. */
  870. public function autoCustomerMoney($customerId,$money,$id,$createTime,$firstOffset,$receiveReceiptId)
  871. {
  872. $ReceivedDbResult = $this->objDReceived->get($id);
  873. if ($ReceivedDbResult === false) {
  874. $this->objDReceived->rollBack();
  875. return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
  876. }
  877. // 随便核销
  878. if ($receiveReceiptId == 0){
  879. // 查询这个客户所有未核销的应收单
  880. $sql = 'select * from ' . $this->objDReceiveReceiptIndex->get_Table() . ' where customerId='.$customerId.' and offsetStatus != '. StatusCode::$standard;
  881. $dbResultOffset = $this->objDReceiveReceiptIndex->query($sql);
  882. }else{ // 核销指定的应收单
  883. $sql = 'select * from ' . $this->objDReceiveReceiptIndex->get_Table() . ' where customerId='.$customerId.' and offsetStatus != '. StatusCode::$standard.' and id !='. $receiveReceiptId;
  884. $dbResultOffset = $this->objDReceiveReceiptIndex->query($sql);
  885. }
  886. $dbResultIndex = array_merge($firstOffset,$dbResultOffset);
  887. if ($dbResultIndex === false) {
  888. return ResultWrapper::fail($this->objDReceiveReceiptIndex->error(), ErrorCode::$dberror);
  889. }
  890. if(empty($dbResultIndex)){//如果为空,说明当前单子只做付款,预付,不做核销.
  891. return ResultWrapper::success([]);
  892. }
  893. $surplusMoney = $money; // 剩余可核销金额
  894. $receivedOffsetMoney = 0; // 收款单累计核销金额
  895. $receivedNotOffsetMoney = $money; // 收款单未核销金额
  896. $updateDReceive = null;//报错修改
  897. foreach ($dbResultIndex as $key => $value){
  898. // 收款单钱核销完了,则不再进行核销
  899. if($surplusMoney <= 0){
  900. break;
  901. }
  902. // 切应收分表
  903. $suffix = date('Y', $value['createTime']) . '_' . ceil(date('m', $value['createTime']) / 3);
  904. $this->objDReceive->setTable('qianniao_receive_receipt_' . $this->enterpriseId . '_' . $suffix);
  905. // 查出当前应收单的信息
  906. $receiveDate = $this->objDReceive->get($value['id']);
  907. if ($receiveDate === false) {
  908. return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
  909. }
  910. if(empty($receiveDate)){
  911. continue;
  912. }
  913. // 只核销金额大于0的,小于0是退款单
  914. if ($receiveDate['receiveMoney']<0) {
  915. continue;
  916. }
  917. switch (true) {
  918. case $receiveDate['notOffsetMoney'] <= $surplusMoney; // 未核销金额小于剩余可核销金额,整单核销
  919. $offsetMoney = $receiveDate['notOffsetMoney'];
  920. $notOffsetMoney = 0;
  921. $offsetStatus = StatusCode::$standard;
  922. $receivedOffsetMoney = bcadd($receivedOffsetMoney, $receiveDate['notOffsetMoney'], 2);
  923. $receivedNotOffsetMoney = bcsub($receivedNotOffsetMoney, $receiveDate['notOffsetMoney'], 2);
  924. $updateReceived = [
  925. 'offsetMoney' => $receivedOffsetMoney,
  926. 'notOffsetMoney' => $receivedNotOffsetMoney,
  927. 'offsetStatus' => ($receivedNotOffsetMoney ==0 ) ? StatusCode::$standard : StatusCode::$partion,
  928. ];
  929. $updateReceivedIndex = [
  930. 'offsetStatus' => ($receivedNotOffsetMoney ==0 ) ? StatusCode::$standard : StatusCode::$partion,
  931. ];
  932. break;
  933. case $receiveDate['notOffsetMoney'] > $surplusMoney; // 未核销金额大于剩余可核销金额,部分核销
  934. $offsetMoney = $surplusMoney;
  935. $notOffsetMoney = bcsub($receiveDate['notOffsetMoney'], $offsetMoney, 2);
  936. $offsetStatus = StatusCode::$partion;
  937. $receivedOffsetMoney = bcadd($receivedOffsetMoney, $surplusMoney, 2);
  938. $updateReceived = [
  939. 'offsetMoney' => $receivedOffsetMoney,
  940. 'notOffsetMoney' => 0,
  941. 'offsetStatus' => StatusCode::$standard,
  942. ];
  943. $updateReceivedIndex = [
  944. 'offsetStatus' => StatusCode::$standard,
  945. ];
  946. break;
  947. }
  948. $surplusMoney = bcsub($surplusMoney, $receiveDate['notOffsetMoney'], 2);
  949. //判断该单据是否已经审核
  950. if ($receiveDate['auditStatus'] == StatusCode:: $auditStatus['auditPass']) {
  951. $update = [
  952. 'offsetMoney' => bcadd($receiveDate['offsetMoney'],$offsetMoney),
  953. 'notOffsetMoney' => $notOffsetMoney,
  954. 'offsetStatus' => $offsetStatus,
  955. ];
  956. } else {
  957. $update = [
  958. 'offsetMoney' => bcadd($receiveDate['offsetMoney'],$offsetMoney),
  959. 'notOffsetMoney' => $notOffsetMoney,
  960. 'offsetStatus' => $offsetStatus,
  961. 'auditStatus' => StatusCode:: $auditStatus['auditPass']
  962. ];
  963. }
  964. //判断index单据是否已经审核
  965. if ($value['auditStatus'] == StatusCode:: $auditStatus['auditPass']) {
  966. $updateIndex = [
  967. 'offsetStatus' => $offsetStatus,
  968. ];
  969. } else {
  970. $updateIndex = [
  971. 'offsetStatus' => $offsetStatus,
  972. 'auditStatus' => StatusCode:: $auditStatus['auditPass']
  973. ];
  974. }
  975. $updateDReceive = $this->objDReceive->update($update, ['id' => $value['id']]);
  976. if ($updateDReceive === false) {
  977. $this->objDReceived->rollBack();
  978. return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
  979. }
  980. $updateDReceiveReceiptIndex = $this->objDReceiveReceiptIndex->update($updateIndex, ['id' => $value['id']]);
  981. if ($updateDReceiveReceiptIndex === false) {
  982. $this->objDReceived->rollBack();
  983. return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
  984. }
  985. //应收核销完成,记录核销信息
  986. $this->objDReceived->setTable('qianniao_received_' . $this->enterpriseId . '_' . date('Y', $createTime) . '_' . ceil(date('m', $createTime) / 3));
  987. $updateReceivedResult = $this->objDReceived->update($updateReceived, ['id' => $id]);
  988. if ($updateReceivedResult === false) {
  989. $this->objDReceived->rollBack();
  990. return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
  991. }
  992. //核销完成更新index表的核销状态
  993. $updateReceivedIndexResult = $this->objDReceivedIndex->update($updateReceivedIndex, ['id' => $ReceivedDbResult['id'], 'receiptTime' => $ReceivedDbResult['receiptTime']]);
  994. if ($updateReceivedIndexResult === false) {
  995. $this->objDReceived->rollBack();
  996. return ResultWrapper::fail($this->objDReceivedIndex->error(), ErrorCode::$dberror);
  997. }
  998. //如果已经记过,需要剔除
  999. if (!empty($firstOffset) && count($firstOffset) - 1 >= $key) {//剔除
  1000. continue;
  1001. } else {
  1002. //核销完成,记录核销明细
  1003. $updateOffset = [
  1004. 'receiveReceiptId' => $value['id'],
  1005. 'receivedId' => $id,
  1006. 'offsetMoney' => $offsetMoney,
  1007. 'receiveCreateTime' => $value['createTime']
  1008. ];
  1009. $result = $this->objDReceiveOffset->insert($updateOffset);
  1010. if ($result === false) {
  1011. $this->objDReceived->rollBack();
  1012. return ResultWrapper::fail($this->objDReceiveOffset->error(), ErrorCode::$dberror);
  1013. }
  1014. }
  1015. }
  1016. return ResultWrapper::success($updateDReceive);
  1017. }
  1018. /**
  1019. * 获取所有收款单数据
  1020. * @param array $selectParams 过滤条件
  1021. * @return ResultWrapper
  1022. * @throws \Exception
  1023. */
  1024. public function getAllReceived($selectParams,$export = 0)
  1025. {
  1026. $limit = $selectParams['limit'];
  1027. unset($selectParams['limit']);
  1028. $offset = $selectParams['offset'];
  1029. unset($selectParams['offset']);
  1030. if($export){
  1031. $limit = 999;
  1032. $offset = 0;
  1033. }
  1034. //组装sql where条件
  1035. $whereSql = ' where deleteStatus = '.StatusCode::$standard;
  1036. if (isset($selectParams['no']) && !empty($selectParams['no'])) {
  1037. //no编号切割
  1038. $explodeNo = explode("-",$selectParams['no']);
  1039. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  1040. $whereSql .= $where . ' no = "' . $explodeNo[1] . '-' . $explodeNo[2] . '"';
  1041. }
  1042. if (isset($selectParams['financeTypeId']) && !empty($selectParams['financeTypeId'])) {
  1043. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  1044. $whereSql .= $where . ' financeTypeId = ' . $selectParams['financeTypeId'];
  1045. }
  1046. if (isset($selectParams['auditStatus']) && !empty($selectParams['auditStatus'])) {
  1047. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  1048. $whereSql .= $where . ' auditStatus = ' . $selectParams['auditStatus'];
  1049. }
  1050. if (isset($selectParams['customerId']) && !empty($selectParams['customerId'])) {
  1051. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  1052. $whereSql .= $where . ' customerId = ' . $selectParams['customerId'];
  1053. }
  1054. if ( (isset($selectParams['start']) && !empty($selectParams['start']))&&(isset($selectParams['end']) && !empty($selectParams['end'])) ) {
  1055. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  1056. $whereSql .= $where . ' createTime BETWEEN ' . $selectParams['start'] . ' AND '. $selectParams['end'];
  1057. }
  1058. $sql = 'SELECT * FROM ' .$this->objDReceivedIndex->get_Table(). $whereSql . ' ORDER BY createTime DESC LIMIT ' . $offset . ' , ' . $limit;
  1059. $receivedIndexResult = $this->objDReceivedIndex->query($sql);
  1060. $tableSuffix = [];
  1061. foreach ($receivedIndexResult as $receivedIndex) {
  1062. $k = date('Y', $receivedIndex['createTime']) . '_' . ceil(date('m', $receivedIndex['createTime']) / 3);
  1063. $tableSuffix[$k][] = $receivedIndex['id'];
  1064. }
  1065. $receivedResult = [];
  1066. foreach ($tableSuffix as $suffix => $receivedIds) {
  1067. $this->objDReceived->setTable('qianniao_received_' . $this->enterpriseId . '_' . $suffix);
  1068. $dbResult = $this->objDReceived->select($receivedIds, '*', 'createTime desc');
  1069. if ($dbResult === false) {
  1070. return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
  1071. }
  1072. if (!empty($dbResult)){
  1073. //渲染前缀
  1074. foreach ($dbResult as $key => $value){
  1075. $dbResult[$key]['no'] = StatusCode::$noPrefix[17] . '-' . $value['no'];
  1076. if(!empty($value['sourceNo'])){
  1077. $dbResult[$key]['sourceNo'] = StatusCode::$noPrefix[1] . '-' . $value['sourceNo'];
  1078. }
  1079. $dbResult[$key]['extends'] = json_decode($dbResult[$key]['extends'],true);
  1080. }
  1081. }
  1082. $receivedResult = array_merge($receivedResult, $dbResult);
  1083. }
  1084. $totalSql = 'SELECT COUNT(1) as count FROM ' .$this->objDReceivedIndex->get_Table() . $whereSql;
  1085. $dbTotalResult = $this->objDReceivedIndex->query($totalSql);
  1086. if ($dbTotalResult === false) {
  1087. return ResultWrapper::fail($this->objDReceivedIndex->error(), ErrorCode::$dberror);
  1088. }
  1089. if(empty($dbTotalResult)){
  1090. return ResultWrapper::success([]);
  1091. }
  1092. $return = [
  1093. 'data' => self::format($receivedResult),
  1094. 'total' => $dbTotalResult[0]['count']
  1095. ];
  1096. //导出
  1097. if($export){
  1098. self::exportReceived($return['data']);
  1099. exit;
  1100. }
  1101. return ResultWrapper::success($return);
  1102. }
  1103. public function format($data)
  1104. {
  1105. $financeTypeIds = [];
  1106. foreach ($data as $key => $received) {
  1107. $financeTypeIds[] = $received['financeTypeId'];
  1108. }
  1109. $financeTypeArr = [];
  1110. if ($financeTypeIds) {
  1111. $financeTypeResult = $this->objMFinanceType->getAllFinanceTypeNoPage(['id'=>array_unique(array_filter($financeTypeIds))]);
  1112. if ($financeTypeResult->isSuccess()) {
  1113. $financeTypeData = $financeTypeResult->getData();
  1114. if (!empty($financeTypeData)) {
  1115. foreach ($financeTypeData as $financeType) {
  1116. $financeTypeArr[$financeType['id']] = $financeType['name'];
  1117. }
  1118. }
  1119. }
  1120. }
  1121. foreach ($data as $key => &$received) {
  1122. $received['financeType'] = isset($financeTypeArr[$received['financeTypeId']]) ? $financeTypeArr[$received['financeTypeId']] : $received['financeType'];
  1123. }
  1124. return $data;
  1125. }
  1126. //搜索的where条件拼接
  1127. public function setWhere($selectParams)
  1128. {
  1129. $defaultDSL = [
  1130. 'from' => $selectParams['offset'],
  1131. 'size' => $selectParams['limit'],
  1132. 'sort' => [
  1133. 'createTime' => [
  1134. 'order' => 'desc'
  1135. ],
  1136. ],
  1137. ];
  1138. $dsl = [];
  1139. $dsl['query']['bool']['must'][] = [
  1140. 'term' => ['enterpriseId' => $this->enterpriseId],
  1141. ];
  1142. if (isset($selectParams['keyword'])) {
  1143. if (!empty($selectParams['keyword'])) {
  1144. $dsl['query']['bool']['must'][] = [
  1145. 'multi_match' => [
  1146. 'fields' => ['no', 'sourceNo', 'currentAccountName'],
  1147. 'query' => $selectParams['keyword'],
  1148. 'fuzziness' => 'AUTO',
  1149. ]
  1150. ];
  1151. }
  1152. }
  1153. if (!empty($selectParams['start']) && !empty($selectParams['end'])) {
  1154. $dsl['query']['bool']['must'][] = [
  1155. 'range' => [
  1156. 'receiptTime' => [
  1157. 'gte' => $selectParams['start'],
  1158. 'lte' => strtotime(date('Y-m-d', $selectParams['end']) . '23:59:59'),
  1159. ]
  1160. ]
  1161. ];
  1162. } else {
  1163. if (!empty($selectParams['start'])) {
  1164. $dsl['query']['bool']['must'][] = [
  1165. 'range' => [
  1166. 'receiptTime' => [
  1167. 'gte' => $selectParams['start'],
  1168. ]
  1169. ]
  1170. ];
  1171. }
  1172. if (!empty($selectParams['end'])) {
  1173. $dsl['query']['bool']['must'][] = [
  1174. 'range' => [
  1175. 'receiptTime' => [
  1176. 'lte' => strtotime(date('Y-m-d', $selectParams['end']) . '23:59:59'),
  1177. ]
  1178. ]
  1179. ];
  1180. }
  1181. }
  1182. if (isset($selectParams['customerId'])) {
  1183. if (!empty($selectParams['customerId'])) {
  1184. $dsl['query']['bool']['filter'][] =
  1185. ['term' => ['customerId' => $selectParams['customerId']]];
  1186. }
  1187. }
  1188. if (isset($selectParams['auditStatus'])) {
  1189. if (!empty($selectParams['auditStatus'])) {
  1190. $dsl['query']['bool']['filter'][] =
  1191. ['term' => ['auditStatus' => $selectParams['auditStatus']]];
  1192. }
  1193. }
  1194. if (isset($selectParams['financeTypeId'])) {
  1195. if (!empty($selectParams['financeTypeId'])) {
  1196. $dsl['query']['bool']['filter'][] =
  1197. ['term' => ['financeTypeId' => $selectParams['financeTypeId']]];
  1198. }
  1199. }
  1200. $dsl = array_merge($defaultDSL, $dsl);
  1201. return $dsl;
  1202. }
  1203. // public function search($selectParams)
  1204. // {
  1205. // $dsl = $this->setWhere($selectParams);
  1206. // $result = $this->objDReceived->getSearchQueryDsl($dsl);
  1207. //
  1208. // if (isset($result['status']) && $result['status'] == 400) {
  1209. // if ($result['error']['reason'] == 'all shards failed') {
  1210. // return ResultWrapper::success([
  1211. // 'data' => [],
  1212. // 'total' => 0
  1213. // ]);
  1214. // }
  1215. // return ResultWrapper::fail('获取应收数据失败' . $result['error']['reason'], ErrorCode::$apiNotResult);
  1216. // }
  1217. //
  1218. // if (!isset($result['hits']) || $result['hits']['total'] == 0) {
  1219. // return ResultWrapper::success([
  1220. // 'data' => [],
  1221. // 'total' => 0
  1222. // ]);
  1223. // }
  1224. // $total = $result['hits']['total'];
  1225. // $dbResult = $result['hits']['hits'];
  1226. //
  1227. // $list = [];
  1228. // foreach ($dbResult as $key => &$value) {
  1229. // $data = [];
  1230. // $data = $value['_source'];
  1231. // $list[] = $data;
  1232. // }
  1233. //
  1234. // $return = [
  1235. // 'data' => $list,
  1236. // 'total' => ($total) ? intval($total) : 0,
  1237. // ];
  1238. // return ResultWrapper::success($return);
  1239. // }
  1240. /**
  1241. * 根据订单no查询应收单
  1242. * @param $params
  1243. * @return ResultWrapper
  1244. * @throws \Exception
  1245. */
  1246. public function getReceivedByOrder($params)
  1247. {
  1248. $this->objDReceiveReceiptIndex->setTable('qianniao_receive_receipt_index_'.$this->enterpriseId);
  1249. $dbResult = $this->objDReceiveReceiptIndex->select($params);
  1250. if ($dbResult === false) {
  1251. return ResultWrapper::fail($this->objDReceiveReceiptIndex->error(), ErrorCode::$dberror);
  1252. }
  1253. if(empty($dbResult)){
  1254. return ResultWrapper::success($dbResult);
  1255. }
  1256. $receiveReceiptIndex = array_shift($dbResult);
  1257. unset($dbResult);
  1258. $tableName = 'qianniao_receive_receipt_'.$this->enterpriseId.'_'.date('Y', $receiveReceiptIndex['createTime']) . '_' . ceil(date('m', $receiveReceiptIndex['createTime']) / 3);
  1259. $sql = 'select * from '.$tableName.' where id = '.$receiveReceiptIndex['id'];
  1260. $dbResult = $this->objDReceiveReceiptIndex->query($sql);
  1261. if($dbResult === false){
  1262. return ResultWrapper::fail($this->objDReceiveReceiptIndex->error(), ErrorCode::$dberror);
  1263. }
  1264. $receiveReceipt = !empty($dbResult) ? array_shift($dbResult) : $dbResult;
  1265. return ResultWrapper::success($receiveReceipt);
  1266. }
  1267. /**
  1268. * 获取今日收款总额
  1269. * @param null $shopId
  1270. * @return ResultWrapper
  1271. */
  1272. public function getTodayReceivedMoney($shopId = null)
  1273. {
  1274. $result = $this->objOverviewCache->getBusinessOverview($this->enterpriseId, 'todayReceivedMoney', $shopId);
  1275. //TODO:拿不到数据去ES筛选
  1276. return $result ? $result : 0;
  1277. }
  1278. /**
  1279. * 导出方法
  1280. *
  1281. * @param $receivedData
  1282. */
  1283. private static function exportReceived($receivedData)
  1284. {
  1285. //导出到本地
  1286. header("Content-type:application/vnd.ms-excel");
  1287. header("Content-Disposition:filename=收款列表记录.csv");
  1288. header('Cache-Control: max-age=0');
  1289. $fp = fopen('php://output', 'a');
  1290. $head = ['单据号','原单据号', '单据日期', '客户', '收购款类型', '金额', '优惠金额', '实收金额','相关店铺', '审核状态']; //定义标题
  1291. foreach ($head as $i => $v) {
  1292. $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8'); //将中文标题转换编码,否则乱码
  1293. }
  1294. fputcsv($fp, $head);
  1295. $limit = 10000;
  1296. $num = 0;//计数器
  1297. foreach ($receivedData as $v) {
  1298. //循环数据
  1299. $num++;
  1300. if ($num == $limit) {
  1301. ob_flush();//释放内存
  1302. flush();
  1303. }
  1304. $rows['no'] = isset($v['no']) ? $v['no'] : '';//单据号
  1305. $rows['sourceNo'] = isset($v['sourceNo']) ? $v['sourceNo'] : '';//原单据号
  1306. $rows['createTime'] = isset($v['createTime']) ? date('Y-m-d H:i:s',$v['createTime']) : '';//单据日期
  1307. $rows['customerName'] = isset($v['customerName']) ? $v['customerName'] : '';//客户
  1308. $rows['financeType'] = isset($v['financeType']) ? $v['financeType'] : '';//收购款类型
  1309. $rows['totalMoney'] = isset($v['totalMoney']) ? $v['totalMoney'] : '';//金额
  1310. $rows['totalDiscountMoney'] = isset($v['totalDiscountMoney']) ? $v['totalDiscountMoney'] : '';//优惠金额
  1311. $rows['totalFinalMoney'] = isset($v['totalFinalMoney']) ? $v['totalFinalMoney'] : '';//实收金额
  1312. $rows['shopName'] = isset($v['shopName']) ? $v['shopName'] : '';//相关店铺
  1313. $rows['auditStatus'] = $v['auditStatus'] === 1 ? '待审核' : '审核通过';//审核状态
  1314. foreach ($rows as $kk => $vv) {
  1315. $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码
  1316. }
  1317. fputcsv($fp, $rs);
  1318. $rows = [];
  1319. }
  1320. exit;
  1321. }
  1322. /**
  1323. * 收款查询核销记录
  1324. */
  1325. public function getAllReceivedOffset($params)
  1326. {
  1327. $receivedId = $params['receivedId'];
  1328. $dbResult = $this->objDReceiveOffset->select(['receivedId'=>$receivedId]);
  1329. if($dbResult === false){
  1330. return ResultWrapper::fail($this->objDReceiveOffset->error(), ErrorCode::$dberror);
  1331. }
  1332. if(empty($dbResult)){
  1333. return ResultWrapper::success([]);
  1334. }
  1335. foreach ($dbResult as $key => $value){
  1336. $suffix = date('Y', $value['receiveCreateTime']) . '_' . ceil(date('m', $value['receiveCreateTime']) / 3);
  1337. $this->objDReceive->setTable('qianniao_receive_receipt_' . $this->enterpriseId . '_' . $suffix);
  1338. $this->objDReceived->setTable('qianniao_received_' . $this->enterpriseId . '_' . date('Y', $value['createTime']) . '_' . ceil(date('m', $value['createTime']) / 3));
  1339. //查询应收
  1340. $receiveDate = $this->objDReceive->get($value['receiveReceiptId']);
  1341. if($receiveDate === false){
  1342. return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
  1343. }
  1344. //查询收款
  1345. $receivedDate = $this->objDReceived->get($value['receivedId']);
  1346. if($receiveDate === false){
  1347. return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
  1348. }
  1349. if(!empty($receiveDate)){
  1350. $dbResult[$key]['receiveId'] = $value['receiveReceiptId'];
  1351. $dbResult[$key]['no'] = StatusCode::$noPrefix[16].'-'.$receiveDate['no'];
  1352. $dbResult[$key]['offsetMoney'] = $value['offsetMoney'];
  1353. $dbResult[$key]['notOffsetMoney'] = $receiveDate['notOffsetMoney'];
  1354. $dbResult[$key]['money'] = $receivedDate['totalFinalMoney'];
  1355. $dbResult[$key]['originId'] = $receiveDate['originId'];
  1356. $dbResult[$key]['originNo'] = StatusCode::$noPrefix[1].'-'.$receiveDate['originNo'];
  1357. $dbResult[$key]['customerName'] = $receiveDate['customerName'];
  1358. $dbResult[$key]['customerId'] = $receiveDate['customerId'];
  1359. }
  1360. }
  1361. return ResultWrapper::success($dbResult);
  1362. }
  1363. }