Sell.php 65 KB

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