vr_refund.model.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * 虚拟兑码退款模型
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class vr_refundModel extends Model{
  11. public function __construct() {
  12. parent::__construct();
  13. }
  14. /**
  15. * 增加退款
  16. *
  17. * @param
  18. * @return int
  19. */
  20. public function addRefund($refund_array, $order = array()) {
  21. if (!empty($order) && is_array($order)) {
  22. $refund_array['order_id'] = $order['order_id'];
  23. $refund_array['order_sn'] = $order['order_sn'];
  24. $refund_array['store_id'] = $order['store_id'];
  25. $refund_array['store_name'] = $order['store_name'];
  26. $refund_array['buyer_id'] = $order['buyer_id'];
  27. $refund_array['buyer_name'] = $order['buyer_name'];
  28. $refund_array['goods_id'] = $order['goods_id'];
  29. $refund_array['goods_name'] = $order['goods_name'];
  30. $refund_array['goods_image'] = $order['goods_image'];
  31. $refund_array['commis_rate'] = $order['commis_rate'];
  32. }
  33. $refund_array['refund_sn'] = $this->getRefundsn($refund_array['store_id']);
  34. try {
  35. $trans = new trans_wapper($this,__METHOD__);
  36. $refund_id = $this->table('vr_refund')->insert($refund_array);
  37. $code_array = explode(',', $refund_array['code_sn']);
  38. $model_vr_order = Model('vr_order');
  39. $model_vr_order->editOrderCode(array('refund_lock'=> 1),array('vr_code'=> array('in',$code_array)));//退款锁定
  40. $trans->commit();
  41. return $refund_id;
  42. } catch (Exception $e) {
  43. $trans->rollback();
  44. return false;
  45. }
  46. }
  47. /**
  48. * 平台退款处理
  49. *
  50. * @param
  51. * @return bool
  52. */
  53. public function editOrderRefund($refund) {
  54. $refund_id = $refund['refund_id'];
  55. $refund_lock = '0';//退款锁定状态:0为正常,1为锁定,2为同意
  56. $model_vr_order = Model('vr_order');
  57. try
  58. {
  59. $trans = new trans_wapper($this,__METHOD__);
  60. $state = $this->editRefund(array('refund_id'=> $refund_id),$refund);////更新退款
  61. if ($state && $refund['admin_state'] == '2') {//审核状态:1为待审核,2为同意,3为不同意
  62. $refund_lock = '2';
  63. $order_id = $refund['order_id'];//订单编号
  64. $order = $model_vr_order->getOrderInfo(array('order_id'=> $order_id));
  65. $order_amount = $order['order_amount'];//订单金额
  66. $rcb_amount = $order['rcb_amount'];//充值卡支付金额
  67. $predeposit_amount = $order_amount-$order['refund_amount']-$rcb_amount;//可退预存款金额
  68. $model_predeposit = Model('predeposit');
  69. if (($rcb_amount > 0) && ($refund['refund_amount'] > $predeposit_amount)) {//退充值卡
  70. $log_array = array();
  71. $log_array['member_id'] = $order['buyer_id'];
  72. $log_array['member_name'] = $order['buyer_name'];
  73. $log_array['order_sn'] = $order['order_sn'];
  74. $log_array['amount'] = $refund['refund_amount'];
  75. if ($predeposit_amount > 0) {
  76. $log_array['amount'] = $refund['refund_amount']-$predeposit_amount;
  77. }
  78. $state = $model_predeposit->changeRcb('refund', $log_array);//增加买家可用充值卡金额
  79. }
  80. if ($predeposit_amount > 0) {//退预存款
  81. $log_array = array();
  82. $log_array['member_id'] = $order['buyer_id'];
  83. $log_array['member_name'] = $order['buyer_name'];
  84. $log_array['order_sn'] = $order['order_sn'];
  85. $log_array['amount'] = $refund['refund_amount'];//退预存款金额
  86. if ($refund['refund_amount'] > $predeposit_amount) {
  87. $log_array['amount'] = $predeposit_amount;
  88. }
  89. $state = $model_predeposit->changePd('refund', $log_array);//增加买家可用预存款金额
  90. }
  91. if ($state) {
  92. $order_array = array();
  93. $order_amount = $order['order_amount'];//订单金额
  94. $refund_amount = $order['refund_amount']+$refund['refund_amount'];//退款金额
  95. $order_array['refund_state'] = ($order_amount-$refund_amount) > 0 ? 1:2;
  96. $order_array['refund_amount'] = ncPriceFormat($refund_amount);
  97. $state = $model_vr_order->editOrder($order_array,array('order_id'=> $order_id));//更新订单退款
  98. }
  99. }
  100. if ($state) {
  101. $code_array = explode(',', $refund['code_sn']);
  102. $state = $model_vr_order->editOrderCode(array('refund_lock'=> $refund_lock),array('vr_code'=> array('in',$code_array)));//更新退款的兑换码
  103. if ($state && $refund['admin_state'] == '2') {
  104. Logic('vr_order')->changeOrderStateSuccess($order_id);//更新订单状态
  105. }
  106. }
  107. $trans->commit();
  108. return $state;
  109. } catch (Exception $e) {
  110. $trans->rollback();
  111. return false;
  112. }
  113. }
  114. /**
  115. * 修改退款
  116. *
  117. * @param
  118. * @return bool
  119. */
  120. public function editRefund($condition, $data) {
  121. if (empty($condition)) {
  122. return false;
  123. }
  124. if (is_array($data)) {
  125. $result = $this->table('vr_refund')->where($condition)->update($data);
  126. return $result;
  127. } else {
  128. return false;
  129. }
  130. }
  131. /**
  132. * 退款编号
  133. *
  134. * @param
  135. * @return array
  136. */
  137. public function getRefundsn($store_id) {
  138. $result = mt_rand(100,999).substr(500+$store_id,-3).date('ymdHis');
  139. return $result;
  140. }
  141. /**
  142. * 退款记录
  143. *
  144. * @param
  145. * @return array
  146. */
  147. public function getRefundList($condition = array(), $page = '', $limit = '', $fields = '*') {
  148. $result = $this->table('vr_refund')->field($fields)->where($condition)->page($page)->limit($limit)->order('refund_id desc')->select();
  149. return $result;
  150. }
  151. /**
  152. * 取得退款记录的数量
  153. * @param array $condition
  154. */
  155. public function getRefundCount($condition) {
  156. $result = $this->table('vr_refund')->where($condition)->count();
  157. return $result;
  158. }
  159. /**
  160. * 详细页右侧订单信息
  161. *
  162. * @param
  163. * @return array
  164. */
  165. public function getRightOrderList($order_condition){
  166. $order_id = $order_condition['order_id'];
  167. $model_vr_order = Model('vr_order');
  168. $order_info = $model_vr_order->getOrderInfo($order_condition);
  169. Tpl::output('order',$order_info);
  170. $order_list = array();
  171. $order_list[$order_id] = $order_info;
  172. $order_list = $model_vr_order->getCodeRefundList($order_list);//没有使用的兑换码列表
  173. $order_info = $order_list[$order_id];
  174. $model_store = Model('store');
  175. $store = $model_store->getStoreInfo(array('store_id' => $order_info['store_id']));
  176. Tpl::output('store',$store);
  177. //显示退款
  178. $order_info['if_refund'] = $model_vr_order->getOrderOperateState('refund',$order_info);
  179. if ($order_info['if_refund']) {
  180. $code_list = $order_info['code_list'];
  181. Tpl::output('code_list',$code_list);
  182. }
  183. return $order_info;
  184. }
  185. /*
  186. * 获得退款的店铺列表
  187. * @param array $complain_list
  188. * @return array
  189. */
  190. public function getRefundStoreList($list) {
  191. $store_ids = array();
  192. if (!empty($list) && is_array($list)) {
  193. foreach ($list as $key => $value) {
  194. $store_ids[] = $value['store_id'];//店铺编号
  195. }
  196. }
  197. $field = 'store_id,store_name,member_id,member_name,seller_name,store_company_name,store_qq,store_ww,store_phone,store_domain';
  198. return Model('store')->getStoreMemberIDList($store_ids, $field);
  199. }
  200. /**
  201. * 向模板页面输出退款状态
  202. *
  203. * @param
  204. * @return array
  205. */
  206. public function getRefundStateArray($type = 'all') {
  207. $admin_array = array(
  208. '1' => '待审核',
  209. '2' => '同意',
  210. '3' => '不同意'
  211. );//退款状态:1为待审核,2为同意,3为不同意
  212. Tpl::output('admin_array', $admin_array);
  213. $state_data = array(
  214. 'admin' => $admin_array
  215. );
  216. if ($type == 'all') return $state_data;//返回所有
  217. return $state_data[$type];
  218. }
  219. }