order_helper.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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. $bonus_amount = unserialize($order['bonus_amount']);
  245. $user_bonus = floatval($bonus_amount['user_bonus']);
  246. $room_bonus = floatval($bonus_amount['room_bonus']);
  247. $pd_amount = $order['pd_amount'];
  248. $order_amount -= $user_bonus + $room_bonus;
  249. $condition = array();
  250. {
  251. $condition['buyer_id'] = $order['buyer_id'];
  252. $condition['order_id'] = $order['order_id'];
  253. $condition['goods_id'] = '0';
  254. $condition['seller_state'] = array('lt', '3'); //状态:1为待审核,2为同意,3为不同意
  255. }
  256. $refund_list = $model_refund->getRefundReturnList($condition);
  257. $refund = array();
  258. if (!empty($refund_list) && is_array($refund_list)) {
  259. $refund = $refund_list[0];
  260. }
  261. $model_trade = Model('trade');
  262. $order_paid = $model_trade->getOrderState('order_paid');
  263. $payment_code = $order['payment_code'];
  264. if ($refund['refund_id'] > 0 || $order['order_state'] != $order_paid || $payment_code == 'offline') {
  265. $err = array("code" => errcode::ErrOrderNotExist,'msg' => "只有付完款尚未发货的时才能退款。");
  266. return false;
  267. }
  268. $refund_array = array();
  269. $refund_array['refund_type'] = '1'; //类型:1为退款,2为退货
  270. $refund_array['seller_state'] = '1';//状态:1为待审核,2为同意,3为不同意
  271. $refund_array['order_lock'] = '2'; //锁定类型:1为不用锁定,2为需要锁定
  272. $refund_array['goods_id'] = '0';
  273. $refund_array['order_goods_id'] = '0';
  274. $refund_array['reason_id'] = '0';
  275. $refund_array['reason_info'] = '取消订单,全部退款';
  276. $refund_array['goods_name'] = '订单商品全部退款';
  277. $refund_array['refund_amount'] = ncPriceFormat($order_amount);
  278. $refund_array['buyer_message'] = remove_tags(urldecode($_POST['buyer_message']));
  279. $refund_array['add_time'] = time();
  280. $pic_array = array();
  281. $pic_array['buyer'] = array();
  282. $refund_array['pic_info'] = serialize($pic_array);
  283. $refund_id = $model_refund->addRefundReturn($refund_array, $order);
  284. if ($refund_id) {
  285. QueueClient::push("reset_fcode",['pay_sn' => $order_info['pay_sn']]);
  286. $model_refund->editOrderLock($order_id);
  287. return $refund_id;
  288. } else {
  289. $err = array("code" => errcode::ErrOrderRefundError, 'msg' => "退款失败.");
  290. return false;
  291. }
  292. }
  293. private function reason_list($model_refund)
  294. {
  295. $condition = array();
  296. $reason_data = $model_refund->getReasonList($condition, '', '', 'reason_id,reason_info');
  297. $reason_list = [];
  298. foreach ($reason_data as $data) {
  299. $reason_list[] = $data;
  300. }
  301. $reason_list[] = array('reason_id' => 0, 'reason_info' => '其他');
  302. return $reason_list;
  303. }
  304. private function reason_info($model_refund,$reason_id)
  305. {
  306. $reason_list = $this->reason_list($model_refund);
  307. $reasons = [];
  308. foreach ($reason_list as $data) {
  309. $reason_id = intval($data['reason_id']);
  310. $reason_info = $data['reason_info'];
  311. $reasons[$reason_id] = $reason_info;
  312. }
  313. if (isset($reasons[$reason_id])) {
  314. return $reasons[$reason_id];
  315. } else {
  316. return '其他';
  317. }
  318. }
  319. public function return_info($order_sn,$rec_id,&$err)
  320. {
  321. $order_info = Model('order')->getOrderInfo(array('order_sn' => $order_sn));
  322. if (empty($order_info)) {
  323. $err = array("code" => errcode::ErrOrderNotExist,'msg' => errcode::msg(errcode::ErrOrderNotExist));
  324. return false;
  325. }
  326. $order_id = intval($order_info['order_id']);
  327. if ($order_id < 1 || $rec_id < 1) {
  328. $err = array("code" => errcode::ErrParamter,'msg' => errcode::msg(errcode::ErrParamter));
  329. return false;
  330. }
  331. $model_refund = Model('refund_return');
  332. $reason_list = $this->reason_list($model_refund);
  333. $condition = array();
  334. $condition['buyer_id'] = $this->member_id;
  335. $condition['order_id'] = $order_id;
  336. $order = $model_refund->getRightOrderList($condition, $rec_id);
  337. $order_amount = $order['order_amount'];//订单金额
  338. $order_refund_amount = $order['refund_amount'];//订单退款金额
  339. $goods_list = $order['goods_list'];
  340. $goods = $goods_list[0];
  341. $goods_pay_price = $goods['goods_pay_price'];//商品实际成交价
  342. if ($order_amount < ($goods_pay_price + $order_refund_amount)) {
  343. $goods_pay_price = $order_amount - $order_refund_amount;
  344. $goods['goods_pay_price'] = $goods_pay_price;
  345. }
  346. $condition = array();
  347. $condition['buyer_id'] = $order['buyer_id'];
  348. $condition['order_id'] = $order['order_id'];
  349. $condition['order_goods_id'] = $rec_id;
  350. $condition['seller_state'] = array('lt', '3');
  351. $refund_list = $model_refund->getRefundReturnList($condition);
  352. $refund = array();
  353. if (!empty($refund_list) && is_array($refund_list)) {
  354. $refund = $refund_list[0];
  355. }
  356. $refund_state = $model_refund->getRefundState($order);//根据订单状态判断是否可以退款退货 '申请状态:1为处理中,2为待管理员处理,3为已完成,默认为1',
  357. if ($refund['refund_id'] > 0) {
  358. $err = array("code" => errcode::ErrOrderRefundError,'msg' => "不能重复退货");
  359. return false;
  360. }
  361. if ($refund_state != 1) {
  362. $err = array("code" => errcode::ErrOrderState,'msg' => '请确认订单状态,在已经付款后才能退款,已经发货后才能退货.');
  363. return false;
  364. }
  365. return array('reason_list' => (array)$reason_list, 'goods' => $goods);
  366. }
  367. public function return_goods($order_sn,$rec_id,$goods_num,$refund_amount,$reason_id,$buyer_msg,&$err)
  368. {
  369. $order_info = Model('order')->getOrderInfo(array('order_sn' => $order_sn));
  370. if (empty($order_info) || intval($order_info['order_id'] < 1)) {
  371. $err = array("code" => errcode::ErrOrderNotExist,'msg' => errcode::msg(errcode::ErrOrderNotExist));
  372. return false;
  373. }
  374. $order_id = intval($order_info['order_id']);
  375. $model_refund = Model('refund_return');
  376. $reson_info = $this->reason_info($model_refund,$reason_id);
  377. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  378. $condition = array();
  379. $condition['buyer_id'] = $this->member_id;
  380. $condition['order_id'] = $order_id;
  381. $order = $model_refund->getRightOrderList($condition, $rec_id);
  382. $order_id = $order['order_id'];
  383. $order_amount = $order['order_amount'];//订单金额
  384. $order_refund_amount = $order['refund_amount'];//订单退款金额
  385. $goods_list = $order['goods_list'];
  386. $goods = $goods_list[0];
  387. $goods_pay_price = $goods['goods_pay_price'];//商品实际成交价
  388. if ($order_amount < ($goods_pay_price + $order_refund_amount)) {
  389. $goods_pay_price = $order_amount - $order_refund_amount;
  390. $goods['goods_pay_price'] = $goods_pay_price;
  391. }
  392. $goods_id = $goods['rec_id'];
  393. $condition = array();
  394. $condition['buyer_id'] = $order['buyer_id'];
  395. $condition['order_id'] = $order['order_id'];
  396. $condition['order_goods_id'] = $goods_id;
  397. $condition['seller_state'] = array('lt', '3');
  398. $refund_list = $model_refund->getRefundReturnList($condition);
  399. $refund = array();
  400. if (!empty($refund_list) && is_array($refund_list)) {
  401. $refund = $refund_list[0];
  402. }
  403. $refund_state = $model_refund->getRefundState($order);
  404. if ($refund['refund_id'] > 0 || $refund_state != 1) {
  405. $err = array("code" => errcode::ErrOrderState,'msg' => errcode::msg(errcode::ErrOrderState));
  406. return false;
  407. }
  408. $refund_array = array();
  409. if (($refund_amount < 0) || ($refund_amount > $goods_pay_price)) {
  410. $refund_amount = $goods_pay_price;
  411. }
  412. if (($goods_num < 0) || ($goods_num > $goods['goods_num'])) {
  413. $goods_num = 1;
  414. }
  415. $refund_array['reason_info'] = $reson_info;
  416. $refund_array['reason_id'] = $reason_id;
  417. $refund_array['pic_info'] = serialize(array('buyer' => array()));
  418. $model_trade = Model('trade');
  419. $order_shipped = $model_trade->getOrderState('order_shipped');//订单状态30:已发货
  420. if ($order['order_state'] == $order_shipped) {
  421. $refund_array['order_lock'] = '2';//锁定类型:1为不用锁定,2为需要锁定
  422. }
  423. $refund_array['refund_type'] = 2;//类型:1为退款,2为退货
  424. $refund_array['return_type'] = '2';//退货类型:1为不用退货,2为需要退货
  425. if ($refund_array['refund_type'] != '2') {
  426. $refund_array['refund_type'] = '1';
  427. $refund_array['return_type'] = '1';
  428. }
  429. $refund_array['seller_state'] = '1';//状态:1为待审核,2为同意,3为不同意
  430. $refund_array['refund_amount'] = ncPriceFormat($refund_amount);
  431. $refund_array['goods_num'] = $goods_num;
  432. $refund_array['buyer_message'] = $buyer_msg;
  433. $refund_array['add_time'] = time();
  434. $refund_id = $model_refund->addRefundReturn($refund_array, $order, $goods);
  435. if ($refund_id)
  436. {
  437. if ($order['order_state'] == $order_shipped) {
  438. $model_refund->editOrderLock($order_id);
  439. }
  440. QueueClient::push("reset_fcode",['pay_sn' => $order_info['pay_sn']]);
  441. return true;
  442. }
  443. else {
  444. $err = array("code" => errcode::ErrOrderRefundError,'msg' => "退货失败");
  445. return false;
  446. }
  447. }
  448. }
  449. class order_action
  450. {
  451. public function change_state($state_type,$order_id)
  452. {
  453. $model_order = Model('order');
  454. $condition = array();
  455. $condition['order_id'] = $order_id;
  456. $condition['buyer_id'] = $_SESSION['member_id'];
  457. $order_info = $model_order->getOrderInfo($condition);
  458. if($state_type == 'order_cancel') {
  459. $result = $this->order_cancel($order_info);
  460. } else if ($state_type == 'order_receive') {
  461. $result = $this->order_receive($order_info);
  462. } else if (in_array($state_type,array('order_delete','order_drop','order_restore'))){
  463. $result = $this->order_recycle($order_info, $state_type);
  464. } else {
  465. return false;
  466. }
  467. return $result['state'];
  468. }
  469. private function order_cancel($order_info)
  470. {
  471. $model_order = Model('order');
  472. $logic_order = Logic('order');
  473. $if_allow = $model_order->getOrderOperateState('buyer_cancel',$order_info);
  474. if (!$if_allow) {
  475. return callback(false,'无权操作');
  476. }
  477. $ret = $logic_order->changeOrderStateCancel($order_info,'buyer', $_SESSION['member_name']);
  478. if($ret['state']) {
  479. QueueClient::push('onBonusChange',['change_type' => 'order_cancel','buyer_id'=>$order_info['buyer_id'],'order_sn'=>$order_info['order_sn']]);
  480. } else {
  481. Log::record('用户取消订单失败SN:' . $order_info['order_sn'],Log::ERR);
  482. }
  483. return $ret;
  484. }
  485. private function order_receive($order_info)
  486. {
  487. $model_order = Model('order');
  488. $logic_order = Logic('order');
  489. $if_allow = $model_order->getOrderOperateState('receive',$order_info);
  490. if (!$if_allow) {
  491. return callback(false,'无权操作');
  492. }
  493. return $logic_order->changeOrderStateReceive($order_info,'buyer',$_SESSION['member_name']);
  494. }
  495. private function order_recycle($order_info, $state_type) {
  496. $model_order = Model('order');
  497. $logic_order = Logic('order');
  498. $state_type = str_replace(array('order_delete','order_drop','order_restore'), array('delete','drop','restore'), $state_type);
  499. $if_allow = $model_order->getOrderOperateState($state_type,$order_info);
  500. if (!$if_allow) {
  501. return callback(false,'无权操作');
  502. }
  503. return $logic_order->changeOrderStateRecycle($order_info,'buyer',$state_type);
  504. }
  505. }
  506. class vorder_action
  507. {
  508. private $mVrModel;
  509. private $mVrLogic;
  510. public function __construct()
  511. {
  512. $this->mVrModel = Model('vr_order');
  513. $this->mVrLogic = Logic('vr_order');
  514. }
  515. public function change_state($state_type,$order_id)
  516. {
  517. $condition = array();
  518. $condition['order_id'] = $order_id;
  519. $condition['buyer_id'] = session_helper::memberid();
  520. $order_info = $this->mVrModel->getOrderInfo($condition);
  521. if($state_type == 'order_cancel') {
  522. $result = $this->order_cancel($order_info);
  523. } else if ($state_type == 'order_receive') {
  524. $result = $this->order_receive($order_info);
  525. } else if (in_array($state_type,array('order_delete','order_drop','order_restore'))){
  526. $result = $this->order_recycle($order_info, $state_type);
  527. } else {
  528. return false;
  529. }
  530. return $result['state'];
  531. }
  532. private function order_cancel($order_info)
  533. {
  534. $if_allow = $this->mVrModel->getOrderOperateState('buyer_cancel',$order_info);
  535. if (!$if_allow) {
  536. return callback(false,'无权操作');
  537. }
  538. $ret = $this->mVrLogic->changeOrderStateCancel($order_info,'buyer', session_helper::memberid());
  539. if($ret['state']) {
  540. QueueClient::push('onBonusChange',['change_type' => 'order_cancel','buyer_id'=>$order_info['buyer_id'],'order_sn'=>$order_info['order_sn']]);
  541. } else {
  542. Log::record('用户取消订单失败SN:' . $order_info['order_sn'],Log::ERR);
  543. }
  544. return $ret;
  545. }
  546. private function order_recycle($order_info, $state_type)
  547. {
  548. $state_type = str_replace(array('order_delete','order_drop','order_restore'), array('delete','drop','restore'), $state_type);
  549. $if_allow = $this->mVrModel->getOrderOperateState($state_type,$order_info);
  550. if (!$if_allow) {
  551. return callback(false,'无权操作');
  552. }
  553. return $this->mVrLogic->changeOrderStateRecycle($order_info,'buyer',$state_type);
  554. }
  555. private function order_receive($order_info)
  556. {
  557. $if_allow = $this->mVrModel->getOrderOperateState('receive',$order_info);
  558. if (!$if_allow) {
  559. return callback(false,'无权操作');
  560. }
  561. return $this->mVrLogic->changeOrderStateReceive($order_info,'buyer',session_helper::memberid());
  562. }
  563. }
  564. class order_base
  565. {
  566. public function __construct() {
  567. }
  568. protected function action_title($key)
  569. {
  570. static $actions = ["if_cancel" => "取消订单",
  571. "if_refund_cancel" => "退款",
  572. "if_payment" => "立即支付",
  573. "if_receive" => "确认收货",
  574. "if_deliver" => "查看物流",
  575. "if_evaluation" => "评价赚红包",
  576. "if_upcert" => "红包兑现",
  577. "if_delete" => "删除订单",
  578. "if_lock" => "退款退货中"];
  579. return $actions[$key];
  580. }
  581. protected function payment_desc($payment_code)
  582. {
  583. static $payments = ['bonus' => "红包支付","predeposit" => "红包支付","online" => "在线支付",
  584. 'alipay' => "支付宝",'wxpay' => "微信支付",'cmbpay' => "一网通银行卡支付"];
  585. if(isset($payments[$payment_code])) {
  586. return $payments[$payment_code];
  587. } else {
  588. return "";
  589. }
  590. }
  591. protected function formate_rates($rates,$pd_amount)
  592. {
  593. $result = [];
  594. if(empty($rates))
  595. {
  596. if(intval($pd_amount * 100 + 0.5) > 0) {
  597. $item['rate'] = 30;
  598. $item['total'] = $pd_amount;
  599. $result[] = $item;
  600. } else {
  601. return null;
  602. }
  603. }
  604. else
  605. {
  606. foreach ($rates as $rate => $val) {
  607. $item['rate'] = $rate;
  608. $item['total'] = $val;
  609. $result[] = $item;
  610. }
  611. }
  612. return $result;
  613. }
  614. protected function goods_name($goods_name)
  615. {
  616. return $goods_name;
  617. // if(empty($goods_spec))
  618. // {
  619. // $goods_name = trim($goods_name);
  620. // $data = explode(' ',$goods_name);
  621. // $count = count($data);
  622. // if($count > 1) {
  623. // $goods_spec = $data[$count -1];
  624. // } else {
  625. // $goods_spec = '';
  626. // }
  627. // }
  628. //
  629. // return $goods_spec;
  630. }
  631. protected function goods_spec($goods_spec,$goods_name)
  632. {
  633. if(empty($goods_spec))
  634. {
  635. $goods_name = trim($goods_name);
  636. $data = explode(' ',$goods_name);
  637. $count = count($data);
  638. if($count > 1) {
  639. $goods_spec = $data[$count -1];
  640. } else {
  641. $goods_spec = '';
  642. }
  643. }
  644. return $goods_spec;
  645. }
  646. protected function type_desc($goods_type)
  647. {
  648. $descs = [1 => "普通商品",2 => "抢购", 3 => "限购", 4 => "套装", 5 => "赠品"];
  649. if($goods_type > 0 && $goods_type <= count($descs)) {
  650. return $descs[$goods_type];
  651. } else {
  652. return "";
  653. }
  654. }
  655. }
  656. class order_helper extends order_base
  657. {
  658. private $mOrders;
  659. private $mod_refund;
  660. private $mod_order;
  661. public function __construct($order_list)
  662. {
  663. $this->mod_order = Model('order');
  664. $this->mod_refund = Model('refund_return');
  665. $this->mOrders = $this->mod_refund->getGoodsRefundList($order_list);
  666. }
  667. private function actions($order)
  668. {
  669. $state['if_payment'] = $this->mod_order->getOrderOperateState('payment',$order);
  670. //显示取消订单
  671. $state['if_cancel'] = $this->mod_order->getOrderOperateState('buyer_cancel', $order);
  672. //显示退款取消订单
  673. $state['if_refund_cancel'] = $this->mod_order->getOrderOperateState('refund_cancel', $order);
  674. //显示收货
  675. $state['if_receive'] = $this->mod_order->getOrderOperateState('receive', $order);
  676. //显示物流跟踪
  677. $state['if_deliver'] = $this->mod_order->getOrderOperateState('deliver', $order);
  678. //显示评价
  679. $state['if_evaluation'] = $this->mod_order->getOrderOperateState('evaluation', $order);
  680. //显示删除
  681. $state['if_delete'] = $this->mod_order->getOrderOperateState('delete', $order);
  682. $state['if_lock'] = $this->mod_order->getOrderOperateState('lock', $order);
  683. $actions = [];
  684. foreach ($state as $key => $value) {
  685. if ($value == true) {
  686. $item['action'] = $key;
  687. $item['title'] = $this->action_title($key);
  688. $actions[] = $item;
  689. }
  690. }
  691. return $actions;
  692. }
  693. private function order_info($order)
  694. {
  695. $result["order_id"] = intval($order["order_id"]);
  696. $result["order_sn"] = $order["order_sn"];
  697. $result["pay_sn"] = $order["pay_sn"];
  698. $result["add_time"] = intval($order["add_time"]);
  699. $result["payment_code"] = $order["payment_code"];
  700. $result["payment_desc"] = $this->payment_desc($order["payment_code"]);
  701. $result['buyer_id'] = intval($order['buyer_id']);
  702. $result["payment_time"] = intval($order["payment_time"]);
  703. $result["finnshed_time"] = intval($order["finnshed_time"]);
  704. $result["goods_amount"] = $this->goods_amount($order);//doubleval($order["goods_amount"]);
  705. $result["order_amount"] = doubleval($order["order_amount"]);
  706. $result["rcb_amount"] = doubleval($order["rcb_amount"]);
  707. $result["pd_amount"] = doubleval($order["pd_amount"]);
  708. $result["user_pred"] = $result["pd_amount"];
  709. $result["shipping_fee"] = doubleval($order["shipping_fee"]);
  710. $result["evaluation_state"] = intval($order["evaluation_state"]);
  711. $result["order_state"] = intval($order["order_state"]);
  712. $result["refund_state"] = intval($order["refund_state"]);
  713. $result["refund_amount"] = doubleval($order["refund_amount"]);
  714. $result["delay_time"] = intval($order["delay_time"]);
  715. $result["shipping_code"] = empty($order["shipping_code"]) ? "" : $order["shipping_code"];
  716. $result["trade_no"] = $order["trade_no"];
  717. $result["state_desc"] = $order["state_desc"];
  718. $bonus_amount = $order['bonus_amount'];
  719. $user_bonus = $bonus_amount['user_bonus'];
  720. $room_id = $bonus_amount['room_id'];
  721. $room_bonus = $bonus_amount['room_bonus'];
  722. $bonus_total = $user_bonus;
  723. if($room_id > 0) {
  724. $bonus_total += $room_bonus;
  725. }
  726. $result['user_bonus'] = $user_bonus;
  727. $result['room_id'] = $room_id;
  728. $result['room_bonus'] = $room_bonus;
  729. if(intval($room_id) > 0) {
  730. $result['show_room'] = true;
  731. } else {
  732. $result['show_room'] = false;
  733. }
  734. $result['bonus_rates'] = $this->formate_rates([],$user_bonus + $room_bonus);
  735. $result['pay_cash'] = $result["order_amount"] - $result["pd_amount"] - $bonus_total;
  736. $result['full_discount'] = $result["goods_amount"] - $order["goods_amount"];
  737. $result['full_desc'] = $this->full_desc($order);
  738. if ($result['order_state'] == ORDER_STATE_NEW) {
  739. $result['cancel_time'] = $result['add_time'] + ORDER_AUTO_CANCEL_DAY * 24 * 3600;
  740. } else {
  741. $result['cancel_time'] = 0;
  742. }
  743. $refund_state = 0;
  744. $order['refund_id'] = intval($this->mod_refund->getRefundId($order,$refund_state));
  745. $result["refund_state"] = intval($refund_state);
  746. return $result;
  747. }
  748. private function full_desc($order)
  749. {
  750. $order_info = $order['extend_order_common'];
  751. $info = '';
  752. if(isset($order_info['promotion_info']))
  753. {
  754. $info = $order_info['promotion_info'];
  755. $ret = preg_match('#(.*?)<a.*?title=("|\')(.*?)\2.*?>.*?</a>#s',$info,$matches);
  756. if($ret != false && count($matches) == 4) {
  757. $info = $matches[1] . $matches[3];
  758. }
  759. }
  760. return (is_null($info) || empty($info)) ? "" : $info;
  761. }
  762. private function goods_amount($order)
  763. {
  764. $goods_amount = doubleval($order['goods_amount']);
  765. $goods_list = $order['extend_order_goods'];
  766. if(empty($goods_list) || !is_array($goods_list)) {
  767. return $goods_amount;
  768. } else {
  769. $goods_amount = 0.00;
  770. }
  771. foreach ($goods_list as $goods) {
  772. $goods_amount += doubleval($goods['goods_price']) * intval($goods['goods_num']);
  773. }
  774. return $goods_amount;
  775. }
  776. private function order_goods($order)
  777. {
  778. $result = [];
  779. $order_goods = $order['extend_order_goods'];
  780. if(empty($order_goods)) return $result;
  781. foreach ($order_goods as $val)
  782. {
  783. $goods["rec_id"] = intval($val["rec_id"]);
  784. $goods["order_id"] = intval($val["order_id"]);
  785. $goods["goods_id"] = intval($val["goods_id"]);
  786. $commonid = commonid_helper::instance()->common_id($goods["goods_id"]);
  787. if($commonid == false) {
  788. continue;
  789. }
  790. $goods["goods_commonid"] = $commonid;
  791. $goods["goods_name"] = $this->goods_name($val["goods_name"]);
  792. $goods["goods_price"] = doubleval($val["goods_price"]);
  793. $goods["goods_num"] = intval($val["goods_num"]);
  794. $goods["goods_image"] = cthumb($val['goods_image'], 480, $val['store_id']);
  795. $goods["goods_pay_price"] = doubleval($val["goods_pay_price"]);
  796. $goods["store_id"] = intval($val["store_id"]);
  797. $goods["buyer_id"] = intval($val["buyer_id"]);
  798. $goods["goods_type"] = intval($val["goods_type"]);
  799. $goods["type_desc"] = $this->type_desc($goods["goods_type"]);
  800. $goods["promotions_id"] = intval($val["promotions_id"]);
  801. $goods["gc_id"] = intval($val["gc_id"]);
  802. $goods["goods_spec"] = $this->goods_spec($val["goods_spec"],$val["goods_name"]);
  803. $goods["refund"] = isset($val["refund"]) ? intval($val["refund"]) : 0;
  804. $refund_state = 0;
  805. $goods['refund_id'] = intval($this->mod_refund->getRefundIdForGood($val,$refund_state));
  806. $goods['refund_state'] = intval($refund_state);
  807. $result[] = $goods;
  808. }
  809. return $result;
  810. }
  811. private function reciver_info($order)
  812. {
  813. $info = $order['reciver_info'];
  814. $info['reciver_name'] = $order['reciver_name'];
  815. return $info;
  816. }
  817. private function invoice_info($order)
  818. {
  819. $result = [];
  820. $result['inv_content'] = "";
  821. $result['inv_title'] = "";
  822. $result['inv_id'] = 0;
  823. if(isset($order['extend_order_common']))
  824. {
  825. $order_common = $order['extend_order_common'];
  826. if(isset($order_common['invoice_info']))
  827. {
  828. $invoice_info = $order_common['invoice_info'];
  829. foreach ($invoice_info as $key => $value)
  830. {
  831. if($key == '内容') {
  832. $result['inv_content'] = $value;
  833. }
  834. elseif($key == '抬头') {
  835. $item['inv_title'] = $value;
  836. }
  837. }
  838. }
  839. }
  840. return $result;
  841. }
  842. public function format()
  843. {
  844. $result = [];
  845. foreach ($this->mOrders as $order) {
  846. $item['order_info'] = $this->order_info($order);
  847. $actions = $this->actions($order);
  848. $item['actions'] = $actions;
  849. $order_goods = $this->order_goods($order);
  850. if($order_goods == false) {
  851. continue;
  852. }
  853. $item['order_goods'] = $order_goods;
  854. $item['reciver_info'] = $this->reciver_info($order);
  855. $item['invoice_info'] = $this->invoice_info($order);
  856. $result[] = $item;
  857. }
  858. return $result;
  859. }
  860. }
  861. class vorder_helper extends order_base
  862. {
  863. private $mOrders;
  864. private $mod_vorder;
  865. private $mod_refund;
  866. public function __construct($order_list)
  867. {
  868. $this->mod_vorder = Model('vr_order');
  869. $this->mod_refund = Model('refund_return');
  870. $this->mOrders = $order_list;
  871. }
  872. private function order_info($order)
  873. {
  874. $result["order_id"] = intval($order["order_id"]);
  875. $result["order_sn"] = $order["order_sn"];
  876. $result["pay_sn"] = $order["order_sn"];
  877. $result["add_time"] = intval($order["add_time"]);
  878. $result["payment_code"] = $order["payment_code"];
  879. $result["payment_desc"] = $this->payment_desc($order["payment_code"]);
  880. $result['buyer_id'] = intval($order['buyer_id']);
  881. $result["payment_time"] = intval($order["payment_time"]);
  882. $result["finnshed_time"] = intval($order["finnshed_time"]);
  883. $result["goods_amount"] = doubleval($order['goods_price'] * $order['goods_num']);
  884. $result["order_amount"] = doubleval($order["order_amount"]);
  885. $result["rcb_amount"] = doubleval($order["rcb_amount"]);
  886. $result["pd_amount"] = doubleval($order["pd_amount"]);
  887. $result['bonus_rates'] = [];
  888. $result["shipping_fee"] = doubleval($order["shipping_fee"]);
  889. $result["evaluation_state"] = intval($order["evaluation_state"]);
  890. $result["order_state"] = intval($order["order_state"]);
  891. $result["refund_state"] = intval($order["refund_state"]);
  892. $result["refund_amount"] = doubleval($order["refund_amount"]);
  893. $result["delay_time"] = intval($order["delay_time"]);
  894. $result["shipping_code"] = empty($order["shipping_code"]) ? "" : $order["shipping_code"];
  895. $result["trade_no"] = $order["trade_no"];
  896. $result["state_desc"] = $order["state_desc"];
  897. $result['pay_cash'] = $result["order_amount"] - $result["pd_amount"];
  898. $result['full_discount'] = $result["goods_amount"] - $order["goods_amount"];
  899. $result['full_desc'] = '';
  900. if ($result['order_state'] == ORDER_STATE_NEW) {
  901. $result['cancel_time'] = $result['add_time'] + ORDER_AUTO_CANCEL_DAY * 24 * 3600;
  902. } else {
  903. $result['cancel_time'] = 0;
  904. }
  905. $order['refund_id'] = 0;
  906. $result["refund_state"] = 0;
  907. return $result;
  908. }
  909. public function format()
  910. {
  911. $result = [];
  912. foreach ($this->mOrders as $order)
  913. {
  914. $item['vorder_info'] = $this->order_info($order);
  915. $item['actions'] = $this->actions($order);
  916. $item['vorder_goods'] = $this->order_goods($order);
  917. if(empty($item['vorder_info']) || empty($item['vorder_goods'])) {
  918. continue;
  919. }
  920. $item['reciver_info'] = $this->reciver_info($order);
  921. foreach ($item['actions'] as $action) {
  922. if ($action['action'] == "if_upcert") {
  923. $item['vorder_info']['tips'] = '您的账户中还有“xx”元现金红包没有使用,请点击右下角“红包兑现”或在“我的-我的订单”页面上传凭证,审核通过立即返现!';
  924. }
  925. }
  926. $result[] = $item;
  927. }
  928. return $result;
  929. }
  930. private function order_goods($order)
  931. {
  932. $result = [];
  933. $val = $order;
  934. $goods["rec_id"] = intval($val["rec_id"]);
  935. $goods["order_id"] = intval($val["order_id"]);
  936. $goods["goods_id"] = intval($val["goods_id"]);
  937. $commonid = commonid_helper::instance()->common_id($goods["goods_id"]);
  938. if($commonid == false) {
  939. return false;
  940. }
  941. $goods["goods_commonid"] = $commonid;
  942. $goods["goods_name"] = $val["goods_name"];
  943. $goods["goods_price"] = doubleval($val["goods_price"]);
  944. $goods["goods_num"] = intval($val["goods_num"]);
  945. $goods["goods_image"] = cthumb($val['goods_image'], 480, $val['store_id']);
  946. $goods["goods_pay_price"] = doubleval($val["goods_pay_price"]);
  947. $goods["store_id"] = intval($val["store_id"]);
  948. $goods["buyer_id"] = intval($val["buyer_id"]);
  949. $goods["goods_type"] = intval($val["goods_type"]);
  950. $goods["type_desc"] = $this->type_desc($goods["goods_type"]);
  951. $goods["promotions_id"] = intval($val["promotions_id"]);
  952. $goods["gc_id"] = intval($val["gc_id"]);
  953. $goods["goods_spec"] = $this->goods_spec($val["goods_spec"],$val["goods_name"]);
  954. $goods["refund"] = isset($val["refund"]) ? intval($val["refund"]) : 0;
  955. $refund_state = 0;
  956. $goods['refund_id'] = intval($this->mod_refund->getRefundIdForGood($val,$refund_state));
  957. $goods['refund_state'] = intval($refund_state);
  958. $result[] = $goods;
  959. return $result;
  960. }
  961. private function actions($order)
  962. {
  963. $state['if_payment'] = $this->mod_vorder->getOrderOperateState('payment',$order);
  964. $state['if_cancel'] = $this->mod_vorder->getOrderOperateState('buyer_cancel', $order);
  965. $state['if_delete'] = $this->mod_vorder->getOrderOperateState('delete', $order);
  966. $state['if_refund'] = $this->mod_vorder->getOrderOperateState('refund_cancel', $order);
  967. $state['if_evaluation'] = $this->mod_vorder->getOrderOperateState('evaluation', $order);
  968. $state['if_upcert'] = $this->mod_vorder->getOrderOperateState('upcert', $order);
  969. $actions = [];
  970. foreach ($state as $key => $value)
  971. {
  972. if ($value == true) {
  973. $item['action'] = $key;
  974. $item['title'] = $this->action_title($key);
  975. $actions[] = $item;
  976. }
  977. }
  978. return $actions;
  979. }
  980. private function reciver_info($order)
  981. {
  982. $info = unserialize($order['receiver_info']) ? : null;
  983. return $info;
  984. }
  985. }