order_helper.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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["user_pred"] = $result["pd_amount"];
  706. $result["shipping_fee"] = doubleval($order["shipping_fee"]);
  707. $result["evaluation_state"] = intval($order["evaluation_state"]);
  708. $result["order_state"] = intval($order["order_state"]);
  709. $result["refund_state"] = intval($order["refund_state"]);
  710. $result["refund_amount"] = doubleval($order["refund_amount"]);
  711. $result["delay_time"] = intval($order["delay_time"]);
  712. $result["shipping_code"] = empty($order["shipping_code"]) ? "" : $order["shipping_code"];
  713. $result["trade_no"] = $order["trade_no"];
  714. $result["state_desc"] = $order["state_desc"];
  715. $bonus_amount = $order['bonus_amount'];
  716. $user_bonus = $bonus_amount['user_bonus'];
  717. $room_id = $bonus_amount['room_id'];
  718. $room_bonus = $bonus_amount['room_bonus'];
  719. $bonus_total = $user_bonus;
  720. if($room_id > 0) {
  721. $bonus_total += $room_bonus;
  722. }
  723. $result['user_bonus'] = $user_bonus;
  724. $result['room_id'] = $room_id;
  725. $result['room_bonus'] = $room_bonus;
  726. if(intval($room_id) > 0) {
  727. $result['show_room'] = true;
  728. } else {
  729. $result['show_room'] = false;
  730. }
  731. $result['bonus_rates'] = $this->formate_rates([],$user_bonus + $room_bonus);
  732. $result['pay_cash'] = $result["order_amount"] - $result["pd_amount"] - $bonus_total;
  733. $result['full_discount'] = $result["goods_amount"] - $order["goods_amount"];
  734. $result['full_desc'] = $this->full_desc($order);
  735. if ($result['order_state'] == ORDER_STATE_NEW) {
  736. $result['cancel_time'] = $result['add_time'] + ORDER_AUTO_CANCEL_DAY * 24 * 3600;
  737. } else {
  738. $result['cancel_time'] = 0;
  739. }
  740. $refund_state = 0;
  741. $order['refund_id'] = intval($this->mod_refund->getRefundId($order,$refund_state));
  742. $result["refund_state"] = intval($refund_state);
  743. return $result;
  744. }
  745. private function full_desc($order)
  746. {
  747. $order_info = $order['extend_order_common'];
  748. $info = '';
  749. if(isset($order_info['promotion_info']))
  750. {
  751. $info = $order_info['promotion_info'];
  752. $ret = preg_match('#(.*?)<a.*?title=("|\')(.*?)\2.*?>.*?</a>#s',$info,$matches);
  753. if($ret != false && count($matches) == 4) {
  754. $info = $matches[1] . $matches[3];
  755. }
  756. }
  757. return (is_null($info) || empty($info)) ? "" : $info;
  758. }
  759. private function goods_amount($order)
  760. {
  761. $goods_amount = doubleval($order['goods_amount']);
  762. $goods_list = $order['extend_order_goods'];
  763. if(empty($goods_list) || !is_array($goods_list)) {
  764. return $goods_amount;
  765. } else {
  766. $goods_amount = 0.00;
  767. }
  768. foreach ($goods_list as $goods) {
  769. $goods_amount += doubleval($goods['goods_price']) * intval($goods['goods_num']);
  770. }
  771. return $goods_amount;
  772. }
  773. private function order_goods($order)
  774. {
  775. $result = [];
  776. $order_goods = $order['extend_order_goods'];
  777. if(empty($order_goods)) return $result;
  778. foreach ($order_goods as $val)
  779. {
  780. $goods["rec_id"] = intval($val["rec_id"]);
  781. $goods["order_id"] = intval($val["order_id"]);
  782. $goods["goods_id"] = intval($val["goods_id"]);
  783. $commonid = commonid_helper::instance()->common_id($goods["goods_id"]);
  784. if($commonid == false) {
  785. continue;
  786. }
  787. $goods["goods_commonid"] = $commonid;
  788. $goods["goods_name"] = $this->goods_name($val["goods_name"]);
  789. $goods["goods_price"] = doubleval($val["goods_price"]);
  790. $goods["goods_num"] = intval($val["goods_num"]);
  791. $goods["goods_image"] = cthumb($val['goods_image'], 480, $val['store_id']);
  792. $goods["goods_pay_price"] = doubleval($val["goods_pay_price"]);
  793. $goods["store_id"] = intval($val["store_id"]);
  794. $goods["buyer_id"] = intval($val["buyer_id"]);
  795. $goods["goods_type"] = intval($val["goods_type"]);
  796. $goods["type_desc"] = $this->type_desc($goods["goods_type"]);
  797. $goods["promotions_id"] = intval($val["promotions_id"]);
  798. $goods["gc_id"] = intval($val["gc_id"]);
  799. $goods["goods_spec"] = $this->goods_spec($val["goods_spec"],$val["goods_name"]);
  800. $goods["refund"] = isset($val["refund"]) ? intval($val["refund"]) : 0;
  801. $refund_state = 0;
  802. $goods['refund_id'] = intval($this->mod_refund->getRefundIdForGood($val,$refund_state));
  803. $goods['refund_state'] = intval($refund_state);
  804. $result[] = $goods;
  805. }
  806. return $result;
  807. }
  808. private function reciver_info($order)
  809. {
  810. $info = $order['reciver_info'];
  811. $info['reciver_name'] = $order['reciver_name'];
  812. return $info;
  813. }
  814. private function invoice_info($order)
  815. {
  816. $result = [];
  817. $result['inv_content'] = "";
  818. $result['inv_title'] = "";
  819. $result['inv_id'] = 0;
  820. if(isset($order['extend_order_common']))
  821. {
  822. $order_common = $order['extend_order_common'];
  823. if(isset($order_common['invoice_info']))
  824. {
  825. $invoice_info = $order_common['invoice_info'];
  826. foreach ($invoice_info as $key => $value)
  827. {
  828. if($key == '内容') {
  829. $result['inv_content'] = $value;
  830. }
  831. elseif($key == '抬头') {
  832. $item['inv_title'] = $value;
  833. }
  834. }
  835. }
  836. }
  837. return $result;
  838. }
  839. public function format()
  840. {
  841. $result = [];
  842. foreach ($this->mOrders as $order) {
  843. $item['order_info'] = $this->order_info($order);
  844. $actions = $this->actions($order);
  845. $item['actions'] = $actions;
  846. $order_goods = $this->order_goods($order);
  847. if($order_goods == false) {
  848. continue;
  849. }
  850. $item['order_goods'] = $order_goods;
  851. $item['reciver_info'] = $this->reciver_info($order);
  852. $item['invoice_info'] = $this->invoice_info($order);
  853. $result[] = $item;
  854. }
  855. return $result;
  856. }
  857. }
  858. class vorder_helper extends order_base
  859. {
  860. private $mOrders;
  861. private $mod_vorder;
  862. private $mod_refund;
  863. public function __construct($order_list)
  864. {
  865. $this->mod_vorder = Model('vr_order');
  866. $this->mod_refund = Model('refund_return');
  867. $this->mOrders = $order_list;
  868. }
  869. private function order_info($order)
  870. {
  871. $result["order_id"] = intval($order["order_id"]);
  872. $result["order_sn"] = $order["order_sn"];
  873. $result["pay_sn"] = $order["order_sn"];
  874. $result["add_time"] = intval($order["add_time"]);
  875. $result["payment_code"] = $order["payment_code"];
  876. $result["payment_desc"] = $this->payment_desc($order["payment_code"]);
  877. $result['buyer_id'] = intval($order['buyer_id']);
  878. $result["payment_time"] = intval($order["payment_time"]);
  879. $result["finnshed_time"] = intval($order["finnshed_time"]);
  880. $result["goods_amount"] = doubleval($order['goods_price'] * $order['goods_num']);
  881. $result["order_amount"] = doubleval($order["order_amount"]);
  882. $result["rcb_amount"] = doubleval($order["rcb_amount"]);
  883. $result["pd_amount"] = doubleval($order["pd_amount"]);
  884. $result['bonus_rates'] = [];
  885. $result["shipping_fee"] = doubleval($order["shipping_fee"]);
  886. $result["evaluation_state"] = intval($order["evaluation_state"]);
  887. $result["order_state"] = intval($order["order_state"]);
  888. $result["refund_state"] = intval($order["refund_state"]);
  889. $result["refund_amount"] = doubleval($order["refund_amount"]);
  890. $result["delay_time"] = intval($order["delay_time"]);
  891. $result["shipping_code"] = empty($order["shipping_code"]) ? "" : $order["shipping_code"];
  892. $result["trade_no"] = $order["trade_no"];
  893. $result["state_desc"] = $order["state_desc"];
  894. $result['pay_cash'] = $result["order_amount"] - $result["pd_amount"];
  895. $result['full_discount'] = $result["goods_amount"] - $order["goods_amount"];
  896. $result['full_desc'] = '';
  897. if ($result['order_state'] == ORDER_STATE_NEW) {
  898. $result['cancel_time'] = $result['add_time'] + ORDER_AUTO_CANCEL_DAY * 24 * 3600;
  899. } else {
  900. $result['cancel_time'] = 0;
  901. }
  902. $order['refund_id'] = 0;
  903. $result["refund_state"] = 0;
  904. return $result;
  905. }
  906. public function format()
  907. {
  908. $result = [];
  909. foreach ($this->mOrders as $order)
  910. {
  911. $item['vorder_info'] = $this->order_info($order);
  912. $item['actions'] = $this->actions($order);
  913. $item['vorder_goods'] = $this->order_goods($order);
  914. if(empty($item['vorder_info']) || empty($item['vorder_goods'])) {
  915. continue;
  916. }
  917. $item['reciver_info'] = $this->reciver_info($order);
  918. foreach ($item['actions'] as $action) {
  919. if ($action['action'] == "if_upcert") {
  920. $item['vorder_info']['tips'] = '您的账户中还有“xx”元现金红包没有使用,请点击右下角“红包兑现”或在“我的-我的订单”页面上传凭证,审核通过立即返现!';
  921. }
  922. }
  923. $result[] = $item;
  924. }
  925. return $result;
  926. }
  927. private function order_goods($order)
  928. {
  929. $result = [];
  930. $val = $order;
  931. $goods["rec_id"] = intval($val["rec_id"]);
  932. $goods["order_id"] = intval($val["order_id"]);
  933. $goods["goods_id"] = intval($val["goods_id"]);
  934. $commonid = commonid_helper::instance()->common_id($goods["goods_id"]);
  935. if($commonid == false) {
  936. return false;
  937. }
  938. $goods["goods_commonid"] = $commonid;
  939. $goods["goods_name"] = $val["goods_name"];
  940. $goods["goods_price"] = doubleval($val["goods_price"]);
  941. $goods["goods_num"] = intval($val["goods_num"]);
  942. $goods["goods_image"] = cthumb($val['goods_image'], 480, $val['store_id']);
  943. $goods["goods_pay_price"] = doubleval($val["goods_pay_price"]);
  944. $goods["store_id"] = intval($val["store_id"]);
  945. $goods["buyer_id"] = intval($val["buyer_id"]);
  946. $goods["goods_type"] = intval($val["goods_type"]);
  947. $goods["type_desc"] = $this->type_desc($goods["goods_type"]);
  948. $goods["promotions_id"] = intval($val["promotions_id"]);
  949. $goods["gc_id"] = intval($val["gc_id"]);
  950. $goods["goods_spec"] = $this->goods_spec($val["goods_spec"],$val["goods_name"]);
  951. $goods["refund"] = isset($val["refund"]) ? intval($val["refund"]) : 0;
  952. $refund_state = 0;
  953. $goods['refund_id'] = intval($this->mod_refund->getRefundIdForGood($val,$refund_state));
  954. $goods['refund_state'] = intval($refund_state);
  955. $result[] = $goods;
  956. return $result;
  957. }
  958. private function actions($order)
  959. {
  960. $state['if_payment'] = $this->mod_vorder->getOrderOperateState('payment',$order);
  961. $state['if_cancel'] = $this->mod_vorder->getOrderOperateState('buyer_cancel', $order);
  962. $state['if_delete'] = $this->mod_vorder->getOrderOperateState('delete', $order);
  963. $state['if_refund'] = $this->mod_vorder->getOrderOperateState('refund_cancel', $order);
  964. $state['if_evaluation'] = $this->mod_vorder->getOrderOperateState('evaluation', $order);
  965. $state['if_upcert'] = $this->mod_vorder->getOrderOperateState('upcert', $order);
  966. $actions = [];
  967. foreach ($state as $key => $value)
  968. {
  969. if ($value == true) {
  970. $item['action'] = $key;
  971. $item['title'] = $this->action_title($key);
  972. $actions[] = $item;
  973. }
  974. }
  975. return $actions;
  976. }
  977. private function reciver_info($order)
  978. {
  979. $info = unserialize($order['receiver_info']) ? : null;
  980. return $info;
  981. }
  982. }