No Description

OrderController.php 148KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095
  1. <?php
  2. /**
  3. * Created by Sublime.
  4. * User: hao
  5. * Date: 19/08/30
  6. * Time: 上午11:20
  7. */
  8. namespace App\Http\Controllers\Admin;
  9. use App\Http\Controllers\Controller;
  10. use App\Log;
  11. use App\Order;
  12. use App\Oplog;
  13. use App\RedisModel;
  14. use App\Goods;
  15. use App\GoodsSkus;
  16. use App\OrderGoodsSkus;
  17. use App\CustomerInfo;
  18. use App\CustomerConsum;
  19. use Illuminate\Http\Request;
  20. use Illuminate\Support\Facades\DB;
  21. use PHPExcel_Reader_Excel2007;
  22. use PHPExcel_Reader_Excel5;
  23. use PHPExcel;
  24. use PHPExcel_Writer_Excel2007;
  25. use YPSMS;
  26. class OrderController extends Controller
  27. {
  28. protected $warehouse = [];
  29. public function __construct()
  30. {
  31. $this->warehouse = config('constants.WAREHOUSE');
  32. $is_fx = env('IS_FX');
  33. if($is_fx > 0) {
  34. unset($this->warehouse[1]);
  35. unset($this->warehouse[2]);
  36. }
  37. }
  38. public function orderindex(Request $request){
  39. $page = (int)$request->input('page');
  40. $pageSize = 20;
  41. if($page<=0){
  42. $page = 1;
  43. }
  44. $offset = ($page-1) * $pageSize;
  45. $self_role = session('role_name');
  46. if($self_role == '超级管理员' || $self_role == '团队主管' || $self_role == '管理员' || $self_role == '售后管理员' || $self_role == '分销管理员'){
  47. $admin_id = $request->input('admin_id');
  48. $search_admin = 1;
  49. }else{
  50. $admin_id = session('admin_id');
  51. $search_admin = 0;
  52. }
  53. $team_id = $request->input('team_id');
  54. #只能看自己团队的
  55. if($self_role != '超级管理员' && $self_role != '售后管理员'){
  56. $self_id = session('admin_id');
  57. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  58. }
  59. //假如有团队筛选,检索销售队员
  60. $sale_ids = null;
  61. if($team_id>0){
  62. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  63. }
  64. $stime = $request->input('stime');
  65. $etime = $request->input('etime');
  66. $receiverName = trim($request->input('receiverName'));
  67. $receiverMobile = trim($request->input('receiverMobile'));
  68. $status = $request->input('status');
  69. $id = (int)$request->input('id');
  70. $warehouse = (int)$request->input('warehouse');
  71. if($status === null){
  72. $status = -1;
  73. }
  74. if($etime){
  75. $etime = $etime. ' 23:59:59';
  76. }
  77. $count = Order::where(function($query) use($admin_id, $stime, $etime, $receiverName, $receiverMobile, $self_role, $sale_ids, $status, $id, $warehouse){
  78. if($admin_id) $query->where('admin_id', $admin_id);
  79. if($stime) $query->where('createTime', '>=', $stime);
  80. if($etime) $query->where('createTime', '<=', $etime);
  81. if($receiverName) $query->where('receiverName', 'like', $receiverName . '%');
  82. if($receiverMobile) $query->where('receiverMobile', $receiverMobile);
  83. if($self_role == '管理员' || $self_role == '分销管理员') $query->where('status','>',0);
  84. if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  85. if(($status>=0)) $query->where('status', $status);
  86. if(($id>0)) $query->where('id', $id);
  87. if(($warehouse>0)) $query->where('warehouse', $warehouse);
  88. })->where('is_del',0)->count();
  89. if ($count > 1) {
  90. // 总页数
  91. $pages = ceil($count/$pageSize);
  92. }else{
  93. // 总页数
  94. $pages = 1;
  95. }
  96. $result = Order::where(function($query) use($admin_id, $stime, $etime, $receiverName, $receiverMobile, $self_role, $sale_ids, $status, $id, $warehouse){
  97. if($admin_id) $query->where('admin_id', $admin_id);
  98. if($stime) $query->where('createTime', '>=', $stime);
  99. if($etime) $query->where('createTime', '<=', $etime);
  100. if($receiverName) $query->where('receiverName', 'like', $receiverName . '%');
  101. if($receiverMobile) $query->where('receiverMobile', $receiverMobile);
  102. if($self_role == '管理员' || $self_role == '分销管理员') $query->where('status','>',0);
  103. if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  104. if(($status>=0)) $query->where('status', $status);
  105. if(($id>0)) $query->where('id', $id);
  106. if(($warehouse>0)) $query->where('warehouse', $warehouse);
  107. })->where('is_del',0)->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
  108. $result = json_decode(json_encode($result),true);
  109. $status_arr = array(
  110. 0 => '已录入',
  111. 1 => '待审核',
  112. 2 => '已审核',
  113. 3 => '已发货',
  114. 4 => '已退款'
  115. );
  116. foreach($result as $k=>&$v){
  117. //毛利 40%下
  118. $v['pre_cost_flag'] = 0;
  119. //是否需要转仓
  120. $v['is_not_fx'] = 0;
  121. //获取商品信息
  122. $v['goods'] = '';
  123. $goods = DB::table('order_goods_skus')->where('order_id', $v['id'])->where('is_del', 0)->get();
  124. if(!empty($goods)){
  125. $v['pre_cost'] = 0;
  126. foreach($goods as $item){
  127. $goods_name = Goods::where('id', $item->goods_id)->pluck('name');
  128. $goods_sku_info = GoodsSkus::select('propsName', 'is_weigh', 'referenceCost', 'is_not_fx', 'warehouse')->where('id', $item->sku_id)->first();
  129. if(!empty($goods_sku_info)){
  130. $props_name = $goods_sku_info->propsName;
  131. $is_weigh = $goods_sku_info->is_weigh;
  132. $weigh_note = $is_weigh==1 ? '重量(斤)':'数量';
  133. if(!$item->gift_id) $v['pre_cost'] += $goods_sku_info->referenceCost * $item->num;
  134. //若有商品不属于C仓并且现在所属仓库是C仓,审核时提醒转仓
  135. if( $goods_sku_info->warehouse != 3 && $v['warehouse']==3 && $v['is_not_fx'] == 0 ){
  136. // if( $goods_sku_info->is_not_fx == 1 && $v['warehouse']==3 && $v['is_not_fx'] == 0 ){
  137. $v['is_not_fx'] = 1;
  138. }
  139. }else{
  140. $props_name = '';
  141. $weigh_note = '数量';
  142. }
  143. $goodsWarehouse = isset($this->warehouse[$goods_sku_info->warehouse]) ? $this->warehouse[$goods_sku_info->warehouse] : '';
  144. $v['goods'][] = $goods_name.'('.$props_name.')&nbsp; '.$weigh_note.':'.$item->num.' '.$goodsWarehouse;
  145. }
  146. $v['pre_cost'] = round($v['pre_cost'], 2);
  147. if( ($v['receivedAmount'] - $v['pre_cost']) / $v['receivedAmount'] < 0.4){
  148. $v['pre_cost_flag'] = 1;
  149. }
  150. }
  151. #加粉时间
  152. $fanTime = DB::table('customers')->select('fanTime')->where('phone', $v['receiverMobile'])->first();
  153. $v['fanTime'] = isset($fanTime->fanTime) ? $fanTime->fanTime : '';
  154. $v['receiverMobile'] = substr($v['receiverMobile'], 0, 3).'****'.substr($v['receiverMobile'], 7);
  155. #获取状态机
  156. if( ($self_role == '销售' || $self_role == '分销销售') && !in_array($v['status'], [0]) ){
  157. $v['enable_status'] = [];
  158. }else{
  159. $v['enable_status'] = $this->getEnableStatus($v['status'], $v['warehouse']);
  160. }
  161. $v['status_name'] = $status_arr[$v['status']];
  162. if($v['refund_type'] == 1){
  163. $v['refund_type'] = '微信支付';
  164. }elseif($v['refund_type'] == 2){
  165. $v['refund_type'] = '付款码支付';
  166. }elseif($v['refund_type'] == 3){
  167. $v['refund_type'] = '个体户支付';
  168. }elseif($v['refund_type'] == 4){
  169. $v['refund_type'] = '充值卡支付';
  170. }else{
  171. $v['refund_type'] = '';
  172. }
  173. if($v['payment_type'] == 1){
  174. $v['payment_type'] = '微信支付';
  175. }elseif($v['payment_type'] == 2){
  176. $v['payment_type'] = '付款码支付';
  177. }elseif($v['payment_type'] == 3){
  178. $v['payment_type'] = '个体户支付';
  179. }elseif($v['payment_type'] == 4){
  180. $v['payment_type'] = '充值卡支付';
  181. }else{
  182. $v['payment_type'] = '';
  183. }
  184. //计算毛利率
  185. }
  186. $yestoday = date('Y-m-d', strtotime('-1 day'));
  187. //获取超时未发订单
  188. $order_overtime = Order::where('is_del',0)->whereIn('status', [2,3])->where('delivery_date', '<', $yestoday)->where('createTime', '>=', '2019-09-18')->where('logistics_id', '=', '')->where(function($query) use($team_id, $self_role){
  189. if($self_role == '管理员') $query->where('team_id', $team_id);
  190. })->lists('id');
  191. $overtime = [];
  192. if(!empty($order_overtime)){
  193. $overtime['list'] = json_decode(json_encode($order_overtime), true);
  194. $overtime['count'] = count($order_overtime);
  195. }
  196. #销售筛选
  197. //$saler_ids = DB::table('admin_role')->where('role_name', '销售')->lists('user_id');
  198. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id){
  199. if($team_id>0) $query->where('team_id', $team_id);
  200. })->where('id','>', 1)->where('is_use', 1)->get();
  201. $adminList = json_decode(json_encode($adminList), true);
  202. $is_fx = env('IS_FX');
  203. return view('order/orderlist', ['result' =>$result,
  204. 'page' => $page,
  205. 'count' => $count,
  206. 'pages' => $pages,
  207. 'admin_id' => $admin_id,
  208. 'team_id' => $team_id,
  209. 'stime' => $stime,
  210. 'etime' => $etime,
  211. 'adminlist' => $adminList,
  212. 'search_admin' => $search_admin,
  213. 'self_role' => $self_role,
  214. 'receiverName' => $receiverName,
  215. 'receiverMobile' => $receiverMobile,
  216. 'status' => $status,
  217. 'id' => $id,
  218. 'last_url' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],
  219. 'overtime' => $overtime,
  220. 'warehouse' => $warehouse,
  221. 'is_fx' => $is_fx,
  222. 'thisTime' => date('Y-m-d H:i:s'),
  223. 'warehouseArr' => $this->warehouse,
  224. ]);
  225. }
  226. /**
  227. * 仓库管理员订单列表
  228. */
  229. public function warehouse(Request $request){
  230. $page = (int)$request->input('page');
  231. $pageSize = 20;
  232. if($page<=0){
  233. $page = 1;
  234. }
  235. $offset = ($page-1) * $pageSize;
  236. $admin_id = $request->input('admin_id');
  237. $search_admin = 1;
  238. $self_role = session('role_name');
  239. if($self_role == 'A仓库管理员'){
  240. $warehouse = 1;
  241. }elseif($self_role == 'B仓库管理员'){
  242. $warehouse = 2;
  243. }elseif($self_role == 'C仓管理员'){
  244. $warehouse = 3;
  245. }else{
  246. $warehouse = 0;
  247. }
  248. $stime = $request->input('stime');
  249. $etime = $request->input('etime');
  250. $delivery_type = (int)$request->input('delivery_type'); //发货状态 0到发货时间(未填物流信息) 2到发货时间全部
  251. $receiverName = trim($request->input('receiverName'));
  252. $receiverMobile = trim($request->input('receiverMobile'));
  253. $id = (int)$request->input('id');
  254. //仓库管理员只能看到 918之后的数据
  255. $_start = '2019-09-18';
  256. //仓库管理员只能看到已审核订单
  257. $today_date = date('Y-m-d');
  258. $count = Order::where(function($query) use($admin_id, $stime, $etime, $today_date, $delivery_type, $receiverName, $receiverMobile, $_start, $id){
  259. if($admin_id) $query->where('admin_id', $admin_id);
  260. if($stime) $query->where('createTime', '>=', $stime);
  261. if($etime) $query->where('createTime', '<=', $etime);
  262. if($delivery_type==0) $query->where('logistics_id', '=', '');
  263. if($receiverName) $query->where('receiverName', 'like', $receiverName . '%');
  264. if($receiverMobile) $query->where('receiverMobile', $receiverMobile);
  265. if($id>0) $query->where('id', $id);
  266. $query->where('createTime', '>=', $_start);
  267. })->where('is_del',0)->where('warehouse', $warehouse)->where('status', '>', 1)->where('delivery_date', '<=', $today_date)->count();
  268. if ($count > 1) {
  269. // 总页数
  270. $pages = ceil($count/$pageSize);
  271. }else{
  272. // 总页数
  273. $pages = 1;
  274. }
  275. $result = Order::where(function($query) use($admin_id, $stime, $etime, $today_date, $delivery_type, $receiverName, $receiverMobile, $_start, $id){
  276. if($admin_id) $query->where('admin_id', $admin_id);
  277. if($stime) $query->where('createTime', '>=', $stime);
  278. if($etime) $query->where('createTime', '<=', $etime);
  279. if($delivery_type==0) $query->where('logistics_id', '=', '');
  280. if($receiverName) $query->where('receiverName', 'like', $receiverName . '%');
  281. if($receiverMobile) $query->where('receiverMobile', $receiverMobile);
  282. $query->where('createTime', '>=', $_start);
  283. if($id>0) $query->where('id', $id);
  284. })->where('is_del',0)->where('warehouse', $warehouse)->where('status', '>', 1)->where('delivery_date', '<=', $today_date)->orderBy('verify_time', 'asc')->offset($offset)->limit($pageSize)->get();
  285. $result = json_decode(json_encode($result),true);
  286. $status_arr = array(
  287. 0 => '已录入',
  288. 1 => '待审核',
  289. 2 => '已审核',
  290. 3 => '已发货',
  291. 4 => '已退款',
  292. );
  293. foreach($result as $k=>&$v){
  294. //获取商品信息
  295. $v['goods'] = '';
  296. $goods = DB::table('order_goods_skus')->where('order_id', $v['id'])->where('is_del', 0)->get();
  297. if(!empty($goods)){
  298. foreach($goods as $item){
  299. $goods_name = Goods::where('id', $item->goods_id)->pluck('name');
  300. $props_name = GoodsSkus::where('id', $item->sku_id)->pluck('propsName');
  301. $v['goods'][] = $goods_name.'('.$props_name.')&nbsp; 数量/重量:'.$item->num.'';
  302. }
  303. }
  304. $v['status_name'] = $status_arr[$v['status']];
  305. }
  306. $yestoday = date('Y-m-d', strtotime('-1 day'));
  307. //获取超时未发订单
  308. $order_overtime = Order::where('is_del',0)->where('warehouse', $warehouse)->where('status', '>', 1)->where('delivery_date', '<', $yestoday)->where('createTime', '>=', $_start)->where('logistics_id', '=', '')->lists('id');
  309. $overtime = [];
  310. if(!empty($order_overtime)){
  311. $overtime['list'] = json_decode(json_encode($order_overtime), true);
  312. $overtime['count'] = count($order_overtime);
  313. }
  314. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('is_use', 1)->where('id','>', 1)->get();
  315. $adminList = json_decode(json_encode($adminList), true);
  316. return view('order/warehouselist', ['result' =>$result,
  317. 'page' =>$page,
  318. 'count' =>$count,
  319. 'pages' =>$pages,
  320. 'admin_id' =>$admin_id,
  321. 'stime' =>$stime,
  322. 'etime' =>$etime,
  323. 'delivery_type' =>$delivery_type,
  324. 'adminlist' =>$adminList,
  325. 'receiverName' =>$receiverName,
  326. 'receiverMobile' =>$receiverMobile,
  327. 'id' =>$id,
  328. 'today' =>$today_date,
  329. 'yestoday' => $yestoday,
  330. 'overtime' => $overtime,
  331. ]);
  332. }
  333. /**
  334. * 预售订单列表
  335. */
  336. public function predictOrder(Request $request){
  337. $page = (int)$request->input('page');
  338. $pageSize = 20;
  339. if($page<=0){
  340. $page = 1;
  341. }
  342. $offset = ($page-1) * $pageSize;
  343. $self_role = session('role_name');
  344. if($self_role == '超级管理员' || $self_role == '团队主管' || $self_role == '管理员' || $self_role == '售后管理员'){
  345. $admin_id = $request->input('admin_id');
  346. $search_admin = 1;
  347. }else{
  348. $admin_id = session('admin_id');
  349. $search_admin = 0;
  350. }
  351. $team_id = $request->input('team_id');
  352. #只能看自己团队的
  353. if($self_role != '超级管理员' && $self_role != '售后管理员'){
  354. $self_id = session('admin_id');
  355. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  356. }
  357. //假如有团队筛选,检索销售队员
  358. $sale_ids = null;
  359. if($team_id>0){
  360. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  361. }
  362. $stime = $request->input('stime');
  363. $etime = $request->input('etime');
  364. $receiverName = trim($request->input('receiverName'));
  365. $receiverMobile = trim($request->input('receiverMobile'));
  366. $status = $request->input('status');
  367. $if_all = (int)$request->input('if_all');
  368. if($status === null){
  369. $status = -1;
  370. }
  371. //预售逻辑 -> 预发货时间大于下单时间
  372. $today = date('Y-m-d');
  373. //DB::connection()->enableQueryLog();
  374. $count = Order::where(function($query) use($admin_id, $stime, $etime, $receiverName, $receiverMobile, $self_role, $sale_ids, $status, $if_all, $today){
  375. if($admin_id) $query->where('admin_id', $admin_id);
  376. if($stime) $query->where('createTime', '>=', $stime);
  377. if($etime) $query->where('createTime', '<=', $etime);
  378. if($receiverName) $query->where('receiverName', 'like', $receiverName . '%');
  379. if($receiverMobile) $query->where('receiverMobile', $receiverMobile);
  380. if($self_role == '管理员') $query->where('status','>',0);
  381. if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  382. if(($status>=0)) $query->where('status', $status);
  383. if(!$if_all) $query->where('delivery_date','>=',$today);
  384. })->where('is_del',0)->whereRaw('delivery_date>createTime')->count();
  385. //print_r(DB::getQueryLog());exit;
  386. if ($count > 1) {
  387. // 总页数
  388. $pages = ceil($count/$pageSize);
  389. }else{
  390. // 总页数
  391. $pages = 1;
  392. }
  393. $result = Order::where(function($query) use($admin_id, $stime, $etime, $receiverName, $receiverMobile, $self_role, $sale_ids, $status, $if_all, $today){
  394. if($admin_id) $query->where('admin_id', $admin_id);
  395. if($stime) $query->where('createTime', '>=', $stime);
  396. if($etime) $query->where('createTime', '<=', $etime);
  397. if($receiverName) $query->where('receiverName', 'like', $receiverName . '%');
  398. if($receiverMobile) $query->where('receiverMobile', $receiverMobile);
  399. if($self_role == '管理员') $query->where('status','>',0);
  400. if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  401. if(($status>=0)) $query->where('status', $status);
  402. if(!$if_all) $query->where('delivery_date','>=',$today);
  403. })->where('is_del',0)->whereRaw('delivery_date>createTime')->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
  404. $result = json_decode(json_encode($result),true);
  405. $status_arr = array(
  406. 0 => '已录入',
  407. 1 => '待审核',
  408. 2 => '已审核',
  409. 3 => '已发货',
  410. 4 => '已退款'
  411. );
  412. foreach($result as $k=>&$v){
  413. //获取商品信息
  414. $v['goods'] = '';
  415. $goods = DB::table('order_goods_skus')->where('order_id', $v['id'])->where('is_del', 0)->get();
  416. if(!empty($goods)){
  417. foreach($goods as $item){
  418. $goods_name = Goods::where('id', $item->goods_id)->pluck('name');
  419. $props_name = GoodsSkus::where('id', $item->sku_id)->pluck('propsName');
  420. $v['goods'][] = $goods_name.'('.$props_name.')&nbsp; 数量/重量:'.$item->num.'';
  421. }
  422. }
  423. #加粉时间
  424. $fanTime = DB::table('customers')->select('fanTime')->where('phone', $v['receiverMobile'])->first();
  425. $v['fanTime'] = isset($fanTime->fanTime) ? $fanTime->fanTime : '';
  426. $v['receiverMobile'] = substr($v['receiverMobile'], 0, 3).'****'.substr($v['receiverMobile'], 7);
  427. #获取状态机
  428. if( ($self_role == '销售' || $self_role == '分销销售') && !in_array($v['status'], [0]) ){
  429. $v['enable_status'] = [];
  430. }else{
  431. $v['enable_status'] = $this->getEnableStatus($v['status']);
  432. }
  433. $v['status_name'] = $status_arr[$v['status']];
  434. }
  435. #销售筛选
  436. //$saler_ids = DB::table('admin_role')->where('role_name', '销售')->lists('user_id');
  437. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id){
  438. if($team_id>0) $query->where('team_id', $team_id);
  439. })->where('id','>', 1)->where('is_use', 1)->get();
  440. $adminList = json_decode(json_encode($adminList), true);
  441. return view('order/predictorderlist', ['result' =>$result,
  442. 'page' => $page,
  443. 'count' => $count,
  444. 'pages' => $pages,
  445. 'admin_id' => $admin_id,
  446. 'team_id' => $team_id,
  447. 'stime' => $stime,
  448. 'etime' => $etime,
  449. 'adminlist' => $adminList,
  450. 'search_admin' => $search_admin,
  451. 'self_role' => $self_role,
  452. 'receiverName' => $receiverName,
  453. 'receiverMobile' => $receiverMobile,
  454. 'status' => $status,
  455. 'if_all' => $if_all,
  456. 'last_url' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],
  457. 'warehouseArr' => $this->warehouse,
  458. ]);
  459. }
  460. /**
  461. * 添加订单
  462. * @return \Illuminate\View\View
  463. */
  464. public function ordercreate(Request $request)
  465. {
  466. $catelist = DB::table('goods_category')->select(DB::raw('distinct goods_category_name'))->lists('goods_category_name');
  467. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('is_use', 1)->where('id','>', 1)->get();
  468. $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
  469. $teamList = json_decode(json_encode($teamList), true);
  470. $adminList = json_decode(json_encode($adminList), true);
  471. $self_role = session('role_name');
  472. $createTime = date('Y-m-d H:i:s');
  473. //当月礼品
  474. $mtime = date('Y-m-01');
  475. $gift_info = DB::table('customer_month_gift')->where('mtime', $mtime)->get();
  476. $gift_info = json_decode(json_encode($gift_info), true);
  477. $m_gift=array();
  478. $b_gift=array();
  479. foreach($gift_info as $k=>$v){
  480. //获取礼品信息
  481. $goods_name = Goods::where('id', $v['goods_id'])->pluck('name');
  482. $sku = GoodsSkus::where('id', $v['sku_id'])->where('is_del', 0)->where(function($query){
  483. $query->where('is_white', 1)->orWhere('quantity','>',0);
  484. })->first();
  485. if(empty($sku)) continue;
  486. //计算实际库存 -还未到卖家云的量
  487. $quantity = GoodsSkus::actualQuantityByRedis($v['sku_id']);
  488. $quantity = $quantity['quantity'];
  489. if($quantity<=0){
  490. continue;
  491. }
  492. $data = array();
  493. $data['id'] = $v['id'];
  494. $data['goods_name'] = $goods_name;
  495. $data['propsName'] = $sku['propsName'];
  496. $data['price'] = $sku['price'];
  497. $data['num'] = $v['num'];
  498. if($v['type'] == 0){
  499. $m_gift[] = $data;
  500. }else{
  501. $b_gift[] = $data;
  502. }
  503. }
  504. return view('order/ordercreate', [
  505. 'categorylist' => $catelist,
  506. 'adminlist' => $adminList,
  507. 'teamlist' => $teamList,
  508. 'self_role' => $self_role,
  509. 'createTime' => $createTime,
  510. 'm_gift' => $m_gift,
  511. 'b_gift' => $b_gift,
  512. 'warehouseArr' => $this->warehouse,
  513. ]);
  514. }
  515. /**
  516. * 分组管理-进行添加操作
  517. * @param Request $request
  518. * @return \Illuminate\Http\RedirectResponse
  519. */
  520. public function orderstore(Request $request)
  521. {
  522. $this->validate($request, [
  523. 'customerName' => 'required|between:1,6',
  524. 'receiverName' => 'required|between:1,6',
  525. 'receiverMobile' => 'required|regex:/^1[3456789]\d{9}$/',
  526. 'buyer_phone' => 'required|regex:/^1[3456789]\d{9}$/',
  527. 'fanTime' => 'required|date',
  528. 'receiverState' => 'required|between:1,5',
  529. 'receiverCity' => 'required|between:1,10',
  530. 'receiverAddress' => 'required',
  531. 'delivery_date' => 'required|date',
  532. //'createTime' => 'required|date',
  533. 'receivedAmount' => 'required|numeric|min:1',
  534. //'goods_note' => 'required|min:1',
  535. 'warehouse' => 'required|integer|min:1',
  536. //'skus' => 'required'
  537. ], [
  538. 'customerName.required' => '买家姓名不能为空',
  539. 'customerName.between' => '买家姓名不能超过6个字符',
  540. 'receiverName.required' => '收货人不能为空',
  541. 'receiverName.between' => '收货人不能超过6个字符',
  542. 'receiverMobile.required' => '收货人手机号不能为空',
  543. 'buyer_phone.required' => '买家手机号不能为空',
  544. 'receiverMobile.regex' => '收货人手机号格式有误',
  545. 'fanTime.required' => '加粉时间不能为空',
  546. 'receiverState.required' => '省不能为空',
  547. 'receiverState.between' => '省填写有误',
  548. 'receiverCity.required' => '市不能为空',
  549. 'receiverCity.between' => '市填写有误',
  550. 'receiverAddress.required' => '详细地址不能为空',
  551. 'delivery_date.required' => '预发货时间不能为空',
  552. //'createTime.required' => '下单时间不能为空',
  553. 'receivedAmount.required' => '付款金额不能为空',
  554. 'receivedAmount.numeric' => '付款金额必须为数字',
  555. 'goods_note.required' => '商品信息不能为空',
  556. 'warehouse.required' => '仓库必须选择',
  557. 'warehouse.min' => '仓库必须选择',
  558. ]);
  559. //redis防重复数据
  560. $phone = $request->input('receiverMobile');
  561. $redisKey = 'seafood.order_phone_add.'.$phone;
  562. $res = RedisModel::setnx($redisKey, 1);
  563. if( $res == 0 ){
  564. $msg = '操作过于频繁,请一分钟后继续操作';
  565. // exit('<script>alert("'.$msg.'");history.back()</script>');
  566. return back()->with('info', $msg);
  567. //return redirect('/admin/order/index')->with('info', $msg);
  568. }
  569. $res = RedisModel::expire( $redisKey, 60 );
  570. $skus = $request->input('skus');
  571. $warehouse = (int)$request->input('warehouse');
  572. if($warehouse == 3 && empty($skus)){
  573. $msg = 'C仓必须添加卖家云商品';
  574. // exit('<script>alert("'.$msg.'");history.back()</script>');
  575. //return redirect('/admin/order/index')->with('info', $msg);
  576. return back()->with('info', $msg);
  577. }
  578. //数据库-新增数据
  579. $order = array();
  580. $order['eshopCode'] = config('constants.ESHOP_CODE'); //默认
  581. $order['outerCode'] = Order::createOuterCode();//随机生成
  582. $order['status'] = (int)$request->input('status');
  583. $order['customerName'] = trim($request->input('customerName')); //买家名称
  584. $order['shippingType'] = '卖家包邮';
  585. $order['expressCompanyCode'] = '10084';
  586. $order['expressCompanyName'] = '顺丰快递';
  587. $order['receiverName'] = trim($request->input('receiverName')); //收件人
  588. $order['receiverMobile'] = trim($request->input('receiverMobile')); //收件人手机
  589. $order['receiverPhone'] = trim($request->input('receiverMobile')); //收件人手机
  590. $order['receiverState'] = trim($request->input('receiverState')); //省份
  591. $order['receiverCity'] = trim($request->input('receiverCity')); //市
  592. #省市格式处理
  593. // $order['receiverState'] = str_replace(['省', '市'], '', $order['receiverState']);
  594. // $order['receiverCity'] = str_replace('市', '', $order['receiverCity']);
  595. $order['receiverDistrict'] = trim($request->input('receiverDistrict')); //区
  596. $order['receiverStreet'] = trim($request->input('receiverStreet')); //街道
  597. $order['receiverAddress'] = trim($request->input('receiverAddress')); //详细地址
  598. $order['buyerPostageFee'] = 0; //买家邮费
  599. $order['discountFee'] = 0; //订单优惠
  600. $order['adjustFee'] = 0; //订单调价
  601. $order['serviceFee'] = 0; //服务费
  602. $order['paymentMethod'] = '在线付款';
  603. $order['paymentType'] = '网银';
  604. $order['paymentAccount'] = '18322225555'; // 付款账号,暂时默认写死
  605. $order['paymentTime'] = date('Y-m-d H:i:s'); // 付款时间
  606. $order['receivedAmount'] = trim($request->input('receivedAmount')); //付款金额
  607. $order['buyerMemo'] = trim($request->input('buyerMemo', '')); // 买家备注
  608. $order['sellerMemo'] = trim($request->input('sellerMemo', '')); // 卖家备注
  609. $order['createTime'] = date('Y-m-d H:i:s'); // 订单创建时间
  610. $order['modifyTime'] = date('Y-m-d H:i:s'); // 订单修改时间
  611. $order['should_amount'] = $request->input('should_amount');
  612. if(!$order['should_amount']) $order['should_amount'] = null;
  613. $order['warehouse'] = (int)$request->input('warehouse'); //仓库
  614. //商品信息
  615. $skus = $request->input('skus');
  616. $gnum = $request->input('gnum');
  617. if(!empty($skus)){
  618. foreach($skus as $k=>$sku_id){
  619. $sku_info = GoodsSkus::where('id', $sku_id)->first();
  620. $goods_info = Goods::where('id', $sku_info->goodsCode)->first();
  621. $params = array();
  622. $params['outerCode'] = $order['outerCode']+$k+1; //外部平台中子订单编码
  623. $params['outerParentCode'] = $order['outerCode']; //外部平台中订单编码
  624. $params['goodsCode'] = $sku_info->goodsCode;// strstr($goods->code,'-',true);
  625. $params['skuCode'] = $sku_id;
  626. #商品名称
  627. $params['goodsName'] = $goods_info->name;
  628. $params['propertiesName'] = $sku_info->propsName;
  629. //$params['goodsName'] = '海钓黄翅鱼';//$goods->code;
  630. $params['price'] = $sku_info->price;
  631. $params['quantity'] = $gnum[$k];
  632. $params['discountFee'] = 0;
  633. $params['adjustFee'] = 0;
  634. $params['refundStatus'] = '正常';
  635. $order['skuList'][] = $params;
  636. #新逻辑,c仓,判断实际库存足否
  637. if($order['warehouse'] == 3 && $sku_info->is_white != 1){
  638. $quantity_redis = GoodsSkus::actualQuantityByRedis($sku_id);
  639. $act_quantity = $quantity_redis['quantity'];
  640. if($act_quantity < $params['quantity']){
  641. // exit('<script>alert("商品库存不足");history.back()</script>');
  642. //return redirect('/admin/order/index')->with('info', '商品库存不足');
  643. // return back()->with('info', '商品库存不足');
  644. echo '<script>alert("商品库存不足");history.back()</script>';die;
  645. }
  646. }
  647. }
  648. }
  649. //卖家云添加订单 ->改为审核通过再同步卖家云
  650. //$mj_add = Order::mjOrderAdd($order);
  651. //本系统订单额外参数
  652. $order['goods_note'] = trim($request->input('goods_note'));
  653. if($request->input('cost')) $order['cost'] = trim($request->input('cost')); //成本
  654. $order['logistics_id'] = trim($request->input('logistics_id')); //物流单号
  655. $order['is_refund'] = (int)$request->input('is_refund'); //是否退过单
  656. if($request->input('refund_price')) $order['refund_price'] = trim($request->input('refund_price')); //应该退补差价
  657. $order['delivery_date'] = !empty($request->input('delivery_date')) ? $request->input('delivery_date') : date('Y-m-d'); //发货日期
  658. $order['order_status'] = (int)$request->input('order_status');
  659. $order['payment_type'] = (int)$request->input('payment_type'); //支付方式
  660. $order['buyer_phone'] = $request->input('buyer_phone');
  661. $order['use_coupon'] = (int)$request->input('use_coupon');
  662. $order['use_b_gift'] = (int)$request->input('use_b_gift');
  663. $m_gift_id = (int)$request->input('m_gift_id');
  664. if($m_gift_id>0){
  665. $order['use_m_gift'] = 1;
  666. }
  667. //充值卡验证余额
  668. if($order['payment_type'] == 4){
  669. $deposit_phone = $request->input('deposit_phone');
  670. $customerInfo = CustomerInfo::where('phone', $deposit_phone)->first();
  671. $balance = isset($customerInfo->balance) ? $customerInfo->balance : 0;
  672. if($balance < $order['receivedAmount']){
  673. //余额不足
  674. // exit('<script>alert("充值卡余额不足");history.back()</script>');
  675. //return redirect('/admin/order/index')->with('info', '充值卡余额不足');
  676. echo '<script>alert("充值卡余额不足");history.back()</script>';die;
  677. }
  678. }
  679. DB::beginTransaction();
  680. try{
  681. #更新客户表
  682. $customer = array();
  683. $customers['name'] = $order['receiverName'];
  684. $customers['receiverState'] = $order['receiverState'];
  685. $customers['receiverCity'] = $order['receiverCity'];
  686. $customers['receiverDistrict'] = $order['receiverDistrict'];
  687. $customers['receiverStreet'] = $order['receiverStreet'];
  688. $customers['receiverAddress'] = $order['receiverAddress'];
  689. $if_e = DB::table('customers')->select('id')->where('phone', $order['receiverMobile'])->first();
  690. if(isset($if_e->id)){
  691. $customers['is_fugou'] = 1;
  692. $order['is_fugou'] = 1;
  693. $up_c = DB::table('customers')->where('id', $if_e->id)->update($customers);
  694. }else{
  695. $customers['phone'] = $order['receiverMobile'];
  696. $customers['is_fugou'] = 0;
  697. $customers['fanTime'] = $request->input('fanTime');
  698. $customers['admin_id'] = session('admin_id');
  699. $in_c = DB::table('customers')->insert($customers);
  700. }
  701. if(!empty($skus)){
  702. $order_goods = $order['skuList'];
  703. unset($order['skuList']);
  704. }
  705. //$order['orderCode'] = $mj_add['order']['code'];
  706. $admin_id = (int)$request->input('admin_id');
  707. if($admin_id>0){
  708. $admin_info = DB::table('admin')->select('realname', 'team_id')->where('id', $admin_id)->first();
  709. $order['admin_id'] = $admin_id;
  710. $order['admin_name'] = $admin_info->realname;
  711. $order['team_id'] = $admin_info->team_id;
  712. }else{
  713. $order['admin_id'] = session('admin_id');
  714. $order['admin_name'] = session('real_name');
  715. $order['team_id'] = session('team_id');
  716. }
  717. //新加is_able 9.4之前粉丝不参与销售复购统计
  718. $fanTime = $request->input('fanTime');
  719. if($fanTime < '2019-09-04'){
  720. $order['is_able'] = 0;
  721. }
  722. $res = DB::table('order')->insertGetId($order);
  723. if($res){
  724. if(!empty($skus)){
  725. //订单商品插入
  726. $order_skus = array();
  727. foreach($order_goods as $k=>$sku){
  728. $order_skus[$k]['sku_id'] = $sku['skuCode'];
  729. $order_skus[$k]['order_id'] = $res;
  730. $order_skus[$k]['goods_id'] = $sku['goodsCode'];
  731. $order_skus[$k]['num'] = $sku['quantity'];
  732. $order_skus[$k]['price'] = $sku['price'];
  733. }
  734. $order_skus_insert = DB::table('order_goods_skus')->insert($order_skus);
  735. }
  736. #加赠品逻辑
  737. $use_coupon = (int)$request->input('use_coupon');
  738. $use_b_gift = (int)$request->input('use_b_gift');
  739. if($use_coupon == 1){
  740. $coupon = DB::table('customer_coupons')->where('phone', $order['buyer_phone'])->where('is_use',0)->orderBy('id', 'desc')->first();
  741. if(!empty($coupon)){
  742. $cup = array();
  743. $cup['is_use'] = 1;
  744. $cup['use_time'] = date('Y-m-d H:i:s');
  745. $cup['order_id'] = $res;
  746. DB::table('customer_coupons')->where('id', $coupon->id)->update($cup);
  747. }
  748. }
  749. if($m_gift_id>0){
  750. $m_gift = DB::table('customer_month_gift')->where('id', $m_gift_id)->where('type', 0)->first();
  751. $order_m_gift = array();
  752. $m_sku = GoodsSkus::where('id', $m_gift->sku_id)->first();
  753. $order_m_gift['sku_id'] = $m_gift->sku_id;
  754. $order_m_gift['order_id'] = $res;
  755. $order_m_gift['goods_id'] = $m_sku->goodsCode;
  756. $order_m_gift['num'] = $m_gift->num;
  757. $order_m_gift['price'] = $m_sku->price;
  758. $order_m_gift['gift_id'] = $m_gift_id;
  759. $order_gift_insert = DB::table('order_goods_skus')->insert($order_m_gift);
  760. #记录领取
  761. $receive_gift = array();
  762. $receive_gift['phone'] = $order['buyer_phone'];
  763. $receive_gift['dtime'] = date('Y-m-d');
  764. $receive_gift['order_id'] = $res;
  765. $receive_gift['gift_id'] = $m_gift_id;
  766. $receive_gift['gift_type'] = 0;
  767. DB::table('customer_gift_receives')->insert($receive_gift);
  768. }
  769. $mdate = date('Y-m-01');
  770. if($use_b_gift>0){
  771. $b_gifts = DB::table('customer_month_gift')->where('mtime', $mdate)->where('type', 1)->get();
  772. $b_gifts = json_decode(json_encode($b_gifts),true);
  773. foreach($b_gifts as $b_gift){
  774. $order_b_gift = array();
  775. $b_sku = GoodsSkus::where('id', $b_gift['sku_id'])->first();
  776. $order_b_gift['sku_id'] = $b_gift['sku_id'];
  777. $order_b_gift['order_id'] = $res;
  778. $order_b_gift['goods_id'] = $b_sku->goodsCode;
  779. $order_b_gift['num'] = $b_gift['num'];
  780. $order_b_gift['price'] = $b_sku->price;
  781. $order_b_gift['gift_id'] = $b_gift['id'];
  782. $order_gift_insert = DB::table('order_goods_skus')->insert($order_b_gift);
  783. }
  784. #记录领取
  785. $receive_gift = array();
  786. $receive_gift['phone'] = $order['buyer_phone'];
  787. $receive_gift['dtime'] = date('Y-m-d');
  788. $receive_gift['order_id'] = $res;
  789. $receive_gift['gift_type'] = 1;
  790. DB::table('customer_gift_receives')->insert($receive_gift);
  791. }
  792. #新加逻辑,若付款方式为充值卡/消费记录
  793. if($order['payment_type'] == 4){
  794. $consum = new CustomerConsum();
  795. $consum->phone = $deposit_phone;
  796. $consum->order_id = $res;
  797. $consum->money = $order['receivedAmount'];
  798. $consum->save();
  799. //更新余额
  800. $customerInfo->balance = $customerInfo->balance - $order['receivedAmount'];
  801. $customerInfo->save();
  802. }
  803. #记录操作日志
  804. $self_id = session('admin_id');
  805. $self_name = session('real_name');
  806. if($order['status'] == 0){
  807. $status_text = '仅录入';
  808. }else{
  809. $status_text = '录入并提审';
  810. }
  811. $context = "录入订单:".$status_text;
  812. $type = 0; //订单录入
  813. $tables = 'order';
  814. $data_id = $res;
  815. Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
  816. #如果提交审核,发短信
  817. //if( $order['status'] == 1 ) $this->sendRemind($self_id, $res, 1);
  818. }
  819. DB::commit();
  820. // +++++ 新加预占库存到redis
  821. if($order['warehouse'] == 3 && $order['status'] == 1){
  822. $sku_redis = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $res)->where('is_del', 0)->groupBy('sku_id')->get();
  823. $sku_redis = json_decode(json_encode($sku_redis),true);
  824. Order::diffSkuToRedis('', $sku_redis);
  825. }
  826. }catch (Exception $e){
  827. DB::rollback();
  828. }
  829. return redirect('/admin/order/index')->with('info', '添加成功');
  830. }
  831. /**
  832. * 分组管理-编辑分组界面
  833. * @param $id
  834. * @return \Illuminate\View\View
  835. */
  836. public function orderedit($id,Request $request)
  837. {
  838. $stime = $request->input('stime');
  839. $etime = $request->input('etime');
  840. $receiverName = $request->input('receiverName');
  841. $receiverMobile = $request->input('receiverMobile');
  842. $admin_id = (int)$request->input('admin_id');
  843. $page = (int)$request->input('page');
  844. $last_url = $request->input('last_url');
  845. $self_role = session('role_name');
  846. //$catelist = DB::table('goods_category')->select(DB::raw('distinct goods_category_name'))->lists('goods_category_name');
  847. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->where('is_use', 1)->get();
  848. $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
  849. $teamList = json_decode(json_encode($teamList), true);
  850. $adminList = json_decode(json_encode($adminList), true);
  851. $order = Order::findOrFail($id);
  852. $order = json_decode(json_encode($order), true);
  853. // 临时处理,只能用自己充值卡
  854. $order['deposit_phone'] = $order['receiverMobile'];
  855. $order['receiverMobile'] = substr($order['receiverMobile'], 0, 3).'****'.substr($order['receiverMobile'], 7);
  856. $order['goods'] = '';
  857. $order_skus = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $id)->whereNull('gift_id')->lists('num','sku_id');
  858. $skus = array_keys($order_skus);
  859. if(!empty($skus)){
  860. $goods = Goods::select('gs.id as sku_id', 'goods.id as goods_id', 'goods.name', 'gs.propsName as props_name', 'gs.price', 'gs.is_weigh', 'gs.is_white', 'gs.quantity')->leftJoin('goods_skus as gs', 'gs.goodsCode', '=', 'goods.id')->whereIn('gs.id', $skus)->orderBy('goods.id', 'desc')->get();
  861. $goods = json_decode(json_encode($goods),true);
  862. foreach($goods as $k=>&$v){
  863. $v['num'] = $order_skus[$v['sku_id']];
  864. //计算实际库存 -还未到卖家云的量
  865. $quantity_redis = GoodsSkus::actualQuantityByRedis($v['sku_id']);
  866. $v['quantity'] = $quantity_redis['quantity'];
  867. $v['quantity'] = $v['quantity'] + $v['num'];
  868. $v['quantity_str'] = $v['quantity'];
  869. if($v['is_white'] == 1){
  870. $v['quantity_str'] = '虚拟库存';
  871. }
  872. }
  873. $order['goods'] = $goods;
  874. }
  875. $deposit_phone = $order['deposit_phone'];
  876. if($order['payment_type'] == 4){
  877. $consum = CustomerConsum::select('phone')->where('order_id', $id)->first();
  878. $deposit_phone = isset($consum->phone) ? $consum->phone : $order['deposit_phone'];
  879. }
  880. //下单当月礼品
  881. $mtime = date('Y-m-01', strtotime($order['create_time']));
  882. $gift_info = DB::table('customer_month_gift')->where('mtime', $mtime)->get();
  883. $gift_info = json_decode(json_encode($gift_info), true);
  884. $m_gift=array();
  885. $b_gift=array();
  886. foreach($gift_info as $k=>$val){
  887. //获取礼品信息
  888. $goods_name = Goods::where('id', $val['goods_id'])->pluck('name');
  889. $sku = GoodsSkus::where('id', $val['sku_id'])->where('is_del', 0)->where(function($query){
  890. $query->where('is_white', 1)->orWhere('quantity','>',0);
  891. })->first();
  892. if(empty($sku)) continue;
  893. //计算实际库存 -还未到卖家云的量
  894. $quantity = GoodsSkus::actualQuantityByRedis($val['sku_id']);
  895. $quantity = $quantity['quantity'];
  896. if($quantity<=0){
  897. continue;
  898. }
  899. $data = array();
  900. $data['id'] = $val['id'];
  901. $data['goods_name'] = $goods_name;
  902. $data['propsName'] = $sku['propsName'];
  903. $data['price'] = $sku['price'];
  904. if($val['type'] == 0){
  905. $m_gift[] = $data;
  906. }else{
  907. $b_gift[] = $data;
  908. }
  909. }
  910. #获取赠品信息
  911. $order_gifts = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $id)->where('gift_id','>',0)->lists('gift_id');
  912. $order_m_gifts = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 0)->lists('id');
  913. $order_b_gifts = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 1)->lists('id');
  914. #查询用户会员信息
  915. $cust_info = array();
  916. $cust_info['coupons'] = 0;
  917. $cust_info['if_m_gift'] = 0;
  918. $cust_info['if_b_gift'] = 0;
  919. $phone = $order['buyer_phone'];
  920. $time = date('Y-m-d H:i:s');
  921. $customer_info = DB::table('customer_vip')->where('phone', $phone)->where('is_del', 0)->first();
  922. if(!empty($customer_info) && $order['create_time']>'2019-11-28'){
  923. //判断是否过期
  924. if($customer_info->vip_end_time > $time){
  925. //未过期,1.获取优惠券,获取当月礼,获取生日礼,判断领取
  926. $coupons = DB::table('customer_coupons')->where('phone', $phone)->where('is_use', 0)->where('end_time', '>', $time)->first();
  927. $cust_info['coupons'] = !empty($coupons) ? 1:0;
  928. //判断是否领取当月礼
  929. $if_m_gift = DB::table('customer_gift_receives')->where('phone', $phone)->where('is_del',0)->where('gift_type', 0)->where('dtime', '>=', $mtime)->first();
  930. if(empty($if_m_gift)){
  931. $cust_info['if_m_gift'] = 1;
  932. }
  933. //判断生日礼
  934. $birth_time = date('m', strtotime($customer_info->birthday));
  935. $tom = date('m');
  936. if($tom==$birth_time){
  937. //生日期内,判断是否已领
  938. $if_b_gift = DB::table('customer_gift_receives')->where('phone', $phone)->where('is_del',0)->where('gift_type', 1)->where('dtime', '>=', $mtime)->first();
  939. if(empty($if_b_gift)){
  940. $cust_info['if_b_gift'] = 1;
  941. }
  942. }
  943. }
  944. }
  945. return view('order/orderedit', [
  946. 'order' => $order,
  947. //'categorylist' => $catelist,
  948. 'adminlist' => $adminList,
  949. 'teamlist' => $teamList,
  950. 'self_role' => $self_role,
  951. 'str_query' => 'page='.$page.'&admin_id='.$admin_id.'&stime='.$stime.'&etime='.$etime.'&receiverName='.$receiverName.'&receiverMobile='.$receiverMobile,
  952. 'last_url' => $last_url,
  953. 'deposit_phone' => $deposit_phone,
  954. 'm_gift' => $m_gift,
  955. 'b_gift' => $b_gift,
  956. 'order_m_gifts' => $order_m_gifts,
  957. 'order_b_gifts' => $order_b_gifts,
  958. 'cust_info' => $cust_info,
  959. 'warehouseArr' => $this->warehouse,
  960. ]);
  961. }
  962. /**
  963. * 分组管理-进行编辑操作
  964. * @param Request $request
  965. * @return \Illuminate\Http\RedirectResponse
  966. */
  967. public function orderupdate(Request $request)
  968. {
  969. $this->validate($request, [
  970. 'id' => 'required',
  971. 'customerName' => 'required|between:1,6',
  972. 'receiverName' => 'required|between:1,6',
  973. 'receiverMobile' => 'required|regex:/^1[3456789][\d\*]{9}$/',
  974. 'receiverState' => 'required|between:1,5',
  975. 'receiverCity' => 'required|between:1,10',
  976. 'receiverAddress' => 'required',
  977. 'delivery_date' => 'required|date',
  978. //'createTime' => 'required|date',
  979. 'receivedAmount' => 'required|numeric|min:1',
  980. //'goods_note' => 'required|min:1',
  981. 'warehouse' => 'required|integer|min:1',
  982. ], [
  983. 'customerName.required' => '买家姓名不能为空',
  984. 'customerName.between' => '买家姓名不能超过6个字符',
  985. 'receiverName.required' => '收货人不能为空',
  986. 'receiverName.between' => '收货人不能超过6个字符',
  987. 'receiverMobile.required' => '收货人手机号不能为空',
  988. 'receiverMobile.regex' => '收货人手机号格式有误',
  989. 'receiverState.required' => '省不能为空',
  990. 'receiverState.between' => '省填写有误',
  991. 'receiverCity.required' => '市不能为空',
  992. 'receiverCity.between' => '市填写有误',
  993. 'receiverAddress.required' => '详细地址不能为空',
  994. 'delivery_date.required' => '预发货时间不能为空',
  995. //'createTime.required' => '下单时间不能为空',
  996. 'receivedAmount.required' => '付款金额不能为空',
  997. 'receivedAmount.numeric' => '付款金额必须为数字',
  998. 'goods_note.required' => '商品信息不能为空',
  999. 'warehouse.required' => '仓库必须选择',
  1000. 'warehouse.min' => '仓库必须选择',
  1001. ]);
  1002. $str_query = trim($request->input('str_query'));
  1003. $last_url = trim($request->input('last_url'));
  1004. $phone = trim($request->input('receiverMobile'));
  1005. //更新数据
  1006. $order = array();
  1007. $order['customerName'] = trim($request->input('customerName')); //买家名称
  1008. $order['receiverName'] = trim($request->input('receiverName')); //收件人
  1009. if(preg_match('/^1\d{10}$/', $phone)) $order['receiverMobile'] = $phone; //收件人手机
  1010. $order['receiverState'] = trim($request->input('receiverState')); //省份
  1011. $order['receiverCity'] = trim($request->input('receiverCity')); //市
  1012. // $order['receiverState'] = str_replace(['省', '市'], '', $order['receiverState']);
  1013. // $order['receiverCity'] = str_replace('市', '', $order['receiverCity']);
  1014. //$order['receiverDistrict'] = trim($request->input('receiverDistrict')); //区
  1015. //$order['receiverStreet'] = trim($request->input('receiverStreet')); //街道
  1016. $order['receiverAddress'] = trim($request->input('receiverAddress')); //详细地址
  1017. $order['receivedAmount'] = trim($request->input('receivedAmount')); //付款金额
  1018. $order['buyerMemo'] = trim($request->input('buyerMemo', '')); // 买家备注
  1019. $order['sellerMemo'] = trim($request->input('sellerMemo', '')); // 卖家备注
  1020. //$order['goods_note'] = trim($request->input('goods_note'));
  1021. if($request->input('cost')) $order['cost'] = trim($request->input('cost')); //成本
  1022. //$order['logistics_id'] = trim($request->input('logistics_id')); //物流单号
  1023. $order['is_refund'] = (int)$request->input('is_refund'); //是否退过单
  1024. if($request->input('refund_price')) $order['refund_price'] = trim($request->input('refund_price')); //应该退补差价
  1025. //$order['is_fugou'] = (int)$request->input('is_fugou'); //是否复购
  1026. $order['warehouse'] = (int)$request->input('warehouse'); //仓库
  1027. $order['delivery_date'] = !empty($request->input('delivery_date')) ? $request->input('delivery_date') : date('Y-m-d'); //发货日期
  1028. //if(!empty($request->input('createTime'))) $order['createTime'] = $request->input('createTime'); // 订单创建时间
  1029. $order['order_status'] = (int)$request->input('order_status');
  1030. $order['payment_type'] = (int)$request->input('payment_type'); //支付方式
  1031. //$order['status'] = (int)$request->input('status');
  1032. $order['should_amount'] = $request->input('should_amount');
  1033. if(!$order['should_amount']) $order['should_amount'] = null;
  1034. $admin_id = (int)$request->input('admin_id');
  1035. if($admin_id>0){
  1036. $admin_info = DB::table('admin')->select('realname', 'team_id')->where('id', $admin_id)->first();
  1037. $order['admin_id'] = $admin_id;
  1038. $order['admin_name'] = $admin_info->realname;
  1039. $order['team_id'] = $admin_info->team_id;
  1040. }
  1041. $id = (int)$request->input('id');
  1042. //获取订单原信息
  1043. $old_order = DB::table('order')->where('id', $id)->first();
  1044. if($old_order->status<2){
  1045. $order['use_coupon'] = (int)$request->input('use_coupon');
  1046. $order['buyer_phone'] = $request->input('buyer_phone');
  1047. $order['use_b_gift'] = (int)$request->input('use_b_gift');
  1048. $m_gift_id = (int)$request->input('m_gift_id');
  1049. if($m_gift_id>0){
  1050. $order['use_m_gift'] = 1;
  1051. }
  1052. }
  1053. // ++++++充值卡验证余额逻辑 +++++++ //
  1054. if($order['payment_type'] == 4){
  1055. $deposit_phone = $request->input('deposit_phone');
  1056. if($old_order->payment_type != 4){
  1057. $customerInfo = CustomerInfo::where('phone', $deposit_phone)->first();
  1058. $balance = $customerInfo->balance;
  1059. if($balance < $order['receivedAmount']){
  1060. //余额不足
  1061. return redirect('/admin/order/index')->with('info', '充值卡余额不足');
  1062. }
  1063. }elseif($old_order->receivedAmount != $order['receivedAmount']){
  1064. $customerInfo = CustomerInfo::where('phone', $deposit_phone)->first();
  1065. $balance = $customerInfo->balance;
  1066. if( $balance < $order['receivedAmount']-$old_order->receivedAmount ){
  1067. //余额不足
  1068. return redirect('/admin/order/index')->with('info', '充值卡余额不足');
  1069. }
  1070. }
  1071. }
  1072. $today_date = date('Y-m-d');
  1073. //是否同步卖家
  1074. $mjFlag = 0;
  1075. //是否取消卖家
  1076. $delFlag = 0;
  1077. if($order['warehouse'] == 3 && $old_order->status >= 2 ){
  1078. if($old_order->orderCode>0){
  1079. $mjFlag = 2; //订单已存在卖家云,更新同步到卖家云
  1080. //判断是否到预发货时间
  1081. if($order['delivery_date']>$today_date){
  1082. //未到发货时间,取消卖家云订单
  1083. $delFlag = 1;
  1084. }
  1085. }
  1086. if( $old_order->warehouse != 3 && $order['delivery_date']<=$today_date){
  1087. //订单其他仓变c仓同步到卖家云
  1088. $mjFlag = 1;
  1089. }
  1090. //获取订单在卖家云信息
  1091. $mjOrder = $this->getMjOrder($id, $old_order->outerCode);
  1092. $orderSkus = [];
  1093. $orderSkus['eshopCode'] = $old_order->eshopCode;
  1094. $orderSkus['outerCode'] = $old_order->outerCode;
  1095. $orderSkus['status'] = $old_order->status;
  1096. $orderSkus['customerName'] = $order['customerName'];
  1097. $orderSkus['customerNickName'] = $order['customerName'];
  1098. $orderSkus['shippingType'] = $old_order->shippingType;
  1099. $orderSkus['expressCompanyCode'] = $old_order->expressCompanyCode;
  1100. $orderSkus['expressCompanyName'] = $old_order->expressCompanyName;
  1101. $orderSkus['receiverName'] = $order['receiverName'];
  1102. $orderSkus['receiverMobile'] = isset($order['receiverMobile']) ? $order['receiverMobile'] : $old_order->receiverMobile;
  1103. $orderSkus['receiverPhone'] = $orderSkus['receiverMobile'];
  1104. $orderSkus['receiverState'] = $order['receiverState'];
  1105. $orderSkus['receiverCity'] = $order['receiverCity'];
  1106. $orderSkus['receiverDistrict'] = $old_order->receiverDistrict;
  1107. $orderSkus['receiverStreet'] = $old_order->receiverStreet;
  1108. $orderSkus['receiverAddress'] = $order['receiverAddress'];
  1109. $orderSkus['buyerPostageFee'] = $old_order->buyerPostageFee;
  1110. $orderSkus['discountFee'] = $old_order->discountFee;
  1111. $orderSkus['adjustFee'] = $old_order->adjustFee;
  1112. $orderSkus['serviceFee'] = $old_order->serviceFee;
  1113. $orderSkus['paymentMethod'] = $old_order->paymentMethod;
  1114. $orderSkus['paymentType'] = $old_order->paymentType;
  1115. $orderSkus['paymentAccount'] = $old_order->paymentAccount;
  1116. $orderSkus['paymentTime'] = $old_order->paymentTime;
  1117. $orderSkus['receivedAmount'] = $order['receivedAmount'];
  1118. $orderSkus['buyerMemo'] = $order['buyerMemo'];
  1119. $orderSkus['sellerMemo'] = $order['sellerMemo'];
  1120. $orderSkus['createTime'] = $old_order->createTime;
  1121. $orderSkus['modifyTime'] = $old_order->modifyTime;
  1122. }
  1123. $wmj_change_warehouse = 0;
  1124. if($order['warehouse'] != 3 && $old_order->status >= 2 && $old_order->warehouse == 3){
  1125. //同步卖家云
  1126. $delFlag = 1;
  1127. }elseif($order['warehouse'] != 3 && $old_order->status ==1 && $old_order->warehouse == 3){
  1128. $wmj_change_warehouse = 1;
  1129. }elseif($order['warehouse'] == 3 && $old_order->status ==1 && $old_order->warehouse != 3){
  1130. $wmj_change_warehouse = 2;
  1131. }
  1132. DB::beginTransaction();
  1133. try{
  1134. $res = DB::table('order')->where('id', $id)->update($order);
  1135. /** 新逻辑 统计预占库存 1.更新前 **/
  1136. if($old_order->orderCode == null){
  1137. $old_skus = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
  1138. $old_skus = json_decode(json_encode($old_skus), true);
  1139. }
  1140. /** end **/
  1141. //商品信息
  1142. $skus = $request->input('skus');
  1143. $gnum = $request->input('gnum');
  1144. if(empty($skus)) $skus = [];
  1145. //先删除
  1146. $del_re = DB::table('order_goods_skus')->where('order_id', $id)->whereNull('gift_id')->whereNotIn('sku_id', $skus)->update(['is_del'=>1]);
  1147. if(!empty($skus)){
  1148. //再同步
  1149. foreach($skus as $k=>$sku_id){
  1150. $sku_info = GoodsSkus::select('goodsCode', 'price', 'propsName','is_weigh', 'is_white')->where('id', $sku_id)->first();
  1151. #新加逻辑,判断库存是否足够
  1152. #1.判断是否已存在该sku
  1153. if($sku_info->is_white != 1){
  1154. $old_order_goods_num = OrderGoodsSkus::where('order_id', $id)->where('sku_id', $sku_id)->where('is_del', 0)->sum('num');
  1155. if( $gnum[$k] > $old_order_goods_num ){
  1156. $quantity_redis = GoodsSkus::actualQuantityByRedis($sku_id);
  1157. $act_quantity = $quantity_redis['quantity'];
  1158. if($act_quantity < $gnum[$k] - $old_order_goods_num){
  1159. throw new Exception("库存不足");
  1160. }
  1161. }
  1162. }
  1163. $sync_re = OrderGoodsSkus::updateOrCreate(['order_id'=>$id, 'sku_id'=>$sku_id, 'goods_id'=>$sku_info->goodsCode], ['num'=>$gnum[$k], 'is_del'=>0, 'price'=>$sku_info->price]);
  1164. if( $mjFlag == 1 && empty($mjOrder) ){
  1165. $goods_info = Goods::where('id', $sku_info->goodsCode)->first();
  1166. $params = array();
  1167. $params['outerCode'] = $old_order->outerCode+$k+1; //外部平台中子订单编码
  1168. $params['outerParentCode'] = $old_order->outerCode; //外部平台中订单编码
  1169. $params['goodsCode'] = $sku_info->goodsCode;// strstr($goods->code,'-',true);
  1170. $params['skuCode'] = $sku_id;
  1171. #商品名称
  1172. $params['goodsName'] = $goods_info->name;
  1173. $params['propertiesName'] = $sku_info->is_weigh==1 ? $sku_info->propsName.'【按半斤称重】' : $sku_info->propsName;
  1174. $params['price'] = $sku_info->is_weigh==1 ? round($sku_info->price/2,2) : $sku_info->price;
  1175. $params['quantity'] = (int)($sku_info->is_weigh==1 ? $gnum[$k]*2 : $gnum[$k]);
  1176. $params['discountFee'] = 0;
  1177. $params['adjustFee'] = 0;
  1178. $params['refundStatus'] = '正常';
  1179. $orderSkus['skuList'][] = $params;
  1180. }
  1181. }
  1182. }
  1183. if($res){
  1184. if($mjFlag == 1){
  1185. //其他仓变C仓
  1186. if(empty($mjOrder)){
  1187. #新加逻辑 查询收件人所在城市天气情况并更新
  1188. Order::updateWeather($id);
  1189. $mj_in = $this->syncOrderToMj($id, $orderSkus);
  1190. if( $mj_in == false ){
  1191. throw new Exception("订单同步到卖家云失败");
  1192. }
  1193. }elseif($mjOrder['status'] == '已取消'){
  1194. //变更状态
  1195. $up = array();
  1196. $up['code'] = $old_order->orderCode;
  1197. $up['eshopCode'] = $old_order->eshopCode;
  1198. $up['outerCode'] = $old_order->outerCode;
  1199. $up['status'] = $old_order->status;
  1200. $mj_up = $this->updateOrderToMj($id, $up);
  1201. if( $mj_up == false ){
  1202. throw new Exception("订单同步到卖家云失败");
  1203. }
  1204. }
  1205. }elseif($mjFlag == 2){
  1206. //C仓 卖家云订单更新
  1207. $orderSkus['code'] = $old_order->orderCode;
  1208. $mj_up = $this->updateOrderToMj($id, $orderSkus);
  1209. if( $mj_up == false ){
  1210. throw new Exception("订单更新同步到卖家云失败");
  1211. }
  1212. }
  1213. if($delFlag == 1){
  1214. //判断是否在卖家云存在
  1215. $mjOrder = $this->getMjOrder($id, $old_order->outerCode);
  1216. if(!empty($mjOrder)){
  1217. $del = array();
  1218. $del['eshopCode'] = $old_order->eshopCode;
  1219. $del['outerCode'] = $old_order->outerCode;
  1220. $del['code'] = $old_order->orderCode;
  1221. $mj_del = Order::mjOrderDel($del);
  1222. if( $mj_del == false ){
  1223. throw new Exception("订单同步到卖家云失败");
  1224. }
  1225. if($order['warehouse'] == 3) {
  1226. //卖家云取消的订单也要加预占库存 ++ C仓
  1227. $sku_redis = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
  1228. $sku_redis = json_decode(json_encode($sku_redis), true);
  1229. Order::diffSkuToRedis('', $sku_redis);
  1230. #更新mj_status
  1231. OrderGoodsSkus::where('order_id', $id)->update(['mj_status' => 0]);
  1232. Order::where('id', $id)->update(['orderCode'=>null, 'outerCode'=>$old_order->outerCode. 'qx']);
  1233. }
  1234. }
  1235. }
  1236. #新加逻辑,若付款方式为充值卡/消费记录
  1237. if($order['payment_type'] == 4 ){
  1238. if($old_order->payment_type != 4 ){
  1239. $consum = new CustomerConsum();
  1240. $consum->phone = $deposit_phone;
  1241. $consum->order_id = $id;
  1242. $consum->money = $order['receivedAmount'];
  1243. $consum->save();
  1244. //更新余额
  1245. $customerInfo->balance = $customerInfo->balance - $order['receivedAmount'];
  1246. $customerInfo->save();
  1247. }elseif($old_order->receivedAmount != $order['receivedAmount']){
  1248. $consum = CustomerConsum::where('order_id', $id)->where('type', 0)->first();
  1249. $consum->money = $order['receivedAmount'];
  1250. $consum->save();
  1251. //更新余额
  1252. $add_amount = $order['receivedAmount'] - $old_order->receivedAmount; //新增消费额,有可能为负数
  1253. $customerInfo->balance = $customerInfo->balance - $add_amount;
  1254. $customerInfo->save();
  1255. }
  1256. }
  1257. #记录操作日志
  1258. $self_id = session('admin_id');
  1259. $self_name = session('real_name');
  1260. $context = "修改订单";
  1261. $type = 0;
  1262. $tables = 'order';
  1263. $data_id = $id;
  1264. Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
  1265. }
  1266. #新加逻辑,使用优惠券,赠礼
  1267. if($old_order->status<2){
  1268. $use_coupon = (int)$request->input('use_coupon');
  1269. $use_b_gift = (int)$request->input('use_b_gift');
  1270. if($use_coupon == 1 && $old_order->use_coupon != 1){
  1271. $coupon = DB::table('customer_coupons')->where('phone', $order['buyer_phone'])->where('is_use',0)->orderBy('id', 'desc')->first();
  1272. if(!empty($coupon)){
  1273. $cup = array();
  1274. $cup['is_use'] = 1;
  1275. $cup['use_time'] = date('Y-m-d H:i:s');
  1276. $cup['order_id'] = $id;
  1277. DB::table('customer_coupons')->where('id', $coupon->id)->update($cup);
  1278. }
  1279. }elseif($use_coupon == 0 && $old_order->use_coupon == 1){
  1280. DB::table('customer_coupons')->where('order_id', $id)->update(['is_use'=>0]);
  1281. }
  1282. #查询本单使用赠礼
  1283. $order_gifts = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $id)->where('gift_id','>',0)->lists('gift_id');
  1284. $order_m_gift_id = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 0)->pluck('id');
  1285. $order_b_gift_ids = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 1)->lists('id');
  1286. if($m_gift_id>0 && empty($order_m_gift_id) ){
  1287. $m_gift = DB::table('customer_month_gift')->where('id', $m_gift_id)->where('type', 0)->first();
  1288. $order_m_gift = array();
  1289. $m_sku = GoodsSkus::where('id', $m_gift->sku_id)->first();
  1290. $order_m_gift['sku_id'] = $m_gift->sku_id;
  1291. $order_m_gift['order_id'] = $id;
  1292. $order_m_gift['goods_id'] = $m_sku->goodsCode;
  1293. $order_m_gift['num'] = $m_gift->num;
  1294. $order_m_gift['price'] = $m_sku->price;
  1295. $order_m_gift['gift_id'] = $m_gift_id;
  1296. $order_gift_insert = DB::table('order_goods_skus')->insert($order_m_gift);
  1297. #记录领取
  1298. $receive_gift = array();
  1299. $receive_gift['phone'] = $order['buyer_phone'];
  1300. $receive_gift['dtime'] = date('Y-m-d');
  1301. $receive_gift['order_id'] = $id;
  1302. $receive_gift['gift_id'] = $m_gift_id;
  1303. $receive_gift['gift_type'] = 0;
  1304. DB::table('customer_gift_receives')->insert($receive_gift);
  1305. }elseif($m_gift_id>0 && $m_gift_id != $order_m_gift_id){
  1306. DB::table('order_goods_skus')->where('gift_id', $order_m_gift_id)->where('order_id', $id)->update(['is_del'=>1]);
  1307. DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_id', $order_m_gift_id)->update(['is_del'=>1]);
  1308. $m_gift = DB::table('customer_month_gift')->where('id', $m_gift_id)->where('type', 0)->first();
  1309. $order_m_gift = array();
  1310. $m_sku = GoodsSkus::where('id', $m_gift->sku_id)->first();
  1311. $order_m_gift['sku_id'] = $m_gift->sku_id;
  1312. $order_m_gift['order_id'] = $id;
  1313. $order_m_gift['goods_id'] = $m_sku->goodsCode;
  1314. $order_m_gift['num'] = $m_gift->num;
  1315. $order_m_gift['price'] = $m_sku->price;
  1316. $order_m_gift['gift_id'] = $m_gift_id;
  1317. $order_gift_insert = DB::table('order_goods_skus')->insert($order_m_gift);
  1318. #记录领取
  1319. $receive_gift = array();
  1320. $receive_gift['phone'] = $order['buyer_phone'];
  1321. $receive_gift['dtime'] = date('Y-m-d');
  1322. $receive_gift['order_id'] = $id;
  1323. $receive_gift['gift_id'] = $m_gift_id;
  1324. $receive_gift['gift_type'] = 0;
  1325. DB::table('customer_gift_receives')->insert($receive_gift);
  1326. }elseif($m_gift_id==0 && $order_m_gift_id>0){
  1327. DB::table('order_goods_skus')->where('gift_id', $order_m_gift_id)->where('order_id', $id)->update(['is_del'=>1]);
  1328. DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_id', $order_m_gift_id)->update(['is_del'=>1]);
  1329. }
  1330. $mdate = date('Y-m-01', strtotime($old_order->create_time));
  1331. if($use_b_gift>0 && empty($order_b_gift_ids) ){
  1332. $b_gifts = DB::table('customer_month_gift')->where('mtime', $mdate)->where('type', 1)->get();
  1333. $b_gifts = json_decode(json_encode($b_gifts),true);
  1334. foreach($b_gifts as $b_gift){
  1335. $order_b_gift = array();
  1336. $b_sku = GoodsSkus::where('id', $b_gift['sku_id'])->first();
  1337. $order_b_gift['sku_id'] = $b_gift['sku_id'];
  1338. $order_b_gift['order_id'] = $id;
  1339. $order_b_gift['goods_id'] = $b_sku->goodsCode;
  1340. $order_b_gift['num'] = $b_gift['num'];
  1341. $order_b_gift['price'] = $b_sku->price;
  1342. $order_b_gift['gift_id'] = $b_gift['id'];
  1343. $order_gift_insert = DB::table('order_goods_skus')->insert($order_b_gift);
  1344. }
  1345. #记录领取
  1346. $receive_gift = array();
  1347. $receive_gift['phone'] = $order['buyer_phone'];
  1348. $receive_gift['dtime'] = date('Y-m-d');
  1349. $receive_gift['order_id'] = $id;
  1350. $receive_gift['gift_type'] = 1;
  1351. DB::table('customer_gift_receives')->insert($receive_gift);
  1352. }elseif($use_b_gift==0 && !empty($order_b_gift_ids)){
  1353. DB::table('order_goods_skus')->whereIn('gift_id', $order_b_gift_ids)->where('order_id', $id)->update(['is_del'=>1]);
  1354. DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_type', 1)->update(['is_del'=>1]);
  1355. }
  1356. }
  1357. DB::commit();
  1358. /** 新逻辑 统计预占库存 2.更新后 **/
  1359. if($old_order->orderCode == null && $old_order->warehouse == 3 && $order['warehouse'] == 3 && $old_order->status >= 1){
  1360. $new_skus = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
  1361. $new_skus = json_decode(json_encode($new_skus), true);
  1362. Order::diffSkuToRedis($old_skus, $new_skus);
  1363. }elseif($wmj_change_warehouse == 1){
  1364. Order::diffSkuToRedis('', $old_skus, -1);
  1365. }elseif($wmj_change_warehouse == 2){
  1366. $new_skus = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
  1367. $new_skus = json_decode(json_encode($new_skus), true);
  1368. Order::diffSkuToRedis('', $new_skus );
  1369. }
  1370. /** end **/
  1371. }catch (Exception $e){
  1372. DB::rollback();
  1373. Log::errorLog($request,['error'=>$e->getMessage()],"order/update",0,'');
  1374. return redirect('/admin/order/index?'.$str_query)->with('info', $e->getMessage());
  1375. }
  1376. if(!empty($last_url)){
  1377. header('Location:'.$last_url);exit;
  1378. }
  1379. return redirect('/admin/order/index?'.$str_query)->with('info', '更新成功');
  1380. }
  1381. /**
  1382. * 分组管理-进行删除操作
  1383. * @param Request $request
  1384. * @return \Illuminate\Http\RedirectResponse
  1385. */
  1386. public function orderdelete($id)
  1387. {
  1388. $order = Order::find($id);
  1389. $order->is_del = 1;
  1390. DB::beginTransaction();
  1391. try{
  1392. if ($order ->save()){
  1393. //假如删除订单在卖家云,同步为取消状态
  1394. if($order->orderCode>0 && $order->warehouse==3){
  1395. $del = array();
  1396. $del['eshopCode'] = $order->eshopCode;
  1397. $del['outerCode'] = $order->outerCode;
  1398. $del['code'] = $order->orderCode;
  1399. $mj_del = Order::mjOrderDel($del);
  1400. if( $mj_del == false ){
  1401. throw new Exception("订单同步到卖家云失败");
  1402. }
  1403. }
  1404. # 新加去除预占库存
  1405. if($order->orderCode == null && $order->warehouse==3 && $order->status >= 1){
  1406. // +++++ 已删除,减去预占库存到redis
  1407. $sku_redis = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
  1408. $sku_redis = json_decode(json_encode($sku_redis),true);
  1409. Order::diffSkuToRedis('', $sku_redis, -1);
  1410. }
  1411. //假如删除订单为充值卡订单,返还余额
  1412. if($order->payment_type == 4){
  1413. //查询消费记录/卡号
  1414. $consum = CustomerConsum::where('order_id', $id)->where('is_del', 0)->first();
  1415. //查询需要返还消费钱数
  1416. $fan_money = CustomerConsum::where('order_id', $id)->where('is_del', 0)->sum('money');
  1417. //消费记录设为删除
  1418. CustomerConsum::where('order_id', $id)->update(['is_del'=>1]);
  1419. //余额返还
  1420. $cust = CustomerInfo::select('balance')->where('phone', $consum->phone)->first();
  1421. $balance = $cust->balance + $fan_money;
  1422. CustomerInfo::where('phone', $consum->phone)->update(['balance'=>$balance]);
  1423. }
  1424. #更新order_goods_skus状态
  1425. $mj_status = array();
  1426. $mj_status['mj_status'] = 2;
  1427. OrderGoodsSkus::where('order_id', $id)->where('is_del', 0)->update($mj_status);
  1428. //假如删除订单使用了优惠券,礼品
  1429. if($order->use_coupon == 1){
  1430. DB::table('customer_coupons')->where('order_id', $id)->update(['is_use'=>0]);
  1431. }
  1432. #查询本单使用赠礼
  1433. $order_gifts = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $id)->where('gift_id','>',0)->lists('gift_id');
  1434. if(!empty($order_gifts)){
  1435. $order_m_gift_id = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 0)->pluck('id');
  1436. $order_b_gift_ids = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 1)->lists('id');
  1437. if($order_m_gift_id>0){
  1438. DB::table('order_goods_skus')->where('gift_id', $order_m_gift_id)->where('order_id', $id)->update(['is_del'=>1]);
  1439. DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_id', $order_m_gift_id)->update(['is_del'=>1]);
  1440. }
  1441. if(!empty($order_b_gift_ids)){
  1442. DB::table('order_goods_skus')->whereIn('gift_id', $order_b_gift_ids)->where('order_id', $id)->update(['is_del'=>1]);
  1443. DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_type', 1)->update(['is_del'=>1]);
  1444. }
  1445. }
  1446. #记录操作日志
  1447. $self_id = session('admin_id');
  1448. $self_name = session('real_name');
  1449. $context = "删除订单";
  1450. $type = 0;
  1451. $tables = 'order';
  1452. $data_id = $id;
  1453. Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
  1454. }
  1455. DB::commit();
  1456. }catch (Exception $e){
  1457. Log::errorLog('',['error'=>$e->getMessage()],"order/orderdelete",0,['id'=>$id]);
  1458. DB::rollback();
  1459. }
  1460. // exit('<script>window.location.replace(document.referrer)</script>');
  1461. //return redirect('/admin/order/index')->with('info', '删除成功');
  1462. echo '<script>window.location.replace(document.referrer)</script>';die;
  1463. }
  1464. /**
  1465. * 设为退款状态操作
  1466. * @param Request $request
  1467. * @return \Illuminate\Http\RedirectResponse
  1468. */
  1469. public function set_refund(Request $request)
  1470. {
  1471. $id = (int)$request->input('refund_id');
  1472. $aftersale_fee = trim($request->input('refund_money'));
  1473. $aftersale_reason = trim($request->input('refund_reason'));
  1474. $aftersale_time = $request->input('refund_time');
  1475. if(empty($aftersale_fee) || empty($aftersale_time)){
  1476. // exit('<script>alert("必须填写退款金额和时间");history.back()</script>');
  1477. echo '<script>alert("必须填写退款金额和时间");history.back()</script>';die;
  1478. }
  1479. $order = Order::find($id);
  1480. $order->status = 4;
  1481. $order->aftersale_fee = $aftersale_fee;
  1482. $order->aftersale_reason = $aftersale_reason;
  1483. $order->aftersale_time = $aftersale_time;
  1484. DB::beginTransaction();
  1485. try{
  1486. if ($order ->save()){
  1487. //假如删除订单在卖家云,同步为取消状态
  1488. if($order->orderCode>0 && $order->warehouse==3){
  1489. $del = array();
  1490. $del['eshopCode'] = $order->eshopCode;
  1491. $del['outerCode'] = $order->outerCode;
  1492. $del['code'] = $order->orderCode;
  1493. $mj_del = Order::mjOrderDel($del);
  1494. if( $mj_del == false ){
  1495. throw new Exception("订单同步到卖家云失败");
  1496. }
  1497. }
  1498. # 新加去除预占库存
  1499. if($order->orderCode == null && $order->warehouse==3 && $order->status>=1){
  1500. // +++++ 已删除,减去预占库存到redis
  1501. $sku_redis = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
  1502. $sku_redis = json_decode(json_encode($sku_redis),true);
  1503. Order::diffSkuToRedis('', $sku_redis, -1);
  1504. }
  1505. //假如删除订单为充值卡订单,返还余额
  1506. if($order->payment_type == 4){
  1507. //查询消费记录/卡号
  1508. $consum = CustomerConsum::where('order_id', $id)->where('is_del', 0)->first();
  1509. //查询需要返还消费钱数
  1510. $fan_money = CustomerConsum::where('order_id', $id)->where('is_del', 0)->sum('money');
  1511. //消费记录设为删除
  1512. CustomerConsum::where('order_id', $id)->update(['is_del'=>1]);
  1513. //余额返还
  1514. $cust = CustomerInfo::select('balance')->where('phone', $consum->phone)->first();
  1515. $balance = $cust->balance + $fan_money;
  1516. CustomerInfo::where('phone', $consum->phone)->update(['balance'=>$balance]);
  1517. }
  1518. #更新order_goods_skus状态,退回库存
  1519. $mj_status = array();
  1520. $mj_status['mj_status'] = 2;
  1521. OrderGoodsSkus::where('order_id', $id)->where('is_del', 0)->update($mj_status);
  1522. //假如删除订单使用了优惠券,礼品
  1523. if($order->use_coupon == 1){
  1524. DB::table('customer_coupons')->where('order_id', $id)->update(['is_use'=>0]);
  1525. }
  1526. #查询本单使用赠礼
  1527. $order_gifts = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $id)->where('gift_id','>',0)->lists('gift_id');
  1528. if(!empty($order_gifts)){
  1529. $order_m_gift_id = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 0)->pluck('id');
  1530. $order_b_gift_ids = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 1)->lists('id');
  1531. if($order_m_gift_id>0){
  1532. DB::table('order_goods_skus')->where('gift_id', $order_m_gift_id)->where('order_id', $id)->update(['is_del'=>1]);
  1533. DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_id', $order_m_gift_id)->update(['is_del'=>1]);
  1534. }
  1535. if(!empty($order_b_gift_ids)){
  1536. DB::table('order_goods_skus')->whereIn('gift_id', $order_b_gift_ids)->where('order_id', $id)->update(['is_del'=>1]);
  1537. DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_type', 1)->update(['is_del'=>1]);
  1538. }
  1539. }
  1540. #记录操作日志
  1541. $self_id = session('admin_id');
  1542. $self_name = session('real_name');
  1543. $context = "订单设为退款";
  1544. $type = 0;
  1545. $tables = 'order';
  1546. $data_id = $id;
  1547. Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
  1548. }
  1549. DB::commit();
  1550. }catch (Exception $e){
  1551. Log::errorLog('',['error'=>$e->getMessage()],"order/set_refund",0,['id'=>$id]);
  1552. DB::rollback();
  1553. }
  1554. // exit('<script>window.location.replace(document.referrer)</script>');
  1555. //return redirect('/admin/order/index')->with('info', '删除成功');
  1556. echo '<script>window.location.replace(document.referrer)</script>';die;
  1557. }
  1558. public function setrefund($id){
  1559. $order = Order::find($id);
  1560. $order->order_status = 2;
  1561. if ($order ->save()){
  1562. // exit('0');
  1563. echo '0';die;
  1564. }
  1565. // exit('1');
  1566. echo '1';die;
  1567. }
  1568. public function setverify($id, Request $request){
  1569. $order = Order::find($id);
  1570. $old_status = $order->status;
  1571. $order->status = (int)$request->input('status');
  1572. if($order->status == 0){
  1573. $refuse_reason = trim($request->input('refuse_reason'));
  1574. if(!empty($refuse_reason)){
  1575. $order->refuse_reason = $refuse_reason;
  1576. }
  1577. }
  1578. if($order->status == 2 || $order->status == 0){
  1579. $order->verify_time = date('Y-m-d H:i:s'); //审核时间
  1580. }
  1581. //新加 提审 判断库存
  1582. if($old_status==0 && $order->status == 1 && $order->warehouse == 3){
  1583. $order_skus = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
  1584. foreach($order_skus as $order_sku){
  1585. $sku_id = $order_sku->sku_id;
  1586. $num = $order_sku->num;
  1587. $sku_info = GoodsSkus::where('id', $sku_id)->first();
  1588. if($sku_info->is_white != 1){
  1589. $quantity_redis = GoodsSkus::actualQuantityByRedis($sku_id);
  1590. $act_quantity = $quantity_redis['quantity'];
  1591. if($act_quantity < $num){
  1592. return redirect('/admin/order/index')->with('info', '库存不足');
  1593. }
  1594. }
  1595. }
  1596. }
  1597. //获取可变更状态
  1598. $enableStatus = $this->getEnableStatus($old_status, $order->warehouse);
  1599. if( !empty($enableStatus) ){
  1600. $statusArr = array_column($enableStatus, 'status');
  1601. if( !in_array($order->status, $statusArr) ){
  1602. return redirect('/admin/order/index')->with('info', '异常操作');
  1603. }
  1604. }else{
  1605. return redirect('/admin/order/index');
  1606. }
  1607. DB::beginTransaction();
  1608. try{
  1609. $res = $order ->save();
  1610. if($res){
  1611. #记录操作日志
  1612. $status_arr = array(
  1613. 0 => '已录入',
  1614. 1 => '待审核',
  1615. 2 => '已审核',
  1616. 3 => '已发货',
  1617. 4 => '已退款'
  1618. );
  1619. $self_id = session('admin_id');
  1620. $self_name = session('real_name');
  1621. $context = "订单状态变更:【".$status_arr[$old_status]."】变更为【". $status_arr[$order->status]."】({$old_status}->{$order->status})";
  1622. $type = 1; //状态变更
  1623. $tables = 'order';
  1624. $data_id = $id;
  1625. Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
  1626. #发提醒短信
  1627. //$this->sendRemind($self_id, $id, $order->status);
  1628. if( $old_status==0 && $order->status == 1 && $order->warehouse == 3){
  1629. # 提审 预占加 redis
  1630. $sku_redis = $order_skus;
  1631. $sku_redis = json_decode(json_encode($sku_redis),true);
  1632. Order::diffSkuToRedis('', $sku_redis);
  1633. }
  1634. if($order->status == 0 && $order->warehouse == 3){
  1635. # 驳回 减预占 redis
  1636. $sku_redis = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
  1637. $sku_redis = json_decode(json_encode($sku_redis),true);
  1638. Order::diffSkuToRedis('', $sku_redis, -1);
  1639. }
  1640. $today_date = date('Y-m-d');
  1641. #如果审核通过,c仓, 并且到预发货时间,同步卖家云
  1642. if($order->status == 2 && $order->warehouse == 3 && $order->delivery_date <= $today_date){
  1643. #新加逻辑 查询收件人所在城市天气情况并更新
  1644. Order::updateWeather($id);
  1645. $syncMj = $this->syncOrderToMj($id);
  1646. if( $syncMj == false ){
  1647. throw new Exception("订单同步到卖家云失败");
  1648. }
  1649. #同步到卖家云,更新order_goods_skus状态
  1650. $mj_status = array();
  1651. $mj_status['mj_status'] = 1;
  1652. OrderGoodsSkus::where('order_id', $id)->where('is_del', 0)->update($mj_status);
  1653. // +++++ 已到卖家,减去预占库存到redis
  1654. $sku_redis = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
  1655. $sku_redis = json_decode(json_encode($sku_redis),true);
  1656. Order::diffSkuToRedis('', $sku_redis, -1);
  1657. }
  1658. }
  1659. DB::commit();
  1660. }catch (Exception $e){
  1661. Log::errorLog($request,['error'=>$e->getMessage()],"order/setverify",0,['id'=>$id]);
  1662. DB::rollback();
  1663. }
  1664. // exit('<script>window.location.replace(document.referrer)</script>');
  1665. echo '<script>window.location.replace(document.referrer)</script>';die;
  1666. $stime = $request->input('stime');
  1667. $etime = $request->input('etime');
  1668. $receiverName = $request->input('receiverName');
  1669. $receiverMobile = $request->input('receiverMobile');
  1670. $admin_id = (int)$request->input('admin_id');
  1671. $page = (int)$request->input('page');
  1672. $str_query = 'page='.$page.'&admin_id='.$admin_id.'&stime='.$stime.'&etime='.$etime.'&receiverName='.$receiverName.'&receiverMobile='.$receiverMobile;
  1673. return redirect('/admin/order/index?'.$str_query)->with('info', '操作成功');
  1674. }
  1675. public function setverifymore(Request $request){
  1676. $ids = $request->get('id');
  1677. foreach($ids as $id){
  1678. $order = Order::find($id);
  1679. $order->status = 1;
  1680. $order ->save();
  1681. }
  1682. $stime = $request->input('stime');
  1683. $etime = $request->input('etime');
  1684. $receiverName = $request->input('receiverName');
  1685. $receiverMobile = $request->input('receiverMobile');
  1686. $admin_id = (int)$request->input('admin_id');
  1687. $page = (int)$request->input('page');
  1688. $str_query = 'page='.$page.'&admin_id='.$admin_id.'&stime='.$stime.'&etime='.$etime.'&receiverName='.$receiverName.'&receiverMobile='.$receiverMobile;
  1689. return redirect('/admin/order/index?'.$str_query)->with('info', '操作成功');
  1690. }
  1691. /**
  1692. * 分组管理-分组展示
  1693. * @param Request $request
  1694. * @return \Illuminate\Http\RedirectResponse
  1695. */
  1696. public function orderup($id)
  1697. {
  1698. //数据库-修改status字段值
  1699. $order = Order::find($id);
  1700. $order ->status = 1;
  1701. if ($order ->update()){
  1702. return redirect('/admin/order/index')->with('info', '启用成功');
  1703. }
  1704. }
  1705. /**
  1706. * 分组管理-取消展示
  1707. * @param Request $request
  1708. * @return \Illuminate\Http\RedirectResponse
  1709. */
  1710. public function orderdown($id)
  1711. {
  1712. //数据库-修改status字段值
  1713. $order = Order::find($id);
  1714. $order ->status = 0;
  1715. if ($order ->update()){
  1716. return redirect('/admin/order/index')->with('info', '禁用成功');
  1717. }
  1718. }
  1719. /**
  1720. * 分类取商品
  1721. */
  1722. public function categoods($category){
  1723. $goods_ids = DB::table('goods_category')->where('goods_category_name', $category)->lists('goods_id');
  1724. $goods = DB::table('goods_sku_list')->select('id', 'price', 'props_name', 'goods_id')->whereIn('goods_id', $goods_ids)->orderBy('goods_id', 'asc')->get();
  1725. foreach($goods as $item){
  1726. $gname = DB::table('goods_category')->select('goods_name')->where('goods_id', $item->goods_id)->first();
  1727. $item->goods_name = $gname->goods_name;
  1728. }
  1729. $goods = json_decode(json_encode($goods), true);
  1730. return json_encode($goods);
  1731. }
  1732. /**
  1733. * 操作员列表
  1734. */
  1735. public function teamAdmins($team_id){
  1736. //$saler_ids = DB::table('admin_role')->where('role_name', '销售')->lists('user_id');
  1737. $res = DB::table('admin')->select('id', 'realname', 'username')->where('team_id', $team_id)->where('id','>', 1)->get();
  1738. $res = json_decode(json_encode($res),true);
  1739. return json_encode($res);
  1740. }
  1741. /**
  1742. * 客户地址信息
  1743. */
  1744. public function getCustomer(Request $request){
  1745. $phone = $request->input('phone');
  1746. if(!preg_match('/^1\d{10}$/')){
  1747. return 0;
  1748. }
  1749. $res = DB::table('customers')->where('phone', $phone)->first();
  1750. if(isset($res->id)){
  1751. $result = json_decode(json_encode($res),true);
  1752. return json_encode($result);
  1753. }
  1754. }
  1755. public function order_export(Request $request){
  1756. $self_role = session('role_name');
  1757. if($self_role == '超级管理员' || $self_role == '团队主管' || $self_role == '管理员' || $self_role == 'A仓库管理员' || $self_role == 'B仓库管理员' || $self_role == '售后管理员' || $self_role == '分销管理员'){
  1758. $admin_id = $request->input('admin_id');
  1759. }else{
  1760. $admin_id = session('admin_id');
  1761. }
  1762. $is_saler = 0;
  1763. if($self_role == '销售' || $self_role == '分销销售'){
  1764. $is_saler = 1;
  1765. }
  1766. if($self_role == 'A仓库管理员'){
  1767. $warehouse = 1;
  1768. }elseif($self_role == 'B仓库管理员'){
  1769. $warehouse = 2;
  1770. }else{
  1771. $warehouse = 0;
  1772. }
  1773. $team_id = $request->input('team_id');
  1774. #只能看自己团队的
  1775. if($self_role != '超级管理员' && $self_role != '售后管理员'){
  1776. $self_id = session('admin_id');
  1777. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  1778. }
  1779. //假如有团队筛选,检索销售队员
  1780. $sale_ids = null;
  1781. if($team_id>0){
  1782. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  1783. }
  1784. $stime = $request->input('stime');
  1785. $etime = $request->input('etime');
  1786. $receiverName = $request->input('receiverName');
  1787. $receiverMobile = $request->input('receiverMobile');
  1788. $status = $request->input('status');
  1789. $id = (int)$request->input('id');
  1790. $warehouse = (int)$request->input('warehouse');
  1791. if($status === null){
  1792. $status = -1;
  1793. }
  1794. if($etime){
  1795. $etime = $etime. ' 23:59:59';
  1796. }
  1797. $result = Order::where(function($query) use($admin_id, $stime, $etime, $receiverName, $receiverMobile, $self_role, $sale_ids, $status, $id, $warehouse){
  1798. if($admin_id) $query->where('admin_id', $admin_id);
  1799. if($stime) $query->where('createTime', '>=', $stime);
  1800. if($etime) $query->where('createTime', '<=', $etime);
  1801. if($receiverName) $query->where('receiverName', 'like', $receiverName . '%');
  1802. if($receiverMobile) $query->where('receiverMobile', $receiverMobile);
  1803. if($self_role == '管理员' || $self_role == '分销管理员') $query->where('status','>',0);
  1804. if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  1805. if(($status>=0)) $query->where('status', $status);
  1806. if(($id>0)) $query->where('id', $id);
  1807. if(($warehouse>0)) $query->where('warehouse', $warehouse);
  1808. })->where('is_del',0)->orderBy('id', 'desc')->get();
  1809. $result = json_decode(json_encode($result),true);
  1810. $status_arr = array(
  1811. 0 => '已录入',
  1812. 1 => '待审核',
  1813. 2 => '已审核',
  1814. 3 => '已发货',
  1815. 4 => '已退款'
  1816. );
  1817. $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
  1818. foreach($result as $k=>&$v){
  1819. #加粉时间
  1820. $fanTime = DB::table('customers')->select('fanTime')->where('phone', $v['receiverMobile'])->first();
  1821. $v['fanTime'] = isset($fanTime->fanTime) ? $fanTime->fanTime : '';
  1822. $v['receiverMobile'] = substr($v['receiverMobile'], 0, 3).'****'.substr($v['receiverMobile'], 7);
  1823. $v['status_name'] = $status_arr[$v['status']];
  1824. $v['address'] = $v['receiverState'].$v['receiverCity'].$v['receiverDistrict'].$v['receiverStreet'].$v['receiverAddress'];
  1825. $v['warehouse'] = $this->warehouse[$v['warehouse']];
  1826. $v['is_fugou'] = $v['is_fugou'] == 1? '是':'否';
  1827. $v['is_refund'] = $v['is_refund'] == 1? '是':'否';
  1828. $v['payment_type'] = $payArr[$v['payment_type']];
  1829. }
  1830. if($is_saler == 0){
  1831. $indexKey = ['id','receiverName','receiverMobile','address','goods_note','delivery_date', 'logistics_id', 'cost','freight_cost','warehouse','receivedAmount', 'payment_type', 'createTime','fanTime','is_fugou','is_refund','refund_price','refund_note','admin_name','status_name','verify_time','buyerMemo','sellerMemo','send_note','aftersale_fee', 'aftersale_reason', 'aftersale_time'];
  1832. $title = ['订单编号', '收货人', '收货人手机号', '收货地址', '商品信息', '预发货时间', '物流单号', '货品成本', '运费成本', '发货仓库', '付款金额', '支付方式', '下单时间', '加粉时间', '是否复购', '是否退补单', '应该退补差价', '退补备注', '所属销售', '订单状态', '审核时间', '买家备注', '卖家备注', '发货备注', '售后费用', '售后原因', '售后时间'];
  1833. }else{
  1834. $indexKey = ['id','receiverName','receiverMobile','address','goods_note','delivery_date', 'logistics_id', 'warehouse','receivedAmount','payment_type','createTime','fanTime','is_fugou','is_refund','refund_price','refund_note','admin_name','status_name','verify_time','buyerMemo','sellerMemo','send_note','aftersale_fee', 'aftersale_reason'];
  1835. $title = ['订单编号', '收货人', '收货人手机号', '收货地址', '商品信息', '预发货时间', '物流单号', '发货仓库', '付款金额', '付款方式', '下单时间', '加粉时间', '是否复购', '是否退补单', '应该退补差价', '退补备注', '所属销售', '订单状态', '审核时间', '买家备注', '卖家备注', '发货备注', '售后费用', '售后原因'];
  1836. }
  1837. $filename = 'dingdan_'.date('Y-m-d_H').'.xlsx';
  1838. return $this->export_excel($result, $filename, $indexKey, $title);
  1839. }
  1840. public function editLogisticsId(Request $request){
  1841. $id = (int)$request->input('id');
  1842. $logistics_id = $request->input('logistics_id');
  1843. $send_time = $request->input('send_time');
  1844. $cost = $request->input('cost');
  1845. $freight_cost = $request->input('freight_cost');
  1846. $send_note = $request->input('send_note');
  1847. $refund_note = $request->input('refund_note');
  1848. $buyerMemo = $request->input('buyerMemo');
  1849. $sellerMemo = $request->input('sellerMemo');
  1850. $aftersale_fee = $request->input('aftersale_fee');
  1851. $aftersale_reason = $request->input('aftersale_reason');
  1852. $aftersale_time = $request->input('aftersale_time');
  1853. $refund_time = $request->input('refund_time');
  1854. $refund_price = $request->input('refund_price');
  1855. $refund_type = $request->input('refund_type');
  1856. $order = Order::where('id', $id)->first();
  1857. $data = array();
  1858. if(isset($logistics_id)){
  1859. $data['logistics_id'] = $logistics_id;
  1860. if($logistics_id && !$order->send_time){
  1861. //填物流单号默认发货,记录发货时间 //更新的时候不变动
  1862. $data['send_time'] = date('Y-m-d');
  1863. }
  1864. }
  1865. if(isset($send_time)) $data['send_time'] = trim($send_time);
  1866. if(isset($cost)){
  1867. if(empty($cost)) $cost = null;
  1868. $data['cost'] = $cost;
  1869. }
  1870. if(isset($freight_cost)){
  1871. if(empty($freight_cost)) $freight_cost = null;
  1872. $data['freight_cost'] = $freight_cost;
  1873. }
  1874. if(isset($buyerMemo)) $data['buyerMemo'] = trim($buyerMemo);
  1875. if(isset($sellerMemo)) $data['sellerMemo'] = trim($sellerMemo);
  1876. if(isset($send_note)) $data['send_note'] = trim($send_note);
  1877. if(isset($refund_note)) $data['refund_note'] = trim($refund_note);
  1878. if(isset($aftersale_fee)) $data['aftersale_fee'] = trim($aftersale_fee);
  1879. if(isset($aftersale_reason)) $data['aftersale_reason'] = trim($aftersale_reason);
  1880. if(isset($aftersale_time)) $data['aftersale_time'] = trim($aftersale_time);
  1881. if(isset($refund_time)) $data['refund_time'] = trim($refund_time);
  1882. if(isset($refund_price)){
  1883. if(empty($refund_price)){
  1884. $refund_price = null;
  1885. $data['is_refund'] = 0;
  1886. }else{
  1887. $data['is_refund'] = 1;
  1888. if(!$order->refund_type) $data['refund_type'] = 1;
  1889. if(!$order->refund_time) $data['refund_time'] = date('Y-m-d');
  1890. if($order->refund_type == 4 && $refund_price != $order->refund_price){
  1891. // 充值退补
  1892. $res = $this->depositRefund($id, $refund_price);
  1893. if(!$res){
  1894. return 0;
  1895. }
  1896. }
  1897. }
  1898. $data['refund_price'] = $refund_price;
  1899. }
  1900. if(isset($refund_type)){
  1901. if(empty($refund_type)){
  1902. $refund_type = null;
  1903. }elseif($refund_type == 4){
  1904. //判断是否是充值卡支付方式
  1905. if($order->payment_type != 4){
  1906. return 0;
  1907. }
  1908. if($order->refund_price != 0 && $order->refund_type != 4){
  1909. $res = $this->depositRefund($id, $order->refund_price);
  1910. if(!$res){
  1911. return 0;
  1912. }
  1913. }
  1914. }elseif($refund_type != 4 && $order->refund_type == 4){
  1915. return 0;
  1916. }
  1917. $data['refund_type'] = (int)$refund_type;
  1918. if(!$order->refund_time) $data['refund_time'] = date('Y-m-d');
  1919. }
  1920. if($id>0 && !empty($data)){
  1921. $res = Order::where('id', $id)->update($data);
  1922. }
  1923. return 1;
  1924. }
  1925. /**
  1926. * 充值卡退补
  1927. */
  1928. public function depositRefund($order_id, $refund_price){
  1929. //查消费记录
  1930. $order_consum = DB::table('customer_consum')->where('is_del', 0)->where('order_id', $order_id)->where('type', 0)->first();
  1931. if(empty($order_consum)){
  1932. return false;
  1933. }
  1934. //查看是否已有退补记录
  1935. $order_refund = DB::table('customer_consum')->where('is_del', 0)->where('order_id', $order_id)->where('type', 1)->first();
  1936. $balance = CustomerInfo::where('phone', $order_consum->phone)->pluck('balance');
  1937. if(empty($order_refund)){
  1938. if($refund_price>0){
  1939. //补钱,判断充值卡余额
  1940. if($balance - $refund_price < 0){
  1941. return false;
  1942. }
  1943. }
  1944. $consum = array();
  1945. $consum['phone'] = $order_consum->phone;
  1946. $consum['order_id'] = $order_id;
  1947. $consum['money'] = $refund_price;
  1948. $consum['type'] = 1;
  1949. DB::table('customer_consum')->insert($consum);
  1950. $balance = $balance - $refund_price;
  1951. }elseif($order_refund->money != $refund_price){
  1952. //已有退款,更变le 退款钱数
  1953. if($balance - $refund_price + $order_refund->money <0 ){
  1954. return false;
  1955. }
  1956. DB::table('customer_consum')->where('id', $order_refund->id)->update(['money'=>$refund_price]);
  1957. $balance = $balance - $refund_price + $order_refund->money;
  1958. }
  1959. CustomerInfo::where('phone', $order_consum->phone)->update(['balance' => $balance]);
  1960. return true;
  1961. }
  1962. public function warehouse_export(Request $request){
  1963. $self_role = session('role_name');
  1964. $admin_id = $request->input('admin_id');
  1965. if($self_role == 'A仓库管理员'){
  1966. $warehouse = 1;
  1967. }elseif($self_role == 'B仓库管理员'){
  1968. $warehouse = 2;
  1969. }else{
  1970. $warehouse = 0;
  1971. }
  1972. $stime = $request->input('stime');
  1973. $etime = $request->input('etime');
  1974. $receiverName = $request->input('receiverName');
  1975. $receiverMobile = $request->input('receiverMobile');
  1976. $delivery_type = (int)$request->input('delivery_type'); //发货状态 0到发货时间(未填物流信息) 2全部
  1977. $id = (int)$request->input('id');
  1978. $_start = '2019-09-18';// 只看18号之后数据
  1979. $today_date = date('Y-m-d');
  1980. $result = Order::where(function($query) use($admin_id, $stime, $etime, $warehouse, $receiverMobile, $receiverName, $today_date, $delivery_type, $_start, $id){
  1981. if($admin_id) $query->where('admin_id', $admin_id);
  1982. if($warehouse) $query->where('warehouse', $warehouse);
  1983. if($stime) $query->where('createTime', '>=', $stime);
  1984. if($etime) $query->where('createTime', '<=', $etime);
  1985. if($receiverName) $query->where('receiverName', 'like', $receiverName . '%');
  1986. if($receiverMobile) $query->where('receiverMobile', $receiverMobile);
  1987. if($id>0) $query->where('id', $id);
  1988. if($delivery_type==0) $query->where('logistics_id', '=', '');
  1989. $query->where('createTime', '>=', $_start);
  1990. })->where('is_del',0)->where('status', '>', 1)->where('delivery_date', '<=', $today_date)->orderBy('id', 'desc')->get();
  1991. $result = json_decode(json_encode($result),true);
  1992. foreach ($result as $k => &$v)
  1993. {
  1994. $v['address'] = $v['receiverState'].$v['receiverCity'].$v['receiverDistrict'].$v['receiverStreet'].$v['receiverAddress'];
  1995. }
  1996. $indexKey = ['id','goods_note','receiverName','receiverMobile','address','createTime','sellerMemo','buyerMemo','cost','logistics_id','freight_cost','send_note'];
  1997. $title = ['订单编号', '商品信息', '收货人', '收货人手机号', '收货地址', '下单时间', '卖家备注', '买家备注', '供应商成本', '顺丰单号', '运费成本', '发货备注'];
  1998. $filename = 'dingdan_'.date('Y-m-d_H').'.xlsx';
  1999. return $this->export_excel($result, $filename, $indexKey, $title);
  2000. }
  2001. /**
  2002. * excel导入订单信息
  2003. * @param Request $request
  2004. */
  2005. public function importGoodsExcel(Request $request) {
  2006. $excelFile = $request->file('orderFile');
  2007. //实例化Excel读取类
  2008. $objReader = new PHPExcel_Reader_Excel2007();
  2009. if(!$objReader->canRead($excelFile)){
  2010. $objReader = new PHPExcel_Reader_Excel5();
  2011. if(!$objReader->canRead($excelFile)){
  2012. echo "\n".'无法识别的Excel文件!';
  2013. return false;
  2014. }
  2015. }
  2016. $objPHPExcel=$objReader->load($excelFile);
  2017. $worksheet=$objPHPExcel->getSheet(0);//获取第一个工作表
  2018. $highestRow=$worksheet->getHighestRow();//取得总行数
  2019. $highestColumn=$worksheet->getHighestColumn(); //取得总列数
  2020. $lines = $highestRow - 1;
  2021. if ($lines <= 0) {
  2022. //'Excel表格中没有数据';
  2023. return false;
  2024. }
  2025. $errorArr=[];
  2026. $okStr = '';
  2027. for ($row = 2; $row <= $highestRow; ++$row) {
  2028. $params = array();
  2029. $order_id = (int)$worksheet->getCellByColumnAndRow(0, $row)->getValue();
  2030. $cost = trim($worksheet->getCellByColumnAndRow(8, $row)->getValue());
  2031. $logistics_id = trim($worksheet->getCellByColumnAndRow(9, $row)->getValue());
  2032. $freight_cost = trim($worksheet->getCellByColumnAndRow(10, $row)->getValue());
  2033. $send_note = trim($worksheet->getCellByColumnAndRow(11, $row)->getValue());
  2034. if($cost>0){
  2035. $params['cost'] = $cost;
  2036. }else{
  2037. $params['cost'] = null;
  2038. }
  2039. if($logistics_id){
  2040. $params['logistics_id'] = $logistics_id;
  2041. }else{
  2042. $params['logistics_id'] = '';
  2043. }
  2044. if($send_note){
  2045. $params['send_note'] = $send_note;
  2046. }else{
  2047. $params['send_note'] = '';
  2048. }
  2049. if($freight_cost>0){
  2050. $params['freight_cost'] = $freight_cost;
  2051. }else{
  2052. $params['freight_cost'] = null;
  2053. }
  2054. $result = Order::where('id', $order_id)->update($params);
  2055. if (!$result) {
  2056. $errorArr[] = $order_id;
  2057. }else{
  2058. $okStr .= $order_id.',';
  2059. }
  2060. }
  2061. if (count($errorArr)) {
  2062. $desc = '共'.($highestRow-1).'个订单, 其中'.count($errorArr).'个订单录入失败';
  2063. }else {
  2064. $desc = '订单导入成功';
  2065. }
  2066. #记录操作日志
  2067. $self_id = session('admin_id');
  2068. $self_name = session('real_name');
  2069. $context = "仓管excel导入订单成本等信息 ".$desc;
  2070. $type = 0;
  2071. $tables = 'order';
  2072. $data_id = $okStr;
  2073. Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
  2074. return redirect('admin/order/warehouse')->with('info',$desc);
  2075. }
  2076. /**
  2077. * excel导入订单运费信息
  2078. * @param Request $request
  2079. */
  2080. public function importDeliveryExcel(Request $request) {
  2081. $excelFile = $request->file('orderFile');
  2082. //实例化Excel读取类
  2083. $objReader = new PHPExcel_Reader_Excel2007();
  2084. if(!$objReader->canRead($excelFile)){
  2085. $objReader = new PHPExcel_Reader_Excel5();
  2086. if(!$objReader->canRead($excelFile)){
  2087. return redirect('admin/order/warehouse')->with('info','无法识别的Excel文件!');
  2088. }
  2089. }
  2090. $objPHPExcel=$objReader->load($excelFile);
  2091. $worksheet=$objPHPExcel->getSheet(0);//获取第一个工作表
  2092. $highestRow=$worksheet->getHighestRow();//取得总行数
  2093. $highestColumn=$worksheet->getHighestColumn(); //取得总列数
  2094. $lines = $highestRow - 1;
  2095. if ($lines <= 0) {
  2096. //'Excel表格中没有数据';
  2097. return false;
  2098. }
  2099. $errorArr=[];
  2100. $okStr = '';
  2101. for ($row = 2; $row <= $highestRow; ++$row) {
  2102. $logistics_id = trim($worksheet->getCellByColumnAndRow(1, $row)->getValue());
  2103. $freight_cost = trim($worksheet->getCellByColumnAndRow(13, $row)->getValue());
  2104. //var_dump($logistics_id,$freight_cost);exit;
  2105. if($freight_cost>0){
  2106. $params['freight_cost'] = $freight_cost;
  2107. $result = Order::where('logistics_id', $logistics_id)->update($params);
  2108. if (!$result) {
  2109. $errorArr[] = $logistics_id;
  2110. }else{
  2111. $okStr .= $logistics_id.',';
  2112. }
  2113. }
  2114. }
  2115. if (count($errorArr)) {
  2116. $desc = '共'.($highestRow-1).'个物流单, 其中'.($highestRow-1-count($errorArr)).'个物流单导入成功';
  2117. }else {
  2118. $desc = '订单导入成功';
  2119. }
  2120. #记录操作日志
  2121. $self_id = session('admin_id');
  2122. $self_name = session('real_name');
  2123. $context = "excel导入订单物流成本 ".$desc;
  2124. $type = 0;
  2125. $tables = 'order';
  2126. $data_id = $okStr;
  2127. Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
  2128. return redirect('admin/order/warehouse')->with('info',$desc);
  2129. }
  2130. /**
  2131. * 导出excel
  2132. * @param $data
  2133. * @param string
  2134. */
  2135. public function export_excel($data, $filename = '未命名.xlsx', $indexKey, $title) {
  2136. if( !is_array($indexKey)) return false;
  2137. $header_arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE');
  2138. //初始化PHPExcel()
  2139. $objPHPExcel = new PHPExcel();
  2140. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  2141. //接下来就是写数据到表格里面去
  2142. $objActSheet = $objPHPExcel->getActiveSheet();
  2143. foreach($title as $k=>$item){
  2144. $objActSheet->setCellValue($header_arr[$k].'1',$item);
  2145. }
  2146. $startRow = 2;
  2147. foreach ($data as $row) {
  2148. foreach ($indexKey as $key => $value){
  2149. //这里是设置单元格的内容
  2150. $objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]);
  2151. }
  2152. $startRow++;
  2153. }
  2154. header("Pragma: public");
  2155. header("Expires: 0");
  2156. header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
  2157. header("Content-Type:application/force-download");
  2158. header("Content-Type:application/vnd.ms-execl");
  2159. header("Content-Type:application/octet-stream");
  2160. header("Content-Type:application/download");;
  2161. header('Content-Disposition:attachment;filename='.$filename.'');
  2162. header("Content-Transfer-Encoding:binary");
  2163. $objWriter->save('php://output');
  2164. exit();
  2165. }
  2166. /**
  2167. * 根据手机号获取默认地址信息
  2168. *
  2169. */
  2170. public function getAddress(Request $request){
  2171. $phone = $request->input('phone');
  2172. if(!preg_match('/^1\d{10}$/', $phone)){
  2173. echo '0';die;
  2174. }
  2175. $result = DB::table('customers')->select('receiverState', 'receiverCity', 'receiverDistrict', 'receiverStreet', 'receiverAddress', 'fanTime')->where('phone', $phone)->first();
  2176. if(empty($result)) { echo '0';die; }
  2177. $result = json_decode(json_encode($result), true);
  2178. echo json_encode($result);
  2179. }
  2180. /**
  2181. * 订单状态机制,获取可变更状态
  2182. * @param status:0已录入 1提交审核 2已审核 3已发货 4已退款
  2183. */
  2184. public function getEnableStatus($status, $warehouse=''){
  2185. $status_arr = [
  2186. 0 => ['status'=>0, 'name'=>'已录入'],
  2187. 1 => ['status'=>1, 'name'=>'提交审核'],
  2188. 2 => ['status'=>2, 'name'=>'审核通过'],
  2189. 3 => ['status'=>3, 'name'=>'设为发货'],
  2190. 4 => ['status'=>4, 'name'=>'设为退款']
  2191. ];
  2192. $status = (int)$status;
  2193. $result = array();
  2194. switch ($status) {
  2195. case '0':
  2196. $result[] = $status_arr[1];
  2197. break;
  2198. case '1':
  2199. $result[] = $status_arr[2];
  2200. $result[] = $status_arr[0];
  2201. break;
  2202. case '2':
  2203. $result[] = $status_arr[1];
  2204. if($warehouse != 3) $result[] = $status_arr[3];
  2205. break;
  2206. default:
  2207. break;
  2208. }
  2209. return $result;
  2210. }
  2211. /**
  2212. * 根据订单状态发短信提醒
  2213. */
  2214. public function sendRemind($user_id, $id, $status){
  2215. if($status == 0 || $status == 3){
  2216. //被驳回,获取销售手机号
  2217. $saler_id = DB::table('order')->where('id', $id)->pluck('admin_id');
  2218. $phone = DB::table('admin')->where('id, $saler_id')->pluck('phone');
  2219. $type = $status == 0 ? 5 : 3;
  2220. }elseif($status == 1){
  2221. //待审核,获取团队管理员手机号
  2222. $admin_ids = DB::table('admin_role')->where('role_name','管理员')->lists('user_id');
  2223. $team_id = DB::table('admin')->where('id', $user_id)->pluck('team_id');
  2224. $admin_info = DB::table('admin')->where('team_id', $team_id)->whereIn('id', $admin_ids)->first();
  2225. $phone = $admin_info->phone;
  2226. $type = 1;
  2227. }elseif($status == 2){
  2228. //审核通过,通知仓管发货
  2229. $warehouse = DB::table('order')->where('id', $id)->pluck('warehouse');
  2230. if($warehouse == 1){
  2231. $warehouse_admin = DB::table('admin_role')->select('user_id')->where('role_name','A仓库管理员')->orderBy('id', 'desc')->first();
  2232. $admin_info = DB::table('admin')->where('id', $warehouse_admin->user_id)->first();
  2233. $phone = $admin_info->phone;
  2234. $type = 2;
  2235. }
  2236. }
  2237. if(!$phone){
  2238. return false;
  2239. }
  2240. $res = YPSMS::sendMsg($phone,$type,$id);
  2241. return $res;
  2242. }
  2243. /**
  2244. * 订单同步到卖家云
  2245. */
  2246. public function syncOrderToMj($id, $order = []){
  2247. if( empty($order) ){
  2248. $order = Order::select('eshopCode','outerCode','status','customerName','shippingType','expressCompanyCode','expressCompanyName','receiverName','receiverMobile','receiverPhone','receiverState','receiverCity','receiverDistrict','receiverStreet','receiverAddress','buyerPostageFee','discountFee','adjustFee','serviceFee','paymentMethod','paymentType','paymentAccount','paymentTime','receivedAmount','buyerMemo','sellerMemo','createTime','modifyTime')->where('id', $id)->first();
  2249. $order = json_decode(json_encode($order), true);
  2250. $skus = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $id)->get();
  2251. $skus = json_decode(json_encode($skus), true);
  2252. foreach($skus as $k=>$v){
  2253. $sku_info = GoodsSkus::where('id', $v['sku_id'])->first();
  2254. $goods_info = Goods::where('id', $v['goods_id'])->first();
  2255. $params = array();
  2256. $params['outerCode'] = $order['outerCode']+$k+1; //外部平台中子订单编码
  2257. $params['outerParentCode'] = $order['outerCode']; //外部平台中订单编码
  2258. $params['goodsCode'] = $sku_info->goodsCode;//
  2259. $params['skuCode'] = $v['sku_id'];
  2260. #商品名称
  2261. $params['goodsName'] = $goods_info->name;
  2262. $params['propertiesName'] = $sku_info->is_weigh==1 ? $sku_info->propsName.'【按半斤称重】' : $sku_info->propsName;
  2263. //$params['goodsName'] = '海钓黄翅鱼';//$goods->code;
  2264. $params['price'] = $sku_info->is_weigh==1 ? round($v['price']/2,2) : $v['price'];
  2265. $params['quantity'] = (int)($sku_info->is_weigh==1 ? $v['num']*2 : $v['num']);
  2266. $params['discountFee'] = 0;
  2267. $params['adjustFee'] = 0;
  2268. $params['refundStatus'] = '正常';
  2269. $order['skuList'][] = $params;
  2270. }
  2271. }
  2272. //卖家云添加订单 ->改为审核通过再同步卖家云
  2273. $mj_add = Order::mjOrderAdd($order);
  2274. //同步卖家云订单数据到本系统
  2275. if( isset($mj_add['order']['code']) ){
  2276. $mjorder = array();
  2277. $mjorder['orderCode'] = $mj_add['order']['code'];
  2278. Order::where('id', $id)->update($mjorder);
  2279. }
  2280. return $mj_add;
  2281. }
  2282. /**
  2283. * 订单同步到卖家云
  2284. */
  2285. public function updateOrderToMj($id, $order = []){
  2286. if( empty($order) ){
  2287. $order = Order::select('orderCode as code','eshopCode','outerCode','status','customerName','shippingType','expressCompanyCode','expressCompanyName','receiverName','receiverMobile','receiverPhone','receiverState','receiverCity','receiverDistrict','receiverStreet','receiverAddress','buyerPostageFee','discountFee','adjustFee','serviceFee','paymentMethod','paymentType','paymentAccount','paymentTime','receivedAmount','buyerMemo','sellerMemo','createTime','modifyTime')->where('id', $id)->first();
  2288. $order = json_decode(json_encode($order), true);
  2289. }
  2290. //卖家云添加订单 ->改为审核通过再同步卖家云
  2291. $mj_add = Order::mjOrderUpdate($order);
  2292. return $mj_add;
  2293. }
  2294. /**
  2295. * 获取订单信息,可用来判断订单是否存在,同步订单状态
  2296. */
  2297. public function getMjOrder($id, $outerCode=''){
  2298. $order = array();
  2299. $order['eshopCode'] = config('constants.ESHOP_CODE');
  2300. if(!$outerCode){
  2301. $outerCode = Order::where('id', $id)->pluck('outerCode');
  2302. }
  2303. $order['outerCode'] = $outerCode;
  2304. $mjOrder = Order::mjOrderGet($order);
  2305. if(!isset($mjOrder['order']['code'])){
  2306. return false;
  2307. }
  2308. return $mjOrder['order'];
  2309. }
  2310. /**
  2311. * 获取手机号会员信息
  2312. */
  2313. public function getVip(Request $request){
  2314. $phone = $request->input('phone');
  2315. $time = date('Y-m-d H:i:s');
  2316. $customer_info = DB::table('customer_vip')->where('phone', $phone)->where('is_del', 0)->first();
  2317. if(!empty($customer_info)){
  2318. //判断是否过期
  2319. if($customer_info->vip_end_time > $time){
  2320. //未过期,1.获取优惠券,获取当月礼,获取生日礼,判断领取
  2321. $coupons = DB::table('customer_coupons')->where('phone', $phone)->where('is_use', 0)->where('end_time', '>', $time)->first();
  2322. $customer_info->coupons = !empty($coupons) ? 1:0;
  2323. //判断是否领取当月礼
  2324. $mtime = date('Y-m-01');
  2325. $if_m_gift = DB::table('customer_gift_receives')->where('phone', $phone)->where('is_del',0)->where('gift_type', 0)->where('dtime', '>=', $mtime)->first();
  2326. $customer_info->if_m_gift = 0;
  2327. if(empty($if_m_gift)){
  2328. $customer_info->if_m_gift = 1;
  2329. }
  2330. //判断生日礼
  2331. $birth_time = date('m', strtotime($customer_info->birthday));
  2332. $tom = date('m');
  2333. $customer_info->if_b_gift = 0;
  2334. if($tom == $birth_time){
  2335. //生日期内,判断是否已领
  2336. $if_b_gift = DB::table('customer_gift_receives')->where('phone', $phone)->where('is_del',0)->where('gift_type', 1)->where('dtime', '>=', $mtime)->first();
  2337. if(empty($if_b_gift)){
  2338. $customer_info->if_b_gift = 1;
  2339. }
  2340. }
  2341. // exit(json_encode(['code'=>0, 'data'=>$customer_info]));
  2342. echo json_encode(['code'=>0, 'data'=>$customer_info]);die;
  2343. }
  2344. // exit(json_encode(['code'=>1,'msg'=>'会员过期']));
  2345. echo json_encode(['code'=>1,'msg'=>'会员过期']);die;
  2346. }
  2347. echo json_encode(['code'=>1,'msg'=>'不是会员']);die;
  2348. }
  2349. /**
  2350. * 获取订单商品成本等信息
  2351. */
  2352. public function order_goods_info(Request $request){
  2353. $id = $request->input('id');
  2354. $is_fx = env('IS_FX');
  2355. $data = OrderGoodsSkus::where('order_id', $id)->where('is_del', 0)->get();
  2356. if(empty($data)) { echo '0';die; }
  2357. $total = 0;
  2358. foreach($data as $good){
  2359. $good->goods_name = Goods::where('id', $good->goods_id)->pluck('name');
  2360. $sku = GoodsSkus::select('code', 'propsName', 'referenceCost', 'outPrice')->where('id', $good->sku_id)->first();
  2361. $good->props_name = $sku->propsName;
  2362. $good->sku_code = $sku->code;
  2363. if($good->cost == null){
  2364. $good->cost = $is_fx == 0? $sku->referenceCost : $sku->outPrice;
  2365. }
  2366. $total += $good->cost * $good->num;
  2367. }
  2368. $total = round($total, 2);
  2369. $res = array();
  2370. $res['data'] = $data;
  2371. $res['total'] = $total;
  2372. echo json_encode($res);
  2373. }
  2374. /*分销退补售后列表*/
  2375. public function customerServiceAfterSaleList(Request $request) {
  2376. $order_id = $request->input('order_id');
  2377. $name = $request->input('receiverName');
  2378. $phone = $request->input('receiverMobile');
  2379. $page = $request->input('page',1);
  2380. $service_type = $request->input('service_type');
  2381. $pageSize = 20;
  2382. $offset = ($page - 1)*$pageSize;
  2383. //总数据条数
  2384. $count = DB::table('customer_service_after_sale_order')->where(function($query) use ($order_id, $name, $phone, $service_type) {
  2385. if($order_id) $query->where('order_id',$order_id);
  2386. if($name) $query->where('receiverName','like','%'.$name.'%');
  2387. if($phone) $query->where('receiverMobile','like','%'.$phone.'%');
  2388. if($service_type != 0) $query->where('service_type', $service_type);
  2389. })->count();
  2390. if ($count > 1) {
  2391. // 总页数
  2392. $pages = ceil($count/$pageSize);
  2393. }else{
  2394. // 总页数
  2395. $pages = 1;
  2396. }
  2397. $result = DB::table('customer_service_after_sale_order')->where(function($query) use ($order_id, $name, $phone, $service_type) {
  2398. if($order_id) $query->where('order_id',$order_id);
  2399. if($name) $query->where('receiverName','like','%'.$name.'%');
  2400. if($phone) $query->where('receiverMobile','like','%'.$phone.'%');
  2401. if($service_type != 0) $query->where('service_type', $service_type);
  2402. })->orderBy('id','desc')->offset($offset)->limit($pageSize)->get();
  2403. $result = json_decode(json_encode($result),true);
  2404. foreach ($result as &$value) {
  2405. $id = $value['order_id'];
  2406. $orderInfo = DB::table('order')->select('receiverAddress','createTime','receivedAmount','logistics_id',
  2407. 'payment_type','receiverState','receiverCity','receiverDistrict','receiverStreet','send_time')->where('id',$id)->where('is_del', 0)->first();
  2408. $value['receiverState'] = isset($orderInfo->receiverState) ? $orderInfo->receiverState : '';
  2409. $value['receiverCity'] = isset($orderInfo->receiverCity) ? $orderInfo->receiverCity : '';
  2410. $value['receiverDistrict'] = isset($orderInfo->receiverDistrict) ? $orderInfo->receiverDistrict : '';
  2411. $value['receiverStreet'] = isset($orderInfo->receiverStreet) ? $orderInfo->receiverStreet : '';
  2412. $value['receiverAddress'] = isset($orderInfo->receiverAddress) ? $orderInfo->receiverAddress : '';
  2413. $value['createTime'] = isset($orderInfo->createTime) ? $orderInfo->createTime : '';
  2414. $value['receivedAmount'] = isset($orderInfo->receivedAmount) ? $orderInfo->receivedAmount : '';
  2415. $value['logistics_id'] = isset($orderInfo->logistics_id) ? $orderInfo->logistics_id : '';
  2416. $value['payment_type'] = isset($orderInfo->payment_type) ? $orderInfo->payment_type : 1;
  2417. $value['send_time'] = isset($orderInfo->send_time) ? $orderInfo->send_time : '';
  2418. $value['receiverMobile'] = substr($value['receiverMobile'], 0, 3).'****'.substr($value['receiverMobile'], 7);
  2419. if($value['payment_type'] == 1){
  2420. $value['payment_type'] = '微信支付';
  2421. }elseif($value['payment_type'] == 2){
  2422. $value['payment_type'] = '付款码支付';
  2423. }elseif($value['payment_type'] == 3){
  2424. $value['payment_type'] = '个体户支付';
  2425. }elseif($value['payment_type'] == 4){
  2426. $value['payment_type'] = '充值卡支付';
  2427. }else{
  2428. $value['payment_type'] = '';
  2429. }
  2430. }
  2431. return view('order/customerServiceAfterSaleList',[
  2432. 'page' => $page,
  2433. 'pages' => $pages,
  2434. 'count' => $count,
  2435. 'result' => $result,
  2436. 'order_id' => $order_id,
  2437. 'receiverName' => $name,
  2438. 'receiverMobile' => $phone,
  2439. 'service_type' => $service_type,
  2440. ]);
  2441. }
  2442. /*分销退补售后添加页*/
  2443. public function customerServiceAfterSaleCreate(Request $request) {
  2444. $order_id = $request->input('order_id');
  2445. //查询该订单id是否已经存在
  2446. $log = DB::table('customer_service_after_sale_order')->where('order_id',$order_id)->first();
  2447. if($log) {
  2448. if($log->service_type == 1) {
  2449. $typeName = '退补';
  2450. } else {
  2451. $typeName = '售后';
  2452. }
  2453. return back()->with('info','该订单已'.$typeName);
  2454. }
  2455. //查询该订单id状态是否合法(满足售后退补条件)
  2456. $info = DB::table('order')->where('id',$order_id)->where('is_del', 0)->first();
  2457. if(!$info) {
  2458. return back()->with('info','订单不存在');
  2459. }
  2460. $order = (array) $info;
  2461. $self_role = session('role_name');
  2462. $order['deposit_phone'] = $order['receiverMobile'];
  2463. $order['receiverMobile'] = substr($order['receiverMobile'], 0, 3).'****'.substr($order['receiverMobile'], 7);
  2464. $order['goods'] = '';
  2465. $order_skus = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $order_id)->whereNull('gift_id')->lists('num','sku_id');
  2466. $skus = array_keys($order_skus);
  2467. if(!empty($skus)){
  2468. $goods = Goods::select('gs.id as sku_id', 'goods.id as goods_id', 'goods.name', 'gs.propsName as props_name', 'gs.price', 'gs.is_weigh', 'gs.is_white', 'gs.quantity')->leftJoin('goods_skus as gs', 'gs.goodsCode', '=', 'goods.id')->whereIn('gs.id', $skus)->orderBy('goods.id', 'desc')->get();
  2469. $goods = json_decode(json_encode($goods),true);
  2470. foreach($goods as $k=>&$v){
  2471. $v['num'] = $order_skus[$v['sku_id']];
  2472. //计算实际库存 -还未到卖家云的量
  2473. $quantity_redis = GoodsSkus::actualQuantityByRedis($v['sku_id']);
  2474. $v['quantity'] = $quantity_redis['quantity'];
  2475. $v['quantity'] = $v['quantity'] + $v['num'];
  2476. $v['quantity_str'] = $v['quantity'];
  2477. if($v['is_white'] == 1){
  2478. $v['quantity_str'] = '虚拟库存';
  2479. }
  2480. }
  2481. $order['goods'] = $goods;
  2482. }
  2483. $deposit_phone = $order['deposit_phone'];
  2484. if($order['payment_type'] == 4){
  2485. $consum = CustomerConsum::select('phone')->where('order_id', $order_id)->first();
  2486. $deposit_phone = isset($consum->phone) ? $consum->phone : $order['deposit_phone'];
  2487. }
  2488. //下单当月礼品
  2489. $mtime = date('Y-m-01', strtotime($order['create_time']));
  2490. $gift_info = DB::table('customer_month_gift')->where('mtime', $mtime)->get();
  2491. $gift_info = json_decode(json_encode($gift_info), true);
  2492. $m_gift=array();
  2493. $b_gift=array();
  2494. foreach($gift_info as $k=>$val){
  2495. //获取礼品信息
  2496. $goods_name = Goods::where('id', $val['goods_id'])->pluck('name');
  2497. $sku = GoodsSkus::where('id', $val['sku_id'])->where('is_del', 0)->where(function($query){
  2498. $query->where('is_white', 1)->orWhere('quantity','>',0);
  2499. })->first();
  2500. if(empty($sku)) continue;
  2501. //计算实际库存 -还未到卖家云的量
  2502. $quantity = GoodsSkus::actualQuantityByRedis($val['sku_id']);
  2503. $quantity = $quantity['quantity'];
  2504. if($quantity<=0){
  2505. continue;
  2506. }
  2507. $data = array();
  2508. $data['id'] = $val['id'];
  2509. $data['goods_name'] = $goods_name;
  2510. $data['propsName'] = $sku['propsName'];
  2511. $data['price'] = $sku['price'];
  2512. if($val['type'] == 0){
  2513. $m_gift[] = $data;
  2514. }else{
  2515. $b_gift[] = $data;
  2516. }
  2517. }
  2518. #获取赠品信息
  2519. $order_gifts = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $order_id)->where('gift_id','>',0)->lists('gift_id');
  2520. $order_m_gifts = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 0)->lists('id');
  2521. $order_b_gifts = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 1)->lists('id');
  2522. #查询用户会员信息
  2523. $cust_info = array();
  2524. $cust_info['coupons'] = 0;
  2525. $cust_info['if_m_gift'] = 0;
  2526. $cust_info['if_b_gift'] = 0;
  2527. $phone = $order['buyer_phone'];
  2528. $time = date('Y-m-d H:i:s');
  2529. $customer_info = DB::table('customer_vip')->where('phone', $phone)->where('is_del', 0)->first();
  2530. if(!empty($customer_info) && $order['create_time']>'2019-11-28'){
  2531. //判断是否过期
  2532. if($customer_info->vip_end_time > $time){
  2533. //未过期,1.获取优惠券,获取当月礼,获取生日礼,判断领取
  2534. $coupons = DB::table('customer_coupons')->where('phone', $phone)->where('is_use', 0)->where('end_time', '>', $time)->first();
  2535. $cust_info['coupons'] = !empty($coupons) ? 1:0;
  2536. //判断是否领取当月礼
  2537. $if_m_gift = DB::table('customer_gift_receives')->where('phone', $phone)->where('is_del',0)->where('gift_type', 0)->where('dtime', '>=', $mtime)->first();
  2538. if(empty($if_m_gift)){
  2539. $cust_info['if_m_gift'] = 1;
  2540. }
  2541. //判断生日礼
  2542. $birth_time = date('m', strtotime($customer_info->birthday));
  2543. $tom = date('m');
  2544. if($tom==$birth_time){
  2545. //生日期内,判断是否已领
  2546. $if_b_gift = DB::table('customer_gift_receives')->where('phone', $phone)->where('is_del',0)->where('gift_type', 1)->where('dtime', '>=', $mtime)->first();
  2547. if(empty($if_b_gift)){
  2548. $cust_info['if_b_gift'] = 1;
  2549. }
  2550. }
  2551. }
  2552. }
  2553. return view('order/customerServiceAfterSaleCreate',[
  2554. 'order' => $order,
  2555. 'deposit_phone' => $deposit_phone,
  2556. 'self_role' => $self_role,
  2557. 'm_gift' => $m_gift,
  2558. 'b_gift' => $b_gift,
  2559. 'order_m_gifts' => $order_m_gifts,
  2560. 'order_b_gifts' => $order_b_gifts,
  2561. 'cust_info' => $cust_info,
  2562. 'warehouseArr' => $this->warehouse,
  2563. ]);
  2564. }
  2565. /*分销退补售后添加*/
  2566. public function customerServiceAfterSaleStore(Request $request) {
  2567. $data['order_id'] = $request->input('order_id');
  2568. $data['service_type'] = $request->input('service_type');
  2569. $data['receiverMobile'] = $request->input('receiverMobile');
  2570. $data['receiverName'] = $request->input('receiverName');
  2571. //商品信息
  2572. $skus = $request->input('skus');
  2573. $gnum = $request->input('gnum');
  2574. $oldnums = $request->input('oldnums');
  2575. $goodsInfo = array();
  2576. if(!empty($skus)){
  2577. foreach($skus as $k=>$sku_id){
  2578. $sku_info = GoodsSkus::where('id', $sku_id)->first();
  2579. $goods_info = Goods::where('id', $sku_info->goodsCode)->first();
  2580. $params = array();
  2581. $params['goods_code'] = $sku_info->goodsCode;// strstr($goods->code,'-',true);
  2582. $params['sku_id'] = $sku_id;
  2583. #商品名称
  2584. $params['goods_name'] = $goods_info->name;
  2585. $params['properties_name'] = $sku_info->propsName;
  2586. //$params['goodsName'] = '海钓黄翅鱼';//$goods->code;
  2587. $params['price'] = $sku_info->price;
  2588. $params['sku_code'] = $sku_info->code;
  2589. $params['old_quantity'] = $oldnums[$k];
  2590. $params['new_quantity'] = $gnum[$k];
  2591. $goodsInfo[] = $params;
  2592. #新逻辑,c仓,判断实际库存足否
  2593. // if($order['warehouse'] == 3 && $sku_info->is_white != 1){
  2594. // $quantity_redis = GoodsSkus::actualQuantityByRedis($sku_id);
  2595. // $act_quantity = $quantity_redis['quantity'];
  2596. // if($act_quantity < $params['quantity']){
  2597. // exit('<script>alert("商品库存不足");history.back()</script>');
  2598. // //return redirect('/admin/order/index')->with('info', '商品库存不足');
  2599. // }
  2600. // }
  2601. }
  2602. }
  2603. $data['goods_info'] = json_encode($goodsInfo,JSON_UNESCAPED_UNICODE);
  2604. $res = DB::table('customer_service_after_sale_order')->insertGetId($data);
  2605. if($res > 0) {
  2606. #记录操作日志
  2607. $self_id = session('admin_id');
  2608. $self_name = session('real_name');
  2609. $context = "商品信息:".$data['goods_info'];
  2610. $type = 0; //订单录入
  2611. $tables = 'customer_service_after_sale_order';
  2612. $data_id = $res;
  2613. Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
  2614. return redirect('/admin/customerServiceAfterSale/index')->with('info','添加成功');
  2615. }
  2616. return back()->with('info','添加失败');
  2617. }
  2618. /*分销退补售后编辑页*/
  2619. public function customerServiceAfterSaleEdit(Request $request) {
  2620. $id = $request->input('id');
  2621. //查询订单信息
  2622. $customerServiceAfterSale = DB::table('customer_service_after_sale_order')->where('id',$id)->first();
  2623. $order_id = $customerServiceAfterSale->order_id;
  2624. $order = (array)DB::table('order')->where('id',$order_id)->where('is_del', 0)->first();
  2625. $goodsInfo = json_decode($customerServiceAfterSale->goods_info,true);
  2626. $order['service_type'] = $customerServiceAfterSale->service_type;
  2627. $self_role = session('role_name');
  2628. $order['deposit_phone'] = $order['receiverMobile'];
  2629. $order['receiverMobile'] = substr($order['receiverMobile'], 0, 3).'****'.substr($order['receiverMobile'], 7);
  2630. $order['goods'] = '';
  2631. $orderGoods = array();
  2632. foreach ($goodsInfo as $item) {
  2633. $orderGoods[$item['sku_id']] = $item;
  2634. }
  2635. $skus = array_column($goodsInfo,'sku_id');
  2636. if(!empty($skus)){
  2637. $goods = Goods::select('gs.id as sku_id', 'goods.id as goods_id', 'goods.name', 'gs.propsName as props_name', 'gs.price', 'gs.is_weigh', 'gs.is_white', 'gs.quantity')->leftJoin('goods_skus as gs', 'gs.goodsCode', '=', 'goods.id')->whereIn('gs.id', $skus)->orderBy('goods.id', 'desc')->get();
  2638. $goods = json_decode(json_encode($goods),true);
  2639. foreach($goods as $k=>&$v){
  2640. $v['num'] = $orderGoods[$v['sku_id']]['old_quantity'];
  2641. $v['new_num'] = $orderGoods[$v['sku_id']]['new_quantity'];
  2642. //计算实际库存 -还未到卖家云的量
  2643. $quantity_redis = GoodsSkus::actualQuantityByRedis($v['sku_id']);
  2644. $v['quantity'] = $quantity_redis['quantity'];
  2645. $v['quantity'] = $v['quantity'] + $v['num'];
  2646. $v['quantity_str'] = $v['quantity'];
  2647. if($v['is_white'] == 1){
  2648. $v['quantity_str'] = '虚拟库存';
  2649. }
  2650. }
  2651. $order['goods'] = $goods;
  2652. }
  2653. $deposit_phone = $order['deposit_phone'];
  2654. if($order['payment_type'] == 4){
  2655. $consum = CustomerConsum::select('phone')->where('order_id', $order_id)->first();
  2656. $deposit_phone = isset($consum->phone) ? $consum->phone : $order['deposit_phone'];
  2657. }
  2658. //下单当月礼品
  2659. $mtime = date('Y-m-01', strtotime($order['create_time']));
  2660. $gift_info = DB::table('customer_month_gift')->where('mtime', $mtime)->get();
  2661. $gift_info = json_decode(json_encode($gift_info), true);
  2662. $m_gift=array();
  2663. $b_gift=array();
  2664. foreach($gift_info as $k=>$val){
  2665. //获取礼品信息
  2666. $goods_name = Goods::where('id', $val['goods_id'])->pluck('name');
  2667. $sku = GoodsSkus::where('id', $val['sku_id'])->where('is_del', 0)->where(function($query){
  2668. $query->where('is_white', 1)->orWhere('quantity','>',0);
  2669. })->first();
  2670. if(empty($sku)) continue;
  2671. //计算实际库存 -还未到卖家云的量
  2672. $quantity = GoodsSkus::actualQuantityByRedis($val['sku_id']);
  2673. $quantity = $quantity['quantity'];
  2674. if($quantity<=0){
  2675. continue;
  2676. }
  2677. $data = array();
  2678. $data['id'] = $val['id'];
  2679. $data['goods_name'] = $goods_name;
  2680. $data['propsName'] = $sku['propsName'];
  2681. $data['price'] = $sku['price'];
  2682. if($val['type'] == 0){
  2683. $m_gift[] = $data;
  2684. }else{
  2685. $b_gift[] = $data;
  2686. }
  2687. }
  2688. #获取赠品信息
  2689. $order_gifts = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $order_id)->where('gift_id','>',0)->lists('gift_id');
  2690. $order_m_gifts = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 0)->lists('id');
  2691. $order_b_gifts = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 1)->lists('id');
  2692. #查询用户会员信息
  2693. $cust_info = array();
  2694. $cust_info['coupons'] = 0;
  2695. $cust_info['if_m_gift'] = 0;
  2696. $cust_info['if_b_gift'] = 0;
  2697. $phone = $order['buyer_phone'];
  2698. $time = date('Y-m-d H:i:s');
  2699. $customer_info = DB::table('customer_vip')->where('phone', $phone)->where('is_del', 0)->first();
  2700. if(!empty($customer_info) && $order['create_time']>'2019-11-28'){
  2701. //判断是否过期
  2702. if($customer_info->vip_end_time > $time){
  2703. //未过期,1.获取优惠券,获取当月礼,获取生日礼,判断领取
  2704. $coupons = DB::table('customer_coupons')->where('phone', $phone)->where('is_use', 0)->where('end_time', '>', $time)->first();
  2705. $cust_info['coupons'] = !empty($coupons) ? 1:0;
  2706. //判断是否领取当月礼
  2707. $if_m_gift = DB::table('customer_gift_receives')->where('phone', $phone)->where('is_del',0)->where('gift_type', 0)->where('dtime', '>=', $mtime)->first();
  2708. if(empty($if_m_gift)){
  2709. $cust_info['if_m_gift'] = 1;
  2710. }
  2711. //判断生日礼
  2712. $birth_time = date('m', strtotime($customer_info->birthday));
  2713. $tom = date('m');
  2714. if($tom==$birth_time){
  2715. //生日期内,判断是否已领
  2716. $if_b_gift = DB::table('customer_gift_receives')->where('phone', $phone)->where('is_del',0)->where('gift_type', 1)->where('dtime', '>=', $mtime)->first();
  2717. if(empty($if_b_gift)){
  2718. $cust_info['if_b_gift'] = 1;
  2719. }
  2720. }
  2721. }
  2722. }
  2723. return view('order/customerServiceAfterSaleEdit',[
  2724. 'order' => $order,
  2725. 'deposit_phone' => $deposit_phone,
  2726. 'self_role' => $self_role,
  2727. 'm_gift' => $m_gift,
  2728. 'b_gift' => $b_gift,
  2729. 'order_m_gifts' => $order_m_gifts,
  2730. 'order_b_gifts' => $order_b_gifts,
  2731. 'cust_info' => $cust_info,
  2732. 'id' => $id,
  2733. 'warehouseArr' => $this->warehouse,
  2734. ]);
  2735. }
  2736. /*分销退补售后编辑*/
  2737. public function customerServiceAfterSaleUpdate(Request $request) {
  2738. $data['service_type'] = $request->input('service_type');
  2739. $id = $request->input('id');
  2740. //商品信息
  2741. $skus = $request->input('skus');
  2742. $gnum = $request->input('gnum');
  2743. $oldnums = $request->input('oldnums');
  2744. $goodsInfo = array();
  2745. if(!empty($skus)){
  2746. foreach($skus as $k=>$sku_id){
  2747. $sku_info = GoodsSkus::where('id', $sku_id)->first();
  2748. $goods_info = Goods::where('id', $sku_info->goodsCode)->first();
  2749. $params = array();
  2750. $params['goods_code'] = $sku_info->goodsCode;// strstr($goods->code,'-',true);
  2751. $params['sku_id'] = $sku_id;
  2752. #商品名称
  2753. $params['goods_name'] = $goods_info->name;
  2754. $params['properties_name'] = $sku_info->propsName;
  2755. //$params['goodsName'] = '海钓黄翅鱼';//$goods->code;
  2756. $params['price'] = $sku_info->price;
  2757. $params['sku_code'] = $sku_info->code;
  2758. $params['old_quantity'] = $oldnums[$k];
  2759. $params['new_quantity'] = $gnum[$k];
  2760. $goodsInfo[] = $params;
  2761. #新逻辑,c仓,判断实际库存足否
  2762. // if($order['warehouse'] == 3 && $sku_info->is_white != 1){
  2763. // $quantity_redis = GoodsSkus::actualQuantityByRedis($sku_id);
  2764. // $act_quantity = $quantity_redis['quantity'];
  2765. // if($act_quantity < $params['quantity']){
  2766. // exit('<script>alert("商品库存不足");history.back()</script>');
  2767. // //return redirect('/admin/order/index')->with('info', '商品库存不足');
  2768. // }
  2769. // }
  2770. }
  2771. }
  2772. $data['goods_info'] = json_encode($goodsInfo,JSON_UNESCAPED_UNICODE);
  2773. $res = DB::table('customer_service_after_sale_order')->where('id',$id)->update($data);
  2774. if($res) {
  2775. #记录操作日志
  2776. $self_id = session('admin_id');
  2777. $self_name = session('real_name');
  2778. $context = "商品信息:".$data['goods_info'];
  2779. $type = 1; //订单录入
  2780. $tables = 'customer_service_after_sale_order';
  2781. $data_id = $id;
  2782. Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
  2783. return redirect('/admin/customerServiceAfterSale/index')->with('info','修改成功');
  2784. }
  2785. return back()->with('info','修改失败');
  2786. }
  2787. /**
  2788. * 获取订单商品成本等信息
  2789. */
  2790. public function orderGoodsInfo(Request $request){
  2791. $id = $request->input('id');
  2792. $is_fx = env('IS_FX');
  2793. $data = DB::table('customer_service_after_sale_order')->where('id', $id)->first();
  2794. if(empty($data)) { echo '0';die; }
  2795. $new_total = 0;
  2796. $old_total = 0;
  2797. $data = json_decode($data->goods_info,true);
  2798. foreach($data as &$good){
  2799. $sku = GoodsSkus::select('code', 'propsName', 'referenceCost', 'outPrice')->where('id', $good['sku_id'])->first();
  2800. $good['cost'] = $is_fx == 0? $sku->referenceCost : $sku->outPrice;
  2801. $new_total += $good['cost'] * $good['new_quantity'];
  2802. $old_total += $good['cost'] * $good['old_quantity'];
  2803. }
  2804. $new_total = round($new_total, 2);
  2805. $old_total = round($old_total,2);
  2806. $res = array();
  2807. $res['data'] = $data;
  2808. $res['new_total'] = $new_total;
  2809. $res['old_total'] = $old_total;
  2810. echo json_encode($res);
  2811. }
  2812. }