Sell.php 40 KB

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