Sell.php 90 KB

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