#
#= manufacture_productsコントローラー
# Authors:: Kazunori Shimizu
# Copyright:: Copyright (C) OrbusNeich Medical K.K.  2010.
#--
#-------------------------------------------------------------------------------
#++
class ManufactureProductsController < Comm::BaseController::General
  #== コンストラクタ
  #-----------------------------------------------------------------#++
  def initialize
    @mcls = ManufactureProduct
  end
  
  def products_index
    products = @mcls.find(:all, :conditions=>['parent_product_id=?', params[:id]], :select => "id, product_set_id, product_id, quantity").only_hashfy
    respond_to do |format|
      format.ext_json{
        render :json => products.to_ext_json('manufacture_products')
      }
    end
  end
  
  def update
    parent_product_id = params[:id]
    unless params[:data].blank?
      products = ActiveSupport::JSON.decode(params[:data])
    end
    #既存データの削除
    unless ManufactureProduct.destroy_all(["parent_product_id = ?", parent_product_id])
      raise UserOperationError, EMJ0003 + EMD0001
    end
    # 新情報登録
    products.each do |product|
      if product['product_set_id'].blank? || product['product_id'].blank? || product['quantity'].blank?
        raise UserOperationError, EMJ0003 + EMD0001
      end
      result = ManufactureProduct.new.create_mng({:parent_product_id=>parent_product_id, :product_set_id => product['product_set_id'], :product_id=>product['product_id'], :quantity=>product['quantity']})
      unless result
        raise UserOperationError, EMJ0003 + EMD0001
      end
    end
    # 返却
    render :json => Comm::Tool::Json.result_json(true,"")
  rescue => e
    render :json => Comm::Tool::Json.result_json(false, e.message)
  end
end
