order_helper.php 32 KB

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