public function storeNewQuotation(Request $request) { $quotation = new Quotation(); //dd($request->all()); $id = $quotation->create($request->all())->id; //var_dump($id);die(); /* if($request->hasFile('img')){ $extension = $request->file('img')->getClientOriginalExtension(); $days = date("Ymd"); $secs = date("His", strtotime('+1 hour')); $imgName = "quotation_id_".$id."_".$days."_".$secs.".".$extension; $path = public_path() . '/upload/quotations'; $image = $request->file('img'); $request->file('img')->move( $path, $imgName); $image = Quotation::find($id); $image->img = $imgName; $image->save(); }*/ return redirect('/administration/quotation/showAllQuotation'); }
/** * Function that makes a quotation. * * @return Response */ public function makeQuotation() { // Validate Input. $validator = Validator::make(Input::all(), array('items' => 'required', 'institution' => 'required', 'discount' => 'required')); if ($validator->fails()) { return response()->json(['error' => 'No se envio la informacion completa!']); } // Check that user is part of authorized staff. if (Auth::user()->Type != 1) { // If they are unauthorized no point in returning anything. return response()->json(['state' => 'Unauthorized']); } // Get user branch. $worker = Worker::find(Auth::user()->TypeId); $userBranch = $worker->BranchId; $quotePrice = 0; $branch = Branch::find($userBranch); // Check discount is not greater than what user is allowed. $permissions = json_decode(UserLevel::find(Auth::user()->UserLevel)->Permissions); if (Input::get('discount') > $permissions->permissions->sales->discount->limit) { // Check if we have an auth code we can use. if (Input::get('authCode') == 0) { $response['state'] = 'Error'; $response['error'] = 'No tiene permiso para otorgar este descuento!'; return response()->json($response); } $request = Request::find(Input::get('authCode')); if ($request->Used == 1) { $response['state'] = 'Error'; $response['error'] = 'No tiene permiso para otorgar este descuento!'; return response()->json($response); } if ($request->Amount != Input::get('discount')) { $response['state'] = 'Error'; $response['error'] = 'No tiene permiso para otorgar este descuento!'; return response()->json($response); } $request->Used = 1; $request->save(); } // Get client. $client = Client::where('Cedula', '=', Input::get('client'))->first(); // Get institution if defined. $institution; if (Input::get('institution') != 0) { $institution = Institution::find(Input::get('institution')); if (!$institution) { $response['state'] = 'Error'; $response['error'] = 'La institucion definida no fue encontrada en el sistema!'; return response()->json($response); } } // Loop through products and services and get prices. $items = json_decode(Input::get('items')); foreach ($items as $code => $info) { // Check if it is a product. $product = Stock::where('Code', '=', $code)->where('BranchId', '=', $userBranch)->first(); if (!$product) { // Check if it is a service. $service = Service::where('Code', '=', $code)->where('BranchId', '=', $userBranch)->first(); if (!$service) { $response['state'] = 'Error'; $response['error'] = 'No se reconocio uno de los productos o servicios!'; return response()->json($response); } // Add price to total of quotation. $quotePrice += $service->Price * $info->quantity; } else { // Add price to total of quotation. $quotePrice += $product->Price * $info->quantity; } } $subTotal = $quotePrice; $discount = $quotePrice * (Input::get('discount') / 100); // Give discount. $quotePrice = $quotePrice * (1 - Input::get('discount') / 100); // Calculate tax if required. $tax = 0; if (json_decode($branch->DefaultExpenses)->regimen == 'cuotageneral') { if (Input::get('institution') != 0) { if (!$institution->Excempt) { $tax = $salePrice * 0.15; } } else { if (!$client || !$client->Excempt) { $tax = $salePrice * 0.15; } } } // Save client information. $clientInfo = array(); if (Input::get('institution') != 0) { // Save institution information. $clientInfo = array('Name' => $institution->Name, 'Address' => $institution->Address, 'Id' => $institution->RUC); } else { // Save institution information. if ($client) { $clientInfo = array('Name' => $client->Name, 'Address' => $client->Address, 'Id' => $client->Cedula); } } // Get quote life span. $config = Configuration::find(0); // Make Quotation. $quotation; if (Input::get('institution') != 0) { // Creditor Type 1 = Client, 2 = Institution. $quotation = Quotation::create(array('WorkerId' => $worker->Id, 'Value' => $quotePrice, 'Discount' => Input::get('discount'), 'Tax' => $tax, 'CreditorId' => Input::get('institution'), 'CreditorType' => 2, 'State' => 'fresh', 'Life' => $config->QuoteLife)); } else { if (!$client) { $quotation = Quotation::create(array('WorkerId' => $worker->Id, 'Value' => $quotePrice, 'Discount' => Input::get('discount'), 'Tax' => $tax, 'CreditorId' => 0, 'CreditorType' => 1, 'State' => 'fresh', 'Life' => $config->QuoteLife)); } else { $quotation = Quotation::create(array('WorkerId' => $worker->Id, 'Value' => $quotePrice, 'Discount' => Input::get('discount'), 'Tax' => $tax, 'CreditorId' => $client->Id, 'CreditorType' => 1, 'State' => 'fresh', 'Life' => $config->QuoteLife)); } } // Loop through items and add them to Quotation Breakdown. foreach ($items as $code => $info) { // Check if product. $product = Stock::where('Code', '=', $code)->where('BranchId', '=', $userBranch)->first(); if (!$product) { // Get service cost. $service = Service::where('Code', '=', $code)->where('BranchId', '=', $userBranch)->first(); QuotationBreakdown::create(array('QuotationId' => $quotation->Id, 'Code' => $code, 'Quantity' => $info->quantity, 'Price' => $service->Price)); } else { QuotationBreakdown::create(array('QuotationId' => $quotation->Id, 'Code' => $code, 'Quantity' => $info->quantity, 'Price' => $product->Price)); } } // Return success message and return quotation information. $response['state'] = 'Success'; $response['quoteInfo'] = array('SubTotal' => $subTotal, 'Discount' => $discount, 'Tax' => $tax, 'Total' => $quotePrice + $tax, 'QuoteId' => $quotation->Id, 'Date' => date('d/m/Y'), 'Life' => $config->QuoteLife); $response['clientInfo'] = $clientInfo; return response()->json($response); }