Sell.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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, '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 = 9000;
  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]['info'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
  200. $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
  201. }
  202. if ($info['invoice_id']) {
  203. $info['invoice'] = Dever::db('passport/invoice')->find($info['invoice_id']);
  204. }
  205. if ($info['user_coupon_id']) {
  206. $coupon = Dever::db('shop/user_coupon')->find($info['user_coupon_id']);
  207. if ($coupon) {
  208. $info['coupon'] = Dever::db('goods/coupon')->find($coupon['coupon_id']);
  209. }
  210. }
  211. } else {
  212. foreach ($info['goods'] as $k => $v) {
  213. $goods = Dever::db('goods/info')->one($v['goods_id']);
  214. $info['goods'][$k]['name'] = $goods['name'];
  215. $info['goods'][$k]['cover'] = $goods['cover'];
  216. $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
  217. }
  218. }
  219. return $info;
  220. }
  221. # 取消订单
  222. public function cancel($id, $order_id)
  223. {
  224. $data = $this->getView($id, $order_id, false);
  225. if ($data['status'] == 1) {
  226. $state = Dever::db('shop/sell_order')->update(array('where_id' => $data['id'], 'status' => 7, 'operdate' => time()));
  227. if ($state) {
  228. if ($data['user_coupon_id']) {
  229. Dever::db('shop/user_coupon')->update(array('where_id' => $data['user_coupon_id'], 'status' => 1));
  230. }
  231. $this->updateSell($data, 2);
  232. }
  233. return 'ok';
  234. } else {
  235. Dever::alert('当前订单状态不允许取消');
  236. }
  237. }
  238. # 获取优惠券
  239. public function coupon(&$data, $type = 1)
  240. {
  241. $user_coupon_id = Dever::input('user_coupon_id');
  242. $coupon_id = Dever::input('coupon_id');
  243. $data['user_coupon_id'] = 0;
  244. $data['coupon_id'] = 0;
  245. $data['coupon_cash'] = 0;
  246. if ($type == 1 && isset($data['uid']) && $data['uid'] > 0) {
  247. # 默认选择优惠券
  248. if ($data['price'] <= 0) {
  249. Dever::alert('付款价格错误');
  250. }
  251. # 查找符合要求的优惠券
  252. $coupon = Dever::db('shop/user_coupon')->getAll(array('uid' => $data['uid'], 'city' => $data['shop']['city'], 'status' => 1, 'edate' => time()));
  253. if ($coupon) {
  254. foreach ($coupon as $k => $v) {
  255. $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
  256. if ($coupon_info) {
  257. if ($v['shop_id'] != $data['shop']['id'] && $coupon_info['method'] == 3) {
  258. # 门店专属券 不能给别的门店用
  259. continue;
  260. }
  261. $kou = false;
  262. if ($coupon_info['type'] == 1) {
  263. # 满减券
  264. if ($data['price'] >= $coupon_info['total_cash']) {
  265. $kou = true;
  266. }
  267. } else {
  268. $kou = true;
  269. }
  270. if ($kou) {
  271. $coupon_info['user_coupon_id'] = $v['id'];
  272. $coupon_info['uid'] = $v['uid'];
  273. $coupon_info['shop_id'] = $v['shop_id'];
  274. $coupon_info['edate'] = date('Y-m-d', $v['edate']);
  275. $data['coupon'][] = $coupon_info;
  276. if (!$user_coupon_id && $data['coupon_cash'] <= $coupon_info['cash']) {
  277. $data['user_coupon_id'] = $v['id'];
  278. $data['coupon_id'] = $coupon_info['id'];
  279. $data['coupon_cash'] = $coupon_info['cash'];
  280. }
  281. }
  282. }
  283. }
  284. }
  285. }
  286. if ($user_coupon_id && isset($data['uid']) && $data['uid'] > 0) {
  287. # 用户自己使用优惠券
  288. if ($data['price'] <= 0) {
  289. Dever::alert('付款价格错误');
  290. }
  291. $coupon = Dever::db('shop/user_coupon')->find(array('uid' => $data['uid'], 'id' => $user_coupon_id, 'status' => 1));
  292. if (!$coupon) {
  293. Dever::alert('优惠券不可用');
  294. }
  295. if (time() > $coupon['edate']) {
  296. Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 3));
  297. Dever::alert('优惠券已过期');
  298. }
  299. $goods_coupon = Dever::db('goods/coupon')->find($coupon['coupon_id']);
  300. if (!$goods_coupon) {
  301. Dever::alert('优惠券不可用');
  302. }
  303. if ($coupon['shop_id'] != $data['shop']['id']) {
  304. if ($goods_coupon['method'] == 3) {
  305. Dever::alert('优惠券不可用');
  306. } else {
  307. $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $coupon['shop_id'], 'coupon_id' => $coupon['coupon_id'], 'city' => $coupon['city']));
  308. if (!$coupon_info) {
  309. Dever::alert('优惠券不可用');
  310. }
  311. }
  312. }
  313. if ($goods_coupon['type'] == 2 && $data['price'] < $goods_coupon['total_cash']) {
  314. Dever::alert('优惠券不可用');
  315. }
  316. $data['user_coupon_id'] = $user_coupon_id;
  317. $data['coupon_id'] = $goods_coupon['id'];
  318. $data['coupon_cash'] = $goods_coupon['cash'];
  319. } elseif ($coupon_id && $type == 3) {
  320. # 店里直接给用优惠券
  321. if ($data['price'] <= 0) {
  322. Dever::alert('付款价格错误');
  323. }
  324. $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $data['shop']['id'], 'id' => $coupon_id));
  325. if (!$coupon_info) {
  326. Dever::alert('优惠券不可用');
  327. }
  328. $goods_coupon = Dever::db('goods/coupon')->find($coupon_info['coupon_id']);
  329. if (!$goods_coupon) {
  330. Dever::alert('优惠券不可用');
  331. }
  332. if ($goods_coupon['type'] == 2 && $data['price'] < $goods_coupon['total_cash']) {
  333. Dever::alert('优惠券不可用');
  334. }
  335. $data['user_coupon_id'] = -1;
  336. $data['coupon_id'] = $goods_coupon['id'];
  337. $data['coupon_cash'] = $goods_coupon['cash'];
  338. }
  339. if (isset($data['coupon_cash']) && $data['coupon_cash'] > 0) {
  340. # 这里还要计算每个商品的优惠金额
  341. if (isset($data['list']) && $data['list']) {
  342. foreach ($data['list'] as $k => $v) {
  343. $data['list'][$k]['coupon_id'] = $data['coupon_id'];
  344. $data['list'][$k]['user_coupon_id'] = $data['user_coupon_id'];
  345. $data['list'][$k]['coupon_cash'] = round($v['price']/$data['price'], 2) * $data['coupon_cash'];
  346. }
  347. }
  348. $data['price'] -= $data['coupon_cash'];
  349. if ($data['price'] < 0) {
  350. $data['price'] = 0;
  351. }
  352. }
  353. return $data;
  354. }
  355. # 得到商品和总价
  356. public function goods(&$data)
  357. {
  358. # 1自提,2配送
  359. $data['method'] = Dever::input('method', 1);
  360. $data['pay_method'] = Dever::input('pay_method');
  361. $card = Dever::input('card');
  362. $pwd = Dever::input('pwd');
  363. if ($data['pay_method'] == 3 && $card && $pwd) {
  364. $data['card'] = Dever::db('goods/card_code')->find(array('card' => $card, 'pwd' => $pwd, 'status' => 1));
  365. if (!$data['card']) {
  366. Dever::alert('卡号/密码错误');
  367. }
  368. if (time() > $data['card']['edate']) {
  369. Dever::alert('礼品卡已失效');
  370. }
  371. $card_info = Dever::db('goods/card')->find($data['card']['card_id']);
  372. if (!$card_info) {
  373. Dever::alert('卡号/密码错误');
  374. }
  375. $goods = Dever::array_decode($card_info['goods']);
  376. $goods_id = array();
  377. $num = array();
  378. $sku_id = array();
  379. foreach ($goods as $k => $v) {
  380. $goods_id[] = $v['goods_id'];
  381. $num[] = $v['num'];
  382. $sku_id[] = -1;
  383. }
  384. } else {
  385. $goods_id = Dever::input('goods_id');
  386. if (!$goods_id) {
  387. Dever::alert('请传入商品');
  388. }
  389. $goods_id = explode(',', $goods_id);
  390. $sku_id = Dever::input('price_id');
  391. if ($sku_id) {
  392. $sku_id = explode(',', $sku_id);
  393. }
  394. $num = Dever::input('num');
  395. if (!$num) {
  396. Dever::alert('请传入商品数量');
  397. }
  398. $num = explode(',', $num);
  399. }
  400. $data['price'] = 0;
  401. $data['num'] = 0;
  402. $data['name'] = array();
  403. $count = count($goods_id);
  404. # 计算总价格
  405. foreach ($goods_id as $k => $v) {
  406. $s = isset($sku_id[$k]) ? $sku_id[$k] : -1;
  407. $n = isset($num[$k]) ? $num[$k] : 1;
  408. $data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v, $s, $n);
  409. $data['list'][$k]['num'] = $n;
  410. # 2是库存不足
  411. $data['list'][$k]['ku_state'] = 1;
  412. # 验证是否有货
  413. $total = Dever::load('shop/lib/info')->checkTotal($n, $v, $data['shop']['id'], $s);
  414. if ($total <= 0) {
  415. $data['list'][$k]['ku_state'] = 2;
  416. $data['list'][$k]['buy_num'] = 0;
  417. }
  418. if ($data['list'][$k]['ku_state'] == 1) {
  419. $data['list'][$k]['buy_num'] = $n;
  420. $data['num'] += $n;
  421. $data['price'] += $data['list'][$k]['price'] * $n;
  422. $data['name'][] = $data['list'][$k]['name'];
  423. }
  424. }
  425. $data['name'] = implode(',', $data['name']);
  426. return $data;
  427. }
  428. # 发起支付
  429. 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 = '')
  430. {
  431. if (!$user) {
  432. Dever::alert('错误的用户信息');
  433. }
  434. $uid = $user['id'];
  435. $order_data['shop_id'] = $shop['id'];
  436. $order_data['uid'] = $uid;
  437. $order_data['mobile'] = $user['mobile'];
  438. $order_data['address_id'] = $address_id;
  439. $order_data['invoice_id'] = $invoice_id;
  440. $order_data['name'] = $name;
  441. $order_data['num'] = $num;
  442. $order_data['info'] = $info;
  443. $order_data['price'] = $price;
  444. $order_data['kou_cash'] = $cash;
  445. $order_data['method'] = $method;
  446. $order_data['pay_method'] = $pay_method;
  447. $order_data['pay_type'] = $pay_type;
  448. if ($card) {
  449. $order_data['card_code_id'] = $card['id'];
  450. }
  451. if ($user_coupon_id) {
  452. $order_data['user_coupon_id'] = $user_coupon_id;
  453. }
  454. if ($coupon_id) {
  455. $order_data['coupon_id'] = $coupon_id;
  456. }
  457. if ($coupon_cash) {
  458. $order_data['coupon_cash'] = $coupon_cash;
  459. }
  460. $order_data['order_num'] = $this->getOrderId();
  461. $id = Dever::db('shop/sell_order')->insert($order_data);
  462. if (!$id) {
  463. Dever::alert('支付失败');
  464. }
  465. foreach($goods as $k => $v) {
  466. if ($v['ku_state'] == 1) {
  467. $data['uid'] = $uid;
  468. $data['order_id'] = $id;
  469. $data['goods_id'] = $v['id'];
  470. $data['sku_id'] = $v['sku_id'];
  471. $data['price'] = $v['price'] * $v['buy_num'];
  472. $data['num'] = $v['buy_num'];
  473. if (isset($v['user_coupon_id']) && $v['user_coupon_id']) {
  474. $data['user_coupon_id'] = $v['user_coupon_id'];
  475. }
  476. if (isset($v['coupon_id']) && $v['coupon_id']) {
  477. $data['coupon_id'] = $v['coupon_id'];
  478. }
  479. if (isset($v['coupon_cash']) && $v['coupon_cash']) {
  480. $data['coupon_cash'] = $v['coupon_cash'];
  481. } else {
  482. $data['coupon_cash'] = 0;
  483. }
  484. Dever::db('shop/sell_order_goods')->insert($data);
  485. }
  486. }
  487. # 减少库存 增加销量
  488. $order_data['id'] = $id;
  489. $this->updateSell($order_data);
  490. if ($card || $price <= 0 || $pay_type == 2) {
  491. $param['pay_product_id'] = $id;
  492. $param['pay_order_id'] = $order_data['order_num'];
  493. $param['pay_status'] = 2;
  494. $param['pay_cash'] = $price;
  495. $param['pay_msg'] = '';
  496. $this->success($param);
  497. if ($cart) {
  498. # 来自购物车,要清空一下
  499. $this->dropCart($cart, $uid, $goods, $shop['id']);
  500. }
  501. # 使用优惠券
  502. $this->useCoupon($coupon_id, $user_coupon_id);
  503. if ($card) {
  504. # 更新礼品卡状态
  505. $card_update['where_id'] = $card['id'];
  506. $card_update['status'] = 2;
  507. $card_update['uid'] = $uid;
  508. $card_update['shop_id'] = $shop['id'];
  509. $card_update['order_id'] = $id;
  510. $card_update['ddate'] = time();
  511. Dever::db('goods/card_code')->update($card_update);
  512. }
  513. return array
  514. (
  515. 'order' => '',
  516. 'order_num' => $order_data['order_num'],
  517. 'order_id' => $id,
  518. 'type' => 'pay',
  519. );
  520. }
  521. $param = array
  522. (
  523. 'project_id' => 1,
  524. 'channel_id' => 1,
  525. 'system_source' => $system_source,
  526. 'uid' => $uid,
  527. 'name' => $order_data['name'],
  528. 'cash' => $price,
  529. 'product_id' => $id,
  530. 'order_id' => $order_data['order_num'],
  531. 'refer' => $refer,
  532. );
  533. $receipt = Dever::input('receipt');
  534. if ($receipt) {
  535. $param['receipt'] = $receipt;
  536. }
  537. //$result = Dever::load('pay/api.pay', $param);
  538. $result['type'] = 'test';
  539. $result['order'] = '';
  540. if ($cart) {
  541. # 来自购物车,要清空一下
  542. $this->dropCart($cart, $uid, $goods, $shop['id']);
  543. }
  544. # 使用优惠券
  545. $this->useCoupon($coupon_id, $user_coupon_id);
  546. $result['order_num'] = $order_data['order_num'];
  547. $result['order_id'] = $id;
  548. return $result;
  549. }
  550. public function dropCart($cart, $uid, $goods, $shop_id)
  551. {
  552. $where['uid'] = $uid;
  553. $where['shop_id'] = $shop_id;
  554. foreach ($goods as $k => $v) {
  555. $where['goods_id'] = $v['id'];
  556. if ($cart == 1) {
  557. Dever::db('shop/cart')->delete($where);
  558. } else {
  559. unset($where['uid']);
  560. Dever::db('shop/shop_cart')->delete($where);
  561. }
  562. }
  563. }
  564. public function useCoupon($coupon_id, $user_coupon_id)
  565. {
  566. if ($user_coupon_id) {
  567. Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 2));
  568. }
  569. # 这里可以记录一下使用日志
  570. }
  571. # 再次发起支付
  572. public function rpay($id, $system_source = 5, $refer = '')
  573. {
  574. $order = Dever::db('shop/sell_order')->find($id);
  575. if (!$order) {
  576. Dever::alert('订单信息错误');
  577. }
  578. if ($order['status'] > 1) {
  579. Dever::alert('订单已支付或已取消');
  580. }
  581. $param = array
  582. (
  583. 'project_id' => 1,
  584. 'channel_id' => 1,
  585. 'system_source' => $system_source,
  586. 'uid' => $order['uid'],
  587. 'name' => $order['name'],
  588. 'cash' => $order['price'],
  589. 'product_id' => $id,
  590. 'order_id' => $order['order_num'],
  591. 'refer' => $refer,
  592. );
  593. $receipt = Dever::input('receipt');
  594. if ($receipt) {
  595. $param['receipt'] = $receipt;
  596. }
  597. $result = Dever::load('pay/api.pay', $param);
  598. $result['order_num'] = $order['order_num'];
  599. $result['order_id'] = $id;
  600. return $result;
  601. }
  602. # 支付成功回调 安全加密 设置token
  603. public function success_secure_api_token()
  604. {
  605. $project_id = Dever::input('pay_project_id');
  606. $info = Dever::db('pay/project')->one($project_id);
  607. if ($info) {
  608. return $info['key'];
  609. }
  610. return 'shop_dever_2020';
  611. }
  612. # 支付成功回调 安全加密
  613. public function success_secure_api($param = array())
  614. {
  615. $this->success($param);
  616. }
  617. # 支付成功回调
  618. public function success($param = array())
  619. {
  620. $send = $param ? $param : Dever::preInput('pay_');
  621. $product_id = $send['pay_product_id'];
  622. $order_id = $send['pay_order_id'];
  623. $status = $send['pay_status'];
  624. $cash = $send['pay_cash'];
  625. $msg = $send['pay_msg'];
  626. $order = Dever::db('shop/sell_order')->one(array('id' => $product_id, 'time' => time()));
  627. if ($order && $order['status'] == 1) {
  628. if ($status == 2) {
  629. # 生成自提码或者取件码
  630. $update['code'] = $this->getCode();
  631. if ($order['method'] == 1) {
  632. $msg = '您有一件自提商品已订购成功,取件码:' . $update['code'];
  633. } else {
  634. $msg = '您有一件外送商品已订购成功,核销码:' . $update['code'];
  635. # 分配配送信息
  636. //$this->updatePs($order);
  637. }
  638. # 发消息
  639. if (Dever::project('message')) {
  640. $shop = Dever::db('shop/info')->one($order['shop_id']);
  641. $msg_param['type'] = 1;//消息类型1是订单消息
  642. $msg_param['id'] = $order['id'];
  643. $msg_param['name'] = $shop['name'];
  644. $msg_param = Dever::json_encode($msg_param);
  645. Dever::load('message/lib/data')->push(-1, $order['uid'], '订购成功通知', $msg, 2, 1, false, $msg_param);
  646. }
  647. }
  648. $update['status'] = 2;
  649. $update['where_id'] = $order['id'];
  650. Dever::db('shop/sell_order')->update($update);
  651. }
  652. return 'ok';
  653. }
  654. # 添加配送信息
  655. public function updatePs($order, $status = 1)
  656. {
  657. $where['order_id'] = $order['id'];
  658. $data = Dever::db('shop/sell_order_ps')->find($where);
  659. if (!$data) {
  660. $insert = $where;
  661. $insert['service_id'] = -1;
  662. $insert['price'] = 0;
  663. $insert['status'] = $status;
  664. $insert['num'] = 1;
  665. if ($status == 2) {
  666. $insert['qu_date'] = time();
  667. }
  668. Dever::db('shop/sell_order_ps')->insert($insert);
  669. } else {
  670. $update = $where;
  671. $update['where_id'] = $data['id'];
  672. $update['status'] = $status;
  673. if ($status == 2) {
  674. $update['qu_date'] = time();
  675. }
  676. if ($status == 3) {
  677. $update['qs_date'] = time();
  678. }
  679. Dever::db('shop/sell_order_ps')->update($update);
  680. }
  681. }
  682. # 减少库存,增加销量
  683. public function updateSell($order, $type = 1)
  684. {
  685. $where['order_id'] = $order['id'];
  686. $where['status'] = 1;
  687. $data = Dever::db('shop/sell_order_goods')->select($where);
  688. if ($data) {
  689. Dever::load('shop/lib/goods')->oper($order, $type, 2, $data);
  690. }
  691. }
  692. # 生成自提码
  693. public function getCode()
  694. {
  695. $where['code'] = Dever::rand(6, 0);
  696. $state = Dever::db('shop/sell_order')->one($where);
  697. if (!$state) {
  698. return $where['code'];
  699. } else {
  700. return $this->getCode();
  701. }
  702. }
  703. # 生成订单号
  704. public function getOrderId()
  705. {
  706. $where['order_num'] = Dever::order('G');
  707. $state = Dever::db('shop/sell_order')->one($where);
  708. if (!$state) {
  709. return $where['order_num'];
  710. } else {
  711. return $this->getOrderId();
  712. }
  713. }
  714. # 展示订单详情
  715. public function show()
  716. {
  717. $id = Dever::input('order_id');
  718. $config = Dever::db('shop/sell_order')->config;
  719. $info = Dever::db('shop/sell_order')->one($id);
  720. $tk_status = Dever::db('shop/sell_order_refund')->config['status'];
  721. $tk_process = Dever::db('shop/sell_order_refund')->config['process'];
  722. $status = $config['status'][$info['status']];
  723. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'type' => 1));
  724. $shop = Dever::db('shop/info')->find($info['shop_id']);
  725. $user = Dever::db('passport/user')->find($info['uid']);
  726. if (!$user['truename']) {
  727. $user['truename'] = $user['username'];
  728. }
  729. $html = '[基本信息]:<table class="layui-table"><thead><tr><th style="width:20%">项目</th><th style="width:80%">详情</th></tr> </thead><tbody>';
  730. $html .= '<tr>
  731. <td>订单号</td>
  732. <td>'.$this->table(false, array(array($info['order_num']))).'</td>
  733. </tr>';
  734. $html .= '<tr>
  735. <td>门店信息</td>
  736. <td>'.$this->table(false, array(array('门店名称:' . $shop['name'], '联系人:' . $shop['truename'], '联系电话:' . $shop['mobile']))).'</td>
  737. </tr>';
  738. $html .= '<tr>
  739. <td>用户信息</td>
  740. <td>'.$this->table(false, array(array('昵称:' . $user['username'], '联系电话:' . $user['mobile']))).'</td>
  741. </tr>';
  742. if ($tk && $tk['process'] == 1) {
  743. $status = '申请' . $tk_status[$tk['status']];
  744. $process = $tk_process[$tk['process']];
  745. $process = '';
  746. }
  747. if ($info['info']) {
  748. $html .= '<tr>
  749. <td>订单备注</td>
  750. <td>'.$this->table(false, array(array($info['info']))).'</td>
  751. </tr>';
  752. }
  753. if (!$info['refund_cash']) {
  754. $info['refund_cash'] = 0;
  755. }
  756. $html .= '<tr>
  757. <td>订单信息</td>
  758. <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>
  759. </tr>';
  760. if ($info['address_id'] > 0) {
  761. $address = Dever::load('passport/address')->getOne($info['uid'], $info['address_id']);
  762. $html .= '<tr>
  763. <td>收货地址</td>
  764. <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>
  765. </tr>';
  766. }
  767. if ($info['invoice_id'] > 0) {
  768. $invoice = Dever::load('passport/invoice')->getOne($info['uid'], $info['invoice_id']);
  769. $html .= '<tr>
  770. <td>发票信息</td>
  771. <td>'.$this->table(array('发票类型', '抬头', '税号','手机号', '邮箱'), array(array($invoice['title_type_name'], $invoice['title'], $invoice['number'], $invoice['mobile'], $invoice['email']))).'</td>
  772. </tr>';
  773. }
  774. $html .= '</tbody></table>';
  775. $html .= '[商品清单]:';
  776. $head = array('名称', '属性', '价格', '数量', '状态');
  777. $body = array();
  778. $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
  779. $goods_status = Dever::db('shop/sell_order_goods')->config['status'];
  780. foreach ($goods as $k => $v) {
  781. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  782. $status = $goods_status[$v['status']];
  783. $tk = Dever::db('shop/sell_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  784. if ($tk && $tk['process'] == 1) {
  785. $status = '申请' . $tk_status[$tk['status']];
  786. //$status .= '&nbsp;&nbsp;[' . $tk_process[$tk['process']] . ']';
  787. }
  788. if (isset($goods_info['sku'])) {
  789. $sku = $goods_info['sku']['string'];
  790. } else {
  791. $sku = '无';
  792. }
  793. $body[$k] = array
  794. (
  795. $goods_info['name'],
  796. $sku,
  797. $v['price'],
  798. $v['num'],
  799. $status,
  800. );
  801. }
  802. $html .= $this->table($head, $body);
  803. return $html;
  804. }
  805. private function table($head, $data)
  806. {
  807. $html = '';
  808. if ($head) {
  809. $html = '<table class="layui-table">';
  810. $html .= '<thead><tr>';
  811. foreach ($head as $k => $v) {
  812. $html .= '<th>'.$v.'</th>';
  813. }
  814. $html .= '</tr></thead>';
  815. $html .= '<tbody>';
  816. foreach ($data as $k => $v) {
  817. $html .= '<tr>';
  818. foreach ($v as $k1 => $v1) {
  819. $html .= '<td>'.$v1.'</td>';
  820. }
  821. $html .= '</tr>';
  822. }
  823. $html .= '</tbody>';
  824. $html .= '</table>';
  825. } else {
  826. foreach ($data as $k => $v) {
  827. $html .= '';
  828. foreach ($v as $k1 => $v1) {
  829. $html .= $v1 . '&nbsp;&nbsp;&nbsp;&nbsp;';
  830. }
  831. $html .= '';
  832. }
  833. }
  834. return $html;
  835. }
  836. }