jeudi 1 octobre 2020

how to do not update value on update function?

i want this code should not be run if user call upate route code

if($getDistanceValue * 1000 > 300){
        if($chk_ord == 0)
        {
        $order->chk_ord_vst = 1;
        }
        elseif($chk_ord != 0){
        $order->chk_ord_vst = $chk_ord + 1;
        }
        }
        elseif($getDistanceValue * 1000 <= 300 ){
            if($chk_ord >= 4){
             $order->chk_ord_vst = 2;
            }
            elseif($chk_ord == 3){
             $order->chk_ord_vst = 2;
            }
            elseif($chk_ord == 2){
             $order->chk_ord_vst = 1;
            }
            elseif($chk_ord <= 1){
             $order->chk_ord_vst = 0;
            }   
        }

i am using a same fucntion to update or store order but on update i donot want to run above code i know it is very simple but i am a student and learning help me here it is controller function first user create order the if wants to update then we use get function to pass value to edit view and then pass again to stor function

public function storeOrder(Request $request , $update = null , $customer_id){

    $order = !is_null($update) ? Order::find($update) : new Order();
    
    $cus_det = explode("-", $request->customer_id);

    $tt_amount = array_sum($request->amount) + $request->old_balance;
    if(is_null ($update))
    $balance = $tt_amount + $request->old_balance;
   
    $auth_id = Auth::id();
    if(Auth::user()->role == 4){
        $auth_id = Customer::where('user_id', $auth_id)->first()->created_by;
    }

    $order->customer_id = $cus_det[0];
    $order->user_id = Auth::id();
    $order->ot_id = !is_null($update) ? $order->ot_id : $auth_id;
    $order->unit = array_sum($request->unit);
    $order->amount = $tt_amount;
    $order->subtotal = array_sum($request->amount);
    $order->order_comments  = $request->order_comments;
    
    if( Auth::user()->role == 5){
        $order->location_url_ot = $request->location_url_ot;
    $order->received_amount = 0;
    }
    else{
        $order->received_amount = $request->received_amount;
        
    }
    $order->discount = $request->discount;
    $order->order_date= date('Y/m/d', strtotime($request->order_date));
    if($tt_amount >= $request->received_amount){
        $order->amount_left = ($tt_amount -  $request->received_amount - $request->discount);
    }
    else{
        $order->advance = $request->received_amount - $tt_amount;
        $order->amount_left = $request->old_balance -  $request->received_amount - $request->discount;
    }
    
    $checkB = $this->checkMinBalance($cus_det[0] , $order->amount_left);
    if($checkB){
        return redirect()->back()->with('error' , 'Customer Balance Limit Exceeded ( Limit is '.$checkB.' )');
    }
    if($request->has('important')){
        $order->is_important=1;
    }
    
     if($request->has('urgent')){
        $order->urgent = urgent ;
    }
    $getothomDistanceValue = ($this->getothomDistance($order) * 1.37);
    
    $getDistanceValue = ($this->getDistance($order) * 1.37);
    
    
    $old_order = Order::where('customer_id' , $order->customer_id)->orderBy('id' , 'desc')->get();
      $chk_ord = $old_order[0]->chk_ord_vst; 

    if($getDistanceValue * 1000 > 300){
        if($chk_ord == 0)
        {
        $order->chk_ord_vst = 1;
        }
        elseif($chk_ord != 0){
        $order->chk_ord_vst = $chk_ord + 1;
        }
        }
        elseif($getDistanceValue * 1000 <= 300 ){
            if($chk_ord >= 4){
             $order->chk_ord_vst = 2;
            }
            elseif($chk_ord == 3){
             $order->chk_ord_vst = 2;
            }
            elseif($chk_ord == 2){
             $order->chk_ord_vst = 1;
            }
            elseif($chk_ord <= 1){
             $order->chk_ord_vst = 0;
            }
            
            
        }

    $order->save();

    // set order if update is null
    if(is_null($update)){
        
        $getDistanceValue = ($this->getDistance($order) * 1.37);
        $succ = Order::where('id', $order->id)->update(['ot_customer_distance'=> $getDistanceValue]);
        
    }
    
    $order_id = $order->id;
    
    if(!is_null($update))
        $order->orderdetail()->delete();
    
    $orderData = $request->all();
    $total_ot_benefit = 0; $total_customer_benefit = 0;$p_amount = 0;
    for($counter = 0;$counter < sizeof($request->amount);$counter++){
        if(!empty($orderData['amount'][$counter]))
        {
            $orderDetails = new OrderDetail();
            $orderDetails->order_id = $order_id;
            $is_custom_price = CustomPrice::where(['customer_id' => $cus_det[0] , 'product_id' => $orderData['product_id'][$counter]])->first();
            
            $is_custom_ot_benefit = CustomOtBenefit::where(['ot_id' => $order->ot_id , 'product_id' => $orderData['product_id'][$counter]])->first();
            $is_default_price = Product::find($orderData['product_id'][$counter]);
           
            if($is_custom_price){
                $orderDetails->ot_benefit = (string)bcmul($is_custom_price->ot_benefit, $orderData['unit'][$counter]);
                $orderDetails->c_benefit = (string)bcmul($is_custom_price->c_benefit, $orderData['unit'][$counter]);
                $orderDetails->p_amount = (string)bcmul($is_custom_price->product->p_price, $orderData['unit'][$counter]);
            }
            else{
                $orderDetails->c_benefit = (string)bcmul($is_default_price->c_benefit, $orderData['unit'][$counter]);
                $orderDetails->p_amount = (string)bcmul($is_default_price->p_price, $orderData['unit'][$counter]);
                $orderDetails->ot_benefit = (string)bcmul($is_custom_ot_benefit->ot_benefit, $orderData['unit'][$counter]);
                
            }
            // exit;
            $orderDetails->product_id = $orderData['product_id'][$counter];
            $orderDetails->unit = $orderData['unit'][$counter];
            $orderDetails->amount = $orderData['amount'][$counter];
            $orderDetails->save();
        }
    }
  
    Order::where('id' , $order_id)->update(['ot_benefit' => (string)$order->orderdetail->sum('ot_benefit') , 'c_benefit' => (string)$order->orderdetail->sum('c_benefit') , 'p_amount' => $order->orderdetail->sum('p_amount')]);
    if(Auth::user()->role == 4){
        return redirect()->back()->with('success', 'Order Placed Successfully!');
    }

    return redirect()->route('unconfirmed.orders')->with('success' , 'Order Created');

}

