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