ref: master
plugins/suppliers/controllers/myprofile/suppliers_plugin/product_controller.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
class SuppliersPlugin::ProductController < MyProfileController include SuppliersPlugin::TranslationHelper no_design_blocks protect 'edit_profile', :profile serialization_scope :view_context helper SuppliersPlugin::TranslationHelper helper SuppliersPlugin::DisplayHelper def index end def create supplier = SuppliersPlugin::Supplier.find params[:product][:supplier_id] base_product = supplier.products.build base_product_params params base_product.save! @product = base_product.distribute_to_consumer profile, distributed_product_params(params) render json: @product.reload, serializer: SuppliersPlugin::ProductSerializer end def edit @product = profile.products.supplied.find params[:id] @product.update params.require(:product).permit(:name, :description, :product_category_id, :margin_percentage, :available, :unit_id, :supplier_price) render json: @product.reload, serializer: SuppliersPlugin::ProductSerializer end def set_image params[:image_builder] = {uploaded_data: params[:image_builder]} if params[:image_builder].present? @product = profile.products.supplied.find params[:id] @product.update params.permit(image_builder: [:uploaded_data]) render json: @product, serializer: SuppliersPlugin::ProductSerializer end def unavailable params[:available] = 'false' respond_to do |format| format.js{ render partial: 'suppliers_plugin/product/search' } end end def import if params[:csv].present? if params[:remove_all_suppliers] == 'true' profile.suppliers.except_self.find_each(batch_size: 40){ |s| s.delay.destroy } end SuppliersPlugin::Import.delay.products profile, params[:csv].read respond_to{ |format| format.js{ render nothing: true } } end end def destroy @product = SuppliersPlugin::DistributedProduct.find params[:id] @product.destroy render text: t('controllers.myprofile.product_controller.product_removed_succe') end def distribute_to_consumers params[:consumers] ||= {} @product = profile.products.find params[:id] @consumers = profile.consumers.find(params[:consumers].keys.to_a).collect(&:profile) to_add = @consumers - @product.consumers to_remove = @product.consumers - @consumers to_add.each{ |c| @product.distribute_to_consumer c } to_remove = to_remove.to_set @product.to_products.each{ |p| p.destroy if to_remove.include? p.profile } @product.reload end def activate ret = Product.where(id: params[:ids]).update_all(available: true) render text: ret > 0 ? "success" : "fail" end def deactivate ret = Product.where(id: params[:ids]).update_all(available: false) render text: ret > 0 ? "success" : "fail" end def categories @categories = environment.product_categories.where("LOWER(name) like ?", "%#{params[:query].downcase}%") render json: @categories, each_serializer: SuppliersPlugin::ProductCategorySerializer end def createAllocation @product = profile.products.find params[:product_id] @product.update_attribute(:use_stock, params[:use_stock] == 'true') if params[:use_stock].present? if params[:place_id].nil? && @product.stock_places.count > 0 params[:place_id] = @product.stock_places.first.id end m = params[:stock_action] == "adition" ? 1 : -1 if params[:use_stock] == 'true' a = @product.stock_allocations.create!( quantity: m * params[:quantity].to_f.abs, description: params[:description], place_id: params[:place_id] ) render text: "fail" if !a end render json: @product end protected def base_product_params params params[:price] = params[:supplier_price] params.require(:product).permit(:product_category_id, :description, :name, :available, :price, :image_id, :unit_id) end def distributed_product_params params params.require(:product).permit(:product_category_id, :description, :name, :available, :price, :image_id, :margin_percentage, :use_stock, :initial_stock, :unit_id) end extend HMVC::ClassMethods hmvc SuppliersPlugin # inherit routes from core skipping use_relative_controller! def url_for options options[:controller] = "/#{options[:controller]}" if options.is_a? Hash and options[:controller] and not options[:controller].to_s.starts_with? '/' super options end helper_method :url_for end |