MMemberBalanceDetail.Class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Gss
  5. * Date: 2021/3/26 0026
  6. * Time: 10:52
  7. */
  8. namespace JinDouYun\Model\Customer;
  9. use Mall\Framework\Core\ErrorCode;
  10. use Mall\Framework\Core\StatusCode;
  11. use Mall\Framework\Core\ResultWrapper;
  12. use Jindouyun\Cache\CustomerCache;
  13. use JinDouYun\Model\Finance\MAccount;
  14. use JinDouYun\Model\Finance\MAccountDetail;
  15. use JinDouYun\Dao\Customer\DMemberBalanceDetail;
  16. use JinDouYun\Dao\Customer\DCustomer;
  17. use JinDouYun\Dao\Order\DOrderIndex;
  18. class MMemberBalanceDetail
  19. {
  20. private $enterpriseId;
  21. private $userCenterId;
  22. private $objDMemberBalanceDetail;
  23. private $objDCustomer;
  24. private $objDOrderIndex;
  25. private $cutTable = 1;//客户按照企业id分表
  26. public function __construct($enterpriseId, $userCenterId)
  27. {
  28. $this->userCenterId = $userCenterId;
  29. $this->enterpriseId = $enterpriseId;
  30. $this->objDMemberBalanceDetail = new DMemberBalanceDetail('default');
  31. $this->objDCustomer = new DCustomer('default');
  32. $tableName = $this->objDCustomer->getTableName($this->objDCustomer->get_Table(), $this->enterpriseId, $this->cutTable);
  33. $this->objDCustomer->setTable($tableName);
  34. $this->objDMemberBalanceDetail->setTable('qianniao_member_balance_detail_' . $enterpriseId);
  35. $this->objDOrderIndex = new DOrderIndex();
  36. $this->objDOrderIndex->setTable('qianniao_order_index_' . $enterpriseId);
  37. }
  38. /**
  39. * 新增会员明细
  40. * $isbackstage后台页面充值
  41. */
  42. public function addMemberBalanceDetail($params,$isbackstage=false)
  43. {
  44. $financeType = getArrayItem($params, 'financeType', '');
  45. unset($params['financeType']);
  46. //查询当前会员的余额
  47. $customerData = $this->objDCustomer->get($params['customerId']);
  48. if ($customerData === false) {
  49. return ResultWrapper::fail($this->objDCustomer->error(), ErrorCode::$dberror);
  50. }
  51. if( empty($customerData) ){
  52. return ResultWrapper::fail('充值的客户不存在', ErrorCode::$contentNotExists);
  53. }
  54. $memberBalance = $customerData['memberBalance'];
  55. if ( bccomp($memberBalance, 0.00,2) < 0 ){
  56. return ResultWrapper::fail('客户余额不足!', ErrorCode::$notAllowAccess);
  57. }
  58. //计算变动后金额
  59. if($params['type'] == 5){
  60. $params['afterMoney'] = bcadd($memberBalance,$params['money'],2);
  61. }else{
  62. $params['afterMoney'] = bcsub($memberBalance,$params['money'],2);
  63. if ( bccomp($params['afterMoney'], 0.00, 2) < 0 ){
  64. return ResultWrapper::fail('余额不足,请切换其它支付方式', ErrorCode::$notAllowAccess);
  65. }
  66. }
  67. $params['beforeMoney'] = $memberBalance;
  68. $params['userCenterId'] = $customerData['userCenterId'];
  69. $beginTransactionStatus = $this->objDMemberBalanceDetail->beginTransaction();
  70. $memberBalanceDetailDate = $this->objDMemberBalanceDetail->insert($params);
  71. if ($memberBalanceDetailDate === false) {
  72. $this->objDMemberBalanceDetail->rollBack();
  73. return ResultWrapper::fail($this->objDMemberBalanceDetail->error(), ErrorCode::$dberror);
  74. }
  75. // 更新客户表会员余额
  76. $dbResult = $this->objDCustomer->update(['memberBalance'=>$params['afterMoney']],['id'=>$params['customerId']]);
  77. if ($dbResult === false) {
  78. $this->objDMemberBalanceDetail->rollBack();
  79. return ResultWrapper::fail($this->objDCustomer->error(), ErrorCode::$dberror);
  80. }
  81. $objCustomerCache = new CustomerCache();
  82. $objCustomerCache->delCustomerData($this->enterpriseId, $params['customerId']);
  83. $objCustomerCache->delCustomerUserData($this->enterpriseId, $customerData['userCenterId']);
  84. // 获取余额账户信息
  85. $objMAccount = new MAccount($this->enterpriseId, $this->userCenterId);
  86. $accountResult = $objMAccount->getDefaultAccount(StatusCode::$payType['balance']);
  87. if (!$accountResult->isSuccess()) {
  88. $this->objDMemberBalanceDetail->rollBack();
  89. return ResultWrapper::fail($accountResult->getData(), $accountResult->getErrorCode());
  90. }
  91. $accountInfo = $accountResult->getData();
  92. if (empty($accountInfo)){
  93. if($beginTransactionStatus){
  94. $this->objDMemberBalanceDetail->commit();
  95. }
  96. return ResultWrapper::success($memberBalanceDetailDate);
  97. }
  98. // 添加财务账户明细
  99. $accountDetail = [
  100. 'accountId' => $accountInfo['id'],
  101. 'accountCode' => $accountInfo['accountCode'],
  102. 'accountName' => $accountInfo['name'],
  103. 'accountNumber' => $accountInfo['accountNumber'],
  104. 'sourceNo' => $memberBalanceDetailDate,
  105. 'sourceId' => $memberBalanceDetailDate,
  106. 'financeType' => $financeType,
  107. 'beginBalance' => $accountInfo['money'],
  108. 'shopId' => 0,
  109. 'shopName' => '平台',
  110. 'income' => $params['type'] == StatusCode::$delete ? $params['money'] : 0, // 扣减 = 账户收入
  111. 'expend' => $params['type'] == StatusCode::$standard ? $params['money'] : 0, // 充值 = 账户支出
  112. 'endBalance' => $accountInfo['money'] + (($params['type'] == StatusCode::$standard) ? -1* $params['money'] : $params['money']),
  113. 'contactUnit' => '客户名称:' . $customerData['name'],
  114. 'supplierId' => 0,
  115. 'customerId' => $customerData['id'],
  116. 'operatorId' => $this->userCenterId,
  117. 'receiveOrPayPerson' => $customerData['name'],
  118. 'remark' => '会员余额充值',
  119. 'createTime' => time(),
  120. 'updateTime' => time(),
  121. ];
  122. $objMAccountDetail = new MAccountDetail($this->enterpriseId, $this->userCenterId);
  123. $result = $objMAccountDetail->addAccountDetail($accountDetail);
  124. if (!$result->isSuccess()) {
  125. $this->objDMemberBalanceDetail->rollBack();
  126. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  127. }
  128. $result = $objMAccount->updateMoney($accountInfo['id'], (($params['type'] == StatusCode::$standard) ? -1* $params['money'] : $params['money']));
  129. if (!$result->isSuccess()) {
  130. $this->objDMemberBalanceDetail->rollBack();
  131. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  132. }
  133. unset($result);
  134. //为了体现会员充值的钱,新增可选账户,充值到那个账户下
  135. if($isbackstage){
  136. $financeAccountResult = $objMAccount->getAccountInfo($params['accountId']);
  137. if (!$financeAccountResult->isSuccess()) {
  138. $this->objDMemberBalanceDetail->rollBack();
  139. return ResultWrapper::fail($financeAccountResult->getData(), $financeAccountResult->getErrorCode());
  140. }
  141. $financeAccountInfo = $financeAccountResult->getData();
  142. if (empty($financeAccountInfo)){
  143. if($beginTransactionStatus){
  144. $this->objDMemberBalanceDetail->commit();
  145. }
  146. return ResultWrapper::success($memberBalanceDetailDate);
  147. }
  148. // 添加财务账户明细
  149. $financeAccountDetail = [
  150. 'accountId' => $financeAccountInfo['id'],
  151. 'accountCode' => $financeAccountInfo['accountCode'],
  152. 'accountName' => $financeAccountInfo['name'],
  153. 'accountNumber' => $financeAccountInfo['accountNumber'],
  154. 'sourceNo' => $memberBalanceDetailDate,
  155. 'sourceId' => $memberBalanceDetailDate,
  156. 'financeType' => $financeType,
  157. 'beginBalance' => $financeAccountInfo['money'],
  158. 'shopId' => 0,
  159. 'shopName' => '平台',
  160. 'income' => $params['type'] == StatusCode::$standard ? $params['money'] : 0, // 扣减 = 账户收入
  161. 'expend' => $params['type'] == StatusCode::$delete ? $params['money'] : 0, // 充值 = 账户支出
  162. 'endBalance' => $financeAccountInfo['money'] + (($params['type'] == StatusCode::$delete) ? -1* $params['money'] : $params['money']),
  163. 'contactUnit' => '客户名称:' . $customerData['name'],
  164. 'supplierId' => 0,
  165. 'customerId' => $customerData['id'],
  166. 'operatorId' => $this->userCenterId,
  167. 'receiveOrPayPerson' => $customerData['name'],
  168. 'remark' => '会员余额充值',
  169. 'createTime' => time(),
  170. 'updateTime' => time(),
  171. ];
  172. $result = $objMAccountDetail->addAccountDetail($financeAccountDetail);
  173. if (!$result->isSuccess()) {
  174. $this->objDMemberBalanceDetail->rollBack();
  175. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  176. }
  177. $result = $objMAccount->updateMoney($financeAccountInfo['id'], (($params['type'] == StatusCode::$delete) ? -1* $params['money'] : $params['money']));
  178. if (!$result->isSuccess()) {
  179. $this->objDMemberBalanceDetail->rollBack();
  180. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  181. }
  182. }
  183. if($beginTransactionStatus){
  184. $this->objDMemberBalanceDetail->commit();
  185. }
  186. return ResultWrapper::success($memberBalanceDetailDate);
  187. }
  188. /**
  189. * 获取所有的明细
  190. */
  191. public function getAllMemberBalanceDetail($selectParams)
  192. {
  193. $limit = $selectParams['limit'];
  194. unset($selectParams['limit']);
  195. $offset = $selectParams['offset'];
  196. unset($selectParams['offset']);
  197. $whereSql = '';
  198. if (isset($selectParams['userCenterId']) && !empty($selectParams['userCenterId'])) {
  199. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  200. $whereSql .= $where . ' userCenterId = ' . $selectParams['userCenterId'];
  201. }
  202. if (isset($selectParams['customerId']) && !empty($selectParams['customerId'])) {
  203. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  204. $whereSql .= $where . ' customerId = ' . $selectParams['customerId'];
  205. }
  206. if ( (isset($selectParams['start']) && !empty($selectParams['start']))&&(isset($selectParams['end']) && !empty($selectParams['end'])) ) {
  207. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  208. $whereSql .= $where . ' createTime BETWEEN ' . $selectParams['start'] . ' AND '. $selectParams['end'];
  209. }
  210. $returnData = [
  211. 'data' => [],
  212. 'total' => 0,
  213. ];
  214. $sql = 'SELECT * FROM ' .$this->objDMemberBalanceDetail->get_Table().$whereSql . ' ORDER BY createTime DESC LIMIT ' . $offset . ' , ' . $limit;
  215. $dbResult = $this->objDMemberBalanceDetail->query($sql);
  216. if ($dbResult === false) {
  217. return ResultWrapper::fail($this->objDMemberBalanceDetail->error(), ErrorCode::$dberror);
  218. }
  219. foreach ($dbResult as $key => $value){
  220. if( !empty($value['orderIds']) ){
  221. $orderIds = explode(',',$value['orderIds']);
  222. $orderResult = $this->objDOrderIndex->select(['id'=>$orderIds]);
  223. if ($orderResult === false) {
  224. return ResultWrapper::fail($this->objDOrderIndex->error(), ErrorCode::$dberror);
  225. }
  226. if(!empty($orderResult)){
  227. foreach ($orderResult as $k => $v){
  228. $orderResult[$k]['no'] = StatusCode::$noPrefix[1].'-'.$v['no'];
  229. }
  230. $dbResult[$key]['orderIdDate'] = $orderResult;
  231. }
  232. }
  233. }
  234. $totalSql = 'SELECT COUNT(1) as count FROM ' .$this->objDMemberBalanceDetail->get_Table().$whereSql;
  235. $dbTotalResult = $this->objDMemberBalanceDetail->query($totalSql);
  236. $return = [
  237. 'data' => $dbResult,
  238. 'total' => $dbTotalResult[0]['count'],
  239. ];
  240. return ResultWrapper::success($return);
  241. }
  242. public function create($customerId, $userCenterId, $type, $before, $money, $after, $purpose = '', $remark = '')
  243. {
  244. $data = [
  245. 'customerId' =>$customerId,
  246. 'userCenterId' => $userCenterId,
  247. 'type' => $type,
  248. 'beforeMoney' => $before,
  249. 'money' => $money,
  250. 'afterMoney' => $after,
  251. 'remark' => $remark,
  252. 'purpose' => $purpose,
  253. ];
  254. $this->objDCustomer->update(['memberBalance' => $after], ['id' => $customerId]);
  255. $this->objDMemberBalanceDetail->insert($data);
  256. }
  257. }