module Comee module Core class QuotationRequestsController < ApplicationController include Common def index super do QuotationRequest.includes(:client).all end end def filter data = QuotationRequest.includes(:client).ransack(params[:q]).result render_content(data) end def rfq_products_for_client client_products = ClientPrice.includes(:product).where(client_id: params[:id]).map(&:product_id) root = Product.roots.first ids = root.indirect_ids - client_products products = Product.where(id: ids) render_content(products) end def create_request_with_items service = QuotationRequestService.new parameters = rfq_params.to_h.deep_symbolize_keys result = service.create_request_with_items(parameters) render_content(result) end def submit service = QuotationRequestService.new rfq = service.submit(params[:id]) render_content(rfq) rescue StandardError => e render json: {success: false, error: e.message} end def submit_for_confirmation service = QuotationRequestService.new rfq = service.submit_for_confirmation(params[:id]) render_content(rfq) rescue StandardError => e render json: {success: false, error: e.message} end def confirm service = QuotationRequestService.new rfq = service.confirm(params[:id]) render_content(rfq) rescue StandardError => e render json: {success: false, error: e.message} end def convert service = QuotationRequestService.new order = service.convert_to_order(params[:id]) render_content(order) rescue StandardError => e render json: {success: false, error: e.message} end private def model_params params.require(:payload) .permit(:reference_no, :order_number, :order_date, :delivery_address, :invoice_address, :status, :client_id) end def rfq_params params.require(:payload).permit(:client_id, :order_number, :order_date, :delivery_address, :invoice_address, items: %i[product_id unit_id customer_item_no quantity expected_delivery_date]) end end end end