Sell.php 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  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. $info['time'] = 0;
  120. if ($info['status'] == 1) {
  121. # 15分钟内支付,900秒
  122. $m = 900;
  123. # 支付倒计时
  124. $info['time'] = time() - $info['cdate'];
  125. if ($info['time'] >= $m) {
  126. # 已过期,自动取消
  127. $info['time'] = -1;
  128. Dever::db($this->table)->update(array('where_id' => $info['id'], 'status' => 11));
  129. $info['status'] = 11;
  130. } else {
  131. $info['time'] = $m - $info['time'];
  132. }
  133. }
  134. $info['status_name'] = $this->config['status'][$info['status']];
  135. $info['method_name'] = $this->config['method'][$info['method']];
  136. $info['pay_method_name'] = $this->config['pay_method'][$info['pay_method']];
  137. $info['pay_type_name'] = $this->config['pay_type'][$info['pay_type']];
  138. if (!$info['coupon_cash']) {
  139. $info['coupon_cash'] = 0;
  140. }
  141. if (!$info['refund_cash']) {
  142. $info['refund_cash'] = 0;
  143. }
  144. if (!$info['ps_cash']) {
  145. $info['ps_cash'] = 0;
  146. }
  147. if (!$info['ps_cash']) {
  148. $info['ps_cash'] = 0;
  149. }
  150. if (!$info['wallet_cash']) {
  151. $info['wallet_cash'] = 0;
  152. } else {
  153. $info['wallet_cash'] = round($info['wallet_cash'], 2);
  154. }
  155. if (!$info['card_code_cash']) {
  156. $info['card_code_cash'] = 0;
  157. } else {
  158. $info['card_code_cash'] = round($info['card_code_cash'], 2);
  159. }
  160. $info['card_name'] = '';
  161. if ($info['card_code_id'] > 0) {
  162. $config = Dever::db('main/sell_config')->find();
  163. $info['card_name'] = $config['card_name'];
  164. $card_code = Dever::db('card/code')->find($info['card_code_id']);
  165. if ($card_code) {
  166. $card_info = Dever::db('card/info')->find($card_code['card_id']);
  167. if ($card_info) {
  168. $card_type = Dever::db('card/type')->find($card_info['type_id']);
  169. $card = substr($card_code['card'], -4);
  170. $info['card_name'] .= '('.$card_type['name'].$card.')';
  171. }
  172. }
  173. }
  174. if ($this->type == 1) {
  175. if ($info['status'] == 2) {
  176. $info['status_name'] = '支付成功';
  177. }
  178. if ($info['status'] == 3) {
  179. if ($info['method'] == 1) {
  180. $info['status_name'] = '待自提';
  181. } else {
  182. $info['status_name'] = '配送中';
  183. }
  184. }
  185. } elseif ($this->type == 2) {
  186. if ($info['status'] == 2) {
  187. $info['status_name'] = '待处理';
  188. }
  189. if ($info['status'] == 3) {
  190. if ($info['method'] == 1) {
  191. $info['status_name'] = '待自提';
  192. } else {
  193. $info['status_name'] = '配送中';
  194. }
  195. }
  196. }
  197. if ($info['method'] == 2) {
  198. $info['ps_info'] = Dever::db('shop/sell_order_ps')->find(array('order_id' => $info['id']));
  199. if ($info['ps_info']) {
  200. $info['ps_info']['service'] = Dever::array_decode($info['ps_info']['service']);
  201. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  202. if ($info['ps_info']['qu_date']) {
  203. $info['ps_info']['qu_date'] = date('Y-m-d H:i', $info['ps_info']['qu_date']);
  204. }
  205. if ($info['ps_info']['qs_date']) {
  206. $info['ps_info']['qs_date'] = date('Y-m-d H:i', $info['ps_info']['qs_date']);
  207. }
  208. $status = Dever::db('shop/sell_order_ps')->config['status'];
  209. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  210. if ($info['ps_info']['service']) {
  211. foreach ($info['ps_info']['service'] as $k => $v) {
  212. $info['ps_info']['service'][$k]['service_name'] = '商家自送';
  213. if ($v['service_id'] > 0) {
  214. $service = Dever::db('shop/service')->find($v['service_id']);
  215. $info['ps_info']['service'][$k]['service_name'] = $service['name'];
  216. }
  217. }
  218. }
  219. }
  220. }
  221. $info['goods'] = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
  222. if ($info['uid'] && $info['uid'] > 0) {
  223. $info['user'] = Dever::load('passport/api')->info($info['uid']);
  224. }
  225. $info['shop'] = Dever::db('shop/info')->getOne($info['shop_id']);
  226. $info['cdate'] = date('Y-m-d H:i', $info['cdate']);
  227. if ($info['operdate']) {
  228. $info['operdate'] = date('Y-m-d H:i', $info['operdate']);
  229. } else {
  230. $info['operdate'] = '';
  231. }
  232. if ($info['fdate']) {
  233. $info['fdate'] = date('Y-m-d H:i', $info['fdate']);
  234. } else {
  235. $info['fdate'] = '';
  236. }
  237. $goods_status = Dever::db('shop/sell_order_goods')->config['status'];
  238. if ($info['address_id']) {
  239. $info['address'] = Dever::db('passport/address')->find($info['address_id']);
  240. }
  241. if ($this->view == 2) {
  242. foreach ($info['goods'] as $k => $v) {
  243. $info['goods'][$k]['info'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
  244. $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
  245. }
  246. if ($info['invoice_id']) {
  247. $info['invoice'] = Dever::db('passport/invoice')->find($info['invoice_id']);
  248. }
  249. if ($info['user_coupon_id']) {
  250. $coupon = Dever::db('shop/user_coupon')->find($info['user_coupon_id']);
  251. if ($coupon) {
  252. $info['coupon'] = Dever::db('goods/coupon')->find($coupon['coupon_id']);
  253. }
  254. }
  255. } else {
  256. foreach ($info['goods'] as $k => $v) {
  257. $goods = Dever::db('goods/info')->one($v['goods_id']);
  258. $info['goods'][$k]['name'] = $goods['name'];
  259. $info['goods'][$k]['cover'] = $goods['cover'];
  260. $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
  261. }
  262. }
  263. return $info;
  264. }
  265. # 取消订单
  266. public function cancel($id, $order_id)
  267. {
  268. $data = $this->getView($id, $order_id, false);
  269. if ($data['status'] == 1) {
  270. $state = Dever::db('shop/sell_order')->update(array('where_id' => $data['id'], 'status' => 7, 'operdate' => time()));
  271. if ($state) {
  272. if ($data['user_coupon_id']) {
  273. # 还原优惠券
  274. Dever::db('shop/user_coupon')->update(array('where_id' => $data['user_coupon_id'], 'status' => 1));
  275. }
  276. if (isset($data['card_code_id']) && $data['card_code_id'] && $data['card_code_cash'] > 0) {
  277. # 还原礼品卡
  278. Dever::db('card/code')->decUseCash(array('where_id' => $data['card_code_id'], 'set_use_cash' => $data['card_code_cash']));
  279. } elseif (isset($data['wallet_cash']) && $data['wallet_cash'] && $data['wallet_cash'] > 0) {
  280. # 还原钱包
  281. }
  282. $this->updateSell($data, 2);
  283. }
  284. return 'ok';
  285. } else {
  286. Dever::alert('当前订单状态不允许取消');
  287. }
  288. }
  289. # 获取优惠券
  290. public function coupon(&$data, $type = 1)
  291. {
  292. $user_coupon_id = Dever::input('user_coupon_id');
  293. $shop_coupon_id = Dever::input('shop_coupon_id');
  294. $data['user_coupon_id'] = 0;
  295. $data['coupon_id'] = 0;
  296. $data['coupon_cash'] = 0;
  297. if ($data['pay_method'] == 3) {
  298. return $data;
  299. }
  300. if ($type == 1) {
  301. if (isset($data['uid']) && $data['uid'] > 0) {
  302. # 默认选择优惠券
  303. # 查找符合要求的优惠券
  304. $coupon = Dever::db('shop/user_coupon')->getAll(array('uid' => $data['uid'], 'city' => $data['shop']['city'], 'status' => 1, 'edate' => time()));
  305. if ($coupon) {
  306. foreach ($coupon as $k => $v) {
  307. $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
  308. if ($coupon_info) {
  309. if ($v['shop_id'] != $data['shop']['id'] && $coupon_info['method'] == 3) {
  310. # 门店专属券 不能给别的门店用
  311. continue;
  312. }
  313. $kou = false;
  314. if ($coupon_info['type'] == 1) {
  315. # 满减券
  316. if ($data['price'] >= $coupon_info['total_cash']) {
  317. $kou = true;
  318. }
  319. } else {
  320. $kou = true;
  321. }
  322. if ($kou) {
  323. $coupon_info['user_coupon_id'] = $v['id'];
  324. $coupon_info['uid'] = $v['uid'];
  325. $coupon_info['edate'] = date('Y-m-d', $v['edate']);
  326. $coupon_info['shop_id'] = $v['shop_id'];
  327. $coupon_info['coupon_id'] = $v['coupon_id'];
  328. $coupon_info['city'] = $v['city'];
  329. $coupon_info = Dever::load('shop/lib/coupon')->getShop($coupon_info, $data['shop'], false);
  330. $data['coupon'][] = $coupon_info;
  331. if (!$user_coupon_id && $data['coupon_cash'] <= $coupon_info['cash']) {
  332. $data['user_coupon_id'] = $v['id'];
  333. $data['coupon_id'] = $coupon_info['id'];
  334. $data['coupon_cash'] = $coupon_info['cash'];
  335. }
  336. }
  337. }
  338. }
  339. }
  340. } else {
  341. $coupon = Dever::db('shop/coupon')->getAll(array('shop_id' => $data['shop']['id']));
  342. if ($coupon) {
  343. foreach ($coupon as $k => $v) {
  344. $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
  345. $kou = false;
  346. if ($coupon_info['type'] == 1) {
  347. # 满减券
  348. if ($data['price'] >= $coupon_info['total_cash']) {
  349. $kou = true;
  350. }
  351. } else {
  352. $kou = true;
  353. }
  354. if ($kou) {
  355. $coupon_info['shop_id'] = $v['shop_id'];
  356. $coupon_info['shop_coupon_id'] = $v['id'];
  357. $coupon_info['coupon_id'] = $v['coupon_id'];
  358. $coupon_info['city'] = $v['city'];
  359. $coupon_info = Dever::load('shop/lib/coupon')->getShop($coupon_info, $data['shop'], false);
  360. $data['coupon'][] = $coupon_info;
  361. }
  362. }
  363. }
  364. }
  365. }
  366. if ($user_coupon_id && isset($data['uid']) && $data['uid'] > 0) {
  367. # 用户自己使用优惠券
  368. if ($data['price'] <= 0) {
  369. Dever::alert('付款价格错误');
  370. }
  371. $coupon = Dever::db('shop/user_coupon')->find(array('uid' => $data['uid'], 'id' => $user_coupon_id, 'status' => 1));
  372. if (!$coupon) {
  373. Dever::alert('优惠券不可用');
  374. }
  375. if (time() > $coupon['edate']) {
  376. Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 3));
  377. Dever::alert('优惠券已过期');
  378. }
  379. $goods_coupon = Dever::db('goods/coupon')->find($coupon['coupon_id']);
  380. if (!$goods_coupon) {
  381. Dever::alert('优惠券不可用');
  382. }
  383. if ($coupon['shop_id'] != $data['shop']['id']) {
  384. if ($goods_coupon['method'] == 3) {
  385. Dever::alert('优惠券不可用');
  386. } else {
  387. $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $coupon['shop_id'], 'coupon_id' => $coupon['coupon_id'], 'city' => $coupon['city']));
  388. if (!$coupon_info) {
  389. Dever::alert('优惠券不可用');
  390. }
  391. }
  392. }
  393. if ($goods_coupon['type'] == 2 && $data['price'] < $goods_coupon['total_cash']) {
  394. Dever::alert('优惠券不可用');
  395. }
  396. $data['user_coupon_id'] = $user_coupon_id;
  397. $data['coupon_id'] = $goods_coupon['id'];
  398. $data['coupon_cash'] = $goods_coupon['cash'];
  399. } elseif ($shop_coupon_id) {
  400. # 店里直接给用优惠券
  401. if ($data['price'] <= 0) {
  402. Dever::alert('付款价格错误');
  403. }
  404. $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $data['shop']['id'], 'id' => $shop_coupon_id));
  405. if (!$coupon_info) {
  406. Dever::alert('优惠券不可用');
  407. }
  408. $goods_coupon = Dever::db('goods/coupon')->find($coupon_info['coupon_id']);
  409. if (!$goods_coupon) {
  410. Dever::alert('优惠券不可用');
  411. }
  412. if ($goods_coupon['type'] == 2 && $data['price'] < $goods_coupon['total_cash']) {
  413. Dever::alert('优惠券不可用');
  414. }
  415. $data['user_coupon_id'] = -1;
  416. $data['coupon_id'] = $goods_coupon['id'];
  417. $data['coupon_cash'] = $goods_coupon['cash'];
  418. }
  419. if (isset($data['coupon_cash']) && $data['coupon_cash'] > 0) {
  420. # 这里还要计算每个商品的优惠金额
  421. if (isset($data['list']) && $data['list']) {
  422. foreach ($data['list'] as $k => $v) {
  423. $data['list'][$k]['coupon_id'] = $data['coupon_id'];
  424. $data['list'][$k]['user_coupon_id'] = $data['user_coupon_id'];
  425. $data['list'][$k]['coupon_cash'] = round(($v['price'] * $v['buy_num'])/$data['price'], 2) * $data['coupon_cash'];
  426. }
  427. }
  428. $data['price'] -= $data['coupon_cash'];
  429. }
  430. # 计算礼品卡
  431. $card = Dever::input('card');
  432. $wallet = Dever::input('wallet');
  433. if ($data['price'] > 0 && isset($data['uid']) && $data['uid'] > 0) {
  434. if ($card) {
  435. # 验证这个礼品卡属于自己
  436. $check = Dever::db('card/code')->find($card);
  437. if ($check['dh_uid'] == $data['uid']) {
  438. if (!$check['total_cash']) {
  439. $card_info = Dever::db('card/info')->find($check['card_id']);
  440. $check['total_cash'] = $card_info['value'];
  441. }
  442. if (!$check['use_cash']) {
  443. $check['use_cash'] = 0;
  444. }
  445. $card_cash = $check['total_cash'] - $check['use_cash'];
  446. if ($card_cash > 0) {
  447. $data['card'] = array();
  448. $data['card']['id'] = $check['id'];
  449. $data['card']['card'] = $check['card'];
  450. if ($card_cash >= $data['price']) {
  451. $data['card']['cash'] = $data['price'];
  452. $data['price'] = 0;
  453. } else {
  454. $data['card']['cash'] = $card_cash;
  455. $data['price'] -= $data['card']['cash'];
  456. }
  457. $data['card']['cash'] = round($data['card']['cash'], 2);
  458. }
  459. }
  460. } elseif ($wallet && $wallet > 0) {
  461. $data['score'] = Dever::load('score/lib/core')->getUserScore($data['uid']);
  462. }
  463. }
  464. if ($data['price'] < 0) {
  465. $data['price'] = 0;
  466. }
  467. $data['price'] = round($data['price'], 2);
  468. return $data;
  469. }
  470. # 获取礼品卡 - 更改商品卡 - 废弃
  471. public function card($uid, $shop)
  472. {
  473. $card = Dever::input('card');
  474. $pwd = Dever::input('pwd');
  475. if (!$card) {
  476. Dever::alert('卡号/密码错误');
  477. }
  478. if (!$pwd) {
  479. Dever::alert('卡号/密码错误');
  480. }
  481. $data = Dever::db('goods/card_code')->find(array('card' => $card, 'pwd' => $pwd));
  482. if (!$data) {
  483. Dever::alert('卡号/密码错误');
  484. }
  485. $info = Dever::db('goods/card')->find($data['card_id']);
  486. if (!$info || ($info && $info['status'] != 1)) {
  487. Dever::alert('卡号/密码错误');
  488. }
  489. if ($data['uid'] && $data['uid'] > 0) {
  490. if ($data['uid'] != $uid) {
  491. Dever::alert('礼品卡已被领取');
  492. }
  493. if (time() > $data['edate']) {
  494. # 更新礼品卡状态
  495. $card_update['where_id'] = $data['id'];
  496. $card_update['status'] = 5;
  497. Dever::db('goods/card_code')->update($card_update);
  498. Dever::alert('礼品卡已失效');
  499. }
  500. } else {
  501. if (time() > $data['edate']) {
  502. # 更新礼品卡状态
  503. $card_update['where_id'] = $data['id'];
  504. $card_update['uid'] = $uid;
  505. $card_update['status'] = 5;
  506. if ($shop) {
  507. $card_update['shop_id'] = $shop['id'];
  508. }
  509. Dever::db('goods/card_code')->update($card_update);
  510. Dever::alert('礼品卡已失效');
  511. } else {
  512. # 更新礼品卡状态
  513. $card_update['where_id'] = $data['id'];
  514. $card_update['status'] = 2;
  515. $card_update['uid'] = $uid;
  516. $card_update['ddate'] = time();
  517. if ($shop) {
  518. $card_update['shop_id'] = $shop['id'];
  519. }
  520. Dever::db('goods/card_code')->update($card_update);
  521. }
  522. }
  523. $data = Dever::db('goods/card_code')->find($data['id']);
  524. $data['goods'] = $info['goods'];
  525. return $data;
  526. }
  527. # 得到商品和总价
  528. public function goods(&$data)
  529. {
  530. # 1自提,2配送
  531. $data['method'] = Dever::input('method', 1);
  532. $data['pay_method'] = Dever::input('pay_method');
  533. if ($data['pay_method'] == 3) {
  534. $data['card'] = $this->card($data['uid'], $data['shop']);
  535. $goods = Dever::array_decode($data['card']['goods']);
  536. $goods_id = array();
  537. $num = array();
  538. $sku_id = array();
  539. foreach ($goods as $k => $v) {
  540. $goods_id[] = $v['goods_id'];
  541. $num[] = $v['num'];
  542. $sku_id[] = -1;
  543. }
  544. } else {
  545. $goods_id = Dever::input('goods_id');
  546. if (!$goods_id) {
  547. Dever::alert('请传入商品');
  548. }
  549. $goods_id = explode(',', $goods_id);
  550. $sku_id = Dever::input('price_id');
  551. if ($sku_id) {
  552. $sku_id = explode(',', $sku_id);
  553. }
  554. $single_id = Dever::input('single_id');
  555. if ($single_id) {
  556. $single_id = explode(',', $single_id);
  557. }
  558. $input_id = Dever::input('input_id');
  559. if ($input_id) {
  560. $input_id = explode(',', $input_id);
  561. }
  562. $input_value = Dever::input('input_value');
  563. if ($input_value) {
  564. $input_value = explode(',', $input_value);
  565. }
  566. $num = Dever::input('num');
  567. if (!$num) {
  568. Dever::alert('请传入商品数量');
  569. }
  570. $num = explode(',', $num);
  571. }
  572. # 钱包金额
  573. $data['wallet_cash'] = 0;
  574. # 配送金额
  575. $data['ps_cash'] = 0;
  576. # 原价
  577. $data['oprice'] = 0;
  578. # 现价
  579. $data['price'] = 0;
  580. $data['num'] = 0;
  581. $data['name'] = array();
  582. $count = count($goods_id);
  583. # 计算总价格
  584. foreach ($goods_id as $k => $v) {
  585. $s = isset($sku_id[$k]) ? $sku_id[$k] : -1;
  586. $n = isset($num[$k]) ? $num[$k] : 1;
  587. $data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v, $s);
  588. $w = array();
  589. $w['goods_id'] = $v;
  590. $w['sku_id'] = $s;
  591. $w['shop_id'] = $data['shop']['id'];
  592. $goods_sku = Dever::db('shop/goods_sku')->find($w);
  593. if ($goods_sku && isset($goods_sku['min'])) {
  594. $data['list'][$k]['min'] = $goods_sku['min'];
  595. }
  596. $data['list'][$k]['num'] = $n;
  597. # 2是库存不足
  598. $data['list'][$k]['ku_state'] = 1;
  599. # 验证是否有货
  600. $total = Dever::load('shop/lib/info')->checkTotal($n, $v, $data['shop']['id'], $s);
  601. if (isset($data['list'][$k]['min']) && $data['list'][$k]['min'] > 0) {
  602. if ($total < $data['list'][$k]['min']) {
  603. $total = 0;
  604. }
  605. }
  606. if ($total <= 0) {
  607. $data['list'][$k]['ku_state'] = 2;
  608. $data['list'][$k]['buy_num'] = 0;
  609. }
  610. if ($data['list'][$k]['ku_state'] == 1) {
  611. $data['list'][$k]['buy_num'] = $n;
  612. $data['num'] += $n;
  613. $data['price'] += $data['list'][$k]['price'] * $n;
  614. $data['name'][] = $data['list'][$k]['name'];
  615. }
  616. }
  617. $data['name'] = implode(',', $data['name']);
  618. if ($data['method'] == 2) {
  619. $data['ps_cash'] = $data['shop']['ps_cash'];
  620. if ($data['shop']['free_ps_cash'] && $data['shop']['free_ps_cash'] > 0 && $data['price'] >= $data['shop']['free_ps_cash']) {
  621. $data['ps_cash'] = 0;
  622. }
  623. }
  624. $data['oprice'] = $data['price'];
  625. return $data;
  626. }
  627. # 发起支付
  628. public function pay($method, $pay_method, $pay_type, $user, $shop, $name, $num, $goods, $oprice, $price, $cash, $address_id, $invoice_id, $info, $card, $wallet_cash, $coupon_id, $user_coupon_id, $coupon_cash, $cart = false, $system_source = 5, $refer = '')
  629. {
  630. if (!$user) {
  631. Dever::alert('错误的用户信息');
  632. }
  633. $uid = $user['id'];
  634. if ($uid > 0) {
  635. $wechat = Dever::db('passport/wechat')->one(array('uid' => $uid, 'type' => 1, 'system_id' => 1));
  636. if (!$wechat) {
  637. //Dever::alert('错误的用户信息');
  638. }
  639. }
  640. if ($method == 2) {
  641. $ps_cash = $shop['ps_cash'];
  642. if ($shop['free_ps_cash'] && $shop['free_ps_cash'] > 0 && $price >= $shop['free_ps_cash']) {
  643. $ps_cash = 0;
  644. }
  645. $order_data['ps_cash'] = $ps_cash;
  646. $price += $order_data['ps_cash'];
  647. }
  648. $order_data['shop_id'] = $shop['id'];
  649. if ($shop['type'] == 10) {
  650. $order_data['shop_type'] = 2;
  651. } else {
  652. $order_data['shop_type'] = 1;
  653. }
  654. $order_data['area'] = $shop['area'] . ',' . $shop['id'];
  655. $order_data['uid'] = $uid;
  656. $order_data['mobile'] = $user['mobile'];
  657. $order_data['address_id'] = $address_id;
  658. $order_data['invoice_id'] = $invoice_id;
  659. $order_data['name'] = $name;
  660. $order_data['num'] = $num;
  661. $order_data['info'] = $info;
  662. $order_data['oprice'] = $oprice;
  663. $order_data['price'] = $price;
  664. $order_data['kou_cash'] = $cash;
  665. $order_data['method'] = $method;
  666. $order_data['pay_method'] = $pay_method;
  667. $order_data['pay_type'] = $pay_type;
  668. $order_data['refund_cash'] = 0;
  669. if ($card) {
  670. $order_data['card_code_id'] = $card['id'];
  671. $order_data['card_code_card'] = $card['card'];
  672. $order_data['card_code_cash'] = $card['cash'];
  673. } elseif ($wallet_cash) {
  674. $order_data['wallet_cash'] = $wallet_cash;
  675. }
  676. if ($user_coupon_id) {
  677. $order_data['user_coupon_id'] = $user_coupon_id;
  678. }
  679. if ($coupon_id) {
  680. $order_data['coupon_id'] = $coupon_id;
  681. }
  682. if ($coupon_cash) {
  683. $order_data['coupon_cash'] = $coupon_cash;
  684. }
  685. $order_data['order_num'] = $this->getOrderId();
  686. $id = Dever::db('shop/sell_order')->insert($order_data);
  687. if (!$id) {
  688. Dever::alert('支付失败');
  689. }
  690. foreach($goods as $k => $v) {
  691. if ($v['ku_state'] == 1) {
  692. $data['uid'] = $uid;
  693. $data['order_id'] = $id;
  694. $data['shop_id'] = $shop['id'];
  695. $data['goods_id'] = $v['id'];
  696. $data['sku_id'] = $v['sku_id'];
  697. $data['price'] = $v['price'];
  698. $data['num'] = $v['buy_num'];
  699. if (isset($v['user_coupon_id']) && $v['user_coupon_id']) {
  700. $data['user_coupon_id'] = $v['user_coupon_id'];
  701. }
  702. if (isset($v['coupon_id']) && $v['coupon_id']) {
  703. $data['coupon_id'] = $v['coupon_id'];
  704. }
  705. if (isset($v['coupon_cash']) && $v['coupon_cash']) {
  706. $data['coupon_cash'] = $v['coupon_cash'];
  707. } else {
  708. $data['coupon_cash'] = 0;
  709. }
  710. Dever::db('shop/sell_order_goods')->insert($data);
  711. }
  712. }
  713. # 减少库存 增加销量
  714. $order_data['id'] = $id;
  715. $this->updateSell($order_data);
  716. if ($price <= 0 || $pay_type == 2) {
  717. $param['pay_product_id'] = $id;
  718. $param['pay_order_id'] = $order_data['order_num'];
  719. $param['pay_status'] = 2;
  720. $param['pay_cash'] = $price;
  721. $param['pay_msg'] = '';
  722. $this->success($param);
  723. if ($cart) {
  724. # 来自购物车,要清空一下
  725. $this->dropCart($cart, $uid, $goods, $shop['id']);
  726. }
  727. # 使用优惠券
  728. $this->useCoupon($coupon_id, $user_coupon_id, $card, $wallet_cash);
  729. /*
  730. if ($card) {
  731. # 更新礼品卡状态
  732. $card_update['where_id'] = $card['id'];
  733. $card_update['status'] = 3;
  734. $card_update['order_id'] = $id;
  735. $card_update['shop_id'] = $shop['id'];
  736. Dever::db('goods/card_code')->update($card_update);
  737. }*/
  738. return array
  739. (
  740. 'order' => '',
  741. 'order_num' => $order_data['order_num'],
  742. 'order_id' => $id,
  743. 'type' => 'pay',
  744. );
  745. }
  746. $sell_config = Dever::db('main/sell_config')->one();
  747. # 分账
  748. $other = false;
  749. if (isset($shop['mid']) && $shop['mid']) {
  750. $other = array
  751. (
  752. 'mid' => $shop['mid'],
  753. 'order_id' => $order_data['order_num'] . '_' . $shop['id'],
  754. 'per' => $sell_config['per'],
  755. 'oprice' => $oprice,
  756. );
  757. }
  758. $param = array
  759. (
  760. 'project_id' => 1,
  761. 'channel_id' => 1,
  762. 'system_source' => $system_source,
  763. 'uid' => $uid,
  764. 'name' => $order_data['name'],
  765. 'cash' => $price,
  766. 'product_id' => $id,
  767. 'order_id' => $order_data['order_num'],
  768. 'other' => $other,
  769. 'refer' => $refer,
  770. );
  771. if ($uid > 0 && isset($wechat) && $wechat) {
  772. $param['openid'] = $wechat['openid'];
  773. }
  774. $receipt = Dever::input('receipt');
  775. if ($receipt) {
  776. $param['receipt'] = $receipt;
  777. }
  778. $result = Dever::load('pay/api.pay', $param);
  779. //$result['type'] = 'test';
  780. //$result['order'] = '';
  781. if ($cart) {
  782. # 来自购物车,要清空一下
  783. $this->dropCart($cart, $uid, $goods, $shop['id']);
  784. }
  785. # 使用优惠券
  786. $this->useCoupon($coupon_id, $user_coupon_id, $card, $wallet_cash);
  787. $result['order_num'] = $order_data['order_num'];
  788. $result['order_id'] = $id;
  789. return $result;
  790. }
  791. public function dropCart($cart, $uid, $goods, $shop_id)
  792. {
  793. $where['uid'] = $uid;
  794. $where['shop_id'] = $shop_id;
  795. foreach ($goods as $k => $v) {
  796. $where['goods_id'] = $v['id'];
  797. if ($cart == 1) {
  798. Dever::db('shop/cart')->delete($where);
  799. } else {
  800. unset($where['uid']);
  801. Dever::db('shop/shop_cart')->delete($where);
  802. }
  803. }
  804. }
  805. public function useCoupon($coupon_id, $user_coupon_id, $card, $wallet_cash)
  806. {
  807. if ($user_coupon_id) {
  808. Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 2));
  809. }
  810. if ($card) {
  811. $state = Dever::db('card/code')->incUseCash(array('where_id' => $card['id'], 'set_use_cash' => $card['cash']));
  812. } elseif ($wallet_cash) {
  813. }
  814. # 这里可以记录一下使用日志
  815. }
  816. # 再次发起支付
  817. public function rpay($id, $system_source = 5, $refer = '')
  818. {
  819. $order = Dever::db('shop/sell_order')->find($id);
  820. if (!$order) {
  821. Dever::alert('订单信息错误');
  822. }
  823. if ($order['status'] > 1) {
  824. Dever::alert('订单已支付或已取消');
  825. }
  826. $param = array
  827. (
  828. 'project_id' => 1,
  829. 'channel_id' => 1,
  830. 'system_source' => $system_source,
  831. 'uid' => $order['uid'],
  832. 'name' => $order['name'],
  833. 'cash' => $order['price'],
  834. 'product_id' => $id,
  835. 'order_id' => $order['order_num'],
  836. 'refer' => $refer,
  837. );
  838. $receipt = Dever::input('receipt');
  839. if ($receipt) {
  840. $param['receipt'] = $receipt;
  841. }
  842. $order = $this->getInfo($order);
  843. $result = Dever::load('pay/api.pay', $param);
  844. $result['order_time'] = $order['time'];
  845. $result['order_price'] = $order['price'];
  846. $result['order_num'] = $order['order_num'];
  847. $result['order_id'] = $id;
  848. return $result;
  849. }
  850. # 查询是否支付
  851. public function cpay($id, $system_source = 5, $refer = '')
  852. {
  853. $order = Dever::db('shop/sell_order')->find($id);
  854. if (!$order) {
  855. Dever::alert('订单信息错误');
  856. }
  857. $result['order_num'] = $order['order_num'];
  858. $result['order_id'] = $id;
  859. if ($order['status'] > 1 && $order['status'] <= 6) {
  860. # 已支付
  861. $result['status'] = 1;
  862. } else {
  863. # 待支付
  864. $result['status'] = 2;
  865. }
  866. return $result;
  867. }
  868. # 支付成功回调 安全加密 设置token
  869. public function success_secure_api_token()
  870. {
  871. $project_id = Dever::input('pay_project_id');
  872. $info = Dever::db('pay/project')->one($project_id);
  873. if ($info) {
  874. return $info['key'];
  875. }
  876. return 'shop_dever_2020';
  877. }
  878. # 支付成功回调 安全加密
  879. public function success_secure_api($param = array())
  880. {
  881. $this->success($param);
  882. }
  883. # 支付成功回调
  884. public function success($param = array())
  885. {
  886. $send = $param ? $param : Dever::preInput('pay_');
  887. $product_id = $send['pay_product_id'];
  888. $order_id = $send['pay_order_id'];
  889. $status = $send['pay_status'];
  890. $cash = $send['pay_cash'];
  891. $msg = $send['pay_msg'];
  892. $order = Dever::db('shop/sell_order')->one(array('id' => $product_id, 'time' => time()));
  893. if ($order && $order['status'] == 1) {
  894. $update = array();
  895. if ($status == 2) {
  896. # 生成自提码或者取件码
  897. $update['code'] = $this->getCode();
  898. if ($order['method'] == 1) {
  899. $msg = '您有一件自提商品已订购成功,取件码:' . $update['code'];
  900. } else {
  901. $msg = '您有一件外送商品已订购成功,核销码:' . $update['code'];
  902. # 分配配送信息
  903. //$this->updatePs($order);
  904. }
  905. # 发消息
  906. if (Dever::project('message') && $order['uid']) {
  907. $shop = Dever::db('shop/info')->one($order['shop_id']);
  908. $msg_param['type'] = 1;//消息类型1是订单消息
  909. $msg_param['id'] = $order['id'];
  910. $msg_param['name'] = $shop['name'];
  911. $msg_param = Dever::json_encode($msg_param);
  912. Dever::project('message') && Dever::load('message/lib/data')->push(-1, $order['uid'], '订购成功通知', $msg, 2, 1, false, $msg_param);
  913. }
  914. if ($order['pay_method'] == 2) {
  915. if ($order['refund_cash'] > 0) {
  916. $status = 6;
  917. } else {
  918. $status = 5;
  919. }
  920. $update['fdate'] = time();
  921. }
  922. $update['status'] = $status;
  923. $update['where_id'] = $order['id'];
  924. $update['paydate'] = time();
  925. Dever::db('shop/sell_order')->update($update);
  926. }
  927. }
  928. return 'ok';
  929. }
  930. # 添加配送信息
  931. public function updatePs($order, $status = 1, $service_id = -1, $order_num = '')
  932. {
  933. $where['order_id'] = $order['id'];
  934. $data = Dever::db('shop/sell_order_ps')->find($where);
  935. $service = array();
  936. if ($service_id) {
  937. $service['service_id'] = $service_id;
  938. }
  939. if ($order_num) {
  940. $service['order_num'] = $order_num;
  941. }
  942. if ($service) {
  943. $service = Dever::array_encode($service);
  944. }
  945. if (!$data) {
  946. $insert = $where;
  947. if ($service) {
  948. $insert['service'][] = $service;
  949. }
  950. $insert['status'] = $status;
  951. if ($status == 2) {
  952. $insert['qu_date'] = time();
  953. }
  954. Dever::db('shop/sell_order_ps')->insert($insert);
  955. } else {
  956. $update = $where;
  957. $update['where_id'] = $data['id'];
  958. $update['status'] = $status;
  959. if ($status == 2) {
  960. $update['qu_date'] = time();
  961. }
  962. if ($status == 3) {
  963. $update['qs_date'] = time();
  964. }
  965. if ($service) {
  966. $data['service'] = Dever::array_decode($data['service']);
  967. $update['service'] = $data['service'];
  968. $update['service'][] = $service;
  969. }
  970. Dever::db('shop/sell_order_ps')->update($update);
  971. }
  972. }
  973. # 减少库存,增加销量
  974. public function updateSell($order, $type = 1)
  975. {
  976. $where['order_id'] = $order['id'];
  977. $where['status'] = 1;
  978. $data = Dever::db('shop/sell_order_goods')->select($where);
  979. if ($data) {
  980. Dever::load('shop/lib/goods')->oper($order, $type, 2, $data);
  981. }
  982. }
  983. # 完成
  984. public function finish($info, $shop_id)
  985. {
  986. if ($info && $info['shop_id'] == $shop_id && $info['status'] <= 4) {
  987. if ($info['refund_cash'] > 0) {
  988. $status = 6;
  989. } else {
  990. $status = 5;
  991. }
  992. $state = Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'status' => $status, 'fdate' => time()));
  993. # 给用户发消息
  994. if ($info['uid'] && $info['uid'] > 0) {
  995. $shop = Dever::db('shop/info')->one($info['shop_id']);
  996. $msg_param['type'] = 1;//消息类型1是订单消息
  997. $msg_param['id'] = $info['id'];
  998. $msg_param['name'] = $shop['name'];
  999. $msg_param = Dever::json_encode($msg_param);
  1000. $msg = '您有一笔订单已签收,祝您用餐愉快~';
  1001. Dever::project('message') && Dever::project('message') && Dever::load('message/lib/data')->push(-1, $info['uid'], '订单完成通知', $msg, 1, 1, false, $msg_param);
  1002. }
  1003. if ($info['method'] == 2) {
  1004. Dever::load('shop/lib/sell')->updatePs($info, 3);
  1005. }
  1006. if ($info['pay_method'] == 3 && $info['card_code_id']) {
  1007. # 更新礼品卡状态
  1008. $card_update['where_id'] = $info['card_code_id'];
  1009. $card_update['status'] = 4;
  1010. Dever::db('goods/card_code')->update($card_update);
  1011. }
  1012. if ($info['shop_type'] == 2) {
  1013. $w['parent_type'] = 2;
  1014. $w['parent_order_id'] = $info['id'];
  1015. $w['status'] = 4;
  1016. $buy_order = Dever::db('shop/buy_order')->select($w);
  1017. if ($buy_order) {
  1018. foreach ($buy_order as $k => $v) {
  1019. Dever::load('mshop/lib/buy')->set(1,1)->finish($shop_id, $v['id']);
  1020. }
  1021. }
  1022. }
  1023. return 'ok';
  1024. } else {
  1025. Dever::alert('您没有权限核销');
  1026. }
  1027. }
  1028. # 平台订单已收货
  1029. public function yes_api()
  1030. {
  1031. $shop_id = Dever::input('shop_id');
  1032. $order_id = Dever::input('order_id');
  1033. $info = Dever::db('shop/sell_order')->find(array('id' => $order_id));
  1034. if ($info && $info['type'] == 10) {
  1035. $this->finish($info, $shop_id);
  1036. } else {
  1037. Dever::alert('您没有权限核销');
  1038. }
  1039. return 'reload';
  1040. }
  1041. # 生成自提码
  1042. public function getCode()
  1043. {
  1044. $where['code'] = Dever::rand(6, 0);
  1045. $state = Dever::db('shop/sell_order')->one($where);
  1046. if (!$state) {
  1047. return $where['code'];
  1048. } else {
  1049. return $this->getCode();
  1050. }
  1051. }
  1052. # 生成订单号
  1053. public function getOrderId()
  1054. {
  1055. $where['order_num'] = Dever::order('G');
  1056. $state = Dever::db('shop/sell_order')->one($where);
  1057. if (!$state) {
  1058. return $where['order_num'];
  1059. } else {
  1060. return $this->getOrderId();
  1061. }
  1062. }
  1063. # 展示订单详情
  1064. public function show()
  1065. {
  1066. $id = Dever::input('order_id');
  1067. $config = Dever::db('shop/sell_order')->config;
  1068. $info = Dever::db('shop/sell_order')->one($id);
  1069. $tk_status = Dever::db('shop/sell_order_refund')->config['status'];
  1070. $tk_process = Dever::db('shop/sell_order_refund')->config['process'];
  1071. $status = $config['status_manage'][$info['status']];
  1072. $tk = Dever::db('shop/sell_order_refund')->find(array('order_id' => $info['id'], 'type' => 1));
  1073. $shop = Dever::db('shop/info')->find($info['shop_id']);
  1074. $user = Dever::db('passport/user')->find($info['uid']);
  1075. $cdate = date('Y-m-d H:i', $info['cdate']);
  1076. if ($info['operdate']) {
  1077. $opertime = date('Y-m-d H:i', $info['operdate']);
  1078. } else {
  1079. $opertime = '';
  1080. }
  1081. if ($info['fdate']) {
  1082. $fdate = date('Y-m-d H:i', $info['fdate']);
  1083. } else {
  1084. $fdate = '';
  1085. }
  1086. if (!$user) {
  1087. $user['username'] = '无';
  1088. }
  1089. if (!isset($user['truename'])) {
  1090. $user['truename'] = $user['username'];
  1091. }
  1092. if (!$info['coupon_cash']) {
  1093. $info['coupon_cash'] = 0;
  1094. }
  1095. if (!$info['kou_cash']) {
  1096. $info['kou_cash'] = 0;
  1097. }
  1098. if (!$info['ps_cash']) {
  1099. $info['ps_cash'] = 0;
  1100. }
  1101. if (!$info['wallet_cash']) {
  1102. $info['wallet_cash'] = 0;
  1103. }
  1104. if (!$info['card_code_cash']) {
  1105. $info['card_code_cash'] = 0;
  1106. }
  1107. if ($info['card_code_card']) {
  1108. $info['card_code_cash'] .= '('.$info['card_code_card'].')';
  1109. }
  1110. if ($tk && $tk['process'] == 1) {
  1111. $status = '申请' . $tk_status[$tk['status']];
  1112. $process = $tk_process[$tk['process']];
  1113. $process = '';
  1114. }
  1115. $time = array();
  1116. $time[] = array('下单时间', $cdate);
  1117. if ($opertime) {
  1118. if ($info['status'] == 2) {
  1119. $time[] = array('通知时间', $opertime);
  1120. }
  1121. if ($info['status'] == 7) {
  1122. $time[] = array('取消时间:', $opertime);
  1123. }
  1124. }
  1125. if ($fdate) {
  1126. $time[] = array('完成时间:', $fdate);
  1127. }
  1128. $result = array();
  1129. $result[$info['order_num']] = array
  1130. (
  1131. 'type' => 'info',
  1132. 'content' => array
  1133. (
  1134. array
  1135. (
  1136. array('订单状态', $status),
  1137. array('支付方式', $config['pay_method'][$info['pay_method']]),
  1138. array('配送方式', $config['method'][$info['method']]),
  1139. ),
  1140. array
  1141. (
  1142. array('门店名称', $shop['name']),
  1143. array('联系人', $shop['truename']),
  1144. array('联系电话', $shop['mobile']),
  1145. ),
  1146. array
  1147. (
  1148. array('下单用户', $user['username']),
  1149. array('联系电话', $info['mobile']),
  1150. array('订单备注', $info['info']),
  1151. ),
  1152. array
  1153. (
  1154. array('支付金额', '¥ ' . round($info['price'], 2)),
  1155. array('优惠券满减', '¥ ' . $info['coupon_cash']),
  1156. array('配送费', '¥ ' . $info['ps_cash']),
  1157. ),
  1158. array
  1159. (
  1160. array('礼品卡抵扣', '¥ ' . round($info['card_code_cash'], 2)),
  1161. array('钱包抵扣', '¥ ' . round($info['wallet_cash'], 2)),
  1162. array('抹零', '¥ ' . round($info['kou_cash'], 2)),
  1163. ),
  1164. $time,
  1165. )
  1166. );
  1167. if ($info['address_id'] > 0) {
  1168. $address = Dever::load('passport/address')->getOne($info['uid'], $info['address_id']);
  1169. if ($address) {
  1170. $result['收货地址'] = array
  1171. (
  1172. 'type' => 'info',
  1173. 'content' => array
  1174. (
  1175. array
  1176. (
  1177. array('联系人', $address['contact']),
  1178. array('联系电话', $address['mobile']),
  1179. ),
  1180. array
  1181. (
  1182. array('收货地址', $address['address_info']),
  1183. array('门牌号', $address['house_number']),
  1184. ),
  1185. ),
  1186. );
  1187. }
  1188. }
  1189. if ($info['invoice_id'] > 0) {
  1190. $invoice = Dever::load('passport/invoice')->getOne($info['uid'], $info['invoice_id']);
  1191. if ($invoice) {
  1192. $result['发票信息'] = array
  1193. (
  1194. 'type' => 'info',
  1195. 'content' => array
  1196. (
  1197. array
  1198. (
  1199. array('发票类型', $invoice['title_type_name']),
  1200. array('抬头', $invoice['title']),
  1201. array('税号', $invoice['number']),
  1202. ),
  1203. array
  1204. (
  1205. array('手机号', $invoice['mobile']),
  1206. array('邮箱', $invoice['email']),
  1207. ),
  1208. ),
  1209. );
  1210. }
  1211. }
  1212. if (!$info['refund_cash']) {
  1213. $info['refund_cash'] = 0;
  1214. }
  1215. if ($info['refund_cash'] > 0 && $tk) {
  1216. $process = $tk_process[$tk['process']];
  1217. $tk['cdate'] = date('Y-m-d H:i', $tk['cdate']);
  1218. $result['退款信息'] = array
  1219. (
  1220. 'type' => 'info',
  1221. 'content' => array
  1222. (
  1223. array
  1224. (
  1225. array('退款金额', $info['refund_cash']),
  1226. array('数量', $info['num']),
  1227. array('退款进度', $process),
  1228. ),
  1229. array
  1230. (
  1231. array('申请时间', $tk['cdate']),
  1232. array('退款原因', $tk['desc']),
  1233. ),
  1234. ),
  1235. );
  1236. }
  1237. $body = $refund_body = array();
  1238. $body_total = array();
  1239. $body_total['price'] = 0;
  1240. $body_total['num'] = 0;
  1241. $refund_body_total = array();
  1242. $refund_body_total['price'] = 0;
  1243. $refund_body_total['num'] = 0;
  1244. $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
  1245. $goods_status = Dever::db('shop/sell_order_goods')->config['status'];
  1246. foreach ($goods as $k => $v) {
  1247. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  1248. $status = $goods_status[$v['status']];
  1249. $tk = Dever::db('shop/sell_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  1250. if ($tk && $tk['process'] == 1) {
  1251. $status = '申请' . $tk_status[$tk['status']];
  1252. //$status .= '&nbsp;&nbsp;[' . $tk_process[$tk['process']] . ']';
  1253. }
  1254. if (isset($goods_info['sku'])) {
  1255. $sku = $goods_info['sku']['string'];
  1256. } else {
  1257. $sku = '无';
  1258. }
  1259. if ($info['shop_type'] == 2) {
  1260. $tui = Dever::url('buy_order_tui?shop_id='.$v['shop_id'].'&order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&cash=' . $v['price'], 'shop');
  1261. $status = '<a href="javascript:;" onclick="fastEdit($(this), \''.$tui.'\', \'退款\', \'\', 1)" class="layui-btn">退款</a>';
  1262. }
  1263. $d = array
  1264. (
  1265. 'pic' => $goods_info['cover'],
  1266. 'name' => $goods_info['name'],
  1267. 'sku' => $sku,
  1268. 'price' => $v['price'],
  1269. 'num' => $v['num'],
  1270. 'status' => $status,
  1271. );
  1272. if ($tk && $v['status'] == 3) {
  1273. $d['status'] = $tk_status[$tk['status']];
  1274. $refund_body[] = $d;
  1275. $price = $v['price']*$v['num'];
  1276. $refund_body_total['price'] += $price;
  1277. $refund_body_total['num'] += $v['num'];
  1278. } else {
  1279. $body[] = $d;
  1280. $price = $v['price']*$v['num'];
  1281. $body_total['price'] += $price;
  1282. $body_total['num'] += $v['num'];
  1283. }
  1284. }
  1285. if ($body) {
  1286. if ($refund_body_total['price']) {
  1287. $body_total['set_price'] = $body_total['price'] - $refund_body_total['price'];
  1288. }
  1289. if ($refund_body_total['num']) {
  1290. $body_total['set_num'] = $body_total['num'] - $refund_body_total['num'];
  1291. }
  1292. $result['商品清单'] = array
  1293. (
  1294. 'type' => 'list',
  1295. 'content' => $body,
  1296. 'total' => $body_total,
  1297. );
  1298. }
  1299. if ($refund_body) {
  1300. $result['退款商品'] = array
  1301. (
  1302. 'type' => 'list',
  1303. 'content' => $refund_body,
  1304. 'total' => $refund_body_total,
  1305. );
  1306. }
  1307. $search_option_shop_type = Dever::input('search_option_shop_type');
  1308. if ($search_option_shop_type == 2) {
  1309. # 平台门店采购单
  1310. $buy_order = Dever::db('shop/buy_order')->getDataToId(array('type' => 1, 'type_id' => $info['shop_id'], 'parent_type' => 2, 'parent_order_id' => $info['id']));
  1311. $source_order_id = array();
  1312. if ($buy_order) {
  1313. $source_order_id = array_keys($buy_order);
  1314. }
  1315. # 平台门店结算单
  1316. if ($source_order_id) {
  1317. $cash_order = Dever::db('cash/order')->getDataByOrderId(array('source_order_id' => $source_order_id));
  1318. $col = 'cash';
  1319. }
  1320. }
  1321. if (isset($cash_order) && $cash_order) {
  1322. $head = array('序号', '结算单号', '数量', '金额', '结算类型', '入账状态', '审核状态', '结算时间', '审核未通过原因', '管理');
  1323. $body = array();
  1324. $config = Dever::db('cash/order')->config;
  1325. foreach ($cash_order as $k => $v) {
  1326. $cdate = date('Y-m-d H:i', $v['cdate']);
  1327. $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
  1328. $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
  1329. $cash = $v[$col];
  1330. $url = Dever::url('project/database/list?project=shop&table=buy_order_goods&order_id='.$v['source_order_id'].'&page_type=1', 'manage');
  1331. $v['source_order_num'] = '<a href="'.$url.'" style="color:blue">'.$v['source_order_num'].'</a>';
  1332. $m = '';
  1333. if ($v['audit'] == 1 && $v['audit_type'] == 2) {
  1334. $url = Dever::url('project/database/update?project=cash&table=order&where_id='.$v['id'].'&col=audit,desc', 'manage');
  1335. $m = '<a href="javascript:;" onclick="fastEdit($(this), \''.$url.'\', \'审核\', \'\', 1)" class="layui-btn">审核</a>';
  1336. }
  1337. $d = array
  1338. (
  1339. $k+1,
  1340. $v['order_num'],
  1341. $v['num'],
  1342. $cash,
  1343. $config['config_jstype'][$v['jstype']],
  1344. $config['config_status'][$v['status']],
  1345. $config['config_audit'][$v['audit']],
  1346. $operdate,
  1347. $v['desc'],
  1348. $m,
  1349. );
  1350. $body[] = $d;
  1351. }
  1352. $result['对账清单'] = array
  1353. (
  1354. 'type' => 'table',
  1355. 'content' => array
  1356. (
  1357. 'head' => $head,
  1358. 'body' => $body,
  1359. )
  1360. );
  1361. }
  1362. $head = array
  1363. (
  1364. 'name' => '基本信息',
  1365. 'btn' => array(),
  1366. );
  1367. $html = Dever::show($head, $result);
  1368. return $html;
  1369. }
  1370. # 第三方确认
  1371. public function tixian()
  1372. {
  1373. $param = array();
  1374. ksort($param);
  1375. $string = array();
  1376. foreach ($param as $k => $v) {
  1377. if (strstr($v, '&') || strstr($v, '@')) {
  1378. $v = urlencode($v);
  1379. }
  1380. $string[] = $k . '=' . $v;
  1381. }
  1382. }
  1383. # 发货
  1384. public function notice($info)
  1385. {
  1386. $config = Dever::db('shop/sell_order')->config;
  1387. $state = Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'status' => 3, 'operdate' => time()));
  1388. # 给用户发消息
  1389. if ($info['uid'] && $info['uid'] > 0) {
  1390. $shop = Dever::db('shop/info')->one($info['shop_id']);
  1391. $msg_param['type'] = 1;//消息类型1是订单消息
  1392. $msg_param['id'] = $info['id'];
  1393. $msg_param['name'] = $shop['name'];
  1394. $msg_param = Dever::json_encode($msg_param);
  1395. if ($info['method'] == 1) {
  1396. $msg = '您有一件自提商品已完成配货,请尽快到指定门店使用取件码取货,取件码:' . $info['code'];
  1397. $name = '取货通知';
  1398. $wechat_msg = '您的商品已完成配货';
  1399. } else {
  1400. $msg = '您有一件外送商品已开始配送,收货时请将取件码提供给配送员核实货品信息,取件码:' . $info['code'];
  1401. $name = '配送通知';
  1402. $wechat_msg = '您的商品已开始配送';
  1403. }
  1404. Dever::project('message') && Dever::load('message/lib/data')->push(-1, $info['uid'], $name, $msg, 1, 1, false, $msg_param);
  1405. if (Dever::project('wechat_applet')) {
  1406. $user = Dever::db('passport/wechat')->one(array('uid' => $info['uid'], 'system_id' => 1, 'system_source' => 5));
  1407. if ($user && $user['openid']) {
  1408. $info['status_name'] = $config['status'][$info['status']];
  1409. $send = array
  1410. (
  1411. 'character_string6' => array
  1412. (
  1413. 'value' => $info['order_num'],
  1414. ),
  1415. 'phrase7' => array
  1416. (
  1417. 'value' => $info['status_name'],
  1418. ),
  1419. 'thing8' => array
  1420. (
  1421. 'value' => $info['code'],
  1422. ),
  1423. 'amount1' => array
  1424. (
  1425. 'value' => $info['price'] . '元',
  1426. ),
  1427. 'thing5' => array
  1428. (
  1429. 'value' => $wechat_msg,
  1430. ),
  1431. );
  1432. $send = Dever::json_encode($send);
  1433. Dever::load('wechat_applet/subscribe')->sendOne('order_process', 1, $user['openid'], 'pages/app/order/order?id=' . $info['id'], $send, Dever::config('base')->wechat_applet);
  1434. }
  1435. }
  1436. }
  1437. }
  1438. # 采购订单审核 拆单
  1439. public function audit_commit()
  1440. {
  1441. $order_id = Dever::input('order_id');
  1442. $order = Dever::db('shop/sell_order')->find($order_id);
  1443. if (!$order || ($order && $order['status'] != 2)) {
  1444. Dever::alert('当前订单状态无法拆单');
  1445. }
  1446. if ($order['shop_type'] != 2) {
  1447. Dever::alert('这不是平台店的订单');
  1448. }
  1449. $order['type'] = 2;
  1450. $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $order_id));
  1451. $shop = Dever::db('shop/info')->find($order['shop_id']);
  1452. $data = Dever::load('mshop/lib/buy')->assign($goods, $order, $shop);
  1453. $send = array();
  1454. if (!$goods && $data) {
  1455. # 开始生成订单
  1456. $i = $order['order_index'];
  1457. foreach ($data as $k => $v) {
  1458. foreach ($v as $k1 => $v1) {
  1459. $v1['order_num'] .= '_' . $i;
  1460. $state = Dever::load('mshop/lib/buy')->createOrder($v1);
  1461. $send[$state] = $v1;
  1462. }
  1463. $i++;
  1464. }
  1465. Dever::db('shop/sell_order')->update(array('where_id' => $order['id'], 'status' => 3));
  1466. } else {
  1467. Dever::db('shop/sell_order')->update(array('where_id' => $order['id'], 'audit' => 1));
  1468. if ($goods) {
  1469. $string = array();
  1470. foreach ($goods as $k => $v) {
  1471. $info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  1472. if ($info['sku_name']) {
  1473. $info['name'] .= '('.$info['sku_name'].')';
  1474. }
  1475. $string[] = $info['name'];
  1476. }
  1477. $string = implode('、', $string);
  1478. Dever::alert('商品:'.$string.'库存不足或厂家未生产,请联系供货商备货,该订单进入重新审核状态');
  1479. } else {
  1480. Dever::alert('供货商库存不足,请联系供货商备货,该订单进入重新审核状态');
  1481. }
  1482. }
  1483. if ($send) {
  1484. foreach ($send as $k => $v) {
  1485. Dever::load('mshop/lib/buy')->sendMsg($v['source_type'], $v['source_id'], $k);
  1486. $order_info = Dever::db('shop/buy_order')->find(array('id' => $k, 'clear' => true));
  1487. Dever::load('cash/lib/order')->up($order_info, 1, 1);
  1488. }
  1489. }
  1490. return 'ok';
  1491. }
  1492. }