spree-contrib / spree_drop_ship

Spree Drop Shipping Extension
BSD 3-Clause "New" or "Revised" License
104 stars 120 forks source link

[2-4-beta] Relating a variant to a supplier #56

Closed josetapadas closed 9 years ago

josetapadas commented 9 years ago

Hello,

I'm using this gem but I have a small question regarding the table spree_supplier_variants.

I'm not being able to add stock items to a specify supplier, receiving constantly the error: There was a problem saving this stock movement. Please try again. This happens because there is no relation generated between newly created Spree::Products (or Variants) and the suppliers. For example, this query does not return anything, as the table is empty, and rollback the whole transaction:

 SELECT "spree_suppliers".id FROM "spree_suppliers" INNER JOIN "spree_supplier_variants" ON 
"spree_suppliers"."id" = "spree_supplier_variants"."supplier_id" INNER JOIN "spree_variants" ON 
"spree_supplier_variants"."variant_id" = "spree_variants"."id" WHERE "spree_variants"."deleted_at" IS 
NULL AND "spree_variants"."product_id" = $1 AND "spree_variants"."is_master" = 't'  [["product_id", 7]]

When we use the sample data, there is a rake task to associate a variant to a specific supplier, using the proper method that you have added to the model:

    puts "Linking existing line items to suppliers"
    Spree::LineItem.all.each do |li|
      print "*" if li.product.add_supplier! @suppliers.shuffle.first.id and li.save
    end

But the same does not happen when we create suppliers and/or new products. I've come up with a simple solution that, when, creating a new Product, from the Products Admin page, it automatically is related to the supplier that is creating it:

Spree::Admin::ProductsController.class_eval do
  create.after :add_product_to_supplier

  private
  def add_product_to_supplier
    if try_spree_current_user && try_spree_current_user.supplier?
      @product.add_supplier!(try_spree_current_user.supplier_id)
    end
  end
end

And I could start Defacing the views in order to add an extra field so the Variants can be associated to a specific supplier, but before that, could you please help me and tell me if I'm missing something and this behaviours is expected. If so, how do you think we should associate the suppliers with the products via the admin UI.

Greets and thanks in advance, José

JDutil commented 9 years ago

I've stopped working on this extension as I'm no longer developing applications that use it.

Suppliers shouldn't be able to add new products within this extension anymore that was moved to spree_marketplace so I think you're solution would be more appropriate there.

josetapadas commented 9 years ago

Hey, thank you @JDutil !

The thing is, I really don't want to use Stripe (I've found out that there are other gems that extend spree_drop_ship and use other Stripe-like systems, but even that is not what I'm looking for). But I've already read what you have done, for instance, with the ProductsController and it is (almost) exactly the same I've wrote here:

Spree::Admin::ProductsController.class_eval do
  create.after :set_supplier

  private
    def set_supplier
      if try_spree_current_user.supplier?
        @object.add_supplier! spree_current_user.supplier
      end
    end
end

Even though you stopped working with this extension I think it would be nice to have this snippet on the ProductsController otherwise the logic is not that useful (besides the sample suppliers). I will create just a simple PR for the 2-4 branch if you don't mind.

By the way, thank you very much in advance :)