Sell.php 53 KB

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