12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119 |
- <?php
- /**
- * Created by Sublime.
- * User: hao
- * Date: 19/08/30
- * Time: 上午11:20
- */
- namespace App\Http\Controllers\Admin;
- use App\Http\Controllers\Controller;
- use App\Log;
- use App\Order;
- use App\Oplog;
- use App\RedisModel;
- use App\Goods;
- use App\GoodsSkus;
- use App\OrderGoodsSkus;
- use App\CustomerInfo;
- use App\CustomerConsum;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use PHPExcel_Reader_Excel2007;
- use PHPExcel_Reader_Excel5;
- use PHPExcel;
- use PHPExcel_Writer_Excel2007;
- use YPSMS;
- class OrderController extends Controller
- {
- protected $warehouse = [];
- public function __construct()
- {
- $this->warehouse = config('constants.WAREHOUSE');
- }
- public function orderindex(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $self_role = session('role_name');
- if($self_role == '超级管理员' || $self_role == '团队主管' || $self_role == '管理员' || $self_role == '售后管理员' || $self_role == '分销管理员'){
- $admin_id = $request->input('admin_id');
- $search_admin = 1;
- }else{
- $admin_id = session('admin_id');
- $search_admin = 0;
- }
- $team_id = $request->input('team_id');
- #只能看自己团队的
- if($self_role != '超级管理员' && $self_role != '售后管理员'){
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $receiverName = trim($request->input('receiverName'));
- $receiverMobile = trim($request->input('receiverMobile'));
- $status = $request->input('status');
- $id = (int)$request->input('id');
- $warehouse = (int)$request->input('warehouse');
- if($status === null){
- $status = -1;
- }
- if($etime){
- $etime = $etime. ' 23:59:59';
- }
- $count = Order::where(function($query) use($admin_id, $stime, $etime, $receiverName, $receiverMobile, $self_role, $sale_ids, $status, $id, $warehouse){
- if($admin_id) $query->where('admin_id', $admin_id);
- if($stime) $query->where('createTime', '>=', $stime);
- if($etime) $query->where('createTime', '<=', $etime);
- if($receiverName) $query->where('receiverName', 'like', $receiverName . '%');
- if($receiverMobile) $query->where('receiverMobile', $receiverMobile);
- // if($self_role == '管理员' || $self_role == '分销管理员') $query->where('status','>',0);
- if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if(($status>=0)) $query->where('status', $status);
- if(($id>0)) $query->where('id', $id);
- if($warehouse>0) $query->where('warehouse', $warehouse);
- })->where('is_del',0)->count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = Order::where(function($query) use($admin_id, $stime, $etime, $receiverName, $receiverMobile, $self_role, $sale_ids, $status, $id, $warehouse){
- if($admin_id) $query->where('admin_id', $admin_id);
- if($stime) $query->where('createTime', '>=', $stime);
- if($etime) $query->where('createTime', '<=', $etime);
- if($receiverName) $query->where('receiverName', 'like', $receiverName . '%');
- if($receiverMobile) $query->where('receiverMobile', $receiverMobile);
- // if($self_role == '管理员' || $self_role == '分销管理员') $query->where('status','>',0);
- if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if(($status>=0)) $query->where('status', $status);
- if(($id>0)) $query->where('id', $id);
- if($warehouse>0) $query->where('warehouse', $warehouse);
- })->where('is_del',0)->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result),true);
-
- $status_arr = array(
- 0 => '已录入',
- 1 => '待审核',
- 2 => '已审核',
- 3 => '已发货',
- 4 => '已退款'
- );
- foreach($result as $k=>&$v){
- //毛利 40%下
- $v['pre_cost_flag'] = 0;
- //是否需要转仓
- $v['is_not_fx'] = 0;
- //获取商品信息
- $v['goods'] = '';
- $goods = DB::table('order_goods_skus')->where('order_id', $v['id'])->where('is_del', 0)->get();
- if(!empty($goods)){
- $v['pre_cost'] = 0;
- foreach($goods as $item){
- $goods_name = Goods::where('id', $item->goods_id)->pluck('name');
- $goods_sku_info = GoodsSkus::select('propsName', 'is_weigh', 'referenceCost', 'is_not_fx', 'warehouse')->where('id', $item->sku_id)->first();
- if(!empty($goods_sku_info)){
- $props_name = $goods_sku_info->propsName;
- $is_weigh = $goods_sku_info->is_weigh;
- $weigh_note = $is_weigh==1 ? '重量(斤)':'数量';
- if(!$item->gift_id) $v['pre_cost'] += $goods_sku_info->referenceCost * $item->num;
- //若有商品不属于C仓并且现在所属仓库是C仓,审核时提醒转仓
- if( $goods_sku_info->warehouse != 3 && $v['warehouse']==3 && $v['is_not_fx'] == 0 ){
- // if( $goods_sku_info->is_not_fx == 1 && $v['warehouse']==3 && $v['is_not_fx'] == 0 ){
- $v['is_not_fx'] = 1;
- }
- }else{
- $props_name = '';
- $weigh_note = '数量';
- }
- $goodsWarehouse = isset($this->warehouse[$goods_sku_info->warehouse]) ? $this->warehouse[$goods_sku_info->warehouse] : '';
-
- $v['goods'][] = $goods_name.'('.$props_name.') '.$weigh_note.':'.$item->num.' '.$goodsWarehouse;
- }
- $v['pre_cost'] = round($v['pre_cost'], 2);
- if( ($v['receivedAmount'] - $v['pre_cost']) / $v['receivedAmount'] < 0.4){
- $v['pre_cost_flag'] = 1;
- }
- }
-
- #加粉时间
- $fanTime = DB::table('customers')->select('fanTime')->where('phone', $v['receiverMobile'])->first();
- $v['fanTime'] = isset($fanTime->fanTime) ? $fanTime->fanTime : '';
- $v['receiverMobile'] = substr($v['receiverMobile'], 0, 3).'****'.substr($v['receiverMobile'], 7);
- #获取状态机
- if( ($self_role == '销售' || $self_role == '分销销售') && !in_array($v['status'], [0]) ){
- $v['enable_status'] = [];
- }else{
- $v['enable_status'] = $this->getEnableStatus($v['status'], $v['warehouse']);
- }
- $v['status_name'] = $status_arr[$v['status']];
- if($v['refund_type'] == 1){
- $v['refund_type'] = '微信支付';
- }elseif($v['refund_type'] == 2){
- $v['refund_type'] = '付款码支付';
- }elseif($v['refund_type'] == 3){
- $v['refund_type'] = '个体户支付';
- }elseif($v['refund_type'] == 4){
- $v['refund_type'] = '充值卡支付';
- }else{
- $v['refund_type'] = '';
- }
- if($v['payment_type'] == 1){
- $v['payment_type'] = '微信支付';
- }elseif($v['payment_type'] == 2){
- $v['payment_type'] = '付款码支付';
- }elseif($v['payment_type'] == 3){
- $v['payment_type'] = '个体户支付';
- }elseif($v['payment_type'] == 4){
- $v['payment_type'] = '充值卡支付';
- }else{
- $v['payment_type'] = '';
- }
- //计算毛利率
- }
- $yestoday = date('Y-m-d', strtotime('-1 day'));
- //获取超时未发订单
- $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){
- if($self_role == '管理员') $query->where('team_id', $team_id);
- })->lists('id');
-
- $overtime = [];
- if(!empty($order_overtime)){
- $overtime['list'] = json_decode(json_encode($order_overtime), true);
- $overtime['count'] = count($order_overtime);
- }
-
- #销售筛选
- //$saler_ids = DB::table('admin_role')->where('role_name', '销售')->lists('user_id');
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->where('id','>', 1)->where('is_use', 1)->get();
- $adminList = json_decode(json_encode($adminList), true);
- $is_fx = env('IS_FX');
- return view('order/orderlist', ['result' =>$result,
- 'page' => $page,
- 'count' => $count,
- 'pages' => $pages,
- 'admin_id' => $admin_id,
- 'team_id' => $team_id,
- 'stime' => $stime,
- 'etime' => $etime,
- 'adminlist' => $adminList,
- 'search_admin' => $search_admin,
- 'self_role' => $self_role,
- 'receiverName' => $receiverName,
- 'receiverMobile' => $receiverMobile,
- 'status' => $status,
- 'id' => $id,
- 'last_url' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],
- 'overtime' => $overtime,
- 'warehouse' => $warehouse,
- 'is_fx' => $is_fx,
- 'thisTime' => date('Y-m-d H:i:s'),
- 'warehouseArr' => $this->warehouse,
- ]);
- }
- /**
- * 仓库管理员订单列表
- */
- public function warehouse(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $admin_id = $request->input('admin_id');
- $search_admin = 1;
- $self_role = session('role_name');
- if($self_role == 'A仓库管理员'){
- $warehouse = 1;
- }elseif($self_role == 'B仓库管理员'){
- $warehouse = 2;
- }elseif($self_role == 'C仓管理员'){
- $warehouse = 3;
- }else{
- $warehouse = 0;
- }
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $delivery_type = (int)$request->input('delivery_type'); //发货状态 0到发货时间(未填物流信息) 2到发货时间全部
- $receiverName = trim($request->input('receiverName'));
- $receiverMobile = trim($request->input('receiverMobile'));
- $id = (int)$request->input('id');
- //仓库管理员只能看到 918之后的数据
- $_start = '2019-09-18';
- //仓库管理员只能看到已审核订单
- $today_date = date('Y-m-d');
- $count = Order::where(function($query) use($admin_id, $stime, $etime, $today_date, $delivery_type, $receiverName, $receiverMobile, $_start, $id){
- if($admin_id) $query->where('admin_id', $admin_id);
- if($stime) $query->where('createTime', '>=', $stime);
- if($etime) $query->where('createTime', '<=', $etime);
- if($delivery_type==0) $query->where('logistics_id', '=', '');
- if($receiverName) $query->where('receiverName', 'like', $receiverName . '%');
- if($receiverMobile) $query->where('receiverMobile', $receiverMobile);
- if($id>0) $query->where('id', $id);
- $query->where('createTime', '>=', $_start);
- })->where('is_del',0)->where('warehouse', $warehouse)->where('status', '>', 1)->where('delivery_date', '<=', $today_date)->count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = Order::where(function($query) use($admin_id, $stime, $etime, $today_date, $delivery_type, $receiverName, $receiverMobile, $_start, $id){
- if($admin_id) $query->where('admin_id', $admin_id);
- if($stime) $query->where('createTime', '>=', $stime);
- if($etime) $query->where('createTime', '<=', $etime);
- if($delivery_type==0) $query->where('logistics_id', '=', '');
- if($receiverName) $query->where('receiverName', 'like', $receiverName . '%');
- if($receiverMobile) $query->where('receiverMobile', $receiverMobile);
- $query->where('createTime', '>=', $_start);
- if($id>0) $query->where('id', $id);
- })->where('is_del',0)->where('warehouse', $warehouse)->where('status', '>', 1)->where('delivery_date', '<=', $today_date)->orderBy('verify_time', 'asc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result),true);
- $status_arr = array(
- 0 => '已录入',
- 1 => '待审核',
- 2 => '已审核',
- 3 => '已发货',
- 4 => '已退款',
- );
- foreach($result as $k=>&$v){
-
- //获取商品信息
- $v['goods'] = '';
- $goods = DB::table('order_goods_skus')->where('order_id', $v['id'])->where('is_del', 0)->get();
- if(!empty($goods)){
- foreach($goods as $item){
- $goods_name = Goods::where('id', $item->goods_id)->pluck('name');
- $props_name = GoodsSkus::where('id', $item->sku_id)->pluck('propsName');
- $v['goods'][] = $goods_name.'('.$props_name.') 数量/重量:'.$item->num.'';
- }
- }
- $v['status_name'] = $status_arr[$v['status']];
- }
- $yestoday = date('Y-m-d', strtotime('-1 day'));
- //获取超时未发订单
- $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');
-
- $overtime = [];
- if(!empty($order_overtime)){
- $overtime['list'] = json_decode(json_encode($order_overtime), true);
- $overtime['count'] = count($order_overtime);
- }
-
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('is_use', 1)->where('id','>', 1)->get();
- $adminList = json_decode(json_encode($adminList), true);
- return view('order/warehouselist', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'admin_id' =>$admin_id,
- 'stime' =>$stime,
- 'etime' =>$etime,
- 'delivery_type' =>$delivery_type,
- 'adminlist' =>$adminList,
- 'receiverName' =>$receiverName,
- 'receiverMobile' =>$receiverMobile,
- 'id' =>$id,
- 'today' =>$today_date,
- 'yestoday' => $yestoday,
- 'overtime' => $overtime,
- ]);
- }
- /**
- * 预售订单列表
- */
- public function predictOrder(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $self_role = session('role_name');
- if($self_role == '超级管理员' || $self_role == '团队主管' || $self_role == '管理员' || $self_role == '售后管理员'){
- $admin_id = $request->input('admin_id');
- $search_admin = 1;
- }else{
- $admin_id = session('admin_id');
- $search_admin = 0;
- }
- $team_id = $request->input('team_id');
- #只能看自己团队的
- if($self_role != '超级管理员' && $self_role != '售后管理员'){
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $receiverName = trim($request->input('receiverName'));
- $receiverMobile = trim($request->input('receiverMobile'));
- $status = $request->input('status');
- $if_all = (int)$request->input('if_all');
- if($status === null){
- $status = -1;
- }
- //预售逻辑 -> 预发货时间大于下单时间
- $today = date('Y-m-d');
- //DB::connection()->enableQueryLog();
- $count = Order::where(function($query) use($admin_id, $stime, $etime, $receiverName, $receiverMobile, $self_role, $sale_ids, $status, $if_all, $today){
- if($admin_id) $query->where('admin_id', $admin_id);
- if($stime) $query->where('createTime', '>=', $stime);
- if($etime) $query->where('createTime', '<=', $etime);
- if($receiverName) $query->where('receiverName', 'like', $receiverName . '%');
- if($receiverMobile) $query->where('receiverMobile', $receiverMobile);
- if($self_role == '管理员') $query->where('status','>',0);
- if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if(($status>=0)) $query->where('status', $status);
- if(!$if_all) $query->where('delivery_date','>=',$today);
- })->where('is_del',0)->whereRaw('delivery_date>createTime')->count();
- //print_r(DB::getQueryLog());exit;
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = Order::where(function($query) use($admin_id, $stime, $etime, $receiverName, $receiverMobile, $self_role, $sale_ids, $status, $if_all, $today){
- if($admin_id) $query->where('admin_id', $admin_id);
- if($stime) $query->where('createTime', '>=', $stime);
- if($etime) $query->where('createTime', '<=', $etime);
- if($receiverName) $query->where('receiverName', 'like', $receiverName . '%');
- if($receiverMobile) $query->where('receiverMobile', $receiverMobile);
- if($self_role == '管理员') $query->where('status','>',0);
- if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if(($status>=0)) $query->where('status', $status);
- if(!$if_all) $query->where('delivery_date','>=',$today);
- })->where('is_del',0)->whereRaw('delivery_date>createTime')->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result),true);
-
- $status_arr = array(
- 0 => '已录入',
- 1 => '待审核',
- 2 => '已审核',
- 3 => '已发货',
- 4 => '已退款'
- );
- foreach($result as $k=>&$v){
- //获取商品信息
- $v['goods'] = '';
- $goods = DB::table('order_goods_skus')->where('order_id', $v['id'])->where('is_del', 0)->get();
- if(!empty($goods)){
- foreach($goods as $item){
- $goods_name = Goods::where('id', $item->goods_id)->pluck('name');
- $props_name = GoodsSkus::where('id', $item->sku_id)->pluck('propsName');
- $v['goods'][] = $goods_name.'('.$props_name.') 数量/重量:'.$item->num.'';
- }
- }
- #加粉时间
- $fanTime = DB::table('customers')->select('fanTime')->where('phone', $v['receiverMobile'])->first();
- $v['fanTime'] = isset($fanTime->fanTime) ? $fanTime->fanTime : '';
- $v['receiverMobile'] = substr($v['receiverMobile'], 0, 3).'****'.substr($v['receiverMobile'], 7);
- #获取状态机
- if( ($self_role == '销售' || $self_role == '分销销售') && !in_array($v['status'], [0]) ){
- $v['enable_status'] = [];
- }else{
- $v['enable_status'] = $this->getEnableStatus($v['status']);
- }
- $v['status_name'] = $status_arr[$v['status']];
- }
-
- #销售筛选
- //$saler_ids = DB::table('admin_role')->where('role_name', '销售')->lists('user_id');
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->where('id','>', 1)->where('is_use', 1)->get();
- $adminList = json_decode(json_encode($adminList), true);
- return view('order/predictorderlist', ['result' =>$result,
- 'page' => $page,
- 'count' => $count,
- 'pages' => $pages,
- 'admin_id' => $admin_id,
- 'team_id' => $team_id,
- 'stime' => $stime,
- 'etime' => $etime,
- 'adminlist' => $adminList,
- 'search_admin' => $search_admin,
- 'self_role' => $self_role,
- 'receiverName' => $receiverName,
- 'receiverMobile' => $receiverMobile,
- 'status' => $status,
- 'if_all' => $if_all,
- 'last_url' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],
- 'warehouseArr' => $this->warehouse,
- ]);
- }
- /**
- * 添加订单
- * @return \Illuminate\View\View
- */
- public function ordercreate(Request $request)
- {
- $catelist = DB::table('goods_category')->select(DB::raw('distinct goods_category_name'))->lists('goods_category_name');
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('is_use', 1)->where('id','>', 1)->get();
- $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- $adminList = json_decode(json_encode($adminList), true);
- $self_role = session('role_name');
- $createTime = date('Y-m-d H:i:s');
- //当月礼品
- $mtime = date('Y-m-01');
- $gift_info = DB::table('customer_month_gift')->where('mtime', $mtime)->get();
- $gift_info = json_decode(json_encode($gift_info), true);
- $m_gift=array();
- $b_gift=array();
- foreach($gift_info as $k=>$v){
- //获取礼品信息
- $goods_name = Goods::where('id', $v['goods_id'])->pluck('name');
- $sku = GoodsSkus::where('id', $v['sku_id'])->where('is_del', 0)->where(function($query){
- $query->where('is_white', 1)->orWhere('quantity','>',0);
- })->first();
- if(empty($sku)) continue;
- //计算实际库存 -还未到卖家云的量
- $quantity = GoodsSkus::actualQuantityByRedis($v['sku_id']);
- $quantity = $quantity['quantity'];
- if($quantity<=0){
- continue;
- }
- $data = array();
- $data['id'] = $v['id'];
- $data['goods_name'] = $goods_name;
- $data['propsName'] = $sku['propsName'];
- $data['price'] = $sku['price'];
- $data['num'] = $v['num'];
- if($v['type'] == 0){
- $m_gift[] = $data;
- }else{
- $b_gift[] = $data;
- }
- }
- return view('order/ordercreate', [
- 'categorylist' => $catelist,
- 'adminlist' => $adminList,
- 'teamlist' => $teamList,
- 'self_role' => $self_role,
- 'createTime' => $createTime,
- 'm_gift' => $m_gift,
- 'b_gift' => $b_gift,
- 'warehouseArr' => $this->warehouse,
- ]);
- }
- /**
- * 分组管理-进行添加操作
- * @param Request $request
- * @return \Illuminate\Http\RedirectResponse
- */
- public function orderstore(Request $request)
- {
- $this->validate($request, [
- 'customerName' => 'required|between:1,6',
- 'receiverName' => 'required|between:1,6',
- 'receiverMobile' => 'required|regex:/^1[3456789]\d{9}$/',
- 'buyer_phone' => 'required|regex:/^1[3456789]\d{9}$/',
- 'fanTime' => 'required|date',
- 'receiverState' => 'required|between:1,5',
- 'receiverCity' => 'required|between:1,10',
- 'receiverAddress' => 'required',
- 'delivery_date' => 'required|date',
- //'createTime' => 'required|date',
- 'receivedAmount' => 'required|numeric|min:1',
- //'goods_note' => 'required|min:1',
- 'warehouse' => 'required|integer|min:1',
- //'skus' => 'required'
- ], [
- 'customerName.required' => '买家姓名不能为空',
- 'customerName.between' => '买家姓名不能超过6个字符',
- 'receiverName.required' => '收货人不能为空',
- 'receiverName.between' => '收货人不能超过6个字符',
- 'receiverMobile.required' => '收货人手机号不能为空',
- 'buyer_phone.required' => '买家手机号不能为空',
- 'receiverMobile.regex' => '收货人手机号格式有误',
- 'fanTime.required' => '加粉时间不能为空',
- 'receiverState.required' => '省不能为空',
- 'receiverState.between' => '省填写有误',
- 'receiverCity.required' => '市不能为空',
- 'receiverCity.between' => '市填写有误',
- 'receiverAddress.required' => '详细地址不能为空',
- 'delivery_date.required' => '预发货时间不能为空',
- //'createTime.required' => '下单时间不能为空',
- 'receivedAmount.required' => '付款金额不能为空',
- 'receivedAmount.numeric' => '付款金额必须为数字',
- 'goods_note.required' => '商品信息不能为空',
- 'warehouse.required' => '仓库必须选择',
- 'warehouse.min' => '仓库必须选择',
- ]);
- //redis防重复数据
- $phone = $request->input('receiverMobile');
-
- $redisKey = 'seafood.order_phone_add.'.$phone;
- $res = RedisModel::setnx($redisKey, 1);
- if( $res == 0 ){
- $msg = '操作过于频繁,请一分钟后继续操作';
- // exit('<script>alert("'.$msg.'");history.back()</script>');
- return back()->with('info', $msg);
- //return redirect('/admin/order/index')->with('info', $msg);
- }
- $res = RedisModel::expire( $redisKey, 60 );
-
- $skus = $request->input('skus');
- $warehouse = (int)$request->input('warehouse');
- if($warehouse == 3 && empty($skus)){
- $msg = 'C仓必须添加卖家云商品';
- // exit('<script>alert("'.$msg.'");history.back()</script>');
- //return redirect('/admin/order/index')->with('info', $msg);
- return back()->with('info', $msg);
- }
-
- //数据库-新增数据
- $order = array();
- $order['eshopCode'] = config('constants.ESHOP_CODE'); //默认
- $order['outerCode'] = Order::createOuterCode();//随机生成
- $order['status'] = (int)$request->input('status');
- $order['customerName'] = trim($request->input('customerName')); //买家名称
- $order['shippingType'] = '卖家包邮';
- $order['expressCompanyCode'] = '10084';
- $order['expressCompanyName'] = '顺丰快递';
- $order['receiverName'] = trim($request->input('receiverName')); //收件人
- $order['receiverMobile'] = trim($request->input('receiverMobile')); //收件人手机
- $order['receiverPhone'] = trim($request->input('receiverMobile')); //收件人手机
- $order['receiverState'] = trim($request->input('receiverState')); //省份
- $order['receiverCity'] = trim($request->input('receiverCity')); //市
- #省市格式处理
- // $order['receiverState'] = str_replace(['省', '市'], '', $order['receiverState']);
- // $order['receiverCity'] = str_replace('市', '', $order['receiverCity']);
- $order['receiverDistrict'] = trim($request->input('receiverDistrict')); //区
- $order['receiverStreet'] = trim($request->input('receiverStreet')); //街道
- $order['receiverAddress'] = trim($request->input('receiverAddress')); //详细地址
- $order['buyerPostageFee'] = 0; //买家邮费
- $order['discountFee'] = 0; //订单优惠
- $order['adjustFee'] = 0; //订单调价
- $order['serviceFee'] = 0; //服务费
- $order['paymentMethod'] = '在线付款';
- $order['paymentType'] = '网银';
- $order['paymentAccount'] = '18322225555'; // 付款账号,暂时默认写死
- $order['paymentTime'] = date('Y-m-d H:i:s'); // 付款时间
- $order['receivedAmount'] = trim($request->input('receivedAmount')); //付款金额
- $order['buyerMemo'] = trim($request->input('buyerMemo', '')); // 买家备注
- $order['sellerMemo'] = trim($request->input('sellerMemo', '')); // 卖家备注
- $order['createTime'] = date('Y-m-d H:i:s'); // 订单创建时间
- $order['modifyTime'] = date('Y-m-d H:i:s'); // 订单修改时间
- $order['should_amount'] = $request->input('should_amount');
- if(!$order['should_amount']) $order['should_amount'] = null;
- $order['warehouse'] = (int)$request->input('warehouse'); //仓库
- //商品信息
- $skus = $request->input('skus');
- $gnum = $request->input('gnum');
- if(!empty($skus)){
- foreach($skus as $k=>$sku_id){
- $sku_info = GoodsSkus::where('id', $sku_id)->first();
- $goods_info = Goods::where('id', $sku_info->goodsCode)->first();
- $params = array();
- $params['outerCode'] = $order['outerCode']+$k+1; //外部平台中子订单编码
- $params['outerParentCode'] = $order['outerCode']; //外部平台中订单编码
- $params['goodsCode'] = $sku_info->goodsCode;// strstr($goods->code,'-',true);
- $params['skuCode'] = $sku_id;
- #商品名称
- $params['goodsName'] = $goods_info->name;
- $params['propertiesName'] = $sku_info->propsName;
- //$params['goodsName'] = '海钓黄翅鱼';//$goods->code;
- $params['price'] = $sku_info->price;
-
- $params['quantity'] = $gnum[$k];
- $params['discountFee'] = 0;
- $params['adjustFee'] = 0;
- $params['refundStatus'] = '正常';
- $order['skuList'][] = $params;
- #新逻辑,c仓,判断实际库存足否
- if($order['warehouse'] == 3 && $sku_info->is_white != 1){
- $quantity_redis = GoodsSkus::actualQuantityByRedis($sku_id);
- $act_quantity = $quantity_redis['quantity'];
- if($act_quantity < $params['quantity']){
- // exit('<script>alert("商品库存不足");history.back()</script>');
- //return redirect('/admin/order/index')->with('info', '商品库存不足');
- // return back()->with('info', '商品库存不足');
- echo '<script>alert("商品库存不足");history.back()</script>';die;
- }
- }
- }
- }
- //卖家云添加订单 ->改为审核通过再同步卖家云
- //$mj_add = Order::mjOrderAdd($order);
- //本系统订单额外参数
- $order['goods_note'] = trim($request->input('goods_note'));
- if($request->input('cost')) $order['cost'] = trim($request->input('cost')); //成本
- $order['logistics_id'] = trim($request->input('logistics_id')); //物流单号
- $order['is_refund'] = (int)$request->input('is_refund'); //是否退过单
- if($request->input('refund_price')) $order['refund_price'] = trim($request->input('refund_price')); //应该退补差价
- $order['delivery_date'] = !empty($request->input('delivery_date')) ? $request->input('delivery_date') : date('Y-m-d'); //发货日期
- $order['order_status'] = (int)$request->input('order_status');
- $order['payment_type'] = (int)$request->input('payment_type'); //支付方式
- $order['buyer_phone'] = $request->input('buyer_phone');
- $order['use_coupon'] = (int)$request->input('use_coupon');
- $order['use_b_gift'] = (int)$request->input('use_b_gift');
- $m_gift_id = (int)$request->input('m_gift_id');
- if($m_gift_id>0){
- $order['use_m_gift'] = 1;
- }
-
- //充值卡验证余额
- if($order['payment_type'] == 4){
- $deposit_phone = $request->input('deposit_phone');
- $customerInfo = CustomerInfo::where('phone', $deposit_phone)->first();
- $balance = isset($customerInfo->balance) ? $customerInfo->balance : 0;
- if($balance < $order['receivedAmount']){
- //余额不足
- // exit('<script>alert("充值卡余额不足");history.back()</script>');
- //return redirect('/admin/order/index')->with('info', '充值卡余额不足');
- echo '<script>alert("充值卡余额不足");history.back()</script>';die;
- }
- }
- DB::beginTransaction();
- try{
- #更新客户表
- $customer = array();
- $customers['name'] = $order['receiverName'];
- $customers['receiverState'] = $order['receiverState'];
- $customers['receiverCity'] = $order['receiverCity'];
- $customers['receiverDistrict'] = $order['receiverDistrict'];
- $customers['receiverStreet'] = $order['receiverStreet'];
- $customers['receiverAddress'] = $order['receiverAddress'];
- $if_e = DB::table('customers')->select('id')->where('phone', $order['receiverMobile'])->first();
- if(isset($if_e->id)){
- $customers['is_fugou'] = 1;
- $order['is_fugou'] = 1;
- $up_c = DB::table('customers')->where('id', $if_e->id)->update($customers);
- }else{
- $customers['phone'] = $order['receiverMobile'];
- $customers['is_fugou'] = 0;
- $customers['fanTime'] = $request->input('fanTime');
- $customers['admin_id'] = session('admin_id');
- $in_c = DB::table('customers')->insert($customers);
- }
- if(!empty($skus)){
- $order_goods = $order['skuList'];
- unset($order['skuList']);
- }
-
- //$order['orderCode'] = $mj_add['order']['code'];
- $admin_id = (int)$request->input('admin_id');
- if($admin_id>0){
- $admin_info = DB::table('admin')->select('realname', 'team_id')->where('id', $admin_id)->first();
- $order['admin_id'] = $admin_id;
- $order['admin_name'] = $admin_info->realname;
- $order['team_id'] = $admin_info->team_id;
- }else{
- $order['admin_id'] = session('admin_id');
- $order['admin_name'] = session('real_name');
- $order['team_id'] = session('team_id');
- }
- //新加is_able 9.4之前粉丝不参与销售复购统计
- $fanTime = $request->input('fanTime');
- if($fanTime < '2019-09-04'){
- $order['is_able'] = 0;
- }
- $res = DB::table('order')->insertGetId($order);
- if($res){
- if(!empty($skus)){
- //订单商品插入
- $order_skus = array();
- foreach($order_goods as $k=>$sku){
- $order_skus[$k]['sku_id'] = $sku['skuCode'];
- $order_skus[$k]['order_id'] = $res;
- $order_skus[$k]['goods_id'] = $sku['goodsCode'];
- $order_skus[$k]['num'] = $sku['quantity'];
- $order_skus[$k]['price'] = $sku['price'];
- }
- $order_skus_insert = DB::table('order_goods_skus')->insert($order_skus);
- }
- #加赠品逻辑
- $use_coupon = (int)$request->input('use_coupon');
- $use_b_gift = (int)$request->input('use_b_gift');
- if($use_coupon == 1){
- $coupon = DB::table('customer_coupons')->where('phone', $order['buyer_phone'])->where('is_use',0)->orderBy('id', 'desc')->first();
- if(!empty($coupon)){
- $cup = array();
- $cup['is_use'] = 1;
- $cup['use_time'] = date('Y-m-d H:i:s');
- $cup['order_id'] = $res;
- DB::table('customer_coupons')->where('id', $coupon->id)->update($cup);
- }
- }
- if($m_gift_id>0){
- $m_gift = DB::table('customer_month_gift')->where('id', $m_gift_id)->where('type', 0)->first();
- $order_m_gift = array();
- $m_sku = GoodsSkus::where('id', $m_gift->sku_id)->first();
- $order_m_gift['sku_id'] = $m_gift->sku_id;
- $order_m_gift['order_id'] = $res;
- $order_m_gift['goods_id'] = $m_sku->goodsCode;
- $order_m_gift['num'] = $m_gift->num;
- $order_m_gift['price'] = $m_sku->price;
- $order_m_gift['gift_id'] = $m_gift_id;
- $order_gift_insert = DB::table('order_goods_skus')->insert($order_m_gift);
- #记录领取
- $receive_gift = array();
- $receive_gift['phone'] = $order['buyer_phone'];
- $receive_gift['dtime'] = date('Y-m-d');
- $receive_gift['order_id'] = $res;
- $receive_gift['gift_id'] = $m_gift_id;
- $receive_gift['gift_type'] = 0;
- DB::table('customer_gift_receives')->insert($receive_gift);
- }
- $mdate = date('Y-m-01');
- if($use_b_gift>0){
- $b_gifts = DB::table('customer_month_gift')->where('mtime', $mdate)->where('type', 1)->get();
- $b_gifts = json_decode(json_encode($b_gifts),true);
- foreach($b_gifts as $b_gift){
- $order_b_gift = array();
- $b_sku = GoodsSkus::where('id', $b_gift['sku_id'])->first();
- $order_b_gift['sku_id'] = $b_gift['sku_id'];
- $order_b_gift['order_id'] = $res;
- $order_b_gift['goods_id'] = $b_sku->goodsCode;
- $order_b_gift['num'] = $b_gift['num'];
- $order_b_gift['price'] = $b_sku->price;
- $order_b_gift['gift_id'] = $b_gift['id'];
- $order_gift_insert = DB::table('order_goods_skus')->insert($order_b_gift);
- }
- #记录领取
- $receive_gift = array();
- $receive_gift['phone'] = $order['buyer_phone'];
- $receive_gift['dtime'] = date('Y-m-d');
- $receive_gift['order_id'] = $res;
- $receive_gift['gift_type'] = 1;
- DB::table('customer_gift_receives')->insert($receive_gift);
- }
- #新加逻辑,若付款方式为充值卡/消费记录
- if($order['payment_type'] == 4){
- $consum = new CustomerConsum();
- $consum->phone = $deposit_phone;
- $consum->order_id = $res;
- $consum->money = $order['receivedAmount'];
- $consum->save();
- //更新余额
- $customerInfo->balance = $customerInfo->balance - $order['receivedAmount'];
- $customerInfo->save();
- }
- #记录操作日志
- $self_id = session('admin_id');
- $self_name = session('real_name');
- if($order['status'] == 0){
- $status_text = '仅录入';
- }else{
- $status_text = '录入并提审';
- }
- $context = "录入订单:".$status_text;
- $type = 0; //订单录入
- $tables = 'order';
- $data_id = $res;
- Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
- #如果提交审核,发短信
- //if( $order['status'] == 1 ) $this->sendRemind($self_id, $res, 1);
- }
-
- DB::commit();
- // +++++ 新加预占库存到redis
- if($order['warehouse'] == 3 && $order['status'] == 1){
- $sku_redis = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $res)->where('is_del', 0)->groupBy('sku_id')->get();
- $sku_redis = json_decode(json_encode($sku_redis),true);
- Order::diffSkuToRedis('', $sku_redis);
- }
-
- }catch (Exception $e){
- DB::rollback();
- }
- return redirect('/admin/order/index')->with('info', '添加成功');
-
- }
- /**
- * 分组管理-编辑分组界面
- * @param $id
- * @return \Illuminate\View\View
- */
- public function orderedit($id,Request $request)
- {
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $receiverName = $request->input('receiverName');
- $receiverMobile = $request->input('receiverMobile');
- $admin_id = (int)$request->input('admin_id');
- $page = (int)$request->input('page');
- $last_url = $request->input('last_url');
- $self_role = session('role_name');
- //$catelist = DB::table('goods_category')->select(DB::raw('distinct goods_category_name'))->lists('goods_category_name');
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->where('is_use', 1)->get();
- $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- $adminList = json_decode(json_encode($adminList), true);
- $order = Order::findOrFail($id);
- $order = json_decode(json_encode($order), true);
- // 临时处理,只能用自己充值卡
- $order['deposit_phone'] = $order['receiverMobile'];
- $order['receiverMobile'] = substr($order['receiverMobile'], 0, 3).'****'.substr($order['receiverMobile'], 7);
- $order['goods'] = '';
- $order_skus = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $id)->whereNull('gift_id')->lists('num','sku_id');
- $skus = array_keys($order_skus);
- if(!empty($skus)){
- $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();
- $goods = json_decode(json_encode($goods),true);
- foreach($goods as $k=>&$v){
- $v['num'] = $order_skus[$v['sku_id']];
- //计算实际库存 -还未到卖家云的量
- $quantity_redis = GoodsSkus::actualQuantityByRedis($v['sku_id']);
- $v['quantity'] = $quantity_redis['quantity'];
- $v['quantity'] = $v['quantity'] + $v['num'];
- $v['quantity_str'] = $v['quantity'];
- if($v['is_white'] == 1){
- $v['quantity_str'] = '虚拟库存';
- }
- }
- $order['goods'] = $goods;
- }
- $deposit_phone = $order['deposit_phone'];
- if($order['payment_type'] == 4){
- $consum = CustomerConsum::select('phone')->where('order_id', $id)->first();
- $deposit_phone = isset($consum->phone) ? $consum->phone : $order['deposit_phone'];
- }
- //下单当月礼品
- $mtime = date('Y-m-01', strtotime($order['create_time']));
- $gift_info = DB::table('customer_month_gift')->where('mtime', $mtime)->get();
- $gift_info = json_decode(json_encode($gift_info), true);
- $m_gift=array();
- $b_gift=array();
- foreach($gift_info as $k=>$val){
- //获取礼品信息
- $goods_name = Goods::where('id', $val['goods_id'])->pluck('name');
- $sku = GoodsSkus::where('id', $val['sku_id'])->where('is_del', 0)->where(function($query){
- $query->where('is_white', 1)->orWhere('quantity','>',0);
- })->first();
- if(empty($sku)) continue;
- //计算实际库存 -还未到卖家云的量
- $quantity = GoodsSkus::actualQuantityByRedis($val['sku_id']);
- $quantity = $quantity['quantity'];
- if($quantity<=0){
- continue;
- }
- $data = array();
- $data['id'] = $val['id'];
- $data['goods_name'] = $goods_name;
- $data['propsName'] = $sku['propsName'];
- $data['price'] = $sku['price'];
- if($val['type'] == 0){
- $m_gift[] = $data;
- }else{
- $b_gift[] = $data;
- }
- }
- #获取赠品信息
- $order_gifts = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $id)->where('gift_id','>',0)->lists('gift_id');
- $order_m_gifts = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 0)->lists('id');
- $order_b_gifts = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 1)->lists('id');
- #查询用户会员信息
- $cust_info = array();
- $cust_info['coupons'] = 0;
- $cust_info['if_m_gift'] = 0;
- $cust_info['if_b_gift'] = 0;
- $phone = $order['buyer_phone'];
- $time = date('Y-m-d H:i:s');
- $customer_info = DB::table('customer_vip')->where('phone', $phone)->where('is_del', 0)->first();
- if(!empty($customer_info) && $order['create_time']>'2019-11-28'){
- //判断是否过期
- if($customer_info->vip_end_time > $time){
- //未过期,1.获取优惠券,获取当月礼,获取生日礼,判断领取
- $coupons = DB::table('customer_coupons')->where('phone', $phone)->where('is_use', 0)->where('end_time', '>', $time)->first();
- $cust_info['coupons'] = !empty($coupons) ? 1:0;
- //判断是否领取当月礼
- $if_m_gift = DB::table('customer_gift_receives')->where('phone', $phone)->where('is_del',0)->where('gift_type', 0)->where('dtime', '>=', $mtime)->first();
- if(empty($if_m_gift)){
- $cust_info['if_m_gift'] = 1;
- }
-
- //判断生日礼
- $birth_time = date('m', strtotime($customer_info->birthday));
- $tom = date('m');
- if($tom==$birth_time){
- //生日期内,判断是否已领
- $if_b_gift = DB::table('customer_gift_receives')->where('phone', $phone)->where('is_del',0)->where('gift_type', 1)->where('dtime', '>=', $mtime)->first();
- if(empty($if_b_gift)){
- $cust_info['if_b_gift'] = 1;
- }
- }
- }
- }
- return view('order/orderedit', [
- 'order' => $order,
- //'categorylist' => $catelist,
- 'adminlist' => $adminList,
- 'teamlist' => $teamList,
- 'self_role' => $self_role,
- 'str_query' => 'page='.$page.'&admin_id='.$admin_id.'&stime='.$stime.'&etime='.$etime.'&receiverName='.$receiverName.'&receiverMobile='.$receiverMobile,
- 'last_url' => $last_url,
- 'deposit_phone' => $deposit_phone,
- 'm_gift' => $m_gift,
- 'b_gift' => $b_gift,
- 'order_m_gifts' => $order_m_gifts,
- 'order_b_gifts' => $order_b_gifts,
- 'cust_info' => $cust_info,
- 'warehouseArr' => $this->warehouse,
- ]);
- }
- /**
- * 分组管理-进行编辑操作
- * @param Request $request
- * @return \Illuminate\Http\RedirectResponse
- */
- public function orderupdate(Request $request)
- {
- $this->validate($request, [
- 'id' => 'required',
- 'customerName' => 'required|between:1,6',
- 'receiverName' => 'required|between:1,6',
- 'receiverMobile' => 'required|regex:/^1[3456789][\d\*]{9}$/',
- 'receiverState' => 'required|between:1,5',
- 'receiverCity' => 'required|between:1,10',
- 'receiverAddress' => 'required',
- 'delivery_date' => 'required|date',
- //'createTime' => 'required|date',
- 'receivedAmount' => 'required|numeric|min:1',
- //'goods_note' => 'required|min:1',
- 'warehouse' => 'required|integer|min:1',
- ], [
- 'customerName.required' => '买家姓名不能为空',
- 'customerName.between' => '买家姓名不能超过6个字符',
- 'receiverName.required' => '收货人不能为空',
- 'receiverName.between' => '收货人不能超过6个字符',
- 'receiverMobile.required' => '收货人手机号不能为空',
- 'receiverMobile.regex' => '收货人手机号格式有误',
- 'receiverState.required' => '省不能为空',
- 'receiverState.between' => '省填写有误',
- 'receiverCity.required' => '市不能为空',
- 'receiverCity.between' => '市填写有误',
- 'receiverAddress.required' => '详细地址不能为空',
- 'delivery_date.required' => '预发货时间不能为空',
- //'createTime.required' => '下单时间不能为空',
- 'receivedAmount.required' => '付款金额不能为空',
- 'receivedAmount.numeric' => '付款金额必须为数字',
- 'goods_note.required' => '商品信息不能为空',
- 'warehouse.required' => '仓库必须选择',
- 'warehouse.min' => '仓库必须选择',
- ]);
- $str_query = trim($request->input('str_query'));
- $last_url = trim($request->input('last_url'));
-
- $phone = trim($request->input('receiverMobile'));
- //更新数据
- $order = array();
- $order['customerName'] = trim($request->input('customerName')); //买家名称
- $order['receiverName'] = trim($request->input('receiverName')); //收件人
- if(preg_match('/^1\d{10}$/', $phone)) $order['receiverMobile'] = $phone; //收件人手机
- $order['receiverState'] = trim($request->input('receiverState')); //省份
- $order['receiverCity'] = trim($request->input('receiverCity')); //市
-
- // $order['receiverState'] = str_replace(['省', '市'], '', $order['receiverState']);
- // $order['receiverCity'] = str_replace('市', '', $order['receiverCity']);
- //$order['receiverDistrict'] = trim($request->input('receiverDistrict')); //区
- //$order['receiverStreet'] = trim($request->input('receiverStreet')); //街道
- $order['receiverAddress'] = trim($request->input('receiverAddress')); //详细地址
- $order['receivedAmount'] = trim($request->input('receivedAmount')); //付款金额
- $order['buyerMemo'] = trim($request->input('buyerMemo', '')); // 买家备注
- $order['sellerMemo'] = trim($request->input('sellerMemo', '')); // 卖家备注
- //$order['goods_note'] = trim($request->input('goods_note'));
- if($request->input('cost')) $order['cost'] = trim($request->input('cost')); //成本
- //$order['logistics_id'] = trim($request->input('logistics_id')); //物流单号
- $order['is_refund'] = (int)$request->input('is_refund'); //是否退过单
- if($request->input('refund_price')) $order['refund_price'] = trim($request->input('refund_price')); //应该退补差价
- //$order['is_fugou'] = (int)$request->input('is_fugou'); //是否复购
- $order['warehouse'] = (int)$request->input('warehouse'); //仓库
- $order['delivery_date'] = !empty($request->input('delivery_date')) ? $request->input('delivery_date') : date('Y-m-d'); //发货日期
- //if(!empty($request->input('createTime'))) $order['createTime'] = $request->input('createTime'); // 订单创建时间
- $order['order_status'] = (int)$request->input('order_status');
- $order['payment_type'] = (int)$request->input('payment_type'); //支付方式
- //$order['status'] = (int)$request->input('status');
- $order['should_amount'] = $request->input('should_amount');
- if(!$order['should_amount']) $order['should_amount'] = null;
- $admin_id = (int)$request->input('admin_id');
- if($admin_id>0){
- $admin_info = DB::table('admin')->select('realname', 'team_id')->where('id', $admin_id)->first();
- $order['admin_id'] = $admin_id;
- $order['admin_name'] = $admin_info->realname;
- $order['team_id'] = $admin_info->team_id;
- }
- $id = (int)$request->input('id');
- //获取订单原信息
- $old_order = DB::table('order')->where('id', $id)->first();
- if($old_order->status<2){
- $order['use_coupon'] = (int)$request->input('use_coupon');
- $order['buyer_phone'] = $request->input('buyer_phone');
- $order['use_b_gift'] = (int)$request->input('use_b_gift');
- $m_gift_id = (int)$request->input('m_gift_id');
- if($m_gift_id>0){
- $order['use_m_gift'] = 1;
- }
- }
- // ++++++充值卡验证余额逻辑 +++++++ //
- if($order['payment_type'] == 4){
- $deposit_phone = $request->input('deposit_phone');
- if($old_order->payment_type != 4){
- $customerInfo = CustomerInfo::where('phone', $deposit_phone)->first();
- $balance = $customerInfo->balance;
- if($balance < $order['receivedAmount']){
- //余额不足
- return redirect('/admin/order/index')->with('info', '充值卡余额不足');
- }
- }elseif($old_order->receivedAmount != $order['receivedAmount']){
- $customerInfo = CustomerInfo::where('phone', $deposit_phone)->first();
- $balance = $customerInfo->balance;
- if( $balance < $order['receivedAmount']-$old_order->receivedAmount ){
- //余额不足
- return redirect('/admin/order/index')->with('info', '充值卡余额不足');
- }
- }
- }
- $today_date = date('Y-m-d');
- //是否同步卖家
- $mjFlag = 0;
- //是否取消卖家
- $delFlag = 0;
- if($order['warehouse'] == 3 && $old_order->status >= 2 ){
- if($old_order->orderCode>0){
- $mjFlag = 2; //订单已存在卖家云,更新同步到卖家云
- //判断是否到预发货时间
- if($order['delivery_date']>$today_date){
- //未到发货时间,取消卖家云订单
- $delFlag = 1;
- }
- }
- if( $old_order->warehouse != 3 && $order['delivery_date']<=$today_date){
- //订单其他仓变c仓同步到卖家云
- $mjFlag = 1;
- }
- //获取订单在卖家云信息
- $mjOrder = $this->getMjOrder($id, $old_order->outerCode);
-
- $orderSkus = [];
- $orderSkus['eshopCode'] = $old_order->eshopCode;
- $orderSkus['outerCode'] = $old_order->outerCode;
- $orderSkus['status'] = $old_order->status;
- $orderSkus['customerName'] = $order['customerName'];
- $orderSkus['customerNickName'] = $order['customerName'];
- $orderSkus['shippingType'] = $old_order->shippingType;
- $orderSkus['expressCompanyCode'] = $old_order->expressCompanyCode;
- $orderSkus['expressCompanyName'] = $old_order->expressCompanyName;
- $orderSkus['receiverName'] = $order['receiverName'];
- $orderSkus['receiverMobile'] = isset($order['receiverMobile']) ? $order['receiverMobile'] : $old_order->receiverMobile;
- $orderSkus['receiverPhone'] = $orderSkus['receiverMobile'];
- $orderSkus['receiverState'] = $order['receiverState'];
- $orderSkus['receiverCity'] = $order['receiverCity'];
- $orderSkus['receiverDistrict'] = $old_order->receiverDistrict;
- $orderSkus['receiverStreet'] = $old_order->receiverStreet;
- $orderSkus['receiverAddress'] = $order['receiverAddress'];
- $orderSkus['buyerPostageFee'] = $old_order->buyerPostageFee;
- $orderSkus['discountFee'] = $old_order->discountFee;
- $orderSkus['adjustFee'] = $old_order->adjustFee;
- $orderSkus['serviceFee'] = $old_order->serviceFee;
- $orderSkus['paymentMethod'] = $old_order->paymentMethod;
- $orderSkus['paymentType'] = $old_order->paymentType;
- $orderSkus['paymentAccount'] = $old_order->paymentAccount;
- $orderSkus['paymentTime'] = $old_order->paymentTime;
- $orderSkus['receivedAmount'] = $order['receivedAmount'];
- $orderSkus['buyerMemo'] = $order['buyerMemo'];
- $orderSkus['sellerMemo'] = $order['sellerMemo'];
- $orderSkus['createTime'] = $old_order->createTime;
- $orderSkus['modifyTime'] = $old_order->modifyTime;
-
- }
- $wmj_change_warehouse = 0;
- if($order['warehouse'] != 3 && $old_order->status >= 2 && $old_order->warehouse == 3){
- //同步卖家云
- $delFlag = 1;
- }elseif($order['warehouse'] != 3 && $old_order->status ==1 && $old_order->warehouse == 3){
- $wmj_change_warehouse = 1;
- }elseif($order['warehouse'] == 3 && $old_order->status ==1 && $old_order->warehouse != 3){
- $wmj_change_warehouse = 2;
- }
- DB::beginTransaction();
- try{
- $res = DB::table('order')->where('id', $id)->update($order);
- /** 新逻辑 统计预占库存 1.更新前 **/
- if($old_order->orderCode == null){
- $old_skus = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
- $old_skus = json_decode(json_encode($old_skus), true);
- }
- /** end **/
- //商品信息
- $skus = $request->input('skus');
- $gnum = $request->input('gnum');
- if(empty($skus)) $skus = [];
- //先删除
- $del_re = DB::table('order_goods_skus')->where('order_id', $id)->whereNull('gift_id')->whereNotIn('sku_id', $skus)->update(['is_del'=>1]);
- if(!empty($skus)){
- //再同步
- foreach($skus as $k=>$sku_id){
- $sku_info = GoodsSkus::select('goodsCode', 'price', 'propsName','is_weigh', 'is_white')->where('id', $sku_id)->first();
- #新加逻辑,判断库存是否足够
- #1.判断是否已存在该sku
- if($sku_info->is_white != 1){
- $old_order_goods_num = OrderGoodsSkus::where('order_id', $id)->where('sku_id', $sku_id)->where('is_del', 0)->sum('num');
- if( $gnum[$k] > $old_order_goods_num ){
- $quantity_redis = GoodsSkus::actualQuantityByRedis($sku_id);
- $act_quantity = $quantity_redis['quantity'];
- if($act_quantity < $gnum[$k] - $old_order_goods_num){
- throw new Exception("库存不足");
- }
- }
- }
-
- $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]);
- if( $mjFlag == 1 && empty($mjOrder) ){
- $goods_info = Goods::where('id', $sku_info->goodsCode)->first();
- $params = array();
- $params['outerCode'] = $old_order->outerCode+$k+1; //外部平台中子订单编码
- $params['outerParentCode'] = $old_order->outerCode; //外部平台中订单编码
- $params['goodsCode'] = $sku_info->goodsCode;// strstr($goods->code,'-',true);
- $params['skuCode'] = $sku_id;
- #商品名称
- $params['goodsName'] = $goods_info->name;
- $params['propertiesName'] = $sku_info->is_weigh==1 ? $sku_info->propsName.'【按半斤称重】' : $sku_info->propsName;
- $params['price'] = $sku_info->is_weigh==1 ? round($sku_info->price/2,2) : $sku_info->price;
- $params['quantity'] = (int)($sku_info->is_weigh==1 ? $gnum[$k]*2 : $gnum[$k]);
- $params['discountFee'] = 0;
- $params['adjustFee'] = 0;
- $params['refundStatus'] = '正常';
- $orderSkus['skuList'][] = $params;
-
- }
- }
- }
-
- if($res){
- if($mjFlag == 1){
- //其他仓变C仓
- if(empty($mjOrder)){
- #新加逻辑 查询收件人所在城市天气情况并更新
- Order::updateWeather($id);
- $mj_in = $this->syncOrderToMj($id, $orderSkus);
- if( $mj_in == false ){
- throw new Exception("订单同步到卖家云失败");
- }
- }elseif($mjOrder['status'] == '已取消'){
- //变更状态
- $up = array();
- $up['code'] = $old_order->orderCode;
- $up['eshopCode'] = $old_order->eshopCode;
- $up['outerCode'] = $old_order->outerCode;
- $up['status'] = $old_order->status;
- $mj_up = $this->updateOrderToMj($id, $up);
- if( $mj_up == false ){
- throw new Exception("订单同步到卖家云失败");
- }
- }
- }elseif($mjFlag == 2){
- //C仓 卖家云订单更新
- $orderSkus['code'] = $old_order->orderCode;
- $mj_up = $this->updateOrderToMj($id, $orderSkus);
- if( $mj_up == false ){
- throw new Exception("订单更新同步到卖家云失败");
- }
- }
- if($delFlag == 1){
- //判断是否在卖家云存在
- $mjOrder = $this->getMjOrder($id, $old_order->outerCode);
- if(!empty($mjOrder)){
- $del = array();
- $del['eshopCode'] = $old_order->eshopCode;
- $del['outerCode'] = $old_order->outerCode;
- $del['code'] = $old_order->orderCode;
- $mj_del = Order::mjOrderDel($del);
- if( $mj_del == false ){
- throw new Exception("订单同步到卖家云失败");
- }
- if($order['warehouse'] == 3) {
- //卖家云取消的订单也要加预占库存 ++ C仓
- $sku_redis = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
- $sku_redis = json_decode(json_encode($sku_redis), true);
- Order::diffSkuToRedis('', $sku_redis);
- #更新mj_status
- OrderGoodsSkus::where('order_id', $id)->update(['mj_status' => 0]);
- Order::where('id', $id)->update(['orderCode'=>null, 'outerCode'=>$old_order->outerCode. 'qx']);
- }
- }
- }
-
- #新加逻辑,若付款方式为充值卡/消费记录
- if($order['payment_type'] == 4 ){
- if($old_order->payment_type != 4 ){
- $consum = new CustomerConsum();
- $consum->phone = $deposit_phone;
- $consum->order_id = $id;
- $consum->money = $order['receivedAmount'];
- $consum->save();
- //更新余额
- $customerInfo->balance = $customerInfo->balance - $order['receivedAmount'];
- $customerInfo->save();
- }elseif($old_order->receivedAmount != $order['receivedAmount']){
- $consum = CustomerConsum::where('order_id', $id)->where('type', 0)->first();
- $consum->money = $order['receivedAmount'];
- $consum->save();
- //更新余额
- $add_amount = $order['receivedAmount'] - $old_order->receivedAmount; //新增消费额,有可能为负数
- $customerInfo->balance = $customerInfo->balance - $add_amount;
- $customerInfo->save();
- }
- }
- #记录操作日志
- $self_id = session('admin_id');
- $self_name = session('real_name');
- $context = "修改订单";
- $type = 0;
- $tables = 'order';
- $data_id = $id;
- Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
- }
- #新加逻辑,使用优惠券,赠礼
- if($old_order->status<2){
- $use_coupon = (int)$request->input('use_coupon');
- $use_b_gift = (int)$request->input('use_b_gift');
- if($use_coupon == 1 && $old_order->use_coupon != 1){
- $coupon = DB::table('customer_coupons')->where('phone', $order['buyer_phone'])->where('is_use',0)->orderBy('id', 'desc')->first();
- if(!empty($coupon)){
- $cup = array();
- $cup['is_use'] = 1;
- $cup['use_time'] = date('Y-m-d H:i:s');
- $cup['order_id'] = $id;
- DB::table('customer_coupons')->where('id', $coupon->id)->update($cup);
- }
- }elseif($use_coupon == 0 && $old_order->use_coupon == 1){
- DB::table('customer_coupons')->where('order_id', $id)->update(['is_use'=>0]);
- }
- #查询本单使用赠礼
- $order_gifts = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $id)->where('gift_id','>',0)->lists('gift_id');
- $order_m_gift_id = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 0)->pluck('id');
- $order_b_gift_ids = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 1)->lists('id');
- if($m_gift_id>0 && empty($order_m_gift_id) ){
- $m_gift = DB::table('customer_month_gift')->where('id', $m_gift_id)->where('type', 0)->first();
- $order_m_gift = array();
- $m_sku = GoodsSkus::where('id', $m_gift->sku_id)->first();
- $order_m_gift['sku_id'] = $m_gift->sku_id;
- $order_m_gift['order_id'] = $id;
- $order_m_gift['goods_id'] = $m_sku->goodsCode;
- $order_m_gift['num'] = $m_gift->num;
- $order_m_gift['price'] = $m_sku->price;
- $order_m_gift['gift_id'] = $m_gift_id;
- $order_gift_insert = DB::table('order_goods_skus')->insert($order_m_gift);
- #记录领取
- $receive_gift = array();
- $receive_gift['phone'] = $order['buyer_phone'];
- $receive_gift['dtime'] = date('Y-m-d');
- $receive_gift['order_id'] = $id;
- $receive_gift['gift_id'] = $m_gift_id;
- $receive_gift['gift_type'] = 0;
- DB::table('customer_gift_receives')->insert($receive_gift);
- }elseif($m_gift_id>0 && $m_gift_id != $order_m_gift_id){
- DB::table('order_goods_skus')->where('gift_id', $order_m_gift_id)->where('order_id', $id)->update(['is_del'=>1]);
- DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_id', $order_m_gift_id)->update(['is_del'=>1]);
- $m_gift = DB::table('customer_month_gift')->where('id', $m_gift_id)->where('type', 0)->first();
- $order_m_gift = array();
- $m_sku = GoodsSkus::where('id', $m_gift->sku_id)->first();
- $order_m_gift['sku_id'] = $m_gift->sku_id;
- $order_m_gift['order_id'] = $id;
- $order_m_gift['goods_id'] = $m_sku->goodsCode;
- $order_m_gift['num'] = $m_gift->num;
- $order_m_gift['price'] = $m_sku->price;
- $order_m_gift['gift_id'] = $m_gift_id;
- $order_gift_insert = DB::table('order_goods_skus')->insert($order_m_gift);
- #记录领取
- $receive_gift = array();
- $receive_gift['phone'] = $order['buyer_phone'];
- $receive_gift['dtime'] = date('Y-m-d');
- $receive_gift['order_id'] = $id;
- $receive_gift['gift_id'] = $m_gift_id;
- $receive_gift['gift_type'] = 0;
- DB::table('customer_gift_receives')->insert($receive_gift);
- }elseif($m_gift_id==0 && $order_m_gift_id>0){
- DB::table('order_goods_skus')->where('gift_id', $order_m_gift_id)->where('order_id', $id)->update(['is_del'=>1]);
- DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_id', $order_m_gift_id)->update(['is_del'=>1]);
- }
- $mdate = date('Y-m-01', strtotime($old_order->create_time));
- if($use_b_gift>0 && empty($order_b_gift_ids) ){
- $b_gifts = DB::table('customer_month_gift')->where('mtime', $mdate)->where('type', 1)->get();
- $b_gifts = json_decode(json_encode($b_gifts),true);
- foreach($b_gifts as $b_gift){
- $order_b_gift = array();
- $b_sku = GoodsSkus::where('id', $b_gift['sku_id'])->first();
- $order_b_gift['sku_id'] = $b_gift['sku_id'];
- $order_b_gift['order_id'] = $id;
- $order_b_gift['goods_id'] = $b_sku->goodsCode;
- $order_b_gift['num'] = $b_gift['num'];
- $order_b_gift['price'] = $b_sku->price;
- $order_b_gift['gift_id'] = $b_gift['id'];
- $order_gift_insert = DB::table('order_goods_skus')->insert($order_b_gift);
- }
- #记录领取
- $receive_gift = array();
- $receive_gift['phone'] = $order['buyer_phone'];
- $receive_gift['dtime'] = date('Y-m-d');
- $receive_gift['order_id'] = $id;
- $receive_gift['gift_type'] = 1;
- DB::table('customer_gift_receives')->insert($receive_gift);
- }elseif($use_b_gift==0 && !empty($order_b_gift_ids)){
- DB::table('order_goods_skus')->whereIn('gift_id', $order_b_gift_ids)->where('order_id', $id)->update(['is_del'=>1]);
- DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_type', 1)->update(['is_del'=>1]);
- }
- }
- DB::commit();
- /** 新逻辑 统计预占库存 2.更新后 **/
- if($old_order->orderCode == null && $old_order->warehouse == 3 && $order['warehouse'] == 3 && $old_order->status >= 1){
- $new_skus = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
- $new_skus = json_decode(json_encode($new_skus), true);
- Order::diffSkuToRedis($old_skus, $new_skus);
- }elseif($wmj_change_warehouse == 1){
- Order::diffSkuToRedis('', $old_skus, -1);
- }elseif($wmj_change_warehouse == 2){
- $new_skus = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
- $new_skus = json_decode(json_encode($new_skus), true);
- Order::diffSkuToRedis('', $new_skus );
- }
- /** end **/
- }catch (Exception $e){
- DB::rollback();
- Log::errorLog($request,['error'=>$e->getMessage()],"order/update",0,'');
- return redirect('/admin/order/index?'.$str_query)->with('info', $e->getMessage());
- }
- if(!empty($last_url)){
- header('Location:'.$last_url);exit;
- }
- return redirect('/admin/order/index?'.$str_query)->with('info', '更新成功');
- }
- /**
- * 分组管理-进行删除操作
- * @param Request $request
- * @return \Illuminate\Http\RedirectResponse
- */
- public function orderdelete($id)
- {
- $order = Order::find($id);
- $order->is_del = 1;
- DB::beginTransaction();
- try{
- if ($order ->save()){
- //假如删除订单在卖家云,同步为取消状态
- if($order->orderCode>0 && $order->warehouse==3){
- $del = array();
- $del['eshopCode'] = $order->eshopCode;
- $del['outerCode'] = $order->outerCode;
- $del['code'] = $order->orderCode;
- $mj_del = Order::mjOrderDel($del);
- if( $mj_del == false ){
- throw new Exception("订单同步到卖家云失败");
- }
- }
- # 新加去除预占库存
- if($order->orderCode == null && $order->warehouse==3 && $order->status >= 1){
- // +++++ 已删除,减去预占库存到redis
- $sku_redis = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
- $sku_redis = json_decode(json_encode($sku_redis),true);
- Order::diffSkuToRedis('', $sku_redis, -1);
- }
- //假如删除订单为充值卡订单,返还余额
- if($order->payment_type == 4){
- //查询消费记录/卡号
- $consum = CustomerConsum::where('order_id', $id)->where('is_del', 0)->first();
- //查询需要返还消费钱数
- $fan_money = CustomerConsum::where('order_id', $id)->where('is_del', 0)->sum('money');
- //消费记录设为删除
- CustomerConsum::where('order_id', $id)->update(['is_del'=>1]);
- //余额返还
- $cust = CustomerInfo::select('balance')->where('phone', $consum->phone)->first();
- $balance = $cust->balance + $fan_money;
- CustomerInfo::where('phone', $consum->phone)->update(['balance'=>$balance]);
- }
- #更新order_goods_skus状态
- $mj_status = array();
- $mj_status['mj_status'] = 2;
- OrderGoodsSkus::where('order_id', $id)->where('is_del', 0)->update($mj_status);
- //假如删除订单使用了优惠券,礼品
- if($order->use_coupon == 1){
- DB::table('customer_coupons')->where('order_id', $id)->update(['is_use'=>0]);
- }
- #查询本单使用赠礼
- $order_gifts = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $id)->where('gift_id','>',0)->lists('gift_id');
- if(!empty($order_gifts)){
- $order_m_gift_id = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 0)->pluck('id');
- $order_b_gift_ids = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 1)->lists('id');
- if($order_m_gift_id>0){
- DB::table('order_goods_skus')->where('gift_id', $order_m_gift_id)->where('order_id', $id)->update(['is_del'=>1]);
- DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_id', $order_m_gift_id)->update(['is_del'=>1]);
- }
- if(!empty($order_b_gift_ids)){
- DB::table('order_goods_skus')->whereIn('gift_id', $order_b_gift_ids)->where('order_id', $id)->update(['is_del'=>1]);
- DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_type', 1)->update(['is_del'=>1]);
- }
- }
- #记录操作日志
- $self_id = session('admin_id');
- $self_name = session('real_name');
- $context = "删除订单";
- $type = 0;
- $tables = 'order';
- $data_id = $id;
- Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
- }
- DB::commit();
- }catch (Exception $e){
- Log::errorLog('',['error'=>$e->getMessage()],"order/orderdelete",0,['id'=>$id]);
- DB::rollback();
- }
- // exit('<script>window.location.replace(document.referrer)</script>');
- //return redirect('/admin/order/index')->with('info', '删除成功');
- echo '<script>window.location.replace(document.referrer)</script>';die;
- }
- /**
- * 设为退款状态操作
- * @param Request $request
- * @return \Illuminate\Http\RedirectResponse
- */
- public function set_refund(Request $request)
- {
- $id = (int)$request->input('refund_id');
- $aftersale_fee = trim($request->input('refund_money'));
- $aftersale_reason = trim($request->input('refund_reason'));
- $aftersale_time = $request->input('refund_time');
- if(empty($aftersale_fee) || empty($aftersale_time)){
- // exit('<script>alert("必须填写退款金额和时间");history.back()</script>');
- echo '<script>alert("必须填写退款金额和时间");history.back()</script>';die;
- }
- $order = Order::find($id);
- $order->status = 4;
- $order->aftersale_fee = $aftersale_fee;
- $order->aftersale_reason = $aftersale_reason;
- $order->aftersale_time = $aftersale_time;
- DB::beginTransaction();
- try{
- if ($order ->save()){
- //假如删除订单在卖家云,同步为取消状态
- if($order->orderCode>0 && $order->warehouse==3){
- $del = array();
- $del['eshopCode'] = $order->eshopCode;
- $del['outerCode'] = $order->outerCode;
- $del['code'] = $order->orderCode;
- $mj_del = Order::mjOrderDel($del);
- if( $mj_del == false ){
- throw new Exception("订单同步到卖家云失败");
- }
- }
- # 新加去除预占库存
- if($order->orderCode == null && $order->warehouse==3 && $order->status>=1){
- // +++++ 已删除,减去预占库存到redis
- $sku_redis = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
- $sku_redis = json_decode(json_encode($sku_redis),true);
- Order::diffSkuToRedis('', $sku_redis, -1);
- }
- //假如删除订单为充值卡订单,返还余额
- if($order->payment_type == 4){
- //查询消费记录/卡号
- $consum = CustomerConsum::where('order_id', $id)->where('is_del', 0)->first();
- //查询需要返还消费钱数
- $fan_money = CustomerConsum::where('order_id', $id)->where('is_del', 0)->sum('money');
- //消费记录设为删除
- CustomerConsum::where('order_id', $id)->update(['is_del'=>1]);
- //余额返还
- $cust = CustomerInfo::select('balance')->where('phone', $consum->phone)->first();
- $balance = $cust->balance + $fan_money;
- CustomerInfo::where('phone', $consum->phone)->update(['balance'=>$balance]);
- }
- #更新order_goods_skus状态,退回库存
- $mj_status = array();
- $mj_status['mj_status'] = 2;
- OrderGoodsSkus::where('order_id', $id)->where('is_del', 0)->update($mj_status);
- //假如删除订单使用了优惠券,礼品
- if($order->use_coupon == 1){
- DB::table('customer_coupons')->where('order_id', $id)->update(['is_use'=>0]);
- }
- #查询本单使用赠礼
- $order_gifts = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $id)->where('gift_id','>',0)->lists('gift_id');
- if(!empty($order_gifts)){
- $order_m_gift_id = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 0)->pluck('id');
- $order_b_gift_ids = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 1)->lists('id');
- if($order_m_gift_id>0){
- DB::table('order_goods_skus')->where('gift_id', $order_m_gift_id)->where('order_id', $id)->update(['is_del'=>1]);
- DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_id', $order_m_gift_id)->update(['is_del'=>1]);
- }
- if(!empty($order_b_gift_ids)){
- DB::table('order_goods_skus')->whereIn('gift_id', $order_b_gift_ids)->where('order_id', $id)->update(['is_del'=>1]);
- DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_type', 1)->update(['is_del'=>1]);
- }
- }
-
- #记录操作日志
- $self_id = session('admin_id');
- $self_name = session('real_name');
- $context = "订单设为退款";
- $type = 0;
- $tables = 'order';
- $data_id = $id;
- Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
- }
- DB::commit();
- }catch (Exception $e){
- Log::errorLog('',['error'=>$e->getMessage()],"order/set_refund",0,['id'=>$id]);
- DB::rollback();
- }
- // exit('<script>window.location.replace(document.referrer)</script>');
- //return redirect('/admin/order/index')->with('info', '删除成功');
- echo '<script>window.location.replace(document.referrer)</script>';die;
- }
- public function setrefund($id){
- $order = Order::find($id);
- $order->order_status = 2;
- if ($order ->save()){
- // exit('0');
- echo '0';die;
- }
- // exit('1');
- echo '1';die;
- }
- public function setverify($id, Request $request){
- $order = Order::find($id);
- $old_status = $order->status;
- $order->status = (int)$request->input('status');
- if($order->status == 0){
- $refuse_reason = trim($request->input('refuse_reason'));
- if(!empty($refuse_reason)){
- $order->refuse_reason = $refuse_reason;
- }
- }
- if($order->status == 2 || $order->status == 0){
- $order->verify_time = date('Y-m-d H:i:s'); //审核时间
- }
- //新加 提审 判断库存
- if($old_status==0 && $order->status == 1 && $order->warehouse == 3){
- $order_skus = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
- foreach($order_skus as $order_sku){
- $sku_id = $order_sku->sku_id;
- $num = $order_sku->num;
- $sku_info = GoodsSkus::where('id', $sku_id)->first();
- if($sku_info->is_white != 1){
- $quantity_redis = GoodsSkus::actualQuantityByRedis($sku_id);
- $act_quantity = $quantity_redis['quantity'];
- if($act_quantity < $num){
- return redirect('/admin/order/index')->with('info', '库存不足');
- }
- }
- }
-
- }
- //获取可变更状态
- $enableStatus = $this->getEnableStatus($old_status, $order->warehouse);
- if( !empty($enableStatus) ){
- $statusArr = array_column($enableStatus, 'status');
- if( !in_array($order->status, $statusArr) ){
- return redirect('/admin/order/index')->with('info', '异常操作');
- }
- }else{
- return redirect('/admin/order/index');
- }
- DB::beginTransaction();
- try{
- $res = $order ->save();
- if($res){
- #记录操作日志
- $status_arr = array(
- 0 => '已录入',
- 1 => '待审核',
- 2 => '已审核',
- 3 => '已发货',
- 4 => '已退款'
- );
- $self_id = session('admin_id');
- $self_name = session('real_name');
- $context = "订单状态变更:【".$status_arr[$old_status]."】变更为【". $status_arr[$order->status]."】({$old_status}->{$order->status})";
- $type = 1; //状态变更
- $tables = 'order';
- $data_id = $id;
- Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
- #发提醒短信
- //$this->sendRemind($self_id, $id, $order->status);
- if( $old_status==0 && $order->status == 1 && $order->warehouse == 3){
- # 提审 预占加 redis
- $sku_redis = $order_skus;
- $sku_redis = json_decode(json_encode($sku_redis),true);
- Order::diffSkuToRedis('', $sku_redis);
- }
- if($order->status == 0 && $order->warehouse == 3){
- # 驳回 减预占 redis
- $sku_redis = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
- $sku_redis = json_decode(json_encode($sku_redis),true);
- Order::diffSkuToRedis('', $sku_redis, -1);
- }
-
- $today_date = date('Y-m-d');
- #如果审核通过,c仓, 并且到预发货时间,同步卖家云
- if($order->status == 2 && $order->warehouse == 3 && $order->delivery_date <= $today_date){
- #新加逻辑 查询收件人所在城市天气情况并更新
- Order::updateWeather($id);
- $syncMj = $this->syncOrderToMj($id);
- if( $syncMj == false ){
- throw new Exception("订单同步到卖家云失败");
- }
- #同步到卖家云,更新order_goods_skus状态
- $mj_status = array();
- $mj_status['mj_status'] = 1;
- OrderGoodsSkus::where('order_id', $id)->where('is_del', 0)->update($mj_status);
- // +++++ 已到卖家,减去预占库存到redis
- $sku_redis = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
- $sku_redis = json_decode(json_encode($sku_redis),true);
- Order::diffSkuToRedis('', $sku_redis, -1);
- }
- # 新加反审逻辑3-11
- if($order->status == 1 && $order->warehouse == 3 && $old_status == 2){
- # 已在卖家
- if($order->orderCode>0){
- //1.取消卖家
- $del = array();
- $del['eshopCode'] = $order->eshopCode;
- $del['outerCode'] = $order->outerCode;
- $del['code'] = $order->orderCode;
- $mj_del = Order::mjOrderDel($del);
- if( $mj_del == false ){
- throw new Exception("取消订单同步到卖家云失败");
- }
- #同步取消到卖家云,更新order_goods_skus状态
- $mj_status = array();
- $mj_status['mj_status'] = 0;
- OrderGoodsSkus::where('order_id', $id)->where('is_del', 0)->update($mj_status);
- Order::where('id', $id)->update(['orderCode'=>null, 'outerCode'=>$order->outerCode. 'qx']);
- # 预占加 redis
- $sku_redis = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
- $sku_redis = json_decode(json_encode($sku_redis),true);
- Order::diffSkuToRedis('', $sku_redis);
- }else{
- #未在卖家云不需处理。
- }
- }
- }
- DB::commit();
- }catch (Exception $e){
- Log::errorLog($request,['error'=>$e->getMessage()],"order/setverify",0,['id'=>$id]);
- DB::rollback();
- }
- // exit('<script>window.location.replace(document.referrer)</script>');
- echo '<script>window.location.replace(document.referrer)</script>';die;
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $receiverName = $request->input('receiverName');
- $receiverMobile = $request->input('receiverMobile');
- $admin_id = (int)$request->input('admin_id');
- $page = (int)$request->input('page');
- $str_query = 'page='.$page.'&admin_id='.$admin_id.'&stime='.$stime.'&etime='.$etime.'&receiverName='.$receiverName.'&receiverMobile='.$receiverMobile;
- return redirect('/admin/order/index?'.$str_query)->with('info', '操作成功');
- }
- public function setverifymore(Request $request){
- $ids = $request->get('id');
- foreach($ids as $id){
- $order = Order::find($id);
- $order->status = 1;
- $order ->save();
- }
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $receiverName = $request->input('receiverName');
- $receiverMobile = $request->input('receiverMobile');
- $admin_id = (int)$request->input('admin_id');
- $page = (int)$request->input('page');
- $str_query = 'page='.$page.'&admin_id='.$admin_id.'&stime='.$stime.'&etime='.$etime.'&receiverName='.$receiverName.'&receiverMobile='.$receiverMobile;
- return redirect('/admin/order/index?'.$str_query)->with('info', '操作成功');
-
- }
- /**
- * 分组管理-分组展示
- * @param Request $request
- * @return \Illuminate\Http\RedirectResponse
- */
- public function orderup($id)
- {
- //数据库-修改status字段值
- $order = Order::find($id);
- $order ->status = 1;
- if ($order ->update()){
- return redirect('/admin/order/index')->with('info', '启用成功');
- }
- }
- /**
- * 分组管理-取消展示
- * @param Request $request
- * @return \Illuminate\Http\RedirectResponse
- */
- public function orderdown($id)
- {
- //数据库-修改status字段值
- $order = Order::find($id);
- $order ->status = 0;
- if ($order ->update()){
- return redirect('/admin/order/index')->with('info', '禁用成功');
- }
- }
-
- /**
- * 分类取商品
- */
- public function categoods($category){
- $goods_ids = DB::table('goods_category')->where('goods_category_name', $category)->lists('goods_id');
- $goods = DB::table('goods_sku_list')->select('id', 'price', 'props_name', 'goods_id')->whereIn('goods_id', $goods_ids)->orderBy('goods_id', 'asc')->get();
- foreach($goods as $item){
- $gname = DB::table('goods_category')->select('goods_name')->where('goods_id', $item->goods_id)->first();
- $item->goods_name = $gname->goods_name;
- }
- $goods = json_decode(json_encode($goods), true);
- return json_encode($goods);
- }
- /**
- * 操作员列表
- */
- public function teamAdmins($team_id){
- //$saler_ids = DB::table('admin_role')->where('role_name', '销售')->lists('user_id');
- $res = DB::table('admin')->select('id', 'realname', 'username')->where('team_id', $team_id)->where('id','>', 1)->get();
- $res = json_decode(json_encode($res),true);
- return json_encode($res);
- }
- /**
- * 客户地址信息
- */
- public function getCustomer(Request $request){
- $phone = $request->input('phone');
- if(!preg_match('/^1\d{10}$/')){
- return 0;
- }
- $res = DB::table('customers')->where('phone', $phone)->first();
- if(isset($res->id)){
- $result = json_decode(json_encode($res),true);
- return json_encode($result);
- }
- }
- public function order_export(Request $request){
-
- $self_role = session('role_name');
- if($self_role == '超级管理员' || $self_role == '团队主管' || $self_role == '管理员' || $self_role == 'A仓库管理员' || $self_role == 'B仓库管理员' || $self_role == '售后管理员' || $self_role == '分销管理员'){
- $admin_id = $request->input('admin_id');
- }else{
- $admin_id = session('admin_id');
- }
- $is_saler = 0;
- if($self_role == '销售' || $self_role == '分销销售'){
- $is_saler = 1;
- }
- if($self_role == 'A仓库管理员'){
- $warehouse = 1;
- }elseif($self_role == 'B仓库管理员'){
- $warehouse = 2;
- }else{
- $warehouse = 0;
- }
- $team_id = $request->input('team_id');
- #只能看自己团队的
- if($self_role != '超级管理员' && $self_role != '售后管理员'){
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $receiverName = $request->input('receiverName');
- $receiverMobile = $request->input('receiverMobile');
- $status = $request->input('status');
- $id = (int)$request->input('id');
- $warehouse = (int)$request->input('warehouse');
- if($status === null){
- $status = -1;
- }
- if($etime){
- $etime = $etime. ' 23:59:59';
- }
- $result = Order::where(function($query) use($admin_id, $stime, $etime, $receiverName, $receiverMobile, $self_role, $sale_ids, $status, $id, $warehouse){
- if($admin_id) $query->where('admin_id', $admin_id);
- if($stime) $query->where('createTime', '>=', $stime);
- if($etime) $query->where('createTime', '<=', $etime);
- if($receiverName) $query->where('receiverName', 'like', $receiverName . '%');
- if($receiverMobile) $query->where('receiverMobile', $receiverMobile);
- if($self_role == '管理员' || $self_role == '分销管理员') $query->where('status','>',0);
- if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if(($status>=0)) $query->where('status', $status);
- if(($id>0)) $query->where('id', $id);
- if(($warehouse>0)) $query->where('warehouse', $warehouse);
- })->where('is_del',0)->orderBy('id', 'desc')->get();
- $result = json_decode(json_encode($result),true);
- $status_arr = array(
- 0 => '已录入',
- 1 => '待审核',
- 2 => '已审核',
- 3 => '已发货',
- 4 => '已退款'
- );
- $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
- foreach($result as $k=>&$v){
- #加粉时间
- $fanTime = DB::table('customers')->select('fanTime')->where('phone', $v['receiverMobile'])->first();
- $v['fanTime'] = isset($fanTime->fanTime) ? $fanTime->fanTime : '';
- $v['receiverMobile'] = substr($v['receiverMobile'], 0, 3).'****'.substr($v['receiverMobile'], 7);
- $v['status_name'] = $status_arr[$v['status']];
- $v['address'] = $v['receiverState'].$v['receiverCity'].$v['receiverDistrict'].$v['receiverStreet'].$v['receiverAddress'];
- $v['warehouse'] = $this->warehouse[$v['warehouse']];
- $v['is_fugou'] = $v['is_fugou'] == 1? '是':'否';
- $v['is_refund'] = $v['is_refund'] == 1? '是':'否';
- $v['payment_type'] = $payArr[$v['payment_type']];
- }
- if($is_saler == 0){
- $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'];
- $title = ['订单编号', '收货人', '收货人手机号', '收货地址', '商品信息', '预发货时间', '物流单号', '货品成本', '运费成本', '发货仓库', '付款金额', '支付方式', '下单时间', '加粉时间', '是否复购', '是否退补单', '应该退补差价', '退补备注', '所属销售', '订单状态', '审核时间', '买家备注', '卖家备注', '发货备注', '售后费用', '售后原因', '售后时间'];
- }else{
- $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'];
- $title = ['订单编号', '收货人', '收货人手机号', '收货地址', '商品信息', '预发货时间', '物流单号', '发货仓库', '付款金额', '付款方式', '下单时间', '加粉时间', '是否复购', '是否退补单', '应该退补差价', '退补备注', '所属销售', '订单状态', '审核时间', '买家备注', '卖家备注', '发货备注', '售后费用', '售后原因'];
- }
- $filename = 'dingdan_'.date('Y-m-d_H').'.xlsx';
- return $this->export_excel($result, $filename, $indexKey, $title);
- }
- public function editLogisticsId(Request $request){
- $id = (int)$request->input('id');
- $logistics_id = $request->input('logistics_id');
- $send_time = $request->input('send_time');
- $cost = $request->input('cost');
- $freight_cost = $request->input('freight_cost');
- $send_note = $request->input('send_note');
- $refund_note = $request->input('refund_note');
- $buyerMemo = $request->input('buyerMemo');
- $sellerMemo = $request->input('sellerMemo');
- $aftersale_fee = $request->input('aftersale_fee');
- $aftersale_reason = $request->input('aftersale_reason');
- $aftersale_time = $request->input('aftersale_time');
- $refund_time = $request->input('refund_time');
- $refund_price = $request->input('refund_price');
- $refund_type = $request->input('refund_type');
- $order = Order::where('id', $id)->first();
- $data = array();
- if(isset($logistics_id)){
- $data['logistics_id'] = $logistics_id;
- if($logistics_id && !$order->send_time){
- //填物流单号默认发货,记录发货时间 //更新的时候不变动
- $data['send_time'] = date('Y-m-d');
- }
- }
- if(isset($send_time)) $data['send_time'] = trim($send_time);
- if(isset($cost)){
- if(empty($cost)) $cost = null;
- $data['cost'] = $cost;
- }
- if(isset($freight_cost)){
- if(empty($freight_cost)) $freight_cost = null;
- $data['freight_cost'] = $freight_cost;
- }
- if(isset($buyerMemo)) $data['buyerMemo'] = trim($buyerMemo);
- if(isset($sellerMemo)) $data['sellerMemo'] = trim($sellerMemo);
- if(isset($send_note)) $data['send_note'] = trim($send_note);
- if(isset($refund_note)) $data['refund_note'] = trim($refund_note);
- if(isset($aftersale_fee)) $data['aftersale_fee'] = trim($aftersale_fee);
- if(isset($aftersale_reason)) $data['aftersale_reason'] = trim($aftersale_reason);
- if(isset($aftersale_time)) $data['aftersale_time'] = trim($aftersale_time);
- if(isset($refund_time)) $data['refund_time'] = trim($refund_time);
- if(isset($refund_price)){
- if(empty($refund_price)){
- $refund_price = null;
- $data['is_refund'] = 0;
- }else{
- $data['is_refund'] = 1;
- if(!$order->refund_type) $data['refund_type'] = 1;
- if(!$order->refund_time) $data['refund_time'] = date('Y-m-d');
- if($order->refund_type == 4 && $refund_price != $order->refund_price){
- // 充值退补
- $res = $this->depositRefund($id, $refund_price);
- if(!$res){
- return 0;
- }
- }
- }
- $data['refund_price'] = $refund_price;
- }
- if(isset($refund_type)){
- if(empty($refund_type)){
- $refund_type = null;
- }elseif($refund_type == 4){
- //判断是否是充值卡支付方式
- if($order->payment_type != 4){
- return 0;
- }
- if($order->refund_price != 0 && $order->refund_type != 4){
- $res = $this->depositRefund($id, $order->refund_price);
- if(!$res){
- return 0;
- }
- }
- }elseif($refund_type != 4 && $order->refund_type == 4){
- return 0;
- }
- $data['refund_type'] = (int)$refund_type;
- if(!$order->refund_time) $data['refund_time'] = date('Y-m-d');
- }
- if($id>0 && !empty($data)){
- $res = Order::where('id', $id)->update($data);
- }
- return 1;
- }
- /**
- * 充值卡退补
- */
- public function depositRefund($order_id, $refund_price){
- //查消费记录
- $order_consum = DB::table('customer_consum')->where('is_del', 0)->where('order_id', $order_id)->where('type', 0)->first();
- if(empty($order_consum)){
- return false;
- }
- //查看是否已有退补记录
- $order_refund = DB::table('customer_consum')->where('is_del', 0)->where('order_id', $order_id)->where('type', 1)->first();
- $balance = CustomerInfo::where('phone', $order_consum->phone)->pluck('balance');
- if(empty($order_refund)){
- if($refund_price>0){
- //补钱,判断充值卡余额
- if($balance - $refund_price < 0){
- return false;
- }
- }
- $consum = array();
- $consum['phone'] = $order_consum->phone;
- $consum['order_id'] = $order_id;
- $consum['money'] = $refund_price;
- $consum['type'] = 1;
- DB::table('customer_consum')->insert($consum);
- $balance = $balance - $refund_price;
- }elseif($order_refund->money != $refund_price){
- //已有退款,更变le 退款钱数
- if($balance - $refund_price + $order_refund->money <0 ){
- return false;
- }
- DB::table('customer_consum')->where('id', $order_refund->id)->update(['money'=>$refund_price]);
- $balance = $balance - $refund_price + $order_refund->money;
- }
-
- CustomerInfo::where('phone', $order_consum->phone)->update(['balance' => $balance]);
- return true;
- }
- public function warehouse_export(Request $request){
-
- $self_role = session('role_name');
- $admin_id = $request->input('admin_id');
- if($self_role == 'A仓库管理员'){
- $warehouse = 1;
- }elseif($self_role == 'B仓库管理员'){
- $warehouse = 2;
- }else{
- $warehouse = 0;
- }
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $receiverName = $request->input('receiverName');
- $receiverMobile = $request->input('receiverMobile');
- $delivery_type = (int)$request->input('delivery_type'); //发货状态 0到发货时间(未填物流信息) 2全部
- $id = (int)$request->input('id');
- $_start = '2019-09-18';// 只看18号之后数据
- $today_date = date('Y-m-d');
- $result = Order::where(function($query) use($admin_id, $stime, $etime, $warehouse, $receiverMobile, $receiverName, $today_date, $delivery_type, $_start, $id){
- if($admin_id) $query->where('admin_id', $admin_id);
- if($warehouse) $query->where('warehouse', $warehouse);
- if($stime) $query->where('createTime', '>=', $stime);
- if($etime) $query->where('createTime', '<=', $etime);
- if($receiverName) $query->where('receiverName', 'like', $receiverName . '%');
- if($receiverMobile) $query->where('receiverMobile', $receiverMobile);
- if($id>0) $query->where('id', $id);
- if($delivery_type==0) $query->where('logistics_id', '=', '');
- $query->where('createTime', '>=', $_start);
- })->where('is_del',0)->where('status', '>', 1)->where('delivery_date', '<=', $today_date)->orderBy('id', 'desc')->get();
- $result = json_decode(json_encode($result),true);
- foreach ($result as $k => &$v)
- {
- $v['address'] = $v['receiverState'].$v['receiverCity'].$v['receiverDistrict'].$v['receiverStreet'].$v['receiverAddress'];
- }
- $indexKey = ['id','goods_note','receiverName','receiverMobile','address','createTime','sellerMemo','buyerMemo','cost','logistics_id','freight_cost','send_note'];
- $title = ['订单编号', '商品信息', '收货人', '收货人手机号', '收货地址', '下单时间', '卖家备注', '买家备注', '供应商成本', '顺丰单号', '运费成本', '发货备注'];
- $filename = 'dingdan_'.date('Y-m-d_H').'.xlsx';
- return $this->export_excel($result, $filename, $indexKey, $title);
-
- }
- /**
- * excel导入订单信息
- * @param Request $request
- */
- public function importGoodsExcel(Request $request) {
- $excelFile = $request->file('orderFile');
- //实例化Excel读取类
- $objReader = new PHPExcel_Reader_Excel2007();
- if(!$objReader->canRead($excelFile)){
- $objReader = new PHPExcel_Reader_Excel5();
- if(!$objReader->canRead($excelFile)){
- echo "\n".'无法识别的Excel文件!';
- return false;
- }
- }
- $objPHPExcel=$objReader->load($excelFile);
- $worksheet=$objPHPExcel->getSheet(0);//获取第一个工作表
- $highestRow=$worksheet->getHighestRow();//取得总行数
- $highestColumn=$worksheet->getHighestColumn(); //取得总列数
- $lines = $highestRow - 1;
- if ($lines <= 0) {
- //'Excel表格中没有数据';
- return false;
- }
- $errorArr=[];
- $okStr = '';
- for ($row = 2; $row <= $highestRow; ++$row) {
- $params = array();
- $order_id = (int)$worksheet->getCellByColumnAndRow(0, $row)->getValue();
- $cost = trim($worksheet->getCellByColumnAndRow(8, $row)->getValue());
- $logistics_id = trim($worksheet->getCellByColumnAndRow(9, $row)->getValue());
- $freight_cost = trim($worksheet->getCellByColumnAndRow(10, $row)->getValue());
- $send_note = trim($worksheet->getCellByColumnAndRow(11, $row)->getValue());
- if($cost>0){
- $params['cost'] = $cost;
- }else{
- $params['cost'] = null;
- }
- if($logistics_id){
- $params['logistics_id'] = $logistics_id;
- }else{
- $params['logistics_id'] = '';
- }
- if($send_note){
- $params['send_note'] = $send_note;
- }else{
- $params['send_note'] = '';
- }
- if($freight_cost>0){
- $params['freight_cost'] = $freight_cost;
- }else{
- $params['freight_cost'] = null;
- }
- $result = Order::where('id', $order_id)->update($params);
- if (!$result) {
- $errorArr[] = $order_id;
- }else{
- $okStr .= $order_id.',';
- }
- }
- if (count($errorArr)) {
- $desc = '共'.($highestRow-1).'个订单, 其中'.count($errorArr).'个订单录入失败';
- }else {
- $desc = '订单导入成功';
- }
- #记录操作日志
- $self_id = session('admin_id');
- $self_name = session('real_name');
- $context = "仓管excel导入订单成本等信息 ".$desc;
- $type = 0;
- $tables = 'order';
- $data_id = $okStr;
- Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
- return redirect('admin/order/warehouse')->with('info',$desc);
-
- }
- /**
- * excel导入订单运费信息
- * @param Request $request
- */
- public function importDeliveryExcel(Request $request) {
- $excelFile = $request->file('orderFile');
- //实例化Excel读取类
- $objReader = new PHPExcel_Reader_Excel2007();
- if(!$objReader->canRead($excelFile)){
- $objReader = new PHPExcel_Reader_Excel5();
- if(!$objReader->canRead($excelFile)){
- return redirect('admin/order/warehouse')->with('info','无法识别的Excel文件!');
- }
- }
- $objPHPExcel=$objReader->load($excelFile);
- $worksheet=$objPHPExcel->getSheet(0);//获取第一个工作表
- $highestRow=$worksheet->getHighestRow();//取得总行数
- $highestColumn=$worksheet->getHighestColumn(); //取得总列数
- $lines = $highestRow - 1;
- if ($lines <= 0) {
- //'Excel表格中没有数据';
- return false;
- }
- $errorArr=[];
- $okStr = '';
- for ($row = 2; $row <= $highestRow; ++$row) {
- $logistics_id = trim($worksheet->getCellByColumnAndRow(1, $row)->getValue());
- $freight_cost = trim($worksheet->getCellByColumnAndRow(13, $row)->getValue());
- //var_dump($logistics_id,$freight_cost);exit;
- if($freight_cost>0){
- $params['freight_cost'] = $freight_cost;
- $result = Order::where('logistics_id', $logistics_id)->update($params);
- if (!$result) {
- $errorArr[] = $logistics_id;
- }else{
- $okStr .= $logistics_id.',';
- }
- }
-
- }
- if (count($errorArr)) {
- $desc = '共'.($highestRow-1).'个物流单, 其中'.($highestRow-1-count($errorArr)).'个物流单导入成功';
- }else {
- $desc = '订单导入成功';
- }
- #记录操作日志
- $self_id = session('admin_id');
- $self_name = session('real_name');
- $context = "excel导入订单物流成本 ".$desc;
- $type = 0;
- $tables = 'order';
- $data_id = $okStr;
- Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
- return redirect('admin/order/warehouse')->with('info',$desc);
-
- }
- /**
- * 导出excel
- * @param $data
- * @param string
- */
- public function export_excel($data, $filename = '未命名.xlsx', $indexKey, $title) {
- if( !is_array($indexKey)) return false;
- $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');
- //初始化PHPExcel()
- $objPHPExcel = new PHPExcel();
- $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
-
- //接下来就是写数据到表格里面去
- $objActSheet = $objPHPExcel->getActiveSheet();
- foreach($title as $k=>$item){
- $objActSheet->setCellValue($header_arr[$k].'1',$item);
- }
- $startRow = 2;
- foreach ($data as $row) {
- foreach ($indexKey as $key => $value){
- //这里是设置单元格的内容
- $objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]);
- }
- $startRow++;
- }
- header("Pragma: public");
- header("Expires: 0");
- header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
- header("Content-Type:application/force-download");
- header("Content-Type:application/vnd.ms-execl");
- header("Content-Type:application/octet-stream");
- header("Content-Type:application/download");;
- header('Content-Disposition:attachment;filename='.$filename.'');
- header("Content-Transfer-Encoding:binary");
- $objWriter->save('php://output');
- exit();
- }
- /**
- * 根据手机号获取默认地址信息
- *
- */
- public function getAddress(Request $request){
- $phone = $request->input('phone');
- if(!preg_match('/^1\d{10}$/', $phone)){
- echo '0';die;
- }
- $result = DB::table('customers')->select('receiverState', 'receiverCity', 'receiverDistrict', 'receiverStreet', 'receiverAddress', 'fanTime')->where('phone', $phone)->first();
- if(empty($result)) { echo '0';die; }
- $result = json_decode(json_encode($result), true);
- echo json_encode($result);
- }
- /**
- * 订单状态机制,获取可变更状态
- * @param status:0已录入 1提交审核 2已审核 3已发货 4已退款
- */
- public function getEnableStatus($status, $warehouse=''){
- $status_arr = [
- 0 => ['status'=>0, 'name'=>'已录入'],
- 1 => ['status'=>1, 'name'=>'提交审核'],
- 2 => ['status'=>2, 'name'=>'审核通过'],
- 3 => ['status'=>3, 'name'=>'设为发货'],
- 4 => ['status'=>4, 'name'=>'设为退款']
- ];
- $status = (int)$status;
- $result = array();
- switch ($status) {
- case '0':
- $result[] = $status_arr[1];
- break;
- case '1':
- $result[] = $status_arr[2];
- $result[] = $status_arr[0];
- break;
- case '2':
- $result[] = $status_arr[1];
- if($warehouse != 3) $result[] = $status_arr[3];
- break;
- default:
- break;
- }
- return $result;
- }
- /**
- * 根据订单状态发短信提醒
- */
- public function sendRemind($user_id, $id, $status){
- if($status == 0 || $status == 3){
- //被驳回,获取销售手机号
- $saler_id = DB::table('order')->where('id', $id)->pluck('admin_id');
- $phone = DB::table('admin')->where('id, $saler_id')->pluck('phone');
- $type = $status == 0 ? 5 : 3;
- }elseif($status == 1){
- //待审核,获取团队管理员手机号
- $admin_ids = DB::table('admin_role')->where('role_name','管理员')->lists('user_id');
- $team_id = DB::table('admin')->where('id', $user_id)->pluck('team_id');
- $admin_info = DB::table('admin')->where('team_id', $team_id)->whereIn('id', $admin_ids)->first();
- $phone = $admin_info->phone;
- $type = 1;
- }elseif($status == 2){
- //审核通过,通知仓管发货
- $warehouse = DB::table('order')->where('id', $id)->pluck('warehouse');
- if($warehouse == 1){
- $warehouse_admin = DB::table('admin_role')->select('user_id')->where('role_name','A仓库管理员')->orderBy('id', 'desc')->first();
- $admin_info = DB::table('admin')->where('id', $warehouse_admin->user_id)->first();
- $phone = $admin_info->phone;
- $type = 2;
- }
- }
- if(!$phone){
- return false;
- }
- $res = YPSMS::sendMsg($phone,$type,$id);
- return $res;
- }
- /**
- * 订单同步到卖家云
- */
- public function syncOrderToMj($id, $order = []){
- if( empty($order) ){
- $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();
- $order = json_decode(json_encode($order), true);
- $skus = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $id)->get();
- $skus = json_decode(json_encode($skus), true);
- foreach($skus as $k=>$v){
- $sku_info = GoodsSkus::where('id', $v['sku_id'])->first();
- $goods_info = Goods::where('id', $v['goods_id'])->first();
- $params = array();
- $params['outerCode'] = $order['outerCode']+$k+1; //外部平台中子订单编码
- $params['outerParentCode'] = $order['outerCode']; //外部平台中订单编码
- $params['goodsCode'] = $sku_info->goodsCode;//
- $params['skuCode'] = $v['sku_id'];
- #商品名称
- $params['goodsName'] = $goods_info->name;
- $params['propertiesName'] = $sku_info->is_weigh==1 ? $sku_info->propsName.'【按半斤称重】' : $sku_info->propsName;
- //$params['goodsName'] = '海钓黄翅鱼';//$goods->code;
- $params['price'] = $sku_info->is_weigh==1 ? round($v['price']/2,2) : $v['price'];
- $params['quantity'] = (int)($sku_info->is_weigh==1 ? $v['num']*2 : $v['num']);
- $params['discountFee'] = 0;
- $params['adjustFee'] = 0;
- $params['refundStatus'] = '正常';
- $order['skuList'][] = $params;
- }
- }
- //卖家云添加订单 ->改为审核通过再同步卖家云
- $mj_add = Order::mjOrderAdd($order);
- //同步卖家云订单数据到本系统
- if( isset($mj_add['order']['code']) ){
- $mjorder = array();
- $mjorder['orderCode'] = $mj_add['order']['code'];
- Order::where('id', $id)->update($mjorder);
- }
- return $mj_add;
- }
- /**
- * 订单同步到卖家云
- */
- public function updateOrderToMj($id, $order = []){
- if( empty($order) ){
- $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();
- $order = json_decode(json_encode($order), true);
- }
- //卖家云添加订单 ->改为审核通过再同步卖家云
- $mj_add = Order::mjOrderUpdate($order);
- return $mj_add;
- }
- /**
- * 获取订单信息,可用来判断订单是否存在,同步订单状态
- */
- public function getMjOrder($id, $outerCode=''){
- $order = array();
- $order['eshopCode'] = config('constants.ESHOP_CODE');
- if(!$outerCode){
- $outerCode = Order::where('id', $id)->pluck('outerCode');
- }
- $order['outerCode'] = $outerCode;
- $mjOrder = Order::mjOrderGet($order);
- if(!isset($mjOrder['order']['code'])){
- return false;
- }
- return $mjOrder['order'];
- }
- /**
- * 获取手机号会员信息
- */
- public function getVip(Request $request){
- $phone = $request->input('phone');
- $time = date('Y-m-d H:i:s');
- $customer_info = DB::table('customer_vip')->where('phone', $phone)->where('is_del', 0)->first();
- if(!empty($customer_info)){
- //判断是否过期
- if($customer_info->vip_end_time > $time){
- //未过期,1.获取优惠券,获取当月礼,获取生日礼,判断领取
- $coupons = DB::table('customer_coupons')->where('phone', $phone)->where('is_use', 0)->where('end_time', '>', $time)->first();
- $customer_info->coupons = !empty($coupons) ? 1:0;
- //判断是否领取当月礼
- $mtime = date('Y-m-01');
- $if_m_gift = DB::table('customer_gift_receives')->where('phone', $phone)->where('is_del',0)->where('gift_type', 0)->where('dtime', '>=', $mtime)->first();
- $customer_info->if_m_gift = 0;
- if(empty($if_m_gift)){
- $customer_info->if_m_gift = 1;
- }
-
- //判断生日礼
- $birth_time = date('m', strtotime($customer_info->birthday));
- $tom = date('m');
- $customer_info->if_b_gift = 0;
- if($tom == $birth_time){
- //生日期内,判断是否已领
- $if_b_gift = DB::table('customer_gift_receives')->where('phone', $phone)->where('is_del',0)->where('gift_type', 1)->where('dtime', '>=', $mtime)->first();
- if(empty($if_b_gift)){
- $customer_info->if_b_gift = 1;
- }
- }
- // exit(json_encode(['code'=>0, 'data'=>$customer_info]));
- echo json_encode(['code'=>0, 'data'=>$customer_info]);die;
- }
- // exit(json_encode(['code'=>1,'msg'=>'会员过期']));
- echo json_encode(['code'=>1,'msg'=>'会员过期']);die;
- }
- echo json_encode(['code'=>1,'msg'=>'不是会员']);die;
- }
- /**
- * 获取订单商品成本等信息
- */
- public function order_goods_info(Request $request){
- $id = $request->input('id');
- $is_fx = env('IS_FX');
- $data = OrderGoodsSkus::where('order_id', $id)->where('is_del', 0)->get();
- if(empty($data)) { echo '0';die; }
- $total = 0;
- foreach($data as $good){
- $good->goods_name = Goods::where('id', $good->goods_id)->pluck('name');
- $sku = GoodsSkus::select('code', 'propsName', 'referenceCost', 'outPrice')->where('id', $good->sku_id)->first();
- $good->props_name = $sku->propsName;
- $good->sku_code = $sku->code;
- if($good->cost == null){
- $good->cost = $is_fx == 0? $sku->referenceCost : $sku->outPrice;
- }
- $total += $good->cost * $good->num;
- }
- $total = round($total, 2);
- $res = array();
- $res['data'] = $data;
- $res['total'] = $total;
- echo json_encode($res);
- }
- /*分销退补售后列表*/
- public function customerServiceAfterSaleList(Request $request) {
- $order_id = $request->input('order_id');
- $name = $request->input('receiverName');
- $phone = $request->input('receiverMobile');
- $page = $request->input('page',1);
- $service_type = $request->input('service_type');
- $pageSize = 20;
- $offset = ($page - 1)*$pageSize;
- //总数据条数
- $count = DB::table('customer_service_after_sale_order')->where(function($query) use ($order_id, $name, $phone, $service_type) {
- if($order_id) $query->where('order_id',$order_id);
- if($name) $query->where('receiverName','like','%'.$name.'%');
- if($phone) $query->where('receiverMobile','like','%'.$phone.'%');
- if($service_type != 0) $query->where('service_type', $service_type);
- })->count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = DB::table('customer_service_after_sale_order')->where(function($query) use ($order_id, $name, $phone, $service_type) {
- if($order_id) $query->where('order_id',$order_id);
- if($name) $query->where('receiverName','like','%'.$name.'%');
- if($phone) $query->where('receiverMobile','like','%'.$phone.'%');
- if($service_type != 0) $query->where('service_type', $service_type);
- })->orderBy('id','desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result),true);
- foreach ($result as &$value) {
- $id = $value['order_id'];
- $orderInfo = DB::table('order')->select('receiverAddress','createTime','receivedAmount','logistics_id',
- 'payment_type','receiverState','receiverCity','receiverDistrict','receiverStreet','send_time')->where('id',$id)->where('is_del', 0)->first();
- $value['receiverState'] = isset($orderInfo->receiverState) ? $orderInfo->receiverState : '';
- $value['receiverCity'] = isset($orderInfo->receiverCity) ? $orderInfo->receiverCity : '';
- $value['receiverDistrict'] = isset($orderInfo->receiverDistrict) ? $orderInfo->receiverDistrict : '';
- $value['receiverStreet'] = isset($orderInfo->receiverStreet) ? $orderInfo->receiverStreet : '';
- $value['receiverAddress'] = isset($orderInfo->receiverAddress) ? $orderInfo->receiverAddress : '';
- $value['createTime'] = isset($orderInfo->createTime) ? $orderInfo->createTime : '';
- $value['receivedAmount'] = isset($orderInfo->receivedAmount) ? $orderInfo->receivedAmount : '';
- $value['logistics_id'] = isset($orderInfo->logistics_id) ? $orderInfo->logistics_id : '';
- $value['payment_type'] = isset($orderInfo->payment_type) ? $orderInfo->payment_type : 1;
- $value['send_time'] = isset($orderInfo->send_time) ? $orderInfo->send_time : '';
- $value['receiverMobile'] = substr($value['receiverMobile'], 0, 3).'****'.substr($value['receiverMobile'], 7);
- if($value['payment_type'] == 1){
- $value['payment_type'] = '微信支付';
- }elseif($value['payment_type'] == 2){
- $value['payment_type'] = '付款码支付';
- }elseif($value['payment_type'] == 3){
- $value['payment_type'] = '个体户支付';
- }elseif($value['payment_type'] == 4){
- $value['payment_type'] = '充值卡支付';
- }else{
- $value['payment_type'] = '';
- }
- }
- return view('order/customerServiceAfterSaleList',[
- 'page' => $page,
- 'pages' => $pages,
- 'count' => $count,
- 'result' => $result,
- 'order_id' => $order_id,
- 'receiverName' => $name,
- 'receiverMobile' => $phone,
- 'service_type' => $service_type,
- ]);
- }
- /*分销退补售后添加页*/
- public function customerServiceAfterSaleCreate(Request $request) {
- $order_id = $request->input('order_id');
- //查询该订单id是否已经存在
- $log = DB::table('customer_service_after_sale_order')->where('order_id',$order_id)->first();
- if($log) {
- if($log->service_type == 1) {
- $typeName = '退补';
- } else {
- $typeName = '售后';
- }
- return back()->with('info','该订单已'.$typeName);
- }
- //查询该订单id状态是否合法(满足售后退补条件)
- $info = DB::table('order')->where('id',$order_id)->where('is_del', 0)->first();
- if(!$info) {
- return back()->with('info','订单不存在');
- }
- $order = (array) $info;
- $self_role = session('role_name');
- $order['deposit_phone'] = $order['receiverMobile'];
- $order['receiverMobile'] = substr($order['receiverMobile'], 0, 3).'****'.substr($order['receiverMobile'], 7);
- $order['goods'] = '';
- $order_skus = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $order_id)->whereNull('gift_id')->lists('num','sku_id');
- $skus = array_keys($order_skus);
- if(!empty($skus)){
- $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();
- $goods = json_decode(json_encode($goods),true);
- foreach($goods as $k=>&$v){
- $v['num'] = $order_skus[$v['sku_id']];
- //计算实际库存 -还未到卖家云的量
- $quantity_redis = GoodsSkus::actualQuantityByRedis($v['sku_id']);
- $v['quantity'] = $quantity_redis['quantity'];
- $v['quantity'] = $v['quantity'] + $v['num'];
- $v['quantity_str'] = $v['quantity'];
- if($v['is_white'] == 1){
- $v['quantity_str'] = '虚拟库存';
- }
- }
- $order['goods'] = $goods;
- }
- $deposit_phone = $order['deposit_phone'];
- if($order['payment_type'] == 4){
- $consum = CustomerConsum::select('phone')->where('order_id', $order_id)->first();
- $deposit_phone = isset($consum->phone) ? $consum->phone : $order['deposit_phone'];
- }
- //下单当月礼品
- $mtime = date('Y-m-01', strtotime($order['create_time']));
- $gift_info = DB::table('customer_month_gift')->where('mtime', $mtime)->get();
- $gift_info = json_decode(json_encode($gift_info), true);
- $m_gift=array();
- $b_gift=array();
- foreach($gift_info as $k=>$val){
- //获取礼品信息
- $goods_name = Goods::where('id', $val['goods_id'])->pluck('name');
- $sku = GoodsSkus::where('id', $val['sku_id'])->where('is_del', 0)->where(function($query){
- $query->where('is_white', 1)->orWhere('quantity','>',0);
- })->first();
- if(empty($sku)) continue;
- //计算实际库存 -还未到卖家云的量
- $quantity = GoodsSkus::actualQuantityByRedis($val['sku_id']);
- $quantity = $quantity['quantity'];
- if($quantity<=0){
- continue;
- }
- $data = array();
- $data['id'] = $val['id'];
- $data['goods_name'] = $goods_name;
- $data['propsName'] = $sku['propsName'];
- $data['price'] = $sku['price'];
- if($val['type'] == 0){
- $m_gift[] = $data;
- }else{
- $b_gift[] = $data;
- }
- }
- #获取赠品信息
- $order_gifts = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $order_id)->where('gift_id','>',0)->lists('gift_id');
- $order_m_gifts = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 0)->lists('id');
- $order_b_gifts = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 1)->lists('id');
- #查询用户会员信息
- $cust_info = array();
- $cust_info['coupons'] = 0;
- $cust_info['if_m_gift'] = 0;
- $cust_info['if_b_gift'] = 0;
- $phone = $order['buyer_phone'];
- $time = date('Y-m-d H:i:s');
- $customer_info = DB::table('customer_vip')->where('phone', $phone)->where('is_del', 0)->first();
- if(!empty($customer_info) && $order['create_time']>'2019-11-28'){
- //判断是否过期
- if($customer_info->vip_end_time > $time){
- //未过期,1.获取优惠券,获取当月礼,获取生日礼,判断领取
- $coupons = DB::table('customer_coupons')->where('phone', $phone)->where('is_use', 0)->where('end_time', '>', $time)->first();
- $cust_info['coupons'] = !empty($coupons) ? 1:0;
- //判断是否领取当月礼
- $if_m_gift = DB::table('customer_gift_receives')->where('phone', $phone)->where('is_del',0)->where('gift_type', 0)->where('dtime', '>=', $mtime)->first();
- if(empty($if_m_gift)){
- $cust_info['if_m_gift'] = 1;
- }
- //判断生日礼
- $birth_time = date('m', strtotime($customer_info->birthday));
- $tom = date('m');
- if($tom==$birth_time){
- //生日期内,判断是否已领
- $if_b_gift = DB::table('customer_gift_receives')->where('phone', $phone)->where('is_del',0)->where('gift_type', 1)->where('dtime', '>=', $mtime)->first();
- if(empty($if_b_gift)){
- $cust_info['if_b_gift'] = 1;
- }
- }
- }
- }
- return view('order/customerServiceAfterSaleCreate',[
- 'order' => $order,
- 'deposit_phone' => $deposit_phone,
- 'self_role' => $self_role,
- 'm_gift' => $m_gift,
- 'b_gift' => $b_gift,
- 'order_m_gifts' => $order_m_gifts,
- 'order_b_gifts' => $order_b_gifts,
- 'cust_info' => $cust_info,
- 'warehouseArr' => $this->warehouse,
- ]);
- }
- /*分销退补售后添加*/
- public function customerServiceAfterSaleStore(Request $request) {
- $data['order_id'] = $request->input('order_id');
- $data['service_type'] = $request->input('service_type');
- $data['receiverMobile'] = $request->input('receiverMobile');
- $data['receiverName'] = $request->input('receiverName');
- //商品信息
- $skus = $request->input('skus');
- $gnum = $request->input('gnum');
- $oldnums = $request->input('oldnums');
- $goodsInfo = array();
- if(!empty($skus)){
- foreach($skus as $k=>$sku_id){
- $sku_info = GoodsSkus::where('id', $sku_id)->first();
- $goods_info = Goods::where('id', $sku_info->goodsCode)->first();
- $params = array();
- $params['goods_code'] = $sku_info->goodsCode;// strstr($goods->code,'-',true);
- $params['sku_id'] = $sku_id;
- #商品名称
- $params['goods_name'] = $goods_info->name;
- $params['properties_name'] = $sku_info->propsName;
- //$params['goodsName'] = '海钓黄翅鱼';//$goods->code;
- $params['price'] = $sku_info->price;
- $params['sku_code'] = $sku_info->code;
- $params['old_quantity'] = $oldnums[$k];
- $params['new_quantity'] = $gnum[$k];
- $goodsInfo[] = $params;
- #新逻辑,c仓,判断实际库存足否
- // if($order['warehouse'] == 3 && $sku_info->is_white != 1){
- // $quantity_redis = GoodsSkus::actualQuantityByRedis($sku_id);
- // $act_quantity = $quantity_redis['quantity'];
- // if($act_quantity < $params['quantity']){
- // exit('<script>alert("商品库存不足");history.back()</script>');
- // //return redirect('/admin/order/index')->with('info', '商品库存不足');
- // }
- // }
- }
- }
- $data['goods_info'] = json_encode($goodsInfo,JSON_UNESCAPED_UNICODE);
- $res = DB::table('customer_service_after_sale_order')->insertGetId($data);
- if($res > 0) {
- #记录操作日志
- $self_id = session('admin_id');
- $self_name = session('real_name');
- $context = "商品信息:".$data['goods_info'];
- $type = 0; //订单录入
- $tables = 'customer_service_after_sale_order';
- $data_id = $res;
- Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
- return redirect('/admin/customerServiceAfterSale/index')->with('info','添加成功');
- }
- return back()->with('info','添加失败');
- }
- /*分销退补售后编辑页*/
- public function customerServiceAfterSaleEdit(Request $request) {
- $id = $request->input('id');
- //查询订单信息
- $customerServiceAfterSale = DB::table('customer_service_after_sale_order')->where('id',$id)->first();
- $order_id = $customerServiceAfterSale->order_id;
- $order = (array)DB::table('order')->where('id',$order_id)->where('is_del', 0)->first();
- $goodsInfo = json_decode($customerServiceAfterSale->goods_info,true);
- $order['service_type'] = $customerServiceAfterSale->service_type;
- $self_role = session('role_name');
- $order['deposit_phone'] = $order['receiverMobile'];
- $order['receiverMobile'] = substr($order['receiverMobile'], 0, 3).'****'.substr($order['receiverMobile'], 7);
- $order['goods'] = '';
- $orderGoods = array();
- foreach ($goodsInfo as $item) {
- $orderGoods[$item['sku_id']] = $item;
- }
- $skus = array_column($goodsInfo,'sku_id');
- if(!empty($skus)){
- $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();
- $goods = json_decode(json_encode($goods),true);
- foreach($goods as $k=>&$v){
- $v['num'] = $orderGoods[$v['sku_id']]['old_quantity'];
- $v['new_num'] = $orderGoods[$v['sku_id']]['new_quantity'];
- //计算实际库存 -还未到卖家云的量
- $quantity_redis = GoodsSkus::actualQuantityByRedis($v['sku_id']);
- $v['quantity'] = $quantity_redis['quantity'];
- $v['quantity'] = $v['quantity'] + $v['num'];
- $v['quantity_str'] = $v['quantity'];
- if($v['is_white'] == 1){
- $v['quantity_str'] = '虚拟库存';
- }
- }
- $order['goods'] = $goods;
- }
- $deposit_phone = $order['deposit_phone'];
- if($order['payment_type'] == 4){
- $consum = CustomerConsum::select('phone')->where('order_id', $order_id)->first();
- $deposit_phone = isset($consum->phone) ? $consum->phone : $order['deposit_phone'];
- }
- //下单当月礼品
- $mtime = date('Y-m-01', strtotime($order['create_time']));
- $gift_info = DB::table('customer_month_gift')->where('mtime', $mtime)->get();
- $gift_info = json_decode(json_encode($gift_info), true);
- $m_gift=array();
- $b_gift=array();
- foreach($gift_info as $k=>$val){
- //获取礼品信息
- $goods_name = Goods::where('id', $val['goods_id'])->pluck('name');
- $sku = GoodsSkus::where('id', $val['sku_id'])->where('is_del', 0)->where(function($query){
- $query->where('is_white', 1)->orWhere('quantity','>',0);
- })->first();
- if(empty($sku)) continue;
- //计算实际库存 -还未到卖家云的量
- $quantity = GoodsSkus::actualQuantityByRedis($val['sku_id']);
- $quantity = $quantity['quantity'];
- if($quantity<=0){
- continue;
- }
- $data = array();
- $data['id'] = $val['id'];
- $data['goods_name'] = $goods_name;
- $data['propsName'] = $sku['propsName'];
- $data['price'] = $sku['price'];
- if($val['type'] == 0){
- $m_gift[] = $data;
- }else{
- $b_gift[] = $data;
- }
- }
- #获取赠品信息
- $order_gifts = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $order_id)->where('gift_id','>',0)->lists('gift_id');
- $order_m_gifts = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 0)->lists('id');
- $order_b_gifts = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 1)->lists('id');
- #查询用户会员信息
- $cust_info = array();
- $cust_info['coupons'] = 0;
- $cust_info['if_m_gift'] = 0;
- $cust_info['if_b_gift'] = 0;
- $phone = $order['buyer_phone'];
- $time = date('Y-m-d H:i:s');
- $customer_info = DB::table('customer_vip')->where('phone', $phone)->where('is_del', 0)->first();
- if(!empty($customer_info) && $order['create_time']>'2019-11-28'){
- //判断是否过期
- if($customer_info->vip_end_time > $time){
- //未过期,1.获取优惠券,获取当月礼,获取生日礼,判断领取
- $coupons = DB::table('customer_coupons')->where('phone', $phone)->where('is_use', 0)->where('end_time', '>', $time)->first();
- $cust_info['coupons'] = !empty($coupons) ? 1:0;
- //判断是否领取当月礼
- $if_m_gift = DB::table('customer_gift_receives')->where('phone', $phone)->where('is_del',0)->where('gift_type', 0)->where('dtime', '>=', $mtime)->first();
- if(empty($if_m_gift)){
- $cust_info['if_m_gift'] = 1;
- }
- //判断生日礼
- $birth_time = date('m', strtotime($customer_info->birthday));
- $tom = date('m');
- if($tom==$birth_time){
- //生日期内,判断是否已领
- $if_b_gift = DB::table('customer_gift_receives')->where('phone', $phone)->where('is_del',0)->where('gift_type', 1)->where('dtime', '>=', $mtime)->first();
- if(empty($if_b_gift)){
- $cust_info['if_b_gift'] = 1;
- }
- }
- }
- }
- return view('order/customerServiceAfterSaleEdit',[
- 'order' => $order,
- 'deposit_phone' => $deposit_phone,
- 'self_role' => $self_role,
- 'm_gift' => $m_gift,
- 'b_gift' => $b_gift,
- 'order_m_gifts' => $order_m_gifts,
- 'order_b_gifts' => $order_b_gifts,
- 'cust_info' => $cust_info,
- 'id' => $id,
- 'warehouseArr' => $this->warehouse,
- ]);
- }
- /*分销退补售后编辑*/
- public function customerServiceAfterSaleUpdate(Request $request) {
- $data['service_type'] = $request->input('service_type');
- $id = $request->input('id');
- //商品信息
- $skus = $request->input('skus');
- $gnum = $request->input('gnum');
- $oldnums = $request->input('oldnums');
- $goodsInfo = array();
- if(!empty($skus)){
- foreach($skus as $k=>$sku_id){
- $sku_info = GoodsSkus::where('id', $sku_id)->first();
- $goods_info = Goods::where('id', $sku_info->goodsCode)->first();
- $params = array();
- $params['goods_code'] = $sku_info->goodsCode;// strstr($goods->code,'-',true);
- $params['sku_id'] = $sku_id;
- #商品名称
- $params['goods_name'] = $goods_info->name;
- $params['properties_name'] = $sku_info->propsName;
- //$params['goodsName'] = '海钓黄翅鱼';//$goods->code;
- $params['price'] = $sku_info->price;
- $params['sku_code'] = $sku_info->code;
- $params['old_quantity'] = $oldnums[$k];
- $params['new_quantity'] = $gnum[$k];
- $goodsInfo[] = $params;
- #新逻辑,c仓,判断实际库存足否
- // if($order['warehouse'] == 3 && $sku_info->is_white != 1){
- // $quantity_redis = GoodsSkus::actualQuantityByRedis($sku_id);
- // $act_quantity = $quantity_redis['quantity'];
- // if($act_quantity < $params['quantity']){
- // exit('<script>alert("商品库存不足");history.back()</script>');
- // //return redirect('/admin/order/index')->with('info', '商品库存不足');
- // }
- // }
- }
- }
- $data['goods_info'] = json_encode($goodsInfo,JSON_UNESCAPED_UNICODE);
- $res = DB::table('customer_service_after_sale_order')->where('id',$id)->update($data);
- if($res) {
- #记录操作日志
- $self_id = session('admin_id');
- $self_name = session('real_name');
- $context = "商品信息:".$data['goods_info'];
- $type = 1; //订单录入
- $tables = 'customer_service_after_sale_order';
- $data_id = $id;
- Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
- return redirect('/admin/customerServiceAfterSale/index')->with('info','修改成功');
- }
- return back()->with('info','修改失败');
- }
- /**
- * 获取订单商品成本等信息
- */
- public function orderGoodsInfo(Request $request){
- $id = $request->input('id');
- $is_fx = env('IS_FX');
- $data = DB::table('customer_service_after_sale_order')->where('id', $id)->first();
- if(empty($data)) { echo '0';die; }
- $new_total = 0;
- $old_total = 0;
- $data = json_decode($data->goods_info,true);
- foreach($data as &$good){
- $sku = GoodsSkus::select('code', 'propsName', 'referenceCost', 'outPrice')->where('id', $good['sku_id'])->first();
- $good['cost'] = $is_fx == 0? $sku->referenceCost : $sku->outPrice;
- $new_total += $good['cost'] * $good['new_quantity'];
- $old_total += $good['cost'] * $good['old_quantity'];
- }
- $new_total = round($new_total, 2);
- $old_total = round($old_total,2);
- $res = array();
- $res['data'] = $data;
- $res['new_total'] = $new_total;
- $res['old_total'] = $old_total;
- echo json_encode($res);
- }
- }
|