order_helper.php 42 KB

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