Sell.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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 = 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]['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. $single_id = Dever::input('single_id');
  395. if ($single_id) {
  396. $single_id = explode(',', $single_id);
  397. }
  398. $input_id = Dever::input('input_id');
  399. if ($input_id) {
  400. $input_id = explode(',', $input_id);
  401. }
  402. $input_value = Dever::input('input_value');
  403. if ($input_value) {
  404. $input_value = explode(',', $input_value);
  405. }
  406. $num = Dever::input('num');
  407. if (!$num) {
  408. Dever::alert('请传入商品数量');
  409. }
  410. $num = explode(',', $num);
  411. }
  412. $data['price'] = 0;
  413. $data['num'] = 0;
  414. $data['name'] = array();
  415. $count = count($goods_id);
  416. # 计算总价格
  417. foreach ($goods_id as $k => $v) {
  418. $s = isset($sku_id[$k]) ? $sku_id[$k] : -1;
  419. $n = isset($num[$k]) ? $num[$k] : 1;
  420. $data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v, $s, $n);
  421. $data['list'][$k]['num'] = $n;
  422. # 2是库存不足
  423. $data['list'][$k]['ku_state'] = 1;
  424. # 验证是否有货
  425. $total = Dever::load('shop/lib/info')->checkTotal($n, $v, $data['shop']['id'], $s);
  426. if ($total <= 0) {
  427. $data['list'][$k]['ku_state'] = 2;
  428. $data['list'][$k]['buy_num'] = 0;
  429. }
  430. if ($data['list'][$k]['ku_state'] == 1) {
  431. $data['list'][$k]['buy_num'] = $n;
  432. $data['num'] += $n;
  433. $data['price'] += $data['list'][$k]['price'] * $n;
  434. $data['name'][] = $data['list'][$k]['name'];
  435. }
  436. }
  437. $data['name'] = implode(',', $data['name']);
  438. return $data;
  439. }
  440. # 发起支付
  441. 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 = '')
  442. {
  443. if (!$user) {
  444. Dever::alert('错误的用户信息');
  445. }
  446. $uid = $user['id'];
  447. $order_data['shop_id'] = $shop['id'];
  448. $order_data['uid'] = $uid;
  449. $order_data['mobile'] = $user['mobile'];
  450. $order_data['address_id'] = $address_id;
  451. $order_data['invoice_id'] = $invoice_id;
  452. $order_data['name'] = $name;
  453. $order_data['num'] = $num;
  454. $order_data['info'] = $info;
  455. $order_data['price'] = $price;
  456. $order_data['kou_cash'] = $cash;
  457. $order_data['method'] = $method;
  458. $order_data['pay_method'] = $pay_method;
  459. $order_data['pay_type'] = $pay_type;
  460. if ($card) {
  461. $order_data['card_code_id'] = $card['id'];
  462. }
  463. if ($user_coupon_id) {
  464. $order_data['user_coupon_id'] = $user_coupon_id;
  465. }
  466. if ($coupon_id) {
  467. $order_data['coupon_id'] = $coupon_id;
  468. }
  469. if ($coupon_cash) {
  470. $order_data['coupon_cash'] = $coupon_cash;
  471. }
  472. $order_data['order_num'] = $this->getOrderId();
  473. $id = Dever::db('shop/sell_order')->insert($order_data);
  474. if (!$id) {
  475. Dever::alert('支付失败');
  476. }
  477. foreach($goods as $k => $v) {
  478. if ($v['ku_state'] == 1) {
  479. $data['uid'] = $uid;
  480. $data['order_id'] = $id;
  481. $data['goods_id'] = $v['id'];
  482. $data['sku_id'] = $v['sku_id'];
  483. $data['price'] = $v['price'] * $v['buy_num'];
  484. $data['num'] = $v['buy_num'];
  485. if (isset($v['user_coupon_id']) && $v['user_coupon_id']) {
  486. $data['user_coupon_id'] = $v['user_coupon_id'];
  487. }
  488. if (isset($v['coupon_id']) && $v['coupon_id']) {
  489. $data['coupon_id'] = $v['coupon_id'];
  490. }
  491. if (isset($v['coupon_cash']) && $v['coupon_cash']) {
  492. $data['coupon_cash'] = $v['coupon_cash'];
  493. } else {
  494. $data['coupon_cash'] = 0;
  495. }
  496. Dever::db('shop/sell_order_goods')->insert($data);
  497. }
  498. }
  499. # 减少库存 增加销量
  500. $order_data['id'] = $id;
  501. $this->updateSell($order_data);
  502. if ($card || $price <= 0 || $pay_type == 2) {
  503. $param['pay_product_id'] = $id;
  504. $param['pay_order_id'] = $order_data['order_num'];
  505. $param['pay_status'] = 2;
  506. $param['pay_cash'] = $price;
  507. $param['pay_msg'] = '';
  508. $this->success($param);
  509. if ($cart) {
  510. # 来自购物车,要清空一下
  511. $this->dropCart($cart, $uid, $goods, $shop['id']);
  512. }
  513. # 使用优惠券
  514. $this->useCoupon($coupon_id, $user_coupon_id);
  515. if ($card) {
  516. # 更新礼品卡状态
  517. $card_update['where_id'] = $card['id'];
  518. $card_update['status'] = 2;
  519. $card_update['uid'] = $uid;
  520. $card_update['shop_id'] = $shop['id'];
  521. $card_update['order_id'] = $id;
  522. $card_update['ddate'] = time();
  523. Dever::db('goods/card_code')->update($card_update);
  524. }
  525. return array
  526. (
  527. 'order' => '',
  528. 'order_num' => $order_data['order_num'],
  529. 'order_id' => $id,
  530. 'type' => 'pay',
  531. );
  532. }
  533. $param = array
  534. (
  535. 'project_id' => 1,
  536. 'channel_id' => 1,
  537. 'system_source' => $system_source,
  538. 'uid' => $uid,
  539. 'name' => $order_data['name'],
  540. 'cash' => $price,
  541. 'product_id' => $id,
  542. 'order_id' => $order_data['order_num'],
  543. 'refer' => $refer,
  544. );
  545. $receipt = Dever::input('receipt');
  546. if ($receipt) {
  547. $param['receipt'] = $receipt;
  548. }
  549. //$result = Dever::load('pay/api.pay', $param);
  550. $result['type'] = 'test';
  551. $result['order'] = '';
  552. if ($cart) {
  553. # 来自购物车,要清空一下
  554. $this->dropCart($cart, $uid, $goods, $shop['id']);
  555. }
  556. # 使用优惠券
  557. $this->useCoupon($coupon_id, $user_coupon_id);
  558. $result['order_num'] = $order_data['order_num'];
  559. $result['order_id'] = $id;
  560. return $result;
  561. }
  562. public function dropCart($cart, $uid, $goods, $shop_id)
  563. {
  564. $where['uid'] = $uid;
  565. $where['shop_id'] = $shop_id;
  566. foreach ($goods as $k => $v) {
  567. $where['goods_id'] = $v['id'];
  568. if ($cart == 1) {
  569. Dever::db('shop/cart')->delete($where);
  570. } else {
  571. unset($where['uid']);
  572. Dever::db('shop/shop_cart')->delete($where);
  573. }
  574. }
  575. }
  576. public function useCoupon($coupon_id, $user_coupon_id)
  577. {
  578. if ($user_coupon_id) {
  579. Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 2));
  580. }
  581. # 这里可以记录一下使用日志
  582. }
  583. # 再次发起支付
  584. public function rpay($id, $system_source = 5, $refer = '')
  585. {
  586. $order = Dever::db('shop/sell_order')->find($id);
  587. if (!$order) {
  588. Dever::alert('订单信息错误');
  589. }
  590. if ($order['status'] > 1) {
  591. Dever::alert('订单已支付或已取消');
  592. }
  593. $param = array
  594. (
  595. 'project_id' => 1,
  596. 'channel_id' => 1,
  597. 'system_source' => $system_source,
  598. 'uid' => $order['uid'],
  599. 'name' => $order['name'],
  600. 'cash' => $order['price'],
  601. 'product_id' => $id,
  602. 'order_id' => $order['order_num'],
  603. 'refer' => $refer,
  604. );
  605. $receipt = Dever::input('receipt');
  606. if ($receipt) {
  607. $param['receipt'] = $receipt;
  608. }
  609. $result = Dever::load('pay/api.pay', $param);
  610. $result['order_num'] = $order['order_num'];
  611. $result['order_id'] = $id;
  612. return $result;
  613. }
  614. # 支付成功回调 安全加密 设置token
  615. public function success_secure_api_token()
  616. {
  617. $project_id = Dever::input('pay_project_id');
  618. $info = Dever::db('pay/project')->one($project_id);
  619. if ($info) {
  620. return $info['key'];
  621. }
  622. return 'shop_dever_2020';
  623. }
  624. # 支付成功回调 安全加密
  625. public function success_secure_api($param = array())
  626. {
  627. $this->success($param);
  628. }
  629. # 支付成功回调
  630. public function success($param = array())
  631. {
  632. $send = $param ? $param : Dever::preInput('pay_');
  633. $product_id = $send['pay_product_id'];
  634. $order_id = $send['pay_order_id'];
  635. $status = $send['pay_status'];
  636. $cash = $send['pay_cash'];
  637. $msg = $send['pay_msg'];
  638. $order = Dever::db('shop/sell_order')->one(array('id' => $product_id, 'time' => time()));
  639. if ($order && $order['status'] == 1) {
  640. if ($status == 2) {
  641. # 生成自提码或者取件码
  642. $update['code'] = $this->getCode();
  643. if ($order['method'] == 1) {
  644. $msg = '您有一件自提商品已订购成功,取件码:' . $update['code'];
  645. } else {
  646. $msg = '您有一件外送商品已订购成功,核销码:' . $update['code'];
  647. # 分配配送信息
  648. //$this->updatePs($order);
  649. }
  650. # 发消息
  651. if (Dever::project('message')) {
  652. $shop = Dever::db('shop/info')->one($order['shop_id']);
  653. $msg_param['type'] = 1;//消息类型1是订单消息
  654. $msg_param['id'] = $order['id'];
  655. $msg_param['name'] = $shop['name'];
  656. $msg_param = Dever::json_encode($msg_param);
  657. Dever::load('message/lib/data')->push(-1, $order['uid'], '订购成功通知', $msg, 2, 1, false, $msg_param);
  658. }
  659. }
  660. $update['status'] = 2;
  661. $update['where_id'] = $order['id'];
  662. Dever::db('shop/sell_order')->update($update);
  663. }
  664. return 'ok';
  665. }
  666. # 添加配送信息
  667. public function updatePs($order, $status = 1)
  668. {
  669. $where['order_id'] = $order['id'];
  670. $data = Dever::db('shop/sell_order_ps')->find($where);
  671. if (!$data) {
  672. $insert = $where;
  673. $insert['service_id'] = -1;
  674. $insert['price'] = 0;
  675. $insert['status'] = $status;
  676. $insert['num'] = 1;
  677. if ($status == 2) {
  678. $insert['qu_date'] = time();
  679. }
  680. Dever::db('shop/sell_order_ps')->insert($insert);
  681. } else {
  682. $update = $where;
  683. $update['where_id'] = $data['id'];
  684. $update['status'] = $status;
  685. if ($status == 2) {
  686. $update['qu_date'] = time();
  687. }
  688. if ($status == 3) {
  689. $update['qs_date'] = time();
  690. }
  691. Dever::db('shop/sell_order_ps')->update($update);
  692. }
  693. }
  694. # 减少库存,增加销量
  695. public function updateSell($order, $type = 1)
  696. {
  697. $where['order_id'] = $order['id'];
  698. $where['status'] = 1;
  699. $data = Dever::db('shop/sell_order_goods')->select($where);
  700. if ($data) {
  701. Dever::load('shop/lib/goods')->oper($order, $type, 2, $data);
  702. }
  703. }
  704. # 生成自提码
  705. public function getCode()
  706. {
  707. $where['code'] = Dever::rand(6, 0);
  708. $state = Dever::db('shop/sell_order')->one($where);
  709. if (!$state) {
  710. return $where['code'];
  711. } else {
  712. return $this->getCode();
  713. }
  714. }
  715. # 生成订单号
  716. public function getOrderId()
  717. {
  718. $where['order_num'] = Dever::order('G');
  719. $state = Dever::db('shop/sell_order')->one($where);
  720. if (!$state) {
  721. return $where['order_num'];
  722. } else {
  723. return $this->getOrderId();
  724. }
  725. }
  726. # 展示订单详情
  727. public function show()
  728. {
  729. $id = Dever::input('order_id');
  730. $config = Dever::db('shop/sell_order')->config;
  731. $info = Dever::db('shop/sell_order')->one($id);
  732. $tk_status = Dever::db('shop/sell_order_refund')->config['status'];
  733. $tk_process = Dever::db('shop/sell_order_refund')->config['process'];
  734. $status = $config['status'][$info['status']];
  735. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'type' => 1));
  736. $shop = Dever::db('shop/info')->find($info['shop_id']);
  737. $user = Dever::db('passport/user')->find($info['uid']);
  738. if (!$user['truename']) {
  739. $user['truename'] = $user['username'];
  740. }
  741. $html = '[基本信息]:<table class="layui-table"><thead><tr><th style="width:20%">项目</th><th style="width:80%">详情</th></tr> </thead><tbody>';
  742. $html .= '<tr>
  743. <td>订单号</td>
  744. <td>'.$this->table(false, array(array($info['order_num']))).'</td>
  745. </tr>';
  746. $html .= '<tr>
  747. <td>门店信息</td>
  748. <td>'.$this->table(false, array(array('门店名称:' . $shop['name'], '联系人:' . $shop['truename'], '联系电话:' . $shop['mobile']))).'</td>
  749. </tr>';
  750. $html .= '<tr>
  751. <td>用户信息</td>
  752. <td>'.$this->table(false, array(array('昵称:' . $user['username'], '联系电话:' . $user['mobile']))).'</td>
  753. </tr>';
  754. if ($tk && $tk['process'] == 1) {
  755. $status = '申请' . $tk_status[$tk['status']];
  756. $process = $tk_process[$tk['process']];
  757. $process = '';
  758. }
  759. if ($info['info']) {
  760. $html .= '<tr>
  761. <td>订单备注</td>
  762. <td>'.$this->table(false, array(array($info['info']))).'</td>
  763. </tr>';
  764. }
  765. if (!$info['refund_cash']) {
  766. $info['refund_cash'] = 0;
  767. }
  768. $html .= '<tr>
  769. <td>订单信息</td>
  770. <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>
  771. </tr>';
  772. if ($info['address_id'] > 0) {
  773. $address = Dever::load('passport/address')->getOne($info['uid'], $info['address_id']);
  774. $html .= '<tr>
  775. <td>收货地址</td>
  776. <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>
  777. </tr>';
  778. }
  779. if ($info['invoice_id'] > 0) {
  780. $invoice = Dever::load('passport/invoice')->getOne($info['uid'], $info['invoice_id']);
  781. $html .= '<tr>
  782. <td>发票信息</td>
  783. <td>'.$this->table(array('发票类型', '抬头', '税号','手机号', '邮箱'), array(array($invoice['title_type_name'], $invoice['title'], $invoice['number'], $invoice['mobile'], $invoice['email']))).'</td>
  784. </tr>';
  785. }
  786. $html .= '</tbody></table>';
  787. $html .= '[商品清单]:';
  788. $head = array('名称', '属性', '价格', '数量', '状态');
  789. $body = array();
  790. $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
  791. $goods_status = Dever::db('shop/sell_order_goods')->config['status'];
  792. foreach ($goods as $k => $v) {
  793. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  794. $status = $goods_status[$v['status']];
  795. $tk = Dever::db('shop/sell_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  796. if ($tk && $tk['process'] == 1) {
  797. $status = '申请' . $tk_status[$tk['status']];
  798. //$status .= '&nbsp;&nbsp;[' . $tk_process[$tk['process']] . ']';
  799. }
  800. if (isset($goods_info['sku'])) {
  801. $sku = $goods_info['sku']['string'];
  802. } else {
  803. $sku = '无';
  804. }
  805. $body[$k] = array
  806. (
  807. $goods_info['name'],
  808. $sku,
  809. $v['price'],
  810. $v['num'],
  811. $status,
  812. );
  813. }
  814. $html .= $this->table($head, $body);
  815. return $html;
  816. }
  817. private function table($head, $data)
  818. {
  819. $html = '';
  820. if ($head) {
  821. $html = '<table class="layui-table">';
  822. $html .= '<thead><tr>';
  823. foreach ($head as $k => $v) {
  824. $html .= '<th>'.$v.'</th>';
  825. }
  826. $html .= '</tr></thead>';
  827. $html .= '<tbody>';
  828. foreach ($data as $k => $v) {
  829. $html .= '<tr>';
  830. foreach ($v as $k1 => $v1) {
  831. $html .= '<td>'.$v1.'</td>';
  832. }
  833. $html .= '</tr>';
  834. }
  835. $html .= '</tbody>';
  836. $html .= '</table>';
  837. } else {
  838. foreach ($data as $k => $v) {
  839. $html .= '';
  840. foreach ($v as $k1 => $v1) {
  841. $html .= $v1 . '&nbsp;&nbsp;&nbsp;&nbsp;';
  842. }
  843. $html .= '';
  844. }
  845. }
  846. return $html;
  847. }
  848. }