public function getOrder($id){
    $added = [];
    $order = Order::find($id);
    $customPrices = CustomPrice::where('customer_id' , $order->customer_id)->get();
    
    if(sizeof($customPrices)){
        foreach($order->orderdetail as $d){
            foreach($customPrices as $custom){
                if($custom->product_id == $d->product_id){
                    $d->product->price = $custom->price;
                    $d->product->c_benefit = $custom->c_benefit;
                }
            }
            $added[] = $d->product_id;
        }
    }
    if(Auth::user()->role == 5)
        $user_id = Auth::user()->ot_of;
    else if(Auth::user()->role == 3)
        $user_id = Auth::user()->seller_of;
    else
        $user_id = Auth::user()->id;
    $products = Product::whereNotIn('id' , $added)->where('user_id' , $user_id)->get();
    foreach($products as $product){
        foreach($customPrices as $custom){
            if($custom->product_id == $product->id){
                $product->price = $custom->price;
                $product->c_benefit = $custom->c_benefit;
            }
        }
    }
      $old_order = Order::where('customer_id' , $order->customer_id)->orderBy('id' , 'desc')->get();
      $amount_left=$old_order[0]->amount_left - $old_order[0]->subtotal + $old_order[0]->received_amount + $old_order[0]->discount; 
     
    $old_balance = 0;
    if(sizeof($old_order) > 1){
        $old_balance = $old_invoices[1]->amount_left;
    }
    return view('orders.edit' , compact('order' , 'products' , 'old_balance','amount_left' , 'discount'));
}


public function updateOrder(Request $request , $id){
     $request->hid;
    $conditions = ['id' => $id];
    $order = Order::where($conditions)->get();
     $request->amount;
    if(sizeof($order)){
        if($request->amount_left_input){
            $amount_left = $order[0]->amount_left + $order[0]->received_amount + $order[0]->discount;
            
            $order = Order::where($conditions)
            ->update(['amount_left' => $amount_left - $request->amount_left_input + $order[0]->received_amount,
             'received_amount' => $order[0]->received_amount + $order[0]->discount,
             'discount' => $order[0]->discount + $request->amount_left_input,  'chk_ord_vst' => $request->chk_ord_vst,
             
            ]
            );
            return Common::Message("Order" , 2);
    }
    
    else{
         
        Order::where('id' , $id)->update(['amount_left' => $order[0]->amount_left + $order[0]->received_amount , 'received_amount' => $order[0]->received_amount + $order[0]->discount , 'discount' => $order[0]->discount + $request->amount_left_input , 'urgent' => $request->urgent , 'location_url_ot' => $request->location_url_ot , 'chk_ord_vst' => $request->chk_ord_vst , ]);
        $this->storeOrder($request , $order[0]->id);
        
        

        return redirect()->route('unconfirmed.orders')->with('success' , 'Order Updated');
        }
    }
    else{
        return Common::Message("Order");
    }
}


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2GeDoLe
via IFTTT

Aucun commentaire:

Enregistrer un commentaire