jeudi 29 novembre 2018

laravel: Why my transaction and try catch is not working properly in this code?

I haven't used any sort of pattern. I'm thinking to use traits. Sorry for this lump of code but I have nested data. I'm inserting multiple data and in the way of inserting into my tables. I'm not getting the exception error. It gives me success but I have no data on my tables. Sometime it rollbacks but doesn't gives me an error. How can I optimize the code below. Please help me. How to structure this code so database transaction works properly.

public function paymentGetaway(Request $request){
        DB::beginTransaction();
        try{
           $user = User::find(auth()->id())->first();

           //Check email if it already exists then update
           $existing_client = Client::where('email', $request->email)->where('is_deleted', 0)->first();
           if($existing_client){
               $existing_client->fname = $request->fname;
               $existing_client->lname = $request->lname;
               $existing_client->useru_id = auth()->id();
               $existing_client->updated_at = date('Y-m-d H:i:s');
               $result = $existing_client->save();

               /*Address section*/
               if($result){
                   $state = true;
                   $old_addr = Address::where('table', "client")->where('table_id', $existing_client->id)->get();

                   foreach ($old_addr as $key => $value) {
                       //Data exits
                       if($value->addr1 == $request->addr1 
                            && $value->zip == $request->zip 
                            && $value->state == $request->state
                            && $value->country == $request->country
                            && $value->city == $request->city )
                       {

                           $state = false;
                           $addr_id = $value->id;
                       }                   

                   }   

                   if($state){
                       $address = new Address();
                       $address_data = $request->only([
                           'addr1','zip', 'city','state', 'country', 'addr2'
                       ]);
                       $address_data['addr_type'] = "Temporary";
                       $address_data['table'] = "client";
                       $address_data['table_id'] = $existing_client->id;
                       $new_c_address = $address->create($address_data); 
                       $addr_id = $new_c_address->id;
                   }  

               }

               $order = $this->storeOrder($existing_client->id, $addr_id);
               $carts = $this->getCartItems();
               foreach ($carts as $key => $cart) {
                   $discount= $this->getDiscount($cart->product_id);

                   $order_items = new OrderItem();
                   $order_items->order_id = $order->id;
                   $order_items->inventory_id = $cart->inventory_id;

                   if($discount !== null){
                       $order_items->product_discount_id = $discount->id;
                   }

                   $order_items->quantity = $cart->quantity;
                   $order_items->userc_id = auth()->id();
                   $order_items->created_at = date('Y-m-d H:i:s');
                   $order_items->save();
               }                 


              /*Set the cart data to is_deleted = 0 which means inactive cart*/ 
              foreach($carts as $cart){
                   $cart->is_deleted = 1;
                   $cart->save();
              } 

              DB::commit();              

           }else{
               $client = new Client();
               $client_data = $request->only([
                   'fname', 'lname', 'email'
               ]); 
               $client_data['userc_id'] = auth()->id();       
               $new_client = $client->create($client_data);

               //Inserting to HR Customer
               $this->addCustomer($request);

               if($new_client){
                   //Store address
                   $address = new Address();
                   $address_data = $request->only([
                       'addr1','zip', 'city','state', 'country', 'addr2'
                   ]);
                   $address_data['addr_type'] = "Temporary";
                   $address_data['table'] = "client";
                   $address_data['table_id'] = $new_client->id;
                   $new_c_address = $address->create($address_data);            
                   $addr_id = $new_c_address->id;
               } 

               $order = $this->storeOrder($new_client->id, $addr_id);
               $carts = $this->getCartItems();
               $purchaseWithoutDis = 0;
               $purchaseWithDis = 0;
               $productCount = 0;
               $discountAmount = 0;
               // $discount_percent = 0;

               foreach ($carts as $key => $cart) {
                   $discount= $this->getDiscount($cart->product_id);

                   $order_items = new OrderItem();
                   $order_items->order_id = $order->id;
                   $order_items->inventory_id = $cart->inventory_id;

                   if($discount !== null){
                       $order_items->product_discount_id = $discount->id;
                   }

                   $order_items->quantity = $cart->quantity;
                   $order_items->userc_id = auth()->id();
                   $order_items->created_at = date('Y-m-d H:i:s');
                   $order_items->save();

                   $productCount +=$cart->quantity;                

                   //Collect Discount
                   if(!is_null($discount)){
                       if($discount->type == "flat"){
                           //Get the total sales   
                           $inventory = Inventory::find($order_items->inventory_id);
                           $purchaseWithoutDis +=$inventory->price * $cart->quantity;                       

                           $discountAmount += $discount->amount * $cart->quantity;

                           $priceAfterDiscount = $inventory->price - $discount->amount;
                           $purchaseWithDis += $priceAfterDiscount * $cart->quantity;

                       }else{                
                           //Get the total sales   
                           $inventory = Inventory::find($order_items->inventory_id);
                           $purchaseWithoutDis +=$inventory->price * $cart->quantity;

                           $disAmount = ($discount->amount/100) * $inventory->price;
                           $purchaseWithDis += $disAmount * $cart->quantity;

                           $discountAmount += $disAmount * $cart->quantity;                        
                       }
                       dd("here");
                   }else{
                       $inventory = Inventory::find($order_items->inventory_id);
                       $purchaseWithoutDis +=$inventory->price * $cart->quantity;
                       $purchaseWithDis += $inventory->price * $cart->quantity;
                   }
               }   
               if($discountAmount == 0){
                   $discountAmount = null;
               }
               //Create Invoice and Journal 
               $invoice = $this->storeInvoice($request,$purchaseWithoutDis,$order->id, $new_client->id, $discountAmount);
               $productDesc = $request->fname.' '.$request->lname.' Ordered '.$productCount.' items. Order No: '.$invoice->invoice_no;    

               $journal = $this->storeJournal($purchaseWithoutDis,$purchaseWithDis, $productDesc, $discountAmount);
               /*Set the cart data to is_deleted = 0 which means inactive cart*/ 
               foreach($carts as $cart){
                   $cart->is_deleted = 1;
                   $cart->save();
               } 
               DB::commit();            
               return response()->json(["Payment Successful"], 200);
           }  
       }catch(\Exception $e){
            DB::rollback();
            return response(["message"=>$e->getMessage()], 500);
       } 



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

Aucun commentaire:

Enregistrer un commentaire