MAccountTransfer.Class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /**
  3. * 资金转账管理模块
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/10/30
  7. * Time: 14:02
  8. */
  9. namespace JinDouYun\Model\Finance;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\StatusCode;
  12. use Mall\Framework\Core\ResultWrapper;
  13. use JinDouYun\Model\MBaseModel;
  14. use JinDouYun\Dao\Finance\DAccountTransfer;
  15. use JinDouYun\Model\Finance\MAccount;
  16. use JinDouYun\Model\Finance\MAccountDetail;
  17. class MAccountTransfer extends MBaseModel {
  18. private $objDAccountTransfer;
  19. private $objMAccount;
  20. private $objMAccountDetail;
  21. private $enterpriseId;
  22. private $userCenterId;
  23. public function __construct($enterpriseId, $userCenterId)
  24. {
  25. $this->userCenterId = $userCenterId;
  26. $this->enterpriseId = $enterpriseId;
  27. parent::__construct($enterpriseId, $userCenterId);
  28. $this->objDAccountTransfer = new DAccountTransfer('finance');
  29. $this->objDAccountTransfer->setTable('qianniao_account_transfer_' . $enterpriseId);
  30. $this->objMAccount = new MAccount($enterpriseId, $userCenterId);
  31. $this->objMAccountDetail = new MAccountDetail($enterpriseId, $userCenterId);
  32. }
  33. /**
  34. * 添加资金转账
  35. *
  36. * @param array $params 资金转账数据
  37. *
  38. * @return ResultWrapper
  39. */
  40. public function addAccountTransfer($params)
  41. {
  42. //判断是否可转出
  43. $records = json_decode($params['records'],true);
  44. foreach ($records as $record) {
  45. $result = self::checkTransferLegal($record['outAccountId'], $record['money']);
  46. if($result->isSuccess() == false) {
  47. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  48. break;
  49. }
  50. }
  51. $params['no'] = createOrderSn(StatusCode::$source['manage'], StatusCode::$orderType['accountTransfer'], $this->userCenterId);
  52. $AccountTransferId = $this->objDAccountTransfer->insert($params);
  53. if($AccountTransferId === false){
  54. return ResultWrapper::fail($this->objDAccountTransfer->error(), ErrorCode::$dberror);
  55. }else{
  56. return ResultWrapper::success($AccountTransferId);
  57. }
  58. }
  59. /**
  60. * 获取指定资金转账信息
  61. * @param $AccountTransferIds
  62. * @return ResultWrapper
  63. */
  64. public function getAccountTransferInfo($AccountTransferIds)
  65. {
  66. $dbResult = $this->objDAccountTransfer->get($AccountTransferIds);
  67. if($dbResult === false){
  68. return ResultWrapper::fail($this->objDAccountTransfer->error(), ErrorCode::$dberror);
  69. }else{
  70. $dbResult = self::format([$dbResult]);
  71. return ResultWrapper::success(array_shift($dbResult));
  72. }
  73. }
  74. public function format($data) {
  75. foreach ($data as $key => $value) {
  76. $data[$key]['records'] = json_decode($data[$key]['records'], true);
  77. }
  78. return $data;
  79. }
  80. /**
  81. * 编辑资金转账
  82. *
  83. * @param int|array $params 修改资金转账的数据
  84. *
  85. * @return ResultWrapper
  86. */
  87. public function editAccountTransfer($params)
  88. {
  89. if( empty($params['id']) ){
  90. return ResultWrapper::fail('没有指定要修改的AccountTransferid', ErrorCode::$paramError);
  91. }
  92. //判断是否可转出
  93. $records = json_decode($params['records'],true);
  94. foreach ($records as $record) {
  95. $result = self::checkTransferLegal($record['outAccountId'], $record['money']);
  96. if($result->isSuccess() == false) {
  97. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  98. break;
  99. }
  100. }
  101. $updateAccountTransferId = $params['id'];
  102. unset($params['id']);
  103. $dbResult = $this->objDAccountTransfer->update($params, $updateAccountTransferId);
  104. if($dbResult === false){
  105. return ResultWrapper::fail($this->objDAccountTransfer->error(), ErrorCode::$dberror);
  106. }else{
  107. return ResultWrapper::success($dbResult);
  108. }
  109. }
  110. //判断转出账户金额是否大于转出金额
  111. public function checkTransferLegal($accountId, $transferMoney) {
  112. if(!$transferMoney) {
  113. return ResultWrapper::fail('转出金额必须大于0', ErrorCode::$paramError);
  114. }
  115. $accountResult = $this->objMAccount->getAccountInfo($accountId);
  116. if($accountResult->isSuccess() == false) {
  117. return ResultWrapper::fail($accountResult->getData(), $accountResult->getErrorCode());
  118. }
  119. $account = $accountResult->getData();
  120. if($account['money'] < $transferMoney) {
  121. return ResultWrapper::fail('账户金额不足', ErrorCode::$paramError);
  122. }
  123. return ResultWrapper::success('可正常转出');
  124. }
  125. /**
  126. * 资金转账审核
  127. * @param array $id
  128. * @return ResultWrapper
  129. */
  130. public function updateAccountTransferStatus($id)
  131. {
  132. $transferResult = $this->objDAccountTransfer->get($id);
  133. if(isset($transferResult['auditStatus']) && $transferResult['auditStatus'] == StatusCode::$auditStatus['auditPass']) {
  134. return ResultWrapper::fail('单据已通过审核,请勿重复操作', ErrorCode::$actionIsDo);
  135. }
  136. $this->objDAccountTransfer->beginTransaction();
  137. $dbResult = $this->objDAccountTransfer->update( ['auditStatus'=>StatusCode::$auditStatus['auditPass']], $id );
  138. if($dbResult === false){
  139. $this->objDAccountTransfer->rollBack();
  140. return ResultWrapper::fail($this->objDAccountTransfer->error(), ErrorCode::$dberror);
  141. }
  142. //循环records,给转出账户减去一定数值,给转入的加数值 添加账户明细
  143. $records = json_decode($transferResult['records'], true);
  144. foreach ($records as $record) {
  145. $result = self::dealAccountDetail($record['inAccountId'], $transferResult, $record);
  146. if($result->isSuccess() == false) {
  147. $this->objDAccountTransfer->rollBack();
  148. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  149. }
  150. $result = $this->objMAccount->updateMoney($record['inAccountId'], $record['money']);
  151. if($result->isSuccess() == false) {
  152. $this->objDAccountTransfer->rollBack();
  153. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  154. }
  155. $result = self::dealAccountDetail($record['outAccountId'], $transferResult, $record, false);
  156. if($result->isSuccess() == false) {
  157. $this->objDAccountTransfer->rollBack();
  158. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  159. }
  160. $result = $this->objMAccount->updateMoney($record['outAccountId'], '-'.$record['money']);
  161. if($result->isSuccess() == false) {
  162. $this->objDAccountTransfer->rollBack();
  163. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  164. }
  165. }
  166. $this->objDAccountTransfer->commit();
  167. return ResultWrapper::success($dbResult);
  168. }
  169. /**
  170. * 账户明细
  171. * @param $accountId
  172. * @param $transferData
  173. * @param $record
  174. * @param $in
  175. * @return ResultWrapper
  176. * @throws \Exception
  177. */
  178. public function dealAccountDetail($accountId, $transferData, $record, $in =true) {
  179. //获取账户信息
  180. $accountResult = $this->objMAccount->getAccountInfo($accountId);
  181. if($accountResult->isSuccess() == false) {
  182. return ResultWrapper::fail($accountResult->getData(), $accountResult->getErrorCode());
  183. }
  184. $accountInfo = $accountResult->getData();
  185. //账户明细
  186. $accountDetail = [
  187. 'accountId'=> $accountInfo['id'],
  188. 'accountCode'=> $accountInfo['accountCode'],
  189. 'accountName'=> $accountInfo['name'],
  190. 'accountNumber'=> $accountInfo['accountNumber'],
  191. 'sourceNo' =>$transferData['no'],
  192. 'sourceId' => $transferData['id'],
  193. 'financeType'=>'资金转账',
  194. 'beginBalance'=>$accountInfo['money'],
  195. 'shopId'=>$transferData['shopId'],
  196. 'shopName'=>$transferData['shopName'],
  197. 'income'=> $in ? $record['money'] : 0,
  198. 'expend'=> $in ? 0 : $record['money'],
  199. 'endBalance'=> $in ? ($accountInfo['money'] + $record['money']) : ($accountInfo['money'] - $record['money']),
  200. 'contactUnit'=>'资金转账',
  201. 'supplierId'=>0,
  202. 'customerId'=>0,
  203. 'operatorId'=>$this->userCenterId,
  204. 'receiveOrPayPerson'=>'',
  205. 'remark'=>$in ? "资金转账:转入".$record['money']."元" : "资金转账:转出".$record['money']."元",
  206. 'createTime'=>time(),
  207. 'updateTime'=>time(),
  208. ];
  209. $result = $this->objMAccountDetail->addAccountDetail($accountDetail);
  210. if ($result->isSuccess() === false) {
  211. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  212. }
  213. return ResultWrapper::success($result);
  214. }
  215. /**
  216. * 获取所有资金转账数据
  217. *
  218. * @param array $selectParams 过滤条件
  219. *
  220. * @return ResultWrapper
  221. */
  222. public function getAllAccountTransfer($selectParams)
  223. {
  224. $limit = $selectParams['limit'];
  225. unset($selectParams['limit']);
  226. $offset = $selectParams['offset'];
  227. unset($selectParams['offset']);
  228. $where = '';
  229. if(!empty($selectParams['no'])) {
  230. $where .= 'no='. $selectParams['no'];
  231. }
  232. if(!empty($selectParams['start'])) {
  233. if(!empty($where)) {
  234. $where .= ' AND ';
  235. }
  236. $where .= 'createTime >= '. $selectParams['start'];
  237. }
  238. if(!empty($selectParams['end'])) {
  239. if(!empty($where)) {
  240. $where .= ' AND ';
  241. }
  242. $where .= 'createTime <= '. $selectParams['end'];
  243. }
  244. if(!empty($selectParams['outAccountId'])) {
  245. if(!empty($where)) {
  246. $where .= ' AND ';
  247. }
  248. $where .= "JSON_CONTAINS(records->'$[*].outAccountId', '".$selectParams['outAccountId']."', '$') = 1";
  249. }
  250. if(!empty($selectParams['inAccountId'])) {
  251. if(!empty($where)) {
  252. $where .= ' AND ';
  253. }
  254. $where .= "JSON_CONTAINS(records->'$[*].inAccountId', '".$selectParams['inAccountId']."', '$') = 1";
  255. }
  256. $dbResult = $this->objDAccountTransfer->select($where, '*', 'createTime desc', $limit, $offset);
  257. if($dbResult === false){
  258. return ResultWrapper::fail($this->objDAccountTransfer->error(), ErrorCode::$dberror);
  259. }
  260. $total = $this->objDAccountTransfer->count($selectParams);
  261. $return = [
  262. 'data' => self::format($dbResult),
  263. 'total' => ($total)?intval($total):0,
  264. ];
  265. return ResultWrapper::success($return);
  266. }
  267. }