Sell.php 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181
  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. Dever::load('cash/lib/order')->up($data, 3, 2, false, 2);
  336. } elseif (isset($data['wallet_cash']) && $data['wallet_cash'] && $data['wallet_cash'] > 0) {
  337. # 还原钱包
  338. }
  339. $this->updateSell($data, 2);
  340. }
  341. return 'ok';
  342. } else {
  343. Dever::alert('当前订单状态不允许取消');
  344. }
  345. }
  346. # 获取优惠券
  347. public function coupon(&$data, $type = 1)
  348. {
  349. $user_coupon_id = Dever::input('user_coupon_id');
  350. $shop_coupon_id = Dever::input('shop_coupon_id');
  351. $data['user_coupon_id'] = 0;
  352. $data['coupon_id'] = 0;
  353. $data['coupon_cash'] = 0;
  354. if ($data['pay_method'] == 3) {
  355. return $data;
  356. }
  357. if ($type == 1) {
  358. if (isset($data['uid']) && $data['uid'] > 0) {
  359. # 默认选择优惠券
  360. # 查找符合要求的优惠券
  361. $coupon = Dever::db('shop/user_coupon')->getAll(array('uid' => $data['uid'], 'city' => $data['shop']['city'], 'status' => 1, 'edate' => time()));
  362. if ($coupon) {
  363. foreach ($coupon as $k => $v) {
  364. $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
  365. if ($coupon_info) {
  366. if ($v['shop_id'] != $data['shop']['id'] && $coupon_info['method'] == 3) {
  367. # 门店专属券 不能给别的门店用
  368. continue;
  369. }
  370. $kou = false;
  371. if ($coupon_info['type'] == 1) {
  372. # 满减券
  373. if ($data['price'] >= $coupon_info['total_cash']) {
  374. $kou = true;
  375. }
  376. } else {
  377. $kou = true;
  378. }
  379. if ($kou) {
  380. $coupon_info['user_coupon_id'] = $v['id'];
  381. $coupon_info['uid'] = $v['uid'];
  382. $coupon_info['edate'] = date('Y-m-d', $v['edate']);
  383. $coupon_info['shop_id'] = $v['shop_id'];
  384. $coupon_info['coupon_id'] = $v['coupon_id'];
  385. $coupon_info['city'] = $v['city'];
  386. $coupon_info = Dever::load('shop/lib/coupon')->getShop($coupon_info, $data['shop'], false);
  387. $data['coupon'][] = $coupon_info;
  388. if (!$user_coupon_id && $data['coupon_cash'] <= $coupon_info['cash']) {
  389. $data['user_coupon_id'] = $v['id'];
  390. $data['coupon_id'] = $coupon_info['id'];
  391. $data['coupon_cash'] = $coupon_info['cash'];
  392. }
  393. }
  394. }
  395. }
  396. }
  397. } else {
  398. $coupon = Dever::db('shop/coupon')->getAll(array('shop_id' => $data['shop']['id']));
  399. if ($coupon) {
  400. foreach ($coupon as $k => $v) {
  401. $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
  402. $kou = false;
  403. if ($coupon_info['type'] == 1) {
  404. # 满减券
  405. if ($data['price'] >= $coupon_info['total_cash']) {
  406. $kou = true;
  407. }
  408. } else {
  409. $kou = true;
  410. }
  411. if ($kou) {
  412. $coupon_info['shop_id'] = $v['shop_id'];
  413. $coupon_info['shop_coupon_id'] = $v['id'];
  414. $coupon_info['coupon_id'] = $v['coupon_id'];
  415. $coupon_info['city'] = $v['city'];
  416. $coupon_info = Dever::load('shop/lib/coupon')->getShop($coupon_info, $data['shop'], false);
  417. $data['coupon'][] = $coupon_info;
  418. }
  419. }
  420. }
  421. }
  422. }
  423. if ($user_coupon_id && isset($data['uid']) && $data['uid'] > 0) {
  424. # 用户自己使用优惠券
  425. if ($data['price'] <= 0) {
  426. Dever::alert('付款价格错误');
  427. }
  428. $coupon = Dever::db('shop/user_coupon')->find(array('uid' => $data['uid'], 'id' => $user_coupon_id, 'status' => 1));
  429. if (!$coupon) {
  430. Dever::alert('优惠券不可用');
  431. }
  432. if (time() > $coupon['edate']) {
  433. Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 3));
  434. Dever::alert('优惠券已过期');
  435. }
  436. $goods_coupon = Dever::db('goods/coupon')->find($coupon['coupon_id']);
  437. if (!$goods_coupon) {
  438. Dever::alert('优惠券不可用');
  439. }
  440. if ($coupon['shop_id'] != $data['shop']['id']) {
  441. if ($goods_coupon['method'] == 3) {
  442. Dever::alert('优惠券不可用');
  443. } else {
  444. $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $coupon['shop_id'], 'coupon_id' => $coupon['coupon_id'], 'city' => $coupon['city']));
  445. if (!$coupon_info) {
  446. Dever::alert('优惠券不可用');
  447. }
  448. }
  449. }
  450. if ($goods_coupon['type'] == 2 && $data['price'] < $goods_coupon['total_cash']) {
  451. Dever::alert('优惠券不可用');
  452. }
  453. $data['user_coupon_id'] = $user_coupon_id;
  454. $data['coupon_id'] = $goods_coupon['id'];
  455. $data['coupon_cash'] = $goods_coupon['cash'];
  456. } elseif ($shop_coupon_id) {
  457. # 店里直接给用优惠券
  458. if ($data['price'] <= 0) {
  459. Dever::alert('付款价格错误');
  460. }
  461. $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $data['shop']['id'], 'id' => $shop_coupon_id));
  462. if (!$coupon_info) {
  463. Dever::alert('优惠券不可用');
  464. }
  465. $goods_coupon = Dever::db('goods/coupon')->find($coupon_info['coupon_id']);
  466. if (!$goods_coupon) {
  467. Dever::alert('优惠券不可用');
  468. }
  469. if ($goods_coupon['type'] == 2 && $data['price'] < $goods_coupon['total_cash']) {
  470. Dever::alert('优惠券不可用');
  471. }
  472. $data['user_coupon_id'] = -1;
  473. $data['coupon_id'] = $goods_coupon['id'];
  474. $data['coupon_cash'] = $goods_coupon['cash'];
  475. }
  476. if (isset($data['coupon_cash']) && $data['coupon_cash'] > 0) {
  477. # 这里还要计算每个商品的优惠金额
  478. if (isset($data['list']) && $data['list']) {
  479. foreach ($data['list'] as $k => $v) {
  480. $data['list'][$k]['coupon_id'] = $data['coupon_id'];
  481. $data['list'][$k]['user_coupon_id'] = $data['user_coupon_id'];
  482. $data['list'][$k]['coupon_cash'] = round(($v['price'] * $v['buy_num'])/$data['price'], 2) * $data['coupon_cash'];
  483. $data['list'][$k]['cash'] = $v['price'] - $data['list'][$k]['coupon_cash'];
  484. }
  485. }
  486. $data['price'] -= $data['coupon_cash'];
  487. }
  488. # 计算礼品卡
  489. $card = Dever::input('card');
  490. $wallet = Dever::input('wallet');
  491. if ($data['price'] > 0 && isset($data['uid']) && $data['uid'] > 0) {
  492. if ($card) {
  493. # 验证这个礼品卡属于自己
  494. $card_code = Dever::db('card/code')->find($card);
  495. if ($card_code['dh_uid'] == $data['uid']) {
  496. $card_state = Dever::load('card/lib/code')->check($card_code);
  497. if ($card_state == 1) {
  498. if (!$card_code['total_cash']) {
  499. $card_code['total_cash'] = $card_code['info']['value'];
  500. }
  501. if (!$card_code['use_cash']) {
  502. $card_code['use_cash'] = 0;
  503. }
  504. $card_cash = $card_code['total_cash'] - $card_code['use_cash'];
  505. if ($card_cash > 0) {
  506. $data['card'] = array();
  507. $card_goods = array();
  508. $check = false;
  509. $xian = false;
  510. if ($data['shop']['type'] == 1 && $card_code['info']['shop_set'] == 1) {
  511. $check = false;
  512. $xian = false;
  513. } else {
  514. $xian = true;
  515. $goods = Dever::db('card/info_goods')->getData(array('card_id' => $card_code['info']['id']));
  516. if ($goods || $card_code['info']['category']) {
  517. $check = true;
  518. }
  519. }
  520. if ($check) {
  521. $price = 0;
  522. $card_code['info']['category'] = explode(',', $card_code['info']['category']);
  523. if ($goods) {
  524. $goods = array_keys($goods);
  525. }
  526. # 检测当前商品是否符合该卡的限制
  527. foreach ($data['list'] as $k => $v) {
  528. if (!isset($v['cash'])) {
  529. $v['cash'] = $v['price'];
  530. }
  531. if ($goods) {
  532. if (in_array($v['id'], $goods)) {
  533. if ($card_code['info']['category']) {
  534. if (in_array($v['top_category_id'], $card_code['info']['category'])) {
  535. $price += ($v['cash'] * $v['num']);
  536. $card_goods[] = $v;
  537. }
  538. } else {
  539. $price += ($v['cash'] * $v['num']);
  540. $card_goods[] = $v;
  541. }
  542. }
  543. } elseif ($card_code['info']['category']) {
  544. if (in_array($v['top_category_id'], $card_code['info']['category'])) {
  545. $price += ($v['cash'] * $v['num']);
  546. $card_goods[] = $v;
  547. }
  548. }
  549. }
  550. } else {
  551. $price = $data['price'];
  552. }
  553. if ($card_cash > $price) {
  554. $data['card']['cash'] = $price;
  555. } else {
  556. $data['card']['cash'] = $card_cash;
  557. }
  558. $data['price'] -= $data['card']['cash'];
  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, false, 2);
  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['card_code_id'] && $info['card_code_id'] > 0) {
  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. Dever::load('cash/lib/order')->up($info, 3, 2, false, 2);
  1157. }
  1158. if ($info['shop_type'] == 2) {
  1159. $w['parent_type'] = 2;
  1160. $w['parent_order_id'] = $info['id'];
  1161. $w['status'] = 4;
  1162. $buy_order = Dever::db('shop/buy_order')->select($w);
  1163. if ($buy_order) {
  1164. foreach ($buy_order as $k => $v) {
  1165. Dever::load('mshop/lib/buy')->set(1,1)->finish($shop_id, $v['id']);
  1166. }
  1167. }
  1168. }
  1169. return 'ok';
  1170. } else {
  1171. Dever::alert('您没有权限核销');
  1172. }
  1173. }
  1174. # 平台订单已收货
  1175. public function yes_api()
  1176. {
  1177. $shop_id = Dever::input('shop_id');
  1178. $order_id = Dever::input('order_id');
  1179. $info = Dever::db('shop/sell_order')->find(array('id' => $order_id));
  1180. if ($info && $info['type'] == 10) {
  1181. $this->finish($info, $shop_id);
  1182. } else {
  1183. Dever::alert('您没有权限核销');
  1184. }
  1185. return 'reload';
  1186. }
  1187. # 生成自提码
  1188. public function getCode()
  1189. {
  1190. $where['code'] = Dever::rand(6, 0);
  1191. $state = Dever::db('shop/sell_order')->one($where);
  1192. if (!$state) {
  1193. return $where['code'];
  1194. } else {
  1195. return $this->getCode();
  1196. }
  1197. }
  1198. # 生成订单号
  1199. public function getOrderId()
  1200. {
  1201. $where['order_num'] = Dever::order('G');
  1202. $state = Dever::db('shop/sell_order')->one($where);
  1203. if (!$state) {
  1204. return $where['order_num'];
  1205. } else {
  1206. return $this->getOrderId();
  1207. }
  1208. }
  1209. # 展示订单详情
  1210. public function show()
  1211. {
  1212. $id = Dever::input('order_id');
  1213. $config = Dever::db('shop/sell_order')->config;
  1214. $info = Dever::db('shop/sell_order')->one($id);
  1215. $tk_status = Dever::db('shop/sell_order_refund')->config['status'];
  1216. $tk_process = Dever::db('shop/sell_order_refund')->config['process'];
  1217. $status = $config['status_manage'][$info['status']];
  1218. $tk = Dever::db('shop/sell_order_refund')->find(array('order_id' => $info['id'], 'type' => 1));
  1219. $shop = Dever::db('shop/info')->find($info['shop_id']);
  1220. $user = Dever::db('passport/user')->find($info['uid']);
  1221. $cdate = date('Y-m-d H:i', $info['cdate']);
  1222. if ($info['operdate']) {
  1223. $opertime = date('Y-m-d H:i', $info['operdate']);
  1224. } else {
  1225. $opertime = '';
  1226. }
  1227. if ($info['fdate']) {
  1228. $fdate = date('Y-m-d H:i', $info['fdate']);
  1229. } else {
  1230. $fdate = '';
  1231. }
  1232. if (!$user) {
  1233. $user['username'] = '无';
  1234. }
  1235. if (!isset($user['truename'])) {
  1236. $user['truename'] = $user['username'];
  1237. }
  1238. if (!$info['coupon_cash']) {
  1239. $info['coupon_cash'] = 0;
  1240. }
  1241. if (!$info['kou_cash']) {
  1242. $info['kou_cash'] = 0;
  1243. }
  1244. if (!$info['ps_cash']) {
  1245. $info['ps_cash'] = 0;
  1246. }
  1247. if (!$info['wallet_cash']) {
  1248. $info['wallet_cash'] = 0;
  1249. }
  1250. if (!$info['card_code_cash']) {
  1251. $info['card_code_cash'] = 0;
  1252. }
  1253. if ($info['card_code_card']) {
  1254. $info['card_code_cash'] .= '('.$info['card_code_card'].')';
  1255. }
  1256. if ($tk && $tk['process'] == 1) {
  1257. $status = '申请' . $tk_status[$tk['status']];
  1258. $process = $tk_process[$tk['process']];
  1259. $process = '';
  1260. }
  1261. $time = array();
  1262. $time[] = array('下单时间', $cdate);
  1263. if ($opertime) {
  1264. if ($info['status'] == 2) {
  1265. $time[] = array('通知时间', $opertime);
  1266. }
  1267. if ($info['status'] == 7) {
  1268. $time[] = array('取消时间', $opertime);
  1269. }
  1270. }
  1271. if ($fdate) {
  1272. $time[] = array('完成时间', $fdate);
  1273. }
  1274. $result = array();
  1275. $info['card'] = '';
  1276. if ($info['card_code_id']) {
  1277. $code = Dever::db('card/code')->find($info['card_code_id']);
  1278. if ($code) {
  1279. $card_info = Dever::db('card/info')->find($code['card_id']);
  1280. if ($card_info) {
  1281. if (!$code['total_cash']) {
  1282. $code['total_cash'] = $card_info['value'];
  1283. }
  1284. $info['card'] = '(' . $card_info['name'] .$code['card']. ',面值:'.$code['total_cash'].', 剩余:'.round($code['total_cash']-$code['use_cash'], 2).')';
  1285. }
  1286. }
  1287. }
  1288. $result[$info['order_num']] = array
  1289. (
  1290. 'type' => 'info',
  1291. 'content' => array
  1292. (
  1293. array
  1294. (
  1295. array('订单状态', $status),
  1296. array('支付方式', $config['pay_method'][$info['pay_method']]),
  1297. array('配送方式', $config['method'][$info['method']]),
  1298. ),
  1299. array
  1300. (
  1301. array('门店名称', $shop['name']),
  1302. array('联系人', $shop['truename']),
  1303. array('联系电话', $shop['mobile']),
  1304. ),
  1305. array
  1306. (
  1307. array('下单用户', $user['username']),
  1308. array('联系电话', $info['mobile']),
  1309. array('订单备注', $info['info']),
  1310. ),
  1311. array
  1312. (
  1313. array('支付金额', '¥ ' . round($info['price'], 2)),
  1314. array('优惠券满减', '¥ ' . $info['coupon_cash']),
  1315. array('配送费', '¥ ' . $info['ps_cash']),
  1316. ),
  1317. array
  1318. (
  1319. array('礼品卡抵扣', '¥ ' . round($info['card_code_cash'], 2) . $info['card']),
  1320. array('钱包抵扣', '¥ ' . round($info['wallet_cash'], 2)),
  1321. array('抹零', '¥ ' . round($info['kou_cash'], 2)),
  1322. ),
  1323. $time,
  1324. )
  1325. );
  1326. if ($info['address_id'] > 0) {
  1327. $address = Dever::load('passport/address')->getOne($info['uid'], $info['address_id']);
  1328. if ($address) {
  1329. $result['收货地址'] = array
  1330. (
  1331. 'type' => 'info',
  1332. 'content' => array
  1333. (
  1334. array
  1335. (
  1336. array('联系人', $address['contact']),
  1337. array('联系电话', $address['mobile']),
  1338. ),
  1339. array
  1340. (
  1341. array('收货地址', $address['address_info']),
  1342. array('门牌号', $address['house_number']),
  1343. ),
  1344. ),
  1345. );
  1346. }
  1347. }
  1348. if ($info['invoice_id'] > 0) {
  1349. $invoice = Dever::load('passport/invoice')->getOne($info['uid'], $info['invoice_id']);
  1350. if ($invoice) {
  1351. $result['发票信息'] = array
  1352. (
  1353. 'type' => 'info',
  1354. 'content' => array
  1355. (
  1356. array
  1357. (
  1358. array('发票类型', $invoice['title_type_name']),
  1359. array('抬头', $invoice['title']),
  1360. array('税号', $invoice['number']),
  1361. ),
  1362. array
  1363. (
  1364. array('手机号', $invoice['mobile']),
  1365. array('邮箱', $invoice['email']),
  1366. ),
  1367. ),
  1368. );
  1369. }
  1370. }
  1371. $audit = Dever::input('audit');
  1372. if ($audit == 1) {
  1373. # 审核信息
  1374. $this->show_audit($info, $result);
  1375. } else {
  1376. # 配送信息
  1377. $this->show_ps($info, $result);
  1378. # 退款信息
  1379. $this->show_refund($info, $result, $tk, $tk_process);
  1380. # 商品信息
  1381. $this->show_goods($info, $result, $tk_status);
  1382. # 对账信息
  1383. $this->show_dz($info, $result);
  1384. }
  1385. $head = array
  1386. (
  1387. 'name' => '基本信息',
  1388. 'btn' => array(),
  1389. );
  1390. $html = Dever::show($head, $result);
  1391. return $html;
  1392. }
  1393. private function show_goods($info, &$result, $tk_status)
  1394. {
  1395. $body = $refund_body = array();
  1396. $body_total = array();
  1397. $body_total['price'] = 0;
  1398. $body_total['num'] = 0;
  1399. $refund_body_total = array();
  1400. $refund_body_total['price'] = 0;
  1401. $refund_body_total['num'] = 0;
  1402. $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
  1403. $goods_status = Dever::db('shop/sell_order_goods')->config['status'];
  1404. foreach ($goods as $k => $v) {
  1405. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  1406. $status = $goods_status[$v['status']];
  1407. $tk = Dever::db('shop/sell_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  1408. if ($tk && $tk['process'] == 1) {
  1409. $status = '申请' . $tk_status[$tk['status']];
  1410. //$status .= '&nbsp;&nbsp;[' . $tk_process[$tk['process']] . ']';
  1411. }
  1412. if (isset($goods_info['sku'])) {
  1413. $sku = $goods_info['sku']['string'];
  1414. } else {
  1415. $sku = '无';
  1416. }
  1417. if ($info['shop_type'] == 2 && $info['status'] > 1 && $info['status'] < 7) {
  1418. $tui = Dever::url('buy_order_tui?shop_id='.$v['shop_id'].'&order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&cash=' . $v['price'], 'shop');
  1419. $status = '<a href="javascript:;" onclick="fastEdit($(this), \''.$tui.'\', \'退款\', \'\', 1)" class="layui-btn">退款</a>';
  1420. }
  1421. $d = array
  1422. (
  1423. 'pic' => $goods_info['cover'],
  1424. 'name' => $goods_info['name'],
  1425. 'sku' => $sku,
  1426. 'price' => $v['price'],
  1427. 'num' => $v['num'] . $goods_info['unit'],
  1428. 'status' => $status,
  1429. );
  1430. if ($goods_info['price_type'] > 2) {
  1431. $d['goods'] = $goods_info['goods'];
  1432. }
  1433. if ($tk && $v['status'] == 3) {
  1434. $d['status'] = $tk_status[$tk['status']];
  1435. $refund_body[] = $d;
  1436. $price = $v['price']*$v['num'];
  1437. $refund_body_total['price'] += $price;
  1438. $refund_body_total['num'] += $v['num'];
  1439. } else {
  1440. $body[] = $d;
  1441. $price = $v['price']*$v['num'];
  1442. $body_total['price'] += $price;
  1443. $body_total['num'] += $v['num'];
  1444. }
  1445. }
  1446. if ($body) {
  1447. if ($refund_body_total['price']) {
  1448. $body_total['set_price'] = $body_total['price'] - $refund_body_total['price'];
  1449. }
  1450. if ($refund_body_total['num']) {
  1451. $body_total['set_num'] = $body_total['num'] - $refund_body_total['num'];
  1452. }
  1453. $result['商品清单'] = array
  1454. (
  1455. 'type' => 'list',
  1456. 'content' => $body,
  1457. 'total' => $body_total,
  1458. );
  1459. }
  1460. if ($refund_body) {
  1461. $result['退款商品'] = array
  1462. (
  1463. 'type' => 'list',
  1464. 'content' => $refund_body,
  1465. 'total' => $refund_body_total,
  1466. );
  1467. }
  1468. }
  1469. private function show_dz($info, &$result)
  1470. {
  1471. $search_option_shop_type = Dever::input('search_option_shop_type');
  1472. if ($search_option_shop_type == 2) {
  1473. # 平台门店采购单
  1474. $buy_order = Dever::db('shop/buy_order')->getDataToId(array('type' => 1, 'type_id' => $info['shop_id'], 'parent_type' => 2, 'parent_order_id' => $info['id']));
  1475. $source_order_id = array();
  1476. if ($buy_order) {
  1477. $source_order_id = array_keys($buy_order);
  1478. }
  1479. # 平台门店结算单
  1480. if ($source_order_id) {
  1481. $cash_order = Dever::db('cash/order')->getDataByOrderId(array('source_order_id' => $source_order_id));
  1482. $col = 'cash';
  1483. }
  1484. }
  1485. if (isset($cash_order) && $cash_order) {
  1486. $head = array('序号', '结算单号', '数量', '金额', '结算类型', '入账状态', '审核状态', '结算时间', '审核备注', '管理');
  1487. $body = array();
  1488. $config = Dever::db('cash/order')->config;
  1489. foreach ($cash_order as $k => $v) {
  1490. $cdate = date('Y-m-d H:i', $v['cdate']);
  1491. $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
  1492. $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
  1493. $cash = $v[$col];
  1494. $url = Dever::url('project/database/list?project=shop&table=buy_order_goods&order_id='.$v['source_order_id'].'&page_type=1', 'manage');
  1495. $v['source_order_num'] = '<a href="'.$url.'" style="color:blue">'.$v['source_order_num'].'</a>';
  1496. $m = '';
  1497. if ($v['audit'] == 1 && $v['audit_type'] == 2) {
  1498. $url = Dever::url('project/database/update?project=cash&table=order&where_id='.$v['id'].'&col=audit,desc', 'manage');
  1499. $m = '<a href="javascript:;" onclick="fastEdit($(this), \''.$url.'\', \'审核\', \'\', 1)" class="layui-btn">审核</a>';
  1500. }
  1501. $d = array
  1502. (
  1503. $k+1,
  1504. $v['order_num'],
  1505. $v['num'],
  1506. $cash,
  1507. $config['config_jstype'][$v['jstype']],
  1508. $config['config_status'][$v['status']],
  1509. $config['config_audit'][$v['audit']],
  1510. $operdate,
  1511. $v['desc'],
  1512. $m,
  1513. );
  1514. $body[] = $d;
  1515. }
  1516. $result['对账清单'] = array
  1517. (
  1518. 'type' => 'table',
  1519. 'content' => array
  1520. (
  1521. 'head' => $head,
  1522. 'body' => $body,
  1523. )
  1524. );
  1525. }
  1526. }
  1527. private function show_refund($info, &$result, $tk, $tk_process)
  1528. {
  1529. if (!$info['refund_cash']) {
  1530. $info['refund_cash'] = 0;
  1531. }
  1532. if ($info['refund_cash'] > 0 && $tk) {
  1533. $process = $tk_process[$tk['process']];
  1534. $tk['cdate'] = date('Y-m-d H:i', $tk['cdate']);
  1535. $result['退款信息'] = array
  1536. (
  1537. 'type' => 'info',
  1538. 'content' => array
  1539. (
  1540. array
  1541. (
  1542. array('退款金额', $info['refund_cash']),
  1543. array('数量', $info['num']),
  1544. array('退款进度', $process),
  1545. ),
  1546. array
  1547. (
  1548. array('申请时间', $tk['cdate']),
  1549. array('退款原因', $tk['desc']),
  1550. ),
  1551. ),
  1552. );
  1553. }
  1554. }
  1555. private function show_ps($info, &$result)
  1556. {
  1557. $info['ps_info'] = Dever::db('shop/sell_order_ps')->find(array('order_id' => $info['id']));
  1558. if ($info['ps_info']) {
  1559. $info['ps_info']['service'] = Dever::array_decode($info['ps_info']['service']);
  1560. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  1561. if ($info['ps_info']['qs_date']) {
  1562. $info['ps_info']['qs_date'] = date('Y-m-d H:i', $info['ps_info']['qs_date']);
  1563. } else {
  1564. $info['ps_info']['qs_date'] = '无';
  1565. }
  1566. $status = Dever::db('shop/sell_order_ps')->config['status'];
  1567. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  1568. $service = array();
  1569. if ($info['ps_info']['service']) {
  1570. foreach ($info['ps_info']['service'] as $k => $v) {
  1571. if (isset($v['order_num']) && $v['order_num']) {
  1572. $info['ps_info']['service'][$k]['service_name'] = '商家自送';
  1573. if ($v['service_id'] > 0) {
  1574. $service_info = Dever::db('shop/service')->find($v['service_id']);
  1575. $info['ps_info']['service'][$k]['service_name'] = $service_info['name'];
  1576. }
  1577. $service[] = array
  1578. (
  1579. array('名称', $info['ps_info']['service'][$k]['service_name']),
  1580. array('单号', isset($v['order_num']) ? $v['order_num'] : ''),
  1581. );
  1582. }
  1583. }
  1584. }
  1585. $result['物流信息'] = array
  1586. (
  1587. 'type' => 'info',
  1588. 'content' => array
  1589. (
  1590. array
  1591. (
  1592. array('发货时间', $info['ps_info']['cdate']),
  1593. array('收货时间', $info['ps_info']['qs_date']),
  1594. array('查单', '<a href="https://www.kuaidi100.com/?from=openv" target="_blank">点此手动查单</a>'),
  1595. ),
  1596. ),
  1597. );
  1598. $result['物流信息']['content'] = array_merge($result['物流信息']['content'], $service);
  1599. }
  1600. }
  1601. private function show_audit($info, &$result)
  1602. {
  1603. $info['type'] = 1;
  1604. $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id'], 'status' => 1));
  1605. $shop = Dever::db('shop/info')->find($info['shop_id']);
  1606. list($data, $source) = Dever::load('mshop/lib/buy')->assign($goods, $info, $shop, true);
  1607. if ($info) {
  1608. $head = array('序号', '商品编号', '商品名称', '供货商', '订货总数量', '商品计量单位', '订货单总金额');
  1609. $body = array();
  1610. $i = 0;
  1611. if ($data) {
  1612. foreach ($data as $k => $v) {
  1613. foreach ($v as $v1) {
  1614. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v1['goods_id'], $v1['sku_id']);
  1615. if (isset($goods_info['sku'])) {
  1616. $sku = '[' . $goods_info['sku']['string'] . ']';
  1617. } else {
  1618. $sku = '';
  1619. }
  1620. $d = array
  1621. (
  1622. $i+1,
  1623. $goods_info['code'],
  1624. $goods_info['name'] . $sku,
  1625. Dever::load('mshop/lib/buy')->getSelect($source, $v1['source_id'], $v1['order_goods_id']),
  1626. $v1['num'],
  1627. $goods_info['unit'],
  1628. $v1['price'] * $v1['num'],
  1629. );
  1630. $body[] = $d;
  1631. $i++;
  1632. }
  1633. }
  1634. }
  1635. if ($goods) {
  1636. foreach ($goods as $k => $v) {
  1637. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  1638. if (isset($goods_info['sku'])) {
  1639. $sku = '[' . $goods_info['sku']['string'] . ']';
  1640. } else {
  1641. $sku = '';
  1642. }
  1643. $d = array
  1644. (
  1645. $i+1,
  1646. $goods_info['code'],
  1647. $goods_info['name'] . $sku,
  1648. Dever::load('mshop/lib/buy')->getSelect($source, false, $v['id']),
  1649. $v['num'],
  1650. $goods_info['unit'],
  1651. $v['price'] * $v['num'],
  1652. );
  1653. $body[] = $d;
  1654. $i++;
  1655. }
  1656. }
  1657. $btn = array();
  1658. $btn[] = array(
  1659. 'type' => 'click',
  1660. 'content' => 'setAudit()',
  1661. 'name' => '全部调整<script>function setAudit(){layer.confirm(\'确定进行此项操作吗?\', function(){var audit = $(\'.audit_set\').val();$(\'.audit\').each(function(){$(this).val(audit);layui.form.render();layer.closeAll()})})}</script>'
  1662. );
  1663. $result['商品拆单'] = array
  1664. (
  1665. 'type' => 'table',
  1666. 'head' => array('name' => '供货商:' . Dever::load('mshop/lib/buy')->getSelect($source, false, false), 'btn' => $btn),
  1667. 'content' => array
  1668. (
  1669. 'head' => $head,
  1670. 'body' => $body,
  1671. )
  1672. );
  1673. $url = Dever::url('lib/sell.audit_set_commit?json=1&order_id=' . $info['id'], 'shop');
  1674. $result['审核结果'] = array
  1675. (
  1676. 'type' => 'html',
  1677. '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>',
  1678. );
  1679. }
  1680. }
  1681. # 第三方确认
  1682. public function tixian()
  1683. {
  1684. $param = array();
  1685. ksort($param);
  1686. $string = array();
  1687. foreach ($param as $k => $v) {
  1688. if (strstr($v, '&') || strstr($v, '@')) {
  1689. $v = urlencode($v);
  1690. }
  1691. $string[] = $k . '=' . $v;
  1692. }
  1693. }
  1694. # 发货
  1695. public function notice($info)
  1696. {
  1697. $config = Dever::db('shop/sell_order')->config;
  1698. $state = Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'status' => 3, 'operdate' => time()));
  1699. # 给用户发消息
  1700. if ($info['uid'] && $info['uid'] > 0) {
  1701. $shop = Dever::db('shop/info')->one($info['shop_id']);
  1702. $msg_param['type'] = 1;//消息类型1是订单消息
  1703. $msg_param['id'] = $info['id'];
  1704. $msg_param['name'] = $shop['name'];
  1705. $msg_param = Dever::json_encode($msg_param);
  1706. if ($info['method'] == 1) {
  1707. $msg = '您有一件自提商品已完成配货,请尽快到指定门店使用取件码取货,取件码:' . $info['code'];
  1708. $name = '取货通知';
  1709. $wechat_msg = '您的商品已完成配货';
  1710. } else {
  1711. $msg = '您有一件外送商品已开始配送,收货时请将取件码提供给配送员核实货品信息,取件码:' . $info['code'];
  1712. $name = '配送通知';
  1713. $wechat_msg = '您的商品已开始配送';
  1714. }
  1715. Dever::project('message') && Dever::load('message/lib/data')->push(-1, $info['uid'], $name, $msg, 1, 1, false, $msg_param);
  1716. if (Dever::project('wechat_applet')) {
  1717. $user = Dever::db('passport/wechat')->one(array('uid' => $info['uid'], 'system_id' => 1, 'system_source' => 5));
  1718. if ($user && $user['openid']) {
  1719. $info['status_name'] = $config['status'][$info['status']];
  1720. $send = array
  1721. (
  1722. 'character_string6' => array
  1723. (
  1724. 'value' => $info['order_num'],
  1725. ),
  1726. 'phrase7' => array
  1727. (
  1728. 'value' => $info['status_name'],
  1729. ),
  1730. 'thing8' => array
  1731. (
  1732. 'value' => $info['code'],
  1733. ),
  1734. 'amount1' => array
  1735. (
  1736. 'value' => $info['price'] . '元',
  1737. ),
  1738. 'thing5' => array
  1739. (
  1740. 'value' => $wechat_msg,
  1741. ),
  1742. );
  1743. $send = Dever::json_encode($send);
  1744. Dever::load('wechat_applet/subscribe')->sendOne('order_process', 1, $user['openid'], 'pages/app/order/order?id=' . $info['id'], $send, Dever::config('base')->wechat_applet);
  1745. }
  1746. }
  1747. }
  1748. }
  1749. # 采购订单审核 手动拆单
  1750. public function audit_set_commit_api()
  1751. {
  1752. Dever::config('base')->hook = true;
  1753. //{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"}
  1754. $id = Dever::input('order_id');
  1755. $goods = Dever::input('goods');
  1756. $audit = Dever::input('audit');
  1757. $audit_text = Dever::input('audit_text');
  1758. $info = Dever::db('shop/sell_order')->one($id);
  1759. if ($audit > 1 && $info && $info['status'] == 2) {
  1760. $shop = Dever::db('shop/info')->find($info['shop_id']);
  1761. if ($audit == 2) {
  1762. Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'audit' => $audit, 'audit_desc' => $audit_text));
  1763. # 成功
  1764. if (strstr($info['order_num'], '_')) {
  1765. $temp = explode('_', $info['order_num']);
  1766. $info['order_num'] = $temp[0];
  1767. $info['order_index'] = $temp[1];
  1768. } else {
  1769. $info['order_index'] = 0;
  1770. }
  1771. $goods = explode(',', $goods);
  1772. $data = $other = array();
  1773. foreach ($goods as $k => $v) {
  1774. list($source_type, $source_id, $name, $goods_id) = explode('_', $v);
  1775. $gv = Dever::db('shop/sell_order_goods')->find($goods_id);
  1776. if ($source_id > 0) {
  1777. $key = $source_type . '_' . $source_id;
  1778. $data[$key][] = array
  1779. (
  1780. 'area' => $shop['area'],
  1781. 'order_id' => $info['id'],
  1782. 'order_type' => 2,
  1783. 'order_num' => $info['order_num'],
  1784. 'order_index' => $info['order_index'],
  1785. 'type' => 1,
  1786. 'type_id' => $shop['id'],
  1787. 'source_type' => $source_type,
  1788. 'source_id' => $source_id,
  1789. 'order_goods_id' => $gv['id'],
  1790. 'goods_id' => $gv['goods_id'],
  1791. 'sku_id' => $gv['sku_id'],
  1792. 'price' => $gv['price'],
  1793. 'p_price' => $gv['price'],
  1794. 'num' => $gv['num'],
  1795. );
  1796. } else {
  1797. $other[] = $gv;
  1798. }
  1799. }
  1800. $this->audit_action($data, $other, $info, $shop, 1);
  1801. } else {
  1802. Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'audit' => $audit, 'audit_desc' => $audit_text));
  1803. # 退款
  1804. $shop = Dever::db('shop/info')->find($info['shop_id']);
  1805. Dever::load('shop/lib/refund')->set('sell')->apply(2, $shop['id'], $id, false, 3, 0, '未通过审核');
  1806. }
  1807. }
  1808. 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');
  1809. }
  1810. # 采购订单审核 拆单
  1811. public function audit_commit()
  1812. {
  1813. $order_id = Dever::input('order_id');
  1814. $order = Dever::db('shop/sell_order')->find($order_id);
  1815. if (!$order || ($order && $order['status'] != 2)) {
  1816. Dever::alert('当前订单状态无法拆单');
  1817. }
  1818. if ($order['shop_type'] != 2) {
  1819. Dever::alert('这不是平台店的订单');
  1820. }
  1821. $order['type'] = 2;
  1822. $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $order_id));
  1823. $shop = Dever::db('shop/info')->find($order['shop_id']);
  1824. $data = Dever::load('mshop/lib/buy')->assign($goods, $order, $shop);
  1825. $this->audit_action($data, $goods, $order, $shop);
  1826. return 'ok';
  1827. }
  1828. private function audit_action($data, $goods, $order, $shop, $state = 2)
  1829. {
  1830. $send = array();
  1831. if (!$goods && $data) {
  1832. # 开始生成订单
  1833. $i = $order['order_index'];
  1834. foreach ($data as $k => $v) {
  1835. foreach ($v as $k1 => $v1) {
  1836. $v1['order_num'] .= '_' . $i;
  1837. $state = Dever::load('mshop/lib/buy')->createOrder($v1);
  1838. $send[$state] = $v1;
  1839. }
  1840. $i++;
  1841. }
  1842. Dever::db('shop/sell_order')->update(array('where_id' => $order['id'], 'status' => 3));
  1843. } else {
  1844. Dever::db('shop/sell_order')->update(array('where_id' => $order['id'], 'audit' => 1));
  1845. if ($goods) {
  1846. $string = array();
  1847. foreach ($goods as $k => $v) {
  1848. $info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  1849. if ($info['sku_name']) {
  1850. $info['name'] .= '('.$info['sku_name'].')';
  1851. }
  1852. $string[] = $info['name'];
  1853. }
  1854. $string = implode('、', $string);
  1855. if ($state == 1) {
  1856. Dever::alert('商品:'.$string.'未选择供货商');
  1857. } else {
  1858. Dever::alert('商品:'.$string.'库存不足或厂家未生产,请联系供货商备货,该订单进入重新审核状态');
  1859. }
  1860. Dever::alert('商品:'.$string.'库存不足或厂家未生产,请联系供货商备货,该订单进入重新审核状态');
  1861. } else {
  1862. if ($state == 1) {
  1863. Dever::alert('未选择供货商,该订单进入重新审核状态');
  1864. } else {
  1865. Dever::alert('供货商库存不足,请联系供货商备货,该订单进入重新审核状态');
  1866. }
  1867. }
  1868. }
  1869. if ($send) {
  1870. foreach ($send as $k => $v) {
  1871. Dever::load('mshop/lib/buy')->sendMsg($v['source_type'], $v['source_id'], $k);
  1872. $order_info = Dever::db('shop/buy_order')->find(array('id' => $k, 'clear' => true));
  1873. Dever::load('cash/lib/order')->up($order_info, 1, 1);
  1874. }
  1875. }
  1876. }
  1877. }