Sell.php 40 KB

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