Sell.php 37 KB

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