WxCash.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use EasyWeChat\Factory;
  5. class WxCash extends Base
  6. {
  7. public $status = [
  8. 0=> '退款处理中',
  9. 1=> '退款成功',
  10. 2=> '退款拒绝',
  11. ];
  12. public function lists($page,$size,$userId,$orgId,$status){
  13. if($status>0){
  14. if($status==1){
  15. $map[]=['status','=',0];
  16. }elseif ($status==2){
  17. $map[]=['status','=',1];
  18. }elseif ($status==3){
  19. $map[]=['status','=',2];
  20. }elseif ($status==4){
  21. $map[]=['status','in',[5,6,7]];
  22. }
  23. }
  24. $map[]=['user_id','=',$userId];
  25. $map[]=['org_id','=',$orgId];
  26. $map[]=['del','=',0];
  27. $ret=Db::name('wx_orders')
  28. ->field('id,order_sn,status,amount')
  29. ->where($map)
  30. ->order('id','desc')
  31. ->page($page,$size)
  32. ->select();
  33. foreach ($ret as $k=>$v){
  34. $orderGoods=Db::name('wx_orders_goods')
  35. ->alias('og')
  36. ->field('og.nums,og.price,og.goods_id,g.title,g.img,g.label')
  37. ->join('wx_goods g','g.id=og.goods_id')
  38. ->where('og.order_id',$v['id'])
  39. ->select();
  40. $nums=0;
  41. foreach ($orderGoods as $kk=>$vv){
  42. $nums+=$vv['nums'];
  43. $orderGoods[$k]['label_name'] = $vv['label']>0?Db::name('wx_goods_label')
  44. ->where('id',$vv['label'])
  45. ->value('title'):"";
  46. }
  47. $ret[$k]['total_num']=$nums;
  48. $ret[$k]['goods_num']=count($orderGoods);
  49. $ret[$k]['order_goods']=$orderGoods;
  50. }
  51. return $ret?$ret:[];
  52. }
  53. public function details($ordersId){
  54. $ret = Db::name('g_orders')->where('id', $ordersId)->find();
  55. $ret['goods'] = Db::name('g_order_goods')
  56. ->alias('og')
  57. ->field('og.nums,og.price,og.goods_id,g.title,g.img')
  58. ->join('g_goods g', 'g.id=og.goods_id')
  59. ->where('og.order_id', $ret['id'])
  60. ->select();
  61. $rd = Db::name('g_order_refund')
  62. ->where('order_id', $ret['id'])
  63. ->find();
  64. if(empty($rd)){
  65. $rd = [
  66. 'sn'=>'',
  67. 'order_id'=>'',
  68. 'money'=>'',
  69. 'remark'=>'',
  70. 'refund_money'=>'',
  71. 'refund_remark'=>'',
  72. ];
  73. }
  74. $ret['refund'] = $rd;
  75. $nums=0;
  76. foreach ($ret['goods'] as $k=>$v){
  77. $nums+=$v['nums'];
  78. }
  79. $ret['sum']=$nums;
  80. if($ret['status']==1 && time() < (strtotime($ret['create_time'])+(30*24*3600))){
  81. $ret['tk'] = 1;
  82. }else{
  83. $ret['tk'] = 0;
  84. }
  85. $s = [
  86. '未支付',
  87. '已支付',
  88. '已完成',
  89. '已取消',
  90. '已退款',
  91. '退款拒绝',
  92. '退款中',
  93. ];
  94. $ret['statusTxt']=isset($s[$ret['status']])?$s[$ret['status']]:'';
  95. return $ret;
  96. }
  97. public function accomplish($ordersId){
  98. $data=[
  99. 'status'=>2,
  100. 'finish_time'=>date('Y-m-d H:i:s'),
  101. ];
  102. $ret = Db::name('g_orders')->where('id', $ordersId)->update($data);
  103. return $ret;
  104. }
  105. public function refundOrder($userId){
  106. $data = [
  107. 'money' => input('money/f',0),
  108. 'order_id' => input('id/d',0),
  109. 'user_id' => $userId,
  110. 'remark' => input('remark','','trim'),
  111. 'create_time' => date('Y-m-d H:i:s'),
  112. ];
  113. if($data['money'] <= 0){
  114. $this->error = '金额必须大于0';
  115. return false;
  116. }
  117. $pay = Db::name('g_orders')
  118. ->where('id',$data['order_id'])->find();
  119. if(!$pay||$pay['status'] != 1){
  120. $this->error = '订单不存在';
  121. return false;
  122. }
  123. $data['sn'] = get_unique_id('TPH');
  124. $data['org_id'] = $pay['org_id'];
  125. if($data['money'] > $pay['amount']){
  126. $this->error = '退款金额过大';
  127. return false;
  128. }
  129. Db::startTrans();
  130. try{
  131. $ret = Db::name('g_order_refund')->insert($data);
  132. if(!$ret){
  133. \exception('操作失败');
  134. }
  135. $order = Db::name('g_orders')
  136. ->where('id',$pay['id'])
  137. ->lock(true)
  138. ->find();
  139. if(!$order){
  140. \exception('订单不存在');
  141. }
  142. $ret = Db::name('g_orders')
  143. ->where('id',$pay['id'])->update([
  144. 'refund_money' => $data['money'],
  145. 'status' => 4
  146. ]);
  147. if(!$ret){
  148. \exception('操作失败');
  149. }
  150. $config = config('app.wx_mini_config');
  151. $app = \EasyWeChat\Factory::payment($config);
  152. $ret = $app->refund->byOutTradeNumber($pay['order_sn'], $data['sn'], $pay['amount']*100, $data['money']*100);
  153. if($ret['return_code'] != 'SUCCESS' || $ret['result_code'] != 'SUCCESS'){
  154. trace(json_encode($ret));
  155. \exception('退款申请提交失败');
  156. }
  157. Db::commit();
  158. return true;
  159. }catch (\Exception $e){
  160. Db::rollback();
  161. trace($e->getMessage());
  162. $this->error = $e->getMessage();
  163. return false;
  164. }
  165. }
  166. public function formatOrder($ids){
  167. $list = [];
  168. $total = 0;
  169. $isWater = 0;
  170. $cash_price = 0;
  171. $remark = [];
  172. foreach ($ids as $k=>$v){
  173. $ret=Db::name('wx_goods')
  174. ->field('price,id as goods_id,title,img,label,barrel_id')
  175. ->where('id','=',$v['id'])
  176. ->find();
  177. $ret['label_name'] = $ret['label']>0?Db::name('wx_goods_label')
  178. ->where('id',$ret['label'])
  179. ->value('title'):"";
  180. $ret['num'] = $v['num'];
  181. $total+=$v['num']*$ret['price'];
  182. if($ret['barrel_id'] >0){
  183. $isWater = 1;
  184. $wx_barrel = Db::name('wx_barrel')
  185. ->where('id',$ret['barrel_id'])
  186. ->find();
  187. $cash_price+=$wx_barrel['cash_price']*$v['num'];
  188. $remark[] =$ret['title'].'押金:'.$wx_barrel['cash_price']*$v['num'];
  189. }
  190. $list[] = $ret;
  191. }
  192. $data = [
  193. 'list'=>$list,
  194. 'isWater'=>$isWater,
  195. 'remark'=>$remark?implode(';',$remark):"",
  196. 'cash_price'=>sprintf("%01.2f", $cash_price),
  197. 'total'=>sprintf("%01.2f", $total),
  198. ];
  199. return $data;
  200. }
  201. }