From fd78969be4c9b17c5da2c037af3f5f717e3ddadd Mon Sep 17 00:00:00 2001 From: tk <87005466+tykeaboyloy@users.noreply.github.com> Date: Wed, 6 Sep 2023 12:48:36 +0700 Subject: [PATCH 01/47] close #532 transit route setup (#544) * close #532 setup new route for Transit * close #532 setup new route for Transit --------- Co-authored-by: Regene27 --- .../spree/transit/base_controller.rb | 19 +++++ .../spree/transit/reservations_controller.rb | 15 ++++ app/views/spree/layouts/transit.html.erb | 74 +++++++++++++++++++ .../spree/transit/reservations/index.html.erb | 5 ++ .../spree/transit/shared/_main_menu.html.erb | 16 ++++ config/locales/en.yml | 2 + config/routes.rb | 5 ++ 7 files changed, 136 insertions(+) create mode 100644 app/controllers/spree/transit/base_controller.rb create mode 100644 app/controllers/spree/transit/reservations_controller.rb create mode 100644 app/views/spree/layouts/transit.html.erb create mode 100644 app/views/spree/transit/reservations/index.html.erb create mode 100644 app/views/spree/transit/shared/_main_menu.html.erb diff --git a/app/controllers/spree/transit/base_controller.rb b/app/controllers/spree/transit/base_controller.rb new file mode 100644 index 000000000..2fe74cc01 --- /dev/null +++ b/app/controllers/spree/transit/base_controller.rb @@ -0,0 +1,19 @@ +module Spree + module Transit + class BaseController < Spree::Admin::ResourceController + helper_method :current_vendor, :vendors + layout 'spree/layouts/transit' + + def vendors + @vendors ||= spree_current_user.vendors.to_a + end + + def current_vendor + @current_vendor ||= vendors.find { |v| v[:slug] == session[:transit_current_vendor_slug] } || vendors.first + session[:transit_current_vendor_slug] ||= @current_vendor&.slug + + @current_vendor + end + end + end +end diff --git a/app/controllers/spree/transit/reservations_controller.rb b/app/controllers/spree/transit/reservations_controller.rb new file mode 100644 index 000000000..7cd03c0c0 --- /dev/null +++ b/app/controllers/spree/transit/reservations_controller.rb @@ -0,0 +1,15 @@ +module Spree + module Transit + class ReservationsController < Spree::Transit::BaseController + def index; end + + def model_class + Spree::Order + end + + def object_name + 'order' + end + end + end +end diff --git a/app/views/spree/layouts/transit.html.erb b/app/views/spree/layouts/transit.html.erb new file mode 100644 index 000000000..208b60acf --- /dev/null +++ b/app/views/spree/layouts/transit.html.erb @@ -0,0 +1,74 @@ + + + + <%= render partial: 'spree/admin/shared/head' %> + <%= yield :extra_head if content_for?(:extra_head) %> + + + <%#-------------------------------------------------%> + <%# Header navbar %> + <%#-------------------------------------------------%> + <%= render partial: 'spree/transit/shared/header' %> + <%#-------------------------------------------------%> + <%# Main content %> + <%#-------------------------------------------------%> +
+
+ <%#-------------------------------------------------%> + <%# Sidebar %> + <%#-------------------------------------------------%> + + <%#-------------------------------------------------%> + <%# Content %> + <%#-------------------------------------------------%> +
+
+ <%#-------------------------------------------------%> + <%# Content header (page title/actions) %> + <%#-------------------------------------------------%> + <%= render partial: 'spree/admin/shared/content_header' %> +
+ <%#-------------------------------------------------%> + <%# Main content %> + <%#-------------------------------------------------%> +
+
+ <%= render partial: 'spree/admin/shared/table_filter' if content_for?(:table_filter) %> + <%= yield %> +
+
+ <%#-------------------------------------------------%> + <%# Inner aside %> + <%#-------------------------------------------------%> + <% if content_for?(:sidebar) || content_for?(:page_actions) %> +
+ <%= render partial: 'spree/admin/shared/sidebar' %> +
+ <% end %> +
+
+
+
+
+ <%#-------------------------------------------------%> + <%# Insert footer scripts here %> + <%#-------------------------------------------------%> + +
+ <%#-------------------------------------------------%> + <%# Alerts %> + <%#-------------------------------------------------%> +
+ <%= flash_alert(flash) %> +
+ + diff --git a/app/views/spree/transit/reservations/index.html.erb b/app/views/spree/transit/reservations/index.html.erb new file mode 100644 index 000000000..7dca7d7e2 --- /dev/null +++ b/app/views/spree/transit/reservations/index.html.erb @@ -0,0 +1,5 @@ +<% content_for :page_title do %> + <%= I18n.t('spree.transit.reservations') %> +<% end %> +<% content_for :page_actions do %> +<% end %> diff --git a/app/views/spree/transit/shared/_main_menu.html.erb b/app/views/spree/transit/shared/_main_menu.html.erb new file mode 100644 index 000000000..970cb76d7 --- /dev/null +++ b/app/views/spree/transit/shared/_main_menu.html.erb @@ -0,0 +1,16 @@ + diff --git a/config/locales/en.yml b/config/locales/en.yml index 76cd7337a..e1b721f24 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -191,6 +191,8 @@ en: other: "Only %{count} rooms available on %{date}" auto_apply: "Auto apply" auto_apply_info: "Path & code will be removed" + transit: + reservations: "Reservations" activerecord: attributes: diff --git a/config/routes.rb b/config/routes.rb index b09684221..ac4c3a835 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -145,6 +145,11 @@ end end + namespace :transit do + resources :reservations + root to: redirect('/transit/reservations') + end + namespace :api, defaults: { format: 'json' } do namespace :v2 do namespace :storefront do From c1d952a548d4466c9b84bce044e896801f93b528 Mon Sep 17 00:00:00 2001 From: tk <87005466+tykeaboyloy@users.noreply.github.com> Date: Wed, 6 Sep 2023 14:51:04 +0700 Subject: [PATCH 02/47] close #546 can now create new location (#548) --- .../spree/transit/locations_controller.rb | 32 +++++++++ .../spree_cm_commissioner/state_decorator.rb | 1 + .../spree/transit/locations/_form.html.erb | 19 +++++ .../spree/transit/locations/edit.html.erb | 15 ++++ .../spree/transit/locations/index.html.erb | 71 +++++++++++++++++++ .../spree/transit/locations/new.html.erb | 17 +++++ .../spree/transit/shared/_header.html.erb | 20 ++++++ .../spree/transit/shared/_main_menu.html.erb | 3 + .../transit/shared/_vendor_switcher.html.erb | 41 +++++++++++ config/locales/en.yml | 2 + config/routes.rb | 1 + 11 files changed, 222 insertions(+) create mode 100644 app/controllers/spree/transit/locations_controller.rb create mode 100644 app/views/spree/transit/locations/_form.html.erb create mode 100644 app/views/spree/transit/locations/edit.html.erb create mode 100644 app/views/spree/transit/locations/index.html.erb create mode 100644 app/views/spree/transit/locations/new.html.erb create mode 100644 app/views/spree/transit/shared/_header.html.erb create mode 100644 app/views/spree/transit/shared/_vendor_switcher.html.erb diff --git a/app/controllers/spree/transit/locations_controller.rb b/app/controllers/spree/transit/locations_controller.rb new file mode 100644 index 000000000..d6cdb76ca --- /dev/null +++ b/app/controllers/spree/transit/locations_controller.rb @@ -0,0 +1,32 @@ +module Spree + module Transit + class LocationsController < Spree::Transit::BaseController + before_action :load_location + + def load_location + @locations = Spree::State.where(country_id: Spree::Country.find_by(iso: 'KH')) + end + + def collection + load_location + return [] if current_vendor.blank? + return @collection if defined?(@collection) + + @search = @locations.ransack(params[:q]) + @collection = @search.result + end + + def location_after_save + transit_locations_url + end + + def model_class + Spree::State + end + + def object_name + 'state' + end + end + end +end diff --git a/app/models/spree_cm_commissioner/state_decorator.rb b/app/models/spree_cm_commissioner/state_decorator.rb index 4e034aad3..8427245ff 100644 --- a/app/models/spree_cm_commissioner/state_decorator.rb +++ b/app/models/spree_cm_commissioner/state_decorator.rb @@ -2,6 +2,7 @@ module SpreeCmCommissioner module StateDecorator def self.prepended(base) base.has_many :vendors + base.whitelisted_ransackable_attributes = %w[name abbr] def update_total_inventory update(total_inventory: vendors.pluck(:total_inventory).sum) diff --git a/app/views/spree/transit/locations/_form.html.erb b/app/views/spree/transit/locations/_form.html.erb new file mode 100644 index 000000000..36f050b66 --- /dev/null +++ b/app/views/spree/transit/locations/_form.html.erb @@ -0,0 +1,19 @@ +
+
+ <%= f.field_container :name do %> + <%= f.label :name, Spree.t(:name) %> + <%= f.text_field :name, class: 'form-control' %> + <%= f.error_message_on :name %> + <% end %> + <%= f.field_container :abbr do %> + <%= f.label :abbreviation, Spree.t(:abbreviation) %> + <%= f.text_field :abbr, class: 'form-control', required: true %> + <%= f.error_message_on :abbr %> + <% end %> + <%= f.field_container :country_id do %> + <%= f.label :country_id %> + <%= f.collection_select(:country_id, Spree::Country.all, :id, :name, {prompt: "Select a country"}, { class: 'form-control select2'} )%> + <%= f.error_message_on :country_id %> + <% end %> +
+
diff --git a/app/views/spree/transit/locations/edit.html.erb b/app/views/spree/transit/locations/edit.html.erb new file mode 100644 index 000000000..fef45e0ce --- /dev/null +++ b/app/views/spree/transit/locations/edit.html.erb @@ -0,0 +1,15 @@ +<% content_for :page_title do %> + <%= page_header_back_button spree.transit_locations_path %> + <%= I18n.t('spree.transit.locations') %> +<% end %> +
+ <%= render partial: 'spree/admin/shared/error_messages', locals: { target: @object } %> +
+
+ <%= form_with model: @object, url: { action: 'update' } do |f| %> + <%= render partial: 'form', locals: { f: f } %> +
+ <%= render partial: 'spree/admin/shared/edit_resource_links', locals: { collection_url: spree.transit_locations_path } %> +
+ <% end %> +
diff --git a/app/views/spree/transit/locations/index.html.erb b/app/views/spree/transit/locations/index.html.erb new file mode 100644 index 000000000..33a879a6a --- /dev/null +++ b/app/views/spree/transit/locations/index.html.erb @@ -0,0 +1,71 @@ +<% content_for :page_title do %> + <%= I18n.t('spree.transit.locations') %> +<% end %> + +<% content_for :page_actions do %> + <%= button_link_to Spree.t(:add_location), new_transit_location_path, class: "btn-success", icon: 'add.svg', id: 'admin_new_customer_link' %> +<% end %> + +<% content_for :table_filter do %> +
+ <%= search_form_for [@search], url: spree.transit_locations_path do |f| %> +
+
+
+ <%= f.label :name, I18n.t('spree.name') %> + <%= f.text_field :name_cont, class: "form-control js-quick-search-target js-filterable" %> +
+
+
+ <%= f.label :abbreviation, Spree.t(:abbreviation) %> +
+ <%= f.text_field :abbr_cont, class: "form-control js-filterable" %> +
+
+
+
+ <%= button Spree.t(:search), 'search.svg' %> +
+ <% end %> +
+<% end %> +<% if @locations.any? %> +
+ + + + + + + + + + + <% @locations.each do |location| %> + + + + + + + <% end %> + +
+ <%= sort_link @search, :name, Spree.t(:name), {}, {title:"id_title"} %> + + <%= sort_link @search, :abbr, Spree.t(:abbreviation), {}, {title: 'fullname_title'} %> + + <%= Spree.t(:Country) %> +
+ <%= location.name %> + + <%= location.abbr %> + + <%= location.country.name %> + + + <%= link_to_edit location, url: edit_transit_location_path(location), no_text: true, class: 'edit' if can?(:edit, location) %> + +
+
+<% end %> diff --git a/app/views/spree/transit/locations/new.html.erb b/app/views/spree/transit/locations/new.html.erb new file mode 100644 index 000000000..86a5ba1db --- /dev/null +++ b/app/views/spree/transit/locations/new.html.erb @@ -0,0 +1,17 @@ +<% content_for :page_title do %> + <%= page_header_back_button spree.transit_locations_path %> + <%= I18n.t('spree.transit.locations') %> +<% end %> + +
+ <%= render partial: 'spree/admin/shared/error_messages', locals: { target: @object } %> +
+ +
+ <%= form_with model: @object, url: { action: 'create' } do |f| %> + <%= render partial: 'form', locals: { f: f } %> +
+ <%= render partial: 'spree/admin/shared/new_resource_links', locals: { collection_url: spree.transit_locations_path } %> +
+ <% end %> +
diff --git a/app/views/spree/transit/shared/_header.html.erb b/app/views/spree/transit/shared/_header.html.erb new file mode 100644 index 000000000..1fc819be5 --- /dev/null +++ b/app/views/spree/transit/shared/_header.html.erb @@ -0,0 +1,20 @@ +
+ +
diff --git a/app/views/spree/transit/shared/_main_menu.html.erb b/app/views/spree/transit/shared/_main_menu.html.erb index 970cb76d7..441e901c9 100644 --- a/app/views/spree/transit/shared/_main_menu.html.erb +++ b/app/views/spree/transit/shared/_main_menu.html.erb @@ -13,4 +13,7 @@ + diff --git a/app/views/spree/transit/shared/_vendor_switcher.html.erb b/app/views/spree/transit/shared/_vendor_switcher.html.erb new file mode 100644 index 000000000..caba0700f --- /dev/null +++ b/app/views/spree/transit/shared/_vendor_switcher.html.erb @@ -0,0 +1,41 @@ +<% if can?(:admin, current_store) %> + + <% if current_vendor.logo %> + <%= image_tag main_app.url_for(current_vendor.logo.url(:small)), :class => 'w-25 p-2' %> + <% else %> + <%= svg_icon name: "building.svg", width: '25', height: '25' %> + <% end %> + + + + <% if vendors.size <= 1 %> + + <% end %> + + +<% end %> + + \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index e1b721f24..41384c703 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -139,6 +139,7 @@ en: new_customer: "New Customer" listing_price: "Listing Price" length: "Length" + name: "Name" promotion_rule_types: fixed_date: name: "Fixed Date" @@ -193,6 +194,7 @@ en: auto_apply_info: "Path & code will be removed" transit: reservations: "Reservations" + locations: "Locations" activerecord: attributes: diff --git a/config/routes.rb b/config/routes.rb index ac4c3a835..859d1d9d4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -147,6 +147,7 @@ namespace :transit do resources :reservations + resources :locations root to: redirect('/transit/reservations') end From 1a85e43443f869feb1a253d7a22160a7e9e6a23c Mon Sep 17 00:00:00 2001 From: tk <87005466+tykeaboyloy@users.noreply.github.com> Date: Mon, 11 Sep 2023 08:51:28 +0700 Subject: [PATCH 03/47] close #555 add default country to vendor (#556) --- .../spree/transit/base_controller.rb | 1 + .../spree/transit/locations_controller.rb | 10 ++- .../spree/transit/vendors_controller.rb | 83 +++++++++++++++++++ .../transit/navigation_helper_decorator.rb | 14 ++++ .../spree/transit/shared/_main_menu.html.erb | 3 + .../transit/shared/_vendor_switcher.html.erb | 41 +++++---- .../transit/shared/sub_menu/_setting.html.erb | 3 + .../spree/transit/vendors/_form.html.erb | 34 ++++++++ app/views/spree/transit/vendors/edit.html.erb | 9 ++ config/locales/en.yml | 4 + config/routes.rb | 1 + ...926_add_default_country_to_spree_vendor.rb | 5 ++ 12 files changed, 186 insertions(+), 22 deletions(-) create mode 100644 app/controllers/spree/transit/vendors_controller.rb create mode 100644 app/helpers/spree/transit/navigation_helper_decorator.rb create mode 100644 app/views/spree/transit/shared/sub_menu/_setting.html.erb create mode 100644 app/views/spree/transit/vendors/_form.html.erb create mode 100644 app/views/spree/transit/vendors/edit.html.erb create mode 100644 db/migrate/20230906090926_add_default_country_to_spree_vendor.rb diff --git a/app/controllers/spree/transit/base_controller.rb b/app/controllers/spree/transit/base_controller.rb index 2fe74cc01..e228b1d65 100644 --- a/app/controllers/spree/transit/base_controller.rb +++ b/app/controllers/spree/transit/base_controller.rb @@ -2,6 +2,7 @@ module Spree module Transit class BaseController < Spree::Admin::ResourceController helper_method :current_vendor, :vendors + before_action :current_vendor layout 'spree/layouts/transit' def vendors diff --git a/app/controllers/spree/transit/locations_controller.rb b/app/controllers/spree/transit/locations_controller.rb index d6cdb76ca..2e5b03ff0 100644 --- a/app/controllers/spree/transit/locations_controller.rb +++ b/app/controllers/spree/transit/locations_controller.rb @@ -4,7 +4,15 @@ class LocationsController < Spree::Transit::BaseController before_action :load_location def load_location - @locations = Spree::State.where(country_id: Spree::Country.find_by(iso: 'KH')) + @locations = Spree::State.all + end + + def load_country + @countries = Spree::Country.where(id: current_vendor.default_country) + end + + def new + @object.country_id = Spree::Country.find_by(id: current_vendor.default_country)&.id end def collection diff --git a/app/controllers/spree/transit/vendors_controller.rb b/app/controllers/spree/transit/vendors_controller.rb new file mode 100644 index 000000000..792ea1cb3 --- /dev/null +++ b/app/controllers/spree/transit/vendors_controller.rb @@ -0,0 +1,83 @@ +module Spree + module Transit + class VendorsController < Spree::Transit::BaseController + before_action :build_logo, only: %i[create update] + before_action :build_payment_qrcode, only: %i[create update] + + def create + @vendor.build_image(attachment: permitted_resource_params.delete(:image)) if permitted_resource_params[:image] + super + end + + def update + @vendor.create_image(attachment: permitted_resource_params.delete(:image)) if permitted_resource_params[:image] + format_translations if defined? SpreeGlobalize + super + end + + def update_positions + params[:positions].each do |id, position| + vendor = Spree::Vendor.find(id) + vendor.set_list_position(position) + end + + respond_to do |format| + format.js { render plain: 'Ok' } + end + end + + private + + def find_resource + Vendor.with_deleted.friendly.find(params[:id]) + end + + def collection + params[:q] = {} if params[:q].blank? + vendors = super.order(priority: :asc) + @search = vendors.ransack(params[:q]) + + @collection = @search.result + .includes(vendor_includes) + .page(params[:page]) + .per(params[:per_page]) + end + + def vendor_includes + { + image: [], + products: [] + } + end + + def format_translations + return if params[:vendor][:translations_attributes].blank? + + params[:vendor][:translations_attributes].each do |_, data| + translation = @vendor.translations.find_or_create_by(locale: data[:locale]) + translation.name = data[:name] + translation.about_us = data[:about_us] + translation.contact_us = data[:contact_us] + translation.slug = data[:slug] + translation.save! + end + end + + def build_logo + return unless permitted_resource_params[:logo] + + @vendor.build_logo(attachment: permitted_resource_params.delete(:logo)) + end + + def build_payment_qrcode + return unless permitted_resource_params[:payment_qrcode] + + @vendor.build_payment_qrcode(attachment: permitted_resource_params.delete(:payment_qrcode)) + end + + def location_after_save + edit_transit_vendor_url(@current_vendor.id) + end + end + end +end diff --git a/app/helpers/spree/transit/navigation_helper_decorator.rb b/app/helpers/spree/transit/navigation_helper_decorator.rb new file mode 100644 index 000000000..bf725867f --- /dev/null +++ b/app/helpers/spree/transit/navigation_helper_decorator.rb @@ -0,0 +1,14 @@ +module Spree + module Transit + module NavigationHelperDecorator + def transit_main_menu_tree(text, icon: nil, sub_menu: nil, url: '#') + content_tag :li, class: 'sidebar-menu-item d-block w-100 text-muted' do + main_menu_item(text, url: url, icon: icon) + + render(partial: "spree/transit/shared/sub_menu/#{sub_menu}") + end + end + end + end +end + +Spree::Admin::NavigationHelper.prepend(Spree::Transit::NavigationHelperDecorator) diff --git a/app/views/spree/transit/shared/_main_menu.html.erb b/app/views/spree/transit/shared/_main_menu.html.erb index 441e901c9..af1249bc4 100644 --- a/app/views/spree/transit/shared/_main_menu.html.erb +++ b/app/views/spree/transit/shared/_main_menu.html.erb @@ -16,4 +16,7 @@ + diff --git a/app/views/spree/transit/shared/_vendor_switcher.html.erb b/app/views/spree/transit/shared/_vendor_switcher.html.erb index caba0700f..cf17c761e 100644 --- a/app/views/spree/transit/shared/_vendor_switcher.html.erb +++ b/app/views/spree/transit/shared/_vendor_switcher.html.erb @@ -1,10 +1,10 @@ <% if can?(:admin, current_store) %> <% if current_vendor.logo %> - <%= image_tag main_app.url_for(current_vendor.logo.url(:small)), :class => 'w-25 p-2' %> - <% else %> + qrcode + <% else %> <%= svg_icon name: "building.svg", width: '25', height: '25' %> - <% end %> + <% end %> - <% if vendors.size <= 1 %> - + <% end %> - <% end %> - \ No newline at end of file + function switch_vendor(slug) { + $.ajax({ + url: '<%= billing_switch_vendor_path %>', + type: 'PUT', + data: { slug: slug }, + success: function(response) { + location.reload(); + }, + error: function(xhr, status, error) { + show_flash("error", error) + } + }); + } + diff --git a/app/views/spree/transit/shared/sub_menu/_setting.html.erb b/app/views/spree/transit/shared/sub_menu/_setting.html.erb new file mode 100644 index 000000000..34271c594 --- /dev/null +++ b/app/views/spree/transit/shared/sub_menu/_setting.html.erb @@ -0,0 +1,3 @@ + diff --git a/app/views/spree/transit/vendors/_form.html.erb b/app/views/spree/transit/vendors/_form.html.erb new file mode 100644 index 000000000..895b63bec --- /dev/null +++ b/app/views/spree/transit/vendors/_form.html.erb @@ -0,0 +1,34 @@ +<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @vendor } %> +
+ <%= f.field_container :name do %> + <%= f.label Spree.t('name') %> + <%= f.text_field :name, class: 'form-control' %> + <% end %> + <%= f.field_container :code do %> + <%= f.label Spree.t('code') %> + <%= f.text_field :code, class: 'form-control' %> + <% end %> + <% if Spree.version.to_f >= 3.6 %> +
+ <%= render 'shared/asset_field', + field: :logo, + label: Spree.t("logo"), + asset: @vendor.logo, + classes: ['col m-2'], + form:f + %> + <%= render 'shared/asset_field', + field: :image, + label: Spree.t('image'), + asset: @vendor.image, + classes: ['col m-2'], + form:f + %> +
+ <% end %> + <%= f.field_container :default_country do %> + <%= f.label :default_country %> + <%= f.collection_select(:default_country, Spree::Country.all, :id, :name, {prompt: "Select a country"}, { class: 'form-control select2'} )%> + <%= f.error_message_on :default_country %> + <% end %> +
diff --git a/app/views/spree/transit/vendors/edit.html.erb b/app/views/spree/transit/vendors/edit.html.erb new file mode 100644 index 000000000..96a2786e3 --- /dev/null +++ b/app/views/spree/transit/vendors/edit.html.erb @@ -0,0 +1,9 @@ +<% content_for :page_title do %> + <%= @vendor.name %> +<% end %> +<%= form_for [:transit, @vendor] do |f| %> +
+ <%= render partial: 'form', locals: { f: f } %> + <%= render partial: 'spree/admin/shared/edit_resource_links',locals: { collection_url: spree.edit_transit_vendor_url(@current_vendor.id) } %> +
+<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 41384c703..3bda05509 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -114,6 +114,8 @@ en: invalid_password: "Invalid Password" spree: + logo: "Logo" + image: "Image" en: "English" date_of_birth: "Date of Birth" kind: "Kind" @@ -195,6 +197,8 @@ en: transit: reservations: "Reservations" locations: "Locations" + settings: "Setting" + vendor: "Vendor" activerecord: attributes: diff --git a/config/routes.rb b/config/routes.rb index 859d1d9d4..6e50d82ab 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -148,6 +148,7 @@ namespace :transit do resources :reservations resources :locations + resources :vendors root to: redirect('/transit/reservations') end diff --git a/db/migrate/20230906090926_add_default_country_to_spree_vendor.rb b/db/migrate/20230906090926_add_default_country_to_spree_vendor.rb new file mode 100644 index 000000000..3e16b9e54 --- /dev/null +++ b/db/migrate/20230906090926_add_default_country_to_spree_vendor.rb @@ -0,0 +1,5 @@ +class AddDefaultCountryToSpreeVendor < ActiveRecord::Migration[7.0] + def change + add_column :spree_vendors, :default_country, :int, if_not_exists: true + end +end From d61562d637e8bffad12c893f8e56e1b63eaa8680 Mon Sep 17 00:00:00 2001 From: "L.Regene" <125754132+Regene27@users.noreply.github.com> Date: Tue, 12 Sep 2023 14:55:32 +0700 Subject: [PATCH 04/47] Close #547 Transit Branch Setup (#557) --- .../spree/transit/base_controller.rb | 8 ++- .../spree/transit/branches_controller.rb | 35 +++++++++++ app/models/spree_cm_commissioner/branch.rb | 14 +++++ app/models/spree_cm_commissioner/place.rb | 18 +++++- .../spree_cm_commissioner/vendor_decorator.rb | 2 + .../spree/transit/branches/_form.html.erb | 43 ++++++++++++++ .../spree/transit/branches/edit.html.erb | 15 +++++ .../spree/transit/branches/index.html.erb | 58 +++++++++++++++++++ app/views/spree/transit/branches/new.html.erb | 19 ++++++ .../spree/transit/shared/_main_menu.html.erb | 2 + config/locales/en.yml | 11 ++++ config/routes.rb | 1 + ...add_type_to_spree_cm_commissioner_place.rb | 5 ++ ...state_id_to_spree_cm_commissioner_place.rb | 5 ++ ...0230911080543_add_vendor_id_to_cm_place.rb | 5 ++ vendor/assets/javascripts/map/map-picker.js | 12 ++-- 16 files changed, 243 insertions(+), 10 deletions(-) create mode 100644 app/controllers/spree/transit/branches_controller.rb create mode 100644 app/models/spree_cm_commissioner/branch.rb create mode 100644 app/views/spree/transit/branches/_form.html.erb create mode 100644 app/views/spree/transit/branches/edit.html.erb create mode 100644 app/views/spree/transit/branches/index.html.erb create mode 100644 app/views/spree/transit/branches/new.html.erb create mode 100644 db/migrate/20230906072540_add_type_to_spree_cm_commissioner_place.rb create mode 100644 db/migrate/20230907094354_add_state_id_to_spree_cm_commissioner_place.rb create mode 100644 db/migrate/20230911080543_add_vendor_id_to_cm_place.rb diff --git a/app/controllers/spree/transit/base_controller.rb b/app/controllers/spree/transit/base_controller.rb index e228b1d65..d6ad3a4dd 100644 --- a/app/controllers/spree/transit/base_controller.rb +++ b/app/controllers/spree/transit/base_controller.rb @@ -2,13 +2,19 @@ module Spree module Transit class BaseController < Spree::Admin::ResourceController helper_method :current_vendor, :vendors - before_action :current_vendor + before_action :required_vendor_user! layout 'spree/layouts/transit' def vendors @vendors ||= spree_current_user.vendors.to_a end + def required_vendor_user! + return unless vendors.empty? + + raise SpreeCmCommissioner::UnauthorizedVendorError + end + def current_vendor @current_vendor ||= vendors.find { |v| v[:slug] == session[:transit_current_vendor_slug] } || vendors.first session[:transit_current_vendor_slug] ||= @current_vendor&.slug diff --git a/app/controllers/spree/transit/branches_controller.rb b/app/controllers/spree/transit/branches_controller.rb new file mode 100644 index 000000000..95950aff7 --- /dev/null +++ b/app/controllers/spree/transit/branches_controller.rb @@ -0,0 +1,35 @@ +module Spree + module Transit + class BranchesController < Spree::Transit::BaseController + + def new + @branch = SpreeCmCommissioner::Branch.new + super + end + + def collection + return @collection if defined?(@collection) + current_vendor.branches + + @search = current_vendor.branches.ransack(params[:q]) + @collection = @search.result + end + + def location_after_save + transit_branches_url + end + + def model_class + SpreeCmCommissioner::Branch + end + + def object_name + 'spree_cm_commissioner_branch' + end + + def branch_params + params.require(:branch).permit(:name, :lat, :lon) + end + end + end +end diff --git a/app/models/spree_cm_commissioner/branch.rb b/app/models/spree_cm_commissioner/branch.rb new file mode 100644 index 000000000..81bf08337 --- /dev/null +++ b/app/models/spree_cm_commissioner/branch.rb @@ -0,0 +1,14 @@ +require_dependency 'spree_cm_commissioner' + +module SpreeCmCommissioner + class Branch < SpreeCmCommissioner::Place + belongs_to :state, class_name: 'Spree::State', optional: true + belongs_to :vendor, class_name: 'Spree::Vendor' + + validates :vendor_id, presence: true + + def validate_reference? + false + end + end +end diff --git a/app/models/spree_cm_commissioner/place.rb b/app/models/spree_cm_commissioner/place.rb index af7531b08..12c42f320 100644 --- a/app/models/spree_cm_commissioner/place.rb +++ b/app/models/spree_cm_commissioner/place.rb @@ -2,11 +2,23 @@ module SpreeCmCommissioner class Place < ApplicationRecord - validates :reference, presence: true - validates :lat, presence: true - validates :lon, presence: true + validates :reference, presence: true, if: :validate_reference? + validates :lat, presence: true, if: :validate_lat? + validates :lon, presence: true, if: :validate_lon? has_many :nearby_places, class_name: 'SpreeCmCommissioner::VendorPlace', dependent: :destroy has_many :vendors, through: :nearby_places, source: :vendor, class_name: 'Spree::Vendor' + + def validate_reference? + true + end + + def validate_lat? + true + end + + def validate_lon? + true + end end end diff --git a/app/models/spree_cm_commissioner/vendor_decorator.rb b/app/models/spree_cm_commissioner/vendor_decorator.rb index 4ab97e978..effe23af7 100644 --- a/app/models/spree_cm_commissioner/vendor_decorator.rb +++ b/app/models/spree_cm_commissioner/vendor_decorator.rb @@ -27,6 +27,8 @@ def self.prepended(base) base.has_many :vendor_kind_option_values, through: :option_value_vendors, source: :option_value + base.has_many :branches, class_name: 'SpreeCmCommissioner::Branch' + base.has_many :places, through: :nearby_places, source: :place, class_name: 'SpreeCmCommissioner::Place' diff --git a/app/views/spree/transit/branches/_form.html.erb b/app/views/spree/transit/branches/_form.html.erb new file mode 100644 index 000000000..ecc2ba301 --- /dev/null +++ b/app/views/spree/transit/branches/_form.html.erb @@ -0,0 +1,43 @@ +
+
+ <%= f.field_container :name do %> + <%= f.label :name, Spree.t(:name) %> + <%= f.text_field :name, class: 'form-control' %> + <%= f.error_message_on :name %> + <% end %> + + <%= f.hidden_field :vendor_id, value: current_vendor.id %> + + <%= f.field_container :lat do %> + <%= f.label :lat, Spree.t("transit.branch.lat.label") %> + <%= f.text_field :lat, class: 'form-control', placeholder: Spree.t("transit.branch.lat.hint") %> + <%= f.error_message_on :lat %> + <% end %> + + <%= f.field_container :lon do %> + <%= f.label :lon, Spree.t("transit.branch.lon.label") %> + <%= f.text_field :lon, class: 'form-control', placeholder: Spree.t("transit.branch.lon.hint") %> + <%= f.error_message_on :lon %> + <% end %> + + <%= f.field_container :formatted_phone_number do %> + <%= f.label :formatted_phone_number, Spree.t("phone_number") %> + <%= f.text_field :formatted_phone_number, class: 'form-control', placeholder: Spree.t("phone_number") %> + <%= f.error_message_on :formatted_phone_number %> + <% end %> + + <%= f.field_container :formatted_address do %> + <%= f.label :formatted_address, Spree.t("transit.branch.address") %> + <%= f.text_field :formatted_address, class: 'form-control', placeholder: ("Google Map Address") %> + <%= f.error_message_on :formatted_address %> + <% end %> + + <%= f.field_container :state_id do %> + <%= f.label :state_id, Spree.t("transit.location") %> + <%= f.collection_select(:state_id, Spree::State.where(country_id: Spree::Country.find_by(iso: 'KH')), :id, :name, { prompt: "Select a location" }, { class: 'form-control select2' }) %> + <%= f.error_message_on :state_id %> + <% end %> + + <%= render 'shared/map', lat: f.object.lat, lon: f.object.lon %> +
+
diff --git a/app/views/spree/transit/branches/edit.html.erb b/app/views/spree/transit/branches/edit.html.erb new file mode 100644 index 000000000..9ebccc6af --- /dev/null +++ b/app/views/spree/transit/branches/edit.html.erb @@ -0,0 +1,15 @@ +<% content_for :page_title do %> + <%= page_header_back_button spree.transit_branches_path %> + <%= I18n.t('spree.transit.branch.branches') %> +<% end %> +
+ <%= render partial: 'spree/admin/shared/error_messages', locals: { target: @object } %> +
+
+ <%= form_with model: @object, url: { action: 'update' } do |f| %> + <%= render partial: 'form', locals: { f: f } %> +
+ <%= render partial: 'spree/admin/shared/edit_resource_links', locals: { collection_url: spree.transit_branches_path } %> +
+ <% end %> +
diff --git a/app/views/spree/transit/branches/index.html.erb b/app/views/spree/transit/branches/index.html.erb new file mode 100644 index 000000000..ef5ee40cf --- /dev/null +++ b/app/views/spree/transit/branches/index.html.erb @@ -0,0 +1,58 @@ +<% content_for :page_title do %> + <%= I18n.t('spree.transit.branch.branches') %> +<% end %> + +<% content_for :page_actions do %> + <%= button_link_to Spree.t(:add_branch), new_transit_branch_path, class: "btn-success", icon: 'add.svg', id: 'admin_new_customer_link' %> +<% end %> + +<% content_for :table_filter do %> +
+ <%= search_form_for [@search], url: spree.transit_branches_path do |f| %> +
+
+
+ <%= f.label :name, I18n.t('spree.name') %> + <%= f.text_field :name_cont, class: "form-control js-quick-search-target js-filterable" %> +
+
+
+
+ <%= button Spree.t(:search), 'search.svg' %> +
+ <% end %> +
+<% end %> +<% if @branches.any? %> +
+ + + + + + + + + + + + <% @branches.each do |branch| %> + + + + + + + + <% end %> + +
+ <%= sort_link @search, :name, Spree.t(:name), {}, {title:"id_title"} %> + <%= Spree.t(:phone_number) %><%= Spree.t(:address) %><%= sort_link @search, :state_id, Spree.t(:location) %>
<%= branch.name %><%= branch.formatted_phone_number %><%= branch.formatted_address %><%= Spree::State.find_by(id: branch.state_id)&.abbr %> + + <%= link_to_edit branch, url: edit_transit_branch_path(branch), no_text: true, class: 'edit' if can?(:edit, branch) %> + <%= link_to_delete branch, url: transit_branch_path(branch), no_text: true if can?(:delete, branch) %> + +
+
+<% end %> diff --git a/app/views/spree/transit/branches/new.html.erb b/app/views/spree/transit/branches/new.html.erb new file mode 100644 index 000000000..40d9df1ff --- /dev/null +++ b/app/views/spree/transit/branches/new.html.erb @@ -0,0 +1,19 @@ +<% content_for :page_title do %> + <%= page_header_back_button spree.transit_branches_path %> + <%= I18n.t('spree.transit.branch.branches') %> +<% end %> + +
+ <%= render partial: 'spree/admin/shared/error_messages', locals: { target: @object } %> +
+ +
+ <%= form_with model: @object, url: { action: 'create' } do |f| %> +
+ <%= render partial: 'form', locals: { f: f } %> +
+ <%= render partial: 'spree/admin/shared/new_resource_links', locals: { collection_url: spree.transit_branches_path } %> +
+
+ <% end %> +
diff --git a/app/views/spree/transit/shared/_main_menu.html.erb b/app/views/spree/transit/shared/_main_menu.html.erb index af1249bc4..f3c3d4cf4 100644 --- a/app/views/spree/transit/shared/_main_menu.html.erb +++ b/app/views/spree/transit/shared/_main_menu.html.erb @@ -18,5 +18,7 @@