module Comee module Core class SalesOrderItemsController < ApplicationController include Common def index super do Comee::Core::SalesOrderItem.includes( :sales_order, :customer_order_item, :source, :unit ).where(sales_order_id: params[:id]) end end def suggest_values service = CustomerOrderService.new result = service.suggest_values(params[:id], params[:unit_id]) render json: {success: true, data: result} end def destroy Comee::Core::SalesOrderItem.destroy(params[:id]) render json: {success: true} end def filter_with_status items = SalesOrderItem.includes(:sales_order, :product, :unit) .ransack(params[:q]).result statuses = ItemStatus.where(sales_order_item_id: items.map(&:id)) .select("MAX(id) AS max_id, sales_order_item_id") .group(:sales_order_item_id) .all ids = statuses.map(&:max_id) statuses = ItemStatus.where(id: ids).each_with_object({}) { |status, res| res[status.sales_order_item_id] = status } result = items.each_with_object([]) do |item, res| status = statuses[item.id] data = { sales_order_no: item.sales_order.order_number, customer_item_no: item.customer_item_no, product_code: item.product.code, unit: item.unit.code, quantity: item.quantity, delivery_date: item.delivery_date, handover_date: item.sales_order.handover_date, po_reference: item.po_reference, status: status&.status } res << data.merge(status&.details) end render json: {success: true, data: result} end private def model_params params.require(:payload).permit(:sales_order_id, :customer_order_item_id, :product_id, :unit_id, :quantity, :quantity_delivered, :customer_item_no, :price, :delivery_date, :eb_number, :lead_time, :comment, :action_note, :po_reference, :source_id) end end end end