Sell.php 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163
  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. # 还原库存
  130. if ($this->type == 1) {
  131. $this->cancel($info['uid'], $info['id'], 11);
  132. } else {
  133. $this->type = 2;
  134. $this->cancel($info['shop_id'], $info['id'], 11);
  135. }
  136. $info['status'] = 11;
  137. } else {
  138. $info['time'] = $m - $info['time'];
  139. }
  140. }
  141. $info['status_name'] = $this->config['status'][$info['status']];
  142. $info['method_name'] = $this->config['method'][$info['method']];
  143. $info['pay_method_name'] = $this->config['pay_method'][$info['pay_method']];
  144. $info['pay_type_name'] = $this->config['pay_type'][$info['pay_type']];
  145. if (!$info['coupon_cash']) {
  146. $info['coupon_cash'] = 0;
  147. }
  148. if (!$info['refund_cash']) {
  149. $info['refund_cash'] = 0;
  150. }
  151. if (!$info['ps_cash']) {
  152. $info['ps_cash'] = 0;
  153. }
  154. if (!$info['ps_cash']) {
  155. $info['ps_cash'] = 0;
  156. }
  157. if (!$info['wallet_cash']) {
  158. $info['wallet_cash'] = 0;
  159. } else {
  160. $info['wallet_cash'] = round($info['wallet_cash'], 2);
  161. }
  162. if (!$info['card_code_cash']) {
  163. $info['card_code_cash'] = 0;
  164. } else {
  165. $info['card_code_cash'] = round($info['card_code_cash'], 2);
  166. }
  167. $tk = Dever::db('shop/sell_order_refund')->find(array('order_id' => $info['id'], 'process' => 2));
  168. if ($tk) {
  169. $info['tk_status'] = 1;
  170. } else {
  171. $info['tk_status'] = 2;
  172. }
  173. $info['card'] = array();
  174. if ($info['card_code_id'] > 0) {
  175. $config = Dever::db('main/sell_config')->find();
  176. $info['card']['name'] = $config['card_name'];
  177. $card_code = Dever::db('card/code')->find($info['card_code_id']);
  178. if ($card_code) {
  179. $card_info = Dever::db('card/info')->find($card_code['card_id']);
  180. if ($card_info) {
  181. $card_type = Dever::db('card/type')->find($card_info['type_id']);
  182. $card = substr($card_code['card'], -4);
  183. $info['card']['name'] .= '('.$card_type['name'].$card.')';
  184. $info['card']['info'] = $card_info;
  185. $info['card']['id'] = $card_code['id'];
  186. $info['card']['card'] = $card_code['card'];
  187. $info['card']['cash'] = $info['card_code_cash'];
  188. }
  189. }
  190. }
  191. if ($this->type == 1) {
  192. if ($info['status'] == 2) {
  193. $info['status_name'] = '支付成功';
  194. }
  195. if ($info['status'] == 3) {
  196. if ($info['method'] == 1) {
  197. $info['status_name'] = '待自提';
  198. } else {
  199. $info['status_name'] = '配送中';
  200. }
  201. }
  202. } elseif ($this->type == 2) {
  203. if ($info['status'] == 2) {
  204. $info['status_name'] = '待处理';
  205. }
  206. if ($info['status'] == 3) {
  207. if ($info['method'] == 1) {
  208. $info['status_name'] = '待自提';
  209. } else {
  210. $info['status_name'] = '配送中';
  211. }
  212. }
  213. }
  214. if ($info['method'] == 2) {
  215. $info['ps_info'] = Dever::db('shop/sell_order_ps')->find(array('order_id' => $info['id']));
  216. if ($info['ps_info']) {
  217. $info['ps_info']['service'] = Dever::array_decode($info['ps_info']['service']);
  218. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  219. if ($info['ps_info']['qu_date']) {
  220. $info['ps_info']['qu_date'] = date('Y-m-d H:i', $info['ps_info']['qu_date']);
  221. }
  222. if ($info['ps_info']['qs_date']) {
  223. $info['ps_info']['qs_date'] = date('Y-m-d H:i', $info['ps_info']['qs_date']);
  224. }
  225. $status = Dever::db('shop/sell_order_ps')->config['status'];
  226. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  227. if ($info['ps_info']['service']) {
  228. foreach ($info['ps_info']['service'] as $k => $v) {
  229. if (isset($v['order_num']) && $v['order_num']) {
  230. $info['ps_info']['service'][$k]['service_name'] = '商家自送';
  231. if ($v['service_id'] > 0) {
  232. $service = Dever::db('shop/service')->find($v['service_id']);
  233. $info['ps_info']['service'][$k]['service_name'] = $service['name'];
  234. }
  235. } else {
  236. unset($info['ps_info']['service'][$k]);
  237. }
  238. }
  239. }
  240. }
  241. }
  242. $info['goods'] = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
  243. if ($info['uid'] && $info['uid'] > 0) {
  244. $info['user'] = Dever::load('passport/api')->info($info['uid']);
  245. }
  246. $info['shop'] = Dever::db('shop/info')->getOne($info['shop_id']);
  247. $info['cdate'] = date('Y-m-d H:i', $info['cdate']);
  248. if ($info['operdate']) {
  249. $info['operdate'] = date('Y-m-d H:i', $info['operdate']);
  250. } else {
  251. $info['operdate'] = '';
  252. }
  253. if ($info['fdate']) {
  254. $info['fdate'] = date('Y-m-d H:i', $info['fdate']);
  255. } else {
  256. $info['fdate'] = '';
  257. }
  258. $goods_status = Dever::db('shop/sell_order_goods')->config['status'];
  259. if ($info['address_id']) {
  260. $info['address'] = Dever::db('passport/address')->find($info['address_id']);
  261. }
  262. if ($this->view == 2) {
  263. $check = false;
  264. if (isset($card_info) && $card_info) {
  265. $info['card']['goods'] = array();
  266. $check = false;
  267. $shop = Dever::db('shop/info')->find($info['shop_id']);
  268. if ($shop['type'] == 1 && $card_info['shop_set'] == 1) {
  269. $check = false;
  270. } else {
  271. $goods = Dever::db('card/info_goods')->getData(array('card_id' => $card_info['id']));
  272. if ($goods || $card_info['category']) {
  273. $check = true;
  274. $card_info['category'] = explode(',', $card_info['category']);
  275. if ($goods) {
  276. $goods = array_keys($goods);
  277. }
  278. }
  279. }
  280. }
  281. foreach ($info['goods'] as $k => $v) {
  282. $info['goods'][$k]['info'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
  283. $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
  284. if ($check) {
  285. if ($goods) {
  286. if (in_array($v['goods_id'], $goods)) {
  287. if ($card_info['category']) {
  288. if (in_array($info['goods'][$k]['info']['top_category_id'], $card_info['category'])) {
  289. $info['card']['goods'][] = $info['goods'][$k]['info'];
  290. }
  291. } else {
  292. $info['card']['goods'][] = $info['goods'][$k]['info'];
  293. }
  294. }
  295. } elseif ($card_info['category']) {
  296. if (in_array($info['goods'][$k]['info']['top_category_id'], $card_info['category'])) {
  297. $info['card']['goods'][] = $info['goods'][$k]['info'];
  298. }
  299. }
  300. }
  301. }
  302. if ($info['invoice_id']) {
  303. $info['invoice'] = Dever::db('passport/invoice')->find($info['invoice_id']);
  304. }
  305. if ($info['user_coupon_id']) {
  306. $coupon = Dever::db('shop/user_coupon')->find($info['user_coupon_id']);
  307. if ($coupon) {
  308. $info['coupon'] = Dever::db('goods/coupon')->find($coupon['coupon_id']);
  309. }
  310. }
  311. } else {
  312. foreach ($info['goods'] as $k => $v) {
  313. $goods = Dever::db('goods/info')->one($v['goods_id']);
  314. $info['goods'][$k]['name'] = $goods['name'];
  315. $info['goods'][$k]['cover'] = $goods['cover'];
  316. $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
  317. }
  318. }
  319. return $info;
  320. }
  321. # 取消订单
  322. public function cancel($id, $order_id, $status = 7)
  323. {
  324. $data = $this->getView($id, $order_id, false);
  325. if ($data['status'] == 1) {
  326. $state = Dever::db('shop/sell_order')->update(array('where_id' => $data['id'], 'status' => $status, 'operdate' => time()));
  327. if ($state) {
  328. if ($data['user_coupon_id']) {
  329. # 还原优惠券
  330. Dever::db('shop/user_coupon')->update(array('where_id' => $data['user_coupon_id'], 'status' => 1));
  331. }
  332. if (isset($data['card_code_id']) && $data['card_code_id'] && $data['card_code_cash'] > 0) {
  333. # 还原礼品卡
  334. Dever::db('card/code')->decUseCash(array('where_id' => $data['card_code_id'], 'set_use_cash' => $data['card_code_cash']));
  335. } elseif (isset($data['wallet_cash']) && $data['wallet_cash'] && $data['wallet_cash'] > 0) {
  336. # 还原钱包
  337. }
  338. $this->updateSell($data, 2);
  339. }
  340. return 'ok';
  341. } else {
  342. Dever::alert('当前订单状态不允许取消');
  343. }
  344. }
  345. # 获取优惠券
  346. public function coupon(&$data, $type = 1)
  347. {
  348. $user_coupon_id = Dever::input('user_coupon_id');
  349. $shop_coupon_id = Dever::input('shop_coupon_id');
  350. $data['user_coupon_id'] = 0;
  351. $data['coupon_id'] = 0;
  352. $data['coupon_cash'] = 0;
  353. if ($data['pay_method'] == 3) {
  354. return $data;
  355. }
  356. if ($type == 1) {
  357. if (isset($data['uid']) && $data['uid'] > 0) {
  358. # 默认选择优惠券
  359. # 查找符合要求的优惠券
  360. $coupon = Dever::db('shop/user_coupon')->getAll(array('uid' => $data['uid'], 'city' => $data['shop']['city'], 'status' => 1, 'edate' => time()));
  361. if ($coupon) {
  362. foreach ($coupon as $k => $v) {
  363. $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
  364. if ($coupon_info) {
  365. if ($v['shop_id'] != $data['shop']['id'] && $coupon_info['method'] == 3) {
  366. # 门店专属券 不能给别的门店用
  367. continue;
  368. }
  369. $kou = false;
  370. if ($coupon_info['type'] == 1) {
  371. # 满减券
  372. if ($data['price'] >= $coupon_info['total_cash']) {
  373. $kou = true;
  374. }
  375. } else {
  376. $kou = true;
  377. }
  378. if ($kou) {
  379. $coupon_info['user_coupon_id'] = $v['id'];
  380. $coupon_info['uid'] = $v['uid'];
  381. $coupon_info['edate'] = date('Y-m-d', $v['edate']);
  382. $coupon_info['shop_id'] = $v['shop_id'];
  383. $coupon_info['coupon_id'] = $v['coupon_id'];
  384. $coupon_info['city'] = $v['city'];
  385. $coupon_info = Dever::load('shop/lib/coupon')->getShop($coupon_info, $data['shop'], false);
  386. $data['coupon'][] = $coupon_info;
  387. if (!$user_coupon_id && $data['coupon_cash'] <= $coupon_info['cash']) {
  388. $data['user_coupon_id'] = $v['id'];
  389. $data['coupon_id'] = $coupon_info['id'];
  390. $data['coupon_cash'] = $coupon_info['cash'];
  391. }
  392. }
  393. }
  394. }
  395. }
  396. } else {
  397. $coupon = Dever::db('shop/coupon')->getAll(array('shop_id' => $data['shop']['id']));
  398. if ($coupon) {
  399. foreach ($coupon as $k => $v) {
  400. $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
  401. $kou = false;
  402. if ($coupon_info['type'] == 1) {
  403. # 满减券
  404. if ($data['price'] >= $coupon_info['total_cash']) {
  405. $kou = true;
  406. }
  407. } else {
  408. $kou = true;
  409. }
  410. if ($kou) {
  411. $coupon_info['shop_id'] = $v['shop_id'];
  412. $coupon_info['shop_coupon_id'] = $v['id'];
  413. $coupon_info['coupon_id'] = $v['coupon_id'];
  414. $coupon_info['city'] = $v['city'];
  415. $coupon_info = Dever::load('shop/lib/coupon')->getShop($coupon_info, $data['shop'], false);
  416. $data['coupon'][] = $coupon_info;
  417. }
  418. }
  419. }
  420. }
  421. }
  422. if ($user_coupon_id && isset($data['uid']) && $data['uid'] > 0) {
  423. # 用户自己使用优惠券
  424. if ($data['price'] <= 0) {
  425. Dever::alert('付款价格错误');
  426. }
  427. $coupon = Dever::db('shop/user_coupon')->find(array('uid' => $data['uid'], 'id' => $user_coupon_id, 'status' => 1));
  428. if (!$coupon) {
  429. Dever::alert('优惠券不可用');
  430. }
  431. if (time() > $coupon['edate']) {
  432. Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 3));
  433. Dever::alert('优惠券已过期');
  434. }
  435. $goods_coupon = Dever::db('goods/coupon')->find($coupon['coupon_id']);
  436. if (!$goods_coupon) {
  437. Dever::alert('优惠券不可用');
  438. }
  439. if ($coupon['shop_id'] != $data['shop']['id']) {
  440. if ($goods_coupon['method'] == 3) {
  441. Dever::alert('优惠券不可用');
  442. } else {
  443. $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $coupon['shop_id'], 'coupon_id' => $coupon['coupon_id'], 'city' => $coupon['city']));
  444. if (!$coupon_info) {
  445. Dever::alert('优惠券不可用');
  446. }
  447. }
  448. }
  449. if ($goods_coupon['type'] == 2 && $data['price'] < $goods_coupon['total_cash']) {
  450. Dever::alert('优惠券不可用');
  451. }
  452. $data['user_coupon_id'] = $user_coupon_id;
  453. $data['coupon_id'] = $goods_coupon['id'];
  454. $data['coupon_cash'] = $goods_coupon['cash'];
  455. } elseif ($shop_coupon_id) {
  456. # 店里直接给用优惠券
  457. if ($data['price'] <= 0) {
  458. Dever::alert('付款价格错误');
  459. }
  460. $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $data['shop']['id'], 'id' => $shop_coupon_id));
  461. if (!$coupon_info) {
  462. Dever::alert('优惠券不可用');
  463. }
  464. $goods_coupon = Dever::db('goods/coupon')->find($coupon_info['coupon_id']);
  465. if (!$goods_coupon) {
  466. Dever::alert('优惠券不可用');
  467. }
  468. if ($goods_coupon['type'] == 2 && $data['price'] < $goods_coupon['total_cash']) {
  469. Dever::alert('优惠券不可用');
  470. }
  471. $data['user_coupon_id'] = -1;
  472. $data['coupon_id'] = $goods_coupon['id'];
  473. $data['coupon_cash'] = $goods_coupon['cash'];
  474. }
  475. if (isset($data['coupon_cash']) && $data['coupon_cash'] > 0) {
  476. # 这里还要计算每个商品的优惠金额
  477. if (isset($data['list']) && $data['list']) {
  478. foreach ($data['list'] as $k => $v) {
  479. $data['list'][$k]['coupon_id'] = $data['coupon_id'];
  480. $data['list'][$k]['user_coupon_id'] = $data['user_coupon_id'];
  481. $data['list'][$k]['coupon_cash'] = round(($v['price'] * $v['buy_num'])/$data['price'], 2) * $data['coupon_cash'];
  482. $data['list'][$k]['cash'] = $v['price'] - $data['list'][$k]['coupon_cash'];
  483. }
  484. }
  485. $data['price'] -= $data['coupon_cash'];
  486. }
  487. # 计算礼品卡
  488. $card = Dever::input('card');
  489. $wallet = Dever::input('wallet');
  490. if ($data['price'] > 0 && isset($data['uid']) && $data['uid'] > 0) {
  491. if ($card) {
  492. # 验证这个礼品卡属于自己
  493. $card_code = Dever::db('card/code')->find($card);
  494. if ($card_code['dh_uid'] == $data['uid']) {
  495. $card_state = Dever::load('card/lib/code')->check($card_code);
  496. if ($card_state == 1) {
  497. if (!$card_code['total_cash']) {
  498. $card_code['total_cash'] = $card_code['info']['value'];
  499. }
  500. if (!$card_code['use_cash']) {
  501. $card_code['use_cash'] = 0;
  502. }
  503. $card_cash = $card_code['total_cash'] - $card_code['use_cash'];
  504. if ($card_cash > 0) {
  505. $data['card'] = array();
  506. $card_goods = array();
  507. $check = false;
  508. $xian = false;
  509. if ($data['shop']['type'] == 1 && $card_code['info']['shop_set'] == 1) {
  510. $check = false;
  511. $xian = false;
  512. } else {
  513. $xian = true;
  514. $goods = Dever::db('card/info_goods')->getData(array('card_id' => $card_code['info']['id']));
  515. if ($goods || $card_code['info']['category']) {
  516. $check = true;
  517. }
  518. }
  519. if ($check) {
  520. $price = 0;
  521. $card_code['info']['category'] = explode(',', $card_code['info']['category']);
  522. if ($goods) {
  523. $goods = array_keys($goods);
  524. }
  525. # 检测当前商品是否符合该卡的限制
  526. foreach ($data['list'] as $k => $v) {
  527. if (!isset($v['cash'])) {
  528. $v['cash'] = $v['price'];
  529. }
  530. if ($goods) {
  531. if (in_array($v['id'], $goods)) {
  532. if ($card_code['info']['category']) {
  533. if (in_array($v['top_category_id'], $card_code['info']['category'])) {
  534. $price += ($v['cash'] * $v['num']);
  535. $card_goods[] = $v;
  536. }
  537. } else {
  538. $price += ($v['cash'] * $v['num']);
  539. $card_goods[] = $v;
  540. }
  541. }
  542. } elseif ($card_code['info']['category']) {
  543. if (in_array($v['top_category_id'], $card_code['info']['category'])) {
  544. $price += ($v['cash'] * $v['num']);
  545. $card_goods[] = $v;
  546. }
  547. }
  548. }
  549. } else {
  550. $price = $data['price'];
  551. }
  552. if ($card_cash >= $price) {
  553. $data['card']['cash'] = $price;
  554. $data['price'] = 0;
  555. } else {
  556. $data['card']['cash'] = $card_cash;
  557. $data['price'] -= $data['card']['cash'];
  558. }
  559. if ($xian && ($price <= 0 || ($card_code['info']['min'] > 0 && $price < $card_code['info']['min']))) {
  560. # 不满足限额
  561. unset($data['card']);
  562. } else {
  563. $data['card']['info'] = $card_code['info'];
  564. $data['card']['goods'] = $card_goods;
  565. $data['card']['id'] = $card_code['id'];
  566. $data['card']['card'] = $card_code['card'];
  567. $data['card']['cash'] = round($data['card']['cash'], 2);
  568. $config = Dever::db('main/sell_config')->find();
  569. $card_type = Dever::db('card/type')->find($card_code['info']['type_id']);
  570. $data['card']['name'] = $config['card_name'];
  571. $card = substr($card_code['card'], -4);
  572. $data['card']['name'] .= '('.$card_type['name'].$card.')';
  573. }
  574. }
  575. }
  576. }
  577. } elseif ($wallet && $wallet > 0) {
  578. $data['score'] = Dever::load('score/lib/core')->getUserScore($data['uid']);
  579. }
  580. }
  581. if ($data['price'] < 0) {
  582. $data['price'] = 0;
  583. }
  584. $data['price'] = round($data['price'], 2);
  585. return $data;
  586. }
  587. # 获取礼品卡 - 更改商品卡 - 废弃
  588. public function card($uid, $shop)
  589. {
  590. $card = Dever::input('card');
  591. $pwd = Dever::input('pwd');
  592. if (!$card) {
  593. Dever::alert('卡号/密码错误');
  594. }
  595. if (!$pwd) {
  596. Dever::alert('卡号/密码错误');
  597. }
  598. $data = Dever::db('goods/card_code')->find(array('card' => $card, 'pwd' => $pwd));
  599. if (!$data) {
  600. Dever::alert('卡号/密码错误');
  601. }
  602. $info = Dever::db('goods/card')->find($data['card_id']);
  603. if (!$info || ($info && $info['status'] != 1)) {
  604. Dever::alert('卡号/密码错误');
  605. }
  606. if ($data['uid'] && $data['uid'] > 0) {
  607. if ($data['uid'] != $uid) {
  608. Dever::alert('礼品卡已被领取');
  609. }
  610. if (time() > $data['edate']) {
  611. # 更新礼品卡状态
  612. $card_update['where_id'] = $data['id'];
  613. $card_update['status'] = 5;
  614. Dever::db('goods/card_code')->update($card_update);
  615. Dever::alert('礼品卡已失效');
  616. }
  617. } else {
  618. if (time() > $data['edate']) {
  619. # 更新礼品卡状态
  620. $card_update['where_id'] = $data['id'];
  621. $card_update['uid'] = $uid;
  622. $card_update['status'] = 5;
  623. if ($shop) {
  624. $card_update['shop_id'] = $shop['id'];
  625. }
  626. Dever::db('goods/card_code')->update($card_update);
  627. Dever::alert('礼品卡已失效');
  628. } else {
  629. # 更新礼品卡状态
  630. $card_update['where_id'] = $data['id'];
  631. $card_update['status'] = 2;
  632. $card_update['uid'] = $uid;
  633. $card_update['ddate'] = time();
  634. if ($shop) {
  635. $card_update['shop_id'] = $shop['id'];
  636. }
  637. Dever::db('goods/card_code')->update($card_update);
  638. }
  639. }
  640. $data = Dever::db('goods/card_code')->find($data['id']);
  641. $data['goods'] = $info['goods'];
  642. return $data;
  643. }
  644. # 得到商品和总价
  645. public function goods(&$data)
  646. {
  647. # 1自提,2配送
  648. $data['method'] = Dever::input('method', 1);
  649. $data['pay_method'] = Dever::input('pay_method');
  650. if ($data['pay_method'] == 3) {
  651. $data['card'] = $this->card($data['uid'], $data['shop']);
  652. $goods = Dever::array_decode($data['card']['goods']);
  653. $goods_id = array();
  654. $num = array();
  655. $sku_id = array();
  656. foreach ($goods as $k => $v) {
  657. $goods_id[] = $v['goods_id'];
  658. $num[] = $v['num'];
  659. $sku_id[] = -1;
  660. }
  661. } else {
  662. $goods_id = Dever::input('goods_id');
  663. if (!$goods_id) {
  664. Dever::alert('请传入商品');
  665. }
  666. $goods_id = explode(',', $goods_id);
  667. $sku_id = Dever::input('price_id');
  668. if ($sku_id) {
  669. $sku_id = explode(',', $sku_id);
  670. }
  671. $single_id = Dever::input('single_id');
  672. if ($single_id) {
  673. $single_id = explode(',', $single_id);
  674. }
  675. $input_id = Dever::input('input_id');
  676. if ($input_id) {
  677. $input_id = explode(',', $input_id);
  678. }
  679. $input_value = Dever::input('input_value');
  680. if ($input_value) {
  681. $input_value = explode(',', $input_value);
  682. }
  683. $num = Dever::input('num');
  684. if (!$num) {
  685. Dever::alert('请传入商品数量');
  686. }
  687. $num = explode(',', $num);
  688. }
  689. # 钱包金额
  690. $data['wallet_cash'] = 0;
  691. # 配送金额
  692. $data['ps_cash'] = 0;
  693. # 原价
  694. $data['oprice'] = 0;
  695. # 现价
  696. $data['price'] = 0;
  697. $data['num'] = 0;
  698. $data['name'] = array();
  699. $count = count($goods_id);
  700. # 计算总价格
  701. foreach ($goods_id as $k => $v) {
  702. $s = isset($sku_id[$k]) ? $sku_id[$k] : -1;
  703. $n = isset($num[$k]) ? $num[$k] : 1;
  704. $data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v, $s);
  705. $data['list'][$k]['cash'] = $data['list'][$k]['price'];
  706. $w = array();
  707. $w['goods_id'] = $v;
  708. $w['sku_id'] = $s;
  709. $w['shop_id'] = $data['shop']['id'];
  710. $goods_sku = Dever::db('shop/goods_sku')->find($w);
  711. if ($goods_sku && isset($goods_sku['min'])) {
  712. $data['list'][$k]['min'] = $goods_sku['min'];
  713. }
  714. $data['list'][$k]['num'] = $n;
  715. # 2是库存不足
  716. $data['list'][$k]['ku_state'] = 1;
  717. # 验证是否有货
  718. $total = Dever::load('shop/lib/info')->checkTotal($n, $v, $data['shop']['id'], $s);
  719. if (isset($data['list'][$k]['min']) && $data['list'][$k]['min'] > 0) {
  720. if ($total < $data['list'][$k]['min']) {
  721. $total = 0;
  722. }
  723. }
  724. if ($total <= 0) {
  725. $data['list'][$k]['ku_state'] = 2;
  726. $data['list'][$k]['buy_num'] = 0;
  727. }
  728. if ($data['list'][$k]['ku_state'] == 1) {
  729. $data['list'][$k]['buy_num'] = $n;
  730. $data['num'] += $n;
  731. $data['price'] += $data['list'][$k]['price'] * $n;
  732. $data['name'][] = $data['list'][$k]['name'];
  733. }
  734. }
  735. $data['name'] = implode(',', $data['name']);
  736. if ($data['method'] == 2) {
  737. $data['ps_cash'] = $data['shop']['ps_cash'];
  738. if ($data['shop']['free_ps_cash'] && $data['shop']['free_ps_cash'] > 0 && $data['price'] >= $data['shop']['free_ps_cash']) {
  739. $data['ps_cash'] = 0;
  740. }
  741. if (!$data['ps_cash']) {
  742. $data['ps_cash'] = 0;
  743. }
  744. }
  745. $data['oprice'] = $data['price'];
  746. return $data;
  747. }
  748. # 发起支付
  749. 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 = '')
  750. {
  751. if (!$user) {
  752. Dever::alert('错误的用户信息');
  753. }
  754. $uid = $user['id'];
  755. if ($uid > 0) {
  756. $wechat = Dever::db('passport/wechat')->one(array('uid' => $uid, 'type' => 1, 'system_id' => 1));
  757. if (!$wechat) {
  758. //Dever::alert('错误的用户信息');
  759. }
  760. }
  761. if ($method == 2) {
  762. $ps_cash = $shop['ps_cash'];
  763. if ($shop['free_ps_cash'] && $shop['free_ps_cash'] > 0 && $price >= $shop['free_ps_cash']) {
  764. $ps_cash = 0;
  765. }
  766. if (!$ps_cash) {
  767. $ps_cash = 0;
  768. }
  769. $order_data['ps_cash'] = $ps_cash;
  770. $price += $order_data['ps_cash'];
  771. }
  772. $order_data['shop_id'] = $shop['id'];
  773. if ($shop['type'] == 10) {
  774. $order_data['shop_type'] = 2;
  775. } else {
  776. $order_data['shop_type'] = 1;
  777. }
  778. $order_data['area'] = $shop['area'] . ',' . $shop['id'];
  779. $order_data['uid'] = $uid;
  780. $order_data['mobile'] = $user['mobile'];
  781. $order_data['address_id'] = $address_id;
  782. $order_data['invoice_id'] = $invoice_id;
  783. $order_data['name'] = $name;
  784. $order_data['num'] = $num;
  785. $order_data['info'] = $info;
  786. $order_data['oprice'] = $oprice;
  787. $order_data['price'] = $price;
  788. $order_data['kou_cash'] = $cash;
  789. $order_data['method'] = $method;
  790. $order_data['pay_method'] = $pay_method;
  791. $order_data['pay_type'] = $pay_type;
  792. $order_data['refund_cash'] = 0;
  793. if ($card) {
  794. $order_data['card_code_id'] = $card['id'];
  795. $order_data['card_code_card'] = $card['card'];
  796. $order_data['card_code_cash'] = $card['cash'];
  797. } elseif ($wallet_cash) {
  798. $order_data['wallet_cash'] = $wallet_cash;
  799. }
  800. if ($user_coupon_id) {
  801. $order_data['user_coupon_id'] = $user_coupon_id;
  802. }
  803. if ($coupon_id) {
  804. $order_data['coupon_id'] = $coupon_id;
  805. }
  806. if ($coupon_cash) {
  807. $order_data['coupon_cash'] = $coupon_cash;
  808. }
  809. $order_data['order_num'] = $this->getOrderId();
  810. $id = Dever::db('shop/sell_order')->insert($order_data);
  811. if (!$id) {
  812. Dever::alert('支付失败');
  813. }
  814. foreach($goods as $k => $v) {
  815. if ($v['ku_state'] == 1) {
  816. $data['uid'] = $uid;
  817. $data['order_id'] = $id;
  818. $data['shop_id'] = $shop['id'];
  819. $data['goods_id'] = $v['id'];
  820. $data['sku_id'] = $v['sku_id'];
  821. $data['price'] = $v['price'];
  822. $data['num'] = $v['buy_num'];
  823. if (isset($v['user_coupon_id']) && $v['user_coupon_id']) {
  824. $data['user_coupon_id'] = $v['user_coupon_id'];
  825. }
  826. if (isset($v['coupon_id']) && $v['coupon_id']) {
  827. $data['coupon_id'] = $v['coupon_id'];
  828. }
  829. if (isset($v['coupon_cash']) && $v['coupon_cash']) {
  830. $data['coupon_cash'] = $v['coupon_cash'];
  831. } else {
  832. $data['coupon_cash'] = 0;
  833. }
  834. Dever::db('shop/sell_order_goods')->insert($data);
  835. }
  836. }
  837. # 减少库存 增加销量
  838. $order_data['id'] = $id;
  839. $this->updateSell($order_data);
  840. if ($price <= 0 || $pay_type == 2) {
  841. $param['pay_product_id'] = $id;
  842. $param['pay_order_id'] = $order_data['order_num'];
  843. $param['pay_status'] = 2;
  844. $param['pay_cash'] = $price;
  845. $param['pay_msg'] = '';
  846. $this->success($param);
  847. if ($cart) {
  848. # 来自购物车,要清空一下
  849. $this->dropCart($cart, $uid, $goods, $shop['id']);
  850. }
  851. # 使用优惠券
  852. $this->useCoupon($order_data, $coupon_id, $user_coupon_id, $card, $wallet_cash);
  853. /*
  854. if ($card) {
  855. # 更新礼品卡状态
  856. $card_update['where_id'] = $card['id'];
  857. $card_update['status'] = 3;
  858. $card_update['order_id'] = $id;
  859. $card_update['shop_id'] = $shop['id'];
  860. Dever::db('goods/card_code')->update($card_update);
  861. }*/
  862. return array
  863. (
  864. 'order' => '',
  865. 'order_num' => $order_data['order_num'],
  866. 'order_id' => $id,
  867. 'type' => 'pay',
  868. );
  869. }
  870. $sell_config = Dever::db('main/sell_config')->one();
  871. # 分账
  872. $other = false;
  873. if (isset($shop['mid']) && $shop['mid']) {
  874. $other = array
  875. (
  876. 'mid' => $shop['mid'],
  877. 'order_id' => $order_data['order_num'] . '_' . $shop['id'],
  878. 'per' => $sell_config['per'],
  879. 'oprice' => $oprice,
  880. );
  881. }
  882. $param = array
  883. (
  884. 'project_id' => 1,
  885. 'channel_id' => 1,
  886. 'system_source' => $system_source,
  887. 'uid' => $uid,
  888. 'name' => $order_data['name'],
  889. 'cash' => $price,
  890. 'product_id' => $id,
  891. 'order_id' => $order_data['order_num'],
  892. 'other' => $other,
  893. 'refer' => $refer,
  894. );
  895. if ($uid > 0 && isset($wechat) && $wechat) {
  896. $param['openid'] = $wechat['openid'];
  897. }
  898. $receipt = Dever::input('receipt');
  899. if ($receipt) {
  900. $param['receipt'] = $receipt;
  901. }
  902. $result = Dever::load('pay/api.pay', $param);
  903. //$result['type'] = 'test';
  904. //$result['order'] = '';
  905. if ($cart) {
  906. # 来自购物车,要清空一下
  907. $this->dropCart($cart, $uid, $goods, $shop['id']);
  908. }
  909. # 使用优惠券
  910. $this->useCoupon($order_data, $coupon_id, $user_coupon_id, $card, $wallet_cash);
  911. $result['order_num'] = $order_data['order_num'];
  912. $result['order_id'] = $id;
  913. return $result;
  914. }
  915. public function dropCart($cart, $uid, $goods, $shop_id)
  916. {
  917. $where['uid'] = $uid;
  918. $where['shop_id'] = $shop_id;
  919. foreach ($goods as $k => $v) {
  920. $where['goods_id'] = $v['id'];
  921. if ($cart == 1) {
  922. Dever::db('shop/cart')->delete($where);
  923. } else {
  924. unset($where['uid']);
  925. Dever::db('shop/shop_cart')->delete($where);
  926. }
  927. }
  928. }
  929. public function useCoupon($order_data, $coupon_id, $user_coupon_id, $card, $wallet_cash)
  930. {
  931. if ($user_coupon_id) {
  932. Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 2));
  933. }
  934. # 使用礼品卡
  935. if ($card) {
  936. $state = Dever::db('card/code')->incUseCash(array('where_id' => $card['id'], 'set_use_cash' => $card['cash']));
  937. # 生成结算单
  938. $code = Dever::db('card/code')->find($card['id']);
  939. if ($code) {
  940. $card_info = Dever::db('card/info')->find($code['card_id']);
  941. if ($card_info) {
  942. if ($code['type'] == 2) {
  943. $zhe = 0.9;
  944. } else {
  945. $zhe = $card_info['price']/$card_info['value'];
  946. }
  947. $price = round($card['cash']*$zhe, 2);
  948. $order_data['type'] = 1;
  949. $order_data['type_id'] = $order_data['shop_id'];
  950. $order_data['source_type'] = 4;
  951. $order_data['source_id'] = -1;
  952. $order_data['price'] = $price;
  953. $order_data['p_price'] = $card['cash'];
  954. Dever::load('cash/lib/order')->up($order_data, 3, 1);
  955. }
  956. }
  957. } elseif ($wallet_cash) {
  958. }
  959. # 这里可以记录一下使用日志
  960. }
  961. # 再次发起支付
  962. public function rpay($id, $system_source = 5, $refer = '')
  963. {
  964. $order = Dever::db('shop/sell_order')->find($id);
  965. if (!$order) {
  966. Dever::alert('订单信息错误');
  967. }
  968. if ($order['status'] > 1) {
  969. Dever::alert('订单已支付或已取消');
  970. }
  971. $param = array
  972. (
  973. 'project_id' => 1,
  974. 'channel_id' => 1,
  975. 'system_source' => $system_source,
  976. 'uid' => $order['uid'],
  977. 'name' => $order['name'],
  978. 'cash' => $order['price'],
  979. 'product_id' => $id,
  980. 'order_id' => $order['order_num'],
  981. 'refer' => $refer,
  982. );
  983. $receipt = Dever::input('receipt');
  984. if ($receipt) {
  985. $param['receipt'] = $receipt;
  986. }
  987. $order = $this->getInfo($order);
  988. $result = Dever::load('pay/api.pay', $param);
  989. $result['order_time'] = $order['time'];
  990. $result['order_price'] = $order['price'];
  991. $result['order_num'] = $order['order_num'];
  992. $result['order_id'] = $id;
  993. return $result;
  994. }
  995. # 查询是否支付
  996. public function cpay($id, $system_source = 5, $refer = '')
  997. {
  998. $order = Dever::db('shop/sell_order')->find($id);
  999. if (!$order) {
  1000. Dever::alert('订单信息错误');
  1001. }
  1002. $result['order_num'] = $order['order_num'];
  1003. $result['order_id'] = $id;
  1004. if ($order['status'] > 1 && $order['status'] <= 6) {
  1005. # 已支付
  1006. $result['status'] = 1;
  1007. } else {
  1008. # 待支付
  1009. $result['status'] = 2;
  1010. }
  1011. return $result;
  1012. }
  1013. # 支付成功回调 安全加密 设置token
  1014. public function success_secure_api_token()
  1015. {
  1016. $project_id = Dever::input('pay_project_id');
  1017. $info = Dever::db('pay/project')->one($project_id);
  1018. if ($info) {
  1019. return $info['key'];
  1020. }
  1021. return 'shop_dever_2020';
  1022. }
  1023. # 支付成功回调 安全加密
  1024. public function success_secure_api($param = array())
  1025. {
  1026. $this->success($param);
  1027. }
  1028. # 支付成功回调
  1029. public function success($param = array())
  1030. {
  1031. $send = $param ? $param : Dever::preInput('pay_');
  1032. $product_id = $send['pay_product_id'];
  1033. $order_id = $send['pay_order_id'];
  1034. $status = $send['pay_status'];
  1035. $cash = $send['pay_cash'];
  1036. $msg = $send['pay_msg'];
  1037. $order = Dever::db('shop/sell_order')->one(array('id' => $product_id, 'time' => time()));
  1038. if ($order && $order['status'] == 1) {
  1039. $update = array();
  1040. if ($status == 2) {
  1041. # 生成自提码或者取件码
  1042. $update['code'] = $this->getCode();
  1043. if ($order['method'] == 1) {
  1044. $msg = '您有一件自提商品已订购成功,取件码:' . $update['code'];
  1045. } else {
  1046. $msg = '您有一件外送商品已订购成功,核销码:' . $update['code'];
  1047. # 分配配送信息
  1048. //$this->updatePs($order);
  1049. }
  1050. # 发消息
  1051. if (Dever::project('message') && $order['uid']) {
  1052. $shop = Dever::db('shop/info')->one($order['shop_id']);
  1053. $msg_param['type'] = 1;//消息类型1是订单消息
  1054. $msg_param['id'] = $order['id'];
  1055. $msg_param['name'] = $shop['name'];
  1056. $msg_param = Dever::json_encode($msg_param);
  1057. Dever::project('message') && Dever::load('message/lib/data')->push(-1, $order['uid'], '订购成功通知', $msg, 2, 1, false, $msg_param);
  1058. }
  1059. if ($order['pay_method'] == 2) {
  1060. if ($order['refund_cash'] > 0) {
  1061. $status = 6;
  1062. } else {
  1063. $status = 5;
  1064. }
  1065. $update['fdate'] = time();
  1066. }
  1067. $update['status'] = $status;
  1068. $update['where_id'] = $order['id'];
  1069. $update['paydate'] = time();
  1070. Dever::db('shop/sell_order')->update($update);
  1071. }
  1072. }
  1073. return 'ok';
  1074. }
  1075. # 添加配送信息
  1076. public function updatePs($order, $status = 1, $service_id = -1, $order_num = '')
  1077. {
  1078. $where['order_id'] = $order['id'];
  1079. $where['clear'] = true;
  1080. $data = Dever::db('shop/sell_order_ps')->find($where);
  1081. $service = array();
  1082. if ($service_id) {
  1083. $service['service_id'] = $service_id;
  1084. }
  1085. if ($order_num) {
  1086. $service['order_num'] = $order_num;
  1087. }
  1088. if (!$data) {
  1089. $insert = $where;
  1090. if ($service) {
  1091. $insert['service'][] = $service;
  1092. $insert['service'] = Dever::array_encode($insert['service']);
  1093. }
  1094. $insert['status'] = $status;
  1095. if ($status == 2) {
  1096. $insert['qu_date'] = time();
  1097. }
  1098. Dever::db('shop/sell_order_ps')->insert($insert);
  1099. } else {
  1100. $update = $where;
  1101. $update['where_id'] = $data['id'];
  1102. $update['status'] = $status;
  1103. if ($status == 2) {
  1104. $update['qu_date'] = time();
  1105. }
  1106. if ($status == 3) {
  1107. $update['qs_date'] = time();
  1108. }
  1109. if ($service) {
  1110. $data['service'] = Dever::array_decode($data['service']);
  1111. $update['service'] = $data['service'];
  1112. $update['service'][] = $service;
  1113. $insert['service'] = Dever::array_encode($update['service']);
  1114. }
  1115. Dever::db('shop/sell_order_ps')->update($update);
  1116. }
  1117. }
  1118. # 减少库存,增加销量
  1119. public function updateSell($order, $type = 1)
  1120. {
  1121. $where['order_id'] = $order['id'];
  1122. $where['status'] = 1;
  1123. $data = Dever::db('shop/sell_order_goods')->select($where);
  1124. if ($data) {
  1125. Dever::load('shop/lib/goods')->oper($order, $type, 2, $data);
  1126. }
  1127. }
  1128. # 完成
  1129. public function finish($info, $shop_id)
  1130. {
  1131. if ($info && $info['shop_id'] == $shop_id && $info['status'] <= 4) {
  1132. if ($info['refund_cash'] > 0) {
  1133. $status = 6;
  1134. } else {
  1135. $status = 5;
  1136. }
  1137. $state = Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'status' => $status, 'fdate' => time()));
  1138. # 给用户发消息
  1139. if ($info['uid'] && $info['uid'] > 0) {
  1140. $shop = Dever::db('shop/info')->one($info['shop_id']);
  1141. $msg_param['type'] = 1;//消息类型1是订单消息
  1142. $msg_param['id'] = $info['id'];
  1143. $msg_param['name'] = $shop['name'];
  1144. $msg_param = Dever::json_encode($msg_param);
  1145. $msg = '您有一笔订单已签收,祝您用餐愉快~';
  1146. Dever::project('message') && Dever::project('message') && Dever::load('message/lib/data')->push(-1, $info['uid'], '订单完成通知', $msg, 1, 1, false, $msg_param);
  1147. }
  1148. if ($info['method'] == 2) {
  1149. Dever::load('shop/lib/sell')->updatePs($info, 3);
  1150. }
  1151. if ($info['pay_method'] == 3 && $info['card_code_id']) {
  1152. # 更新礼品卡状态
  1153. $card_update['where_id'] = $info['card_code_id'];
  1154. $card_update['status'] = 4;
  1155. Dever::db('goods/card_code')->update($card_update);
  1156. }
  1157. if ($info['shop_type'] == 2) {
  1158. $w['parent_type'] = 2;
  1159. $w['parent_order_id'] = $info['id'];
  1160. $w['status'] = 4;
  1161. $buy_order = Dever::db('shop/buy_order')->select($w);
  1162. if ($buy_order) {
  1163. foreach ($buy_order as $k => $v) {
  1164. Dever::load('mshop/lib/buy')->set(1,1)->finish($shop_id, $v['id']);
  1165. }
  1166. }
  1167. }
  1168. return 'ok';
  1169. } else {
  1170. Dever::alert('您没有权限核销');
  1171. }
  1172. }
  1173. # 平台订单已收货
  1174. public function yes_api()
  1175. {
  1176. $shop_id = Dever::input('shop_id');
  1177. $order_id = Dever::input('order_id');
  1178. $info = Dever::db('shop/sell_order')->find(array('id' => $order_id));
  1179. if ($info && $info['type'] == 10) {
  1180. $this->finish($info, $shop_id);
  1181. } else {
  1182. Dever::alert('您没有权限核销');
  1183. }
  1184. return 'reload';
  1185. }
  1186. # 生成自提码
  1187. public function getCode()
  1188. {
  1189. $where['code'] = Dever::rand(6, 0);
  1190. $state = Dever::db('shop/sell_order')->one($where);
  1191. if (!$state) {
  1192. return $where['code'];
  1193. } else {
  1194. return $this->getCode();
  1195. }
  1196. }
  1197. # 生成订单号
  1198. public function getOrderId()
  1199. {
  1200. $where['order_num'] = Dever::order('G');
  1201. $state = Dever::db('shop/sell_order')->one($where);
  1202. if (!$state) {
  1203. return $where['order_num'];
  1204. } else {
  1205. return $this->getOrderId();
  1206. }
  1207. }
  1208. # 展示订单详情
  1209. public function show()
  1210. {
  1211. $id = Dever::input('order_id');
  1212. $config = Dever::db('shop/sell_order')->config;
  1213. $info = Dever::db('shop/sell_order')->one($id);
  1214. $tk_status = Dever::db('shop/sell_order_refund')->config['status'];
  1215. $tk_process = Dever::db('shop/sell_order_refund')->config['process'];
  1216. $status = $config['status_manage'][$info['status']];
  1217. $tk = Dever::db('shop/sell_order_refund')->find(array('order_id' => $info['id'], 'type' => 1));
  1218. $shop = Dever::db('shop/info')->find($info['shop_id']);
  1219. $user = Dever::db('passport/user')->find($info['uid']);
  1220. $cdate = date('Y-m-d H:i', $info['cdate']);
  1221. if ($info['operdate']) {
  1222. $opertime = date('Y-m-d H:i', $info['operdate']);
  1223. } else {
  1224. $opertime = '';
  1225. }
  1226. if ($info['fdate']) {
  1227. $fdate = date('Y-m-d H:i', $info['fdate']);
  1228. } else {
  1229. $fdate = '';
  1230. }
  1231. if (!$user) {
  1232. $user['username'] = '无';
  1233. }
  1234. if (!isset($user['truename'])) {
  1235. $user['truename'] = $user['username'];
  1236. }
  1237. if (!$info['coupon_cash']) {
  1238. $info['coupon_cash'] = 0;
  1239. }
  1240. if (!$info['kou_cash']) {
  1241. $info['kou_cash'] = 0;
  1242. }
  1243. if (!$info['ps_cash']) {
  1244. $info['ps_cash'] = 0;
  1245. }
  1246. if (!$info['wallet_cash']) {
  1247. $info['wallet_cash'] = 0;
  1248. }
  1249. if (!$info['card_code_cash']) {
  1250. $info['card_code_cash'] = 0;
  1251. }
  1252. if ($info['card_code_card']) {
  1253. $info['card_code_cash'] .= '('.$info['card_code_card'].')';
  1254. }
  1255. if ($tk && $tk['process'] == 1) {
  1256. $status = '申请' . $tk_status[$tk['status']];
  1257. $process = $tk_process[$tk['process']];
  1258. $process = '';
  1259. }
  1260. $time = array();
  1261. $time[] = array('下单时间', $cdate);
  1262. if ($opertime) {
  1263. if ($info['status'] == 2) {
  1264. $time[] = array('通知时间', $opertime);
  1265. }
  1266. if ($info['status'] == 7) {
  1267. $time[] = array('取消时间', $opertime);
  1268. }
  1269. }
  1270. if ($fdate) {
  1271. $time[] = array('完成时间', $fdate);
  1272. }
  1273. $result = array();
  1274. $result[$info['order_num']] = array
  1275. (
  1276. 'type' => 'info',
  1277. 'content' => array
  1278. (
  1279. array
  1280. (
  1281. array('订单状态', $status),
  1282. array('支付方式', $config['pay_method'][$info['pay_method']]),
  1283. array('配送方式', $config['method'][$info['method']]),
  1284. ),
  1285. array
  1286. (
  1287. array('门店名称', $shop['name']),
  1288. array('联系人', $shop['truename']),
  1289. array('联系电话', $shop['mobile']),
  1290. ),
  1291. array
  1292. (
  1293. array('下单用户', $user['username']),
  1294. array('联系电话', $info['mobile']),
  1295. array('订单备注', $info['info']),
  1296. ),
  1297. array
  1298. (
  1299. array('支付金额', '¥ ' . round($info['price'], 2)),
  1300. array('优惠券满减', '¥ ' . $info['coupon_cash']),
  1301. array('配送费', '¥ ' . $info['ps_cash']),
  1302. ),
  1303. array
  1304. (
  1305. array('礼品卡抵扣', '¥ ' . round($info['card_code_cash'], 2)),
  1306. array('钱包抵扣', '¥ ' . round($info['wallet_cash'], 2)),
  1307. array('抹零', '¥ ' . round($info['kou_cash'], 2)),
  1308. ),
  1309. $time,
  1310. )
  1311. );
  1312. if ($info['address_id'] > 0) {
  1313. $address = Dever::load('passport/address')->getOne($info['uid'], $info['address_id']);
  1314. if ($address) {
  1315. $result['收货地址'] = array
  1316. (
  1317. 'type' => 'info',
  1318. 'content' => array
  1319. (
  1320. array
  1321. (
  1322. array('联系人', $address['contact']),
  1323. array('联系电话', $address['mobile']),
  1324. ),
  1325. array
  1326. (
  1327. array('收货地址', $address['address_info']),
  1328. array('门牌号', $address['house_number']),
  1329. ),
  1330. ),
  1331. );
  1332. }
  1333. }
  1334. if ($info['invoice_id'] > 0) {
  1335. $invoice = Dever::load('passport/invoice')->getOne($info['uid'], $info['invoice_id']);
  1336. if ($invoice) {
  1337. $result['发票信息'] = array
  1338. (
  1339. 'type' => 'info',
  1340. 'content' => array
  1341. (
  1342. array
  1343. (
  1344. array('发票类型', $invoice['title_type_name']),
  1345. array('抬头', $invoice['title']),
  1346. array('税号', $invoice['number']),
  1347. ),
  1348. array
  1349. (
  1350. array('手机号', $invoice['mobile']),
  1351. array('邮箱', $invoice['email']),
  1352. ),
  1353. ),
  1354. );
  1355. }
  1356. }
  1357. $audit = Dever::input('audit');
  1358. if ($audit == 1) {
  1359. # 审核信息
  1360. $this->show_audit($info, $result);
  1361. } else {
  1362. # 配送信息
  1363. $this->show_ps($info, $result);
  1364. # 退款信息
  1365. $this->show_refund($info, $result, $tk, $tk_process);
  1366. # 商品信息
  1367. $this->show_goods($info, $result, $tk_status);
  1368. # 对账信息
  1369. $this->show_dz($info, $result);
  1370. }
  1371. $head = array
  1372. (
  1373. 'name' => '基本信息',
  1374. 'btn' => array(),
  1375. );
  1376. $html = Dever::show($head, $result);
  1377. return $html;
  1378. }
  1379. private function show_goods($info, &$result, $tk_status)
  1380. {
  1381. $body = $refund_body = array();
  1382. $body_total = array();
  1383. $body_total['price'] = 0;
  1384. $body_total['num'] = 0;
  1385. $refund_body_total = array();
  1386. $refund_body_total['price'] = 0;
  1387. $refund_body_total['num'] = 0;
  1388. $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
  1389. $goods_status = Dever::db('shop/sell_order_goods')->config['status'];
  1390. foreach ($goods as $k => $v) {
  1391. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  1392. $status = $goods_status[$v['status']];
  1393. $tk = Dever::db('shop/sell_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  1394. if ($tk && $tk['process'] == 1) {
  1395. $status = '申请' . $tk_status[$tk['status']];
  1396. //$status .= '&nbsp;&nbsp;[' . $tk_process[$tk['process']] . ']';
  1397. }
  1398. if (isset($goods_info['sku'])) {
  1399. $sku = $goods_info['sku']['string'];
  1400. } else {
  1401. $sku = '无';
  1402. }
  1403. if ($info['shop_type'] == 2 && $info['status'] > 1 && $info['status'] < 7) {
  1404. $tui = Dever::url('buy_order_tui?shop_id='.$v['shop_id'].'&order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&cash=' . $v['price'], 'shop');
  1405. $status = '<a href="javascript:;" onclick="fastEdit($(this), \''.$tui.'\', \'退款\', \'\', 1)" class="layui-btn">退款</a>';
  1406. }
  1407. $d = array
  1408. (
  1409. 'pic' => $goods_info['cover'],
  1410. 'name' => $goods_info['name'],
  1411. 'sku' => $sku,
  1412. 'price' => $v['price'],
  1413. 'num' => $v['num'] . $goods_info['unit'],
  1414. 'status' => $status,
  1415. );
  1416. if ($goods_info['price_type'] > 2) {
  1417. $d['goods'] = $goods_info['goods'];
  1418. }
  1419. if ($tk && $v['status'] == 3) {
  1420. $d['status'] = $tk_status[$tk['status']];
  1421. $refund_body[] = $d;
  1422. $price = $v['price']*$v['num'];
  1423. $refund_body_total['price'] += $price;
  1424. $refund_body_total['num'] += $v['num'];
  1425. } else {
  1426. $body[] = $d;
  1427. $price = $v['price']*$v['num'];
  1428. $body_total['price'] += $price;
  1429. $body_total['num'] += $v['num'];
  1430. }
  1431. }
  1432. if ($body) {
  1433. if ($refund_body_total['price']) {
  1434. $body_total['set_price'] = $body_total['price'] - $refund_body_total['price'];
  1435. }
  1436. if ($refund_body_total['num']) {
  1437. $body_total['set_num'] = $body_total['num'] - $refund_body_total['num'];
  1438. }
  1439. $result['商品清单'] = array
  1440. (
  1441. 'type' => 'list',
  1442. 'content' => $body,
  1443. 'total' => $body_total,
  1444. );
  1445. }
  1446. if ($refund_body) {
  1447. $result['退款商品'] = array
  1448. (
  1449. 'type' => 'list',
  1450. 'content' => $refund_body,
  1451. 'total' => $refund_body_total,
  1452. );
  1453. }
  1454. }
  1455. private function show_dz($info, &$result)
  1456. {
  1457. $search_option_shop_type = Dever::input('search_option_shop_type');
  1458. if ($search_option_shop_type == 2) {
  1459. # 平台门店采购单
  1460. $buy_order = Dever::db('shop/buy_order')->getDataToId(array('type' => 1, 'type_id' => $info['shop_id'], 'parent_type' => 2, 'parent_order_id' => $info['id']));
  1461. $source_order_id = array();
  1462. if ($buy_order) {
  1463. $source_order_id = array_keys($buy_order);
  1464. }
  1465. # 平台门店结算单
  1466. if ($source_order_id) {
  1467. $cash_order = Dever::db('cash/order')->getDataByOrderId(array('source_order_id' => $source_order_id));
  1468. $col = 'cash';
  1469. }
  1470. }
  1471. if (isset($cash_order) && $cash_order) {
  1472. $head = array('序号', '结算单号', '数量', '金额', '结算类型', '入账状态', '审核状态', '结算时间', '审核备注', '管理');
  1473. $body = array();
  1474. $config = Dever::db('cash/order')->config;
  1475. foreach ($cash_order as $k => $v) {
  1476. $cdate = date('Y-m-d H:i', $v['cdate']);
  1477. $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
  1478. $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
  1479. $cash = $v[$col];
  1480. $url = Dever::url('project/database/list?project=shop&table=buy_order_goods&order_id='.$v['source_order_id'].'&page_type=1', 'manage');
  1481. $v['source_order_num'] = '<a href="'.$url.'" style="color:blue">'.$v['source_order_num'].'</a>';
  1482. $m = '';
  1483. if ($v['audit'] == 1 && $v['audit_type'] == 2) {
  1484. $url = Dever::url('project/database/update?project=cash&table=order&where_id='.$v['id'].'&col=audit,desc', 'manage');
  1485. $m = '<a href="javascript:;" onclick="fastEdit($(this), \''.$url.'\', \'审核\', \'\', 1)" class="layui-btn">审核</a>';
  1486. }
  1487. $d = array
  1488. (
  1489. $k+1,
  1490. $v['order_num'],
  1491. $v['num'],
  1492. $cash,
  1493. $config['config_jstype'][$v['jstype']],
  1494. $config['config_status'][$v['status']],
  1495. $config['config_audit'][$v['audit']],
  1496. $operdate,
  1497. $v['desc'],
  1498. $m,
  1499. );
  1500. $body[] = $d;
  1501. }
  1502. $result['对账清单'] = array
  1503. (
  1504. 'type' => 'table',
  1505. 'content' => array
  1506. (
  1507. 'head' => $head,
  1508. 'body' => $body,
  1509. )
  1510. );
  1511. }
  1512. }
  1513. private function show_refund($info, &$result, $tk, $tk_process)
  1514. {
  1515. if (!$info['refund_cash']) {
  1516. $info['refund_cash'] = 0;
  1517. }
  1518. if ($info['refund_cash'] > 0 && $tk) {
  1519. $process = $tk_process[$tk['process']];
  1520. $tk['cdate'] = date('Y-m-d H:i', $tk['cdate']);
  1521. $result['退款信息'] = array
  1522. (
  1523. 'type' => 'info',
  1524. 'content' => array
  1525. (
  1526. array
  1527. (
  1528. array('退款金额', $info['refund_cash']),
  1529. array('数量', $info['num']),
  1530. array('退款进度', $process),
  1531. ),
  1532. array
  1533. (
  1534. array('申请时间', $tk['cdate']),
  1535. array('退款原因', $tk['desc']),
  1536. ),
  1537. ),
  1538. );
  1539. }
  1540. }
  1541. private function show_ps($info, &$result)
  1542. {
  1543. $info['ps_info'] = Dever::db('shop/sell_order_ps')->find(array('order_id' => $info['id']));
  1544. if ($info['ps_info']) {
  1545. $info['ps_info']['service'] = Dever::array_decode($info['ps_info']['service']);
  1546. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  1547. if ($info['ps_info']['qs_date']) {
  1548. $info['ps_info']['qs_date'] = date('Y-m-d H:i', $info['ps_info']['qs_date']);
  1549. } else {
  1550. $info['ps_info']['qs_date'] = '无';
  1551. }
  1552. $status = Dever::db('shop/sell_order_ps')->config['status'];
  1553. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  1554. $service = array();
  1555. if ($info['ps_info']['service']) {
  1556. foreach ($info['ps_info']['service'] as $k => $v) {
  1557. if (isset($v['order_num']) && $v['order_num']) {
  1558. $info['ps_info']['service'][$k]['service_name'] = '商家自送';
  1559. if ($v['service_id'] > 0) {
  1560. $service_info = Dever::db('shop/service')->find($v['service_id']);
  1561. $info['ps_info']['service'][$k]['service_name'] = $service_info['name'];
  1562. }
  1563. $service[] = array
  1564. (
  1565. array('名称', $info['ps_info']['service'][$k]['service_name']),
  1566. array('单号', isset($v['order_num']) ? $v['order_num'] : ''),
  1567. );
  1568. }
  1569. }
  1570. }
  1571. $result['物流信息'] = array
  1572. (
  1573. 'type' => 'info',
  1574. 'content' => array
  1575. (
  1576. array
  1577. (
  1578. array('发货时间', $info['ps_info']['cdate']),
  1579. array('收货时间', $info['ps_info']['qs_date']),
  1580. array('查单', '<a href="https://www.kuaidi100.com/?from=openv" target="_blank">点此手动查单</a>'),
  1581. ),
  1582. ),
  1583. );
  1584. $result['物流信息']['content'] = array_merge($result['物流信息']['content'], $service);
  1585. }
  1586. }
  1587. private function show_audit($info, &$result)
  1588. {
  1589. $info['type'] = 1;
  1590. $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id'], 'status' => 1));
  1591. $shop = Dever::db('shop/info')->find($info['shop_id']);
  1592. list($data, $source) = Dever::load('mshop/lib/buy')->assign($goods, $info, $shop, true);
  1593. if ($info) {
  1594. $head = array('序号', '商品编号', '商品名称', '供货商', '订货总数量', '商品计量单位', '订货单总金额');
  1595. $body = array();
  1596. $i = 0;
  1597. if ($data) {
  1598. foreach ($data as $k => $v) {
  1599. foreach ($v as $v1) {
  1600. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v1['goods_id'], $v1['sku_id']);
  1601. if (isset($goods_info['sku'])) {
  1602. $sku = '[' . $goods_info['sku']['string'] . ']';
  1603. } else {
  1604. $sku = '';
  1605. }
  1606. $d = array
  1607. (
  1608. $i+1,
  1609. $goods_info['code'],
  1610. $goods_info['name'] . $sku,
  1611. Dever::load('mshop/lib/buy')->getSelect($source, $v1['source_id'], $v1['order_goods_id']),
  1612. $v1['num'],
  1613. $goods_info['unit'],
  1614. $v1['price'] * $v1['num'],
  1615. );
  1616. $body[] = $d;
  1617. $i++;
  1618. }
  1619. }
  1620. }
  1621. if ($goods) {
  1622. foreach ($goods as $k => $v) {
  1623. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  1624. if (isset($goods_info['sku'])) {
  1625. $sku = '[' . $goods_info['sku']['string'] . ']';
  1626. } else {
  1627. $sku = '';
  1628. }
  1629. $d = array
  1630. (
  1631. $i+1,
  1632. $goods_info['code'],
  1633. $goods_info['name'] . $sku,
  1634. Dever::load('mshop/lib/buy')->getSelect($source, false, $v['id']),
  1635. $v['num'],
  1636. $goods_info['unit'],
  1637. $v['price'] * $v['num'],
  1638. );
  1639. $body[] = $d;
  1640. $i++;
  1641. }
  1642. }
  1643. $btn = array();
  1644. $btn[] = array(
  1645. 'type' => 'click',
  1646. 'content' => 'setAudit()',
  1647. 'name' => '全部调整<script>function setAudit(){layer.confirm(\'确定进行此项操作吗?\', function(){var audit = $(\'.audit_set\').val();$(\'.audit\').each(function(){$(this).val(audit);layui.form.render();layer.closeAll()})})}</script>'
  1648. );
  1649. $result['商品拆单'] = array
  1650. (
  1651. 'type' => 'table',
  1652. 'head' => array('name' => '供货商:' . Dever::load('mshop/lib/buy')->getSelect($source, false, false), 'btn' => $btn),
  1653. 'content' => array
  1654. (
  1655. 'head' => $head,
  1656. 'body' => $body,
  1657. )
  1658. );
  1659. $url = Dever::url('lib/sell.audit_set_commit?json=1&order_id=' . $info['id'], 'shop');
  1660. $result['审核结果'] = array
  1661. (
  1662. 'type' => 'html',
  1663. 'content' => '<script>function saveAudit(){var goods = [];var audit = 1; var audit_text = $(".update_audit_text").val();$(".update_audit").each(function(){if ($(this).get(0).checked){audit = $(this).val()}});$(\'.audit\').each(function(){var v = $(this).val();var id = $(this).attr("id");goods.push(v + "_" + id);});layer.confirm(\'确定进行此项操作吗?\', function(){layui.form.render();var send = {};send.goods = goods.join(",");send.audit = audit;send.audit_text = audit_text;console.info(send);$.post("'.$url.'", send, function(t){t = JSON.parse(t);showYes(t)})})}</script><style>.layui-form-radio{padding-right: 0px;margin: 0px 0px 0 0;margin-left:40px}</style><div class="layui-form-item show_input"><div classs="layui-col-lg6"><div><label style="font-size:14px;font-weight:bold;" class="">审核状态</label><div style="display:inline" class="am-btn-group"><input update_type="checked" class="checkbox-checkall-0_update_audit update_audit update_value validate[required]" type="radio" name="update_audit[]" value="2" checked="" show_id="audit" autocomplete="off" id="form-validation-field-1"> 审核通过 &nbsp;&nbsp;<input update_type="checked" class="autoload checkbox-checkall-0_update_audit update_audit update_value validate[required]" type="radio" name="update_audit[]" value="3" autocomplete="off" id="form-validation-field-0"> 审核未通过 &nbsp;&nbsp;</div></div></div></div><div class="layui-form-item" style="display: block;"><div classs="layui-col-lg6"><div><label style="font-size:14px;font-weight:bold;" class="">审核备注</label></div><div><textarea style="height:100px;" update_type="value" class="update_value update_audit_text form-control layui-input validate[required]" name="update_desc"></textarea></div></div></div> <div style="text-align:left" class="layui-layer-btn layui-layer-btn-"><a class="layui-btn layui-btn-primary" onclick="saveAudit()">保存</a><a class="layui-layer-btn1" style="display:none">取消</a></div>',
  1664. );
  1665. }
  1666. }
  1667. # 第三方确认
  1668. public function tixian()
  1669. {
  1670. $param = array();
  1671. ksort($param);
  1672. $string = array();
  1673. foreach ($param as $k => $v) {
  1674. if (strstr($v, '&') || strstr($v, '@')) {
  1675. $v = urlencode($v);
  1676. }
  1677. $string[] = $k . '=' . $v;
  1678. }
  1679. }
  1680. # 发货
  1681. public function notice($info)
  1682. {
  1683. $config = Dever::db('shop/sell_order')->config;
  1684. $state = Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'status' => 3, 'operdate' => time()));
  1685. # 给用户发消息
  1686. if ($info['uid'] && $info['uid'] > 0) {
  1687. $shop = Dever::db('shop/info')->one($info['shop_id']);
  1688. $msg_param['type'] = 1;//消息类型1是订单消息
  1689. $msg_param['id'] = $info['id'];
  1690. $msg_param['name'] = $shop['name'];
  1691. $msg_param = Dever::json_encode($msg_param);
  1692. if ($info['method'] == 1) {
  1693. $msg = '您有一件自提商品已完成配货,请尽快到指定门店使用取件码取货,取件码:' . $info['code'];
  1694. $name = '取货通知';
  1695. $wechat_msg = '您的商品已完成配货';
  1696. } else {
  1697. $msg = '您有一件外送商品已开始配送,收货时请将取件码提供给配送员核实货品信息,取件码:' . $info['code'];
  1698. $name = '配送通知';
  1699. $wechat_msg = '您的商品已开始配送';
  1700. }
  1701. Dever::project('message') && Dever::load('message/lib/data')->push(-1, $info['uid'], $name, $msg, 1, 1, false, $msg_param);
  1702. if (Dever::project('wechat_applet')) {
  1703. $user = Dever::db('passport/wechat')->one(array('uid' => $info['uid'], 'system_id' => 1, 'system_source' => 5));
  1704. if ($user && $user['openid']) {
  1705. $info['status_name'] = $config['status'][$info['status']];
  1706. $send = array
  1707. (
  1708. 'character_string6' => array
  1709. (
  1710. 'value' => $info['order_num'],
  1711. ),
  1712. 'phrase7' => array
  1713. (
  1714. 'value' => $info['status_name'],
  1715. ),
  1716. 'thing8' => array
  1717. (
  1718. 'value' => $info['code'],
  1719. ),
  1720. 'amount1' => array
  1721. (
  1722. 'value' => $info['price'] . '元',
  1723. ),
  1724. 'thing5' => array
  1725. (
  1726. 'value' => $wechat_msg,
  1727. ),
  1728. );
  1729. $send = Dever::json_encode($send);
  1730. Dever::load('wechat_applet/subscribe')->sendOne('order_process', 1, $user['openid'], 'pages/app/order/order?id=' . $info['id'], $send, Dever::config('base')->wechat_applet);
  1731. }
  1732. }
  1733. }
  1734. }
  1735. # 采购订单审核 手动拆单
  1736. public function audit_set_commit_api()
  1737. {
  1738. Dever::config('base')->hook = true;
  1739. //{goods: "2_4_audit_615,2_4_audit_614,-1_-1_audit_617,-1_-1_audit_616,-1_-1_audit_613", audit: "1", audit_text: "1231"}
  1740. $id = Dever::input('order_id');
  1741. $goods = Dever::input('goods');
  1742. $audit = Dever::input('audit');
  1743. $audit_text = Dever::input('audit_text');
  1744. $info = Dever::db('shop/sell_order')->one($id);
  1745. if ($audit > 1 && $info && $info['status'] == 2) {
  1746. $shop = Dever::db('shop/info')->find($info['shop_id']);
  1747. if ($audit == 2) {
  1748. Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'audit' => $audit, 'audit_desc' => $audit_text));
  1749. # 成功
  1750. if (strstr($info['order_num'], '_')) {
  1751. $temp = explode('_', $info['order_num']);
  1752. $info['order_num'] = $temp[0];
  1753. $info['order_index'] = $temp[1];
  1754. } else {
  1755. $info['order_index'] = 0;
  1756. }
  1757. $goods = explode(',', $goods);
  1758. $data = $other = array();
  1759. foreach ($goods as $k => $v) {
  1760. list($source_type, $source_id, $name, $goods_id) = explode('_', $v);
  1761. $gv = Dever::db('shop/sell_order_goods')->find($goods_id);
  1762. if ($source_id > 0) {
  1763. $key = $source_type . '_' . $source_id;
  1764. $data[$key][] = array
  1765. (
  1766. 'area' => $shop['area'],
  1767. 'order_id' => $info['id'],
  1768. 'order_type' => 2,
  1769. 'order_num' => $info['order_num'],
  1770. 'order_index' => $info['order_index'],
  1771. 'type' => 1,
  1772. 'type_id' => $shop['id'],
  1773. 'source_type' => $source_type,
  1774. 'source_id' => $source_id,
  1775. 'order_goods_id' => $gv['id'],
  1776. 'goods_id' => $gv['goods_id'],
  1777. 'sku_id' => $gv['sku_id'],
  1778. 'price' => $gv['price'],
  1779. 'p_price' => $gv['price'],
  1780. 'num' => $gv['num'],
  1781. );
  1782. } else {
  1783. $other[] = $gv;
  1784. }
  1785. }
  1786. $this->audit_action($data, $other, $info, $shop, 1);
  1787. } else {
  1788. Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'audit' => $audit, 'audit_desc' => $audit_text));
  1789. # 退款
  1790. $shop = Dever::db('shop/info')->find($info['shop_id']);
  1791. Dever::load('shop/lib/refund')->set('sell')->apply(2, $shop['id'], $id, false, 3, 0, '未通过审核');
  1792. }
  1793. }
  1794. return Dever::url('project/database/list&project=shop&table=sell_order&menu=shop&search_option_state=1&search_option_shop_type=2&menu_id=218', 'manage');
  1795. }
  1796. # 采购订单审核 拆单
  1797. public function audit_commit()
  1798. {
  1799. $order_id = Dever::input('order_id');
  1800. $order = Dever::db('shop/sell_order')->find($order_id);
  1801. if (!$order || ($order && $order['status'] != 2)) {
  1802. Dever::alert('当前订单状态无法拆单');
  1803. }
  1804. if ($order['shop_type'] != 2) {
  1805. Dever::alert('这不是平台店的订单');
  1806. }
  1807. $order['type'] = 2;
  1808. $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $order_id));
  1809. $shop = Dever::db('shop/info')->find($order['shop_id']);
  1810. $data = Dever::load('mshop/lib/buy')->assign($goods, $order, $shop);
  1811. $this->audit_action($data, $goods, $order, $shop);
  1812. return 'ok';
  1813. }
  1814. private function audit_action($data, $goods, $order, $shop, $state = 2)
  1815. {
  1816. $send = array();
  1817. if (!$goods && $data) {
  1818. # 开始生成订单
  1819. $i = $order['order_index'];
  1820. foreach ($data as $k => $v) {
  1821. foreach ($v as $k1 => $v1) {
  1822. $v1['order_num'] .= '_' . $i;
  1823. $state = Dever::load('mshop/lib/buy')->createOrder($v1);
  1824. $send[$state] = $v1;
  1825. }
  1826. $i++;
  1827. }
  1828. Dever::db('shop/sell_order')->update(array('where_id' => $order['id'], 'status' => 3));
  1829. } else {
  1830. Dever::db('shop/sell_order')->update(array('where_id' => $order['id'], 'audit' => 1));
  1831. if ($goods) {
  1832. $string = array();
  1833. foreach ($goods as $k => $v) {
  1834. $info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  1835. if ($info['sku_name']) {
  1836. $info['name'] .= '('.$info['sku_name'].')';
  1837. }
  1838. $string[] = $info['name'];
  1839. }
  1840. $string = implode('、', $string);
  1841. if ($state == 1) {
  1842. Dever::alert('商品:'.$string.'未选择供货商');
  1843. } else {
  1844. Dever::alert('商品:'.$string.'库存不足或厂家未生产,请联系供货商备货,该订单进入重新审核状态');
  1845. }
  1846. Dever::alert('商品:'.$string.'库存不足或厂家未生产,请联系供货商备货,该订单进入重新审核状态');
  1847. } else {
  1848. if ($state == 1) {
  1849. Dever::alert('未选择供货商,该订单进入重新审核状态');
  1850. } else {
  1851. Dever::alert('供货商库存不足,请联系供货商备货,该订单进入重新审核状态');
  1852. }
  1853. }
  1854. }
  1855. if ($send) {
  1856. foreach ($send as $k => $v) {
  1857. Dever::load('mshop/lib/buy')->sendMsg($v['source_type'], $v['source_id'], $k);
  1858. $order_info = Dever::db('shop/buy_order')->find(array('id' => $k, 'clear' => true));
  1859. Dever::load('cash/lib/order')->up($order_info, 1, 1);
  1860. }
  1861. }
  1862. }
  1863. }