Sell.php 44 KB

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