order_helper.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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 = [1 => "待审核",2 => "同意", 3 => "不同意"];
  16. static $refund_state = [1 => "处理中",2 => "待管理员处理", 3 => "已完成"];
  17. static $refund_type = [1 => "退款",2 => "退货"];
  18. static $return_type = [1 => "不用退货",2 => "需要退货"];
  19. static $order_goods_type = [1 => "普通商品",2 => "团购商品",3 => "限时折扣商品",4 => "组合套装"];
  20. static $goods_state = [1 => "待发货",2 => "待收货",3 => "未收到",4 => "已收货"];
  21. static $payments = ['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. elseif(isset($goods_info[$order_id]))
  34. {
  35. $order_goods = $goods_info[$order_id];
  36. if(isset($order_goods[$goods_id])) {
  37. $goods = $order_goods[$goods_id];
  38. $price = doubleval($goods['goods_price']);
  39. $spec = $goods['goods_spec'];
  40. if(empty($spec)) $spec = "";
  41. }
  42. }
  43. }
  44. static public function get_payment($order_infos,$order_id)
  45. {
  46. if($order_id <= 0) {
  47. return "";
  48. }
  49. elseif(isset($order_infos[$order_id]))
  50. {
  51. $order = $order_infos[$order_id];
  52. if(isset($order['payment_code'])) {
  53. return self::$payments[$order['payment_code']];
  54. }
  55. }
  56. return "";
  57. }
  58. static public function format($info,$goods_info,$order_infos)
  59. {
  60. $result['refund_id'] = intval($info['refund_id']);
  61. $result['order_id'] = intval($info['order_id']);
  62. $result['order_sn'] = $info['order_sn'];
  63. $result['refund_sn'] = $info['refund_sn'];
  64. $result['refund_amount'] = doubleval($info['refund_amount']);
  65. $result['refund_step'] = self::refund_step($info);
  66. $result['refund_type'] = intval($info['refund_type']);
  67. $result['refund_type_desc'] = self::$refund_type[intval($info['refund_type'])];
  68. $result['add_time'] = intval($info['add_time']);
  69. $result['goods_name'] = empty($info['goods_name']) ? "" : $info['goods_name'];
  70. $image = empty($info['goods_image']) ? "" : $info['goods_image'];
  71. $result['goods_image'] = cthumb($image, 480, $info['store_id']);
  72. $result['goods_id'] = intval($info['goods_id']);
  73. $result['rec_id'] = intval($info['order_goods_id']);
  74. $result['goods_num'] = intval($info['goods_num']);
  75. $result['order_goods_type'] = self::$order_goods_type[intval($info['order_goods_type'])];
  76. $result['goods_state'] = self::$goods_state[intval($info['goods_state'])];
  77. self::get_price($goods_info,$result['order_id'],$result['goods_id'],$price,$spec);
  78. $result['goods_price'] = $price;
  79. $result['goods_spec'] = $spec;
  80. $result['payment_desc'] = self::get_payment($order_infos,$result['order_id']);
  81. $result['seller_state'] = self::$seller_state[intval($info['seller_state'])];
  82. $result['refund_state'] = self::$refund_state[intval($info['refund_state'])];
  83. $result['buyer_message'] = empty($info['buyer_message']) ? "" : $info['buyer_message'];
  84. $result['seller_message'] = empty($info['seller_message']) ? "" : $info['seller_message'];
  85. $result['admin_message'] = empty($info['admin_message']) ? "" : $info['admin_message'];
  86. //$result['return_type'] = intval($info['return_type']); //'退货类型:1为不用退货,2为需要退货,默认为1'
  87. //$result['order_lock'] = intval($info['order_lock']); //'订单锁定类型:1为不用锁定,2为需要锁定,默认为1',
  88. //$result['seller_time'] = intval($info['seller_time']);
  89. //$result['admin_time'] = intval($info['admin_time']);
  90. //$result['ship_time'] = intval($info['ship_time']);
  91. //$result['reason_id'] = intval($info['reason_id']);
  92. $result['shipping_code'] = empty($info['invoice_no']) ? "" : $info['invoice_no']; //物流编码
  93. $result['express_id'] = intval($info['express_id']);
  94. $result['delay_time'] = intval($info['delay_time']);
  95. $result['receive_time'] = intval($info['receive_time']);
  96. $result['receive_message'] = empty($info['receive_message']) ? "" : $info['receive_message'];
  97. $result['reason_info'] = empty($info['reason_info']) ? "" : $info['reason_info'];
  98. return $result;
  99. }
  100. private static function refund_step($info)
  101. {
  102. $state = 1;
  103. $type = intval($info['refund_type']);
  104. $seller_time = intval($info['seller_time']);
  105. $admin_time = intval($info['admin_time']);
  106. $ship_time = intval($info['ship_time']);
  107. $return_type = intval($info['return_type']);
  108. if($type == self::REFUND_MONEY)
  109. {
  110. if($seller_time > 0) {
  111. $state = 2;
  112. }
  113. if($admin_time > 0) {
  114. $state = 3;
  115. }
  116. }
  117. else if($type == self::RETURN_GOODS)
  118. {
  119. if($seller_time > 0) {
  120. $state = 2;
  121. }
  122. if($ship_time > 0 || $return_type == 1) {
  123. $state = 3;
  124. }
  125. if($admin_time > 0) {
  126. $state = 4;
  127. }
  128. }
  129. return $state;
  130. }
  131. }
  132. class refund_helper
  133. {
  134. private $member_id;
  135. public function __construct($member_id)
  136. {
  137. $this->member_id = intval($member_id);
  138. }
  139. private function format($refund_list,$goods_info,$order_infos)
  140. {
  141. $result = [];
  142. foreach ($refund_list as $item) {
  143. $result[] = refund_item::format($item,$goods_info,$order_infos);
  144. }
  145. return $result;
  146. }
  147. private function goods_info($refund_list,&$order_infos)
  148. {
  149. $ids = [];
  150. foreach ($refund_list as $item) {
  151. $ids[] = intval($item['order_id']);
  152. }
  153. if(empty($ids)) return false;
  154. $model_order = Model('order');
  155. $order_list = $model_order->getNormalOrderList(array('order_id' => array('in',$ids)), $this->page_size, '*', 'order_id desc', '', array('order_goods'));
  156. $order_infos = [];
  157. $result = [];
  158. foreach ($order_list as $order)
  159. {
  160. $order_id = intval($order['order_id']);
  161. $order_goods = $order['extend_order_goods'];
  162. $ex_goods = [];
  163. foreach ($order_goods as $goods) {
  164. $goods_id = intval($goods['goods_id']);
  165. $ex_goods[$goods_id] = $goods;
  166. }
  167. $result[$order_id] = $ex_goods;
  168. $order_infos[$order_id]['payment_code'] = $order['payment_code'];
  169. }
  170. return $result;
  171. }
  172. public function list_all($page,&$total_page)
  173. {
  174. $model_refund = Model('refund_return');
  175. $condition = array();
  176. $condition['buyer_id'] = $this->member_id;
  177. $refund_list = $model_refund->getRefundReturnList($condition,$page);
  178. $total_page = $model_refund->gettotalpage();
  179. $goods_info = $this->goods_info($refund_list,$order_infos);
  180. return $this->format($refund_list,$goods_info,$order_infos);
  181. }
  182. public function view($refund_id,&$err)
  183. {
  184. $model_refund = Model('refund_return');
  185. $return_list = $model_refund->getRefundReturnList(array('buyer_id' => $this->member_id,'refund_id' => $refund_id));
  186. if(empty($return_list)) {
  187. $err = array('code' => errcode::ErrRefundNotExist,'msg' => '无此退款信息.');
  188. return false;
  189. }
  190. $goods_info = $this->goods_info($return_list,$order_infos);
  191. $refunds = $this->format($return_list,$goods_info,$order_infos);
  192. return $refunds[0];
  193. }
  194. public function ship($refund_id,$express_id,$invoice_no,&$err)
  195. {
  196. $model_refund = Model('refund_return');
  197. $condition = array();
  198. $condition['buyer_id'] = $this->member_id;
  199. $condition['refund_id'] = $refund_id;
  200. $return_list = $model_refund->getReturnList($condition);
  201. if(empty($return_list)) {
  202. $err = array('code' => errcode::ErrOrderRefundError,'msg' => "无此退款信息");
  203. return false;
  204. }
  205. $return = $return_list[0];
  206. if ($return['seller_state'] != '2' || $return['goods_state'] != '1') {
  207. $err = array('code' => errcode::ErrFrequentlyRequest,'msg' => "该申请已发货");
  208. return false;
  209. }
  210. $refund_array = array();
  211. $refund_array['ship_time'] = time();
  212. $refund_array['delay_time'] = time();
  213. $refund_array['express_id'] = $express_id;
  214. $refund_array['invoice_no'] = $invoice_no;
  215. $refund_array['goods_state'] = '2';
  216. $state = $model_refund->editRefundReturn($condition, $refund_array);
  217. if ($state) {
  218. return true;
  219. } else {
  220. $err = array('code' => errcode::ErrOrderRefundError,'msg' => "发货失败");
  221. return false;
  222. }
  223. }
  224. public function refund($order_sn,&$err)
  225. {
  226. $order_info = Model('order')->getOrderInfo(array('order_sn' => $order_sn));
  227. if (empty($order_info)) {
  228. $err = array("code" => errcode::ErrOrderNotExist,'msg' => errcode::msg(errcode::ErrOrderNotExist));
  229. return false;
  230. }
  231. $model_refund = Model('refund_return');
  232. $order_id = intval($order_info['order_id']);
  233. $order = $model_refund->getRightOrderList(array('buyer_id' => $this->member_id, 'order_id' => $order_id));
  234. $order_amount = $order['order_amount'];//订单金额
  235. $bonus_amount = unserialize($order['bonus_amount']);
  236. $user_bonus = floatval($bonus_amount['user_bonus']);
  237. $room_bonus = floatval($bonus_amount['room_bonus']);
  238. $pd_amount = $order['pd_amount'];
  239. $order_amount -= $user_bonus + $room_bonus;
  240. $condition = array();
  241. {
  242. $condition['buyer_id'] = $order['buyer_id'];
  243. $condition['order_id'] = $order['order_id'];
  244. $condition['goods_id'] = '0';
  245. $condition['seller_state'] = array('lt', '3'); //状态:1为待审核,2为同意,3为不同意
  246. }
  247. $refund_list = $model_refund->getRefundReturnList($condition);
  248. $refund = array();
  249. if (!empty($refund_list) && is_array($refund_list)) {
  250. $refund = $refund_list[0];
  251. }
  252. $model_trade = Model('trade');
  253. $order_paid = $model_trade->getOrderState('order_paid');
  254. $payment_code = $order['payment_code'];
  255. if ($refund['refund_id'] > 0 || $order['order_state'] != $order_paid || $payment_code == 'offline') {
  256. $err = array("code" => errcode::ErrOrderNotExist,'msg' => "只有付完款尚未发货的时才能退款。");
  257. return false;
  258. }
  259. $refund_array = array();
  260. $refund_array['refund_type'] = '1'; //类型:1为退款,2为退货
  261. $refund_array['seller_state'] = '1';//状态:1为待审核,2为同意,3为不同意
  262. $refund_array['order_lock'] = '2'; //锁定类型:1为不用锁定,2为需要锁定
  263. $refund_array['goods_id'] = '0';
  264. $refund_array['order_goods_id'] = '0';
  265. $refund_array['reason_id'] = '0';
  266. $refund_array['reason_info'] = '取消订单,全部退款';
  267. $refund_array['goods_name'] = '订单商品全部退款';
  268. $refund_array['refund_amount'] = ncPriceFormat($order_amount);
  269. $refund_array['buyer_message'] = remove_tags(urldecode($_POST['buyer_message']));
  270. $refund_array['add_time'] = time();
  271. $pic_array = array();
  272. $pic_array['buyer'] = array();
  273. $refund_array['pic_info'] = serialize($pic_array);
  274. $refund_id = $model_refund->addRefundReturn($refund_array, $order);
  275. if ($refund_id) {
  276. QueueClient::push("reset_fcode",['pay_sn' => $order_info['pay_sn']]);
  277. $model_refund->editOrderLock($order_id);
  278. return $refund_id;
  279. } else {
  280. $err = array("code" => errcode::ErrOrderRefundError, 'msg' => "退款失败.");
  281. return false;
  282. }
  283. }
  284. private function reason_list($model_refund)
  285. {
  286. $condition = array();
  287. $reason_data = $model_refund->getReasonList($condition, '', '', 'reason_id,reason_info');
  288. $reason_list = [];
  289. foreach ($reason_data as $data) {
  290. $reason_list[] = $data;
  291. }
  292. $reason_list[] = array('reason_id' => 0, 'reason_info' => '其他');
  293. return $reason_list;
  294. }
  295. private function reason_info($model_refund,$reason_id)
  296. {
  297. $reason_list = $this->reason_list($model_refund);
  298. $reasons = [];
  299. foreach ($reason_list as $data) {
  300. $reason_id = intval($data['reason_id']);
  301. $reason_info = $data['reason_info'];
  302. $reasons[$reason_id] = $reason_info;
  303. }
  304. if (isset($reasons[$reason_id])) {
  305. return $reasons[$reason_id];
  306. } else {
  307. return '其他';
  308. }
  309. }
  310. public function return_info($order_sn,$rec_id,&$err)
  311. {
  312. $order_info = Model('order')->getOrderInfo(array('order_sn' => $order_sn));
  313. if (empty($order_info)) {
  314. $err = array("code" => errcode::ErrOrderNotExist,'msg' => errcode::msg(errcode::ErrOrderNotExist));
  315. return false;
  316. }
  317. $order_id = intval($order_info['order_id']);
  318. if ($order_id < 1 || $rec_id < 1) {
  319. $err = array("code" => errcode::ErrParamter,'msg' => errcode::msg(errcode::ErrParamter));
  320. return false;
  321. }
  322. $model_refund = Model('refund_return');
  323. $reason_list = $this->reason_list($model_refund);
  324. $condition = array();
  325. $condition['buyer_id'] = $this->member_id;
  326. $condition['order_id'] = $order_id;
  327. $order = $model_refund->getRightOrderList($condition, $rec_id);
  328. $order_amount = $order['order_amount'];//订单金额
  329. $order_refund_amount = $order['refund_amount'];//订单退款金额
  330. $goods_list = $order['goods_list'];
  331. $goods = $goods_list[0];
  332. $goods_pay_price = $goods['goods_pay_price'];//商品实际成交价
  333. if ($order_amount < ($goods_pay_price + $order_refund_amount)) {
  334. $goods_pay_price = $order_amount - $order_refund_amount;
  335. $goods['goods_pay_price'] = $goods_pay_price;
  336. }
  337. $condition = array();
  338. $condition['buyer_id'] = $order['buyer_id'];
  339. $condition['order_id'] = $order['order_id'];
  340. $condition['order_goods_id'] = $rec_id;
  341. $condition['seller_state'] = array('lt', '3');
  342. $refund_list = $model_refund->getRefundReturnList($condition);
  343. $refund = array();
  344. if (!empty($refund_list) && is_array($refund_list)) {
  345. $refund = $refund_list[0];
  346. }
  347. $refund_state = $model_refund->getRefundState($order);//根据订单状态判断是否可以退款退货 '申请状态:1为处理中,2为待管理员处理,3为已完成,默认为1',
  348. if ($refund['refund_id'] > 0) {
  349. $err = array("code" => errcode::ErrOrderRefundError,'msg' => "不能重复退货");
  350. return false;
  351. }
  352. if ($refund_state != 1) {
  353. $err = array("code" => errcode::ErrOrderState,'msg' => '请确认订单状态,在已经付款后才能退款,已经发货后才能退货.');
  354. return false;
  355. }
  356. return array('reason_list' => (array)$reason_list, 'goods' => $goods);
  357. }
  358. public function return_goods($order_sn,$rec_id,$goods_num,$refund_amount,$reason_id,$buyer_msg,&$err)
  359. {
  360. $order_info = Model('order')->getOrderInfo(array('order_sn' => $order_sn));
  361. if (empty($order_info) || intval($order_info['order_id'] < 1)) {
  362. $err = array("code" => errcode::ErrOrderNotExist,'msg' => errcode::msg(errcode::ErrOrderNotExist));
  363. return false;
  364. }
  365. $order_id = intval($order_info['order_id']);
  366. $model_refund = Model('refund_return');
  367. $reson_info = $this->reason_info($model_refund,$reason_id);
  368. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  369. $condition = array();
  370. $condition['buyer_id'] = $this->member_id;
  371. $condition['order_id'] = $order_id;
  372. $order = $model_refund->getRightOrderList($condition, $rec_id);
  373. $order_id = $order['order_id'];
  374. $order_amount = $order['order_amount'];//订单金额
  375. $order_refund_amount = $order['refund_amount'];//订单退款金额
  376. $goods_list = $order['goods_list'];
  377. $goods = $goods_list[0];
  378. $goods_pay_price = $goods['goods_pay_price'];//商品实际成交价
  379. if ($order_amount < ($goods_pay_price + $order_refund_amount)) {
  380. $goods_pay_price = $order_amount - $order_refund_amount;
  381. $goods['goods_pay_price'] = $goods_pay_price;
  382. }
  383. $goods_id = $goods['rec_id'];
  384. $condition = array();
  385. $condition['buyer_id'] = $order['buyer_id'];
  386. $condition['order_id'] = $order['order_id'];
  387. $condition['order_goods_id'] = $goods_id;
  388. $condition['seller_state'] = array('lt', '3');
  389. $refund_list = $model_refund->getRefundReturnList($condition);
  390. $refund = array();
  391. if (!empty($refund_list) && is_array($refund_list)) {
  392. $refund = $refund_list[0];
  393. }
  394. $refund_state = $model_refund->getRefundState($order);
  395. if ($refund['refund_id'] > 0 || $refund_state != 1) {
  396. $err = array("code" => errcode::ErrOrderState,'msg' => errcode::msg(errcode::ErrOrderState));
  397. return false;
  398. }
  399. $refund_array = array();
  400. if (($refund_amount < 0) || ($refund_amount > $goods_pay_price)) {
  401. $refund_amount = $goods_pay_price;
  402. }
  403. if (($goods_num < 0) || ($goods_num > $goods['goods_num'])) {
  404. $goods_num = 1;
  405. }
  406. $refund_array['reason_info'] = $reson_info;
  407. $refund_array['reason_id'] = $reason_id;
  408. $refund_array['pic_info'] = serialize(array('buyer' => array()));
  409. $model_trade = Model('trade');
  410. $order_shipped = $model_trade->getOrderState('order_shipped');//订单状态30:已发货
  411. if ($order['order_state'] == $order_shipped) {
  412. $refund_array['order_lock'] = '2';//锁定类型:1为不用锁定,2为需要锁定
  413. }
  414. $refund_array['refund_type'] = 2;//类型:1为退款,2为退货
  415. $refund_array['return_type'] = '2';//退货类型:1为不用退货,2为需要退货
  416. if ($refund_array['refund_type'] != '2') {
  417. $refund_array['refund_type'] = '1';
  418. $refund_array['return_type'] = '1';
  419. }
  420. $refund_array['seller_state'] = '1';//状态:1为待审核,2为同意,3为不同意
  421. $refund_array['refund_amount'] = ncPriceFormat($refund_amount);
  422. $refund_array['goods_num'] = $goods_num;
  423. $refund_array['buyer_message'] = $buyer_msg;
  424. $refund_array['add_time'] = time();
  425. $refund_id = $model_refund->addRefundReturn($refund_array, $order, $goods);
  426. if ($refund_id)
  427. {
  428. if ($order['order_state'] == $order_shipped) {
  429. $model_refund->editOrderLock($order_id);
  430. }
  431. QueueClient::push("reset_fcode",['pay_sn' => $order_info['pay_sn']]);
  432. return true;
  433. }
  434. else {
  435. $err = array("code" => errcode::ErrOrderRefundError,'msg' => "退货失败");
  436. return false;
  437. }
  438. }
  439. }
  440. class order_action
  441. {
  442. public function change_state($state_type,$order_id)
  443. {
  444. $model_order = Model('order');
  445. $condition = array();
  446. $condition['order_id'] = $order_id;
  447. $condition['buyer_id'] = $_SESSION['member_id'];
  448. $order_info = $model_order->getOrderInfo($condition);
  449. if($state_type == 'order_cancel') {
  450. $result = $this->order_cancel($order_info);
  451. } else if ($state_type == 'order_receive') {
  452. $result = $this->order_receive($order_info);
  453. } else if (in_array($state_type,array('order_delete','order_drop','order_restore'))){
  454. $result = $this->order_recycle($order_info, $state_type);
  455. } else {
  456. return false;
  457. }
  458. return $result['state'];
  459. }
  460. private function order_cancel($order_info)
  461. {
  462. $model_order = Model('order');
  463. $logic_order = Logic('order');
  464. $if_allow = $model_order->getOrderOperateState('buyer_cancel',$order_info);
  465. if (!$if_allow) {
  466. return callback(false,'无权操作');
  467. }
  468. $ret = $logic_order->changeOrderStateCancel($order_info,'buyer', $_SESSION['member_name']);
  469. if($ret['state']) {
  470. QueueClient::push('onBonusChange',['change_type' => 'order_cancel','buyer_id'=>$order_info['buyer_id'],'order_sn'=>$order_info['order_sn']]);
  471. } else {
  472. Log::record('用户取消订单失败SN:' . $order_info['order_sn'],Log::ERR);
  473. }
  474. return $ret;
  475. }
  476. private function order_receive($order_info)
  477. {
  478. $model_order = Model('order');
  479. $logic_order = Logic('order');
  480. $if_allow = $model_order->getOrderOperateState('receive',$order_info);
  481. if (!$if_allow) {
  482. return callback(false,'无权操作');
  483. }
  484. return $logic_order->changeOrderStateReceive($order_info,'buyer',$_SESSION['member_name']);
  485. }
  486. private function order_recycle($order_info, $state_type) {
  487. $model_order = Model('order');
  488. $logic_order = Logic('order');
  489. $state_type = str_replace(array('order_delete','order_drop','order_restore'), array('delete','drop','restore'), $state_type);
  490. $if_allow = $model_order->getOrderOperateState($state_type,$order_info);
  491. if (!$if_allow) {
  492. return callback(false,'无权操作');
  493. }
  494. return $logic_order->changeOrderStateRecycle($order_info,'buyer',$state_type);
  495. }
  496. }
  497. class vorder_action
  498. {
  499. private $mVrModel;
  500. private $mVrLogic;
  501. public function __construct()
  502. {
  503. $this->mVrModel = Model('vr_order');
  504. $this->mVrLogic = Logic('vr_order');
  505. }
  506. public function change_state($state_type,$order_id)
  507. {
  508. $condition = array();
  509. $condition['order_id'] = $order_id;
  510. $condition['buyer_id'] = session_helper::memberid();
  511. $order_info = $this->mVrModel->getOrderInfo($condition);
  512. if($state_type == 'order_cancel') {
  513. $result = $this->order_cancel($order_info);
  514. } else if ($state_type == 'order_receive') {
  515. $result = $this->order_receive($order_info);
  516. } else if (in_array($state_type,array('order_delete','order_drop','order_restore'))){
  517. $result = $this->order_recycle($order_info, $state_type);
  518. } else {
  519. return false;
  520. }
  521. return $result['state'];
  522. }
  523. private function order_cancel($order_info)
  524. {
  525. $if_allow = $this->mVrModel->getOrderOperateState('buyer_cancel',$order_info);
  526. if (!$if_allow) {
  527. return callback(false,'无权操作');
  528. }
  529. $ret = $this->mVrLogic->changeOrderStateCancel($order_info,'buyer', session_helper::memberid());
  530. if($ret['state']) {
  531. QueueClient::push('onBonusChange',['change_type' => 'order_cancel','buyer_id'=>$order_info['buyer_id'],'order_sn'=>$order_info['order_sn']]);
  532. } else {
  533. Log::record('用户取消订单失败SN:' . $order_info['order_sn'],Log::ERR);
  534. }
  535. return $ret;
  536. }
  537. private function order_recycle($order_info, $state_type)
  538. {
  539. $state_type = str_replace(array('order_delete','order_drop','order_restore'), array('delete','drop','restore'), $state_type);
  540. $if_allow = $this->mVrModel->getOrderOperateState($state_type,$order_info);
  541. if (!$if_allow) {
  542. return callback(false,'无权操作');
  543. }
  544. return $this->mVrLogic->changeOrderStateRecycle($order_info,'buyer',$state_type);
  545. }
  546. private function order_receive($order_info)
  547. {
  548. $if_allow = $this->mVrModel->getOrderOperateState('receive',$order_info);
  549. if (!$if_allow) {
  550. return callback(false,'无权操作');
  551. }
  552. return $this->mVrLogic->changeOrderStateReceive($order_info,'buyer',session_helper::memberid());
  553. }
  554. }
  555. class order_base
  556. {
  557. public function __construct() {
  558. }
  559. protected function action_title($key)
  560. {
  561. static $actions = ["if_cancel" => "取消订单",
  562. "if_refund_cancel" => "退款",
  563. "if_payment" => "立即支付",
  564. "if_receive" => "确认收货",
  565. "if_deliver" => "查看物流",
  566. "if_evaluation" => "评价赚红包",
  567. "if_upcert" => "红包兑现",
  568. "if_delete" => "删除订单",
  569. "if_lock" => "退款退货中"];
  570. return $actions[$key];
  571. }
  572. protected function payment_desc($payment_code)
  573. {
  574. static $payments = ['bonus' => "红包支付","predeposit" => "红包支付","online" => "在线支付",
  575. 'alipay' => "支付宝",'wxpay' => "微信支付",'cmbpay' => "一网通银行卡支付"];
  576. if(isset($payments[$payment_code])) {
  577. return $payments[$payment_code];
  578. } else {
  579. return "";
  580. }
  581. }
  582. protected function formate_rates($rates,$pd_amount)
  583. {
  584. $result = [];
  585. if(empty($rates))
  586. {
  587. if(intval($pd_amount * 100 + 0.5) > 0) {
  588. $item['rate'] = 30;
  589. $item['total'] = $pd_amount;
  590. $result[] = $item;
  591. } else {
  592. return null;
  593. }
  594. }
  595. else
  596. {
  597. foreach ($rates as $rate => $val) {
  598. $item['rate'] = $rate;
  599. $item['total'] = $val;
  600. $result[] = $item;
  601. }
  602. }
  603. return $result;
  604. }
  605. protected function goods_name($goods_name)
  606. {
  607. return $goods_name;
  608. // if(empty($goods_spec))
  609. // {
  610. // $goods_name = trim($goods_name);
  611. // $data = explode(' ',$goods_name);
  612. // $count = count($data);
  613. // if($count > 1) {
  614. // $goods_spec = $data[$count -1];
  615. // } else {
  616. // $goods_spec = '';
  617. // }
  618. // }
  619. //
  620. // return $goods_spec;
  621. }
  622. protected function goods_spec($goods_spec,$goods_name)
  623. {
  624. if(empty($goods_spec))
  625. {
  626. $goods_name = trim($goods_name);
  627. $data = explode(' ',$goods_name);
  628. $count = count($data);
  629. if($count > 1) {
  630. $goods_spec = $data[$count -1];
  631. } else {
  632. $goods_spec = '';
  633. }
  634. }
  635. else {
  636. $spec = unserialize($goods_spec);
  637. foreach ($spec as $key => $val) {
  638. return $val;
  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. parent::__construct();
  661. $this->mod_order = Model('order');
  662. $this->mod_refund = Model('refund_return');
  663. $this->mOrders = $this->mod_refund->getGoodsRefundList($order_list);
  664. }
  665. private function actions($order)
  666. {
  667. $state['if_payment'] = $this->mod_order->getOrderOperateState('payment',$order);
  668. //显示取消订单
  669. $state['if_cancel'] = $this->mod_order->getOrderOperateState('buyer_cancel', $order);
  670. //显示退款取消订单
  671. $state['if_refund_cancel'] = $this->mod_order->getOrderOperateState('refund_cancel', $order);
  672. //显示收货
  673. $state['if_receive'] = $this->mod_order->getOrderOperateState('receive', $order);
  674. //显示物流跟踪
  675. $state['if_deliver'] = $this->mod_order->getOrderOperateState('deliver', $order);
  676. //显示评价
  677. $state['if_evaluation'] = $this->mod_order->getOrderOperateState('evaluation', $order);
  678. //显示删除
  679. $state['if_delete'] = $this->mod_order->getOrderOperateState('delete', $order);
  680. //显示锁定
  681. $state['if_lock'] = $this->mod_order->getOrderOperateState('lock', $order);
  682. $actions = [];
  683. foreach ($state as $key => $value) {
  684. if ($value == true) {
  685. $item['action'] = $key;
  686. $item['title'] = $this->action_title($key);
  687. $actions[] = $item;
  688. }
  689. }
  690. return $actions;
  691. }
  692. private function order_info($order)
  693. {
  694. $result["order_id"] = intval($order["order_id"]);
  695. $result["order_sn"] = $order["order_sn"];
  696. $result["pay_sn"] = $order["pay_sn"];
  697. $result["add_time"] = intval($order["add_time"]);
  698. $result["payment_code"] = $order["payment_code"];
  699. $result["payment_desc"] = $this->payment_desc($order["payment_code"]);
  700. $result['buyer_id'] = intval($order['buyer_id']);
  701. $result["payment_time"] = intval($order["payment_time"]);
  702. $result["finnshed_time"] = intval($order["finnshed_time"]);
  703. $result["goods_amount"] = $this->goods_amount($order);//doubleval($order["goods_amount"]);
  704. $result["order_amount"] = doubleval($order["order_amount"]);
  705. $result["rcb_amount"] = doubleval($order["rcb_amount"]);
  706. $result["pd_amount"] = doubleval($order["pd_amount"]);
  707. $result["user_pred"] = $result["pd_amount"];
  708. $result["shipping_fee"] = doubleval($order["shipping_fee"]);
  709. $result["evaluation_state"] = intval($order["evaluation_state"]);
  710. $result["order_state"] = intval($order["order_state"]);
  711. $result["refund_state"] = intval($order["refund_state"]);
  712. $result["refund_amount"] = doubleval($order["refund_amount"]);
  713. $result["delay_time"] = intval($order["delay_time"]);
  714. $result["shipping_code"] = empty($order["shipping_code"]) ? "" : $order["shipping_code"];
  715. $result["trade_no"] = $order["trade_no"];
  716. $result["state_desc"] = $order["state_desc"];
  717. $bonus_amount = $order['bonus_amount'];
  718. $user_bonus = $bonus_amount['user_bonus'];
  719. $room_id = $bonus_amount['room_id'];
  720. $room_bonus = $bonus_amount['room_bonus'];
  721. $bonus_total = $user_bonus;
  722. if($room_id > 0) {
  723. $bonus_total += $room_bonus;
  724. }
  725. $result['user_bonus'] = $user_bonus;
  726. $result['room_id'] = $room_id;
  727. $result['room_bonus'] = $room_bonus;
  728. if(intval($room_id) > 0) {
  729. $result['show_room'] = true;
  730. } else {
  731. $result['show_room'] = false;
  732. }
  733. $result['bonus_rates'] = $this->formate_rates([],$user_bonus + $room_bonus);
  734. $result['pay_cash'] = $result["order_amount"] - $result["pd_amount"] - $bonus_total;
  735. $result['full_discount'] = $result["goods_amount"] - $order["goods_amount"];
  736. $result['full_desc'] = $this->full_desc($order);
  737. if ($result['order_state'] == ORDER_STATE_NEW) {
  738. $result['cancel_time'] = $result['add_time'] + ORDER_AUTO_CANCEL_DAY * 24 * 3600;
  739. } else {
  740. $result['cancel_time'] = 0;
  741. }
  742. $refund_state = 0;
  743. $order['refund_id'] = intval($this->mod_refund->getRefundId($order,$refund_state));
  744. $result["refund_state"] = intval($refund_state);
  745. return $result;
  746. }
  747. private function full_desc($order)
  748. {
  749. $order_info = $order['extend_order_common'];
  750. $info = '';
  751. if(isset($order_info['promotion_info']))
  752. {
  753. $info = $order_info['promotion_info'];
  754. $ret = preg_match('#(.*?)<a.*?title=("|\')(.*?)\2.*?>.*?</a>#s',$info,$matches);
  755. if($ret != false && count($matches) == 4) {
  756. $info = $matches[1] . $matches[3];
  757. }
  758. }
  759. return (is_null($info) || empty($info)) ? "" : $info;
  760. }
  761. private function goods_amount($order)
  762. {
  763. $goods_amount = doubleval($order['goods_amount']);
  764. $goods_list = $order['extend_order_goods'];
  765. if(empty($goods_list) || !is_array($goods_list)) {
  766. return $goods_amount;
  767. } else {
  768. $goods_amount = 0.00;
  769. }
  770. foreach ($goods_list as $goods) {
  771. $goods_amount += doubleval($goods['goods_price']) * intval($goods['goods_num']);
  772. }
  773. return $goods_amount;
  774. }
  775. private function order_goods($order)
  776. {
  777. $result = [];
  778. $order_goods = $order['extend_order_goods'];
  779. if(empty($order_goods)) return $result;
  780. foreach ($order_goods as $val)
  781. {
  782. $goods["rec_id"] = intval($val["rec_id"]);
  783. $goods["order_id"] = intval($val["order_id"]);
  784. $goods["goods_id"] = intval($val["goods_id"]);
  785. $commonid = commonid_helper::instance()->common_id($goods["goods_id"]);
  786. if($commonid == false) {
  787. continue;
  788. }
  789. $goods["goods_commonid"] = $commonid;
  790. $goods["goods_name"] = $this->goods_name($val["goods_name"]);
  791. $goods["goods_price"] = doubleval($val["goods_price"]);
  792. $goods["goods_num"] = intval($val["goods_num"]);
  793. $goods["goods_image"] = cthumb($val['goods_image'], 480, $val['store_id']);
  794. $goods["goods_pay_price"] = doubleval($val["goods_pay_price"]);
  795. $goods["store_id"] = intval($val["store_id"]);
  796. $goods["buyer_id"] = intval($val["buyer_id"]);
  797. $goods["goods_type"] = intval($val["goods_type"]);
  798. $goods["type_desc"] = $this->type_desc($goods["goods_type"]);
  799. $goods["promotions_id"] = intval($val["promotions_id"]);
  800. $goods["gc_id"] = intval($val["gc_id"]);
  801. $goods["goods_spec"] = $this->goods_spec($val["goods_spec"],$val["goods_name"]);
  802. $goods["refund"] = isset($val["refund"]) ? intval($val["refund"]) : 0;
  803. $refund_state = 0;
  804. $goods['refund_id'] = intval($this->mod_refund->getRefundIdForGood($val,$refund_state));
  805. $goods['refund_state'] = intval($refund_state);
  806. $result[] = $goods;
  807. }
  808. return $result;
  809. }
  810. private function reciver_info($order)
  811. {
  812. $info = $order['reciver_info'];
  813. $info['reciver_name'] = $order['reciver_name'];
  814. return $info;
  815. }
  816. private function invoice_info($order)
  817. {
  818. $result = [];
  819. $result['inv_content'] = "";
  820. $result['inv_title'] = "";
  821. $result['inv_id'] = 0;
  822. if(isset($order['extend_order_common']))
  823. {
  824. $order_common = $order['extend_order_common'];
  825. if(isset($order_common['invoice_info']))
  826. {
  827. $invoice_info = $order_common['invoice_info'];
  828. foreach ($invoice_info as $key => $value)
  829. {
  830. if($key == '内容') {
  831. $result['inv_content'] = $value;
  832. }
  833. elseif($key == '抬头') {
  834. $item['inv_title'] = $value;
  835. }
  836. }
  837. }
  838. }
  839. return $result;
  840. }
  841. public function format()
  842. {
  843. $result = [];
  844. foreach ($this->mOrders as $order) {
  845. $item['order_info'] = $this->order_info($order);
  846. $actions = $this->actions($order);
  847. $item['actions'] = $actions;
  848. $order_goods = $this->order_goods($order);
  849. if($order_goods == false) {
  850. continue;
  851. }
  852. $item['order_goods'] = $order_goods;
  853. $item['reciver_info'] = $this->reciver_info($order);
  854. $item['invoice_info'] = $this->invoice_info($order);
  855. $result[] = $item;
  856. }
  857. return $result;
  858. }
  859. }
  860. class vorder_helper extends order_base
  861. {
  862. private $mOrders;
  863. private $mod_vorder;
  864. private $mod_refund;
  865. public function __construct($order_list)
  866. {
  867. parent::__construct();
  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'] + VRORDER_AUTO_CANCEL_MINUTE * 60;
  902. } else {
  903. $result['cancel_time'] = 0;
  904. }
  905. $result['cancel_desc'] = $this->cancle_desc($result['cancel_time']);
  906. $order['refund_id'] = 0;
  907. $result["refund_state"] = 0;
  908. return $result;
  909. }
  910. private function cancle_desc($cancel_time)
  911. {
  912. $now = time();
  913. if($cancel_time > $now) {
  914. $left = $cancel_time - $now;
  915. $min = intval($left / 60);
  916. $sec = $left % 60;
  917. $desc = "{$min}分{$sec}秒";
  918. }
  919. else {
  920. $desc = "";
  921. }
  922. return $desc;
  923. }
  924. public function format()
  925. {
  926. $gids = [];
  927. foreach ($this->mOrders as $order) {
  928. $gid = intval($order['goods_id']);
  929. $gids[] = $gid;
  930. }
  931. $helper = new goods_helper(new bonus\normal_calc());
  932. $items = $helper->order_goods($gids);
  933. $all_goods = [];
  934. foreach ($items as $item) {
  935. $gid = intval($item['goods_id']);
  936. $all_goods[$gid] = $item;
  937. }
  938. $result = [];
  939. foreach ($this->mOrders as $order)
  940. {
  941. $gid = intval($order['goods_id']);
  942. $goods = $all_goods[$gid];
  943. $item['vorder_info'] = $this->order_info($order);
  944. $item['actions'] = $this->actions($order);
  945. $item['vorder_goods'] = $this->order_goods($order,$goods);
  946. if(empty($item['vorder_info']) || empty($item['vorder_goods'])) {
  947. continue;
  948. }
  949. $item['reciver_info'] = $this->reciver_info($order);
  950. $extra_info = json_decode($order['extra_info'],true);
  951. if(!empty($extra_info)) {
  952. $extra_info = $extra_info['input'];
  953. } else {
  954. $extra_info = [];
  955. }
  956. $item['extra_info'] = $extra_info;
  957. $result[] = $item;
  958. }
  959. return $result;
  960. }
  961. private function order_goods($order,$goods_info)
  962. {
  963. $result = [];
  964. $val = $order;
  965. $goods["rec_id"] = intval($val["rec_id"]);
  966. $goods["order_id"] = intval($val["order_id"]);
  967. $goods["goods_id"] = intval($val["goods_id"]);
  968. $commonid = commonid_helper::instance()->common_id($goods["goods_id"]);
  969. if($commonid == false) {
  970. return false;
  971. }
  972. $goods["goods_commonid"] = $commonid;
  973. $goods["goods_name"] = $val["goods_name"];
  974. $goods["goods_price"] = doubleval($val["goods_price"]);
  975. $goods["goods_num"] = intval($val["goods_num"]);
  976. $goods["goods_image"] = cthumb($val['goods_image'], 480, $val['store_id']);
  977. $goods["goods_pay_price"] = doubleval($val["goods_pay_price"]);
  978. $goods["store_id"] = intval($val["store_id"]);
  979. $goods["buyer_id"] = intval($val["buyer_id"]);
  980. $goods["goods_type"] = intval($val["goods_type"]);
  981. $goods["type_desc"] = $this->type_desc($goods["goods_type"]);
  982. $goods["promotions_id"] = intval($val["promotions_id"]);
  983. $goods["gc_id"] = intval($val["gc_id"]);
  984. $goods["goods_spec"] = $this->goods_spec($goods_info["goods_spec"],$val["goods_name"]);
  985. $goods["refund"] = isset($val["refund"]) ? intval($val["refund"]) : 0;
  986. $refund_state = 0;
  987. $goods['refund_id'] = intval($this->mod_refund->getRefundIdForGood($val,$refund_state));
  988. $goods['refund_state'] = intval($refund_state);
  989. $result[] = $goods;
  990. return $result;
  991. }
  992. private function actions($order)
  993. {
  994. $state['if_payment'] = $this->mod_vorder->getOrderOperateState('payment',$order);
  995. $state['if_cancel'] = $this->mod_vorder->getOrderOperateState('buyer_cancel', $order);
  996. $state['if_delete'] = $this->mod_vorder->getOrderOperateState('delete', $order);
  997. $state['if_refund'] = $this->mod_vorder->getOrderOperateState('refund_cancel', $order);
  998. $state['if_evaluation'] = $this->mod_vorder->getOrderOperateState('evaluation', $order);
  999. $state['if_upcert'] = $this->mod_vorder->getOrderOperateState('upcert', $order);
  1000. $actions = [];
  1001. foreach ($state as $key => $value)
  1002. {
  1003. if ($value == true) {
  1004. $item['action'] = $key;
  1005. $item['title'] = $this->action_title($key);
  1006. $actions[] = $item;
  1007. }
  1008. }
  1009. return $actions;
  1010. }
  1011. private function reciver_info($order)
  1012. {
  1013. $info = unserialize($order['receiver_info']) ? : null;
  1014. return $info;
  1015. }
  1016. private function extra_info($order)
  1017. {
  1018. $info = json_decode($order['receiver_info'],true);
  1019. return $info;
  1020. }
  1021. }