module Comee module Core class CustomerOrdersController < ApplicationController include Common before_action :set_service, only: %i[create_with_items submit submit_for_confirmation accept cancel] def index super do [ CustomerOrder.includes(:client, customer_order_items: :unit).order(:created_at), include: ["client", "customer_order_items.unit"] ] end end def show super do [ CustomerOrder.includes(:client, customer_order_items: :unit).find(params[:id]), include: ["client", "customer_order_items.unit"] ] end end def create_with_items order = @service.create(create_with_item_params) render json: {success: true, data: serialize(order)} end def submit order = @service.submit(params[:id]) render json: {success: true, data: serialize(order)} rescue StandardError => e render json: {success: false, error: e.message} end def submit_for_confirmation order = @service.submit_for_confirmation(params[:id]) render json: {success: true, data: serialize(order)} rescue StandardError => e render json: {success: false, error: e.message} end def accept order = @service.accept(params[:id], current_user.name) render json: {success: true, data: serialize(order)} rescue StandardError => e render json: {success: false, error: e.message} end def cancel order = @service.cancel(params[:id]) render json: {success: true, data: serialize(order)} rescue StandardError => e render json: {success: false, error: e.message} end def filter orders = CustomerOrder.includes(:client, :customer_order_items).ransack(params[:q]).result render_content(orders, include: ["client", "customer_order_items.unit"]) end private def model_params params.require(:payload) .permit( :order_number, :order_date, :order_terms, :consignee, :shipment_address, :final_destination, :final_delivery_date, :handover_date, :shipping_date, :delivery_address, :invoice_address, :status, :client_id, :file ) end def create_with_item_params params.require(:payload) .permit( :order_number, :order_date, :order_terms, :consignee, :shipment_address, :final_destination, :final_delivery_date, :handover_date, :shipping_date, :delivery_address, :invoice_address, :status, :client_id, items: %i[customer_item_no customer_item_description product_id quantity price total_price unit_id delivery_date] ) end def set_service @service = CustomerOrderService.new end end end end