Sell.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. <?php
  2. # 销售支付
  3. namespace Shop\Lib;
  4. use Dever;
  5. class Sell
  6. {
  7. # 1是用户,2是门店
  8. public $type = 1;
  9. # 1是列表,2是详情
  10. public $view = 1;
  11. # 获取配置
  12. public $config = array();
  13. # table
  14. public $table = 'shop/sell_order';
  15. public function __construct()
  16. {
  17. $this->config = Dever::db($this->table)->config;
  18. }
  19. # 设置订单的类型
  20. public function set($type, $view)
  21. {
  22. $this->type = $type;
  23. $this->view = $view;
  24. return $this;
  25. }
  26. # 获取公共的where
  27. public function where($id)
  28. {
  29. $where = array();
  30. if ($this->type == 1) {
  31. $where['uid'] = $id;
  32. } elseif ($this->type == 2) {
  33. $where['shop_id'] = $id;
  34. }
  35. if (!$where) {
  36. Dever::alert('参数错误');
  37. }
  38. return $where;
  39. }
  40. # 获取订单列表
  41. public function getList($id)
  42. {
  43. $result = array();
  44. $where = $this->where($id);
  45. $mobile = Dever::input('mobile');
  46. if ($mobile) {
  47. $where['mobile'] = $mobile;
  48. }
  49. $status = Dever::input('status');
  50. if ($status) {
  51. $where['status'] = $status;
  52. }
  53. $method = Dever::input('method');
  54. if ($method) {
  55. $where['method'] = $method;
  56. }
  57. $pay_method = Dever::input('pay_method');
  58. if ($pay_method) {
  59. $where['pay_method'] = $pay_method;
  60. }
  61. $result['search_value'] = $where;
  62. $result['search_value']['day'] = $day = Dever::input('day');
  63. if ($day) {
  64. $where['start'] = Dever::maketime($day . ' 00:00:00');
  65. $where['end'] = Dever::maketime($day . ' 23:59:59');
  66. }
  67. $result['order'] = Dever::db($this->table)->getAll($where);
  68. if ($result['order']) {
  69. foreach ($result['order'] as $k => $v) {
  70. $result['order'][$k] = $this->getInfo($v);
  71. }
  72. }
  73. $result['search'] = array();
  74. $result['search']['status'] = array
  75. (
  76. array('value' => 1, 'name' => '待支付'),
  77. array('value' => 2, 'name' => '待处理'),
  78. );
  79. if ($method == 1) {
  80. $result['search']['status'][] = array('value' => 3, 'name' => '待自提');
  81. } elseif ($method == 2) {
  82. $result['search']['status'][] = array('value' => 3, 'name' => '配送中');
  83. } else {
  84. $result['search']['status'][] = array('value' => 3, 'name' => '待自提');
  85. $result['search']['status'][] = array('value' => 3, 'name' => '配送中');
  86. }
  87. $result['search']['status'][] = array('value' => 8, 'name' => '退款');
  88. $result['search']['status'][] = array('value' => '5,6', 'name' => '已完成');
  89. $result['search']['status'][] = array('value' => '7,11', 'name' => '已取消');
  90. $result['search']['method'] = array
  91. (
  92. array('value' => 1, 'name' => '自提'),
  93. array('value' => 2, 'name' => '配送')
  94. );
  95. $result['search']['pay_method'] = array
  96. (
  97. array('value' => 1, 'name' => '平台结算'),
  98. array('value' => 2, 'name' => '门店代下单')
  99. );
  100. return $result;
  101. }
  102. # 查看详情
  103. public function getView($id, $order_id, $show = true)
  104. {
  105. $where = $this->where($id);
  106. $where['id'] = $order_id;
  107. $result = Dever::db($this->table)->find($where);
  108. if (!$result) {
  109. Dever::alert('订单不存在');
  110. }
  111. if ($show) {
  112. $result = $this->getInfo($result);
  113. }
  114. return $result;
  115. }
  116. # 获取订单详细信息
  117. public function getInfo($info)
  118. {
  119. if ($info['status'] == 1) {
  120. # 15分钟内支付,900秒
  121. $m = 900;
  122. # 支付倒计时
  123. $info['time'] = time() - $info['cdate'];
  124. if ($info['time'] >= $m) {
  125. # 已过期,自动取消
  126. $info['time'] = -1;
  127. Dever::db($this->table)->update(array('where_id' => $info['id'], 'status' => 11));
  128. $info['status'] = 11;
  129. } else {
  130. $info['time'] = $m - $info['time'];
  131. }
  132. }
  133. $info['status_name'] = $this->config['status'][$info['status']];
  134. $info['method_name'] = $this->config['method'][$info['method']];
  135. $info['pay_method_name'] = $this->config['pay_method'][$info['pay_method']];
  136. $info['pay_type_name'] = $this->config['pay_type'][$info['pay_type']];
  137. if (!$info['coupon_cash']) {
  138. $info['coupon_cash'] = 0;
  139. }
  140. if (!$info['refund_cash']) {
  141. $info['refund_cash'] = 0;
  142. }
  143. if ($this->type == 1) {
  144. if ($info['status'] == 2) {
  145. $info['status_name'] = '支付成功';
  146. }
  147. if ($info['status'] == 3) {
  148. if ($info['method'] == 1) {
  149. $info['status_name'] = '已配货';
  150. } else {
  151. $info['status_name'] = '配送中';
  152. }
  153. }
  154. } elseif ($this->type == 2) {
  155. if ($info['status'] == 2) {
  156. $info['status_name'] = '待处理';
  157. }
  158. if ($info['status'] == 3) {
  159. if ($info['method'] == 1) {
  160. $info['status_name'] = '待自提';
  161. } else {
  162. $info['status_name'] = '配送中';
  163. }
  164. }
  165. }
  166. if ($info['method'] == 2) {
  167. $info['ps_info'] = Dever::db('shop/sell_order_ps')->find(array('order_id' => $info['id']));
  168. if ($info['ps_info']) {
  169. $info['ps_info']['service_name'] = '商家自送';
  170. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  171. if ($info['ps_info']['qu_date']) {
  172. $info['ps_info']['qu_date'] = date('Y-m-d H:i', $info['ps_info']['qu_date']);
  173. }
  174. if ($info['ps_info']['qs_date']) {
  175. $info['ps_info']['qs_date'] = date('Y-m-d H:i', $info['ps_info']['qs_date']);
  176. }
  177. $status = Dever::db('shop/sell_order_ps')->config['status'];
  178. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  179. if ($info['ps_info']['service_id'] > 0) {
  180. //暂时还没有
  181. }
  182. }
  183. }
  184. $info['goods'] = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
  185. if ($info['uid'] && $info['uid'] > 0) {
  186. $info['user'] = Dever::load('passport/api')->info($info['uid']);
  187. }
  188. $info['shop'] = Dever::db('shop/info')->getOne($info['shop_id']);
  189. $info['cdate'] = date('Y-m-d H:i', $info['cdate']);
  190. if ($info['operdate']) {
  191. $info['operdate'] = date('Y-m-d H:i', $info['operdate']);
  192. }
  193. $goods_status = Dever::db('shop/sell_order_goods')->config['status'];
  194. if ($info['address_id']) {
  195. $info['address'] = Dever::db('passport/address')->find($info['address_id']);
  196. }
  197. if ($this->view == 2) {
  198. foreach ($info['goods'] as $k => $v) {
  199. $info['goods'][$k]['price'] = $v['d_price'] ? $v['d_price'] : round($v['price']/$v['num'], 2);
  200. $info['goods'][$k]['info'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
  201. $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
  202. }
  203. if ($info['invoice_id']) {
  204. $info['invoice'] = Dever::db('passport/invoice')->find($info['invoice_id']);
  205. }
  206. if ($info['user_coupon_id']) {
  207. $coupon = Dever::db('shop/user_coupon')->find($info['user_coupon_id']);
  208. if ($coupon) {
  209. $info['coupon'] = Dever::db('goods/coupon')->find($coupon['coupon_id']);
  210. }
  211. }
  212. } else {
  213. foreach ($info['goods'] as $k => $v) {
  214. $goods = Dever::db('goods/info')->one($v['goods_id']);
  215. $info['goods'][$k]['price'] = $v['d_price'] ? $v['d_price'] : round($v['price']/$v['num'], 2);
  216. $info['goods'][$k]['name'] = $goods['name'];
  217. $info['goods'][$k]['cover'] = $goods['cover'];
  218. $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
  219. }
  220. }
  221. return $info;
  222. }
  223. # 取消订单
  224. public function cancel($id, $order_id)
  225. {
  226. $data = $this->getView($id, $order_id, false);
  227. if ($data['status'] == 1) {
  228. $state = Dever::db('shop/sell_order')->update(array('where_id' => $data['id'], 'status' => 7, 'operdate' => time()));
  229. if ($state) {
  230. if ($data['user_coupon_id']) {
  231. Dever::db('shop/user_coupon')->update(array('where_id' => $data['user_coupon_id'], 'status' => 1));
  232. }
  233. $this->updateSell($data, 2);
  234. }
  235. return 'ok';
  236. } else {
  237. Dever::alert('当前订单状态不允许取消');
  238. }
  239. }
  240. # 获取优惠券
  241. public function coupon(&$data, $type = 1)
  242. {
  243. $user_coupon_id = Dever::input('user_coupon_id');
  244. $coupon_id = Dever::input('coupon_id');
  245. $data['user_coupon_id'] = 0;
  246. $data['coupon_id'] = 0;
  247. $data['coupon_cash'] = 0;
  248. if ($type == 1 && isset($data['uid']) && $data['uid'] > 0) {
  249. # 默认选择优惠券
  250. if ($data['price'] <= 0) {
  251. Dever::alert('付款价格错误');
  252. }
  253. # 查找符合要求的优惠券
  254. $coupon = Dever::db('shop/user_coupon')->getAll(array('uid' => $data['uid'], 'city' => $data['shop']['city'], 'status' => 1, 'edate' => time()));
  255. if ($coupon) {
  256. foreach ($coupon as $k => $v) {
  257. $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
  258. if ($coupon_info) {
  259. if ($v['shop_id'] != $data['shop']['id'] && $coupon_info['method'] == 3) {
  260. # 门店专属券 不能给别的门店用
  261. continue;
  262. }
  263. $kou = false;
  264. if ($coupon_info['type'] == 1) {
  265. # 满减券
  266. if ($data['price'] >= $coupon_info['total_cash']) {
  267. $kou = true;
  268. }
  269. } else {
  270. $kou = true;
  271. }
  272. if ($kou) {
  273. $coupon_info['user_coupon_id'] = $v['id'];
  274. $coupon_info['uid'] = $v['uid'];
  275. $coupon_info['shop_id'] = $v['shop_id'];
  276. $coupon_info['edate'] = date('Y-m-d', $v['edate']);
  277. $data['coupon'][] = $coupon_info;
  278. if (!$user_coupon_id && $data['coupon_cash'] <= $coupon_info['cash']) {
  279. $data['user_coupon_id'] = $v['id'];
  280. $data['coupon_id'] = $coupon_info['id'];
  281. $data['coupon_cash'] = $coupon_info['cash'];
  282. }
  283. }
  284. }
  285. }
  286. }
  287. }
  288. if ($user_coupon_id && isset($data['uid']) && $data['uid'] > 0) {
  289. # 用户自己使用优惠券
  290. if ($data['price'] <= 0) {
  291. Dever::alert('付款价格错误');
  292. }
  293. $coupon = Dever::db('shop/user_coupon')->find(array('uid' => $data['uid'], 'id' => $user_coupon_id, 'status' => 1));
  294. if (!$coupon) {
  295. Dever::alert('优惠券不可用');
  296. }
  297. if (time() > $coupon['edate']) {
  298. Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 3));
  299. Dever::alert('优惠券已过期');
  300. }
  301. $goods_coupon = Dever::db('goods/coupon')->find($coupon['coupon_id']);
  302. if (!$goods_coupon) {
  303. Dever::alert('优惠券不可用');
  304. }
  305. if ($coupon['shop_id'] != $data['shop']['id']) {
  306. if ($goods_coupon['method'] == 3) {
  307. Dever::alert('优惠券不可用');
  308. } else {
  309. $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $coupon['shop_id'], 'coupon_id' => $coupon['coupon_id'], 'city' => $coupon['city']));
  310. if (!$coupon_info) {
  311. Dever::alert('优惠券不可用');
  312. }
  313. }
  314. }
  315. if ($goods_coupon['type'] == 2 && $data['price'] < $goods_coupon['total_cash']) {
  316. Dever::alert('优惠券不可用');
  317. }
  318. $data['user_coupon_id'] = $user_coupon_id;
  319. $data['coupon_id'] = $goods_coupon['id'];
  320. $data['coupon_cash'] = $goods_coupon['cash'];
  321. } elseif ($coupon_id && $type == 3) {
  322. # 店里直接给用优惠券
  323. if ($data['price'] <= 0) {
  324. Dever::alert('付款价格错误');
  325. }
  326. $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $data['shop']['id'], 'id' => $coupon_id));
  327. if (!$coupon_info) {
  328. Dever::alert('优惠券不可用');
  329. }
  330. $goods_coupon = Dever::db('goods/coupon')->find($coupon_info['coupon_id']);
  331. if (!$goods_coupon) {
  332. Dever::alert('优惠券不可用');
  333. }
  334. if ($goods_coupon['type'] == 2 && $data['price'] < $goods_coupon['total_cash']) {
  335. Dever::alert('优惠券不可用');
  336. }
  337. $data['user_coupon_id'] = -1;
  338. $data['coupon_id'] = $goods_coupon['id'];
  339. $data['coupon_cash'] = $goods_coupon['cash'];
  340. }
  341. if (isset($data['coupon_cash']) && $data['coupon_cash'] > 0) {
  342. # 这里还要计算每个商品的优惠金额
  343. if (isset($data['list']) && $data['list']) {
  344. foreach ($data['list'] as $k => $v) {
  345. $data['list'][$k]['coupon_id'] = $data['coupon_id'];
  346. $data['list'][$k]['user_coupon_id'] = $data['user_coupon_id'];
  347. $data['list'][$k]['coupon_cash'] = round($v['price']/$data['price'], 2) * $data['coupon_cash'];
  348. }
  349. }
  350. $data['price'] -= $data['coupon_cash'];
  351. if ($data['price'] < 0) {
  352. $data['price'] = 0;
  353. }
  354. }
  355. return $data;
  356. }
  357. # 得到商品和总价
  358. public function goods(&$data)
  359. {
  360. # 1自提,2配送
  361. $data['method'] = Dever::input('method', 1);
  362. $data['pay_method'] = Dever::input('pay_method');
  363. $card = Dever::input('card');
  364. $pwd = Dever::input('pwd');
  365. if ($data['pay_method'] == 3 && $card && $pwd) {
  366. $data['card'] = Dever::db('goods/card_code')->find(array('card' => $card, 'pwd' => $pwd, 'status' => 1));
  367. if (!$data['card']) {
  368. Dever::alert('卡号/密码错误');
  369. }
  370. if (time() > $data['card']['edate']) {
  371. Dever::alert('礼品卡已失效');
  372. }
  373. $card_info = Dever::db('goods/card')->find($data['card']['card_id']);
  374. if (!$card_info) {
  375. Dever::alert('卡号/密码错误');
  376. }
  377. $goods = Dever::array_decode($card_info['goods']);
  378. $goods_id = array();
  379. $num = array();
  380. $sku_id = array();
  381. foreach ($goods as $k => $v) {
  382. $goods_id[] = $v['goods_id'];
  383. $num[] = $v['num'];
  384. $sku_id[] = -1;
  385. }
  386. } else {
  387. $goods_id = Dever::input('goods_id');
  388. if (!$goods_id) {
  389. Dever::alert('请传入商品');
  390. }
  391. $goods_id = explode(',', $goods_id);
  392. $sku_id = Dever::input('price_id');
  393. if ($sku_id) {
  394. $sku_id = explode(',', $sku_id);
  395. }
  396. $single_id = Dever::input('single_id');
  397. if ($single_id) {
  398. $single_id = explode(',', $single_id);
  399. }
  400. $input_id = Dever::input('input_id');
  401. if ($input_id) {
  402. $input_id = explode(',', $input_id);
  403. }
  404. $input_value = Dever::input('input_value');
  405. if ($input_value) {
  406. $input_value = explode(',', $input_value);
  407. }
  408. $num = Dever::input('num');
  409. if (!$num) {
  410. Dever::alert('请传入商品数量');
  411. }
  412. $num = explode(',', $num);
  413. }
  414. $data['price'] = 0;
  415. $data['num'] = 0;
  416. $data['name'] = array();
  417. $count = count($goods_id);
  418. # 计算总价格
  419. foreach ($goods_id as $k => $v) {
  420. $s = isset($sku_id[$k]) ? $sku_id[$k] : -1;
  421. $n = isset($num[$k]) ? $num[$k] : 1;
  422. $data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v, $s, $n);
  423. $data['list'][$k]['num'] = $n;
  424. # 2是库存不足
  425. $data['list'][$k]['ku_state'] = 1;
  426. # 验证是否有货
  427. $total = Dever::load('shop/lib/info')->checkTotal($n, $v, $data['shop']['id'], $s);
  428. if ($total <= 0) {
  429. $data['list'][$k]['ku_state'] = 2;
  430. $data['list'][$k]['buy_num'] = 0;
  431. }
  432. if ($data['list'][$k]['ku_state'] == 1) {
  433. $data['list'][$k]['buy_num'] = $n;
  434. $data['num'] += $n;
  435. $data['price'] += $data['list'][$k]['price'] * $n;
  436. $data['name'][] = $data['list'][$k]['name'];
  437. }
  438. }
  439. $data['name'] = implode(',', $data['name']);
  440. return $data;
  441. }
  442. # 发起支付
  443. public function pay($method, $pay_method, $pay_type, $user, $shop, $name, $num, $goods, $price, $cash, $address_id, $invoice_id, $info, $card, $coupon_id, $user_coupon_id, $coupon_cash, $cart = false, $system_source = 5, $refer = '')
  444. {
  445. if (!$user) {
  446. Dever::alert('错误的用户信息');
  447. }
  448. $uid = $user['id'];
  449. $order_data['shop_id'] = $shop['id'];
  450. $order_data['uid'] = $uid;
  451. $order_data['mobile'] = $user['mobile'];
  452. $order_data['address_id'] = $address_id;
  453. $order_data['invoice_id'] = $invoice_id;
  454. $order_data['name'] = $name;
  455. $order_data['num'] = $num;
  456. $order_data['info'] = $info;
  457. $order_data['price'] = $price;
  458. $order_data['kou_cash'] = $cash;
  459. $order_data['method'] = $method;
  460. $order_data['pay_method'] = $pay_method;
  461. $order_data['pay_type'] = $pay_type;
  462. if ($card) {
  463. $order_data['card_code_id'] = $card['id'];
  464. }
  465. if ($user_coupon_id) {
  466. $order_data['user_coupon_id'] = $user_coupon_id;
  467. }
  468. if ($coupon_id) {
  469. $order_data['coupon_id'] = $coupon_id;
  470. }
  471. if ($coupon_cash) {
  472. $order_data['coupon_cash'] = $coupon_cash;
  473. }
  474. $order_data['order_num'] = $this->getOrderId();
  475. $id = Dever::db('shop/sell_order')->insert($order_data);
  476. if (!$id) {
  477. Dever::alert('支付失败');
  478. }
  479. foreach($goods as $k => $v) {
  480. if ($v['ku_state'] == 1) {
  481. $data['uid'] = $uid;
  482. $data['order_id'] = $id;
  483. $data['goods_id'] = $v['id'];
  484. $data['sku_id'] = $v['sku_id'];
  485. $data['price'] = $v['price'] * $v['buy_num'];
  486. $data['d_price'] = $v['price'];
  487. $data['num'] = $v['buy_num'];
  488. if (isset($v['user_coupon_id']) && $v['user_coupon_id']) {
  489. $data['user_coupon_id'] = $v['user_coupon_id'];
  490. }
  491. if (isset($v['coupon_id']) && $v['coupon_id']) {
  492. $data['coupon_id'] = $v['coupon_id'];
  493. }
  494. if (isset($v['coupon_cash']) && $v['coupon_cash']) {
  495. $data['coupon_cash'] = $v['coupon_cash'];
  496. } else {
  497. $data['coupon_cash'] = 0;
  498. }
  499. Dever::db('shop/sell_order_goods')->insert($data);
  500. }
  501. }
  502. # 减少库存 增加销量
  503. $order_data['id'] = $id;
  504. $this->updateSell($order_data);
  505. if ($card || $price <= 0 || $pay_type == 2) {
  506. $param['pay_product_id'] = $id;
  507. $param['pay_order_id'] = $order_data['order_num'];
  508. $param['pay_status'] = 2;
  509. $param['pay_cash'] = $price;
  510. $param['pay_msg'] = '';
  511. $this->success($param);
  512. if ($cart) {
  513. # 来自购物车,要清空一下
  514. $this->dropCart($cart, $uid, $goods, $shop['id']);
  515. }
  516. # 使用优惠券
  517. $this->useCoupon($coupon_id, $user_coupon_id);
  518. if ($card) {
  519. # 更新礼品卡状态
  520. $card_update['where_id'] = $card['id'];
  521. $card_update['status'] = 2;
  522. $card_update['uid'] = $uid;
  523. $card_update['shop_id'] = $shop['id'];
  524. $card_update['order_id'] = $id;
  525. $card_update['ddate'] = time();
  526. Dever::db('goods/card_code')->update($card_update);
  527. }
  528. return array
  529. (
  530. 'order' => '',
  531. 'order_num' => $order_data['order_num'],
  532. 'order_id' => $id,
  533. 'type' => 'pay',
  534. );
  535. }
  536. $param = array
  537. (
  538. 'project_id' => 1,
  539. 'channel_id' => 1,
  540. 'system_source' => $system_source,
  541. 'uid' => $uid,
  542. 'name' => $order_data['name'],
  543. 'cash' => $price,
  544. 'product_id' => $id,
  545. 'order_id' => $order_data['order_num'],
  546. 'refer' => $refer,
  547. );
  548. $receipt = Dever::input('receipt');
  549. if ($receipt) {
  550. $param['receipt'] = $receipt;
  551. }
  552. //$result = Dever::load('pay/api.pay', $param);
  553. $result['type'] = 'test';
  554. $result['order'] = '';
  555. if ($cart) {
  556. # 来自购物车,要清空一下
  557. $this->dropCart($cart, $uid, $goods, $shop['id']);
  558. }
  559. # 使用优惠券
  560. $this->useCoupon($coupon_id, $user_coupon_id);
  561. $result['order_num'] = $order_data['order_num'];
  562. $result['order_id'] = $id;
  563. return $result;
  564. }
  565. public function dropCart($cart, $uid, $goods, $shop_id)
  566. {
  567. $where['uid'] = $uid;
  568. $where['shop_id'] = $shop_id;
  569. foreach ($goods as $k => $v) {
  570. $where['goods_id'] = $v['id'];
  571. if ($cart == 1) {
  572. Dever::db('shop/cart')->delete($where);
  573. } else {
  574. unset($where['uid']);
  575. Dever::db('shop/shop_cart')->delete($where);
  576. }
  577. }
  578. }
  579. public function useCoupon($coupon_id, $user_coupon_id)
  580. {
  581. if ($user_coupon_id) {
  582. Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 2));
  583. }
  584. # 这里可以记录一下使用日志
  585. }
  586. # 再次发起支付
  587. public function rpay($id, $system_source = 5, $refer = '')
  588. {
  589. $order = Dever::db('shop/sell_order')->find($id);
  590. if (!$order) {
  591. Dever::alert('订单信息错误');
  592. }
  593. if ($order['status'] > 1) {
  594. Dever::alert('订单已支付或已取消');
  595. }
  596. $param = array
  597. (
  598. 'project_id' => 1,
  599. 'channel_id' => 1,
  600. 'system_source' => $system_source,
  601. 'uid' => $order['uid'],
  602. 'name' => $order['name'],
  603. 'cash' => $order['price'],
  604. 'product_id' => $id,
  605. 'order_id' => $order['order_num'],
  606. 'refer' => $refer,
  607. );
  608. $receipt = Dever::input('receipt');
  609. if ($receipt) {
  610. $param['receipt'] = $receipt;
  611. }
  612. $result = Dever::load('pay/api.pay', $param);
  613. $result['order_num'] = $order['order_num'];
  614. $result['order_id'] = $id;
  615. return $result;
  616. }
  617. # 支付成功回调 安全加密 设置token
  618. public function success_secure_api_token()
  619. {
  620. $project_id = Dever::input('pay_project_id');
  621. $info = Dever::db('pay/project')->one($project_id);
  622. if ($info) {
  623. return $info['key'];
  624. }
  625. return 'shop_dever_2020';
  626. }
  627. # 支付成功回调 安全加密
  628. public function success_secure_api($param = array())
  629. {
  630. $this->success($param);
  631. }
  632. # 支付成功回调
  633. public function success($param = array())
  634. {
  635. $send = $param ? $param : Dever::preInput('pay_');
  636. $product_id = $send['pay_product_id'];
  637. $order_id = $send['pay_order_id'];
  638. $status = $send['pay_status'];
  639. $cash = $send['pay_cash'];
  640. $msg = $send['pay_msg'];
  641. $order = Dever::db('shop/sell_order')->one(array('id' => $product_id, 'time' => time()));
  642. if ($order && $order['status'] == 1) {
  643. if ($status == 2) {
  644. # 生成自提码或者取件码
  645. $update['code'] = $this->getCode();
  646. if ($order['method'] == 1) {
  647. $msg = '您有一件自提商品已订购成功,取件码:' . $update['code'];
  648. } else {
  649. $msg = '您有一件外送商品已订购成功,核销码:' . $update['code'];
  650. # 分配配送信息
  651. //$this->updatePs($order);
  652. }
  653. # 发消息
  654. if (Dever::project('message')) {
  655. $shop = Dever::db('shop/info')->one($order['shop_id']);
  656. $msg_param['type'] = 1;//消息类型1是订单消息
  657. $msg_param['id'] = $order['id'];
  658. $msg_param['name'] = $shop['name'];
  659. $msg_param = Dever::json_encode($msg_param);
  660. Dever::load('message/lib/data')->push(-1, $order['uid'], '订购成功通知', $msg, 2, 1, false, $msg_param);
  661. }
  662. }
  663. $update['status'] = 2;
  664. $update['where_id'] = $order['id'];
  665. Dever::db('shop/sell_order')->update($update);
  666. }
  667. return 'ok';
  668. }
  669. # 添加配送信息
  670. public function updatePs($order, $status = 1)
  671. {
  672. $where['order_id'] = $order['id'];
  673. $data = Dever::db('shop/sell_order_ps')->find($where);
  674. if (!$data) {
  675. $insert = $where;
  676. $insert['service_id'] = -1;
  677. $insert['price'] = 0;
  678. $insert['status'] = $status;
  679. $insert['num'] = 1;
  680. if ($status == 2) {
  681. $insert['qu_date'] = time();
  682. }
  683. Dever::db('shop/sell_order_ps')->insert($insert);
  684. } else {
  685. $update = $where;
  686. $update['where_id'] = $data['id'];
  687. $update['status'] = $status;
  688. if ($status == 2) {
  689. $update['qu_date'] = time();
  690. }
  691. if ($status == 3) {
  692. $update['qs_date'] = time();
  693. }
  694. Dever::db('shop/sell_order_ps')->update($update);
  695. }
  696. }
  697. # 减少库存,增加销量
  698. public function updateSell($order, $type = 1)
  699. {
  700. $where['order_id'] = $order['id'];
  701. $where['status'] = 1;
  702. $data = Dever::db('shop/sell_order_goods')->select($where);
  703. if ($data) {
  704. Dever::load('shop/lib/goods')->oper($order, $type, 2, $data);
  705. }
  706. }
  707. # 生成自提码
  708. public function getCode()
  709. {
  710. $where['code'] = Dever::rand(6, 0);
  711. $state = Dever::db('shop/sell_order')->one($where);
  712. if (!$state) {
  713. return $where['code'];
  714. } else {
  715. return $this->getCode();
  716. }
  717. }
  718. # 生成订单号
  719. public function getOrderId()
  720. {
  721. $where['order_num'] = Dever::order('G');
  722. $state = Dever::db('shop/sell_order')->one($where);
  723. if (!$state) {
  724. return $where['order_num'];
  725. } else {
  726. return $this->getOrderId();
  727. }
  728. }
  729. # 展示订单详情
  730. public function show()
  731. {
  732. $id = Dever::input('order_id');
  733. $config = Dever::db('shop/sell_order')->config;
  734. $info = Dever::db('shop/sell_order')->one($id);
  735. $tk_status = Dever::db('shop/sell_order_refund')->config['status'];
  736. $tk_process = Dever::db('shop/sell_order_refund')->config['process'];
  737. $status = $config['status'][$info['status']];
  738. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'type' => 1));
  739. $shop = Dever::db('shop/info')->find($info['shop_id']);
  740. $user = Dever::db('passport/user')->find($info['uid']);
  741. if (!$user['truename']) {
  742. $user['truename'] = $user['username'];
  743. }
  744. $html = '[基本信息]:<table class="layui-table"><thead><tr><th style="width:20%">项目</th><th style="width:80%">详情</th></tr> </thead><tbody>';
  745. $html .= '<tr>
  746. <td>订单号</td>
  747. <td>'.$this->table(false, array(array($info['order_num']))).'</td>
  748. </tr>';
  749. $html .= '<tr>
  750. <td>门店信息</td>
  751. <td>'.$this->table(false, array(array('门店名称:' . $shop['name'], '联系人:' . $shop['truename'], '联系电话:' . $shop['mobile']))).'</td>
  752. </tr>';
  753. $html .= '<tr>
  754. <td>用户信息</td>
  755. <td>'.$this->table(false, array(array('昵称:' . $user['username'], '联系电话:' . $user['mobile']))).'</td>
  756. </tr>';
  757. if ($tk && $tk['process'] == 1) {
  758. $status = '申请' . $tk_status[$tk['status']];
  759. $process = $tk_process[$tk['process']];
  760. $process = '';
  761. }
  762. if ($info['info']) {
  763. $html .= '<tr>
  764. <td>订单备注</td>
  765. <td>'.$this->table(false, array(array($info['info']))).'</td>
  766. </tr>';
  767. }
  768. if (!$info['refund_cash']) {
  769. $info['refund_cash'] = 0;
  770. }
  771. $html .= '<tr>
  772. <td>订单信息</td>
  773. <td>'.$this->table(array('金额', '退款金额', '数量', '支付方式', '配送方式', '订单状态'), array(array($info['price'], $info['refund_cash'], $info['num'],$config['pay_method'][$info['pay_method']], $config['method'][$info['method']], $status))).'</td>
  774. </tr>';
  775. if ($info['address_id'] > 0) {
  776. $address = Dever::load('passport/address')->getOne($info['uid'], $info['address_id']);
  777. $html .= '<tr>
  778. <td>收货地址</td>
  779. <td>'.$this->table(array('省份', '城市', '地区','地址', '门牌号', '姓名', '手机号'), array(array($address['province_name'], $address['city_name'], $address['county_name'], $address['address'], $address['house_number'], $address['contact'], $address['mobile']))).'</td>
  780. </tr>';
  781. }
  782. if ($info['invoice_id'] > 0) {
  783. $invoice = Dever::load('passport/invoice')->getOne($info['uid'], $info['invoice_id']);
  784. $html .= '<tr>
  785. <td>发票信息</td>
  786. <td>'.$this->table(array('发票类型', '抬头', '税号','手机号', '邮箱'), array(array($invoice['title_type_name'], $invoice['title'], $invoice['number'], $invoice['mobile'], $invoice['email']))).'</td>
  787. </tr>';
  788. }
  789. $html .= '</tbody></table>';
  790. $html .= '[商品清单]:';
  791. $head = array('名称', '属性', '价格', '数量', '状态');
  792. $body = array();
  793. $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
  794. $goods_status = Dever::db('shop/sell_order_goods')->config['status'];
  795. foreach ($goods as $k => $v) {
  796. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  797. $status = $goods_status[$v['status']];
  798. $tk = Dever::db('shop/sell_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  799. if ($tk && $tk['process'] == 1) {
  800. $status = '申请' . $tk_status[$tk['status']];
  801. //$status .= '&nbsp;&nbsp;[' . $tk_process[$tk['process']] . ']';
  802. }
  803. if (isset($goods_info['sku'])) {
  804. $sku = $goods_info['sku']['string'];
  805. } else {
  806. $sku = '无';
  807. }
  808. $body[$k] = array
  809. (
  810. $goods_info['name'],
  811. $sku,
  812. $v['price'],
  813. $v['num'],
  814. $status,
  815. );
  816. }
  817. $html .= $this->table($head, $body);
  818. return $html;
  819. }
  820. private function table($head, $data)
  821. {
  822. $html = '';
  823. if ($head) {
  824. $html = '<table class="layui-table">';
  825. $html .= '<thead><tr>';
  826. foreach ($head as $k => $v) {
  827. $html .= '<th>'.$v.'</th>';
  828. }
  829. $html .= '</tr></thead>';
  830. $html .= '<tbody>';
  831. foreach ($data as $k => $v) {
  832. $html .= '<tr>';
  833. foreach ($v as $k1 => $v1) {
  834. $html .= '<td>'.$v1.'</td>';
  835. }
  836. $html .= '</tr>';
  837. }
  838. $html .= '</tbody>';
  839. $html .= '</table>';
  840. } else {
  841. foreach ($data as $k => $v) {
  842. $html .= '';
  843. foreach ($v as $k1 => $v1) {
  844. $html .= $v1 . '&nbsp;&nbsp;&nbsp;&nbsp;';
  845. }
  846. $html .= '';
  847. }
  848. }
  849. return $html;
  850. }
  851. }