-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bulk upload store credit button added
- Loading branch information
1 parent
5f68c82
commit a01fd7e
Showing
16 changed files
with
220 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
function CsvInputButton(input) { | ||
this.fileInput = input.fileInput; | ||
this.submitButton = input.submitButton; | ||
this.form = input.form; | ||
} | ||
CsvInputButton.prototype.init = function() { | ||
var _this = this; | ||
this.submitButton.on('click', function() { | ||
event.preventDefault(); | ||
_this.fileInput.click(); | ||
_this.submitForm(); | ||
}); | ||
}; | ||
CsvInputButton.prototype.submitForm = function() { | ||
var _this = this; | ||
this.fileInput.on('change', function() { | ||
_this.form.submit(); | ||
}); | ||
}; | ||
$(function() { | ||
var inputs = { | ||
fileInput: $('input#file'), | ||
submitButton: $('input[data-disable-with="Import Store Credit CSV"]'), | ||
form: $("form[data-hook='csv_form']") | ||
}, | ||
csvButtonsManager = new CsvInputButton(inputs); | ||
csvButtonsManager.init(); | ||
}); |
3 changes: 2 additions & 1 deletion
3
app/assets/javascripts/spree/backend/spree_bulk_store_credits.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
// Placeholder manifest file. | ||
// the installer will append this file to the app vendored assets here: vendor/assets/javascripts/spree/backend/all.js' | ||
// the installer will append this file to the app vendored assets here: vendor/assets/javascripts/spree/backend/all.js' | ||
//= require spree/backend/csv_input_button |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Spree::Admin::UsersController.class_eval do | ||
def sample_store_credit_csv | ||
send_file STORE_CREDIT_CSV_FILE[:sample_store_credit_file] | ||
end | ||
|
||
def import_store_credits | ||
begin | ||
create_store_credit_updater | ||
redirect_to admin_users_path, notice: Spree.t(:email_sent, filename: @store_credit_updater.data_file_file_name) | ||
rescue | ||
redirect_to admin_users_path, notice: "Invalid CSV file format." | ||
end | ||
end | ||
|
||
private | ||
|
||
def create_store_credit_updater | ||
@store_credit_updater = Spree::BulkStoreCreditUpdater.create(data_file: params[:file]) | ||
NotifyStoreCreditService.delay(run_at: 1.minutes.from_now).new(@store_credit_updater.id, try_spree_current_user.email) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Spree | ||
class StoreCreditUpdaterMailer < BaseMailer | ||
def update_admin(status_csv, admin_email, filename, total_records, successfull_records) | ||
@total_records = total_records | ||
@failed_records = total_records - successfull_records | ||
attachments['stock_items.csv'] = status_csv | ||
subject = "#{Spree::Store.current.name} import of #{ filename } has finished" | ||
mail(to: admin_email, from: from_address, subject: subject) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module Spree | ||
class BulkStoreCreditUpdater < Spree::Base | ||
has_attached_file :data_file | ||
validates_attachment :data_file, content_type: { content_type: ["text/csv", "text/plain"] } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Deface::Override.new( | ||
virtual_path: 'spree/admin/shared/_content_header', | ||
name: 'add_import_button_on_user_index_page', | ||
insert_bottom: "div[data-hook='toolbar']", | ||
partial: 'spree/admin/users/import_button' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
require 'csv' | ||
class NotifyStoreCreditService | ||
CSV_HEADERS = { 'Email': :Email, 'Store_Credit': :Store_Credit, 'Memo': :Memo, 'Category_ID': :Category_ID, 'Status': :Status } | ||
|
||
def initialize(store_credit_updater_id, admin_email) | ||
@store_credit_updater = Spree::BulkStoreCreditUpdater.find_by(id: store_credit_updater_id) | ||
update_store_credits | ||
@store_credit_updater.update_column(:job_executed, true) | ||
Spree::StoreCreditUpdaterMailer.update_admin(@csv_export, admin_email, @store_credit_updater.data_file_file_name, @total_records, @successfull_records).deliver_now | ||
@store_credit_updater.destroy if @store_credit_updater.job_executed? | ||
end | ||
|
||
private | ||
def update_store_credits | ||
@total_records = 0 | ||
@successfull_records = 0 | ||
@csv_export = CSV.generate do |csv| | ||
unless csv_empty? | ||
csv << CSV_HEADERS.keys | ||
CSV.foreach(@store_credit_updater.data_file.path, headers: true) do |row| | ||
@error = nil | ||
@total_records += 1 | ||
@row = row | ||
@user = find_user | ||
if @user | ||
update_stocks_with_csv_values | ||
else | ||
@error = Spree.t(:user_not_found) | ||
end | ||
csv << set_row | ||
end | ||
end | ||
end | ||
end | ||
|
||
def csv_empty? | ||
CSV.readlines(@store_credit_updater.data_file.path) == [[]] | ||
end | ||
|
||
def set_row | ||
row = [] | ||
CSV_HEADERS.each do |key, value| | ||
row << create_csv_row(key, value) | ||
end | ||
row | ||
end | ||
|
||
def create_csv_row(key, value) | ||
( key == :Status ) ? error_message : @row[value.to_s] | ||
end | ||
|
||
def error_message | ||
@error ? @error : 'Successfully Updated' | ||
end | ||
|
||
def update_stocks_with_csv_values | ||
admin = Spree::User.admin.first | ||
store_credit = Spree::StoreCredit.new(amount: @row['Store_Credit'].to_f, created_by_id: admin.id, currency: "USD", category_id: @row['Category_ID'], memo: @row['Memo']) | ||
@user.store_credits << store_credit | ||
@successfull_records += 1 | ||
end | ||
|
||
def find_user | ||
Spree::User.find_by_email(@row['Email']) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<% if request.path == admin_users_path %> | ||
<%= form_tag import_store_credits_admin_users_path, multipart: true, class: 'form-inline', data: { hook: "csv_form" } do %> | ||
<%= file_field_tag :file, class: 'file-browse' %> | ||
<%= submit_tag Spree.t(:import), class: 'btn-success btn' %> | ||
<%= link_to Spree.t(:example) , sample_store_credit_csv_admin_users_path, class: 'sample-download' %> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<!-- You can override this template to design your own header. --> | ||
<table class="row header"> | ||
<tr> | ||
<td class="center" align="center"> | ||
<center> | ||
|
||
<table class="container"> | ||
<tr> | ||
<td class="wrapper last"> | ||
|
||
<table class="twelve columns"> | ||
<tr> | ||
<td class="twelve sub-columns last"> | ||
<% if frontend_available? %> | ||
<%= link_to root_url, class: 'template-label' do %> | ||
<%= image_tag Spree::Config.logo, class: 'logo', alt: Spree::Store.current.name, title: Spree::Store.current.name %> | ||
<% end %> | ||
<% else %> | ||
<%= image_tag Spree::Config.logo, class: 'logo', alt: Spree::Store.current.name, title: Spree::Store.current.name %> | ||
<% end %> | ||
</td> | ||
<td class="expander"></td> | ||
</tr> | ||
</table> | ||
|
||
</td> | ||
</tr> | ||
</table> | ||
|
||
</center> | ||
</td> | ||
</tr> | ||
</table> |
8 changes: 8 additions & 0 deletions
8
app/views/spree/store_credit_updater_mailer/update_admin.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<div> | ||
<h5><%= Spree.t(:update_status) %></h5> | ||
<ul> | ||
<li>Number of records imported : <%= @total_records %> </li> | ||
<li>Number of records failed : <%= @failed_records %> </li> | ||
</ul> | ||
<strong>Attached is a csv file of failed records for your information.</strong> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
STORE_CREDIT_CSV_FILE = { | ||
sample_store_credit_file: SpreeBulkStoreCredits::Engine.root.join('lib', 'sample_csv', 'bulk_store_credits_example.csv') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
Spree::Core::Engine.add_routes do | ||
# Add your extension routes here | ||
namespace :admin, path: Spree.admin_path do | ||
resources :users do | ||
collection { post :import_store_credits } | ||
collection { get :sample_store_credit_csv } | ||
end | ||
end | ||
end |
8 changes: 8 additions & 0 deletions
8
db/migrate/20180806080802_create_spree_bulk_store_credit_updater.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class CreateSpreeBulkStoreCreditUpdater < ActiveRecord::Migration[4.2] | ||
def change | ||
create_table :spree_bulk_store_credit_updaters do |t| | ||
t.boolean :job_executed, default: false | ||
t.attachment :data_file | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Email,Store Credit,Memo,Category ID | ||
[email protected],10,Order benefit,1 |