Vrrefund.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. }
  97. if ($state) {
  98. $code_array = explode(',', $refund['redeemcode_sn']);
  99. $state = $vrorder_model->editVrorderCode(array('refund_lock' => $refund_lock), array(array('vr_code','in', $code_array))); //更新退款的兑换码
  100. if ($state && $refund['admin_state'] == '2') {
  101. model('vrorder','logic')->changeOrderStateSuccess($order_id); //更新订单状态
  102. }
  103. }
  104. $logic_payment = model('payment', 'logic');
  105. $logic_payment->refundRecordPush($order_id,1);
  106. Db::commit();
  107. return $state;
  108. } catch (Exception $e) {
  109. Db::rollback();
  110. return false;
  111. }
  112. }
  113. /**
  114. * 修改退款
  115. * @access public
  116. * @author csdeshang
  117. * @param type $condition 条件
  118. * @param type $data 数据
  119. * @return boolean
  120. */
  121. public function editVrrefund($condition, $data) {
  122. if (empty($condition)) {
  123. return false;
  124. }
  125. if (is_array($data)) {
  126. $result = Db::name('vrrefund')->where($condition)->update($data);
  127. return $result;
  128. } else {
  129. return false;
  130. }
  131. }
  132. /**
  133. * 退款编号
  134. * @access public
  135. * @author csdeshang
  136. * @param type $store_id 店铺ID
  137. * @return string
  138. */
  139. public function getVrrefundSn($store_id) {
  140. $result = mt_rand(100, 999) . substr(500 + $store_id, -3) . date('ymdHis');
  141. return $result;
  142. }
  143. /**
  144. * 退款记录
  145. * @access public
  146. * @author csdeshang
  147. * @param type $condition 条件
  148. * @param type $pagesize 分页
  149. * @param type $limit 限制
  150. * @param type $fields 字段
  151. * @return type
  152. */
  153. public function getVrrefundList($condition = array(), $pagesize = '', $field = '*', $order = 'refund_id desc', $limit = 0) {
  154. if($pagesize){
  155. $result = Db::name('vrrefund')->field($field)->where($condition)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  156. $this->page_info = $result;
  157. $result = $result->items();
  158. }else{
  159. $result = Db::name('vrrefund')->field($field)->where($condition)->order($order)->limit($limit)->select()->toArray();
  160. }
  161. return $result;
  162. }
  163. /**
  164. * 取得退款记录的数量
  165. * @access public
  166. * @author csdeshang
  167. * @param type $condition 条件
  168. * @return type
  169. */
  170. public function getVrrefundCount($condition) {
  171. $result = Db::name('vrrefund')->where($condition)->count();
  172. return $result;
  173. }
  174. /**
  175. * 详细页右侧订单信息
  176. * @access public
  177. * @author csdeshang
  178. * @param type $order_condition 条件
  179. * @return type
  180. */
  181. public function getRightVrorderList($order_condition,$order_id) {
  182. $vrorder_model = model('vrorder');
  183. $order_info = $vrorder_model->getVrorderInfo($order_condition);
  184. $order_list = array();
  185. $order_list[$order_id] = $order_info;
  186. $order_list = $vrorder_model->getCodeRefundList($order_list); //没有使用的兑换码列表
  187. $order_info = $order_list[$order_id];
  188. $store_model = model('store');
  189. $store = $store_model->getStoreInfo(array('store_id' => $order_info['store_id']));
  190. //显示退款
  191. $order_info['if_refund'] = $vrorder_model->getVrorderOperateState('refund', $order_info);
  192. $code_list=array();
  193. if ($order_info['if_refund']) {
  194. $code_list = $order_info['code_list'];
  195. }
  196. return array('order_info'=>$order_info,'store'=>$store,'code_list'=>$code_list);
  197. }
  198. /**
  199. * 获得退款的店铺列表
  200. * @access public
  201. * @author csdeshang
  202. * @param type $list 列表
  203. * @return type
  204. */
  205. public function getVrrefundStoreList($list) {
  206. $store_ids = array();
  207. if (!empty($list) && is_array($list)) {
  208. foreach ($list as $key => $value) {
  209. $store_ids[] = $value['store_id']; //店铺编号
  210. }
  211. }
  212. $field = 'store_id,store_name,member_id,member_name,seller_name,store_company_name,store_qq,store_ww,store_phone';
  213. return model('store')->getStoreMemberIDList($store_ids, $field);
  214. }
  215. /**
  216. * 获取一条退款记录
  217. * @access public
  218. * @author csdeshang
  219. * @param type $condition 条件
  220. * @return type
  221. */
  222. public function getOneVrrefund($condition){
  223. $refund = Db::name('vrrefund')->where($condition)->find();
  224. return $refund;
  225. }
  226. /**
  227. * 向模板页面输出退款状态
  228. * @access public
  229. * @author csdeshang
  230. * @param type $type 类型
  231. * @return string
  232. */
  233. public function getRefundStateArray($type = 'all') {
  234. $admin_array = array(
  235. '1' => '待审核',
  236. '2' => '同意',
  237. '3' => '不同意'
  238. ); //退款状态:1为待审核,2为同意,3为不同意
  239. $state_data = array(
  240. 'admin' => $admin_array
  241. );
  242. if ($type == 'all')
  243. return $state_data; //返回所有
  244. return $state_data[$type];
  245. }
  246. }