Vrrefund.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  9. * 网站地址: https://www.valimart.net/
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 数据层模型
  14. */
  15. class Vrrefund extends BaseModel {
  16. public $page_info;
  17. /**
  18. * 增加退款
  19. * @access public
  20. * @author csdeshang
  21. * @param type $refund_array 退款数组数据
  22. * @param type $order 排序
  23. * @return boolean
  24. */
  25. public function addVrrefund($refund_array, $order = array()) {
  26. if (!empty($order) && is_array($order)) {
  27. $refund_array['order_id'] = $order['order_id'];
  28. $refund_array['order_sn'] = $order['order_sn'];
  29. $refund_array['store_id'] = $order['store_id'];
  30. $refund_array['store_name'] = $order['store_name'];
  31. $refund_array['buyer_id'] = $order['buyer_id'];
  32. $refund_array['buyer_name'] = $order['buyer_name'];
  33. $refund_array['goods_id'] = $order['goods_id'];
  34. $refund_array['goods_name'] = $order['goods_name'];
  35. $refund_array['goods_image'] = $order['goods_image'];
  36. $refund_array['commis_rate'] = $order['commis_rate'];
  37. }
  38. $refund_array['refund_sn'] = $this->getVrrefundSn($refund_array['store_id']);
  39. try {
  40. Db::startTrans();
  41. $refund_id = Db::name('vrrefund')->insertGetId($refund_array);
  42. $code_array = explode(',', $refund_array['redeemcode_sn']);
  43. $vrorder_model = model('vrorder');
  44. $vrorder_model->editVrorderCode(array('refund_lock' => 1), array(array('vr_code','in', $code_array))); //退款锁定
  45. Db::commit();
  46. return $refund_id;
  47. } catch (Exception $e) {
  48. Db::rollback();
  49. return false;
  50. }
  51. }
  52. /**
  53. * 平台退款处理
  54. * @access public
  55. * @author csdeshang
  56. * @param type $refund 退款
  57. * @return boolean
  58. */
  59. public function editVrorderRefund($refund) {
  60. $refund_id = $refund['refund_id'];
  61. $refund_lock = '0'; //退款锁定状态:0为正常,1为锁定,2为同意
  62. $vrorder_model = model('vrorder');
  63. $order_id = $refund['order_id']; //订单编号
  64. try {
  65. Db::startTrans();
  66. $order = $vrorder_model->getVrorderInfo(array('order_id' => $order_id));
  67. $state = $this->editVrrefund(array('refund_id' => $refund_id), $refund); ////更新退款
  68. if ($state && $refund['admin_state'] == '2') {//审核状态:1为待审核,2为同意,3为不同意
  69. $refundreturn_model=model('refundreturn');
  70. $refundreturn_model->refundAmount($order, $order['order_amount']);
  71. if($order['order_promotion_type']==2){//如果是拼团
  72. $ppintuangroup_info=Db::name('ppintuangroup')->where('pintuangroup_id', $order['promotions_id'])->lock(true)->find();
  73. if ($ppintuangroup_info && $ppintuangroup_info['pintuangroup_state'] == 1) {
  74. if ($ppintuangroup_info['pintuangroup_joined'] > 0) {
  75. Db::name('ppintuangroup')->where('pintuangroup_id', $order['promotions_id'])->dec('pintuangroup_joined')->update();
  76. if ($ppintuangroup_info['pintuangroup_joined'] == 1) {
  77. //拼团统计开团数量
  78. $condition = array();
  79. $condition[] = array('pintuan_id', '=', $ppintuangroup_info['pintuan_id']);
  80. $condition[] = array('pintuan_count', '>', 0);
  81. Db::name('ppintuan')->where($condition)->dec('pintuan_count')->update();
  82. }
  83. }
  84. }
  85. }
  86. $refund_lock = '2';
  87. if ($state) {
  88. $order_array = array();
  89. $order_amount = $order['order_amount']; //订单金额
  90. $refund_amount = $order['refund_amount'] + $refund['refund_amount']; //退款金额
  91. $order_array['refund_state'] = ($order_amount - $refund_amount) > 0 ? 1 : 2;
  92. $order_array['refund_amount'] = ds_price_format($refund_amount);
  93. $state = $vrorder_model->editVrorder($order_array, array('order_id' => $order_id)); //更新订单退款
  94. //修改分销佣金
  95. $condition=array();
  96. $condition[]=array('orderinviter_order_id','=',$order_id);
  97. $condition[]=array('orderinviter_valid','=',0);
  98. $condition[]=array('orderinviter_order_type','=',1);
  99. $orderinviter_list=Db::name('orderinviter')->where($condition)->select()->toArray();
  100. foreach($orderinviter_list as $orderinviter_info){
  101. $orderinviter_goods_amount=round($order_amount-$refund_amount,2);
  102. $orderinviter_money=round($orderinviter_info['orderinviter_ratio']/100*$orderinviter_goods_amount,2);
  103. Db::name('orderinviter')->where(array(array('orderinviter_id','=',$orderinviter_info['orderinviter_id'])))->update(['orderinviter_goods_amount' => $orderinviter_goods_amount,'orderinviter_money'=>$orderinviter_money]);
  104. }
  105. }
  106. }
  107. if ($state) {
  108. $code_array = explode(',', $refund['redeemcode_sn']);
  109. $state = $vrorder_model->editVrorderCode(array('refund_lock' => $refund_lock), array(array('vr_code','in', $code_array))); //更新退款的兑换码
  110. if ($state && $refund['admin_state'] == '2') {
  111. model('vrorder','logic')->changeOrderStateSuccess($order_id); //更新订单状态
  112. }
  113. }
  114. Db::commit();
  115. return $state;
  116. } catch (Exception $e) {
  117. Db::rollback();
  118. return false;
  119. }
  120. }
  121. /**
  122. * 修改退款
  123. * @access public
  124. * @author csdeshang
  125. * @param type $condition 条件
  126. * @param type $data 数据
  127. * @return boolean
  128. */
  129. public function editVrrefund($condition, $data) {
  130. if (empty($condition)) {
  131. return false;
  132. }
  133. if (is_array($data)) {
  134. $result = Db::name('vrrefund')->where($condition)->update($data);
  135. return $result;
  136. } else {
  137. return false;
  138. }
  139. }
  140. /**
  141. * 退款编号
  142. * @access public
  143. * @author csdeshang
  144. * @param type $store_id 店铺ID
  145. * @return string
  146. */
  147. public function getVrrefundSn($store_id) {
  148. $result = mt_rand(100, 999) . substr(500 + $store_id, -3) . date('ymdHis');
  149. return $result;
  150. }
  151. /**
  152. * 退款记录
  153. * @access public
  154. * @author csdeshang
  155. * @param type $condition 条件
  156. * @param type $pagesize 分页
  157. * @param type $limit 限制
  158. * @param type $fields 字段
  159. * @return type
  160. */
  161. public function getVrrefundList($condition = array(), $pagesize = '', $field = '*', $order = 'refund_id desc', $limit = 0) {
  162. if($pagesize){
  163. $result = Db::name('vrrefund')->field($field)->where($condition)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  164. $this->page_info = $result;
  165. $result = $result->items();
  166. }else{
  167. $result = Db::name('vrrefund')->field($field)->where($condition)->order($order)->limit($limit)->select()->toArray();
  168. }
  169. return $result;
  170. }
  171. /**
  172. * 取得退款记录的数量
  173. * @access public
  174. * @author csdeshang
  175. * @param type $condition 条件
  176. * @return type
  177. */
  178. public function getVrrefundCount($condition) {
  179. $result = Db::name('vrrefund')->where($condition)->count();
  180. return $result;
  181. }
  182. /**
  183. * 详细页右侧订单信息
  184. * @access public
  185. * @author csdeshang
  186. * @param type $order_condition 条件
  187. * @return type
  188. */
  189. public function getRightVrorderList($order_condition,$order_id) {
  190. $vrorder_model = model('vrorder');
  191. $order_info = $vrorder_model->getVrorderInfo($order_condition);
  192. $order_list = array();
  193. $order_list[$order_id] = $order_info;
  194. $order_list = $vrorder_model->getCodeRefundList($order_list); //没有使用的兑换码列表
  195. $order_info = $order_list[$order_id];
  196. $store_model = model('store');
  197. $store = $store_model->getStoreInfo(array('store_id' => $order_info['store_id']));
  198. //显示退款
  199. $order_info['if_refund'] = $vrorder_model->getVrorderOperateState('refund', $order_info);
  200. $code_list=array();
  201. if ($order_info['if_refund']) {
  202. $code_list = $order_info['code_list'];
  203. }
  204. return array('order_info'=>$order_info,'store'=>$store,'code_list'=>$code_list);
  205. }
  206. /**
  207. * 获得退款的店铺列表
  208. * @access public
  209. * @author csdeshang
  210. * @param type $list 列表
  211. * @return type
  212. */
  213. public function getVrrefundStoreList($list) {
  214. $store_ids = array();
  215. if (!empty($list) && is_array($list)) {
  216. foreach ($list as $key => $value) {
  217. $store_ids[] = $value['store_id']; //店铺编号
  218. }
  219. }
  220. $field = 'store_id,store_name,member_id,member_name,seller_name,store_company_name,store_qq,store_ww,store_phone';
  221. return model('store')->getStoreMemberIDList($store_ids, $field);
  222. }
  223. /**
  224. * 获取一条退款记录
  225. * @access public
  226. * @author csdeshang
  227. * @param type $condition 条件
  228. * @return type
  229. */
  230. public function getOneVrrefund($condition){
  231. $refund = Db::name('vrrefund')->where($condition)->find();
  232. return $refund;
  233. }
  234. /**
  235. * 向模板页面输出退款状态
  236. * @access public
  237. * @author csdeshang
  238. * @param type $type 类型
  239. * @return string
  240. */
  241. public function getRefundStateArray($type = 'all') {
  242. $admin_array = array(
  243. '1' => '待审核',
  244. '2' => '同意',
  245. '3' => '不同意'
  246. ); //退款状态:1为待审核,2为同意,3为不同意
  247. $state_data = array(
  248. 'admin' => $admin_array
  249. );
  250. if ($type == 'all')
  251. return $state_data; //返回所有
  252. return $state_data[$type];
  253. }
  254. }