diff --git a/app/controllers/admin/warehouse/skus_controller.rb b/app/controllers/admin/warehouse/skus_controller.rb index 418f1da..c86a922 100644 --- a/app/controllers/admin/warehouse/skus_controller.rb +++ b/app/controllers/admin/warehouse/skus_controller.rb @@ -1,48 +1,12 @@ module Admin module Warehouse class SKUsController < Admin::ApplicationController - # Overwrite any of the RESTful controller actions to implement custom behavior - # For example, you may want to send an email after a foo is updated. - # - # def update - # super - # send_foo_updated_email(requested_resource) - # end - - # Override this method to specify custom lookup behavior. - # This will be used to set the resource for the `show`, `edit`, and `update` - # actions. - # - # def find_resource(param) - # Foo.find_by!(slug: param) - # end - - # The result of this lookup will be available as `requested_resource` - - # Override this if you have certain roles that require a subset - # this will be used to set the records shown on the `index` action. - # - # def scoped_resource - # if current_user.super_admin? - # resource_class - # else - # resource_class.with_less_stuff - # end - # end - - # Override `resource_params` if you want to transform the submitted - # data before it's persisted. For example, the following would turn all - # empty values into nil values. It uses other APIs such as `resource_class` - # and `dashboard`: - # - # def resource_params - # params.require(resource_class.model_name.param_key). - # permit(dashboard.permitted_attributes(action_name)). - # transform_values { |value| value == "" ? nil : value } - # end - - # See https://administrate-demo.herokuapp.com/customizing_controller_actions - # for more information + def sync_to_zenventory + requested_resource.sync_to_zenventory! + redirect_to [:admin, requested_resource], notice: "Synced to Zenventory" + rescue Zenventory::ZenventoryError => e + redirect_to [:admin, requested_resource], alert: "Zenventory sync failed: #{e.message}" + end end end end diff --git a/app/models/warehouse/sku.rb b/app/models/warehouse/sku.rb index 0a6470d..ac381e5 100644 --- a/app/models/warehouse/sku.rb +++ b/app/models/warehouse/sku.rb @@ -61,4 +61,23 @@ class Warehouse::SKU < ApplicationRecord } has_zenventory_url "https://app.zenventory.com/admin/item-details/%s/basic", :zenventory_id + + def sync_to_zenventory! + params = { + sku: sku, + description: name, + category: category&.to_s&.humanize, + active: enabled || false, + unitCost: declared_unit_cost, + userField1: country_of_origin, + userField2: hs_code, + }.compact + + if zenventory_id.present? + Zenventory.update_item(zenventory_id, params) + else + response = Zenventory.create_item(params) + update!(zenventory_id: response[:id].to_s) + end + end end diff --git a/app/views/admin/warehouse/skus/show.html.erb b/app/views/admin/warehouse/skus/show.html.erb new file mode 100644 index 0000000..ea4291b --- /dev/null +++ b/app/views/admin/warehouse/skus/show.html.erb @@ -0,0 +1,49 @@ +<% content_for(:title) { t("administrate.actions.show_resource", name: page.page_title) } %> +
+

+ <%= content_for(:title) %> +

+
+ <%= link_to( + t("administrate.actions.edit_resource", name: page.page_title), + [:edit, namespace, page.resource], + class: "button", + ) if accessible_action?(page.resource, :edit) %> + <%= button_to( + "Sync to Zenventory", + sync_to_zenventory_admin_warehouse_sku_path(page.resource), + method: :post, + class: "button button--secondary", + form: { style: "display: inline" }, + ) %> + <% if page.resource.zenventory_url.present? %> + <%= link_to( + "View on Zenventory", + page.resource.zenventory_url, + class: "button button--secondary", + target: "_blank", + ) %> + <% end %> + <%= link_to( + t("administrate.actions.destroy"), + [namespace, page.resource], + class: "button button--danger", + method: :delete, + data: { confirm: t("administrate.actions.confirm") } + ) if accessible_action?(page.resource, :destroy) %> +
+
+
+
+ <% page.attributes.each do |attribute| %> +
+ <%= t( + "helpers.label.#{resource_name}.#{attribute.name}", + default: page.resource.class.human_attribute_name(attribute.name), + ) %> +
+
<%= render_field attribute, page: page %>
+ <% end %> +
+
diff --git a/config/routes.rb b/config/routes.rb index b6ec3df..2a34eab 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -504,7 +504,11 @@ Rails.application.routes.draw do namespace :warehouse do resources :templates resources :orders - resources :skus + resources :skus do + member do + post :sync_to_zenventory + end + end end namespace :usps do