MReceiptRequisition.Class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Gss
  5. * Date: 2021/3/6 0006
  6. * Time: 15:01
  7. */
  8. namespace JinDouYun\Model\Finance;
  9. use JinDouYun\Controller\Market\VipCard;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\StatusCode;
  12. use Mall\Framework\Core\ResultWrapper;
  13. use JinDouYun\Dao\Finance\DReceiptRequisition;
  14. use JinDouYun\Dao\Finance\DReceiptRequisitionAccount;
  15. class MReceiptRequisition
  16. {
  17. private $objDReceiptRequisition;
  18. private $objDReceiptRequisitionAccount;
  19. private $enterpriseId;
  20. private $userCenterId;
  21. public function __construct($enterpriseId, $userCenterId)
  22. {
  23. $this->userCenterId = $userCenterId;
  24. $this->enterpriseId = $enterpriseId;
  25. $this->objDReceiptRequisition = new DReceiptRequisition('finance');
  26. $this->objDReceiptRequisitionAccount = new DReceiptRequisitionAccount('finance');
  27. $this->objDReceiptRequisition->setTable('qianniao_receipt_requisition_' . $enterpriseId);
  28. $this->objDReceiptRequisitionAccount->setTable('qianniao_receipt_requisition_account_' . $enterpriseId);
  29. }
  30. /**
  31. * 新增收款单
  32. */
  33. public function addReceiptRequisition($params)
  34. {
  35. $receiptRequisitionAccountDate = $params['receiptRequisitionAccountDate'];
  36. unset($params['receiptRequisitionAccountDate']);
  37. $dbResult = $this->objDReceiptRequisition->get('createTime >='.strtotime(date('Ymd'.'0:0:0')), 'no', 'createTime desc');
  38. if ($dbResult === false) {
  39. return ResultWrapper::fail($this->objDReceiptRequisition->error(), ErrorCode::$dberror);
  40. }
  41. if(empty($dbResult)){
  42. $params['no'] = createSerialNumberByDate('');
  43. }else{
  44. $params['no'] = createSerialNumberByDate($dbResult['no']);
  45. }
  46. //开启事务
  47. $beginTransactionStatus = $this->objDReceiptRequisition->beginTransaction();
  48. $receiptRequisitionId = $this->objDReceiptRequisition->insert($params);
  49. if ($receiptRequisitionId === false) {
  50. $this->objDReceiptRequisition->rollBack();
  51. return ResultWrapper::fail($this->objDReceiptRequisition->error(), ErrorCode::$dberror);
  52. }
  53. foreach ($receiptRequisitionAccountDate as $key => $value){
  54. $receiptRequisitionAccountDate[$key]['receiptRequisitionId'] = $receiptRequisitionId;
  55. $receiptRequisitionAccountDate[$key]['createTime'] = time();
  56. }
  57. $receiptRequisitionAccountId = $this->objDReceiptRequisitionAccount->insert($receiptRequisitionAccountDate,true);
  58. if ($receiptRequisitionAccountId === false) {
  59. $this->objDReceiptRequisition->rollBack();
  60. return ResultWrapper::fail($this->objDReceiptRequisitionAccount->error(), ErrorCode::$dberror);
  61. }
  62. if($beginTransactionStatus){
  63. $this->objDReceiptRequisition->commit();
  64. }
  65. return ResultWrapper::success($receiptRequisitionId);
  66. }
  67. /**
  68. * 获取指定的收款单
  69. */
  70. public function getReceiptRequisitionInfo($receiptRequisitionId)
  71. {
  72. $dbResult = $this->objDReceiptRequisition->get($receiptRequisitionId);
  73. if($dbResult === false){
  74. return ResultWrapper::fail($this->objDReceiptRequisition->error(), ErrorCode::$dberror);
  75. }
  76. if(empty($dbResult)){
  77. return ResultWrapper::success([]);
  78. }
  79. //渲染前缀
  80. $dbResult['no'] = StatusCode::$noPrefix[17] . '-' . $dbResult['no'];
  81. $dbResultAccount = $this->objDReceiptRequisitionAccount->select(['receiptRequisitionId'=>$receiptRequisitionId,'deleteStatus'=>StatusCode::$standard]);
  82. if($dbResultAccount === false){
  83. return ResultWrapper::fail($this->objDReceiptRequisitionAccount->error(), ErrorCode::$dberror);
  84. }
  85. if(!empty($dbResultAccount)){
  86. $dbResult['receiptRequisitionAccountDate'] = $dbResultAccount;
  87. }
  88. return ResultWrapper::success($dbResult);
  89. }
  90. /**
  91. * 编辑指定的收款单
  92. */
  93. public function editReceiptRequisition($params)
  94. {
  95. $updateReceiptRequisitionId = $params['id'];
  96. unset($params['id']);
  97. unset($params['createTime']);
  98. $receiptRequisitionAccountDate = $params['receiptRequisitionAccountDate'];
  99. unset($params['receiptRequisitionAccountDate']);
  100. //累计金额
  101. $params['totalCollectionAmount'] = 0;
  102. $params['totalPreferentialAmount'] = 0;
  103. $params['totalActualAmount'] = 0;
  104. foreach ($receiptRequisitionAccountDate as $account) {
  105. $account['preferentialAmount'] = !empty($account['preferentialAmount']) ? $account['preferentialAmount'] : 0;
  106. $params['totalCollectionAmount'] += $account['actualAmount'];
  107. $params['totalPreferentialAmount'] += $account['preferentialAmount'];
  108. $params['totalActualAmount'] += ($account['actualAmount'] - $account['preferentialAmount']);
  109. }
  110. //开启事务
  111. $beginTransactionStatus = $this->objDReceiptRequisition->beginTransaction();
  112. $receiptRequisitionId = $this->objDReceiptRequisition->update($params, $updateReceiptRequisitionId);
  113. if($receiptRequisitionId === false){
  114. $this->objDReceiptRequisition->rollBack();
  115. return ResultWrapper::fail($this->objDReceiptRequisition->error(), ErrorCode::$dberror);
  116. }
  117. foreach ($receiptRequisitionAccountDate as $key => $value){
  118. $id = getArrayItem($value, 'id', 0);
  119. if($id){
  120. $dbResult = $this->objDReceiptRequisitionAccount->update($value,['receiptRequisitionId'=>$id]);
  121. if ($dbResult === false) {
  122. $this->objDReceiptRequisition->rollBack();
  123. return ResultWrapper::fail($this->objDReceiptRequisitionAccount->error(), ErrorCode::$dberror);
  124. }
  125. }else{
  126. $value['receiptRequisitionId'] = $updateReceiptRequisitionId;
  127. $dbResult = $this->objDReceiptRequisitionAccount->insert($value);
  128. if ($dbResult === false) {
  129. $this->objDReceiptRequisition->rollBack();
  130. return ResultWrapper::fail($this->objDReceiptRequisitionAccount->error(), ErrorCode::$dberror);
  131. }
  132. }
  133. }
  134. if($beginTransactionStatus){
  135. $this->objDReceiptRequisition->commit();
  136. }
  137. return ResultWrapper::success($receiptRequisitionId);
  138. }
  139. /**
  140. * 删除指定的收款单
  141. */
  142. public function delReceiptRequisition($receiptRequisitionId)
  143. {
  144. if (empty($receiptRequisitionId)) {
  145. return ResultWrapper::fail('参数错误', ErrorCode::$paramError);
  146. }
  147. $beginTransactionStatus = $this->objDReceiptRequisition->beginTransaction();
  148. $dbResult = $this->objDReceiptRequisition->update( ['deleteStatus'=>StatusCode::$delete], $receiptRequisitionId);
  149. if($dbResult === false){
  150. $this->objDReceiptRequisition->rollBack();
  151. return ResultWrapper::fail($this->objDReceiptRequisition->error(), ErrorCode::$dberror);
  152. }
  153. unset($dbResult);
  154. $dbResult = $this->objDReceiptRequisitionAccount->update( ['deleteStatus'=>StatusCode::$delete], ['receiptRequisitionId'=>$receiptRequisitionId]);
  155. if($dbResult === false){
  156. $this->objDReceiptRequisition->rollBack();
  157. return ResultWrapper::fail($this->objDReceiptRequisitionAccount->error(), ErrorCode::$dberror);
  158. }
  159. if($beginTransactionStatus){
  160. $this->objDReceiptRequisition->commit();
  161. }
  162. return ResultWrapper::success($receiptRequisitionId);
  163. }
  164. /**
  165. * 获取所有的收款单
  166. */
  167. public function getAllReceiptRequisition($selectParams)
  168. {
  169. $limit = $selectParams['limit'];
  170. unset($selectParams['limit']);
  171. $offset = $selectParams['offset'];
  172. unset($selectParams['offset']);
  173. $selectParams['deleteStatus'] = StatusCode::$standard;
  174. $returnData = [
  175. 'data' => [],
  176. 'total' => 0,
  177. ];
  178. $whereSql = '';
  179. // if (isset($selectParams['operatorName']) && !empty($selectParams['operatorName'])) {
  180. // $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  181. // $whereSql .= $where . ' operatorName like "%' . $selectParams['operatorName'] . '%"';
  182. // }
  183. if (isset($selectParams['auditStatus']) && !empty($selectParams['auditStatus'])) {
  184. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  185. $whereSql .= $where . ' auditStatus = ' . $selectParams['auditStatus'];
  186. }
  187. if (isset($selectParams['custormerId']) && !empty($selectParams['custormerId'])) {
  188. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  189. $whereSql .= $where . ' customerId = ' . $selectParams['custormerId'];
  190. }
  191. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  192. $whereSql .= $where . 'deleteStatus = ' . StatusCode::$standard;
  193. $sql = 'SELECT * FROM ' .$this->objDReceiptRequisition->get_Table().$whereSql . ' ORDER BY createTime DESC LIMIT ' . $offset . ' , ' . $limit;
  194. $dbResult = $this->objDReceiptRequisition->query($sql);
  195. if ($dbResult === false) {
  196. return ResultWrapper::fail($this->objDReceiptRequisition->error(), ErrorCode::$dberror);
  197. }
  198. if(empty($dbResult)){
  199. return ResultWrapper::success($returnData);
  200. }
  201. //渲染前缀
  202. foreach ($dbResult as $key => $value){
  203. $dbResult[$key]['no'] = StatusCode::$noPrefix[17] . '-' . $value['no'];
  204. }
  205. $totalSql = 'SELECT COUNT(1) as count FROM ' .$this->objDReceiptRequisition->get_Table().$whereSql;
  206. $dbTotalResult = $this->objDReceiptRequisition->query($totalSql);
  207. if ($dbTotalResult === false) {
  208. return ResultWrapper::fail($this->objDReceiptRequisition->error(), ErrorCode::$dberror);
  209. }
  210. if(empty($dbTotalResult)){
  211. return ResultWrapper::success([]);
  212. }
  213. $return = [
  214. 'data' => $dbResult,
  215. 'total' => $dbTotalResult[0]['count']
  216. ];
  217. return ResultWrapper::success($return);
  218. }
  219. /**
  220. * 审核申请收款单
  221. */
  222. public function updateReceiptRequisitionStatus($params)
  223. {
  224. $ReceiptRequisitionData = $this->objDReceiptRequisition->get($params['id']);
  225. if ($ReceiptRequisitionData === false) {
  226. return ResultWrapper::fail($this->objDReceiptRequisition->error(), ErrorCode::$dberror);
  227. }
  228. if( empty($ReceiptRequisitionData) ){
  229. return ResultWrapper::fail('要审核的单据不存在', ErrorCode::$contentNotExists);
  230. }
  231. if( $ReceiptRequisitionData['auditStatus'] == StatusCode::$auditStatus['auditPass'] ){
  232. return ResultWrapper::fail('该单据已经审核过了', ErrorCode::$notAllowAccess);
  233. }
  234. //查询accountList信息
  235. $ReceiptRequisitionAccountList = $this->objDReceiptRequisitionAccount->select(['receiptRequisitionId'=>$params['id']]);
  236. //组装accountList
  237. $ReceiptRequisitionData['accountList']= [];
  238. foreach ($ReceiptRequisitionAccountList as $key => $value){
  239. $accountList = [
  240. 'accountId' => $value['accountId'],
  241. 'accountNumber'=>'',
  242. 'accountName'=> $value['accountName'],
  243. 'money'=> $value['collectionAmount'],
  244. 'discountMoney'=>$value['preferentialAmount'],
  245. 'finalMoney' => $value['actualAmount'],
  246. 'payWay'=> '',
  247. 'remark'=>$value['remark']
  248. ];
  249. array_push($ReceiptRequisitionData['accountList'],$accountList);
  250. }
  251. $beginTransactionStatus = $this->objDReceiptRequisition->beginTransaction();
  252. // 判断是否自动生成收款单 autotype
  253. if($params['autoType'] == StatusCode::$delete){//只审核单据,不生成收款单
  254. $dbResult = $this->objDReceiptRequisition->update(['auditStatus' => StatusCode::$auditStatus['auditPass']], $params['id']);
  255. }
  256. if($params['autoType'] == StatusCode::$standard){//自动生成
  257. //请求add收款单,生成新的收款单
  258. //生成收款单
  259. $objMReceived = new MReceived($this->enterpriseId, $this->userCenterId);
  260. //创建收款单
  261. $received = [
  262. 'currentAccountName' => $ReceiptRequisitionData['operatorName'],
  263. 'financeType' => '销售收款',
  264. 'financeTypeId' => 2,
  265. 'shopId' => $ReceiptRequisitionData['shopId'],
  266. 'shopName' => $ReceiptRequisitionData['shopName'],
  267. 'receiptTime' => time(),
  268. 'operatorId' => $this->userCenterId,
  269. 'accountList' => $ReceiptRequisitionData['accountList'],
  270. 'customerId' => $ReceiptRequisitionData['customerId'],
  271. 'customerName' => $ReceiptRequisitionData['customerName'],
  272. 'sourceNo' => $ReceiptRequisitionData['no'],
  273. 'sourceNoMoney' => $ReceiptRequisitionData['totalActualAmount'],
  274. 'createTime' => time(),
  275. 'auditStatus' => StatusCode::$auditStatus['auditing'],
  276. 'updateTime' => time(),
  277. 'receiptOffsetData' => []
  278. ];
  279. $receivedResult = $objMReceived->addReceived($received, true);
  280. if (!$receivedResult->isSuccess()) {
  281. $this->objDReceiptRequisition->rollBack();
  282. return ResultWrapper::fail($receivedResult->getData(), $receivedResult->getErrorCode());
  283. }
  284. $dbResult = $this->objDReceiptRequisition->update(['auditStatus' => StatusCode::$auditStatus['auditPass']], $params['id']);
  285. //走更新接口
  286. $updateReceived = $objMReceived->updateReceivedStatus(['id'=>$receivedResult->getData(),'createTime'=>time()]);
  287. if (!$updateReceived->isSuccess()) {
  288. $this->objDReceiptRequisition->rollBack();
  289. return ResultWrapper::fail($updateReceived->getData(), $updateReceived->getErrorCode());
  290. }
  291. }
  292. if ($dbResult === false) {
  293. $this->objDReceiptRequisition->rollBack();
  294. return ResultWrapper::fail($this->objDReceiptRequisition->error(), ErrorCode::$dberror);
  295. }
  296. if ($beginTransactionStatus){
  297. $this->objDReceiptRequisition->commit();
  298. }
  299. return ResultWrapper::success($dbResult);
  300. }
  301. }