Sin descripción

OrderController.php 149KB

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