Sell.php 38 KB

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