<?php

use App\ReservationBreakdown;
use App\Stock;
use App\Service;
use App\Worker;
// Get the worker and branch.
$worker = Worker::find(Auth::user()->TypeId);
$userBranch = $worker->BranchId;
// Get the breakdown of the reservation.
$reservationBreakdown = ReservationBreakdown::where('ReservationId', '=', $id)->get();
function getName($code, $branch)
{
    // Try getting a normal product for this code
    $product = Stock::where('Code', '=', $code)->where('BranchId', '=', $branch)->first();
    if (!$product) {
        // If we failed try getting a service.
        $service = Service::where('Code', '=', $code)->where('BranchId', '=', $branch)->first();
        return $service->Description;
    }
    return $product->Description;
}
?>
<thead>
	<tr><th>Producto</th><th>Precio Individual</th><th>Cantidad</th></tr>
</thead>
<tbody>
	@foreach($reservationBreakdown as $breakdown)
		<tr><td>{{ getName($breakdown->Code, $userBranch) }}</td><td>{{ $breakdown->Price }}</td><td>{{ $breakdown->Quantity }}</td></tr>
	@endforeach
</tbody>
 /**
  * Function that gets reservation information.
  *
  * @return Response
  */
 public function getReservationData()
 {
     // Validate Input.
     $validator = Validator::make(Input::all(), array('id' => 'required'));
     if ($validator->fails()) {
         return response()->json(['error' => 'No se recibieron los datos necesarios!']);
     }
     // Check that user is part of authorized staff.
     if (Auth::user()->Type != 1) {
         $response['state'] = 'No Autorizado';
         $response['error'] = 'Usuario no autorizado!';
         return response()->json($response);
     }
     // Get user branch.
     $worker = Worker::find(Auth::user()->TypeId);
     $userBranch = $worker->BranchId;
     // Get the reservation.
     $reservation = Reservation::find(Input::get('id'));
     // Get the breakdown of the reservation.
     $reservationBreakdown = ReservationBreakdown::where('ReservationId', '=', $reservation->Id)->get();
     $reservationItems = array();
     foreach ($reservationBreakdown as $breakdown) {
         // Try getting the product.
         $product = Stock::where('Code', '=', $breakdown->Code)->where('BranchId', '=', $userBranch)->first();
         if (!$product) {
             // If it's not a product it's a service.
             $service = Service::where('Code', '=', $breakdown->Code)->where('BranchId', '=', $userBranch)->first();
             array_push($reservationItems, array('quantity' => $breakdown->Quantity, 'description' => $service->Description, 'price' => $service->Price));
         } else {
             array_push($reservationItems, array('quantity' => $breakdown->Quantity, 'note' => $product->Description, 'price' => $product->Price));
         }
     }
     // Get the client's or institution's information.
     $client;
     if ($reservation->CreditorId != 0) {
         if ($reservation->CreditorType != 1) {
             $temp = Institution::find($reservation->CreditorId);
             $client = array('Name' => $temp->Name, 'Address' => $temp->Address, 'Id' => $temp->RUC);
         } else {
             $temp = Client::find($reservation->CreditorId);
             $client = array('Name' => $temp->Name, 'Address' => $temp->Address, 'Id' => $temp->Id);
         }
     } else {
         $client = array('Name' => 'No asignado', 'Address' => 'No asignado', 'Id' => 'No asignado');
     }
     // Get configuration info.
     $config = Configuration::find(0);
     //  Now prepare all the information to be returned to user.
     $response['state'] = 'Success';
     $response['clientInfo'] = $client;
     $response['reservationInfo'] = array('Date' => date('Y-m-d', strtotime($reservation->Created)), 'Life' => $config->ReservationLife, 'ReservationId' => $reservation->Id, 'SubTotal' => $reservation->Value, 'Discount' => $reservation->Discount, 'Tax' => $reservation->Tax, 'Deposit' => $reservation->Deposit, 'Total' => $reservation->Value + $reservation->Tax - $reservation->Discount);
     $response['reservationItems'] = $reservationItems;
     // Return response.
     return response()->json($response);
 }