123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Gss
- * Date: 2021/3/6 0006
- * Time: 15:01
- */
- namespace JinDouYun\Model\Finance;
- use JinDouYun\Controller\Market\VipCard;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ResultWrapper;
- use JinDouYun\Dao\Finance\DReceiptRequisition;
- use JinDouYun\Dao\Finance\DReceiptRequisitionAccount;
- class MReceiptRequisition
- {
- private $objDReceiptRequisition;
- private $objDReceiptRequisitionAccount;
- private $enterpriseId;
- private $userCenterId;
- public function __construct($enterpriseId, $userCenterId)
- {
- $this->userCenterId = $userCenterId;
- $this->enterpriseId = $enterpriseId;
- $this->objDReceiptRequisition = new DReceiptRequisition('finance');
- $this->objDReceiptRequisitionAccount = new DReceiptRequisitionAccount('finance');
- $this->objDReceiptRequisition->setTable('qianniao_receipt_requisition_' . $enterpriseId);
- $this->objDReceiptRequisitionAccount->setTable('qianniao_receipt_requisition_account_' . $enterpriseId);
- }
- /**
- * 新增收款单
- */
- public function addReceiptRequisition($params)
- {
- $receiptRequisitionAccountDate = $params['receiptRequisitionAccountDate'];
- unset($params['receiptRequisitionAccountDate']);
- $dbResult = $this->objDReceiptRequisition->get('createTime >='.strtotime(date('Ymd'.'0:0:0')), 'no', 'createTime desc');
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDReceiptRequisition->error(), ErrorCode::$dberror);
- }
- if(empty($dbResult)){
- $params['no'] = createSerialNumberByDate('');
- }else{
- $params['no'] = createSerialNumberByDate($dbResult['no']);
- }
- //开启事务
- $beginTransactionStatus = $this->objDReceiptRequisition->beginTransaction();
- $receiptRequisitionId = $this->objDReceiptRequisition->insert($params);
- if ($receiptRequisitionId === false) {
- $this->objDReceiptRequisition->rollBack();
- return ResultWrapper::fail($this->objDReceiptRequisition->error(), ErrorCode::$dberror);
- }
- foreach ($receiptRequisitionAccountDate as $key => $value){
- $receiptRequisitionAccountDate[$key]['receiptRequisitionId'] = $receiptRequisitionId;
- $receiptRequisitionAccountDate[$key]['createTime'] = time();
- }
- $receiptRequisitionAccountId = $this->objDReceiptRequisitionAccount->insert($receiptRequisitionAccountDate,true);
- if ($receiptRequisitionAccountId === false) {
- $this->objDReceiptRequisition->rollBack();
- return ResultWrapper::fail($this->objDReceiptRequisitionAccount->error(), ErrorCode::$dberror);
- }
- if($beginTransactionStatus){
- $this->objDReceiptRequisition->commit();
- }
- return ResultWrapper::success($receiptRequisitionId);
- }
- /**
- * 获取指定的收款单
- */
- public function getReceiptRequisitionInfo($receiptRequisitionId)
- {
- $dbResult = $this->objDReceiptRequisition->get($receiptRequisitionId);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDReceiptRequisition->error(), ErrorCode::$dberror);
- }
- if(empty($dbResult)){
- return ResultWrapper::success([]);
- }
- //渲染前缀
- $dbResult['no'] = StatusCode::$noPrefix[17] . '-' . $dbResult['no'];
- $dbResultAccount = $this->objDReceiptRequisitionAccount->select(['receiptRequisitionId'=>$receiptRequisitionId,'deleteStatus'=>StatusCode::$standard]);
- if($dbResultAccount === false){
- return ResultWrapper::fail($this->objDReceiptRequisitionAccount->error(), ErrorCode::$dberror);
- }
- if(!empty($dbResultAccount)){
- $dbResult['receiptRequisitionAccountDate'] = $dbResultAccount;
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 编辑指定的收款单
- */
- public function editReceiptRequisition($params)
- {
- $updateReceiptRequisitionId = $params['id'];
- unset($params['id']);
- unset($params['createTime']);
- $receiptRequisitionAccountDate = $params['receiptRequisitionAccountDate'];
- unset($params['receiptRequisitionAccountDate']);
- //累计金额
- $params['totalCollectionAmount'] = 0;
- $params['totalPreferentialAmount'] = 0;
- $params['totalActualAmount'] = 0;
- foreach ($receiptRequisitionAccountDate as $account) {
- $account['preferentialAmount'] = !empty($account['preferentialAmount']) ? $account['preferentialAmount'] : 0;
- $params['totalCollectionAmount'] += $account['actualAmount'];
- $params['totalPreferentialAmount'] += $account['preferentialAmount'];
- $params['totalActualAmount'] += ($account['actualAmount'] - $account['preferentialAmount']);
- }
- //开启事务
- $beginTransactionStatus = $this->objDReceiptRequisition->beginTransaction();
- $receiptRequisitionId = $this->objDReceiptRequisition->update($params, $updateReceiptRequisitionId);
- if($receiptRequisitionId === false){
- $this->objDReceiptRequisition->rollBack();
- return ResultWrapper::fail($this->objDReceiptRequisition->error(), ErrorCode::$dberror);
- }
- foreach ($receiptRequisitionAccountDate as $key => $value){
- $id = getArrayItem($value, 'id', 0);
- if($id){
- $dbResult = $this->objDReceiptRequisitionAccount->update($value,['receiptRequisitionId'=>$id]);
- if ($dbResult === false) {
- $this->objDReceiptRequisition->rollBack();
- return ResultWrapper::fail($this->objDReceiptRequisitionAccount->error(), ErrorCode::$dberror);
- }
- }else{
- $value['receiptRequisitionId'] = $updateReceiptRequisitionId;
- $dbResult = $this->objDReceiptRequisitionAccount->insert($value);
- if ($dbResult === false) {
- $this->objDReceiptRequisition->rollBack();
- return ResultWrapper::fail($this->objDReceiptRequisitionAccount->error(), ErrorCode::$dberror);
- }
- }
- }
- if($beginTransactionStatus){
- $this->objDReceiptRequisition->commit();
- }
- return ResultWrapper::success($receiptRequisitionId);
- }
- /**
- * 删除指定的收款单
- */
- public function delReceiptRequisition($receiptRequisitionId)
- {
- if (empty($receiptRequisitionId)) {
- return ResultWrapper::fail('参数错误', ErrorCode::$paramError);
- }
- $beginTransactionStatus = $this->objDReceiptRequisition->beginTransaction();
- $dbResult = $this->objDReceiptRequisition->update( ['deleteStatus'=>StatusCode::$delete], $receiptRequisitionId);
- if($dbResult === false){
- $this->objDReceiptRequisition->rollBack();
- return ResultWrapper::fail($this->objDReceiptRequisition->error(), ErrorCode::$dberror);
- }
- unset($dbResult);
- $dbResult = $this->objDReceiptRequisitionAccount->update( ['deleteStatus'=>StatusCode::$delete], ['receiptRequisitionId'=>$receiptRequisitionId]);
- if($dbResult === false){
- $this->objDReceiptRequisition->rollBack();
- return ResultWrapper::fail($this->objDReceiptRequisitionAccount->error(), ErrorCode::$dberror);
- }
- if($beginTransactionStatus){
- $this->objDReceiptRequisition->commit();
- }
- return ResultWrapper::success($receiptRequisitionId);
- }
- /**
- * 获取所有的收款单
- */
- public function getAllReceiptRequisition($selectParams)
- {
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- $selectParams['deleteStatus'] = StatusCode::$standard;
- $returnData = [
- 'data' => [],
- 'total' => 0,
- ];
- $whereSql = '';
- // if (isset($selectParams['operatorName']) && !empty($selectParams['operatorName'])) {
- // $where = empty($whereSql) ? ' WHERE ' : ' AND ';
- // $whereSql .= $where . ' operatorName like "%' . $selectParams['operatorName'] . '%"';
- // }
- if (isset($selectParams['auditStatus']) && !empty($selectParams['auditStatus'])) {
- $where = empty($whereSql) ? ' WHERE ' : ' AND ';
- $whereSql .= $where . ' auditStatus = ' . $selectParams['auditStatus'];
- }
- if (isset($selectParams['custormerId']) && !empty($selectParams['custormerId'])) {
- $where = empty($whereSql) ? ' WHERE ' : ' AND ';
- $whereSql .= $where . ' customerId = ' . $selectParams['custormerId'];
- }
- $where = empty($whereSql) ? ' WHERE ' : ' AND ';
- $whereSql .= $where . 'deleteStatus = ' . StatusCode::$standard;
- $sql = 'SELECT * FROM ' .$this->objDReceiptRequisition->get_Table().$whereSql . ' ORDER BY createTime DESC LIMIT ' . $offset . ' , ' . $limit;
- $dbResult = $this->objDReceiptRequisition->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDReceiptRequisition->error(), ErrorCode::$dberror);
- }
- if(empty($dbResult)){
- return ResultWrapper::success($returnData);
- }
- //渲染前缀
- foreach ($dbResult as $key => $value){
- $dbResult[$key]['no'] = StatusCode::$noPrefix[17] . '-' . $value['no'];
- }
- $totalSql = 'SELECT COUNT(1) as count FROM ' .$this->objDReceiptRequisition->get_Table().$whereSql;
- $dbTotalResult = $this->objDReceiptRequisition->query($totalSql);
- if ($dbTotalResult === false) {
- return ResultWrapper::fail($this->objDReceiptRequisition->error(), ErrorCode::$dberror);
- }
- if(empty($dbTotalResult)){
- return ResultWrapper::success([]);
- }
- $return = [
- 'data' => $dbResult,
- 'total' => $dbTotalResult[0]['count']
- ];
- return ResultWrapper::success($return);
- }
- /**
- * 审核申请收款单
- */
- public function updateReceiptRequisitionStatus($params)
- {
- $ReceiptRequisitionData = $this->objDReceiptRequisition->get($params['id']);
- if ($ReceiptRequisitionData === false) {
- return ResultWrapper::fail($this->objDReceiptRequisition->error(), ErrorCode::$dberror);
- }
- if( empty($ReceiptRequisitionData) ){
- return ResultWrapper::fail('要审核的单据不存在', ErrorCode::$contentNotExists);
- }
- if( $ReceiptRequisitionData['auditStatus'] == StatusCode::$auditStatus['auditPass'] ){
- return ResultWrapper::fail('该单据已经审核过了', ErrorCode::$notAllowAccess);
- }
- //查询accountList信息
- $ReceiptRequisitionAccountList = $this->objDReceiptRequisitionAccount->select(['receiptRequisitionId'=>$params['id']]);
- //组装accountList
- $ReceiptRequisitionData['accountList']= [];
- foreach ($ReceiptRequisitionAccountList as $key => $value){
- $accountList = [
- 'accountId' => $value['accountId'],
- 'accountNumber'=>'',
- 'accountName'=> $value['accountName'],
- 'money'=> $value['collectionAmount'],
- 'discountMoney'=>$value['preferentialAmount'],
- 'finalMoney' => $value['actualAmount'],
- 'payWay'=> '',
- 'remark'=>$value['remark']
- ];
- array_push($ReceiptRequisitionData['accountList'],$accountList);
- }
- $beginTransactionStatus = $this->objDReceiptRequisition->beginTransaction();
- // 判断是否自动生成收款单 autotype
- if($params['autoType'] == StatusCode::$delete){//只审核单据,不生成收款单
- $dbResult = $this->objDReceiptRequisition->update(['auditStatus' => StatusCode::$auditStatus['auditPass']], $params['id']);
- }
- if($params['autoType'] == StatusCode::$standard){//自动生成
- //请求add收款单,生成新的收款单
- //生成收款单
- $objMReceived = new MReceived($this->enterpriseId, $this->userCenterId);
- //创建收款单
- $received = [
- 'currentAccountName' => $ReceiptRequisitionData['operatorName'],
- 'financeType' => '销售收款',
- 'financeTypeId' => 2,
- 'shopId' => $ReceiptRequisitionData['shopId'],
- 'shopName' => $ReceiptRequisitionData['shopName'],
- 'receiptTime' => time(),
- 'operatorId' => $this->userCenterId,
- 'accountList' => $ReceiptRequisitionData['accountList'],
- 'customerId' => $ReceiptRequisitionData['customerId'],
- 'customerName' => $ReceiptRequisitionData['customerName'],
- 'sourceNo' => $ReceiptRequisitionData['no'],
- 'sourceNoMoney' => $ReceiptRequisitionData['totalActualAmount'],
- 'createTime' => time(),
- 'auditStatus' => StatusCode::$auditStatus['auditing'],
- 'updateTime' => time(),
- 'receiptOffsetData' => []
- ];
- $receivedResult = $objMReceived->addReceived($received, true);
- if (!$receivedResult->isSuccess()) {
- $this->objDReceiptRequisition->rollBack();
- return ResultWrapper::fail($receivedResult->getData(), $receivedResult->getErrorCode());
- }
- $dbResult = $this->objDReceiptRequisition->update(['auditStatus' => StatusCode::$auditStatus['auditPass']], $params['id']);
- //走更新接口
- $updateReceived = $objMReceived->updateReceivedStatus(['id'=>$receivedResult->getData(),'createTime'=>time()]);
- if (!$updateReceived->isSuccess()) {
- $this->objDReceiptRequisition->rollBack();
- return ResultWrapper::fail($updateReceived->getData(), $updateReceived->getErrorCode());
- }
- }
- if ($dbResult === false) {
- $this->objDReceiptRequisition->rollBack();
- return ResultWrapper::fail($this->objDReceiptRequisition->error(), ErrorCode::$dberror);
- }
- if ($beginTransactionStatus){
- $this->objDReceiptRequisition->commit();
- }
- return ResultWrapper::success($dbResult);
- }
- }
|