module Comee module Core class SalesOrdersController < ApplicationController include Common before_action :set_service, only: %i[submit confirm accept cancel] def index super do data = SalesOrder.includes( :customer_order, :client, :customs_detail, sales_order_items: :unit ).order(created_at: :desc) [ data, fields: [ "client", "customer_order", "sales_order_items", "sales_order_items.product", "sales_order_items.unit", "customs_detail" ] ] end end def show super do [ SalesOrder.includes(:customer_order, :client, :customs_detail, sales_order_items: :unit).find(params[:id]), fields: [ "client", "customer_order", "sales_order_items", "sales_order_items.product", "sales_order_items.unit", "customs_detail" ] ] end end def update super do {fields: ["client", "customer_order", "sales_order_items", "sales_order_items.product", "sales_order_items.unit"]} end end def filter data = SalesOrder.includes( :customer_order, :client, :customs_detail, sales_order_items: :unit ).ransack(params[:q]).result render_content( data, fields: ["client", "customer_order", "sales_order_items.product", "sales_order_items.unit", "customs_detail"] ) end def submit order = @service.submit(params[:id]) render_content(order) rescue StandardError => e render json: {success: false, error: e.message} end def confirm order = @service.confirm(params[:id]) render_content(order) rescue StandardError => e render json: {success: false, error: e.message} end def accept order = @service.accept(params[:id]) render_content(order) rescue StandardError => e render json: {success: false, error: e.message} end def cancel order = @service.cancel(params[:id]) render_content(order) rescue StandardError => e render json: {success: false, error: e.message} end def create_back_order service = SalesOrderService.new result = service.create_back_orders(back_order_params[:payload]) if result[:success] render json: {success: true} else render json: {success: false, error: result[:error]} end end private def model_params params.require(:payload).permit(:order_number, :order_date, :customer_order_id, :fulfillment_center_id, :client_id, :order_terms, :shipment_address, :delivery_address, :invoice_address, :destination, :handover_date, :shipping_date, :delivery_date, :consignee, :payment_term, :delivery_term, :payment_penalty, :status, :total_price, :amount_paid, :pallete_note, :remark, files: []) end def back_order_params params.permit(payload: [:supplier_id, :order_number, :order_date, :delivery_date, :delivery_address, :invoice_address, back_order_items: %i[product_id requested_quantity requested_price supplier_quantity delivery_date]]) end def set_service @service = SalesOrderService.new end end end end