123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Gss
- * Date: 2021/4/8 0008
- * Time: 19:35
- */
- namespace JinDouYun\Model\Finance;
- use Jindouyun\Cache\EnterpriseCache;
- use JinDouYun\Dao\Customer\DCustomer;
- use JinDouYun\Dao\Order\DOrderIndex;
- use JinDouYun\Model\System\MSystemPushMessage;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ResultWrapper;
- use JinDouYun\Model\Order\MPay;
- use JinDouYun\Dao\Finance\DRefund;
- use JinDouYun\Dao\Finance\DRefundAccount;
- use JinDouYun\Dao\Finance\DRefundIndex;
- use JinDouYun\Dao\Order\DOrder;
- use JinDouYun\Dao\Finance\DPay;
- use JinDouYun\Dao\Finance\DPayReceiptIndex;
- use JinDouYun\Dao\Finance\DReceive;
- use JinDouYun\Dao\Finance\DReceiveReceiptIndex;
- use Util\WeiXin\Pay;
- class MRefund
- {
- private $enterpriseId;
- private $userCenterId;
- private $objDReceiveReceiptIndex;
- private $objDReceive;
- private $objDay;
- private $objDPayReceiptIndex;
- private $objDOrder;
- private $objDRefund;
- private $objDRefundIndex;
- private $objDRefundAccount;
- private $objMFinanceType;
- private $objMCustomerBalance;
- private $objMCustomerBalanceDetail;
- private $objMSupplierBalanceDetail;
- private $objMSupplierBalance;
- private $cutTable = 200000;
- /**
- * @param $enterpriseId
- * @param $userCenterId
- * @throws \Exception
- */
- public function __construct($enterpriseId, $userCenterId)
- {
- $this->userCenterId = $userCenterId;
- $this->enterpriseId = $enterpriseId;
- $this->objDReceive = new DReceive('finance');
- $this->objDReceiveReceiptIndex = new DReceiveReceiptIndex('finance');
- $this->objDay = new DPay('finance');
- $this->objDPayReceiptIndex = new DPayReceiptIndex('finance');
- $this->objDOrder = new DOrder();
- $this->objDRefund = new DRefund('finance');
- $this->objDRefundIndex = new DRefundIndex('finance');
- $this->objDRefundAccount = new DRefundAccount('finance');
- $this->objMFinanceType = new MFinanceType($enterpriseId, $userCenterId);
- $this->objMCustomerBalance = new MCustomerBalance($enterpriseId, $userCenterId);
- $this->objMCustomerBalanceDetail = new MCustomerBalanceDetail($enterpriseId,$userCenterId);
- $this->objMSupplierBalance = new MSupplierBalance($enterpriseId, $userCenterId);
- $this->objMSupplierBalanceDetail = new MSupplierBalanceDetail($enterpriseId, $userCenterId);
- // $this->objDReceiveOffset->setTable('qianniao_receive_offset_'. $enterpriseId);
- $this->objDRefund->setTable('qianniao_refund_' . $enterpriseId . '_' . date('Y') . '_' . ceil(date('m') / 3));
- $this->objDRefundAccount->setTable('qianniao_refund_account_' . $enterpriseId . '_' . date('Y') . '_' . ceil(date('m') / 3));
- $this->objDRefundIndex->setTable('qianniao_refund_index_' . $enterpriseId);
- $this->objDPayReceiptIndex->setTable('qianniao_pay_receipt_index_'.$this->enterpriseId);
- $this->objDReceiveReceiptIndex->setTable('qianniao_receive_receipt_index_'.$this->enterpriseId);
- }
- /**
- * 添加退款单
- */
- public function addRefund($params, $autoCheck= false)
- {
- $accountList = $params['accountList'];
- unset($params['accountList']);
- $beginTransactionStatus = $this->objDRefund->beginTransaction();
- // 生成编号
- $dbResult = $this->objDRefund->get('createTime >='.strtotime(date('Ymd'.'0:0:0')), 'no', 'createTime desc');
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDRefund->error(), ErrorCode::$dberror);
- }
- if(empty($dbResult)){
- $params['no'] = createSerialNumberByDate('');
- }else{
- $params['no'] = createSerialNumberByDate($dbResult['no']);
- }
- $refundId = $this->objDRefund->insert($params);
- if ($refundId === false) {
- $this->objDRefund->rollBack();
- return ResultWrapper::fail($this->objDRefund->error(), ErrorCode::$dberror);
- }
- //索引表数据
- $indexData = [
- 'no' => $params['no'],
- 'refundId' => $refundId,
- 'unitId' => isset($params['unitId']) ? isset($params['unitId']): '',
- 'originId' => isset($params['originId']) ? isset($params['originId']): '',
- 'originNo' => isset($params['originNo']) ? isset($params['originNo']): '',
- 'type' => getArrayItem($params,'type',''),
- 'payType' => getArrayItem($params,'payType',''),
- 'currentAccountName' => $params['currentAccountName'],//制单人
- 'sourceNo' => $params['sourceNo'],
- 'auditStatus' => $params['auditStatus'],
- 'financeTypeId' => $params['financeTypeId'],
- 'financeType' => $params['financeType'],
- 'createTime' => $params['createTime'],
- 'updateTime' => $params['updateTime'],
- 'refundTime' => $params['refundTime'],
- ];
- $result = $this->objDRefundIndex->insert($indexData);
- if ($result === false) {
- $this->objDRefund->rollBack();
- return ResultWrapper::fail($this->objDRefundIndex->error(), ErrorCode::$dberror);
- }
- unset($result);
- //收款账户数据
- foreach ($accountList as $account) {
- $account['operatorId'] = $this->userCenterId;
- $account['refundId'] = $refundId;
- $account = self::buildAccountData($account);
- $result = $this->objDRefundAccount->insert($account);
- if ($result === false) {
- $this->objDRefund->rollBack();
- return ResultWrapper::fail($this->objDRefundAccount->error(), ErrorCode::$dberror);
- }
- unset($result);
- }
- if( $beginTransactionStatus ){
- $this->objDRefund->commit();
- }
- return ResultWrapper::success($refundId);
- }
- public static function buildAccountData(array $params)
- {
- return [
- 'refundId' => isset($params['refundId']) ? $params['refundId'] : 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,
- 'payWay' => isset($params['payWay']) ? $params['payWay'] : 0,
- 'remark' => isset($params['remark']) ? $params['remark'] : '',
- 'createTime' => time(),
- 'updateTime' => time(),
- 'operatorId' => isset($params['operatorId']) ? $params['operatorId'] : 0,
- ];
- }
- /**
- * 修改退款单
- */
- public function editRefund($params)
- {
- $this->objDRefund->setTable('qianniao_refund_' . $this->enterpriseId . '_' . date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3));
- $this->objDRefundAccount->setTable('qianniao_refund_account_' . $this->enterpriseId . '_' . date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3));
- $beginTransactionstatus = $this->objDRefund->beginTransaction();
- $accountList = $params['accountList'];
- unset($params['accountList']);
- $params['money'] = 0;
- foreach ($accountList as $account) {
- $params['money'] += $account['money'];
- }
- $refundId = $params['id'];
- unset($params['id']);
- $dbResult = $this->objDRefund->update($params, $refundId);
- if ($dbResult === false) {
- $this->objDRefund->rollBack();
- return ResultWrapper::fail($this->objDRefund->error(), ErrorCode::$dberror);
- }
- //索引表数据
- $indexData = [
- 'refundId' => $refundId,
- 'unitId' => isset($params['unitId']) ? isset($params['unitId']): '',
- 'originId' => isset($params['originId']) ? isset($params['originId']): '',
- 'originNo' => isset($params['originNo']) ? isset($params['originNo']): '',
- 'type' => getArrayItem($params,'type',''),
- 'currentAccountName' => $params['currentAccountName'],
- 'no' => $params['no'],
- 'sourceNo' => $params['sourceNo'],
- 'auditStatus' => $params['auditStatus'],
- 'financeTypeId' => $params['financeTypeId'],
- 'financeType' => $params['financeType'],
- 'createTime' => $params['createTime'],
- 'updateTime' => $params['updateTime'],
- 'refundTime' => $params['refundTime'],
- ];
- $result = $this->objDRefundIndex->update($indexData,['refundId'=>$refundId]);
- if ($result === false) {
- $this->objDRefund->rollBack();
- return ResultWrapper::fail($this->objDRefundIndex->error(), ErrorCode::$dberror);
- }
- unset($result);
- //收款账户数据
- foreach ($accountList as $account) {
- if (isset($account['id'])) {
- $account['refundId'] = $refundId;
- $account['operatorId'] = $this->userCenterId;
- $account['updateTime'] = $params['updateTime'];
- $result = $this->objDRefundAccount->update($account, $account['id']);
- if ($result === false) {
- $this->objDRefund->rollBack();
- return ResultWrapper::fail($this->objDRefundAccount->error(), ErrorCode::$dberror);
- }
- unset($result);
- }
- }
- if($beginTransactionstatus){
- $this->objDRefund->commit();
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 退款单审核
- */
- public function updateRefundStatus($params)
- {
- $this->objDRefund->setTable('qianniao_refund_' . $this->enterpriseId . '_' . date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3));
- $this->objDRefundAccount->setTable('qianniao_refund_account_' . $this->enterpriseId . '_' . date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3));
- // 查询退款单数据
- $refundData = $this->objDRefund->get(['id' => $params['id']]);
- if ($refundData === false) {
- return ResultWrapper::fail($this->objDRefund->error(), ErrorCode::$dberror);
- }
- if(empty($refundData)){
- return ResultWrapper::fail('要操作得单据不存在', ErrorCode::$contentNotExists);
- }
- if($refundData['auditStatus'] == StatusCode:: $auditStatus['auditPass']){
- return ResultWrapper::fail('当前退款单已经审核过了', ErrorCode::$notAllowAccess);
- }
- // 1,根据退款单中的originId(来源订单id)查询订单的source来源是否为销售订单
- $orderIndexId = $refundData['originId'];
- $objDOrderIndex = new DOrderIndex('default');
- $objDOrderIndex->setTable('qianniao_order_index_' . $this->enterpriseId);
- $orderData = $objDOrderIndex->get($orderIndexId);
- if($orderData === false){
- return ResultWrapper::fail($objDOrderIndex->error(), ErrorCode::$dberror);
- }
- if( empty($orderData) ){
- return ResultWrapper::fail('该订单为空', ErrorCode::$contentNotExists);
- }
-
- // 根据订单的来源判断是否为销售订单
- if($orderData['source'] == StatusCode::$orderType['saleOrder']){
- // 根据客户id (customerId) 获取 openId
- $customerId = $orderData['customerId'];
- $objDCustomer = new DCustomer('default');
- $objDCustomer->setTable('qianniao_customer_' . $this->enterpriseId);
- $customerData = $objDCustomer->get($customerId);
- if($customerData === false){
- return ResultWrapper::fail($objDCustomer->error(), ErrorCode::$dberror);
- }
- if( empty($customerData) ){
- return ResultWrapper::fail('查找不到该客户数据', ErrorCode::$contentNotExists);
- }
- // 小程序订阅消息退款通知
- if (isset($customerData['openId']) && !empty($customerData['openId'])) {
- // 发送小程序订阅消息
- $sendMessageData = [
- 'time8' => ['value' => date('Y-m-d H:i:s',$orderData['createTime'])], // 下单时间
- 'character_string7' => ['value' => StatusCode::$noPrefix[1].'-'.$orderData['no']], // 订单编号
- 'amount9' => ['value' => '¥'.$refundData['money']], // 支付金额
- 'amount3' => ['value' => '¥'.$refundData['money']], // 退款金额
- 'thing1' => ['value' =>'退款成功(店主已同意退款)'], // 退款状态
- ];
- $objMSystemPushMessage = new MSystemPushMessage($this->userCenterId,$this->enterpriseId);
- $result = $objMSystemPushMessage->sendWeiXinPushMessage(7,$customerData['openId'],$sendMessageData);
- if (!$result->isSuccess()) {
- file_put_contents('/www/wwwroot/logs/api.junhailan.com/sendMessageError.log', date('Y-m-d H:i:s') . 'bb' . var_export($result->getData(), true) . PHP_EOL, FILE_APPEND);
- }
- }
- }
- $beginTransactionStatus = $this->objDRefund->beginTransaction();
- // 更新退款单审核状态
- $dbResult = $this->objDRefund->update(['auditStatus' => StatusCode:: $auditStatus['auditPass']], ['id' => $params['id']]);
- if ($dbResult === false) {
- $this->objDRefund->rollBack();
- return ResultWrapper::fail($this->objDRefund->error(), ErrorCode::$dberror);
- }
- // 更新退款单索引表审核状态
- $dbResult = $this->objDRefundIndex->update(['auditStatus' => StatusCode:: $auditStatus['auditPass']], ['refundId' => $params['id']]);
- if ($dbResult === false) {
- $this->objDRefund->rollBack();
- return ResultWrapper::fail($this->objDRefundIndex->error(), ErrorCode::$dberror);
- }
- if (isset($refundData['offsetId'])){//有核销id说明有应收单id
- //自动核销
- //self::autoMoney($refundData['offsetId'],$refundData['money'],$refundData['createTime'],$refundData['type']);
- }
- if ($refundData['type'] == StatusCode::$standard){//客户
- // 获取客户往来余额
- $customerBalance = $this->objMCustomerBalance->getCustomerBalance($refundData['unitId']);
- $detailData = [
- 'customerId' => $refundData['unitId'],//'客户id',
- 'receiptTime' => $refundData['createTime'],//'单据日期',
- 'sourceNo' => $refundData['no'],//'单据编号',
- 'sourceId' => $refundData['id'],//'单据编号',
- 'financeType' => $refundData['financeType'],//'财务类型名称',
- 'financeTypeId' => $refundData['financeTypeId'],//'财务类型id',
- 'originId' => $refundData['sourceId'], //
- 'originNo' => $refundData['sourceNo'],//
- 'salesAmount' => 0,//总金额
- 'discountMoney' => 0,// '优惠金额',
- 'customerAmount' => 0,// '客户承担金额',
- 'receivableAmount' => 0,// '应收金额',
- 'actualReceivableAmount' => 0,//'实际收款金额',
- 'refundMoney' => $refundData['money'],
- 'receivableBalance' => bcadd($customerBalance , $refundData['money'], 4),//'应收后余额',
- 'remark' => '退款单审核通过,本次退款' . $refundData['money'] . '元',//'备注',
- 'createTime' => time(),//'创建日期',
- 'updateTime' => time(),//'修改日期',
- ];
- // 添加客户往来余额明细
- $result = $this->objMCustomerBalanceDetail->addCustomerBalanceDetail($detailData);
- if (!$result->isSuccess()) {
- $this->objDRefund->rollBack();
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- // 编辑客户往来余额
- $changedMoney = $refundData['money'] < 0 ? -1 * $refundData['money'] : abs($refundData['money']);
- $result = $this->objMCustomerBalance->addCustomerBalance($refundData['unitId'], $changedMoney);
- if (!$result->isSuccess()) {
- $this->objDRefund->rollBack();
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- // 如果是订单生成的退款单,同步操作订单
- if ( !empty($refundData['originId']) && !empty($refundData['payType'])){
- // 获取订单outerTradeNo
- $tableName = $this->objDOrder->getTableName('qianniao_order_' . $this->enterpriseId, $this->userCenterId, $this->cutTable);
- $this->objDOrder->setTable($tableName);
- $orderDate = $this->objDOrder->get($refundData['originId']);
- if( $orderDate === false ){
- return ResultWrapper::fail($this->objDOrder->error(), ErrorCode::$dberror);
- }
- if( empty($orderDate) ){
- return ResultWrapper::fail('查询指定订单数据为空', ErrorCode::$contentNotExists);
- }
- // 读取后台设置的退款返还方式
- $refundWay = StatusCode::$standard;
- $objEnterpriseCache = new EnterpriseCache();
- $enterSettingData = $objEnterpriseCache->getEnterpriseSetting($this->enterpriseId);
- if( isset($enterSettingData['returnWay'])){
- $refundWay = $enterSettingData['returnWay'];
- }
- // 进行退款操作
- $objMPay = new MPay($this->enterpriseId, $this->userCenterId);
- $result = $objMPay->refund($orderDate, false, $refundData, $refundWay);
- if(!$result->isSuccess()){
- $this->objDRefund->rollBack();
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- //判断退款金额
- if($refundData['money'] < $orderDate['payAmount']){
- $payStatus = StatusCode::$payStatus['partRefund'];
- }else{
- $payStatus = StatusCode::$payStatus['refund'];
- }
- // 修改订单支付状态为已经退款
- $updateData = [
- 'isRet' => StatusCode::$standard,
- 'retTime' => time(),
- 'retMoney' => $refundData['money'],
- 'payStatus' => $payStatus
- ];
- $dbResult = $this->objDOrder->update($updateData, $orderDate['id']);
- if($dbResult == false){
- return ResultWrapper::fail($this->objDOrder->error(), ErrorCode::$dberror);
- }
- $objDOrderIndex = new DOrderIndex('default');
- $objDOrderIndex->setTable('qianniao_order_index_'.$this->enterpriseId);
- $updateData = [
- 'isRet' => StatusCode::$standard,
- 'payStatus' => $payStatus
- ];
- $orderIndex = $objDOrderIndex->update($updateData, $orderDate['id']);
- if ($orderIndex===false){
- return ResultWrapper::fail($objDOrderIndex->error(),ErrorCode::$dberror);
- }
- }
- }
- if($refundData['type'] == StatusCode::$delete){
- // 查询供应商当前的余额
- $supplierMoney = $this->objMSupplierBalance->getSupplierBalance($refundData['unitId']);
- // 组装供应商余额明细数据
- $detailData = [
- 'supplierId' => $refundData['unitId'],
- 'receiptTime' => $refundData['createTime'],//'单据日期',
- 'no' => $refundData['no'],//'单据编号',
- 'financeType' => $refundData['financeType'],//'财务类型名称',
- 'financeTypeId' => $refundData['financeTypeId'],//'财务类型id',
- 'sourceId' => $refundData['sourceId'], //
- 'sourceNo' => $refundData['sourceNo'],
- 'salesAmount' => $refundData['money'],
- 'discountMoney' => 0.00,// '优惠金额',
- 'supplierAmount' => 0.00,
- 'shouldPayAmount' => 0.00,//实际应付金额
- 'actualPaidAmount' => 0,
- 'refundMoney' => $refundData['money'],
- // 'actualPaidAmount' => $refundData['money'],
- // 'shouldPayBalance' => bcadd($supplierMoney,$refundData['money'],2),//应付款余额 当前供应商余额 + 退款金额
- 'shouldPayBalance' => 0, // 退款直接记录退款金额
- 'remark' => '退款单审核通过,应付供应商'.$refundData['payMoney'].'元',//'备注',
- 'createTime' => time(),//'创建日期',
- 'updateTime' => time(),//'修改日期',
- ];
- //供应商余额明细
- $result = $this->objMSupplierBalanceDetail->addSupplierBalanceDetail($detailData);
- if ($result->isSuccess() === false) {
- $this->objDRefund->rollBack();
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- //供应商余额
- $changedMoney = $refundData['money'];
- $changedMoney = $changedMoney > 0 ? -1 * $refundData['money'] : abs($refundData['money']);
- $result = $this->objMSupplierBalance->addSupplierBalance($refundData['unitId'], $changedMoney);
- if ($result->isSuccess() === false) {
- $this->objDRefund->rollBack();
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- }
- if ($beginTransactionStatus){
- $this->objDRefund->commit();
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 自动核销退款单
- */
- public function autoMoney($offsetId,$money,$createTime,$type)
- {
- if($type == StatusCode::$standard){//type 5 说明是订单退款
- //查询负应收
- $suffix = date('Y', $createTime) . '_' . ceil(date('m', $createTime) / 3);
- $this->objDReceive->setTable('qianniao_receive_receipt_' . $this->enterpriseId . '_' . $suffix);
- $receiveDate = $this->objDReceive->get(['id'=>$offsetId]);
- if ($receiveDate === false) {
- return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
- }
- if(empty($receiveDate)){
- return ResultWrapper::fail('要操作得应收单据不存在', ErrorCode::$contentNotExists);
- }
- //判断如果金额为负
- if($receiveDate['receiveMoney'] < 0){
- $updata = [ // 负应收没有核销金额一说
- // $offsetMoney = $money,
- // $notOffsetMoney = 0,
- 'offsetStatus' => StatusCode::$standard,
- ];
- $updataIndex = [
- 'offsetStatus' => StatusCode::$standard,
- ];
- $updataReceive = $this->objDReceive->update($updata,['id'=>$receiveDate['id']]);
- if ($updataReceive === false) {
- $this->objDRefund->rollBack();
- return ResultWrapper::fail($this->objDReceive->error(), ErrorCode::$dberror);
- }
- //更新index表核销状态
- $updataReceivesIndex = $this->objDReceiveReceiptIndex->update($updataIndex,['id'=>$receiveDate['id']]);
- if ($updataReceivesIndex === false) {
- $this->objDRefund->rollBack();
- return ResultWrapper::fail($this->objDReceiveReceiptIndex->error(), ErrorCode::$dberror);
- }
- }
- }else{// 说明是采购单
- //查询负应付
- $suffix = date('Y', $createTime) . '_' . ceil(date('m', $createTime) / 3);
- $this->objDay->setTable('qianniao_pay_receipt_' . $this->enterpriseId . '_' . $suffix);
- $payDate = $this->objDay->get(['id'=>$offsetId]);
- if ($payDate === false) {
- return ResultWrapper::fail($this->objDay->error(), ErrorCode::$dberror);
- }
- if(empty($payDate)){
- return ResultWrapper::fail('要操作得单据不存在', ErrorCode::$contentNotExists);
- }
- //判断如果金额为负,并且财务类型为采购退款单
- if($payDate['financeType'] == '采购退款单' && $payDate['payMoney'] == $money){
- $updata = [
- $offsetMoney = $money,
- $notOffsetMoney = 0,
- $offsetStatus = StatusCode::$standard,
- ];
- $updataIndex = [
- $offsetStatus = StatusCode::$standard,
- ];
- $updataPay = $this->objDay->update($updata,['id'=>$payDate['id']]);
- if ($updataPay === false) {
- $this->objDRefund->rollBack();
- return ResultWrapper::fail($this->objDay->error(), ErrorCode::$dberror);
- }
- //更新index表核销状态
- $updataPayIndex = $this->objDPayReceiptIndex->update($updataIndex,['id'=>$payDate['id']]);
- if ($updataPayIndex === false) {
- $this->objDRefund->rollBack();
- return ResultWrapper::fail($this->objDPayReceiptIndex->error(), ErrorCode::$dberror);
- }
- }
- }
- return ResultWrapper::success([]);
- }
- /**
- * 获取指定退款单信息
- * @throws \Exception
- */
- public function getRefundInfo($refundParams)
- {
- $this->objDRefund->setTable('qianniao_refund_' . $this->enterpriseId . '_' . date('Y', $refundParams['createTime']) . '_' . ceil(date('m', $refundParams['createTime']) / 3));
- $this->objDRefundAccount->setTable('qianniao_refund_account_' . $this->enterpriseId . '_' . date('Y', $refundParams['createTime']) . '_' . ceil(date('m', $refundParams['createTime']) / 3));
- $dbResult = $this->objDRefund->get($refundParams['id']);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDRefund->error(), ErrorCode::$dberror);
- }
- //渲染前缀
- // $dbResult['no'] = StatusCode::$noPrefix[17] . '-' . $dbResult['no'];
- $accountData = $this->objDRefundAccount->select(['refundId' => $refundParams['id']]);
- if ($accountData === false) {
- return ResultWrapper::fail($this->objDRefundAccount->error(), ErrorCode::$dberror);
- }
- $accountData = array_shift($accountData);
- $dbResult['accountList'] = $accountData;
- //查出客户当前余额
- $dbResult['currentBalanceMoney'] = !empty($dbResult['unitId']) && $dbResult['type'] == 1 ? $this->objMCustomerBalance->getCustomerBalance($dbResult['unitId']) : 0;
- return ResultWrapper::success($dbResult);
- }
- /**
- * 后台所有收款单列表
- */
- public function getAllRefund($selectParams)
- {
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- $type = $selectParams['type'];
- unset($selectParams['type']);
- //组装sql where条件
- $whereSql = '';
- 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['unitId']) && !empty($selectParams['unitId'])) {
- $where = empty($whereSql) ? ' WHERE ' : ' AND ';
- $whereSql .= $where . ' unitId = ' . $selectParams['unitId'];
- }
- if (isset($selectParams['type']) && !empty($selectParams['type'])) {
- $where = empty($whereSql) ? ' WHERE ' : ' AND ';
- $whereSql .= $where . ' type = ' . $selectParams['type'];
- }
- 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->objDRefundIndex->get_Table(). $whereSql . ' ORDER BY createTime DESC LIMIT ' . $offset . ' , ' . $limit;
- $refundIndexResult = $this->objDRefundIndex->query($sql);
- $tableSuffix = [];
- foreach ($refundIndexResult as $refundIndex) {
- $k = date('Y', $refundIndex['createTime']) . '_' . ceil(date('m', $refundIndex['createTime']) / 3);
- $tableSuffix[$k][] = $refundIndex['refundId'];
- }
- $refundResult = [];
- foreach ($tableSuffix as $suffix => $refundIds) {
- $this->objDRefund->setTable('qianniao_refund_' . $this->enterpriseId . '_' . $suffix);
- $dbResult = $this->objDRefund->select($refundIds, '*', 'createTime desc');
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDRefund->error(), ErrorCode::$dberror);
- }
- // if (!empty($dbResult)){
- // //渲染前缀
- // foreach ($dbResult as $key => $value){
- // $dbResult[$key]['no'] = StatusCode::$noPrefix[17] . '-' . $value['no'];
- // }
- // }
- $refundResult = array_merge($refundResult, $dbResult);
- }
- $totalSql = 'SELECT COUNT(1) as count FROM ' .$this->objDRefundIndex->get_Table() . $whereSql;
- $dbTotalResult = $this->objDRefundIndex->query($totalSql);
- if ($dbTotalResult === false) {
- return ResultWrapper::fail($this->objDRefundIndex->error(), ErrorCode::$dberror);
- }
- if(empty($dbTotalResult)){
- return ResultWrapper::success([]);
- }
- $return = [
- 'data' => self::format($refundResult),
- 'total' => $dbTotalResult[0]['count']
- ];
- return ResultWrapper::success($return);
- }
- public function format($data)
- {
- foreach( $data as $k => $v ){
- $data[$k]['no'] = StatusCode::$noPrefix[20].'-'.$v['no'];
- switch ($v['financeType']){
- case '订单退货退款':
- $data[$k]['originNo'] = StatusCode::$noPrefix[1].'-'.$v['originNo'];
- $data[$k]['sourceNo'] = StatusCode::$noPrefix[15].'-'.$v['sourceNo'];
- break;
- case '采购退货收款':
- $data[$k]['originNo'] = StatusCode::$noPrefix[2].'-'.$v['originNo'];
- $data[$k]['sourceNo'] = StatusCode::$noPrefix[4].'-'.$v['sourceNo'];
- break;
- case '订单完结退款':
- $data[$k]['originNo'] = StatusCode::$noPrefix[1].'-'.$v['originNo'];
- $data[$k]['sourceNo'] = StatusCode::$noPrefix[1].'-'.$v['sourceNo'];
- break;
- case '订单取消退款':
- $data[$k]['originNo'] = StatusCode::$noPrefix[1].'-'.$v['originNo'];
- $data[$k]['sourceNo'] = StatusCode::$noPrefix[1].'-'.$v['sourceNo'];
- }
- }
- return $data;
- }
- }
|