Vrrefund.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. * DSMall多用户商城
  7. * ============================================================================
  8. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  9. * 网站地址: http://www.csdeshang.com
  10. * ----------------------------------------------------------------------------
  11. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  12. * 不允许对程序代码以任何形式任何目的的再发布。
  13. * ============================================================================
  14. * 数据层模型
  15. */
  16. class Vrrefund extends BaseModel {
  17. public $page_info;
  18. /**
  19. * 增加退款
  20. * @access public
  21. * @author csdeshang
  22. * @param type $refund_array 退款数组数据
  23. * @param type $order 排序
  24. * @return boolean
  25. */
  26. public function addVrrefund($refund_array, $order = array()) {
  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. $refund_id = $refund['refund_id'];
  62. $refund_lock = '0'; //退款锁定状态:0为正常,1为锁定,2为同意
  63. $vrorder_model = model('vrorder');
  64. $order_id = $refund['order_id']; //订单编号
  65. try {
  66. Db::startTrans();
  67. $order = $vrorder_model->getVrorderInfo(array('order_id' => $order_id));
  68. $state = $this->editVrrefund(array('refund_id' => $refund_id), $refund); ////更新退款
  69. if ($state && $refund['admin_state'] == '2') {//审核状态:1为待审核,2为同意,3为不同意
  70. $refundreturn_model=model('refundreturn');
  71. $refundreturn_model->refundAmount($order, $order['order_amount']);
  72. if($order['order_promotion_type']==2){//如果是拼团
  73. $ppintuangroup_info=Db::name('ppintuangroup')->where('pintuangroup_id', $order['promotions_id'])->lock(true)->find();
  74. if ($ppintuangroup_info && $ppintuangroup_info['pintuangroup_state'] == 1) {
  75. if ($ppintuangroup_info['pintuangroup_joined'] > 0) {
  76. Db::name('ppintuangroup')->where('pintuangroup_id', $order['promotions_id'])->dec('pintuangroup_joined')->update();
  77. if ($ppintuangroup_info['pintuangroup_joined'] == 1) {
  78. //拼团统计开团数量
  79. $condition = array();
  80. $condition[] = array('pintuan_id', '=', $ppintuangroup_info['pintuan_id']);
  81. $condition[] = array('pintuan_count', '>', 0);
  82. Db::name('ppintuan')->where($condition)->dec('pintuan_count')->update();
  83. }
  84. }
  85. }
  86. }
  87. $refund_lock = '2';
  88. if ($state) {
  89. $order_array = array();
  90. $order_amount = $order['order_amount']; //订单金额
  91. $refund_amount = $order['refund_amount'] + $refund['refund_amount']; //退款金额
  92. $order_array['refund_state'] = ($order_amount - $refund_amount) > 0 ? 1 : 2;
  93. $order_array['refund_amount'] = ds_price_format($refund_amount);
  94. $state = $vrorder_model->editVrorder($order_array, array('order_id' => $order_id)); //更新订单退款
  95. //修改分销佣金
  96. $condition=array();
  97. $condition[]=array('orderinviter_order_id','=',$order_id);
  98. $condition[]=array('orderinviter_valid','=',0);
  99. $condition[]=array('orderinviter_order_type','=',1);
  100. $orderinviter_list=Db::name('orderinviter')->where($condition)->select()->toArray();
  101. foreach($orderinviter_list as $orderinviter_info){
  102. $orderinviter_goods_amount=round($order_amount-$refund_amount,2);
  103. $orderinviter_money=round($orderinviter_info['orderinviter_ratio']/100*$orderinviter_goods_amount,2);
  104. Db::name('orderinviter')->where(array(array('orderinviter_id','=',$orderinviter_info['orderinviter_id'])))->update(['orderinviter_goods_amount' => $orderinviter_goods_amount,'orderinviter_money'=>$orderinviter_money]);
  105. }
  106. }
  107. }
  108. if ($state) {
  109. $code_array = explode(',', $refund['redeemcode_sn']);
  110. $state = $vrorder_model->editVrorderCode(array('refund_lock' => $refund_lock), array(array('vr_code','in', $code_array))); //更新退款的兑换码
  111. if ($state && $refund['admin_state'] == '2') {
  112. model('vrorder','logic')->changeOrderStateSuccess($order_id); //更新订单状态
  113. }
  114. }
  115. Db::commit();
  116. return $state;
  117. } catch (Exception $e) {
  118. Db::rollback();
  119. return false;
  120. }
  121. }
  122. /**
  123. * 修改退款
  124. * @access public
  125. * @author csdeshang
  126. * @param type $condition 条件
  127. * @param type $data 数据
  128. * @return boolean
  129. */
  130. public function editVrrefund($condition, $data) {
  131. if (empty($condition)) {
  132. return false;
  133. }
  134. if (is_array($data)) {
  135. $result = Db::name('vrrefund')->where($condition)->update($data);
  136. return $result;
  137. } else {
  138. return false;
  139. }
  140. }
  141. /**
  142. * 退款编号
  143. * @access public
  144. * @author csdeshang
  145. * @param type $store_id 店铺ID
  146. * @return string
  147. */
  148. public function getVrrefundSn($store_id) {
  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. if($pagesize){
  164. $result = Db::name('vrrefund')->field($field)->where($condition)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  165. $this->page_info = $result;
  166. $result = $result->items();
  167. }else{
  168. $result = Db::name('vrrefund')->field($field)->where($condition)->order($order)->limit($limit)->select()->toArray();
  169. }
  170. return $result;
  171. }
  172. /**
  173. * 取得退款记录的数量
  174. * @access public
  175. * @author csdeshang
  176. * @param type $condition 条件
  177. * @return type
  178. */
  179. public function getVrrefundCount($condition) {
  180. $result = Db::name('vrrefund')->where($condition)->count();
  181. return $result;
  182. }
  183. /**
  184. * 详细页右侧订单信息
  185. * @access public
  186. * @author csdeshang
  187. * @param type $order_condition 条件
  188. * @return type
  189. */
  190. public function getRightVrorderList($order_condition,$order_id) {
  191. $vrorder_model = model('vrorder');
  192. $order_info = $vrorder_model->getVrorderInfo($order_condition);
  193. $order_list = array();
  194. $order_list[$order_id] = $order_info;
  195. $order_list = $vrorder_model->getCodeRefundList($order_list); //没有使用的兑换码列表
  196. $order_info = $order_list[$order_id];
  197. $store_model = model('store');
  198. $store = $store_model->getStoreInfo(array('store_id' => $order_info['store_id']));
  199. //显示退款
  200. $order_info['if_refund'] = $vrorder_model->getVrorderOperateState('refund', $order_info);
  201. $code_list=array();
  202. if ($order_info['if_refund']) {
  203. $code_list = $order_info['code_list'];
  204. }
  205. return array('order_info'=>$order_info,'store'=>$store,'code_list'=>$code_list);
  206. }
  207. /**
  208. * 获得退款的店铺列表
  209. * @access public
  210. * @author csdeshang
  211. * @param type $list 列表
  212. * @return type
  213. */
  214. public function getVrrefundStoreList($list) {
  215. $store_ids = array();
  216. if (!empty($list) && is_array($list)) {
  217. foreach ($list as $key => $value) {
  218. $store_ids[] = $value['store_id']; //店铺编号
  219. }
  220. }
  221. $field = 'store_id,store_name,member_id,member_name,seller_name,store_company_name,store_qq,store_ww,store_phone';
  222. return model('store')->getStoreMemberIDList($store_ids, $field);
  223. }
  224. /**
  225. * 获取一条退款记录
  226. * @access public
  227. * @author csdeshang
  228. * @param type $condition 条件
  229. * @return type
  230. */
  231. public function getOneVrrefund($condition){
  232. $refund = Db::name('vrrefund')->where($condition)->find();
  233. return $refund;
  234. }
  235. /**
  236. * 向模板页面输出退款状态
  237. * @access public
  238. * @author csdeshang
  239. * @param type $type 类型
  240. * @return string
  241. */
  242. public function getRefundStateArray($type = 'all') {
  243. $admin_array = array(
  244. '1' => '待审核',
  245. '2' => '同意',
  246. '3' => '不同意'
  247. ); //退款状态:1为待审核,2为同意,3为不同意
  248. $state_data = array(
  249. 'admin' => $admin_array
  250. );
  251. if ($type == 'all')
  252. return $state_data; //返回所有
  253. return $state_data[$type];
  254. }
  255. }