Vrrefund.php 11 KB

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