Sell.php 85 KB

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