Sell.php 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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. if ($status == 2) {
  736. # 生成自提码或者取件码
  737. $update['code'] = $this->getCode();
  738. if ($order['method'] == 1) {
  739. $msg = '您有一件自提商品已订购成功,取件码:' . $update['code'];
  740. } else {
  741. $msg = '您有一件外送商品已订购成功,核销码:' . $update['code'];
  742. # 分配配送信息
  743. //$this->updatePs($order);
  744. }
  745. # 发消息
  746. if (Dever::project('message') && $order['uid']) {
  747. $shop = Dever::db('shop/info')->one($order['shop_id']);
  748. $msg_param['type'] = 1;//消息类型1是订单消息
  749. $msg_param['id'] = $order['id'];
  750. $msg_param['name'] = $shop['name'];
  751. $msg_param = Dever::json_encode($msg_param);
  752. Dever::load('message/lib/data')->push(-1, $order['uid'], '订购成功通知', $msg, 2, 1, false, $msg_param);
  753. }
  754. $update = array();
  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'][$info['status']];
  839. $tk = Dever::db('shop/buy_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 ($tk && $tk['process'] == 1) {
  860. $status = '申请' . $tk_status[$tk['status']];
  861. $process = $tk_process[$tk['process']];
  862. $process = '';
  863. }
  864. $time = array('下单时间:' . $cdate);
  865. if ($opertime) {
  866. if ($info['status'] == 2) {
  867. $time[] = '通知时间:' . $opertime;
  868. }
  869. if ($info['status'] == 7) {
  870. $time[] = '取消时间:' . $opertime;
  871. }
  872. }
  873. if ($fdate) {
  874. $time[] = '完成时间:' . $fdate;
  875. }
  876. $result = array();
  877. $result['基本信息'] = array
  878. (
  879. 'head' => array('项目', '详情'),
  880. 'body' => array
  881. (
  882. array('订单信息', array('订单号:' . $info['order_num'], '金额:' . $info['price'], '数量:' . $info['num'], '状态:' . $status, '支付方式:' . $config['pay_method'][$info['pay_method']], '配送方式:' . $config['method'][$info['method']])),
  883. array('订单时间', $time),
  884. array('门店信息', array('门店名称:' . $shop['name'], '联系人:' . $shop['truename'], '联系电话:' . $shop['mobile'])),
  885. array('用户信息', array('昵称:' . $user['username'], '联系电话:' . $info['mobile'])),
  886. array('订单备注', $info['info']),
  887. array('其他信息', array('优惠券满减:' . $info['coupon_cash'], '抹零:' . $info['kou_cash'], '配送费:' . $info['ps_cash'])),
  888. )
  889. );
  890. if ($info['address_id'] > 0) {
  891. $address = Dever::load('passport/address')->getOne($info['uid'], $info['address_id']);
  892. if ($address) {
  893. $result['收货地址'] = array
  894. (
  895. 'head' => array('地址', '门牌号', '姓名', '手机号'),
  896. 'body' => array($address['address'], $address['house_number'], $address['contact'], $address['mobile'])
  897. );
  898. }
  899. }
  900. if ($info['invoice_id'] > 0) {
  901. $invoice = Dever::load('passport/invoice')->getOne($info['uid'], $info['invoice_id']);
  902. if ($invoice) {
  903. $result['发票信息'] = array
  904. (
  905. 'head' => array('发票类型', '抬头', '税号','手机号', '邮箱'),
  906. 'body' => array($invoice['title_type_name'], $invoice['title'], $invoice['number'], $invoice['mobile'], $invoice['email'])
  907. );
  908. }
  909. }
  910. if (!$info['refund_cash']) {
  911. $info['refund_cash'] = 0;
  912. }
  913. if ($info['refund_cash'] > 0 && $tk) {
  914. $process = $tk_process[$tk['process']];
  915. $result['退款信息'] = array
  916. (
  917. 'head' => array('退款金额', '数量', '退款原因', '退款进度'),
  918. 'body' => array($info['refund_cash'], $info['num'], $tk['desc'], $process)
  919. );
  920. }
  921. $body = $refund_body = array();
  922. $body_total = array();
  923. $body_total['price'] = 0;
  924. $body_total['num'] = 0;
  925. $refund_body_total = array();
  926. $refund_body_total['price'] = 0;
  927. $refund_body_total['num'] = 0;
  928. $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
  929. $goods_status = Dever::db('shop/sell_order_goods')->config['status'];
  930. foreach ($goods as $k => $v) {
  931. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  932. $status = $goods_status[$v['status']];
  933. $tk = Dever::db('shop/sell_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  934. if ($tk && $tk['process'] == 1) {
  935. $status = '申请' . $tk_status[$tk['status']];
  936. //$status .= '&nbsp;&nbsp;[' . $tk_process[$tk['process']] . ']';
  937. }
  938. if (isset($goods_info['sku'])) {
  939. $sku = $goods_info['sku']['string'];
  940. } else {
  941. $sku = '无';
  942. }
  943. if ($tk && $v['status'] == 3) {
  944. $refund_body[] = array
  945. (
  946. $goods_info['name'],
  947. $sku,
  948. $v['price'],
  949. $v['num'],
  950. $tk_status[$tk['status']],
  951. );
  952. $price = $v['price']*$v['num'];
  953. $refund_body_total['price'] += $price;
  954. $refund_body_total['num'] += $v['num'];
  955. } else {
  956. $body[] = array
  957. (
  958. $goods_info['name'],
  959. $sku,
  960. $v['price'],
  961. $v['num'],
  962. $status,
  963. );
  964. $price = $v['price']*$v['num'];
  965. $body_total['price'] += $price;
  966. $body_total['num'] += $v['num'];
  967. }
  968. }
  969. if ($body) {
  970. $body[] = array
  971. (
  972. '合计',
  973. '-',
  974. round($body_total['price'], 2),
  975. $body_total['num'],
  976. '-'
  977. );
  978. $result['商品清单'] = array
  979. (
  980. 'head' => array('名称', '属性', '单价', '数量', '状态'),
  981. 'body' => $body,
  982. );
  983. }
  984. if ($refund_body) {
  985. $refund_body[] = array
  986. (
  987. '合计',
  988. '-',
  989. round($refund_body_total['price'], 2),
  990. $refund_body_total['num'],
  991. '-'
  992. );
  993. $result['退款商品'] = array
  994. (
  995. 'head' => array('名称', '属性', '单价', '数量', '状态'),
  996. 'body' => $refund_body,
  997. );
  998. }
  999. $html = Dever::show($result);
  1000. return $html;
  1001. }
  1002. # 打印小票信息
  1003. }