Sell.php 38 KB

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