order_helper.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/9/28
  6. * Time: 上午12:09
  7. */
  8. require_once (BASE_ROOT_PATH . '/helper/goods_helper.php');
  9. require_once (BASE_ROOT_PATH . '/helper/account_helper.php');
  10. class refund_item
  11. {
  12. const REFUND_MONEY = 1;
  13. const RETURN_GOODS = 2;
  14. static $seller_state = array(1 => "待审核",2 => "同意", 3 => "不同意");
  15. static $refund_state = array(1 => "处理中",2 => "待管理员处理", 3 => "已完成");
  16. static $refund_type = array(1 => "退款",2 => "退货");
  17. static $return_type = array(1 => "不用退货",2 => "需要退货");
  18. static $order_goods_type = array(1 => "普通商品",2 => "团购商品",3 => "限时折扣商品",4 => "组合套装");
  19. static $goods_state = array(1 => "待发货",2 => "待收货",3 => "未收到",4 => "已收货");
  20. static $payments = array('bonus' => "红包支付","predeposit" => "红包支付","online" => "在线支付",
  21. "alipay" => "支付宝",'wxpay' => "微信支付",'cmbpay' => "一网通支付");
  22. public function __construct()
  23. {
  24. }
  25. static public function get_price($goods_info,$order_id,$goods_id,&$price,&$spec)
  26. {
  27. $price = 0.00;
  28. $spec = "";
  29. if($goods_id <= 0 || $order_id <= 0) {
  30. return false;
  31. }
  32. else
  33. {
  34. if(isset($goods_info[$order_id]))
  35. {
  36. $order_goods = $goods_info[$order_id];
  37. if(isset($order_goods[$goods_id])) {
  38. $goods = $order_goods[$goods_id];
  39. $price = doubleval($goods['goods_price']);
  40. $spec = $goods['goods_spec'];
  41. if(empty($spec)) $spec = "";
  42. }
  43. }
  44. }
  45. }
  46. static public function get_payment($order_infos,$order_id)
  47. {
  48. if($order_id <= 0) {
  49. return "";
  50. }
  51. else
  52. {
  53. if(isset($order_infos[$order_id]))
  54. {
  55. $order = $order_infos[$order_id];
  56. if(isset($order['payment_code'])) {
  57. return self::$payments[$order['payment_code']];
  58. }
  59. }
  60. }
  61. return "";
  62. }
  63. static public function format($info,$goods_info,$order_infos)
  64. {
  65. $result['refund_id'] = intval($info['refund_id']);
  66. $result['order_id'] = intval($info['order_id']);
  67. $result['order_sn'] = $info['order_sn'];
  68. $result['refund_sn'] = $info['refund_sn'];
  69. $result['refund_amount'] = doubleval($info['refund_amount']);
  70. $result['refund_step'] = self::refund_step($info);
  71. $result['refund_type'] = intval($info['refund_type']);
  72. $result['refund_type_desc'] = self::$refund_type[intval($info['refund_type'])];
  73. $result['add_time'] = intval($info['add_time']);
  74. $result['goods_name'] = empty($info['goods_name']) ? "" : $info['goods_name'];
  75. $image = empty($info['goods_image']) ? "" : $info['goods_image'];
  76. $result['goods_image'] = cthumb($image, 480, $info['store_id']);
  77. $result['goods_id'] = intval($info['goods_id']);
  78. $result['rec_id'] = intval($info['order_goods_id']);
  79. $result['goods_num'] = intval($info['goods_num']);
  80. $result['order_goods_type'] = self::$order_goods_type[intval($info['order_goods_type'])];
  81. $result['goods_state'] = self::$goods_state[intval($info['goods_state'])];
  82. self::get_price($goods_info,$result['order_id'],$result['goods_id'],$price,$spec);
  83. $result['goods_price'] = $price;
  84. $result['goods_spec'] = $spec;
  85. $result['payment_desc'] = self::get_payment($order_infos,$result['order_id']);
  86. $result['seller_state'] = self::$seller_state[intval($info['seller_state'])];
  87. $result['refund_state'] = self::$refund_state[intval($info['refund_state'])];
  88. $result['buyer_message'] = empty($info['buyer_message']) ? "" : $info['buyer_message'];
  89. $result['seller_message'] = empty($info['seller_message']) ? "" : $info['seller_message'];
  90. $result['admin_message'] = empty($info['admin_message']) ? "" : $info['admin_message'];
  91. //$result['return_type'] = intval($info['return_type']); //'退货类型:1为不用退货,2为需要退货,默认为1'
  92. //$result['order_lock'] = intval($info['order_lock']); //'订单锁定类型:1为不用锁定,2为需要锁定,默认为1',
  93. //$result['seller_time'] = intval($info['seller_time']);
  94. //$result['admin_time'] = intval($info['admin_time']);
  95. //$result['ship_time'] = intval($info['ship_time']);
  96. //$result['reason_id'] = intval($info['reason_id']);
  97. $result['shipping_code'] = empty($info['invoice_no']) ? "" : $info['invoice_no']; //物流编码
  98. $result['express_id'] = intval($info['express_id']);
  99. $result['delay_time'] = intval($info['delay_time']);
  100. $result['receive_time'] = intval($info['receive_time']);
  101. $result['receive_message'] = empty($info['receive_message']) ? "" : $info['receive_message'];
  102. $result['reason_info'] = empty($info['reason_info']) ? "" : $info['reason_info'];
  103. return $result;
  104. }
  105. private static function refund_step($info)
  106. {
  107. $state = 1;
  108. $type = intval($info['refund_type']);
  109. $seller_time = intval($info['seller_time']);
  110. $admin_time = intval($info['admin_time']);
  111. $ship_time = intval($info['ship_time']);
  112. $return_type = intval($info['return_type']);
  113. if($type == self::REFUND_MONEY)
  114. {
  115. if($seller_time > 0) {
  116. $state = 2;
  117. }
  118. if($admin_time > 0) {
  119. $state = 3;
  120. }
  121. }
  122. else if($type == self::RETURN_GOODS)
  123. {
  124. if($seller_time > 0) {
  125. $state = 2;
  126. }
  127. if($ship_time > 0 || $return_type == 1) {
  128. $state = 3;
  129. }
  130. if($admin_time > 0) {
  131. $state = 4;
  132. }
  133. }
  134. else
  135. {
  136. }
  137. return $state;
  138. }
  139. }
  140. class refund_helper
  141. {
  142. private $member_id;
  143. public function __construct($member_id)
  144. {
  145. $this->member_id = intval($member_id);
  146. }
  147. private function format($refund_list,$goods_info,$order_infos)
  148. {
  149. $result = [];
  150. foreach ($refund_list as $item) {
  151. $result[] = refund_item::format($item,$goods_info,$order_infos);
  152. }
  153. return $result;
  154. }
  155. private function goods_info($refund_list,&$order_infos)
  156. {
  157. $ids = [];
  158. foreach ($refund_list as $item) {
  159. $ids[] = intval($item['order_id']);
  160. }
  161. if(empty($ids)) return false;
  162. $model_order = Model('order');
  163. $order_list = $model_order->getNormalOrderList(array('order_id' => array('in',$ids)), $this->page_size, '*', 'order_id desc', '', array('order_goods'));
  164. $order_infos = [];
  165. $result = [];
  166. foreach ($order_list as $order)
  167. {
  168. $order_id = intval($order['order_id']);
  169. $order_goods = $order['extend_order_goods'];
  170. $ex_goods = [];
  171. foreach ($order_goods as $goods) {
  172. $goods_id = intval($goods['goods_id']);
  173. $ex_goods[$goods_id] = $goods;
  174. }
  175. $result[$order_id] = $ex_goods;
  176. $order_infos[$order_id]['payment_code'] = $order['payment_code'];
  177. }
  178. return $result;
  179. }
  180. public function list_all($page,&$total_page)
  181. {
  182. $model_refund = Model('refund_return');
  183. $condition = array();
  184. $condition['buyer_id'] = $this->member_id;
  185. $refund_list = $model_refund->getRefundReturnList($condition,$page);
  186. $total_page = $model_refund->gettotalpage();
  187. $goods_info = $this->goods_info($refund_list,$order_infos);
  188. return $this->format($refund_list,$goods_info,$order_infos);
  189. }
  190. public function view($refund_id,&$err)
  191. {
  192. $model_refund = Model('refund_return');
  193. $return_list = $model_refund->getRefundReturnList(array('buyer_id' => $this->member_id,'refund_id' => $refund_id));
  194. if(empty($return_list)) {
  195. $err = array('code' => errcode::ErrRefundNotExist,'msg' => '无此退款信息.');
  196. return false;
  197. }
  198. $goods_info = $this->goods_info($return_list,$order_infos);
  199. $refunds = $this->format($return_list,$goods_info,$order_infos);
  200. return $refunds[0];
  201. }
  202. public function ship($refund_id,$express_id,$invoice_no,&$err)
  203. {
  204. $model_refund = Model('refund_return');
  205. $condition = array();
  206. $condition['buyer_id'] = $this->member_id;
  207. $condition['refund_id'] = $refund_id;
  208. $return_list = $model_refund->getReturnList($condition);
  209. if(empty($return_list)) {
  210. $err = array('code' => errcode::ErrOrderRefundError,'msg' => "无此退款信息");
  211. return false;
  212. }
  213. $return = $return_list[0];
  214. if ($return['seller_state'] != '2' || $return['goods_state'] != '1') {
  215. $err = array('code' => errcode::ErrFrequentlyRequest,'msg' => "该申请已发货");
  216. return false;
  217. }
  218. $refund_array = array();
  219. $refund_array['ship_time'] = time();
  220. $refund_array['delay_time'] = time();
  221. $refund_array['express_id'] = $express_id;
  222. $refund_array['invoice_no'] = $invoice_no;
  223. $refund_array['goods_state'] = '2';
  224. $state = $model_refund->editRefundReturn($condition, $refund_array);
  225. if ($state) {
  226. return true;
  227. } else {
  228. $err = array('code' => errcode::ErrOrderRefundError,'msg' => "发货失败");
  229. return false;
  230. }
  231. }
  232. public function refund($order_sn,&$err)
  233. {
  234. $order_info = Model('order')->getOrderInfo(array('order_sn' => $order_sn));
  235. if (empty($order_info)) {
  236. $err = array("code" => errcode::ErrOrderNotExist,'msg' => errcode::msg(errcode::ErrOrderNotExist));
  237. return false;
  238. }
  239. $model_refund = Model('refund_return');
  240. $order_id = intval($order_info['order_id']);
  241. $order = $model_refund->getRightOrderList(array('buyer_id' => $this->member_id, 'order_id' => $order_id));
  242. $order_amount = $order['order_amount'];//订单金额
  243. $pd_amount = $order['pd_amount'];
  244. $order_amount -= $pd_amount;
  245. $condition = array();
  246. {
  247. $condition['buyer_id'] = $order['buyer_id'];
  248. $condition['order_id'] = $order['order_id'];
  249. $condition['goods_id'] = '0';
  250. $condition['seller_state'] = array('lt', '3'); //状态:1为待审核,2为同意,3为不同意
  251. }
  252. $refund_list = $model_refund->getRefundReturnList($condition);
  253. $refund = array();
  254. if (!empty($refund_list) && is_array($refund_list)) {
  255. $refund = $refund_list[0];
  256. }
  257. $model_trade = Model('trade');
  258. $order_paid = $model_trade->getOrderState('order_paid');
  259. $payment_code = $order['payment_code'];
  260. if ($refund['refund_id'] > 0 || $order['order_state'] != $order_paid || $payment_code == 'offline') {
  261. $err = array("code" => errcode::ErrOrderNotExist,'msg' => "只有付完款尚未发货的时才能退款。");
  262. return false;
  263. }
  264. $refund_array = array();
  265. $refund_array['refund_type'] = '1'; //类型:1为退款,2为退货
  266. $refund_array['seller_state'] = '1';//状态:1为待审核,2为同意,3为不同意
  267. $refund_array['order_lock'] = '2'; //锁定类型:1为不用锁定,2为需要锁定
  268. $refund_array['goods_id'] = '0';
  269. $refund_array['order_goods_id'] = '0';
  270. $refund_array['reason_id'] = '0';
  271. $refund_array['reason_info'] = '取消订单,全部退款';
  272. $refund_array['goods_name'] = '订单商品全部退款';
  273. $refund_array['refund_amount'] = ncPriceFormat($order_amount);
  274. $refund_array['buyer_message'] = remove_tags(urldecode($_POST['buyer_message']));
  275. $refund_array['add_time'] = time();
  276. $pic_array = array();
  277. $pic_array['buyer'] = array();
  278. $refund_array['pic_info'] = serialize($pic_array);
  279. $refund_id = $model_refund->addRefundReturn($refund_array, $order);
  280. if ($refund_id) {
  281. $model_refund->editOrderLock($order_id);
  282. return $refund_id;
  283. } else {
  284. $err = array("code" => errcode::ErrOrderRefundError, 'msg' => "退款失败.");
  285. return false;
  286. }
  287. }
  288. private function reason_list($model_refund)
  289. {
  290. $condition = array();
  291. $reason_data = $model_refund->getReasonList($condition, '', '', 'reason_id,reason_info');
  292. $reason_list = [];
  293. foreach ($reason_data as $data) {
  294. $reason_list[] = $data;
  295. }
  296. $reason_list[] = array('reason_id' => 0, 'reason_info' => '其他');
  297. return $reason_list;
  298. }
  299. private function reason_info($model_refund,$reason_id)
  300. {
  301. $reason_list = $this->reason_list($model_refund);
  302. $reasons = [];
  303. foreach ($reason_list as $data) {
  304. $reason_id = intval($data['reason_id']);
  305. $reason_info = $data['reason_info'];
  306. $reasons[$reason_id] = $reason_info;
  307. }
  308. if (isset($reasons[$reason_id])) {
  309. return $reasons[$reason_id];
  310. } else {
  311. return '其他';
  312. }
  313. }
  314. public function return_info($order_sn,$rec_id,&$err)
  315. {
  316. $order_info = Model('order')->getOrderInfo(array('order_sn' => $order_sn));
  317. if (empty($order_info)) {
  318. $err = array("code" => errcode::ErrOrderNotExist,'msg' => errcode::msg(errcode::ErrOrderNotExist));
  319. return false;
  320. }
  321. $order_id = intval($order_info['order_id']);
  322. if ($order_id < 1 || $rec_id < 1) {
  323. $err = array("code" => errcode::ErrParamter,'msg' => errcode::msg(errcode::ErrParamter));
  324. return false;
  325. }
  326. $model_refund = Model('refund_return');
  327. $reason_list = $this->reason_list($model_refund);
  328. $condition = array();
  329. $condition['buyer_id'] = $this->member_id;
  330. $condition['order_id'] = $order_id;
  331. $order = $model_refund->getRightOrderList($condition, $rec_id);
  332. $order_amount = $order['order_amount'];//订单金额
  333. $order_refund_amount = $order['refund_amount'];//订单退款金额
  334. $goods_list = $order['goods_list'];
  335. $goods = $goods_list[0];
  336. $goods_pay_price = $goods['goods_pay_price'];//商品实际成交价
  337. if ($order_amount < ($goods_pay_price + $order_refund_amount)) {
  338. $goods_pay_price = $order_amount - $order_refund_amount;
  339. $goods['goods_pay_price'] = $goods_pay_price;
  340. }
  341. $condition = array();
  342. $condition['buyer_id'] = $order['buyer_id'];
  343. $condition['order_id'] = $order['order_id'];
  344. $condition['order_goods_id'] = $rec_id;
  345. $condition['seller_state'] = array('lt', '3');
  346. $refund_list = $model_refund->getRefundReturnList($condition);
  347. $refund = array();
  348. if (!empty($refund_list) && is_array($refund_list)) {
  349. $refund = $refund_list[0];
  350. }
  351. $refund_state = $model_refund->getRefundState($order);//根据订单状态判断是否可以退款退货 '申请状态:1为处理中,2为待管理员处理,3为已完成,默认为1',
  352. if ($refund['refund_id'] > 0) {
  353. $err = array("code" => errcode::ErrOrderRefundError,'msg' => "不能重复退货");
  354. return false;
  355. }
  356. if ($refund_state != 1) {
  357. $err = array("code" => errcode::ErrOrderState,'msg' => '请确认订单状态,在已经付款后才能退款,已经发货后才能退货.');
  358. return false;
  359. }
  360. return array('reason_list' => (array)$reason_list, 'goods' => $goods);
  361. }
  362. public function return_goods($order_sn,$rec_id,$goods_num,$refund_amount,$reason_id,$buyer_msg,&$err)
  363. {
  364. $order_info = Model('order')->getOrderInfo(array('order_sn' => $order_sn));
  365. if (empty($order_info) || intval($order_info['order_id'] < 1)) {
  366. $err = array("code" => errcode::ErrOrderNotExist,'msg' => errcode::msg(errcode::ErrOrderNotExist));
  367. return false;
  368. }
  369. $order_id = intval($order_info['order_id']);
  370. $model_refund = Model('refund_return');
  371. $reson_info = $this->reason_info($model_refund,$reason_id);
  372. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  373. $condition = array();
  374. $condition['buyer_id'] = $this->member_id;
  375. $condition['order_id'] = $order_id;
  376. $order = $model_refund->getRightOrderList($condition, $rec_id);
  377. $order_id = $order['order_id'];
  378. $order_amount = $order['order_amount'];//订单金额
  379. $order_refund_amount = $order['refund_amount'];//订单退款金额
  380. $goods_list = $order['goods_list'];
  381. $goods = $goods_list[0];
  382. $goods_pay_price = $goods['goods_pay_price'];//商品实际成交价
  383. if ($order_amount < ($goods_pay_price + $order_refund_amount)) {
  384. $goods_pay_price = $order_amount - $order_refund_amount;
  385. $goods['goods_pay_price'] = $goods_pay_price;
  386. }
  387. $goods_id = $goods['rec_id'];
  388. $condition = array();
  389. $condition['buyer_id'] = $order['buyer_id'];
  390. $condition['order_id'] = $order['order_id'];
  391. $condition['order_goods_id'] = $goods_id;
  392. $condition['seller_state'] = array('lt', '3');
  393. $refund_list = $model_refund->getRefundReturnList($condition);
  394. $refund = array();
  395. if (!empty($refund_list) && is_array($refund_list)) {
  396. $refund = $refund_list[0];
  397. }
  398. $refund_state = $model_refund->getRefundState($order);
  399. if ($refund['refund_id'] > 0 || $refund_state != 1) {
  400. $err = array("code" => errcode::ErrOrderState,'msg' => errcode::msg(errcode::ErrOrderState));
  401. return false;
  402. }
  403. $refund_array = array();
  404. if (($refund_amount < 0) || ($refund_amount > $goods_pay_price)) {
  405. $refund_amount = $goods_pay_price;
  406. }
  407. if (($goods_num < 0) || ($goods_num > $goods['goods_num'])) {
  408. $goods_num = 1;
  409. }
  410. $refund_array['reason_info'] = $reson_info;
  411. $refund_array['reason_id'] = $reason_id;
  412. $refund_array['pic_info'] = serialize(array('buyer' => array()));
  413. $model_trade = Model('trade');
  414. $order_shipped = $model_trade->getOrderState('order_shipped');//订单状态30:已发货
  415. if ($order['order_state'] == $order_shipped) {
  416. $refund_array['order_lock'] = '2';//锁定类型:1为不用锁定,2为需要锁定
  417. }
  418. $refund_array['refund_type'] = 2;//类型:1为退款,2为退货
  419. $refund_array['return_type'] = '2';//退货类型:1为不用退货,2为需要退货
  420. if ($refund_array['refund_type'] != '2') {
  421. $refund_array['refund_type'] = '1';
  422. $refund_array['return_type'] = '1';
  423. }
  424. $refund_array['seller_state'] = '1';//状态:1为待审核,2为同意,3为不同意
  425. $refund_array['refund_amount'] = ncPriceFormat($refund_amount);
  426. $refund_array['goods_num'] = $goods_num;
  427. $refund_array['buyer_message'] = $buyer_msg;
  428. $refund_array['add_time'] = time();
  429. $refund_id = $model_refund->addRefundReturn($refund_array, $order, $goods);
  430. if ($refund_id)
  431. {
  432. if ($order['order_state'] == $order_shipped) {
  433. $model_refund->editOrderLock($order_id);
  434. }
  435. return true;
  436. }
  437. else {
  438. $err = array("code" => errcode::ErrOrderRefundError,'msg' => "退货失败");
  439. return false;
  440. }
  441. }
  442. }
  443. class order_action
  444. {
  445. public function change_state($state_type,$order_id)
  446. {
  447. $model_order = Model('order');
  448. $condition = array();
  449. $condition['order_id'] = $order_id;
  450. $condition['buyer_id'] = $_SESSION['member_id'];
  451. $order_info = $model_order->getOrderInfo($condition);
  452. if($state_type == 'order_cancel') {
  453. $result = $this->order_cancel($order_info);
  454. } else if ($state_type == 'order_receive') {
  455. $result = $this->order_receive($order_info);
  456. } else if (in_array($state_type,array('order_delete','order_drop','order_restore'))){
  457. $result = $this->order_recycle($order_info, $state_type);
  458. } else {
  459. return false;
  460. }
  461. return $result['state'];
  462. }
  463. private function order_cancel($order_info)
  464. {
  465. $model_order = Model('order');
  466. $logic_order = Logic('order');
  467. $if_allow = $model_order->getOrderOperateState('buyer_cancel',$order_info);
  468. if (!$if_allow) {
  469. return callback(false,'无权操作');
  470. }
  471. $ret = $logic_order->changeOrderStateCancel($order_info,'buyer', $_SESSION['member_name']);
  472. if($ret['state']) {
  473. account_helper::onPredeposit('order_cancel',$order_info['buyer_id'],$order_info['order_sn']);
  474. } else {
  475. Log::record('用户取消订单失败SN:' . $order_info['order_sn'],Log::ERR);
  476. }
  477. return $ret;
  478. }
  479. private function order_receive($order_info)
  480. {
  481. $model_order = Model('order');
  482. $logic_order = Logic('order');
  483. $if_allow = $model_order->getOrderOperateState('receive',$order_info);
  484. if (!$if_allow) {
  485. return callback(false,'无权操作');
  486. }
  487. return $logic_order->changeOrderStateReceive($order_info,'buyer',$_SESSION['member_name']);
  488. }
  489. private function order_recycle($order_info, $state_type) {
  490. $model_order = Model('order');
  491. $logic_order = Logic('order');
  492. $state_type = str_replace(array('order_delete','order_drop','order_restore'), array('delete','drop','restore'), $state_type);
  493. $if_allow = $model_order->getOrderOperateState($state_type,$order_info);
  494. if (!$if_allow) {
  495. return callback(false,'无权操作');
  496. }
  497. return $logic_order->changeOrderStateRecycle($order_info,'buyer',$state_type);
  498. }
  499. }
  500. class order_helper
  501. {
  502. private $mOrders;
  503. private $mod_refund;
  504. private $mod_order;
  505. public function __construct($order_list)
  506. {
  507. $this->mod_order = Model('order');
  508. $this->mod_refund = Model('refund_return');
  509. $this->mOrders = $this->mod_refund->getGoodsRefundList($order_list);
  510. }
  511. private function action_title($key)
  512. {
  513. static $actions = array("if_cancel" => "取消订单",
  514. "if_refund_cancel" => "退款",
  515. "if_payment" => "立即支付",
  516. "if_receive" => "确认收货",
  517. "if_deliver" => "查看物流",
  518. "if_evaluation" => "评价赚红包",
  519. "if_delete" => "删除订单",
  520. "if_lock" => "退款退货中");
  521. return $actions[$key];
  522. }
  523. private function actions($order)
  524. {
  525. $state['if_payment'] = $this->mod_order->getOrderOperateState('payment',$order);
  526. //显示取消订单
  527. $state['if_cancel'] = $this->mod_order->getOrderOperateState('buyer_cancel', $order);
  528. //显示退款取消订单
  529. $state['if_refund_cancel'] = $this->mod_order->getOrderOperateState('refund_cancel', $order);
  530. //显示收货
  531. $state['if_receive'] = $this->mod_order->getOrderOperateState('receive', $order);
  532. //显示物流跟踪
  533. $state['if_deliver'] = $this->mod_order->getOrderOperateState('deliver', $order);
  534. //显示评价
  535. $state['if_evaluation'] = $this->mod_order->getOrderOperateState('evaluation', $order);
  536. //显示删除
  537. $state['if_delete'] = $this->mod_order->getOrderOperateState('delete', $order);
  538. $state['if_lock'] = $this->mod_order->getOrderOperateState('lock', $order);
  539. $actions = [];
  540. foreach ($state as $key => $value) {
  541. if ($value == true) {
  542. $item['action'] = $key;
  543. $item['title'] = $this->action_title($key);
  544. $actions[] = $item;
  545. }
  546. }
  547. return $actions;
  548. }
  549. private function payment_desc($payment_code)
  550. {
  551. static $payments = array('bonus' => "红包支付","predeposit" => "红包支付","online" => "在线支付",
  552. "alipay" => "支付宝",'wxpay' => "微信支付",'cmbpay' => "一网通银行卡支付");
  553. if(isset($payments[$payment_code])) {
  554. return $payments[$payment_code];
  555. } else {
  556. return "";
  557. }
  558. }
  559. private function order_info($order)
  560. {
  561. $result["order_id"] = intval($order["order_id"]);
  562. $result["order_sn"] = $order["order_sn"];
  563. $result["pay_sn"] = $order["pay_sn"];
  564. $result["add_time"] = intval($order["add_time"]);
  565. $result["payment_code"] = $order["payment_code"];
  566. $result["payment_desc"] = $this->payment_desc($order["payment_code"]);
  567. $result["payment_time"] = intval($order["payment_time"]);
  568. $result["finnshed_time"] = intval($order["finnshed_time"]);
  569. $result["goods_amount"] = $this->goods_amount($order);//doubleval($order["goods_amount"]);
  570. $result["order_amount"] = doubleval($order["order_amount"]);
  571. $result["rcb_amount"] = doubleval($order["rcb_amount"]);
  572. $result["pd_amount"] = doubleval($order["pd_amount"]);
  573. $result['bonus_rates'] = $this->formate_rates(unserialize($order["bonus_rate"]),doubleval($order["pd_amount"]));
  574. $result["shipping_fee"] = doubleval($order["shipping_fee"]);
  575. $result["evaluation_state"] = intval($order["evaluation_state"]);
  576. $result["order_state"] = intval($order["order_state"]);
  577. $result["refund_state"] = intval($order["refund_state"]);
  578. $result["refund_amount"] = doubleval($order["refund_amount"]);
  579. $result["delay_time"] = intval($order["delay_time"]);
  580. $result["shipping_code"] = empty($order["shipping_code"]) ? "" : $order["shipping_code"];
  581. $result["trade_no"] = $order["trade_no"];
  582. $result["state_desc"] = $order["state_desc"];
  583. $result['pay_cash'] = $result["order_amount"] - $result["pd_amount"];
  584. $result['full_discount'] = $result["goods_amount"] - $order["goods_amount"];
  585. $result['full_desc'] = $this->full_desc($order);
  586. if ($result['order_state'] == ORDER_STATE_NEW) {
  587. $result['cancel_time'] = $result['add_time'] + ORDER_AUTO_CANCEL_DAY * 24 * 3600;
  588. } else {
  589. $result['cancel_time'] = 0;
  590. }
  591. $refund_state = 0;
  592. $order['refund_id'] = intval($this->mod_refund->getRefundId($order,$refund_state));
  593. $result["refund_state"] = intval($refund_state);
  594. return $result;
  595. }
  596. private function formate_rates($rates,$pd_amount)
  597. {
  598. $result = [];
  599. if(empty($rates))
  600. {
  601. if(intval($pd_amount * 100 + 0.5) > 0) {
  602. $item['rate'] = 30;
  603. $item['total'] = $pd_amount;
  604. $result[] = $item;
  605. } else {
  606. return null;
  607. }
  608. }
  609. else
  610. {
  611. foreach ($rates as $rate => $val) {
  612. $item['rate'] = $rate;
  613. $item['total'] = $val;
  614. $result[] = $item;
  615. }
  616. }
  617. return $result;
  618. }
  619. private function full_desc($order)
  620. {
  621. $order_info = $order['extend_order_common'];
  622. $info = '';
  623. if(isset($order_info['promotion_info']))
  624. {
  625. $info = $order_info['promotion_info'];
  626. $ret = preg_match('#(.*?)<a.*?title=("|\')(.*?)\2.*?>.*?</a>#s',$info,$matches);
  627. if($ret != false && count($matches) == 4) {
  628. $info = $matches[1] . $matches[3];
  629. }
  630. }
  631. return (is_null($info) || empty($info)) ? "" : $info;
  632. }
  633. private function goods_amount($order)
  634. {
  635. $goods_amount = doubleval($order['goods_amount']);
  636. $goods_list = $order['extend_order_goods'];
  637. if(empty($goods_list) || !is_array($goods_list)) {
  638. return $goods_amount;
  639. } else {
  640. $goods_amount = 0.00;
  641. }
  642. foreach ($goods_list as $goods) {
  643. $goods_amount += doubleval($goods['goods_price']) * intval($goods['goods_num']);
  644. }
  645. return $goods_amount;
  646. }
  647. private function order_goods($order)
  648. {
  649. $result = [];
  650. $order_goods = $order['extend_order_goods'];
  651. foreach ($order_goods as $val)
  652. {
  653. $goods["rec_id"] = intval($val["rec_id"]);
  654. $goods["order_id"] = intval($val["order_id"]);
  655. $goods["goods_id"] = intval($val["goods_id"]);
  656. $commonid = goods_helper::common_id($goods["goods_id"]);
  657. if($commonid == false) {
  658. continue;
  659. }
  660. $goods["goods_commonid"] = $commonid;
  661. $goods["goods_name"] = $val["goods_name"];
  662. $goods["goods_price"] = doubleval($val["goods_price"]);
  663. $goods["goods_num"] = intval($val["goods_num"]);
  664. $goods["goods_image"] = cthumb($val['goods_image'], 480, $val['store_id']);
  665. $goods["goods_pay_price"] = doubleval($val["goods_pay_price"]);
  666. $goods["store_id"] = intval($val["store_id"]);
  667. $goods["buyer_id"] = intval($val["buyer_id"]);
  668. $goods["goods_type"] = intval($val["goods_type"]);
  669. $goods["type_desc"] = $this->type_desc($goods["goods_type"]);
  670. $goods["promotions_id"] = intval($val["promotions_id"]);
  671. $goods["gc_id"] = intval($val["gc_id"]);
  672. $goods["goods_spec"] = $val["goods_spec"] == null ? "" : $val["goods_spec"];
  673. $goods["refund"] = isset($val["refund"]) ? intval($val["refund"]) : 0;
  674. $refund_state = 0;
  675. $goods['refund_id'] = intval($this->mod_refund->getRefundIdForGood($val,$refund_state));
  676. $goods['refund_state'] = intval($refund_state);
  677. $result[] = $goods;
  678. }
  679. return $result;
  680. }
  681. private function type_desc($goods_type)
  682. {
  683. $descs = array(1 => "普通商品",2 => "抢购", 3 => "限购", 4 => "套装", 5 => "赠品");
  684. if($goods_type > 0 && $goods_type <= count($descs)) {
  685. return $descs[$goods_type];
  686. } else {
  687. return "";
  688. }
  689. }
  690. private function reciver_info($order)
  691. {
  692. $info = $order['reciver_info'];
  693. $info['reciver_name'] = $order['reciver_name'];
  694. return $info;
  695. }
  696. private function invoice_info($order)
  697. {
  698. $result = [];
  699. $result['inv_content'] = "";
  700. $result['inv_title'] = "";
  701. $result['inv_id'] = 0;
  702. if(isset($order['extend_order_common']))
  703. {
  704. $order_common = $order['extend_order_common'];
  705. if(isset($order_common['invoice_info']))
  706. {
  707. $invoice_info = $order_common['invoice_info'];
  708. foreach ($invoice_info as $key => $value)
  709. {
  710. if($key == '内容') {
  711. $result['inv_content'] = $value;
  712. }
  713. elseif($key == '抬头') {
  714. $item['inv_title'] = $value;
  715. }
  716. }
  717. }
  718. }
  719. return $result;
  720. }
  721. public function format()
  722. {
  723. $result = [];
  724. foreach ($this->mOrders as $order) {
  725. $item['order_info'] = $this->order_info($order);
  726. $actions = $this->actions($order);
  727. $item['actions'] = $actions;
  728. $order_goods = $this->order_goods($order);
  729. if($order_goods == false) {
  730. continue;
  731. }
  732. $item['order_goods'] = $order_goods;
  733. $item['reciver_info'] = $this->reciver_info($order);
  734. $item['invoice_info'] = $this->invoice_info($order);
  735. $result[] = $item;
  736. }
  737. return $result;
  738. }
  739. }