MerchantReconciliationRepository.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\store\order;
  12. use app\common\model\store\order\MerchantReconciliationOrder;
  13. use app\common\model\system\merchant\Merchant;
  14. use app\common\repositories\BaseRepository;
  15. use app\common\dao\store\order\MerchantReconciliationDao as dao;
  16. use app\common\repositories\store\order\StoreOrderRepository;
  17. use app\common\repositories\store\order\StoreRefundOrderRepository;
  18. use app\common\repositories\system\admin\AdminRepository;
  19. use app\common\repositories\system\merchant\FinancialRecordRepository;
  20. use app\common\repositories\system\merchant\MerchantRepository;
  21. use crmeb\services\SwooleTaskService;
  22. use think\exception\ValidateException;
  23. use think\facade\Db;
  24. use FormBuilder\Factory\Elm;
  25. use think\facade\Route;
  26. class MerchantReconciliationRepository extends BaseRepository
  27. {
  28. public function __construct(dao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. public function getWhereCountById($id)
  33. {
  34. $where = ['reconciliation_id' => $id,'status' => 2];
  35. return $this->dao->getWhereCount($where) > 0 ;
  36. }
  37. public function merWhereCountById($id,$merId)
  38. {
  39. $where = ['reconciliation_id' => $id,'mer_id' => $merId,'is_accounts' => 0,'status' => 0];
  40. return ($this->dao->getWhereCount($where) > 0) ;
  41. }
  42. /**
  43. * TODO 列表
  44. * @param $where
  45. * @param $page
  46. * @param $limit
  47. * @return array
  48. * @author Qinii
  49. * @day 2020-06-15
  50. */
  51. public function getList($where,$page,$limit)
  52. {
  53. $query = $this->dao->search($where)->with([
  54. 'merchant' => function($query){
  55. $query->field('mer_id,mer_name');
  56. },
  57. 'admin' =>function($query){
  58. $query->field('admin_id,real_name');
  59. }]);
  60. $count = $query->count();
  61. $list = $query->page($page,$limit)->select()->append(['charge'])->each(function($item){
  62. if($item->type == 1) return $item->price = '-'.$item->price;
  63. });
  64. return compact('count','list');
  65. }
  66. /**
  67. * TODO 创建对账单
  68. * @param $id
  69. * @param $data
  70. * @author Qinii
  71. * @day 2020-06-15
  72. */
  73. public function create(int $id, array $data)
  74. {
  75. $orderMake = app()->make(StoreOrderRepository::class);
  76. $refundMake = app()->make(StoreRefundOrderRepository::class);
  77. $bank = merchantConfig($id,'bank');
  78. $bank_name = merchantConfig($id,'bank_name');
  79. $bank_number = merchantConfig($id,'bank_number');
  80. $bank_address = merchantConfig($id,'bank_address');
  81. if( !$bank || !$bank_name || !$bank_number || !$bank_address )
  82. throw new ValidateException('商户未填写银行卡信息');
  83. $order_ids = $data['order_ids'];
  84. $refund_order_ids = $data['refund_order_ids'];
  85. if($data['order_type']) { //全选
  86. $order_ids = $orderMake->search([
  87. 'date' => $data['date'],
  88. 'mer_id' => $id,
  89. 'reconciliation_type' => 0,
  90. 'paid' => 1,
  91. 'order_id'
  92. ], null
  93. )->whereNotIn('order_id', $data['order_out_ids'])->column('order_id');
  94. }
  95. if($data['refund_type']){ //全选
  96. $refund_order_ids = $refundMake->search([
  97. 'date' => $data['date'],
  98. 'mer_id' => $id,
  99. 'reconciliation_type' => 0,
  100. 'status' => 3
  101. ])->whereNotIn('refund_order_id',$data['refund_out_ids'])->column('refund_order_id');
  102. }
  103. if(is_array($order_ids) && (count($order_ids) < 1) && is_array($refund_order_ids) && (count($refund_order_ids) < 1)){
  104. throw new ValidateException('没有数据可对账');
  105. }
  106. $compute = $this->compute($id,$order_ids,$refund_order_ids);
  107. $createData = [
  108. 'status' => 0,
  109. 'mer_id' => $id,
  110. 'is_accounts' => 0,
  111. 'mer_admin_id' => 0,
  112. 'bank' => $bank,
  113. 'bank_name' => $bank_name,
  114. 'bank_number' => $bank_number,
  115. 'bank_address' => $bank_address,
  116. 'admin_id' => $data['adminId'],
  117. 'price' => round($compute['price'],2),
  118. 'order_price' => round($compute['order_price'],2),
  119. 'refund_price' => round($compute['refund_price'],2),
  120. //'refund_rate' => round($compute['refund_rate'],2),
  121. 'order_rate' => round($compute['order_rate'],2),
  122. 'order_extension' => round($compute['order_extension'],2),
  123. 'refund_extension' => round($compute['refund_extension'],2),
  124. ];
  125. Db::transaction(function()use($order_ids,$refund_order_ids,$orderMake,$refundMake,$createData){
  126. $res = $this->dao->create($createData);
  127. $orderMake->updates($order_ids,['reconciliation_id' => $res['reconciliation_id']]);
  128. $refundMake->updates($refund_order_ids,['reconciliation_id' => $res['reconciliation_id']]);
  129. $this->reconciliationOrder($order_ids,$refund_order_ids,$res['reconciliation_id']);
  130. SwooleTaskService::merchant('notice', [
  131. 'type' => 'accoubts',
  132. 'data'=>[
  133. 'title' => '新对账',
  134. 'message' => '您有一条新的对账单',
  135. 'id' => $res['reconciliation_id']
  136. ]
  137. ], $createData['mer_id']);
  138. });
  139. }
  140. /**
  141. * TODO 计算对账单金额
  142. * @param $merId
  143. * @param $order_ids
  144. * @param $refund_order_ids
  145. * @return array
  146. * @author Qinii
  147. * @day 2020-06-23
  148. */
  149. public function compute($merId,$order_ids,$refund_order_ids)
  150. {
  151. $order_price = $refund_price = $order_extension = $refund_extension = $order_rate = $refund_rate =0;
  152. $orderMake = app()->make(StoreOrderRepository::class);
  153. $refundMake = app()->make(StoreRefundOrderRepository::class);
  154. foreach($order_ids as $item){
  155. if(!$order = $orderMake->getWhere(['order_id' => $item,'mer_id' => $merId,'paid' => 1]))
  156. throw new ValidateException('订单信息不存在或状态错误');
  157. if($order['reconciliation_id']) throw new ValidateException('订单重复提交');
  158. //(实付金额 - 一级佣金 - 二级佣金) * 抽成
  159. $commission_rate = ($order['commission_rate'] / 100);
  160. //佣金
  161. $_order_extension = bcadd($order['extension_one'],$order['extension_two'],3);
  162. $order_extension = bcadd($order_extension,$_order_extension,3);
  163. //手续费 = (实付金额 - 一级佣金 - 二级佣金) * 比例
  164. $_order_rate = bcmul(bcsub($order['pay_price'],$_order_extension,3),$commission_rate,3);
  165. $order_rate = bcadd($order_rate,$_order_rate,3);
  166. //金额
  167. $_order_price = bcsub(bcsub($order['pay_price'],$_order_extension,3),$_order_rate,3);
  168. $order_price = bcadd($order_price,$_order_price,3);
  169. }
  170. foreach($refund_order_ids as $item){
  171. if(!$refundOrder = $refundMake->getWhere(['refund_order_id' => $item,'mer_id' => $merId,'status' => 3],'*',['order']))
  172. throw new ValidateException('退款订单信息不存在或状态错误');
  173. if($refundOrder['reconciliation_id']) throw new ValidateException('退款订单重复提交');
  174. //退款金额 + 一级佣金 + 二级佣金
  175. $refund_commission_rate = ($refundOrder['order']['commission_rate'] / 100);
  176. //佣金
  177. $_refund_extension = bcadd($refundOrder['extension_one'],$refundOrder['extension_two'],3);
  178. $refund_extension = bcadd($refund_extension,$_refund_extension,3);
  179. //手续费
  180. // $_refund_rate = bcmul(bcsub($refundOrder['refund_price'],$_refund_extension,3),$refund_commission_rate,3);
  181. // $refund_rate = bcadd($refund_rate,$_refund_rate,3);
  182. //金额
  183. $_refund_price = bcadd($refundOrder['refund_price'],$_refund_extension,3);
  184. $refund_price = bcadd($refund_price,$_refund_price,3);
  185. }
  186. $price = bcsub($order_price,$refund_price,3);
  187. return compact('price','refund_price','order_extension','refund_extension','order_price','order_rate');
  188. }
  189. /**
  190. * TODO
  191. * @param $order_ids
  192. * @param $refund_ids
  193. * @param $reconciliation_id
  194. * @return mixed
  195. * @author Qinii
  196. * @day 2020-06-23
  197. */
  198. public function reconciliationOrder($order_ids,$refund_ids,$reconciliation_id)
  199. {
  200. $data = [];
  201. foreach ($order_ids as $item){
  202. $data[] = [
  203. 'order_id' => $item,
  204. 'reconciliation_id' => $reconciliation_id,
  205. 'type' => 0,
  206. ];
  207. }
  208. foreach ($refund_ids as $item) {
  209. $data[] = [
  210. 'order_id' => $item,
  211. 'reconciliation_id' => $reconciliation_id,
  212. 'type' => 1,
  213. ];
  214. }
  215. return app()->make(MerchantReconciliationOrderRepository::class)->insertAll($data);
  216. }
  217. /**
  218. * TODO 修改状态
  219. * @param $id
  220. * @param $data
  221. * @param $type
  222. * @author Qinii
  223. * @day 2020-06-15
  224. */
  225. public function switchStatus($id,$data)
  226. {
  227. Db::transaction(function()use($id,$data){
  228. if(isset($data['status']) && $data['status'] == 1){
  229. app()->make(StoreRefundOrderRepository::class)->reconciliationUpdate($id);
  230. app()->make(StoreOrderRepository::class)->reconciliationUpdate($id);
  231. }
  232. $this->dao->update($id,$data);
  233. });
  234. $res = $this->dao->get($id);
  235. $mer = app()->make(MerchantRepository::class)->get($res['mer_id']);
  236. if(isset($data['is_accounts']) && $data['is_accounts']){
  237. // $make = app()->make(FinancialRecordRepository::class);
  238. //
  239. // $make->dec([
  240. // 'order_id' => $id,
  241. // 'order_sn' => $id,
  242. // 'user_info' => $mer['mer_name'],
  243. // 'user_id' => $res['mer_id'],
  244. // 'financial_type' => 'sys_accoubts',
  245. // 'number' => $res->price,
  246. // ],0);
  247. //
  248. // $make->inc([
  249. // 'order_id' => $id,
  250. // 'order_sn' => $id,
  251. // 'user_info' => '总平台',
  252. // 'user_id' => 0,
  253. // 'financial_type' => 'mer_accoubts',
  254. // 'number' => $res->price,
  255. // ],$res->mer_id);
  256. SwooleTaskService::merchant('notice', [
  257. 'type' => 'accoubts',
  258. 'data'=>[
  259. 'title' => '新对账打款',
  260. 'message' => '您有一条新对账打款通知',
  261. 'id' => $id
  262. ]
  263. ], $res['mer_id']);
  264. }
  265. }
  266. public function markForm($id)
  267. {
  268. $data = $this->dao->get($id);
  269. $form = Elm::createForm(Route::buildUrl('merchantReconciliationMark', ['id' => $id])->build());
  270. $form->setRule([
  271. Elm::text('mark', '备注',$data['mark'])->required(),
  272. ]);
  273. return $form->setTitle('修改备注');
  274. }
  275. public function adminMarkForm($id)
  276. {
  277. $data = $this->dao->get($id);
  278. $form = Elm::createForm(Route::buildUrl('systemMerchantReconciliationMark', ['id' => $id])->build());
  279. $form->setRule([
  280. Elm::text('admin_mark', '备注',$data['admin_mark'])->required(),
  281. ]);
  282. return $form->setTitle('修改备注');
  283. }
  284. }