Sell.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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. if ($data['method'] == 2) {
  528. $data['ps_cash'] = $data['shop']['ps_cash'];
  529. }
  530. return $data;
  531. }
  532. # 发起支付
  533. 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 = '')
  534. {
  535. if (!$user) {
  536. Dever::alert('错误的用户信息');
  537. }
  538. $uid = $user['id'];
  539. if ($method == 2) {
  540. $order_data['ps_cash'] = $shop['ps_cash'];
  541. $price += $order_data['ps_cash'];
  542. }
  543. $order_data['shop_id'] = $shop['id'];
  544. $order_data['uid'] = $uid;
  545. $order_data['mobile'] = $user['mobile'];
  546. $order_data['address_id'] = $address_id;
  547. $order_data['invoice_id'] = $invoice_id;
  548. $order_data['name'] = $name;
  549. $order_data['num'] = $num;
  550. $order_data['info'] = $info;
  551. $order_data['price'] = $price;
  552. $order_data['kou_cash'] = $cash;
  553. $order_data['method'] = $method;
  554. $order_data['pay_method'] = $pay_method;
  555. $order_data['pay_type'] = $pay_type;
  556. $order_data['refund_cash'] = 0;
  557. if ($card) {
  558. $order_data['card_code_id'] = $card['id'];
  559. }
  560. if ($user_coupon_id) {
  561. $order_data['user_coupon_id'] = $user_coupon_id;
  562. }
  563. if ($coupon_id) {
  564. $order_data['coupon_id'] = $coupon_id;
  565. }
  566. if ($coupon_cash) {
  567. $order_data['coupon_cash'] = $coupon_cash;
  568. }
  569. $order_data['order_num'] = $this->getOrderId();
  570. $id = Dever::db('shop/sell_order')->insert($order_data);
  571. if (!$id) {
  572. Dever::alert('支付失败');
  573. }
  574. foreach($goods as $k => $v) {
  575. if ($v['ku_state'] == 1) {
  576. $data['uid'] = $uid;
  577. $data['order_id'] = $id;
  578. $data['goods_id'] = $v['id'];
  579. $data['sku_id'] = $v['sku_id'];
  580. $data['price'] = $v['price'];
  581. $data['num'] = $v['buy_num'];
  582. if (isset($v['user_coupon_id']) && $v['user_coupon_id']) {
  583. $data['user_coupon_id'] = $v['user_coupon_id'];
  584. }
  585. if (isset($v['coupon_id']) && $v['coupon_id']) {
  586. $data['coupon_id'] = $v['coupon_id'];
  587. }
  588. if (isset($v['coupon_cash']) && $v['coupon_cash']) {
  589. $data['coupon_cash'] = $v['coupon_cash'];
  590. } else {
  591. $data['coupon_cash'] = 0;
  592. }
  593. Dever::db('shop/sell_order_goods')->insert($data);
  594. }
  595. }
  596. # 减少库存 增加销量
  597. $order_data['id'] = $id;
  598. $this->updateSell($order_data);
  599. if ($card || $price <= 0 || $pay_type == 2) {
  600. $param['pay_product_id'] = $id;
  601. $param['pay_order_id'] = $order_data['order_num'];
  602. $param['pay_status'] = 2;
  603. $param['pay_cash'] = $price;
  604. $param['pay_msg'] = '';
  605. $this->success($param);
  606. if ($cart) {
  607. # 来自购物车,要清空一下
  608. $this->dropCart($cart, $uid, $goods, $shop['id']);
  609. }
  610. # 使用优惠券
  611. $this->useCoupon($coupon_id, $user_coupon_id);
  612. if ($card) {
  613. # 更新礼品卡状态
  614. $card_update['where_id'] = $card['id'];
  615. $card_update['status'] = 3;
  616. $card_update['order_id'] = $id;
  617. $card_update['shop_id'] = $shop['id'];
  618. Dever::db('goods/card_code')->update($card_update);
  619. }
  620. return array
  621. (
  622. 'order' => '',
  623. 'order_num' => $order_data['order_num'],
  624. 'order_id' => $id,
  625. 'type' => 'pay',
  626. );
  627. }
  628. $param = array
  629. (
  630. 'project_id' => 1,
  631. 'channel_id' => 1,
  632. 'system_source' => $system_source,
  633. 'uid' => $uid,
  634. 'name' => $order_data['name'],
  635. 'cash' => $price,
  636. 'product_id' => $id,
  637. 'order_id' => $order_data['order_num'],
  638. 'refer' => $refer,
  639. );
  640. $receipt = Dever::input('receipt');
  641. if ($receipt) {
  642. $param['receipt'] = $receipt;
  643. }
  644. //$result = Dever::load('pay/api.pay', $param);
  645. $result['type'] = 'test';
  646. $result['order'] = '';
  647. if ($cart) {
  648. # 来自购物车,要清空一下
  649. $this->dropCart($cart, $uid, $goods, $shop['id']);
  650. }
  651. # 使用优惠券
  652. $this->useCoupon($coupon_id, $user_coupon_id);
  653. $result['order_num'] = $order_data['order_num'];
  654. $result['order_id'] = $id;
  655. return $result;
  656. }
  657. public function dropCart($cart, $uid, $goods, $shop_id)
  658. {
  659. $where['uid'] = $uid;
  660. $where['shop_id'] = $shop_id;
  661. foreach ($goods as $k => $v) {
  662. $where['goods_id'] = $v['id'];
  663. if ($cart == 1) {
  664. Dever::db('shop/cart')->delete($where);
  665. } else {
  666. unset($where['uid']);
  667. Dever::db('shop/shop_cart')->delete($where);
  668. }
  669. }
  670. }
  671. public function useCoupon($coupon_id, $user_coupon_id)
  672. {
  673. if ($user_coupon_id) {
  674. Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 2));
  675. }
  676. # 这里可以记录一下使用日志
  677. }
  678. # 再次发起支付
  679. public function rpay($id, $system_source = 5, $refer = '')
  680. {
  681. $order = Dever::db('shop/sell_order')->find($id);
  682. if (!$order) {
  683. Dever::alert('订单信息错误');
  684. }
  685. if ($order['status'] > 1) {
  686. Dever::alert('订单已支付或已取消');
  687. }
  688. $param = array
  689. (
  690. 'project_id' => 1,
  691. 'channel_id' => 1,
  692. 'system_source' => $system_source,
  693. 'uid' => $order['uid'],
  694. 'name' => $order['name'],
  695. 'cash' => $order['price'],
  696. 'product_id' => $id,
  697. 'order_id' => $order['order_num'],
  698. 'refer' => $refer,
  699. );
  700. $receipt = Dever::input('receipt');
  701. if ($receipt) {
  702. $param['receipt'] = $receipt;
  703. }
  704. $result = Dever::load('pay/api.pay', $param);
  705. $result['order_num'] = $order['order_num'];
  706. $result['order_id'] = $id;
  707. return $result;
  708. }
  709. # 支付成功回调 安全加密 设置token
  710. public function success_secure_api_token()
  711. {
  712. $project_id = Dever::input('pay_project_id');
  713. $info = Dever::db('pay/project')->one($project_id);
  714. if ($info) {
  715. return $info['key'];
  716. }
  717. return 'shop_dever_2020';
  718. }
  719. # 支付成功回调 安全加密
  720. public function success_secure_api($param = array())
  721. {
  722. $this->success($param);
  723. }
  724. # 支付成功回调
  725. public function success($param = array())
  726. {
  727. $send = $param ? $param : Dever::preInput('pay_');
  728. $product_id = $send['pay_product_id'];
  729. $order_id = $send['pay_order_id'];
  730. $status = $send['pay_status'];
  731. $cash = $send['pay_cash'];
  732. $msg = $send['pay_msg'];
  733. $order = Dever::db('shop/sell_order')->one(array('id' => $product_id, 'time' => time()));
  734. if ($order && $order['status'] == 1) {
  735. $update = array();
  736. if ($status == 2) {
  737. # 生成自提码或者取件码
  738. $update['code'] = $this->getCode();
  739. if ($order['method'] == 1) {
  740. $msg = '您有一件自提商品已订购成功,取件码:' . $update['code'];
  741. } else {
  742. $msg = '您有一件外送商品已订购成功,核销码:' . $update['code'];
  743. # 分配配送信息
  744. //$this->updatePs($order);
  745. }
  746. # 发消息
  747. if (Dever::project('message') && $order['uid']) {
  748. $shop = Dever::db('shop/info')->one($order['shop_id']);
  749. $msg_param['type'] = 1;//消息类型1是订单消息
  750. $msg_param['id'] = $order['id'];
  751. $msg_param['name'] = $shop['name'];
  752. $msg_param = Dever::json_encode($msg_param);
  753. Dever::load('message/lib/data')->push(-1, $order['uid'], '订购成功通知', $msg, 2, 1, false, $msg_param);
  754. }
  755. if ($order['pay_method'] == 2) {
  756. if ($order['refund_cash'] > 0) {
  757. $status = 6;
  758. } else {
  759. $status = 5;
  760. }
  761. $update['fdate'] = time();
  762. }
  763. $update['status'] = $status;
  764. $update['where_id'] = $order['id'];
  765. Dever::db('shop/sell_order')->update($update);
  766. }
  767. }
  768. return 'ok';
  769. }
  770. # 添加配送信息
  771. public function updatePs($order, $status = 1)
  772. {
  773. $where['order_id'] = $order['id'];
  774. $data = Dever::db('shop/sell_order_ps')->find($where);
  775. if (!$data) {
  776. $insert = $where;
  777. $insert['service_id'] = -1;
  778. $insert['price'] = 0;
  779. $insert['status'] = $status;
  780. $insert['num'] = 1;
  781. if ($status == 2) {
  782. $insert['qu_date'] = time();
  783. }
  784. Dever::db('shop/sell_order_ps')->insert($insert);
  785. } else {
  786. $update = $where;
  787. $update['where_id'] = $data['id'];
  788. $update['status'] = $status;
  789. if ($status == 2) {
  790. $update['qu_date'] = time();
  791. }
  792. if ($status == 3) {
  793. $update['qs_date'] = time();
  794. }
  795. Dever::db('shop/sell_order_ps')->update($update);
  796. }
  797. }
  798. # 减少库存,增加销量
  799. public function updateSell($order, $type = 1)
  800. {
  801. $where['order_id'] = $order['id'];
  802. $where['status'] = 1;
  803. $data = Dever::db('shop/sell_order_goods')->select($where);
  804. if ($data) {
  805. Dever::load('shop/lib/goods')->oper($order, $type, 2, $data);
  806. }
  807. }
  808. # 生成自提码
  809. public function getCode()
  810. {
  811. $where['code'] = Dever::rand(6, 0);
  812. $state = Dever::db('shop/sell_order')->one($where);
  813. if (!$state) {
  814. return $where['code'];
  815. } else {
  816. return $this->getCode();
  817. }
  818. }
  819. # 生成订单号
  820. public function getOrderId()
  821. {
  822. $where['order_num'] = Dever::order('G');
  823. $state = Dever::db('shop/sell_order')->one($where);
  824. if (!$state) {
  825. return $where['order_num'];
  826. } else {
  827. return $this->getOrderId();
  828. }
  829. }
  830. # 展示订单详情
  831. public function show()
  832. {
  833. $id = Dever::input('order_id');
  834. $config = Dever::db('shop/sell_order')->config;
  835. $info = Dever::db('shop/sell_order')->one($id);
  836. $tk_status = Dever::db('shop/sell_order_refund')->config['status'];
  837. $tk_process = Dever::db('shop/sell_order_refund')->config['process'];
  838. $status = $config['status_manage'][$info['status']];
  839. $tk = Dever::db('shop/sell_order_refund')->find(array('order_id' => $info['id'], 'type' => 1));
  840. $shop = Dever::db('shop/info')->find($info['shop_id']);
  841. $user = Dever::db('passport/user')->find($info['uid']);
  842. $cdate = date('Y-m-d H:i', $info['cdate']);
  843. if ($info['operdate']) {
  844. $opertime = date('Y-m-d H:i', $info['operdate']);
  845. } else {
  846. $opertime = '';
  847. }
  848. if ($info['fdate']) {
  849. $fdate = date('Y-m-d H:i', $info['fdate']);
  850. } else {
  851. $fdate = '';
  852. }
  853. if (!$user) {
  854. $user['username'] = '无';
  855. }
  856. if (!isset($user['truename'])) {
  857. $user['truename'] = $user['username'];
  858. }
  859. if (!$info['coupon_cash']) {
  860. $info['coupon_cash'] = 0;
  861. }
  862. if (!$info['kou_cash']) {
  863. $info['kou_cash'] = 0;
  864. }
  865. if (!$info['ps_cash']) {
  866. $info['ps_cash'] = 0;
  867. }
  868. if ($tk && $tk['process'] == 1) {
  869. $status = '申请' . $tk_status[$tk['status']];
  870. $process = $tk_process[$tk['process']];
  871. $process = '';
  872. }
  873. $time = array();
  874. $time[] = array('下单时间', $cdate);
  875. if ($opertime) {
  876. if ($info['status'] == 2) {
  877. $time[] = array('通知时间', $opertime);
  878. }
  879. if ($info['status'] == 7) {
  880. $time[] = array('取消时间:', $opertime);
  881. }
  882. }
  883. if ($fdate) {
  884. $time[] = array('完成时间:', $fdate);
  885. }
  886. $result = array();
  887. $result[$info['order_num']] = array
  888. (
  889. 'type' => 'info',
  890. 'content' => array
  891. (
  892. array
  893. (
  894. array('订单状态', $status),
  895. array('支付方式', $config['pay_method'][$info['pay_method']]),
  896. array('配送方式', $config['method'][$info['method']]),
  897. ),
  898. array
  899. (
  900. array('门店名称', $shop['name']),
  901. array('联系人', $shop['truename']),
  902. array('联系电话', $shop['mobile']),
  903. ),
  904. array
  905. (
  906. array('下单用户', $user['username']),
  907. array('联系电话', $info['mobile']),
  908. array('订单备注', $info['info']),
  909. ),
  910. array
  911. (
  912. array('优惠券满减', '¥ ' . $info['coupon_cash']),
  913. array('抹零', '¥ ' . $info['kou_cash']),
  914. array('配送费', '¥ ' . $info['ps_cash']),
  915. ),
  916. $time,
  917. )
  918. );
  919. if ($info['address_id'] > 0) {
  920. $address = Dever::load('passport/address')->getOne($info['uid'], $info['address_id']);
  921. if ($address) {
  922. $result['收货地址'] = array
  923. (
  924. 'type' => 'info',
  925. 'content' => array
  926. (
  927. array
  928. (
  929. array('联系人', $address['contact']),
  930. array('联系电话', $address['mobile']),
  931. ),
  932. array
  933. (
  934. array('收货地址', $address['address']),
  935. array('门牌号', $address['house_number']),
  936. ),
  937. ),
  938. );
  939. }
  940. }
  941. if ($info['invoice_id'] > 0) {
  942. $invoice = Dever::load('passport/invoice')->getOne($info['uid'], $info['invoice_id']);
  943. if ($invoice) {
  944. $result['发票信息'] = array
  945. (
  946. 'type' => 'info',
  947. 'content' => array
  948. (
  949. array
  950. (
  951. array('发票类型', $invoice['title_type_name']),
  952. array('抬头', $invoice['title']),
  953. array('税号', $invoice['number']),
  954. ),
  955. array
  956. (
  957. array('手机号', $invoice['mobile']),
  958. array('邮箱', $invoice['email']),
  959. ),
  960. ),
  961. );
  962. }
  963. }
  964. if (!$info['refund_cash']) {
  965. $info['refund_cash'] = 0;
  966. }
  967. if ($info['refund_cash'] > 0 && $tk) {
  968. $process = $tk_process[$tk['process']];
  969. $tk['cdate'] = date('Y-m-d H:i', $tk['cdate']);
  970. $result['退款信息'] = array
  971. (
  972. 'type' => 'info',
  973. 'content' => array
  974. (
  975. array
  976. (
  977. array('退款金额', $info['refund_cash']),
  978. array('数量', $info['num']),
  979. array('退款进度', $process),
  980. ),
  981. array
  982. (
  983. array('申请时间', $tk['cdate']),
  984. array('退款原因', $tk['desc']),
  985. ),
  986. ),
  987. );
  988. }
  989. $body = $refund_body = array();
  990. $body_total = array();
  991. $body_total['price'] = 0;
  992. $body_total['num'] = 0;
  993. $refund_body_total = array();
  994. $refund_body_total['price'] = 0;
  995. $refund_body_total['num'] = 0;
  996. $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
  997. $goods_status = Dever::db('shop/sell_order_goods')->config['status'];
  998. foreach ($goods as $k => $v) {
  999. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  1000. $status = $goods_status[$v['status']];
  1001. $tk = Dever::db('shop/sell_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  1002. if ($tk && $tk['process'] == 1) {
  1003. $status = '申请' . $tk_status[$tk['status']];
  1004. //$status .= '&nbsp;&nbsp;[' . $tk_process[$tk['process']] . ']';
  1005. }
  1006. if (isset($goods_info['sku'])) {
  1007. $sku = $goods_info['sku']['string'];
  1008. } else {
  1009. $sku = '无';
  1010. }
  1011. $d = array
  1012. (
  1013. 'pic' => $goods_info['cover'],
  1014. 'name' => $goods_info['name'],
  1015. 'sku' => $sku,
  1016. 'price' => $v['price'],
  1017. 'num' => $v['num'],
  1018. 'status' => $status,
  1019. );
  1020. if ($tk && $v['status'] == 3) {
  1021. $d['status'] = $tk_status[$tk['status']];
  1022. $refund_body[] = $d;
  1023. $price = $v['price']*$v['num'];
  1024. $refund_body_total['price'] += $price;
  1025. $refund_body_total['num'] += $v['num'];
  1026. } else {
  1027. $body[] = $d;
  1028. $price = $v['price']*$v['num'];
  1029. $body_total['price'] += $price;
  1030. $body_total['num'] += $v['num'];
  1031. }
  1032. }
  1033. if ($body) {
  1034. if ($refund_body_total['price']) {
  1035. $body_total['set_price'] = $body_total['price'] - $refund_body_total['price'];
  1036. }
  1037. if ($refund_body_total['num']) {
  1038. $body_total['set_num'] = $body_total['num'] - $refund_body_total['num'];
  1039. }
  1040. $result['商品清单'] = array
  1041. (
  1042. 'type' => 'list',
  1043. 'content' => $body,
  1044. 'total' => $body_total,
  1045. );
  1046. }
  1047. if ($refund_body) {
  1048. $result['退款商品'] = array
  1049. (
  1050. 'type' => 'list',
  1051. 'content' => $refund_body,
  1052. 'total' => $refund_body_total,
  1053. );
  1054. }
  1055. $head = array
  1056. (
  1057. 'name' => '基本信息',
  1058. 'btn' => array(),
  1059. );
  1060. $html = Dever::show($head, $result);
  1061. return $html;
  1062. }
  1063. # 打印小票信息
  1064. }