Advertisement
Alessio_S10

funzione php

Feb 15th, 2022
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.08 KB | None | 0 0
  1.     public function available(Request $request)
  2.     {
  3.  
  4.  
  5.        
  6.         $data = $request->all();
  7.         $newData = array();
  8.        
  9.         $categoryArr = array();
  10.  
  11.         // manipolo la request per pulirla e salvarmi un altro array da utilizzare dopo
  12.        
  13.         foreach ($data as $key => $value) {
  14.            
  15.             if ($value == 0) {
  16.                
  17.             } else {
  18.  
  19.                 $newData[$key] = $value;
  20.                
  21.                
  22.             }
  23.            
  24.         }
  25.        
  26.    
  27.  
  28.         // ciclo la request e verifico con il nuovo array le categorie e creo un nuovo array con $key == id categoria && $value == quantita ordianata
  29.         foreach ($data as $key => $value) {
  30.        
  31.             /* dd($data); */
  32.             foreach ($newData['category'] as $key2 => $value2) {
  33.                 /* dd($newData['category']); */
  34.                 if ($value2 == $key ) {
  35.                     $categoryArr[$value2] = $value;
  36.                    
  37.                    
  38.                 }
  39.                
  40.             }
  41.  
  42.         }  
  43.        // dd($categoryArr);
  44.  
  45.  
  46.        
  47.  
  48.         // Gestione date
  49.         $dataRange = $newData['range_date'];
  50.  
  51.         //
  52.  
  53.         $arraySplit = explode(' - ', $dataRange);
  54.  
  55.         $formatCarbonStart = Carbon::createFromFormat('d/m/Y', $arraySplit[0])->format('Y-m-d');
  56.  
  57.         $formatCarbonEnd = Carbon::createFromFormat('d/m/Y', $arraySplit[1])->format('Y-m-d');
  58.  
  59.         $inputStartDate = Carbon::parse($formatCarbonStart)->format('Y-m-d');
  60.         $inputEndDate = Carbon::parse($formatCarbonEnd)->format('Y-m-d');
  61.  
  62.         $bookingStart = Carbon::createFromFormat('Y-m-d',$inputStartDate);
  63.         $bookingEnd = Carbon::createFromFormat('Y-m-d',$inputEndDate);
  64.  
  65.         //
  66.         // Gestione date
  67.  
  68.         // Dichiaro i due array delle bici
  69.         $biciCorretta = array();
  70.        
  71.         $biciSbagliata= array();
  72.         // Ciclo l'array delle categorie
  73.         //dd($categoryArr);
  74.         foreach ($categoryArr as $keys => $value) {
  75.             //dd($categoryArr);
  76.             $category = Category::find($keys);
  77.             $bikes = Bike::with('Category')->where([['manutenzione', '=', 0],['category_id', '=', $keys]])->get();
  78.             //dd($bikes);
  79.                
  80.             // Per ogni iterazione faccio una query e limito le risposte di questa query in base alla quantità che mi hanno mandato
  81.             if (count($bikes) < $value) {
  82.                 //dd($bikes);
  83.                 return back()->with('message', 'Quantità maggiore delle bici disponibili '.$category->tipo);
  84.             } else {
  85.                 // ciclo le bici trovate
  86.                 foreach ($bikes as $bike) {
  87.                     //dd($bikes);
  88.                     $i=0;
  89.                     // se hanno dei contratti
  90.                     //dd(count($bike->contract) > 0);
  91.                     if (count($bike->contract) > 0 && $i<=count($bikes)) {
  92.                         // ciclo i loro contratti
  93.                         foreach ($bike->contract as $contrattoEsistente) {
  94.                             //dd($contrattoEsistente->id);
  95.                             // faccio una validazione dove vedo se le date immesse dal cliente hanno bici disponibili
  96.                             $contrattoEsistenteStart = Carbon::createFromFormat('Y-m-d',$contrattoEsistente->data_inizio);
  97.  
  98.                             $contrattoEsistenteEnd = Carbon::createFromFormat('Y-m-d',$contrattoEsistente->data_fine);
  99.  
  100.                             $check1 = $bookingStart->lte($contrattoEsistenteEnd);
  101.                            
  102.                             $check2 = $bookingEnd->gte($contrattoEsistenteStart);
  103.                            
  104.                            
  105.                            
  106.                             if ($check1 && $check2) {
  107.                                
  108.                                 array_push($biciSbagliata, $bike);
  109.                                
  110.                             }else {
  111.                                 if (!in_array($bike,$biciCorretta, true)) {
  112.                                 array_push($biciCorretta,$bike);
  113.                                 }
  114.                             }
  115.                            
  116.                             $i++;
  117.                             if (count($biciSbagliata)==count($bikes)) {
  118.                                
  119.                                  return back()->with('message', 'Bici non disponibili in quelle date '.$bike->id);
  120.                             }
  121.                         }
  122.                     } else {
  123.                         if (!in_array($bike,$biciCorretta, true)) {
  124.                             array_push($biciCorretta, $bike);
  125.                         }
  126.                     }
  127.                 }
  128.                
  129.             }
  130.         }
  131.         // dichiaro l'array delle bici che selezionerò
  132.         $biciSelezionata = array();
  133.         // dd($categoryArr);
  134.         // ciclo di nuovo l'array delle categorie
  135.         foreach ($categoryArr as $key => $value) {
  136.             // dichiaro un contatore per la quantita di bici che debbo trovare
  137.             $contatoreqty=1;
  138.             foreach ($biciCorretta as $chiave => $valore) {
  139.                 $biciSingola = $valore;
  140.                 // se la categoria della bici singola è corretta e la quantita ($value) è inferiore o uguale al contatore
  141.                 // pusho la bici nell'array
  142.                 if ($biciSingola->category_id == $key && $contatoreqty <= $value/*  && $contatoreiteration <= count($categoryArr) */) {
  143.                    
  144.                     if (!in_array($biciSingola,$biciSelezionata, true)) {
  145.                         array_push($biciSelezionata, $biciSingola);
  146.                     }
  147.                     $contatoreqty ++;
  148.                 }
  149.             }
  150.         }
  151.         //dd($biciSelezionata);
  152.         // Gestione Costi
  153.  
  154.         $diffDays = $bookingEnd->diffInDays($bookingStart);
  155.  
  156.         $price = 0;
  157.  
  158.         foreach ($biciSelezionata as $key) {
  159.         //    
  160.             if ($diffDays <= 1) {
  161.                 $price += $key->category->base;
  162.        
  163.                
  164.             } elseif ($diffDays == 2) {
  165.                 $price += $key->category->twoDay;
  166.        
  167.                
  168.             } elseif ($diffDays == 3) {
  169.                 $price += $key->category->threeDay;
  170.        
  171.  
  172.             } elseif ($diffDays == 4) {
  173.                 $price += $key->category->fourDay;
  174.        
  175.  
  176.             } elseif ($diffDays == 5) {
  177.                 $price += $key->category->fiveDay;
  178.        
  179.  
  180.             } elseif ($diffDays == 6) {
  181.                 $price += $key->category->sixDay;
  182.        
  183.  
  184.             } elseif ($diffDays == 7) {
  185.                 $price += $key->category->sevenDay;
  186.        
  187.  
  188.             } elseif($diffDays > 7) {
  189.                 $overpriceDiff = $diffDays - 7;
  190.                 $sette = $key->category->sevenDay;
  191.                 $surplus = $key->category->overprice * $overpriceDiff;
  192.                 $price += $sette + $surplus;
  193.                
  194.  
  195.             }
  196.         }
  197.        
  198.  
  199.         $nome = $request->name;
  200.         $cognome = $request->cognome;
  201.         $mail = $request->mail;
  202.         $telefono = $request->tel;
  203.        
  204.  
  205.         $contract = new Contract;
  206.         $contract->nome = $nome;
  207.         $contract->cognome = $cognome;
  208.         $contract->data_inizio = $inputStartDate;
  209.         $contract->data_fine = $inputEndDate;
  210.         $contract->tel = $telefono;
  211.         $contract->mail = $mail;
  212.         $contract->temporanei = 1;
  213.         $contract->online = 1;
  214.         $contract->costo = $price;
  215.         $contract->save();
  216.         foreach ($biciSelezionata as $biciDaAttaccare) {
  217.             //dd($biciSelezionata);
  218.             $contract->bike()->attach($biciDaAttaccare->id);
  219.            
  220.         }
  221.         $contract->load('bike');
  222.        
  223.         // dd($contract);
  224.  
  225.             //invio mail alla mail interna info@entratribe.it per tracciabilita prenotazione
  226.             Mail::to('info@etnatribe.it')->send(new sendInternalMail($contract));
  227.  
  228.        
  229.         return view('booking.confirm', compact('contract') );
  230.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement