dbfdg
can('manage quotation'))
{
$quotations = Quotation::where('created_by', \Auth::user()->creatorId())->with(['customer','warehouse'])->get();
return view('quotation.index',compact('quotations'));
}
else
{
return redirect()->back()->with('error', __('Permission denied.'));
}
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
if(\Auth::user()->can('create quotation'))
{
$customers = Customer::where('created_by', \Auth::user()->creatorId())->get()->pluck('name', 'id');
$customers->prepend('Select Customer', '');
$warehouse = warehouse::where('created_by', \Auth::user()->creatorId())->get()->pluck('name', 'id');
$warehouse->prepend('Select Warehouse', '');
// $product_services = ['--'];
$quotation_number = \Auth::user()->quotationNumberFormat($this->quotationNumber());
return view('quotation.create', compact('customers','warehouse' , 'quotation_number'));
}
else
{
return response()->json(['error' => __('Permission denied.')], 401);
}
}
public function quotationCreate(Request $request)
{
$customer = Customer::find($request->customer_id);
$warehouse = warehouse::find($request->warehouse_id);
$quotation_date = $request->quotation_date;
$quotation_number = $request->quotation_number;
$warehouseProducts = WarehouseProduct::where('created_by', '=', \Auth::user()->creatorId())->where('warehouse_id',$request->warehouse_id)->get()->pluck('product_id')->toArray();
$product_services = ProductService::where('created_by', \Auth::user()->creatorId())->whereIn('id',$warehouseProducts)->where('type','!=', 'service')->get()->pluck('name', 'id');
$product_services->prepend(' -- ', '');
return view('quotation.quotation_create', compact('customer','warehouse' ,'quotation_date' , 'quotation_number','product_services'));
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
if(\Auth::user()->can('create quotation'))
{
$validator = \Validator::make(
$request->all(), [
'customer_id' => 'required',
'warehouse_id' => 'required',
'quotation_date' => 'required',
'items' => 'required',
]
);
if($validator->fails())
{
$messages = $validator->getMessageBag();
return redirect()->back()->with('error', $messages->first());
}
$customer = Customer::where('name',$request->customer_id)->first();
$warehouse = warehouse::where('name',$request->warehouse_id)->first();
$quotations = new Quotation();
$quotations->quotation_id = $this->quotationNumber();
$quotations->customer_id = $customer->id;
$quotations->warehouse_id = $warehouse->id;
$quotations->quotation_date = $request->quotation_date;
$quotations->status = 0;
$quotations->category_id = 0;
$quotations->created_by = \Auth::user()->creatorId();
$quotations->save();
$products = $request->items;
for($i = 0; $i < count($products); $i++)
{
$quotationItems = new QuotationProduct();
$quotationItems->quotation_id = $quotations->id;
$quotationItems->product_id = $products[$i]['item'];
$quotationItems->price = $products[$i]['price'];
$quotationItems->quantity = $products[$i]['quantity'];
$quotationItems->tax = $products[$i]['tax'];
$quotationItems->discount = $products[$i]['discount'];
$quotationItems->save();
}
return redirect()->route('quotation.index', $quotations->id)->with('success', __('Quotation successfully created.'));
}
else
{
return redirect()->back()->with('error', __('Permission denied.'));
}
}
/**
* Display the specified resource.
*/
public function show($ids)
{
if (\Auth::user()->can('show quotation') || \Auth::user()->type == 'company') {
try {
$id = Crypt::decrypt($ids);
} catch (\Throwable $th) {
return redirect()->back()->with('error', __('Quotation Not Found.'));
}
$id = Crypt::decrypt($ids);
$quotation = Quotation::find($id);
if ($quotation->created_by == \Auth::user()->creatorId()) {
$quotationPayment = PosPayment::where('pos_id', $quotation->id)->first();
$customer = $quotation->customer;
$iteams = $quotation->items;
return view('quotation.view', compact('quotation', 'customer', 'iteams', 'quotationPayment'));
} else {
return redirect()->back()->with('error', __('Permission denied.'));
}
} else {
return redirect()->back()->with('error', __('Permission denied.'));
}
}
/**
* Show the form for editing the specified resource.
*/
public function edit($ids)
{
if(\Auth::user()->can('edit quotation'))
{
$id = Crypt::decrypt($ids);
$quotation = Quotation::find($id);
$customer = Customer::where('id',$quotation->customer_id)->first();
$warehouse = warehouse::where('id',$quotation->warehouse_id)->first();
$warehouseProducts = WarehouseProduct::where('created_by', '=', \Auth::user()->creatorId())->where('warehouse_id',$quotation->warehouse_id)->get()->pluck('product_id')->toArray();
$product_services = ProductService::where('created_by', \Auth::user()->creatorId())->whereIn('id',$warehouseProducts)->where('type','!=', 'service')->get()->pluck('name', 'id');
$product_services->prepend(' -- ', '');
$quotation_number = \Auth::user()->quotationNumberFormat($this->quotationNumber());
return view('quotation.edit', compact('customer', 'product_services','warehouse' , 'quotation_number' , 'quotation'));
}
else
{
return response()->json(['error' => __('Permission denied.')], 401);
}
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Quotation $quotation)
{
if(\Auth::user()->can('edit quotation'))
{
if($quotation->created_by == \Auth::user()->creatorId())
{
$validator = \Validator::make(
$request->all(), [
'customer_id' => 'required',
'quotation_date' => 'required',
'items' => 'required',
]
);
if($validator->fails())
{
$messages = $validator->getMessageBag();
return redirect()->route('quotation.index')->with('error', $messages->first());
}
$customer = Customer::where('name',$request->customer_id)->first();
$warehouse = warehouse::where('name',$request->warehouse_id)->first();
$quotation->customer_id = $customer->id;
$quotation->warehouse_id = $warehouse->id;
$quotation->quotation_date = $request->quotation_date;
$quotation->status = 0;
$quotation->category_id = 0;
$quotation->created_by = \Auth::user()->creatorId();
$quotation->save();
$products = $request->items;
for($i = 0; $i < count($products); $i++)
{
$quotationProduct = QuotationProduct::find($products[$i]['id']);
if($quotationProduct == null)
{
$quotationProduct = new QuotationProduct();
$quotationProduct->quotation_id = $quotation->id;
}
if(isset($products[$i]['item']))
{
$quotationProduct->product_id = $products[$i]['item'];
}
$quotationProduct->quantity = $products[$i]['quantity'];
$quotationProduct->tax = $products[$i]['tax'];
$quotationProduct->discount = $products[$i]['discount'];
$quotationProduct->price = $products[$i]['price'];
$quotationProduct->description = $products[$i]['description'];
$quotationProduct->save();
}
return redirect()->route('quotation.index')->with('success', __('Quotation successfully updated.'));
}
else
{
return redirect()->back()->with('error', __('Permission denied.'));
}
}
else
{
return redirect()->back()->with('error', __('Permission denied.'));
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Quotation $quotation)
{
if(\Auth::user()->can('delete quotation'))
{
if($quotation->created_by == \Auth::user()->creatorId())
{
$quotation->delete();
QuotationProduct::where('quotation_id', '=', $quotation->id)->delete();
return redirect()->route('quotation.index')->with('success', __('Quotation successfully deleted.'));
}
else
{
return redirect()->back()->with('error', __('Permission denied.'));
}
}
else
{
return redirect()->back()->with('error', __('Permission denied.'));
}
}
function quotationNumber()
{
$latest = Quotation::where('created_by', '=', \Auth::user()->creatorId())->latest()->first();
if(!$latest)
{
return 1;
}
return $latest->quotation_id + 1;
}
public function product(Request $request)
{
$data['product'] = $product = ProductService::find($request->product_id);
$data['unit'] = !empty($product->unit) ? $product->unit->name : '';
$data['taxRate'] = $taxRate = !empty($product->tax_id) ? $product->taxRate($product->tax_id) : 0;
$data['taxes'] = !empty($product->tax_id) ? $product->tax($product->tax_id) : 0;
$salePrice = $product->sale_price;
$quantity = 1;
$taxPrice = ($taxRate / 100) * ($salePrice * $quantity);
$data['totalAmount'] = ($salePrice * $quantity);
$product = ProductService::find($request->product_id);
$productquantity = 0;
if ($product) {
$productquantity = $product->getQuantity();
}
$data['productquantity'] = $productquantity;
return json_encode($data);
}
public function productDestroy(Request $request)
{
if(\Auth::user()->can('delete quotation'))
{
QuotationProduct::where('id', '=', $request->id)->delete();
return redirect()->back()->with('success', __('Quotation product successfully deleted.'));
}
else
{
return redirect()->back()->with('error', __('Permission denied.'));
}
}
public function items(Request $request)
{
$items = QuotationProduct::where('quotation_id', $request->quotation_id)->where('product_id', $request->product_id)->first();
return json_encode($items);
}
public function productQuantity(Request $request)
{
$product = ProductService::find($request->item_id);
$productquantity = 0;
if ($product) {
$productquantity = $product->getQuantity();
}
return json_encode($productquantity);
}
public function previewQuotation($template, $color)
{
$objUser = \Auth::user();
$settings = Utility::settings();
$quotation = new Quotation();
$quotationPayment = new posPayment();
$quotationPayment->amount = 360;
$quotationPayment->discount = 100;
$customer = new \stdClass();
$customer->email = '
' . $details['customer']['billing_phone'] . '
' . '' . $details['customer']['billing_address'] . '
' . '' . $details['customer']['billing_city'] . $details['customer']['billing_state'] . '
' . '' . $details['customer']['billing_country'] . '
' . '' . $details['customer']['billing_zip'] . '
'; $shippdetails = '' . $details['customer']['shipping_phone'] . '
' . '' . $details['customer']['shipping_address'] . '
' . '' . $details['customer']['shipping_city'] . $details['customer']['shipping_state'] . '
' . '' . $details['customer']['shipping_country'] . '
' . '' . $details['customer']['shipping_zip'] . '
'; } else { $customerdetails = '' . $settings['company_name'] . $settings['company_telephone'] . '
' . '' . $settings['company_address'] . '
' . '' . $settings['company_city'] . $settings['company_state'] . '
' . '' . $settings['company_country'] . '
' . '' . $settings['company_zipcode'] . '
'; $details['customer']['details'] = $customerdetails; $details['warehouse']['details'] = $warehousedetails; // $details['customer']['shippdetails'] = $shippdetails; $details['user']['details'] = $userdetails; $mainsubtotal = 0; $sales = []; foreach ($sess as $key => $value) { $subtotal = $value['price'] * $value['quantity']; $tax = ($subtotal * $value['tax']) / 100; $sales['data'][$key]['name'] = $value['name']; $sales['data'][$key]['quantity'] = $value['quantity']; $sales['data'][$key]['price'] = Auth::user()->priceFormat($value['price']); $sales['data'][$key]['tax'] = $value['tax'] . '%'; $sales['data'][$key]['product_tax'] = $value['product_tax']; $sales['data'][$key]['tax_amount'] = Auth::user()->priceFormat($tax); $sales['data'][$key]['subtotal'] = Auth::user()->priceFormat($value['subtotal']); $mainsubtotal += $value['subtotal']; } $discount = !empty($request->discount) ? $request->discount : 0; $sales['discount'] = Auth::user()->priceFormat($discount); $total = $mainsubtotal - $discount; $sales['sub_total'] = Auth::user()->priceFormat($mainsubtotal); $sales['total'] = Auth::user()->priceFormat($total); //for barcode $productServices = ProductService::where('created_by', '=', \Auth::user()->creatorId())->get(); $barcode = [ 'barcodeType' => Auth::user()->barcodeType(), 'barcodeFormat' => Auth::user()->barcodeFormat(), ]; return view('quotation.printview', compact('details', 'sales', 'customer', 'productServices', 'barcode')); } public function quotation($quotation_Id) { $settings = Utility::settings(); $quotationId = Crypt::decrypt($quotation_Id); $quotation = Quotation::where('id', $quotationId)->first(); $data = \DB::table('settings'); $data = $data->where('created_by', '=', $quotation->created_by); $data1 = $data->get(); foreach ($data1 as $row) { $settings[$row->name] = $row->value; } $customer = $quotation->customer; $totalTaxPrice = 0; $totalQuantity = 0; $totalRate = 0; $totalDiscount = 0; $taxesData = []; $items = []; foreach ($quotation->items as $product) { $item = new \stdClass(); $item->name = !empty($product->product()) ? $product->product()->name : ''; $item->quantity = $product->quantity; $item->tax = $product->tax; $item->discount = $product->discount; $item->price = $product->price; $item->description = $product->description; $totalQuantity += $item->quantity; $totalRate += $item->price; $totalDiscount += $item->discount; $taxes = Utility::tax($product->tax); $itemTaxes = []; if (!empty($item->tax)) { foreach ($taxes as $tax) { $taxPrice = Utility::taxRate($tax->rate, $item->price, $item->quantity); $totalTaxPrice += $taxPrice; $itemTax['name'] = $tax->name; $itemTax['rate'] = $tax->rate . '%'; $itemTax['price'] = Utility::priceFormat($settings, $taxPrice); $itemTaxes[] = $itemTax; if (array_key_exists($tax->name, $taxesData)) { $taxesData[$tax->name] = $taxesData[$tax->name] + $taxPrice; } else { $taxesData[$tax->name] = $taxPrice; } } $item->itemTax = $itemTaxes; } else { $item->itemTax = []; } $items[] = $item; } $quotation->itemData = $items; $quotation->totalTaxPrice = $totalTaxPrice; $quotation->totalQuantity = $totalQuantity; $quotation->totalRate = $totalRate; $quotation->totalDiscount = $totalDiscount; $quotation->taxesData = $taxesData; $logo = asset(Storage::url('uploads/logo/')); $company_logo = Utility::getValByName('company_logo_dark'); $quotation_logo = Utility::getValByName('quotation_logo'); if (isset($quotation_logo) && !empty($quotation_logo)) { $img = Utility::get_file('quotation_logo/') . $quotation_logo; } else { $img = asset($logo . '/' . (isset($company_logo) && !empty($company_logo) ? $company_logo : 'logo-dark.png')); } if ($quotation) { $color = '#' . $settings['quotation_color']; $font_color = Utility::getFontColor($color); return view('quotation.templates.' . $settings['quotation_template'], compact('quotation', 'color', 'settings', 'customer', 'img', 'font_color')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } }