vr_refund.model.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. $this->beginTransaction();
  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. $this->commit();
  41. return $refund_id;
  42. } catch (Exception $e) {
  43. $this->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. $this->beginTransaction();
  59. $state = $this->editRefund(array('refund_id'=> $refund_id),$refund);////更新退款
  60. if ($state && $refund['admin_state'] == '2') {//审核状态:1为待审核,2为同意,3为不同意
  61. $refund_lock = '2';
  62. $order_id = $refund['order_id'];//订单编号
  63. $order = $model_vr_order->getOrderInfo(array('order_id'=> $order_id));
  64. $order_amount = $order['order_amount'];//订单金额
  65. $rcb_amount = $order['rcb_amount'];//充值卡支付金额
  66. $predeposit_amount = $order_amount-$order['refund_amount']-$rcb_amount;//可退预存款金额
  67. $model_predeposit = Model('predeposit');
  68. if (($rcb_amount > 0) && ($refund['refund_amount'] > $predeposit_amount)) {//退充值卡
  69. $log_array = array();
  70. $log_array['member_id'] = $order['buyer_id'];
  71. $log_array['member_name'] = $order['buyer_name'];
  72. $log_array['order_sn'] = $order['order_sn'];
  73. $log_array['amount'] = $refund['refund_amount'];
  74. if ($predeposit_amount > 0) {
  75. $log_array['amount'] = $refund['refund_amount']-$predeposit_amount;
  76. }
  77. $state = $model_predeposit->changeRcb('refund', $log_array);//增加买家可用充值卡金额
  78. }
  79. if ($predeposit_amount > 0) {//退预存款
  80. $log_array = array();
  81. $log_array['member_id'] = $order['buyer_id'];
  82. $log_array['member_name'] = $order['buyer_name'];
  83. $log_array['order_sn'] = $order['order_sn'];
  84. $log_array['amount'] = $refund['refund_amount'];//退预存款金额
  85. if ($refund['refund_amount'] > $predeposit_amount) {
  86. $log_array['amount'] = $predeposit_amount;
  87. }
  88. $state = $model_predeposit->changePd('refund', $log_array);//增加买家可用预存款金额
  89. }
  90. if ($state) {
  91. $order_array = array();
  92. $order_amount = $order['order_amount'];//订单金额
  93. $refund_amount = $order['refund_amount']+$refund['refund_amount'];//退款金额
  94. $order_array['refund_state'] = ($order_amount-$refund_amount) > 0 ? 1:2;
  95. $order_array['refund_amount'] = ncPriceFormat($refund_amount);
  96. $state = $model_vr_order->editOrder($order_array,array('order_id'=> $order_id));//更新订单退款
  97. }
  98. }
  99. if ($state) {
  100. $code_array = explode(',', $refund['code_sn']);
  101. $state = $model_vr_order->editOrderCode(array('refund_lock'=> $refund_lock),array('vr_code'=> array('in',$code_array)));//更新退款的兑换码
  102. if ($state && $refund['admin_state'] == '2') {
  103. Logic('vr_order')->changeOrderStateSuccess($order_id);//更新订单状态
  104. }
  105. }
  106. $this->commit();
  107. return $state;
  108. } catch (Exception $e) {
  109. $this->rollback();
  110. return false;
  111. }
  112. }
  113. /**
  114. * 修改退款
  115. *
  116. * @param
  117. * @return bool
  118. */
  119. public function editRefund($condition, $data) {
  120. if (empty($condition)) {
  121. return false;
  122. }
  123. if (is_array($data)) {
  124. $result = $this->table('vr_refund')->where($condition)->update($data);
  125. return $result;
  126. } else {
  127. return false;
  128. }
  129. }
  130. /**
  131. * 退款编号
  132. *
  133. * @param
  134. * @return array
  135. */
  136. public function getRefundsn($store_id) {
  137. $result = mt_rand(100,999).substr(500+$store_id,-3).date('ymdHis');
  138. return $result;
  139. }
  140. /**
  141. * 退款记录
  142. *
  143. * @param
  144. * @return array
  145. */
  146. public function getRefundList($condition = array(), $page = '', $limit = '', $fields = '*') {
  147. $result = $this->table('vr_refund')->field($fields)->where($condition)->page($page)->limit($limit)->order('refund_id desc')->select();
  148. return $result;
  149. }
  150. /**
  151. * 取得退款记录的数量
  152. * @param array $condition
  153. */
  154. public function getRefundCount($condition) {
  155. $result = $this->table('vr_refund')->where($condition)->count();
  156. return $result;
  157. }
  158. /**
  159. * 详细页右侧订单信息
  160. *
  161. * @param
  162. * @return array
  163. */
  164. public function getRightOrderList($order_condition){
  165. $order_id = $order_condition['order_id'];
  166. $model_vr_order = Model('vr_order');
  167. $order_info = $model_vr_order->getOrderInfo($order_condition);
  168. Tpl::output('order',$order_info);
  169. $order_list = array();
  170. $order_list[$order_id] = $order_info;
  171. $order_list = $model_vr_order->getCodeRefundList($order_list);//没有使用的兑换码列表
  172. $order_info = $order_list[$order_id];
  173. $model_store = Model('store');
  174. $store = $model_store->getStoreInfo(array('store_id' => $order_info['store_id']));
  175. Tpl::output('store',$store);
  176. //显示退款
  177. $order_info['if_refund'] = $model_vr_order->getOrderOperateState('refund',$order_info);
  178. if ($order_info['if_refund']) {
  179. $code_list = $order_info['code_list'];
  180. Tpl::output('code_list',$code_list);
  181. }
  182. return $order_info;
  183. }
  184. /*
  185. * 获得退款的店铺列表
  186. * @param array $complain_list
  187. * @return array
  188. */
  189. public function getRefundStoreList($list) {
  190. $store_ids = array();
  191. if (!empty($list) && is_array($list)) {
  192. foreach ($list as $key => $value) {
  193. $store_ids[] = $value['store_id'];//店铺编号
  194. }
  195. }
  196. $field = 'store_id,store_name,member_id,member_name,seller_name,store_company_name,store_qq,store_ww,store_phone,store_domain';
  197. return Model('store')->getStoreMemberIDList($store_ids, $field);
  198. }
  199. /**
  200. * 向模板页面输出退款状态
  201. *
  202. * @param
  203. * @return array
  204. */
  205. public function getRefundStateArray($type = 'all') {
  206. $admin_array = array(
  207. '1' => '待审核',
  208. '2' => '同意',
  209. '3' => '不同意'
  210. );//退款状态:1为待审核,2为同意,3为不同意
  211. Tpl::output('admin_array', $admin_array);
  212. $state_data = array(
  213. 'admin' => $admin_array
  214. );
  215. if ($type == 'all') return $state_data;//返回所有
  216. return $state_data[$type];
  217. }
  218. }