12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Apiistrator
- * Date: 2018-02-22
- * Time: 10:32
- */
- namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Controller;
- use Illuminate\Http\Request;
- use App\Log;
- use App\Admin;
- use App\Order;
- use App\Oplog;
- use App\RedisModel;
- use App\Goods;
- use App\GoodsSkus;
- use App\OrderGoodsSkus;
- use App\CustomerInfo;
- use App\CustomerConsum;
- use Illuminate\Support\Facades\DB;
- class OrderController extends Controller {
- /**
- * 订单列表
- */
- public function orderList(Request $request){
- $admin_id = (int)$request->input('admin_id');
- $self_role = DB::table('admin_role')->where('user_id', $admin_id)->pluck('role_name');
- $team_id = null;
- if($self_role == '管理员'){
- $team_id = Admin::where('id', $admin_id)->pluck('team_id');
- $admin_id = null;
- }
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $count = Order::where('is_del', 0)->where('warehouse', 3)->where(function($query) use($admin_id, $team_id){
- if($admin_id>0) $query->where('admin_id', $admin_id);
- if($team_id>0) $query->where('team_id', $team_id);
- })->count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = Order::select('id', 'status', 'orderCode', 'customerName', 'receiverName', 'receiverMobile', 'receiverState','receiverCity','receiverDistrict','receiverAddress','receivedAmount','buyerMemo','sellerMemo','createTime','admin_id','admin_name','cost', 'logistics_id','is_refund','warehouse','delivery_date','refund_price','refund_type','send_note','freight_cost','payment_type','refuse_reason','refund_note','verify_time','aftersale_fee','aftersale_reason','aftersale_time','refund_time','send_time','should_amount', 'buyer_phone', 'use_coupon', 'use_m_gift', 'use_b_gift')->where('is_del', 0)->where('warehouse', 3)->where(function($query) use($admin_id, $team_id){
- if($admin_id>0) $query->where('admin_id', $admin_id);
- if($team_id>0) $query->where('team_id', $team_id)->where('status','>',0);
- })->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result),true);
-
- $status_arr = array(
- 0 => '已录入',
- 1 => '待审核',
- 2 => '已审核',
- 3 => '已发货',
- 4 => '已退款'
- );
- foreach($result as $k=>&$v){
- $goods = DB::table('order_goods_skus')->where('order_id', $v['id'])->where('is_del', 0)->orderBy('gift_id', 'asc')->get();
- if(!empty($goods)){
- foreach($goods as &$item){
- $item->goods_name = Goods::where('id', $item->goods_id)->pluck('name');
- $item->picUrl = Goods::where('id', $item->goods_id)->pluck('picUrl');
- $goods_sku_info = GoodsSkus::select('propsName', 'is_weigh')->where('id', $item->sku_id)->first();
- if(!empty($goods_sku_info)){
- $item->props_name = $goods_sku_info->propsName;
- $item->is_weigh = $goods_sku_info->is_weigh;
- }else{
- $item->props_name = '';
- $item->is_weigh = 0;
- }
- $item->gift_type = null;
- if($item->gift_id>0){
- $gift_flag = DB::table('customer_month_gift')->where('id', $item->gift_id)->first();
- if($gift_flag->type==1){
- $item->gift_type = 1;
- }else{
- $item->gift_type = 0;
- }
- }
-
- }
- }
- $goods = json_decode(json_encode($goods), true);
- $v['sku_count'] = count($goods);
- $goods = array_slice($goods, 0, 2);
- $v['skus'] = $goods;
-
- #加粉时间
- $fanTime = DB::table('customers')->select('fanTime')->where('phone', $v['receiverMobile'])->first();
- $v['fanTime'] = isset($fanTime->fanTime) ? $fanTime->fanTime : '';
- //$v['receiverMobile'] = substr($v['receiverMobile'], 0, 3).'****'.substr($v['receiverMobile'], 7);
- $v['status_name'] = $status_arr[$v['status']];
- // 代金券
- if($v['use_coupon'] == 1){
- $v['coupon_id'] = DB::table('customer_coupons')->where('order_id', $v['id'])->where('is_use', 1)->pluck('id');
- }
- }
- $pageInfo = [
- 'page' => $page,
- 'total' => $count,
- 'pageSize' => $pageSize,
- 'pages' => $pages,
- ];
- return self::returnValue(['data'=>$result, 'page_info'=>$pageInfo]);
- }
- /**
- * 分组管理-进行添加操作
- * @param Request $request
- * @return \Illuminate\Http\RedirectResponse
- */
- public function orderstore(Request $request)
- {
- $admin_id = (int)$request->input('admin_id');
- if(empty($admin_id)){
- return self::returnValue(['msg'=>'登录过期'], 2000);
- }
- $buyer_phone = $request->input('buyer_phone');
- if(!preg_match('/^1\d{10}$/', $buyer_phone)){
- return self::returnValue(['msg'=>'买家手机号格式有误'], 2000);
- }
- $phone = $request->input('receiverMobile');
- if(!preg_match('/^1\d{10}$/', $phone)){
- return self::returnValue(['msg'=>'收货人手机号格式有误'], 2000);
- }
- $customerName = $request->input('customerName');
- if(empty($customerName)){
- return self::returnValue(['msg'=>'买家姓名不能为空'], 2000);
- }
- $receiverName = $request->input('receiverName');
- if(empty($receiverName)){
- return self::returnValue(['msg'=>'收货人姓名不能为空'], 2000);
- }
- $receiverState = $request->input('receiverState');
- if(empty($receiverState)){
- return self::returnValue(['msg'=>'省份不能为空'], 2000);
- }
- $receiverCity = $request->input('receiverCity');
- if(empty($receiverCity)){
- return self::returnValue(['msg'=>'市不能为空'], 2000);
- }
- $receiverAddress = $request->input('receiverAddress');
- if(empty($receiverAddress)){
- return self::returnValue(['msg'=>'详细地址不能为空'], 2000);
- }
- $delivery_date = $request->input('delivery_date');
- if(empty($delivery_date)){
- return self::returnValue(['msg'=>'预发货时间不能为空'], 2000);
- }
- $receivedAmount = $request->input('receivedAmount');
- if(empty($receivedAmount) || !is_numeric($receivedAmount) ){
- return self::returnValue(['msg'=>'实付金额不能为空且必须为数字'], 2000);
- }
- $skus = $request->input('skus');
- if(empty($skus)){
- return self::returnValue(['msg'=>'必须添加商品'], 2000);
- }
- $payment_type = (int)$request->input('payment_type');
- if(empty($payment_type)){
- return self::returnValue(['msg'=>'必选支付方式'], 2000);
- }
- /*
- $redisKey = 'seafood.order_phone_add.'.$phone;
- $res = RedisModel::setnx($redisKey, 1);
- if( $res == 0 ){
- $msg = '操作过于频繁,请一分钟后继续操作';
- return self::returnValue(['msg'=>$msg], 2000);
- }
- $res = RedisModel::expire( $redisKey, 60 );
- */
- //数据库-新增数据
- $order = array();
- $order['eshopCode'] = config('constants.ESHOP_CODE'); //默认
- $order['outerCode'] = Order::createOuterCode();//随机生成
- $order['status'] = (int)$request->input('status');
- $order['customerName'] = trim($request->input('customerName')); //买家名称
- $order['shippingType'] = '卖家包邮';
- $order['expressCompanyCode'] = '10084';
- $order['expressCompanyName'] = '顺丰快递';
- $order['receiverName'] = trim($request->input('receiverName')); //收件人
- $order['receiverMobile'] = trim($request->input('receiverMobile')); //收件人手机
- $order['receiverPhone'] = trim($request->input('receiverMobile')); //收件人手机
- $order['receiverState'] = trim($request->input('receiverState')); //省份
- $order['receiverCity'] = trim($request->input('receiverCity')); //市
- $order['receiverDistrict'] = trim($request->input('receiverDistrict')); //市
- #省市格式处理
- $order['receiverState'] = str_replace(['省', '市'], '', $order['receiverState']);
- $order['receiverCity'] = str_replace('市', '', $order['receiverCity']);
- $order['receiverDistrict'] = trim($request->input('receiverDistrict')); //区
- $order['receiverStreet'] = trim($request->input('receiverStreet')); //街道
- $order['receiverAddress'] = trim($request->input('receiverAddress')); //详细地址
- $order['buyerPostageFee'] = 0; //买家邮费
- $order['discountFee'] = 0; //订单优惠
- $order['adjustFee'] = 0; //订单调价
- $order['serviceFee'] = 0; //服务费
- $order['paymentMethod'] = '在线付款';
- $order['paymentType'] = '网银';
- $order['paymentAccount'] = '18322225555'; // 付款账号,暂时默认写死
- $order['paymentTime'] = date('Y-m-d H:i:s'); // 付款时间
- $order['receivedAmount'] = trim($request->input('receivedAmount')); //付款金额
- $order['buyerMemo'] = trim($request->input('buyerMemo', '')); // 买家备注
- $order['sellerMemo'] = trim($request->input('sellerMemo', '')); // 卖家备注
- $order['createTime'] = date('Y-m-d H:i:s'); // 订单创建时间
- $order['modifyTime'] = date('Y-m-d H:i:s'); // 订单修改时间
- $order['should_amount'] = $request->input('should_amount');
- if(!$order['should_amount']) $order['should_amount'] = null;
- //商品信息
- $skus = $request->input('skus');
- $order_goods = array();
- if(!empty($skus)){
- foreach($skus as $k=>$sku){
- $sku_id = $sku['sku_id'];
- $num = $sku['num'];
- $sku_info = GoodsSkus::where('id', $sku_id)->first();
- $goods_info = Goods::where('id', $sku_info->goodsCode)->first();
- $params = array();
- $params['outerCode'] = $order['outerCode']+$k+1; //外部平台中子订单编码
- $params['outerParentCode'] = $order['outerCode']; //外部平台中订单编码
- $params['goodsCode'] = $sku_info->goodsCode;// strstr($goods->code,'-',true);
- $params['skuCode'] = $sku_id;
- #商品名称
- $params['goodsName'] = $goods_info->name;
- $params['propertiesName'] = $sku_info->propsName;
- $params['price'] = $sku_info->price;
-
- $params['quantity'] = $num;
- $params['discountFee'] = 0;
- $params['adjustFee'] = 0;
- $params['refundStatus'] = '正常';
- $order_goods[] = $params;
- }
- }
- //本系统订单额外参数
- $order['warehouse'] = 3; //仓库
- $order['delivery_date'] = !empty($request->input('delivery_date')) ? $request->input('delivery_date') : date('Y-m-d'); //发货日期
- $order['payment_type'] = (int)$request->input('payment_type'); //支付方式
- $order['buyer_phone'] = $request->input('buyer_phone');
- $coupon_id = (int)$request->input('coupon_id');
- if($coupon_id>0){
- if($order['payment_type'] == 4){
- return self::returnValue(['msg'=>'优惠券和充值卡不能同时使用'], 2000);
- }
- $order['use_coupon'] = 1;
- }
- $order['use_b_gift'] = (int)$request->input('use_b_gift');
- $m_gift_id = (int)$request->input('m_gift_id');
- if($m_gift_id>0){
- $order['use_m_gift'] = 1;
- }
-
- //充值卡验证余额
- if($order['payment_type'] == 4){
- $deposit_phone = $request->input('deposit_phone');
- $customerInfo = CustomerInfo::where('phone', $deposit_phone)->first();
- $balance = isset($customerInfo->balance) ? $customerInfo->balance : 0;
- if($balance < $order['receivedAmount']){
- $msg = '余额不足';
- return self::returnValue(['msg'=>$msg], 2003);
- }
- }
- $admin_id = (int)$request->input('admin_id');
- DB::beginTransaction();
- try{
- #更新客户表
- $customer = array();
- $customers['name'] = $order['receiverName'];
- $customers['receiverState'] = $order['receiverState'];
- $customers['receiverCity'] = $order['receiverCity'];
- $customers['receiverDistrict'] = $order['receiverDistrict'];
- $customers['receiverStreet'] = $order['receiverStreet'];
- $customers['receiverAddress'] = $order['receiverAddress'];
- $customers['buyerMobile'] = $order['buyer_phone'];
- $customers['buyerName'] = $order['customerName'];
- $if_e = DB::table('customers')->select('id', 'fanTime')->where('phone', $order['receiverMobile'])->first();
- if(isset($if_e->id)){
- $customers['is_fugou'] = 1;
- $order['is_fugou'] = 1;
- $up_c = DB::table('customers')->where('id', $if_e->id)->update($customers);
- }else{
- $customers['phone'] = $order['receiverMobile'];
- $customers['is_fugou'] = 0;
- $customers['fanTime'] = $request->input('fanTime');
- $customers['admin_id'] = $admin_id;
- $in_c = DB::table('customers')->insert($customers);
- }
-
- $admin_info = DB::table('admin')->select('realname', 'team_id')->where('id', $admin_id)->first();
- $order['admin_id'] = $admin_id;
- $order['admin_name'] = $admin_info->realname;
- $order['team_id'] = $admin_info->team_id;
-
- //新加is_able 9.4之前粉丝不参与销售复购统计
- $fanTime = $request->input('fanTime');
- if(!$fanTime && isset($if_e->id)) $fanTime = $if_e->fanTime;
- if($fanTime < '2019-09-04'){
- $order['is_able'] = 0;
- }
- $res = DB::table('order')->insertGetId($order);
- if($res){
- if(!empty($skus)){
- //订单商品插入
- $order_skus = array();
- foreach($order_goods as $k=>$sku){
- $order_skus[$k]['sku_id'] = $sku['skuCode'];
- $order_skus[$k]['order_id'] = $res;
- $order_skus[$k]['goods_id'] = $sku['goodsCode'];
- $order_skus[$k]['num'] = $sku['quantity'];
- $order_skus[$k]['price'] = $sku['price'];
- }
- $order_skus_insert = DB::table('order_goods_skus')->insert($order_skus);
- }
- #加赠品逻辑
- $use_b_gift = (int)$request->input('use_b_gift');
- if($coupon_id > 0){
- $coupon = DB::table('customer_coupons')->where('phone', $order['buyer_phone'])->where('id', $coupon_id)->where('is_use',0)->first();
- if(!empty($coupon)){
- $cup = array();
- $cup['is_use'] = 1;
- $cup['use_time'] = date('Y-m-d H:i:s');
- $cup['order_id'] = $res;
- DB::table('customer_coupons')->where('id', $coupon->id)->update($cup);
- }
- }
- if($m_gift_id>0){
- $m_gift = DB::table('customer_month_gift')->where('id', $m_gift_id)->where('type', 0)->first();
- $order_m_gift = array();
- $m_sku = GoodsSkus::where('id', $m_gift->sku_id)->first();
- $order_m_gift['sku_id'] = $m_gift->sku_id;
- $order_m_gift['order_id'] = $res;
- $order_m_gift['goods_id'] = $m_sku->goodsCode;
- $order_m_gift['num'] = $m_gift->num;
- $order_m_gift['price'] = $m_sku->price;
- $order_m_gift['gift_id'] = $m_gift_id;
- $order_gift_insert = DB::table('order_goods_skus')->insert($order_m_gift);
- #记录领取
- $receive_gift = array();
- $receive_gift['phone'] = $order['buyer_phone'];
- $receive_gift['dtime'] = date('Y-m-d');
- $receive_gift['order_id'] = $res;
- $receive_gift['gift_id'] = $m_gift_id;
- $receive_gift['gift_type'] = 0;
- DB::table('customer_gift_receives')->insert($receive_gift);
- }
- $mdate = date('Y-m-01');
- if($use_b_gift>0){
- $b_gifts = DB::table('customer_month_gift')->where('mtime', $mdate)->where('type', 1)->get();
- $b_gifts = json_decode(json_encode($b_gifts),true);
- foreach($b_gifts as $b_gift){
- $order_b_gift = array();
- $b_sku = GoodsSkus::where('id', $b_gift['sku_id'])->first();
- $order_b_gift['sku_id'] = $b_gift['sku_id'];
- $order_b_gift['order_id'] = $res;
- $order_b_gift['goods_id'] = $b_sku->goodsCode;
- $order_b_gift['num'] = $b_gift['num'];
- $order_b_gift['price'] = $b_sku->price;
- $order_b_gift['gift_id'] = $b_gift['id'];
- $order_gift_insert = DB::table('order_goods_skus')->insert($order_b_gift);
- }
- #记录领取
- $receive_gift = array();
- $receive_gift['phone'] = $order['buyer_phone'];
- $receive_gift['dtime'] = date('Y-m-d');
- $receive_gift['order_id'] = $res;
- $receive_gift['gift_type'] = 1;
- DB::table('customer_gift_receives')->insert($receive_gift);
- }
- #新加逻辑,若付款方式为充值卡/消费记录
- if($order['payment_type'] == 4){
- $consum = new CustomerConsum();
- $consum->phone = $deposit_phone;
- $consum->order_id = $res;
- $consum->money = $order['receivedAmount'];
- $consum->save();
- //更新余额
- $customerInfo->balance = $customerInfo->balance - $order['receivedAmount'];
- $customerInfo->save();
- }
- #记录操作日志
- $self_id = $admin_id;
- $self_name = $admin_info->realname;
- if($order['status'] == 0){
- $status_text = '仅录入';
- }else{
- $status_text = '录入并提审';
- }
- $context = "录入订单:".$status_text;
- $type = 0; //订单录入
- $tables = 'order';
- $data_id = $res;
- Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
- }
-
- DB::commit();
- }catch (Exception $e){
- DB::rollback();
- }
- return self::returnValue(['msg'=>'添加成功']);
-
- }
- /**
- * 根据手机号获取默认地址信息
- *
- */
- public function getAddress(Request $request){
- $phone = $request->input('phone');
- if(!preg_match('/^1\d{10}$/', $phone)){
- return self::returnValue([],2000);
- }
- $result = DB::table('customers')->select('receiverState', 'receiverCity', 'receiverDistrict', 'receiverAddress', 'fanTime')->where('phone', $phone)->first();
- $result = json_decode(json_encode($result), true);
- return self::returnValue(['data'=>$result]);
- }
- //获余额
- public function getBalance(Request $request){
- $phone = $request->input('phone');
- if(!preg_match('/^1\d{10}$/', $phone)){
- return self::returnValue([],2000);
- }
- $result = CustomerInfo::where('phone', $phone)->pluck('balance');
- if(empty($result)) $result = 0;
- return self::returnValue(['balance'=>$result]);
- }
- /**
- * 分组管理-进行编辑操作
- * @param Request $request
- * @return \Illuminate\Http\RedirectResponse
- */
- public function orderupdate(Request $request)
- {
- $admin_id = (int)$request->input('admin_id');
- if(empty($admin_id)){
- return self::returnValue(['msg'=>'登录过期'], 2000);
- }
- $id = (int)$request->input('id');
- if(empty($id)){
- return self::returnValue(['msg'=>'订单id有误'], 2000);
- }
- $phone = $request->input('receiverMobile');
- if(!preg_match('/^1\d{10}$/', $phone)){
- return self::returnValue(['msg'=>'手机号格式有误'], 2000);
- }
- $customerName = $request->input('customerName');
- if(empty($customerName)){
- return self::returnValue(['msg'=>'买家姓名不能为空'], 2000);
- }
- $receiverName = $request->input('receiverName');
- if(empty($receiverName)){
- return self::returnValue(['msg'=>'收货人姓名不能为空'], 2000);
- }
- $receiverState = $request->input('receiverState');
- if(empty($receiverState)){
- return self::returnValue(['msg'=>'省份不能为空'], 2000);
- }
- $receiverCity = $request->input('receiverCity');
- if(empty($receiverCity)){
- return self::returnValue(['msg'=>'市不能为空'], 2000);
- }
- $receiverAddress = $request->input('receiverAddress');
- if(empty($receiverAddress)){
- return self::returnValue(['msg'=>'详细地址不能为空'], 2000);
- }
- $delivery_date = $request->input('delivery_date');
- if(empty($delivery_date)){
- return self::returnValue(['msg'=>'预发货时间不能为空'], 2000);
- }
- $receivedAmount = $request->input('receivedAmount');
- if(empty($receivedAmount) || !is_numeric($receivedAmount) ){
- return self::returnValue(['msg'=>'实付金额不能为空且必须为数字'], 2000);
- }
- $skus = $request->input('skus');
- if(empty($skus)){
- return self::returnValue(['msg'=>'必须添加商品'], 2000);
- }
- $payment_type = (int)$request->input('payment_type');
- if(empty($payment_type)){
- return self::returnValue(['msg'=>'必选支付方式'], 2000);
- }
-
- //更新数据
- $order = array();
- $order['customerName'] = trim($request->input('customerName')); //买家名称
- $order['receiverName'] = trim($request->input('receiverName')); //收件人
- if(preg_match('/^1\d{10}$/', $phone)) $order['receiverMobile'] = $phone; //收件人手机
- $order['receiverState'] = trim($request->input('receiverState')); //省份
- $order['receiverCity'] = trim($request->input('receiverCity')); //市
-
- $order['receiverState'] = str_replace(['省', '市'], '', $order['receiverState']);
- $order['receiverCity'] = str_replace('市', '', $order['receiverCity']);
- $order['receiverDistrict'] = trim($request->input('receiverDistrict')); //区
- $order['receiverAddress'] = trim($request->input('receiverAddress')); //详细地址
- $order['receivedAmount'] = trim($request->input('receivedAmount')); //付款金额
- $order['buyerMemo'] = trim($request->input('buyerMemo', '')); // 买家备注
- $order['sellerMemo'] = trim($request->input('sellerMemo', '')); // 卖家备注
- $order['delivery_date'] = !empty($request->input('delivery_date')) ? $request->input('delivery_date') : date('Y-m-d'); //发货日期
- $order['payment_type'] = (int)$request->input('payment_type'); //支付方式
- $order['use_coupon'] = 0;
- $coupon_id = (int)$request->input('coupon_id');
- if($coupon_id>0){
- if($order['payment_type'] == 4){
- return self::returnValue(['msg'=>'充值卡不能和优惠券同时使用'], 2000);
- }
- $order['use_coupon'] = 1;
- }
- $order['buyer_phone'] = $request->input('buyer_phone');
- $order['use_b_gift'] = (int)$request->input('use_b_gift');
- $m_gift_id = (int)$request->input('m_gift_id');
- if($m_gift_id>0){
- $order['use_m_gift'] = 1;
- }
- $order['status'] = (int)$request->input('status');
- $order['should_amount'] = $request->input('should_amount');
- if(!$order['should_amount']) $order['should_amount'] = null;
-
- $id = (int)$request->input('id');
- //获取订单原信息
- $old_order = DB::table('order')->where('id', $id)->first();
- // ++++++充值卡验证余额逻辑 +++++++ //
- if($order['payment_type'] == 4){
- if($old_order->payment_type != 4){
- $deposit_phone = $request->input('deposit_phone');
- $customerInfo = CustomerInfo::where('phone', $deposit_phone)->first();
- $balance = isset($customerInfo->balance) ? $customerInfo->balance : 0 ;
- if($balance < $order['receivedAmount']){
- //余额不足
- return self::returnValue(['msg'=>'充值卡余额不足'], 2003);
- }
- }elseif($old_order->receivedAmount != $order['receivedAmount']){
- $deposit_phone = CustomerConsum::where('order_id', $id)->where('is_del', 0)->where('type', 0)->pluck('phone');
- $customerInfo = CustomerInfo::where('phone', $deposit_phone)->first();
- $balance = $customerInfo->balance;
- if( $balance < $order['receivedAmount']-$old_order->receivedAmount ){
- //余额不足
- return self::returnValue(['msg'=>'充值卡余额不足'], 2003);
- }
- }
- }
- $today_date = date('Y-m-d');
- DB::beginTransaction();
- try{
- $res = DB::table('order')->where('id', $id)->update($order);
- //商品信息
- $skus = $request->input('skus');
- $sku_ids = array_column($skus, 'sku_id');
- //先删除
- $del_re = DB::table('order_goods_skus')->where('order_id', $id)->whereNotIn('sku_id', $sku_ids)->update(['is_del'=>1]);
- if(!empty($skus)){
- //再同步
- foreach($skus as $k=>$sku){
- $sku_id = $sku['sku_id'];
- $num = $sku['num'];
- $sku_info = GoodsSkus::select('goodsCode', 'price', 'propsName','is_weigh')->where('id', $sku_id)->first();
- $sync_re = OrderGoodsSkus::updateOrCreate(['order_id'=>$id, 'sku_id'=>$sku_id, 'goods_id'=>$sku_info->goodsCode], ['num'=>$num, 'is_del'=>0, 'price'=>$sku_info->price]);
- }
- }
-
- if($res){
- #新加逻辑,若付款方式为充值卡/消费记录
- if($order['payment_type'] == 4 ){
- if($old_order->payment_type != 4 ){
- $consum = new CustomerConsum();
- $consum->phone = $deposit_phone;
- $consum->order_id = $id;
- $consum->money = $order['receivedAmount'];
- $consum->save();
- //更新余额
- $customerInfo->balance = $customerInfo->balance - $order['receivedAmount'];
- $customerInfo->save();
- }elseif($old_order->receivedAmount != $order['receivedAmount']){
- $consum = CustomerConsum::where('order_id', $id)->first();
- $consum->money = $order['receivedAmount'];
- $consum->save();
- //更新余额
- $add_amount = $order['receivedAmount'] - $old_order->receivedAmount; //新增消费额,有可能为负数
- $customerInfo->balance = $customerInfo->balance - $add_amount;
- $customerInfo->save();
- }
- }
- #记录操作日志
- $self_id = (int)$request->input('admin_id');
- $self_name = Admin::where('id', $self_id)->pluck('realname');
- $context = "修改订单";
- $type = 0;
- $tables = 'order';
- $data_id = $id;
- Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
- }
- #新加逻辑,使用优惠券,赠礼
- $use_b_gift = (int)$request->input('use_b_gift');
- if($coupon_id > 0 && $old_order->use_coupon != 1){
- $coupon = DB::table('customer_coupons')->where('phone', $order['buyer_phone'])->where('id', $coupon_id)->where('is_use',0)->orderBy('id', 'desc')->first();
- if(!empty($coupon)){
- $cup = array();
- $cup['is_use'] = 1;
- $cup['use_time'] = date('Y-m-d H:i:s');
- $cup['order_id'] = $id;
- DB::table('customer_coupons')->where('id', $coupon->id)->update($cup);
- }
- }elseif($coupon_id == 0 && $old_order->use_coupon == 1){
- DB::table('customer_coupons')->where('order_id', $id)->update(['is_use'=>0]);
- }elseif($coupon_id > 0 && $old_order->use_coupon == 1){
- $coupon = DB::table('customer_coupons')->where('phone', $order['buyer_phone'])->where('id', $coupon_id)->first();
- if(!empty($coupon) && $coupon->is_use == 0){
- DB::table('customer_coupons')->where('order_id', $id)->update(['is_use'=>0]);
- $cup = array();
- $cup['is_use'] = 1;
- $cup['use_time'] = date('Y-m-d H:i:s');
- $cup['order_id'] = $id;
- DB::table('customer_coupons')->where('id', $coupon->id)->update($cup);
- }
- }
- #查询本单使用赠礼
- $order_gifts = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $id)->where('gift_id','>',0)->lists('gift_id');
- $order_m_gift_id = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 0)->pluck('id');
- $order_b_gift_ids = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 1)->lists('id');
- if($m_gift_id>0 && empty($order_m_gift_id) ){
- $m_gift = DB::table('customer_month_gift')->where('id', $m_gift_id)->where('type', 0)->first();
- $order_m_gift = array();
- $m_sku = GoodsSkus::where('id', $m_gift->sku_id)->first();
- $order_m_gift['sku_id'] = $m_gift->sku_id;
- $order_m_gift['order_id'] = $id;
- $order_m_gift['goods_id'] = $m_sku->goodsCode;
- $order_m_gift['num'] = $m_gift->num;
- $order_m_gift['price'] = $m_sku->price;
- $order_m_gift['gift_id'] = $m_gift_id;
- $order_gift_insert = DB::table('order_goods_skus')->insert($order_m_gift);
- #记录领取
- $receive_gift = array();
- $receive_gift['phone'] = $order['buyer_phone'];
- $receive_gift['dtime'] = date('Y-m-d');
- $receive_gift['order_id'] = $id;
- $receive_gift['gift_id'] = $m_gift_id;
- $receive_gift['gift_type'] = 0;
- DB::table('customer_gift_receives')->insert($receive_gift);
- }elseif($m_gift_id>0 && $m_gift_id != $order_m_gift_id){
- DB::table('order_goods_skus')->where('gift_id', $order_m_gift_id)->where('order_id', $id)->update(['is_del'=>1]);
- DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_id', $order_m_gift_id)->update(['is_del'=>1]);
- $m_gift = DB::table('customer_month_gift')->where('id', $m_gift_id)->where('type', 0)->first();
- $order_m_gift = array();
- $m_sku = GoodsSkus::where('id', $m_gift->sku_id)->first();
- $order_m_gift['sku_id'] = $m_gift->sku_id;
- $order_m_gift['order_id'] = $id;
- $order_m_gift['goods_id'] = $m_sku->goodsCode;
- $order_m_gift['num'] = $m_gift->num;
- $order_m_gift['price'] = $m_sku->price;
- $order_m_gift['gift_id'] = $m_gift_id;
- $order_gift_insert = DB::table('order_goods_skus')->insert($order_m_gift);
- #记录领取
- $receive_gift = array();
- $receive_gift['phone'] = $order['buyer_phone'];
- $receive_gift['dtime'] = date('Y-m-d');
- $receive_gift['order_id'] = $id;
- $receive_gift['gift_id'] = $m_gift_id;
- $receive_gift['gift_type'] = 0;
- DB::table('customer_gift_receives')->insert($receive_gift);
- }elseif($m_gift_id==0 && $order_m_gift_id>0){
- DB::table('order_goods_skus')->where('gift_id', $order_m_gift_id)->where('order_id', $id)->update(['is_del'=>1]);
- DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_id', $order_m_gift_id)->update(['is_del'=>1]);
- }
- $mdate = date('Y-m-01', strtotime($old_order->create_time));
- if($use_b_gift>0 && empty($order_b_gift_ids) ){
- $b_gifts = DB::table('customer_month_gift')->where('mtime', $mdate)->where('type', 1)->get();
- $b_gifts = json_decode(json_encode($b_gifts),true);
- foreach($b_gifts as $b_gift){
- $order_b_gift = array();
- $b_sku = GoodsSkus::where('id', $b_gift['sku_id'])->first();
- $order_b_gift['sku_id'] = $b_gift['sku_id'];
- $order_b_gift['order_id'] = $id;
- $order_b_gift['goods_id'] = $b_sku->goodsCode;
- $order_b_gift['num'] = $b_gift['num'];
- $order_b_gift['price'] = $b_sku->price;
- $order_b_gift['gift_id'] = $b_gift['id'];
- $order_gift_insert = DB::table('order_goods_skus')->insert($order_b_gift);
- }
- #记录领取
- $receive_gift = array();
- $receive_gift['phone'] = $order['buyer_phone'];
- $receive_gift['dtime'] = date('Y-m-d');
- $receive_gift['order_id'] = $id;
- $receive_gift['gift_type'] = 1;
- DB::table('customer_gift_receives')->insert($receive_gift);
- }elseif($use_b_gift==0 && !empty($order_b_gift_ids)){
- DB::table('order_goods_skus')->whereIn('gift_id', $order_b_gift_ids)->where('order_id', $id)->update(['is_del'=>1]);
- DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_type', 1)->update(['is_del'=>1]);
- }
- DB::commit();
- }catch (Exception $e){
- DB::rollback();
- Log::errorLog($request,['error'=>$e->getMessage()],"order/update",0,'');
- return self::returnValue(['msg'=>$e->getMessage()], 2000);
- }
- return self::returnValue(['msg' => '更新成功']);
- }
- /**
- * 订单审核
- */
- public function setverify(Request $request){
- $id = (int)$request->input('id');
- $order = Order::find($id);
- $old_status = $order->status;
- $order->status = (int)$request->input('status');
- if($order->status == 0){
- $refuse_reason = trim($request->input('refuse_reason'));
- if(!empty($refuse_reason)){
- $order->refuse_reason = $refuse_reason;
- }
- }
- if($order->status == 2 || $order->status == 0){
- $order->verify_time = date('Y-m-d H:i:s'); //审核时间
- }
- //获取可变更状态
- $enableStatus = $this->getEnableStatus($old_status);
- if( !empty($enableStatus) ){
- $statusArr = array_column($enableStatus, 'status');
- if( !in_array($order->status, $statusArr) ){
- return self::returnValue(['msg'=>'异常操作'], 2000);
- }
- }else{
- return self::returnValue(['msg'=>'异常操作'], 2000);
- }
- DB::beginTransaction();
- try{
- $res = $order ->save();
- if($res){
- #记录操作日志
- $status_arr = array(
- 0 => '已录入',
- 1 => '待审核',
- 2 => '已审核',
- 3 => '已发货',
- 4 => '已退款'
- );
- $self_id = (int)$request->input('admin_id');
- $self_name = Admin::where('id', $self_id)->pluck('realname');
- $context = "订单状态变更:【".$status_arr[$old_status]."】变更为【". $status_arr[$order->status]."】({$old_status}->{$order->status})";
- $type = 1; //状态变更
- $tables = 'order';
- $data_id = $id;
- Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
- $today_date = date('Y-m-d');
- #如果审核通过,c仓, 并且到预发货时间,同步卖家云
- if($order->status == 2 && $order->warehouse == 3 && $order->delivery_date <= $today_date){
- $syncMj = $this->syncOrderToMj($id);
- if( $syncMj == false ){
- throw new Exception("订单同步到卖家云失败");
- }
- }
- }
- DB::commit();
- }catch (Exception $e){
- Log::errorLog($request,['error'=>$e->getMessage()],"order/setverify",0,['id'=>$id]);
- DB::rollback();
- return self::returnValue(['msg'=>$e->getMessage()], 2000);
- }
- return self::returnValue(['msg'=>'操作成功']);
- }
- /**
- * 订单状态机制,获取可变更状态
- * @param status:0已录入 1提交审核 2已审核 3已发货 4已退款
- */
- public function getEnableStatus($status){
- $status_arr = [
- 0 => ['status'=>0, 'name'=>'已录入'],
- 1 => ['status'=>1, 'name'=>'提交审核'],
- 2 => ['status'=>2, 'name'=>'审核通过'],
- 3 => ['status'=>3, 'name'=>'设为发货'],
- 4 => ['status'=>4, 'name'=>'设为退款']
- ];
- $status = (int)$status;
- $result = array();
- switch ($status) {
- case '0':
- $result[] = $status_arr[1];
- break;
- case '1':
- $result[] = $status_arr[2];
- $result[] = $status_arr[0];
- break;
- case '2':
- $result[] = $status_arr[1];
- $result[] = $status_arr[3];
- break;
- default:
- break;
- }
- return $result;
- }
- /**
- * 订单同步到卖家云
- */
- public function syncOrderToMj($id, $order = []){
- if( empty($order) ){
- $order = Order::select('eshopCode','outerCode','status','customerName','shippingType','expressCompanyCode','expressCompanyName','receiverName','receiverMobile','receiverPhone','receiverState','receiverCity','receiverDistrict','receiverStreet','receiverAddress','buyerPostageFee','discountFee','adjustFee','serviceFee','paymentMethod','paymentType','paymentAccount','paymentTime','receivedAmount','buyerMemo','sellerMemo','createTime','modifyTime')->where('id', $id)->first();
- $order = json_decode(json_encode($order), true);
- $skus = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $id)->get();
- $skus = json_decode(json_encode($skus), true);
- foreach($skus as $k=>$v){
- $sku_info = GoodsSkus::where('id', $v['sku_id'])->first();
- $goods_info = Goods::where('id', $v['goods_id'])->first();
- $params = array();
- $params['outerCode'] = $order['outerCode']+$k+1; //外部平台中子订单编码
- $params['outerParentCode'] = $order['outerCode']; //外部平台中订单编码
- $params['goodsCode'] = $sku_info->goodsCode;//
- $params['skuCode'] = $v['sku_id'];
- #商品名称
- $params['goodsName'] = $goods_info->name;
- $params['propertiesName'] = $sku_info->is_weigh==1 ? $sku_info->propsName.'【按半斤称重】' : $sku_info->propsName;
- //$params['goodsName'] = '海钓黄翅鱼';//$goods->code;
- $params['price'] = $sku_info->is_weigh==1 ? round($v['price']/2,2) : $v['price'];
- $params['quantity'] = (int)($sku_info->is_weigh==1 ? $v['num']*2 : $v['num']);
- $params['discountFee'] = 0;
- $params['adjustFee'] = 0;
- $params['refundStatus'] = '正常';
- $order['skuList'][] = $params;
- }
- }
- //卖家云添加订单 ->改为审核通过再同步卖家云
- $mj_add = Order::mjOrderAdd($order);
- //同步卖家云订单数据到本系统
- if( isset($mj_add['order']['code']) ){
- $mjorder = array();
- $mjorder['orderCode'] = $mj_add['order']['code'];
- Order::where('id', $id)->update($mjorder);
- }
- return $mj_add;
- }
- /**
- * 订单详情
- */
- public function orderDetail(Request $request){
- $admin_id = (int)$request->input('admin_id');
- $id = (int)$request->input('id');
- $self_role = DB::table('admin_role')->where('user_id', $admin_id)->pluck('role_name');
- $team_id = null;
- if($self_role == '管理员'){
- $team_id = Admin::where('id', $admin_id)->pluck('team_id');
- $admin_id = null;
- }
- $order = Order::select('id', 'status', 'orderCode', 'customerName', 'receiverName', 'receiverMobile', 'receiverState','receiverCity','receiverDistrict','receiverAddress','receivedAmount','buyerMemo','sellerMemo','createTime','admin_id','admin_name','cost', 'logistics_id','is_refund','warehouse','delivery_date','refund_price','refund_type','send_note','freight_cost','payment_type','refuse_reason','refund_note','verify_time','aftersale_fee','aftersale_reason','aftersale_time','refund_time','send_time','should_amount', 'buyer_phone', 'use_coupon', 'use_m_gift', 'use_b_gift')->where('is_del', 0)->where('warehouse', 3)->where('id', $id)->first();
-
- $status_arr = array(
- 0 => '已录入',
- 1 => '待审核',
- 2 => '已审核',
- 3 => '已发货',
- 4 => '已退款'
- );
-
- $goods = OrderGoodsSkus::where('order_id', $id)->where('is_del', 0)->get();
- $skus = array();
- $m_gifts = array();
- $b_gifts = array();
- if(!empty($goods)){
- foreach($goods as &$item){
- $item->goods_name = Goods::where('id', $item->goods_id)->pluck('name');
- $item->picUrl = Goods::where('id', $item->goods_id)->pluck('picUrl');
- $goods_sku_info = GoodsSkus::select('propsName', 'is_weigh')->where('id', $item->sku_id)->first();
- if(!empty($goods_sku_info)){
- $item->props_name = $goods_sku_info->propsName;
- $item->is_weigh = $goods_sku_info->is_weigh;
- }else{
- $item->props_name = '';
- $item->is_weigh = 0;
- }
- $item->gift_type = null;
- if($item->gift_id>0){
- $gift_flag = DB::table('customer_month_gift')->where('id', $item->gift_id)->first();
- if($gift_flag->type==1){
- $item->gift_type = 1;
- $b_gifts[] = $item;
- }else{
- $item->gift_type = 0;
- $m_gifts[] = $item;
- }
- }else{
- $skus[] = $item;
- }
-
- }
- }
- $order->skus = $skus;
- $order->m_gifts = $m_gifts;
- $order->b_gifts = $b_gifts;
-
- #加粉时间
- $fanTime = DB::table('customers')->select('fanTime')->where('phone', $order->receiverMobile)->first();
- $order->fanTime = isset($fanTime->fanTime) ? $fanTime->fanTime : '';
- //$v['receiverMobile'] = substr($v['receiverMobile'], 0, 3).'****'.substr($v['receiverMobile'], 7);
- $order->status_name = $status_arr[$order->status];
- // 代金券
- $order->coupon_price = null;
- if($order->use_coupon == 1){
- $coupon_info = DB::table('customer_coupons')->where('order_id', $id)->where('is_use', 1)->first();
- $order->coupon_id = $coupon_info->id;
- $order->coupon_price = $coupon_info->coupon_price;
- }
- #查询用户会员信息
- $cust_info = array();
- $cust_info['if_coupons'] = 0;
- $cust_info['if_m_gift'] = 0;
- $cust_info['if_b_gift'] = 0;
- $order->is_vip = 0;
- $phone = $order->buyer_phone;
- $time = date('Y-m-d H:i:s');
- $mtime = date('Y-m-01');
- $customer_info = DB::table('customer_vip')->where('phone', $phone)->where('is_del', 0)->first();
- if( !empty($customer_info) ){
- //判断是否过期
- if($customer_info->vip_end_time > $time){
- $order->is_vip = 1;
- $coupons = DB::table('customer_coupons')->where('phone', $phone)->where('is_use', 0)->where('end_time', '>', $time)->first();
- $cust_info['if_coupons'] = !empty($coupons) ? 1:0;
- if($order['createTime'] >= $mtime){
- //未过期,1.获取优惠券,获取当月礼,获取生日礼,判断领取
- //判断是否领取当月礼
- $if_m_gift = DB::table('customer_gift_receives')->where('phone', $phone)->where('is_del',0)->where('gift_type', 0)->where('dtime', '>=', $mtime)->first();
- if(empty($if_m_gift)){
- $cust_info['if_m_gift'] = 1;
- }
-
- //判断生日礼
- $birth_time = date('m', strtotime($customer_info->birthday));
- $tom = date('m');
- if($tom==$birth_time){
- //生日期内,判断是否已领
- $if_b_gift = DB::table('customer_gift_receives')->where('phone', $phone)->where('is_del',0)->where('gift_type', 1)->where('dtime', '>=', $mtime)->first();
- if(empty($if_b_gift)){
- $cust_info['if_b_gift'] = 1;
- }
- }
- }
- }
- }
- //额外+判断 ,若已领,则可领
- if($order->use_coupon == 1){
- $cust_info['if_coupons'] = 1;
- }
- if($order->use_m_gift == 1){
- $cust_info['if_m_gift'] = 1;
- }
- if($order->use_b_gift == 1){
- $cust_info['if_b_gift'] = 1;
- }
-
- $order->if_coupons = $cust_info['if_coupons'];
- $order->if_m_gift = $cust_info['if_m_gift'];
- $order->if_b_gift = $cust_info['if_b_gift'];
-
- return self::returnValue(['data'=>$order]);
- }
- }
|