Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views3 pages

Profilecontroller (Copy)

The document outlines methods for managing quotes in a Laravel application, including printing a quote view and generating a PDF version. It captures user inputs for delivery, lead time, and payment terms, and saves the generated PDF to storage. Additionally, it includes functionality to check for existing quote numbers and to store new quotes with associated products in the database, handling transactions and errors appropriately.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Profilecontroller (Copy)

The document outlines methods for managing quotes in a Laravel application, including printing a quote view and generating a PDF version. It captures user inputs for delivery, lead time, and payment terms, and saves the generated PDF to storage. Additionally, it includes functionality to check for existing quote numbers and to store new quotes with associated products in the database, handling transactions and errors appropriately.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

use Barryvdh\DomPDF\Facade\Pdf;

use Illuminate\Support\Facades\Storage;

public function printView($id)


{
$quote = Quote::with('products.product.images')->findOrFail($id);

return view('quotes.print', [
'quote' => $quote,
'isPdf' => false,
]);
}

public function generateQuotePdf(Request $request, $quoteId)


{
$quote = Quote::findOrFail($quoteId);

// Capture user-edited values


$delivery = $request->input('delivery', 'Free in Dubai');
$leadTime = $request->input('lead_time', '1–2 weeks');
$paymentTerms = $request->input('payment_terms', '100% advance');

$html = view('quotes.print', [
'quote' => $quote,
'isPdf' => true,
'delivery' => $delivery,
'lead_time' => $leadTime,
'payment_terms' => $paymentTerms,
])->render();

$pdf = Pdf::loadHtml($html)->setOptions([
'isHtml5ParserEnabled' => true,
'isRemoteEnabled' => true,
]);

$filename = 'quotation_' . $quote->number . '.pdf';


$filePath = 'public/pdfs/' . $filename;

Storage::makeDirectory('public/pdfs');
Storage::put($filePath, $pdf->output());

$publicUrl = asset('storage/pdfs/' . $filename);

return response()->json([
'whatsapp_link' => "https://wa.me/?text=Download%20your%20quotation:
%20" . urlencode($publicUrl),
]);
}

public function checkNumber($number, Request $request)


{
$ignoreId = $request->query('ignore_id');

$exists = Quote::where('number', $number)


->when($ignoreId, fn($q) => $q->where('id', '!=', $ignoreId))
->exists();

return response()->json(['exists' => $exists]);


}

public function store(Request $request)


{
$request->validate([
'number' => [
'required',
'numeric',
function ($attribute, $value, $fail) {
if (Quote::where('number', $value)->exists()) {
$fail("The quote number '{$value}' is already
used.");
}
},
],

]);
try {
DB::beginTransaction();
//dd($request->all());
$quote = Quote::create([
'company_name' => $request->company_name,
'address' => $request->address,
'person_name' => $request->person_name,
'grand_total' => $request->grand_total,
'subtotal'=>$request->subtotal,
'vat_amount'=>$request->vat_amount,
'discount_amount'=>$request->discount_amount,
'discount_type' => $request->discount_type,
'discount_value' => $request->discount_value_input,
'final_total' => $request->final_total,
'date' => $request->date,
'number' => $request->number,
'created_by' => auth()->id(),
'updated_by' => auth()->id(),
]);

//dd($request->products);
if (!empty($request->products)) {

foreach ($request->products as $product) {

QProduct::create([
'quote_id' => $quote->id,
'product_id' => $product['product_id'],
'variation_id' => $product['variation_id'] ?? null,
'custom_name' => $product['custom_name'] ?? null,
'price' => $product['price'],
'quantity' => $product['quantity'],
'total' => $product['price'] * $product['quantity'],
]);
}
} else {

}
//dd($products->all());
DB::commit();
//return redirect()->route('admin.quotes.index')->with('success',
'Quote saved successfully!');
return redirect()->route('admin.quotes.quotes')->with('success',
'Quote saved successfully!');

} catch (\Exception $e) {


// dd( $e->getMessage());
DB::rollBack();
return back()->with('error', 'Error: ' . $e->getMessage());
}
}

You might also like