123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482 |
- <?php
- /**
- * 收款单管理模块
- * Created by PhpStorm.
- * User: wxj
- * Date: 2019/10/30
- * Time: 14:02
- */
- namespace JinDouYun\Model\Finance;
- use JinDouYun\Dao\Order\DOrder;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ResultWrapper;
- use JinDouYun\Dao\Finance\DReceived;
- use JinDouYun\Dao\Finance\DReceivedIndex;
- use JinDouYun\Dao\Finance\DReceivedAccount;
- use JinDouYun\Dao\Finance\DReceivedReceiptCheckQueue;
- use JinDouYun\Dao\Finance\DReceiveReceiptIndex;
- use JinDouYun\Dao\Finance\DReceiveOffset;
- use JinDouYun\Dao\Finance\DReceive;
- use JinDouYun\Model\Order\MOrder;
- use JinDouYun\Model\MBaseModel;
- use JinDouYun\Model\Finance\MCustomerBalanceDetail;
- use JinDouYun\Model\Finance\MCustomerBalance;
- use JinDouYun\Model\Finance\MAccount;
- use JinDouYun\Model\Finance\MAccountDetail;
- use JinDouYun\Model\Finance\MFinanceType;
- use JinDouYun\Cache\OverviewCache;
- class MReceived extends MBaseModel
- {
- private $objDReceive;
- private $objDReceived;
- private $objDReceivedIndex;
- private $objDReceivedAccount;
- private $objMCustomerBalanceDetail;
- private $objMCustomerBalance;
- private $objMAccount;
- private $objMAccountDetail;
- private $objOverviewCache;
- private $objDReceivedReceiptCheckQueue;
- private $objDReceiveReceiptIndex;
- private $objMFinanceType;
- private $objDReceiveOffset;
- private $enterpriseId;
- private $userCenterId;
- /**
- * @param $enterpriseId
- * @param $userCenterId
- * @throws \Exception
- */
- public function __construct($enterpriseId, $userCenterId)
- {
- $this->userCenterId = $userCenterId;
- $this->enterpriseId = $enterpriseId;
- parent::__construct($enterpriseId, $userCenterId);
- $this->objMCustomerBalanceDetail = new MCustomerBalanceDetail($enterpriseId, $userCenterId);
- $this->objMCustomerBalance = new MCustomerBalance($enterpriseId, $userCenterId);
- $this->objMAccount = new MAccount($enterpriseId, $userCenterId);
- $this->objMAccountDetail = new MAccountDetail($enterpriseId, $userCenterId);
- $this->objMFinanceType = new MFinanceType($enterpriseId, $userCenterId);
- $this->objDReceive = new DReceive('finance');
- $this->objDReceived = new DReceived('finance');
- $this->objDReceivedIndex = new DReceivedIndex('finance');
- $this->objDReceivedAccount = new DReceivedAccount('finance');
- $this->objOverviewCache = new OverviewCache();
- $this->objDReceivedReceiptCheckQueue = new DReceivedReceiptCheckQueue('finance');
- $this->objDReceiveReceiptIndex = new DReceiveReceiptIndex('finance');
- $this->objDReceiveOffset = new DReceiveOffset('finance');
- $this->objDReceiveOffset->setTable('qianniao_receive_offset_'. $enterpriseId);
- $this->objDReceived->setTable('qianniao_received_' . $enterpriseId . '_' . date('Y') . '_' . ceil(date('m') / 3));
- $this->objDReceivedAccount->setTable('qianniao_received_account_' . $enterpriseId . '_' . date('Y') . '_' . ceil(date('m') / 3));
- $this->objDReceivedIndex->setTable('qianniao_received_index_' . $enterpriseId);
- $this->objDReceiveReceiptIndex->setTable('qianniao_receive_receipt_index_'.$enterpriseId);
- //$this->objDReceived->setSearchIndex('received_receipt_search')->setType('received_receipt');
- }
- /**
- * 添加收款单
- *
- * @param array $params 收款单数据
- * @param bool $autoCheck 是否需要自动审核
- * @return ResultWrapper
- */
- public function addReceived($params, $autoCheck= false)
- {
- $offsetDate = $params['receiptOffsetData'];
- unset($params['receiptOffsetData']);
- $accountList = $params['accountList'];
- unset($params['accountList']);
- $beginTransactionStatus = $this->objDReceived->beginTransaction();
- //先款后货查询收款单是否已生成
- $params['originId'] = empty($params['originId']) ? 0 : $params['originId'];
- if($params['originId'] != 0){
- $dbResult = $this->objDReceived->get(['originId'=>$params['originId']]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
- }
- if(!empty($dbResult)){
- // 查询订单是否是先款后货的
- $objMOrder = new DOrder();
- $orderDbResult = $objMOrder->query('select * from qianniao_order_'.$this->enterpriseId.'_1 where id = '.$params['originId']);
- if ($orderDbResult === false) {
- return ResultWrapper::fail($objMOrder->error(), ErrorCode::$dberror);
- }
- if (!empty($orderDbResult) && $orderDbResult[0]['payType'] == StatusCode::$payType['cashBefore']){// 说明该先款后货的单子已经生成过
- return ResultWrapper::fail('该订单已经创建收款单,请先审核收款单',ErrorCode::$notAllowAccess);
- }
- }
- }
- unset($dbResult);
- // 生成编号
- $dbResult = $this->objDReceived->get('createTime >='.strtotime(date('Ymd'.'0:0:0')), 'no', 'createTime desc');
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
- }
- if(empty($dbResult)){
- $params['no'] = createSerialNumberByDate('');
- }else{
- $params['no'] = createSerialNumberByDate($dbResult['no']);
- }
- // 索引表数据
- $indexData = [
- 'no' => $params['no'],
- 'receivedId' => 0,
- 'customerId' => $params['customerId'],
- 'currentAccountName' => $params['currentAccountName'],//制单人
- 'sourceNo' => $params['sourceNo'],
- 'sourceId' => getArrayItem($params,'sourceId',0),
- 'originId' => getArrayItem($params,'originId',0),
- 'originNo' => getArrayItem($params,'originNo',''),
- 'auditStatus' => $params['auditStatus'],
- 'financeTypeId' => $params['financeTypeId'],
- 'financeType' => $params['financeType'],
- 'receivedType' => empty($offsetDate) ? StatusCode::$standard : StatusCode::$delete,
- 'createTime' => $params['createTime'],
- 'updateTime' => $params['updateTime'],
- 'receiptTime' => $params['receiptTime'],
- ];
- $ReceivedId = $this->objDReceivedIndex->insert($indexData);
- if ($ReceivedId === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($this->objDReceivedIndex->error(), ErrorCode::$dberror);
- }
- // 收款单表数据
- $params['id'] = $ReceivedId; // 收款单id = 索引表自动id
- $params['totalMoney'] = 0;
- $params['totalDiscountMoney'] = 0;
- $params['totalFinalMoney'] = 0;
- foreach ($accountList as $account) {
- $account['discountMoney'] = !empty($account['discountMoney']) ? $account['discountMoney'] : 0;
- $params['totalMoney'] = bcadd($params['totalMoney'], $account['money'], 4);//总金额
- $params['totalDiscountMoney'] = bcadd($params['totalDiscountMoney'], $account['discountMoney'], 4); //总优惠金额
- $params['totalFinalMoney'] = bcadd($params['totalFinalMoney'], bcsub($account['money'] , $account['discountMoney'], 4), 4);//总实付金额
- $params['offsetStatus'] = StatusCode::$delete;
- }
- $params['offsetMoney'] = 0;
- $params['notOffsetMoney'] = bcadd($params['totalFinalMoney'],$params['totalDiscountMoney'],2);
- $result = $this->objDReceived->insert($params);
- if ($result === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
- }
- //收款账户数据
- foreach ($accountList as $account) {
- $account['operatorId'] = $this->userCenterId;
- $account['receivedId'] = $ReceivedId;
- $account['finalMoney'] = bcsub($account['money'],$account['discountMoney'],2);
- $account = self::buildAccountData($account);
- $result = $this->objDReceivedAccount->insert($account);
- if ($result === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($this->objDReceivedAccount->error(), ErrorCode::$dberror);
- }
- unset($result);
- }
- //需要自动审核的单据加到队列表里,通过cron审核
- if($autoCheck) {
- $data = [
- 'enterpriseId'=>$this->enterpriseId,
- 'receivedNo'=>$params['no'],
- 'createTime'=>$params['createTime']
- ];
- $result = $this->objDReceivedReceiptCheckQueue->insert($data);
- if ($result === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($this->objDReceivedReceiptCheckQueue->error(), ErrorCode::$dberror);
- }
- }
- //记录核销明细
- if (empty($offsetDate)){
- if( $beginTransactionStatus ){
- $this->objDReceived->commit();
- }
- return ResultWrapper::success($ReceivedId);
- }
- if(!empty($offsetDate)){
- foreach ($offsetDate as $key => $value){
- $offsetDate[$key]['receivedId'] = $ReceivedId;
- }
- }
- unset($result);
- $result = $this->objDReceiveOffset->insert($offsetDate,true);
- if ($result === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($this->objDReceiveOffset->error(), ErrorCode::$dberror);
- }
- if( $beginTransactionStatus ){
- $this->objDReceived->commit();
- }
- /*
- $_id = self::createEsDocumentId($ReceivedId, $params['createTime']);
- $esData = $params;
- $esData['id'] = $ReceivedId;
- $esData['enterpriseId'] = $this->enterpriseId;
- $result = $this->objDReceived->addUpSearchIndexDocument($esData, $_id);
- if (isset($result['_shards']) && isset($result['_shards']['successful']) && $result['_shards']['successful'] == 1) {
- //echo "es操作成功";die;
- }else {
- 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);
- }
- */
- return ResultWrapper::success($ReceivedId);
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2020/11/5
- * Time: 7:08 下午
- * @param array $params
- * @return array
- */
- public static function buildAccountData(array $params)
- {
- return [
- 'receivedId' => isset($params['receivedId']) ? $params['receivedId'] : 0,
- 'accountId' => isset($params['accountId']) ? $params['accountId'] : 0,
- 'accountNumber' => isset($params['accountNumber']) ? $params['accountNumber'] : 0,
- 'accountName' => getArrayItem($params, 'accountName', ''),
- 'money' => isset($params['money']) ? $params['money'] : 0,
- 'discountMoney' => isset($params['discountMoney']) ? $params['discountMoney'] : 0,
- 'finalMoney' => isset($params['finalMoney']) ? $params['finalMoney'] : 0,
- 'payWay' => isset($params['payWay']) ? $params['payWay'] : 0,
- 'remark' => isset($params['remark']) ? $params['remark'] : '',
- 'createTime' => time(),
- 'updateTime' => time(),
- 'operatorId' => isset($params['operatorId']) ? $params['operatorId'] : 0,
- ];
- }
- /**
- * 编辑收款单
- * @param $params
- * @return ResultWrapper
- * @throws \Exception
- */
- public function editReceived($params)
- {
- $this->objDReceived->setTable('qianniao_received_' . $this->enterpriseId . '_' . date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3));
- $this->objDReceivedAccount->setTable('qianniao_received_account_' . $this->enterpriseId . '_' . date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3));
- $beginTransactionstatus = $this->objDReceived->beginTransaction();
- $accountList = $params['accountList'];
- unset($params['accountList']);
- $offsetDate = $params['receiptOffsetData'];
- unset($params['receiptOffsetData']);
- $params['totalMoney'] = 0;
- $params['totalDiscountMoney'] = 0;
- $params['totalFinalMoney'] = 0;
- foreach ($accountList as $account) {
- $account['discountMoney'] = !empty($account['discountMoney']) ? $account['discountMoney'] : 0;
- $params['totalMoney'] += $account['money'];
- $params['totalDiscountMoney'] += $account['discountMoney'];
- $params['totalFinalMoney'] += ($account['money'] - $account['discountMoney']);
- }
- $receivedId = $params['id'];
- unset($params['id']);
- if( isset($params['no']) ){
- unset($params['no']);
- }
- $dbResult = $this->objDReceived->update($params, $receivedId);
- if ($dbResult === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
- }
- //索引表数据
- $indexData = [
- 'customerId' => $params['customerId'],
- 'currentAccountName' => $params['currentAccountName'],
- 'sourceNo' => $params['sourceNo'],
- 'auditStatus' => $params['auditStatus'],
- 'financeTypeId' => $params['financeTypeId'],
- 'financeType' => $params['financeType'],
- 'receivedType' => empty($offsetDate) ? StatusCode::$standard : StatusCode::$delete,
- 'createTime' => $params['createTime'],
- 'updateTime' => $params['updateTime'],
- 'receiptTime' => $params['receiptTime'],
- ];
- $result = $this->objDReceivedIndex->update($indexData,['id'=>$receivedId]);
- if ($result === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($this->objDReceivedIndex->error(), ErrorCode::$dberror);
- }
- unset($result);
- //收款账户数据
- foreach ($accountList as $account) {
- if (isset($account['id'])) {
- $account['receivedId'] = $receivedId;
- $account['operatorId'] = $this->userCenterId;
- $account['updateTime'] = $params['updateTime'];
- $result = $this->objDReceivedAccount->update($account, $account['id']);
- }else{
- $account['receivedId'] = $receivedId;
- $account['operatorId'] = $this->userCenterId;
- $account['createTime'] = $params['createTime'];
- $result = $this->objDReceivedAccount->insert($account);
- }
- if ($result === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($this->objDReceivedAccount->error(), ErrorCode::$dberror);
- }
- unset($result);
- }
- //更新核销明细
- if (empty($offsetDate)){
- if($beginTransactionstatus){
- $this->objDReceived->commit();
- }
- return ResultWrapper::success($dbResult);
- }
- if(!empty($offsetDate)){
- foreach ($offsetDate as $key => $value){
- $offsetDate[$key]['receivedId'] = $receivedId;
- unset($result);
- $result = $this->objDReceiveOffset->update($value, ['receivedId'=>$receivedId]);
- }
- }
- if ($result === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($this->objDReceiveOffset->error(), ErrorCode::$dberror);
- }
- if($beginTransactionstatus){
- $this->objDReceived->commit();
- }
- /*
- $_id = self::createEsDocumentId($receivedId, $params['createTime']);
- $esData = $params;
- $esData['id'] = $receivedId;
- $esData['enterpriseId'] = $this->enterpriseId;
- $this->objDReceived->esupdateTypeFieldVaule($esData, $_id);
- */
- return ResultWrapper::success($dbResult);
- }
- private function createEsDocumentId($receivedId, $time)
- {
- $t = date('Y', $time) . '_' . ceil(date('m', $time) / 3);
- return 'EnterpriseId_' . $this->enterpriseId . '_' . $t . '_receivedId_' . $receivedId;
- }
- /**
- * 获取指定收款单信息
- * @param $receivedParams
- * @return ResultWrapper
- * @throws \Exception
- */
- public function getReceivedInfo($receivedParams)
- {
- unset($receivedParams['createTime']);
- $receivedIndexData = $this->objDReceivedIndex->get($receivedParams['id']);
- if ($receivedIndexData === false) {
- return ResultWrapper::fail($this->objDReceivedIndex->error(), ErrorCode::$dberror);
- }
- if(empty($receivedIndexData)){
- return ResultWrapper::fail('要查看的收款单索引数据不存在', ErrorCode::$contentNotExists);
- }
- $this->objDReceived->setTable('qianniao_received_' . $this->enterpriseId . '_' . date('Y', $receivedIndexData['createTime']) . '_' . ceil(date('m', $receivedIndexData['createTime']) / 3));
- $this->objDReceivedAccount->setTable('qianniao_received_account_' . $this->enterpriseId . '_' . date('Y', $receivedIndexData['createTime']) . '_' . ceil(date('m', $receivedIndexData['createTime']) / 3));
- $dbResult = $this->objDReceived->get($receivedParams);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
- }
- if(empty($dbResult)){
- return ResultWrapper::fail('要查看的收款单不存在', ErrorCode::$contentNotExists);
- }
- //渲染前缀
- $dbResult['no'] = StatusCode::$noPrefix[17] . '-' . $dbResult['no'];
- //渲染凭证
- if( isset($dbResult['extends'])&& !empty($dbResult['extends']) ){
- $dbResult['extends'] = json_decode($dbResult['extends'],true);
- }
- $accountData = $this->objDReceivedAccount->select(['receivedId' => $receivedParams['id'],'deleteStatus'=>StatusCode::$standard]);
- if ($accountData === false) {
- return ResultWrapper::fail($this->objDReceivedAccount->error(), ErrorCode::$dberror);
- }
- $dbResult['accountList'] = $accountData;
- //查出客户当前余额
- $dbResult['currentBalanceMoney'] = !empty($dbResult['customerId']) ? $this->objMCustomerBalance->getCustomerBalance($dbResult['customerId']) : 0;
- // 处理应收和收款单核销关系
- $dbResult['offsetDate']= [];
- $offsetdbResult = $this->objDReceiveOffset->select(['receivedId' => $receivedParams['id']]);
- if ( $offsetdbResult === false ) {
- return ResultWrapper::fail($this->objDReceiveOffset->error(), ErrorCode::$dberror);
- }
- if ( !empty($offsetdbResult) ){
- // 映射应收单对应的核销金额
- $receiveReceiptIdBindOffsetMoney = [];
- foreach ($offsetdbResult as $key => $value){
- $receiveReceiptIdBindOffsetMoney[$value['receiveReceiptId']] = $value['offsetMoney'];
- }
- // 批量查询应收数据
- $receiveReceiptIds = array_column($offsetdbResult,'receiveReceiptId');
- $objMReceive = new MReceive($this->enterpriseId, $this->userCenterId);
- $selectParams = [
- 'limit' => 200,
- 'offset' => 0,
- 'receiveReceiptIds' => $receiveReceiptIds,
- 'offsetStatus' =>$offsetdbResult['offsetStatus']
- ];
- $receiveData = $objMReceive->getAllReceive($selectParams);
- if(!$receiveData->isSuccess()){
- return ResultWrapper::fail($receiveData->getData(), $receiveData->getErrorCode());
- }
- $receiveData = $receiveData->getData();
- if(!empty($receiveData['total'])){
- foreach ($receiveData['data'] as $key => $value){
- $receiveData['data'][$key]['offsetMoney'] = getArrayItem($receiveReceiptIdBindOffsetMoney, $value['id'], 0);
- }
- $dbResult['offsetDate'] = $receiveData['data'];
- }
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 删除收款单
- */
- public function updateReceivedDeleteStatus($where, $createTime)
- {
- $this->objDReceived->setTable('qianniao_received_' . $this->enterpriseId . '_' . date('Y', $createTime) . '_' . ceil(date('m', $createTime) / 3));
- $this->objDReceivedAccount->setTable('qianniao_received_account_' . $this->enterpriseId . '_' . date('Y', $createTime) . '_' . ceil(date('m', $createTime) / 3));
- $receivedId = $this->objDReceivedIndex->get($where, 'receivedId');
- if ($receivedId === false) {
- return ResultWrapper::fail($this->objDReceivedIndex->error(), ErrorCode::$dberror);
- }
- if(empty($receivedId)){
- return ResultWrapper::fail('要删除的收款单不存在', ErrorCode::$contentNotExists);
- }
- // 删除索引
- $dbResult = $this->objDReceivedIndex->update(['deleteStatus'=>StatusCode::$delete,'updateTime'=>time()], ['id'=>$receivedId]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDReceivedIndex->error(), ErrorCode::$dberror);
- }
- // 删除收款单单据
- $dbResult = $this->objDReceived->update(['deleteStatus'=>StatusCode::$delete,'updateTime'=>time()], ['id'=>$receivedId]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
- }
- // 删除收款单账号
- $dbResult = $this->objDReceivedAccount->update(['deleteStatus'=>StatusCode::$delete,'updateTime'=>time(), ['receivedId'=>$receivedId]]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDReceivedAccount->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success('删除成功');
- }
- /**
- * 收款单审核 (修改 判断账户是否存在)
- * @param array $params
- * @return ResultWrapper
- * @throws \Exception
- */
- public function updateReceivedStatus($params)
- {
- // 切换收款单和收款单账户表
- $this->objDReceived->setTable('qianniao_received_' . $this->enterpriseId . '_' . date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3));
- $this->objDReceivedAccount->setTable('qianniao_received_account_' . $this->enterpriseId . '_' . date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3));
- if(empty($params['id']) && empty($params['no'])){
- return ResultWrapper::fail('ID和编号不能同时为空', ErrorCode::$paramError);
- }
- $getWhere=array();
- if(empty($params['id'])){
- $getWhere["no"] = $params['no'];
- }else{
- $getWhere["id"] = $params['id'];
- }
- // 查询收款单数据
- $receivedData = $this->objDReceived->get($getWhere);
- if ($receivedData === false) {
- return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
- }
- if(empty($receivedData)){
- return ResultWrapper::fail('要操作得收款单不存在', ErrorCode::$contentNotExists);
- }
- if($receivedData['auditStatus'] == StatusCode:: $auditStatus['auditPass']){
- return ResultWrapper::fail('当前收款单已经审核过了', ErrorCode::$notAllowAccess);
- }
- $params['id'] = $receivedData['id'];
- $beginTransactionStatus = $this->objDReceived->beginTransaction();
- // 更新收款单审核状态
- $dbResult = $this->objDReceived->update(['auditStatus' => StatusCode:: $auditStatus['auditPass']], ['id' => $params['id']]);
- if ($dbResult === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
- }
- unset($dbResult);
- // 更新收款单索引表审核状态
- $dbResult = $this->objDReceivedIndex->update(['auditStatus' => StatusCode:: $auditStatus['auditPass']], ['id' => $params['id']]);
- if ($dbResult === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($this->objDReceivedIndex->error(), ErrorCode::$dberror);
- }
- unset($dbResult);
- // 获取客户往来余额
- $customerBalance = $this->objMCustomerBalance->getCustomerBalance($receivedData['customerId']);
- // 查出源单据已收过的金额
- $remark = '';
- if (!empty($receivedData['sourceNo'])) {
- $beforeReceivedMoney = 0;
- $where = 'sourceNo = ' . $receivedData['sourceNo'] . ' AND auditStatus = ' . StatusCode::$auditStatus['auditPass'] . ' AND receivedId !=' . $params['id'];
- $sourceNoReceivedResult = $this->objDReceivedIndex->get($where);
- if (!empty($sourceNoReceivedResult)) {
- $this->objDReceived->setTable('qianniao_received_' . $this->enterpriseId . '_' . date('Y', $sourceNoReceivedResult['createTime']) . '_' . ceil(date('m', $sourceNoReceivedResult['createTime']) / 3));
- $beforeReceivedMoney = $this->objDReceived->get_field('totalMoney', $sourceNoReceivedResult['receivedId']);
- }
- $remark = ',源单据剩余应收'. bcsub(bcsub($receivedData['sourceNoMoney'] , $beforeReceivedMoney, 4), $receivedData['totalMoney'], 4) . '元';
- }
- $detailData = [
- 'customerId' => $receivedData['customerId'],//'客户id',
- 'receiptTime' => $receivedData['receiptTime'],//'单据日期',
- 'sourceId' => $receivedData['id'],//'单据编号',
- 'sourceNo' => $receivedData['no'],//'单据编号',
- 'financeType' => $receivedData['financeType'],//'财务类型名称',
- 'financeTypeId' => $receivedData['financeTypeId'],//'财务类型id',
- 'originNo' => $receivedData['sourceNo'],//'源订单销货号',
- 'originId' => $receivedData['sourceId'],//'源订单销货号',
- 'salesAmount' => $receivedData['totalMoney'],//总金额
- 'discountMoney' => $receivedData['totalDiscountMoney'],// '优惠金额',
- 'customerAmount' => $receivedData['totalFinalMoney'],// '客户承担金额',
- 'receivableAmount' => $receivedData['totalFinalMoney'],// '应收金额',
- 'actualReceivableAmount' => $receivedData['totalFinalMoney'],//'实际收款金额',
- 'receivableBalance' => bcsub($customerBalance , $receivedData['totalFinalMoney'], 4),//'应收后余额',
- 'remark' => '收款单审核通过,本次优惠后实收' . $receivedData['totalFinalMoney'] . '元,优惠'.$receivedData['totalDiscountMoney'].'元'.$remark,//'备注',
- 'createTime' => time(),//'创建日期',
- 'updateTime' => time(),//'修改日期',
- ];
- // 添加客户往来余额明细,修改客户往来余额
- $result = $this->objMCustomerBalanceDetail->addCustomerBalanceDetail($detailData);
- if (!$result->isSuccess()) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- // 编辑客户往来余额
- $changedMoney = $receivedData['totalMoney'] > 0 ? -1 * $receivedData['totalFinalMoney'] : abs($receivedData['totalFinalMoney']);
- $result = $this->objMCustomerBalance->addCustomerBalance($receivedData['customerId'], $changedMoney);
- if (!$result->isSuccess()) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- // 查询核销记录表
- $offsetDbResult = $this->objDReceiveOffset->select(['receivedId'=>$receivedData['id'],'type'=>StatusCode::$standard]);
- if ($offsetDbResult === false) {
- return ResultWrapper::fail($this->objDReceiveOffset->error(), ErrorCode::$dberror);
- }
- // 核销收款单指定的应收单
- if (!empty($offsetDbResult)){
- // 手动核销指定的应收单数据
- foreach ($offsetDbResult as $offsetKey =>$offsetValue){
- // 查询应收单数据
- $sql = 'select * from ' . $this->objDReceiveReceiptIndex->get_Table() . ' where id='.$offsetValue['receiveReceiptId'];
- $firstOffset = $this->objDReceiveReceiptIndex->query($sql);
- if ($firstOffset === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($this->objDReceiveReceiptIndex->error(), ErrorCode::$dberror);
- }
- if(empty($firstOffset)){
- $this->objDReceived->rollBack();
- return ResultWrapper::fail('要操作得应收单不存在', ErrorCode::$contentNotExists);
- }
- // 当前要处理的应收单已经核销则不做处理
- if( $firstOffset[0]['offsetStatus'] == StatusCode::$standard ){
- continue;
- }
- $autoCustomerMoney = self::autoCustomerMoney($receivedData['customerId'],bcadd($receivedData['totalFinalMoney'],$receivedData['totalDiscountMoney'],2),$receivedData['id'],$receivedData['createTime'],$firstOffset,$offsetValue['receiveReceiptId']);
- if(!$autoCustomerMoney->isSuccess()){
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($autoCustomerMoney->getData(),$autoCustomerMoney->getErrorCode());
- }
- }
- }else{ // 从最老单子到最新单子的顺序依次核销
- // 核销应收单
- if (empty($receivedData['originId'])){
- $autoCustomerMoney = self::autoCustomerMoney($receivedData['customerId'],bcadd($receivedData['totalFinalMoney'],$receivedData['totalDiscountMoney'],2),$receivedData['id'],$receivedData['createTime'],$firstOffsetDate=[],0);
- if(!$autoCustomerMoney->isSuccess()){
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($autoCustomerMoney->getData(),$autoCustomerMoney->getErrorCode());
- }
- }else{ //核销指定订单
- //查询应收表
- $dbResult = $this->objDReceiveReceiptIndex->get(['originId'=>$receivedData['originId']]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDReceiveReceiptIndex->error(), ErrorCode::$dberror);
- }
- if(!empty($dbResult)){
- //根据时间切表
- $suffix = date('Y', $dbResult['createTime']) . '_' . ceil(date('m', $dbResult['createTime']) / 3);
- $this->objDReceive->setTable('qianniao_receive_receipt_' . $this->enterpriseId . '_' . $suffix);
- $receiveResult = $this->objDReceive->get(['id'=>$dbResult['id']]);
- if ($receiveResult === false) {
- return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
- }
- //核销应收
- switch (true){
- case bcsub($receivedData['notOffsetMoney'],$receiveResult['notOffsetMoney'],2) == 0;
- $receiveOffsetDate = [
- 'offsetMoney'=> bcadd($receiveResult['offsetMoney'],$receiveResult['notOffsetMoney'],2),
- 'notOffsetMoney'=> 0,
- 'offsetStatus'=> StatusCode::$standard,
- 'updateTime' => time()
- ];
- $receiveIndexOffsetDate = [
- 'offsetStatus' => StatusCode::$standard,
- 'updateTime' => time()
- ];
- $receivedOffsetDate = [
- 'offsetMoney' => !empty($receivedData['offsetMoney']) ? $receivedData['totalFinalMoney'] : $receiveResult['notOffsetMoney'],
- 'notOffsetMoney' =>0,
- 'offsetStatus' =>StatusCode::$standard,
- 'updateTime' => time()
- ];
- break;
- case bcsub($receivedData['notOffsetMoney'],$receiveResult['notOffsetMoney'],2) > 0;
- $receiveOffsetDate = [
- 'offsetMoney'=> bcadd($receiveResult['offsetMoney'],$receiveResult['notOffsetMoney'],2),
- 'notOffsetMoney'=> 0,
- 'offsetStatus'=> StatusCode::$standard,
- 'updateTime' => time()
- ];
- $receiveIndexOffsetDate = [
- 'offsetStatus' => StatusCode::$standard,
- 'updateTime' => time()
- ];
- $receivedOffsetDate = [
- 'offsetMoney' =>$receiveResult['notOffsetMoney'],
- 'notOffsetMoney' =>bcsub($receivedData['notOffsetMoney'],$receiveResult['notOffsetMoney'],2),
- 'offsetStatus' =>StatusCode::$partion,
- 'updateTime' => time()
- ];
- break;
- case bcsub($receivedData['notOffsetMoney'],$receiveResult['notOffsetMoney'],2) < 0;
- $receiveOffsetDate = [
- 'offsetMoney'=> bcadd($receiveResult['offsetMoney'],$receivedData['notOffsetMoney'],2),
- 'notOffsetMoney'=> bcsub($receiveResult['notOffsetMoney'],$receivedData['notOffsetMoney'],2),
- 'offsetStatus'=> StatusCode::$partion,
- 'updateTime' => time()
- ];
- $receiveIndexOffsetDate = [
- 'offsetStatus' => StatusCode::$partion,
- 'updateTime' => time()
- ];
- $receivedOffsetDate = [
- 'offsetMoney' => empty($receivedData['offsetMoney']) ? $receivedData['totalFinalMoney'] :$receivedData['notOffsetMoney'],
- 'notOffsetMoney' =>0,
- 'offsetStatus' =>StatusCode::$standard,
- 'updateTime' => time()
- ];
- }
- $updateOffsetStatus = $this->objDReceive->update($receiveOffsetDate,['id'=>$receiveResult['id']]);
- if ($updateOffsetStatus === false) {
- $this->objDReceive->rollBack();
- return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
- }
- $updateIndexOffsetStatus = $this->objDReceiveReceiptIndex->update($receiveIndexOffsetDate, ['id' => $receiveResult['id']]);
- if ($updateIndexOffsetStatus === false) {
- $this->objDReceive->rollBack();
- return ResultWrapper::fail($this->objDReceiveReceiptIndex->error(), ErrorCode::$dberror);
- }
- //在线支付的收款单直接核销
- $updateReceivedOffset = $this->objDReceived->update($receivedOffsetDate,['id'=>$receivedData['id']]);
- if ($updateReceivedOffset === false) {
- $this->objDReceive->rollBack();
- return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
- }
- // 记录核销明细
- //核销完成,记录核销明细
- $updateOffset = [
- 'receiveReceiptId' => $receiveResult['id'],
- 'receivedId' => $receivedData['id'],
- 'offsetMoney' => $receiveOffsetDate['offsetMoney'],
- 'receiveCreateTime' => $receiveResult['createTime'],
- 'type' => StatusCode::$delete
- ];
- $result = $this->objDReceiveOffset->insert($updateOffset);
- if ($result === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($this->objDReceiveOffset->error(), ErrorCode::$dberror);
- }
- }
- }
- }
-
- //更新客户总付款金额
- $result = $this->objMCustomerBalance->updateCustomerTotalPayMoney($receivedData['customerId'], $receivedData['totalFinalMoney']);
- if (!$result->isSuccess()) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- //给账户加钱
- $accounts = $this->objDReceivedAccount->select(['receivedId' => $receivedData['id']]);
- foreach ($accounts as $account) {
- //获取账户信息
- $accountResult = $this->objMAccount->getAccountInfo($account['accountId']);
- if (!$accountResult->isSuccess()) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($accountResult->getData(), $accountResult->getErrorCode());
- }
- $accountInfo = $accountResult->getData();
- //账户明细
- $accountDetail = [
- 'accountId' => $accountInfo['id'],
- 'accountCode' => $accountInfo['accountCode'],
- 'accountName' => $accountInfo['name'],
- 'accountNumber' => $accountInfo['accountNumber'],
- 'sourceId' => $receivedData['id'],
- 'sourceNo' => $receivedData['no'],
- 'financeType' => $receivedData['financeType'],
- 'beginBalance' => $accountInfo['money'],
- 'shopId' => $receivedData['shopId'],
- 'shopName' => $receivedData['shopName'],
- 'income' => $receivedData['totalFinalMoney']>0 ? $receivedData['totalFinalMoney'] : 0,
- 'expend' => $receivedData['totalFinalMoney']<0 ? -1 * $receivedData['totalFinalMoney'] : 0,
- 'endBalance' => $accountInfo['money'] + $receivedData['totalFinalMoney'],
- 'contactUnit' => '客户名称:' . $receivedData['customerName'],
- 'supplierId' => 0,
- 'customerId' => $receivedData['customerId'],
- 'operatorId' => $this->userCenterId,
- 'receiveOrPayPerson' => $receivedData['currentAccountName'],
- 'remark' => '收款单审核通过,本次收客户' . $receivedData['totalMoney'] . '元,优惠' . $receivedData['totalDiscountMoney'] . '元,最终收款' . $receivedData['totalFinalMoney'] . '元',
- 'createTime' => time(),
- 'updateTime' => time(),
- ];
- $result = $this->objMAccountDetail->addAccountDetail($accountDetail);
- if (!$result->isSuccess()) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- $result = $this->objMAccount->updateMoney($account['accountId'], $receivedData['totalFinalMoney']);
- if (!$result->isSuccess()) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- }
- //审核通过记录核销金额,未核销金额,核销状态。
- /* $objMReceive = new MReceive($this->enterpriseId, $this->userCenterId);
- if( !empty($offsetDbResult) ){
- foreach ($offsetDbResult as $key =>$value) {
- // //如果$customerBalance客户余额是负的,直接自动核销订单。
- // if ($customerBalance<0){
- // $autoOffset = $objMReceive->autoOffset($customerBalance,$offsetDbResult[$key]['receiveReceiptId'],$offsetDbResult[$key]['receiveCreateTime']);
- // if ($autoOffset->isSuccess() === false) {
- // $this->objDReceived->rollBack();
- // return ResultWrapper::fail($autoOffset->getData(), $autoOffset->getErrorCode());
- // }
- // }else{
- //修改应付单核销金额,索引表核销状态。
- $updateOffsetDbResult = $objMReceive->updateOffsetStatus($offsetDbResult[$key]['receiveReceiptId'], $offsetDbResult[$key]['offsetMoney'], $offsetDbResult[$key]['receiveCreateTime']);
- if ($updateOffsetDbResult->isSuccess() === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($updateOffsetDbResult->getData(), $updateOffsetDbResult->getErrorCode());
- }
- // }
- }
- }*/
- if ($beginTransactionStatus){
- $this->objDReceived->commit();
- }
- $objMOrder = new MOrder($this->userCenterId, $this->enterpriseId);
- //根据orderId修改订单状态
- if(!empty($receivedData['originId'])){
- //查询所有的收款
- $allReceivedMoney = 0;
- $allReceiveMoney = 0;
- unset($dbResult);
- $dbResult = $this->objDReceived->select(['originId' => $receivedData['originId']]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
- }
- foreach ($dbResult as $receivedKey => $receivedValue){
- $allReceivedMoney = bcadd($allReceivedMoney,$receivedValue['totalFinalMoney'],2);
- }
- unset($dbResult);
- //查询订单对应的应收
- $dbResult = $this->objDReceiveReceiptIndex->select(['originId'=>$receivedData['originId']]);
- foreach ($dbResult as $receiptIndexKey => $receiptIndexValue){
- $suffix = date('Y', $receiptIndexValue['createTime']) . '_' . ceil(date('m', $receiptIndexValue['createTime']) / 3);
- $this->objDReceive->setTable('qianniao_receive_receipt_' . $this->enterpriseId . '_' . $suffix);
- $objReceiveResult = $this->objDReceive->get(['id'=>$receiptIndexValue['id']]);
- if ($objReceiveResult === false) {
- return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
- }
- if(!empty($objReceiveResult)){
- $allReceiveMoney = $objReceiveResult['offsetMoney'];
- }
- }
- //判断订单状态
- $orderTable = $objMOrder->getOrderTable($this->enterpriseId,$this->userCenterId);
- $objDOrder = new DOrder();
- $statusOrder = $objDOrder->query('select * from '.$orderTable.' where id = '.$receivedData['originId']);
- //收银端收款有时候会没有订单
- $xpayAmount = empty($statusOrder)?0:$statusOrder[0]['payAmount'];//订单实际需要支付金额
-
- if( bcsub(bcsub($allReceivedMoney,$allReceiveMoney,2),$xpayAmount,2) >= 0 ){//已支付
- $orderPayStatus = [
- 'payStatus' => StatusCode::$standard,
- 'payTime' => time()
- ];
- }else{
- $orderPayStatus = [
- 'payStatus' => StatusCode::$partion,
- 'payTime' => time()
- ];
- }
- $orderDbResult = $objMOrder->updateOrderPayData($orderPayStatus, ['id' => $receivedData['originId']]);
- if ($orderDbResult->isSuccess() === false) {
- return ResultWrapper::fail($orderDbResult->getData(), $orderDbResult->getErrorCode());
- }
- }else{
- //查询当前核销表
- unset($offsetDbResult);
- $offsetDbResult = $this->objDReceiveOffset->select(['receivedId'=>$receivedData['id'],'type'=>StatusCode::$standard]);
- if ($offsetDbResult === false) {
- return ResultWrapper::fail($this->objDReceiveOffset->error(), ErrorCode::$dberror);
- }
- //财务完成,修改订单表支付状态
- foreach ($offsetDbResult as $key =>$value) {
- //根据应付单获取orderId,用来修改订单支付状态
- $suffix = date('Y', $offsetDbResult[$key]['receiveCreateTime']) . '_' . ceil(date('m', $offsetDbResult[$key]['receiveCreateTime']) / 3);
- $this->objDReceive->setTable('qianniao_receive_receipt_' . $this->enterpriseId . '_' . $suffix);
- $orderIdDbResult = $this->objDReceive->get(['id' => $offsetDbResult[$key]['receiveReceiptId']]);
- if ($orderIdDbResult === false) {
- return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
- }
- if (empty($orderIdDbResult)) {
- return ResultWrapper::fail('应付单不存在', ErrorCode::$notAllowAccess);
- }
- //判断核销金额,未核销金额,来计算payStatus的状态
- $updateOrderPayData1 = ['payStatus' => StatusCode::$standard, 'payTime' => time()];
- $updateOrderPayData2 = ['id' => $orderIdDbResult['originId']];
- switch (true) {
- case $orderIdDbResult['offsetMoney'] == $orderIdDbResult['receiveMoney'];
- $updateOrderPayData1['payStatus'] = StatusCode::$standard;
- break;
- case $orderIdDbResult['offsetMoney'] == 0;
- $updateOrderPayData1['payStatus'] = StatusCode::$delete;
- break;
- case $orderIdDbResult['offsetMoney'] > 0 && $orderIdDbResult['offsetMoney'] < $orderIdDbResult['receiveMoney'];
- $updateOrderPayData1['payStatus'] = StatusCode::$partion;
- $updateOrderPayData1['notPayMoney'] = $orderIdDbResult['notOffsetMoney'];
- }
- $orderDbResult = $objMOrder->updateOrderPayData($updateOrderPayData1, $updateOrderPayData2);
- if ($orderDbResult->isSuccess() === false) {
- return ResultWrapper::fail($orderDbResult->getData(), $orderDbResult->getErrorCode());
- }
- }
- }
- //今日收款金额redis
- $this->objOverviewCache->saveBusinessOverview($this->enterpriseId, 'todayReceivedMoney', $receivedData['totalFinalMoney']);
- $this->objOverviewCache->saveBusinessOverview($this->enterpriseId, 'todayReceivedMoney', $receivedData['totalFinalMoney'], $receivedData['shopId']);
- /*
- $_id = self::createEsDocumentId($receivedData['id'], $receivedData['createTime']);
- $this->objDReceived->esupdateTypeFieldVaule(['auditStatus' => StatusCode::$auditStatus['auditPass']], $_id);
- */
- // return ResultWrapper::success($dbResult);
- return ResultWrapper::success(null);
- }
- /**
- * 自动核销客户余额
- * 客户id,收款单总的money进行自动核销
- */
- public function autoCustomerMoney($customerId,$money,$id,$createTime,$firstOffset,$receiveReceiptId)
- {
- $ReceivedDbResult = $this->objDReceived->get($id);
- if ($ReceivedDbResult === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
- }
- // 随便核销
- if ($receiveReceiptId == 0){
- // 查询这个客户所有未核销的应收单
- $sql = 'select * from ' . $this->objDReceiveReceiptIndex->get_Table() . ' where customerId='.$customerId.' and offsetStatus != '. StatusCode::$standard;
- $dbResultOffset = $this->objDReceiveReceiptIndex->query($sql);
- }else{ // 核销指定的应收单
- $sql = 'select * from ' . $this->objDReceiveReceiptIndex->get_Table() . ' where customerId='.$customerId.' and offsetStatus != '. StatusCode::$standard.' and id !='. $receiveReceiptId;
- $dbResultOffset = $this->objDReceiveReceiptIndex->query($sql);
- }
- $dbResultIndex = array_merge($firstOffset,$dbResultOffset);
- if ($dbResultIndex === false) {
- return ResultWrapper::fail($this->objDReceiveReceiptIndex->error(), ErrorCode::$dberror);
- }
- if(empty($dbResultIndex)){//如果为空,说明当前单子只做付款,预付,不做核销.
- return ResultWrapper::success([]);
- }
- $surplusMoney = $money; // 剩余可核销金额
- $receivedOffsetMoney = 0; // 收款单累计核销金额
- $receivedNotOffsetMoney = $money; // 收款单未核销金额
-
- $updateDReceive = null;//报错修改
- foreach ($dbResultIndex as $key => $value){
- // 收款单钱核销完了,则不再进行核销
- if($surplusMoney <= 0){
- break;
- }
- // 切应收分表
- $suffix = date('Y', $value['createTime']) . '_' . ceil(date('m', $value['createTime']) / 3);
- $this->objDReceive->setTable('qianniao_receive_receipt_' . $this->enterpriseId . '_' . $suffix);
- // 查出当前应收单的信息
- $receiveDate = $this->objDReceive->get($value['id']);
- if ($receiveDate === false) {
- return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
- }
- if(empty($receiveDate)){
- continue;
- }
- // 只核销金额大于0的,小于0是退款单
- if ($receiveDate['receiveMoney']<0) {
- continue;
- }
- switch (true) {
- case $receiveDate['notOffsetMoney'] <= $surplusMoney; // 未核销金额小于剩余可核销金额,整单核销
- $offsetMoney = $receiveDate['notOffsetMoney'];
- $notOffsetMoney = 0;
- $offsetStatus = StatusCode::$standard;
- $receivedOffsetMoney = bcadd($receivedOffsetMoney, $receiveDate['notOffsetMoney'], 2);
- $receivedNotOffsetMoney = bcsub($receivedNotOffsetMoney, $receiveDate['notOffsetMoney'], 2);
- $updateReceived = [
- 'offsetMoney' => $receivedOffsetMoney,
- 'notOffsetMoney' => $receivedNotOffsetMoney,
- 'offsetStatus' => ($receivedNotOffsetMoney ==0 ) ? StatusCode::$standard : StatusCode::$partion,
- ];
- $updateReceivedIndex = [
- 'offsetStatus' => ($receivedNotOffsetMoney ==0 ) ? StatusCode::$standard : StatusCode::$partion,
- ];
- break;
- case $receiveDate['notOffsetMoney'] > $surplusMoney; // 未核销金额大于剩余可核销金额,部分核销
- $offsetMoney = $surplusMoney;
- $notOffsetMoney = bcsub($receiveDate['notOffsetMoney'], $offsetMoney, 2);
- $offsetStatus = StatusCode::$partion;
- $receivedOffsetMoney = bcadd($receivedOffsetMoney, $surplusMoney, 2);
- $updateReceived = [
- 'offsetMoney' => $receivedOffsetMoney,
- 'notOffsetMoney' => 0,
- 'offsetStatus' => StatusCode::$standard,
- ];
- $updateReceivedIndex = [
- 'offsetStatus' => StatusCode::$standard,
- ];
- break;
- }
- $surplusMoney = bcsub($surplusMoney, $receiveDate['notOffsetMoney'], 2);
- //判断该单据是否已经审核
- if ($receiveDate['auditStatus'] == StatusCode:: $auditStatus['auditPass']) {
- $update = [
- 'offsetMoney' => bcadd($receiveDate['offsetMoney'],$offsetMoney),
- 'notOffsetMoney' => $notOffsetMoney,
- 'offsetStatus' => $offsetStatus,
- ];
- } else {
- $update = [
- 'offsetMoney' => bcadd($receiveDate['offsetMoney'],$offsetMoney),
- 'notOffsetMoney' => $notOffsetMoney,
- 'offsetStatus' => $offsetStatus,
- 'auditStatus' => StatusCode:: $auditStatus['auditPass']
- ];
- }
- //判断index单据是否已经审核
- if ($value['auditStatus'] == StatusCode:: $auditStatus['auditPass']) {
- $updateIndex = [
- 'offsetStatus' => $offsetStatus,
- ];
- } else {
- $updateIndex = [
- 'offsetStatus' => $offsetStatus,
- 'auditStatus' => StatusCode:: $auditStatus['auditPass']
- ];
- }
- $updateDReceive = $this->objDReceive->update($update, ['id' => $value['id']]);
- if ($updateDReceive === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
- }
- $updateDReceiveReceiptIndex = $this->objDReceiveReceiptIndex->update($updateIndex, ['id' => $value['id']]);
- if ($updateDReceiveReceiptIndex === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
- }
- //应收核销完成,记录核销信息
- $this->objDReceived->setTable('qianniao_received_' . $this->enterpriseId . '_' . date('Y', $createTime) . '_' . ceil(date('m', $createTime) / 3));
- $updateReceivedResult = $this->objDReceived->update($updateReceived, ['id' => $id]);
- if ($updateReceivedResult === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
- }
- //核销完成更新index表的核销状态
- $updateReceivedIndexResult = $this->objDReceivedIndex->update($updateReceivedIndex, ['id' => $ReceivedDbResult['id'], 'receiptTime' => $ReceivedDbResult['receiptTime']]);
- if ($updateReceivedIndexResult === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($this->objDReceivedIndex->error(), ErrorCode::$dberror);
- }
- //如果已经记过,需要剔除
- if (!empty($firstOffset) && count($firstOffset) - 1 >= $key) {//剔除
- continue;
- } else {
- //核销完成,记录核销明细
- $updateOffset = [
- 'receiveReceiptId' => $value['id'],
- 'receivedId' => $id,
- 'offsetMoney' => $offsetMoney,
- 'receiveCreateTime' => $value['createTime']
- ];
- $result = $this->objDReceiveOffset->insert($updateOffset);
- if ($result === false) {
- $this->objDReceived->rollBack();
- return ResultWrapper::fail($this->objDReceiveOffset->error(), ErrorCode::$dberror);
- }
- }
- }
- return ResultWrapper::success($updateDReceive);
- }
- /**
- * 获取所有收款单数据
- * @param array $selectParams 过滤条件
- * @return ResultWrapper
- * @throws \Exception
- */
- public function getAllReceived($selectParams,$export = 0)
- {
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- if($export){
- $limit = 999;
- $offset = 0;
- }
- //组装sql where条件
- $whereSql = ' where deleteStatus = '.StatusCode::$standard;
- if (isset($selectParams['no']) && !empty($selectParams['no'])) {
- //no编号切割
- $explodeNo = explode("-",$selectParams['no']);
- $where = empty($whereSql) ? ' WHERE ' : ' AND ';
- $whereSql .= $where . ' no = "' . $explodeNo[1] . '-' . $explodeNo[2] . '"';
- }
- if (isset($selectParams['financeTypeId']) && !empty($selectParams['financeTypeId'])) {
- $where = empty($whereSql) ? ' WHERE ' : ' AND ';
- $whereSql .= $where . ' financeTypeId = ' . $selectParams['financeTypeId'];
- }
- if (isset($selectParams['auditStatus']) && !empty($selectParams['auditStatus'])) {
- $where = empty($whereSql) ? ' WHERE ' : ' AND ';
- $whereSql .= $where . ' auditStatus = ' . $selectParams['auditStatus'];
- }
- if (isset($selectParams['customerId']) && !empty($selectParams['customerId'])) {
- $where = empty($whereSql) ? ' WHERE ' : ' AND ';
- $whereSql .= $where . ' customerId = ' . $selectParams['customerId'];
- }
- if ( (isset($selectParams['start']) && !empty($selectParams['start']))&&(isset($selectParams['end']) && !empty($selectParams['end'])) ) {
- $where = empty($whereSql) ? ' WHERE ' : ' AND ';
- $whereSql .= $where . ' createTime BETWEEN ' . $selectParams['start'] . ' AND '. $selectParams['end'];
- }
- $sql = 'SELECT * FROM ' .$this->objDReceivedIndex->get_Table(). $whereSql . ' ORDER BY createTime DESC LIMIT ' . $offset . ' , ' . $limit;
- $receivedIndexResult = $this->objDReceivedIndex->query($sql);
- $tableSuffix = [];
- foreach ($receivedIndexResult as $receivedIndex) {
- $k = date('Y', $receivedIndex['createTime']) . '_' . ceil(date('m', $receivedIndex['createTime']) / 3);
- $tableSuffix[$k][] = $receivedIndex['id'];
- }
- $receivedResult = [];
- foreach ($tableSuffix as $suffix => $receivedIds) {
- $this->objDReceived->setTable('qianniao_received_' . $this->enterpriseId . '_' . $suffix);
- $dbResult = $this->objDReceived->select($receivedIds, '*', 'createTime desc');
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDReceived->error(), ErrorCode::$dberror);
- }
- if (!empty($dbResult)){
- //渲染前缀
- foreach ($dbResult as $key => $value){
- $dbResult[$key]['no'] = StatusCode::$noPrefix[17] . '-' . $value['no'];
- if(!empty($value['sourceNo'])){
- $dbResult[$key]['sourceNo'] = StatusCode::$noPrefix[1] . '-' . $value['sourceNo'];
- }
- $dbResult[$key]['extends'] = json_decode($dbResult[$key]['extends'],true);
- }
- }
- $receivedResult = array_merge($receivedResult, $dbResult);
- }
- $totalSql = 'SELECT COUNT(1) as count FROM ' .$this->objDReceivedIndex->get_Table() . $whereSql;
- $dbTotalResult = $this->objDReceivedIndex->query($totalSql);
- if ($dbTotalResult === false) {
- return ResultWrapper::fail($this->objDReceivedIndex->error(), ErrorCode::$dberror);
- }
- if(empty($dbTotalResult)){
- return ResultWrapper::success([]);
- }
- $return = [
- 'data' => self::format($receivedResult),
- 'total' => $dbTotalResult[0]['count']
- ];
- //导出
- if($export){
- self::exportReceived($return['data']);
- exit;
- }
-
- return ResultWrapper::success($return);
- }
- public function format($data)
- {
- $financeTypeIds = [];
- foreach ($data as $key => $received) {
- $financeTypeIds[] = $received['financeTypeId'];
- }
- $financeTypeArr = [];
- if ($financeTypeIds) {
- $financeTypeResult = $this->objMFinanceType->getAllFinanceTypeNoPage(['id'=>array_unique(array_filter($financeTypeIds))]);
- if ($financeTypeResult->isSuccess()) {
- $financeTypeData = $financeTypeResult->getData();
- if (!empty($financeTypeData)) {
- foreach ($financeTypeData as $financeType) {
- $financeTypeArr[$financeType['id']] = $financeType['name'];
- }
- }
- }
- }
- foreach ($data as $key => &$received) {
- $received['financeType'] = isset($financeTypeArr[$received['financeTypeId']]) ? $financeTypeArr[$received['financeTypeId']] : $received['financeType'];
- }
- return $data;
- }
- //搜索的where条件拼接
- public function setWhere($selectParams)
- {
- $defaultDSL = [
- 'from' => $selectParams['offset'],
- 'size' => $selectParams['limit'],
- 'sort' => [
- 'createTime' => [
- 'order' => 'desc'
- ],
- ],
- ];
- $dsl = [];
- $dsl['query']['bool']['must'][] = [
- 'term' => ['enterpriseId' => $this->enterpriseId],
- ];
- if (isset($selectParams['keyword'])) {
- if (!empty($selectParams['keyword'])) {
- $dsl['query']['bool']['must'][] = [
- 'multi_match' => [
- 'fields' => ['no', 'sourceNo', 'currentAccountName'],
- 'query' => $selectParams['keyword'],
- 'fuzziness' => 'AUTO',
- ]
- ];
- }
- }
- if (!empty($selectParams['start']) && !empty($selectParams['end'])) {
- $dsl['query']['bool']['must'][] = [
- 'range' => [
- 'receiptTime' => [
- 'gte' => $selectParams['start'],
- 'lte' => strtotime(date('Y-m-d', $selectParams['end']) . '23:59:59'),
- ]
- ]
- ];
- } else {
- if (!empty($selectParams['start'])) {
- $dsl['query']['bool']['must'][] = [
- 'range' => [
- 'receiptTime' => [
- 'gte' => $selectParams['start'],
- ]
- ]
- ];
- }
- if (!empty($selectParams['end'])) {
- $dsl['query']['bool']['must'][] = [
- 'range' => [
- 'receiptTime' => [
- 'lte' => strtotime(date('Y-m-d', $selectParams['end']) . '23:59:59'),
- ]
- ]
- ];
- }
- }
- if (isset($selectParams['customerId'])) {
- if (!empty($selectParams['customerId'])) {
- $dsl['query']['bool']['filter'][] =
- ['term' => ['customerId' => $selectParams['customerId']]];
- }
- }
- if (isset($selectParams['auditStatus'])) {
- if (!empty($selectParams['auditStatus'])) {
- $dsl['query']['bool']['filter'][] =
- ['term' => ['auditStatus' => $selectParams['auditStatus']]];
- }
- }
- if (isset($selectParams['financeTypeId'])) {
- if (!empty($selectParams['financeTypeId'])) {
- $dsl['query']['bool']['filter'][] =
- ['term' => ['financeTypeId' => $selectParams['financeTypeId']]];
- }
- }
- $dsl = array_merge($defaultDSL, $dsl);
- return $dsl;
- }
- // public function search($selectParams)
- // {
- // $dsl = $this->setWhere($selectParams);
- // $result = $this->objDReceived->getSearchQueryDsl($dsl);
- //
- // if (isset($result['status']) && $result['status'] == 400) {
- // if ($result['error']['reason'] == 'all shards failed') {
- // return ResultWrapper::success([
- // 'data' => [],
- // 'total' => 0
- // ]);
- // }
- // return ResultWrapper::fail('获取应收数据失败' . $result['error']['reason'], ErrorCode::$apiNotResult);
- // }
- //
- // if (!isset($result['hits']) || $result['hits']['total'] == 0) {
- // return ResultWrapper::success([
- // 'data' => [],
- // 'total' => 0
- // ]);
- // }
- // $total = $result['hits']['total'];
- // $dbResult = $result['hits']['hits'];
- //
- // $list = [];
- // foreach ($dbResult as $key => &$value) {
- // $data = [];
- // $data = $value['_source'];
- // $list[] = $data;
- // }
- //
- // $return = [
- // 'data' => $list,
- // 'total' => ($total) ? intval($total) : 0,
- // ];
- // return ResultWrapper::success($return);
- // }
- /**
- * 根据订单no查询应收单
- * @param $params
- * @return ResultWrapper
- * @throws \Exception
- */
- public function getReceivedByOrder($params)
- {
- $this->objDReceiveReceiptIndex->setTable('qianniao_receive_receipt_index_'.$this->enterpriseId);
- $dbResult = $this->objDReceiveReceiptIndex->select($params);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDReceiveReceiptIndex->error(), ErrorCode::$dberror);
- }
- if(empty($dbResult)){
- return ResultWrapper::success($dbResult);
- }
- $receiveReceiptIndex = array_shift($dbResult);
- unset($dbResult);
- $tableName = 'qianniao_receive_receipt_'.$this->enterpriseId.'_'.date('Y', $receiveReceiptIndex['createTime']) . '_' . ceil(date('m', $receiveReceiptIndex['createTime']) / 3);
- $sql = 'select * from '.$tableName.' where id = '.$receiveReceiptIndex['id'];
- $dbResult = $this->objDReceiveReceiptIndex->query($sql);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDReceiveReceiptIndex->error(), ErrorCode::$dberror);
- }
- $receiveReceipt = !empty($dbResult) ? array_shift($dbResult) : $dbResult;
- return ResultWrapper::success($receiveReceipt);
- }
- /**
- * 获取今日收款总额
- * @param null $shopId
- * @return ResultWrapper
- */
- public function getTodayReceivedMoney($shopId = null)
- {
- $result = $this->objOverviewCache->getBusinessOverview($this->enterpriseId, 'todayReceivedMoney', $shopId);
- //TODO:拿不到数据去ES筛选
- return $result ? $result : 0;
- }
-
- /**
- * 导出方法
- *
- * @param $receivedData
- */
- private static function exportReceived($receivedData)
- {
- //导出到本地
- header("Content-type:application/vnd.ms-excel");
- header("Content-Disposition:filename=收款列表记录.csv");
- header('Cache-Control: max-age=0');
- $fp = fopen('php://output', 'a');
-
- $head = ['单据号','原单据号', '单据日期', '客户', '收购款类型', '金额', '优惠金额', '实收金额','相关店铺', '审核状态']; //定义标题
-
- foreach ($head as $i => $v) {
- $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8'); //将中文标题转换编码,否则乱码
- }
- fputcsv($fp, $head);
- $limit = 10000;
- $num = 0;//计数器
- foreach ($receivedData as $v) {
- //循环数据
- $num++;
- if ($num == $limit) {
- ob_flush();//释放内存
- flush();
- }
- $rows['no'] = isset($v['no']) ? $v['no'] : '';//单据号
- $rows['sourceNo'] = isset($v['sourceNo']) ? $v['sourceNo'] : '';//原单据号
- $rows['createTime'] = isset($v['createTime']) ? date('Y-m-d H:i:s',$v['createTime']) : '';//单据日期
- $rows['customerName'] = isset($v['customerName']) ? $v['customerName'] : '';//客户
- $rows['financeType'] = isset($v['financeType']) ? $v['financeType'] : '';//收购款类型
- $rows['totalMoney'] = isset($v['totalMoney']) ? $v['totalMoney'] : '';//金额
- $rows['totalDiscountMoney'] = isset($v['totalDiscountMoney']) ? $v['totalDiscountMoney'] : '';//优惠金额
- $rows['totalFinalMoney'] = isset($v['totalFinalMoney']) ? $v['totalFinalMoney'] : '';//实收金额
- $rows['shopName'] = isset($v['shopName']) ? $v['shopName'] : '';//相关店铺
- $rows['auditStatus'] = $v['auditStatus'] === 1 ? '待审核' : '审核通过';//审核状态
-
- foreach ($rows as $kk => $vv) {
- $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码
- }
- fputcsv($fp, $rs);
- $rows = [];
- }
- exit;
- }
- /**
- * 收款查询核销记录
- */
- public function getAllReceivedOffset($params)
- {
- $receivedId = $params['receivedId'];
- $dbResult = $this->objDReceiveOffset->select(['receivedId'=>$receivedId]);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDReceiveOffset->error(), ErrorCode::$dberror);
- }
- if(empty($dbResult)){
- return ResultWrapper::success([]);
- }
- foreach ($dbResult as $key => $value){
- $suffix = date('Y', $value['receiveCreateTime']) . '_' . ceil(date('m', $value['receiveCreateTime']) / 3);
- $this->objDReceive->setTable('qianniao_receive_receipt_' . $this->enterpriseId . '_' . $suffix);
- $this->objDReceived->setTable('qianniao_received_' . $this->enterpriseId . '_' . date('Y', $value['createTime']) . '_' . ceil(date('m', $value['createTime']) / 3));
- //查询应收
- $receiveDate = $this->objDReceive->get($value['receiveReceiptId']);
- if($receiveDate === false){
- return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
- }
- //查询收款
- $receivedDate = $this->objDReceived->get($value['receivedId']);
- if($receiveDate === false){
- return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
- }
- if(!empty($receiveDate)){
- $dbResult[$key]['receiveId'] = $value['receiveReceiptId'];
- $dbResult[$key]['no'] = StatusCode::$noPrefix[16].'-'.$receiveDate['no'];
- $dbResult[$key]['offsetMoney'] = $value['offsetMoney'];
- $dbResult[$key]['notOffsetMoney'] = $receiveDate['notOffsetMoney'];
- $dbResult[$key]['money'] = $receivedDate['totalFinalMoney'];
- $dbResult[$key]['originId'] = $receiveDate['originId'];
- $dbResult[$key]['originNo'] = StatusCode::$noPrefix[1].'-'.$receiveDate['originNo'];
- $dbResult[$key]['customerName'] = $receiveDate['customerName'];
- $dbResult[$key]['customerId'] = $receiveDate['customerId'];
- }
- }
- return ResultWrapper::success($dbResult);
- }
- }
|