Sha256: e27b77791fa051fe29580af2f18798b8ebc9db339e4c402ba02cda17593dd16c

Contents?: true

Size: 1.68 KB

Versions: 2

Compression:

Stored size: 1.68 KB

Contents

module Comee
  module Core
    class ShipmentItemsController < ApplicationController
      include Common

      def filter_with_status
        items = ShipmentItem.includes(sales_order_item: %i[sales_order product unit])
                            .ransack(params[:q]).result
        statuses = ItemStatus.where(shipment_item_id: items.map(&:id))
                             .select("MAX(id) AS max_id, shipment_item_id")
                             .group(:shipment_item_id)
                             .all
        ids = statuses.map(&:max_id)
        statuses = ItemStatus.where(id: ids).each_with_object({}) { |status, res| res[status.shipment_item_id] = status }
        result = items.each_with_object([]) do |item, res|
          status = statuses[item.id]
          data = {
            sales_order_no: item.sales_order_item.sales_order.order_number,
            customer_item_no: item.sales_order_item.customer_item_no,
            product_code: item.sales_order_item.product.code,
            unit: item.sales_order_item.unit.code,
            quantity: item.sales_order_item.quantity,
            delivery_date: item.sales_order_item.delivery_date,
            handover_date: item.sales_order_item.sales_order.handover_date,
            po_reference: item.sales_order_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_item_id, :quantity, :shipment_status, :status, :handover_date, :delivery_date,
                                        :pallet_no)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
comee_core-0.1.69 app/controllers/comee/core/shipment_items_controller.rb
comee_core-0.1.68 app/controllers/comee/core/shipment_items_controller.rb