Using MomentJS to handle dates on bulk order management

This commit is contained in:
Rob Harrington
2016-12-02 11:49:47 +11:00
parent 7644f08d5c
commit f6af5098b0
4 changed files with 34 additions and 84 deletions

View File

@@ -20,6 +20,7 @@
//= require admin/spree_paypal_express
//= require ../shared/ng-infinite-scroll.min.js
//= require ../shared/ng-tags-input.min.js
//= require moment
//= require angular-rails-templates
//= require_tree ../templates/admin
//= require ./admin_ofn

View File

@@ -3,8 +3,8 @@ angular.module("admin.lineItems").controller 'LineItemsCtrl', ($scope, $timeout,
$scope.RequestMonitor = RequestMonitor
$scope.filteredLineItems = []
$scope.confirmDelete = true
$scope.startDate = formatDate daysFromToday -7
$scope.endDate = formatDate daysFromToday 1
$scope.startDate = moment().startOf('day').subtract(7, 'days').format('YYYY-MM-DD')
$scope.endDate = moment().startOf('day').format('YYYY-MM-DD')
$scope.bulkActions = [ { name: t("admin.orders.bulk_management.actions_delete"), callback: 'deleteLineItems' } ]
$scope.selectedUnitsProduct = {}
$scope.selectedUnitsVariant = {}
@@ -22,15 +22,15 @@ angular.module("admin.lineItems").controller 'LineItemsCtrl', ($scope, $timeout,
$scope.refreshData = ->
unless !$scope.orderCycleFilter? || $scope.orderCycleFilter == 0
$scope.startDate = OrderCycles.byID[$scope.orderCycleFilter].first_order
$scope.endDate = OrderCycles.byID[$scope.orderCycleFilter].last_order
$scope.startDate = moment(OrderCycles.byID[$scope.orderCycleFilter].first_order).format('YYYY-MM-DD')
$scope.endDate = moment(OrderCycles.byID[$scope.orderCycleFilter].last_order).startOf('day').format('YYYY-MM-DD')
RequestMonitor.load $scope.orders = Orders.index("q[state_not_eq]": "canceled", "q[completed_at_not_null]": "true", "q[completed_at_gt]": "#{parseDate($scope.startDate)}", "q[completed_at_lt]": "#{parseDate($scope.endDate)}")
RequestMonitor.load $scope.lineItems = LineItems.index("q[order][state_not_eq]": "canceled", "q[order][completed_at_not_null]": "true", "q[order][completed_at_gt]": "#{parseDate($scope.startDate)}", "q[order][completed_at_lt]": "#{parseDate($scope.endDate)}")
RequestMonitor.load $scope.orders = Orders.index("q[state_not_eq]": "canceled", "q[completed_at_not_null]": "true", "q[completed_at_gteq]": "#{moment($scope.startDate).format()}", "q[completed_at_lt]": "#{moment($scope.endDate).add(1,'day').format()}")
RequestMonitor.load $scope.lineItems = LineItems.index("q[order][state_not_eq]": "canceled", "q[order][completed_at_not_null]": "true", "q[order][completed_at_gteq]": "#{moment($scope.startDate).format()}", "q[order][completed_at_lt]": "#{moment($scope.endDate).add(1,'day').format()}")
unless $scope.initialized
RequestMonitor.load $scope.distributors = Enterprises.index(action: "visible", ams_prefix: "basic", "q[sells_in][]": ["own", "any"])
RequestMonitor.load $scope.orderCycles = OrderCycles.index(ams_prefix: "basic", as: "distributor", "q[orders_close_at_gt]": "#{daysFromToday(-90)}")
RequestMonitor.load $scope.orderCycles = OrderCycles.index(ams_prefix: "basic", as: "distributor", "q[orders_close_at_gt]": "#{moment().subtract(90,'days').format()}")
RequestMonitor.load $scope.suppliers = Enterprises.index(action: "visible", ams_prefix: "basic", "q[is_primary_producer_eq]": "true")
RequestMonitor.load $q.all([$scope.orders.$promise, $scope.distributors.$promise, $scope.orderCycles.$promise]).then ->
@@ -142,31 +142,3 @@ angular.module("admin.lineItems").controller 'LineItemsCtrl', ($scope, $timeout,
if lineItem.quantity > 0
lineItem.final_weight_volume = LineItems.pristineByID[lineItem.id].final_weight_volume * lineItem.quantity / LineItems.pristineByID[lineItem.id].quantity
$scope.weightAdjustedPrice(lineItem)
daysFromToday = (days) ->
now = new Date
now.setHours(0)
now.setMinutes(0)
now.setSeconds(0)
now.setDate( now.getDate() + days )
now
formatDate = (date) ->
year = date.getFullYear()
month = twoDigitNumber date.getMonth() + 1
day = twoDigitNumber date.getDate()
return year + "-" + month + "-" + day
formatTime = (date) ->
hours = twoDigitNumber date.getHours()
mins = twoDigitNumber date.getMinutes()
secs = twoDigitNumber date.getSeconds()
return hours + ":" + mins + ":" + secs
parseDate = (dateString) ->
new Date(Date.parse(dateString))
twoDigitNumber = (number) ->
twoDigits = "" + number
twoDigits = ("0" + number) if number < 10
twoDigits

View File

@@ -420,12 +420,14 @@ feature %q{
end
context "using date restriction controls" do
let!(:o1) { create(:order_with_distributor, state: 'complete', completed_at: (Date.current - 8).strftime("%F %T") ) }
let!(:o2) { create(:order_with_distributor, state: 'complete', completed_at: Time.zone.now ) }
let!(:o3) { create(:order_with_distributor, state: 'complete', completed_at: (Date.current + 2).strftime("%F %T") ) }
let!(:o1) { create(:order_with_distributor, state: 'complete', completed_at: Time.zone.today - 7.days - 1.second) }
let!(:o2) { create(:order_with_distributor, state: 'complete', completed_at: Time.zone.today - 7.days) }
let!(:o3) { create(:order_with_distributor, state: 'complete', completed_at: Time.zone.now.end_of_day) }
let!(:o4) { create(:order_with_distributor, state: 'complete', completed_at: Time.zone.now.end_of_day + 1.second) }
let!(:li1) { create(:line_item, order: o1, :quantity => 1 ) }
let!(:li2) { create(:line_item, order: o2, :quantity => 2 ) }
let!(:li3) { create(:line_item, order: o3, :quantity => 3 ) }
let!(:li4) { create(:line_item, order: o4, :quantity => 4 ) }
before :each do
visit '/admin/orders/bulk_management'
@@ -433,29 +435,31 @@ feature %q{
it "displays date fields for filtering orders, with default values set" do
# use Date.current since Date.today is without timezone
today = Date.current
today = Time.zone.today
one_week_ago = today.prev_day(7).strftime("%F")
tonight = today.next_day.strftime("%F")
expect(page).to have_field "start_date_filter", with: one_week_ago
expect(page).to have_field "end_date_filter", with: tonight
expect(page).to have_field "end_date_filter", with: today.strftime("%F")
end
it "only loads line items whose orders meet the date restriction criteria" do
expect(page).to have_no_selector "tr#li_#{li1.id}"
expect(page).to have_selector "tr#li_#{li2.id}"
expect(page).to have_no_selector "tr#li_#{li3.id}"
expect(page).to have_selector "tr#li_#{li3.id}"
expect(page).to have_no_selector "tr#li_#{li4.id}"
end
it "displays only line items whose orders meet the date restriction criteria, when changed" do
fill_in "start_date_filter", :with => (Date.current - 9).strftime("%F")
expect(page).to have_selector "tr#li_#{li1.id}"
expect(page).to have_selector "tr#li_#{li2.id}"
expect(page).to have_no_selector "tr#li_#{li3.id}"
fill_in "end_date_filter", :with => (Date.current + 3).strftime("%F")
fill_in "start_date_filter", :with => (Time.zone.today - 8.days).strftime("%F")
expect(page).to have_selector "tr#li_#{li1.id}"
expect(page).to have_selector "tr#li_#{li2.id}"
expect(page).to have_selector "tr#li_#{li3.id}"
expect(page).to have_no_selector "tr#li_#{li4.id}"
fill_in "end_date_filter", :with => (Time.zone.today + 1.day).strftime("%F")
expect(page).to have_selector "tr#li_#{li1.id}"
expect(page).to have_selector "tr#li_#{li2.id}"
expect(page).to have_selector "tr#li_#{li3.id}"
expect(page).to have_selector "tr#li_#{li4.id}"
end
context "when the form is dirty" do

View File

@@ -23,9 +23,13 @@ describe "LineItemsCtrl", ->
LineItems = _LineItems_
OrderCycles = _OrderCycles_
VariantUnitManager = _VariantUnitManager_
spyOn(window, "daysFromToday").and.returnValue "SomeDate"
spyOn(window, "formatDate").and.returnValue "SomeDate"
spyOn(window, "parseDate").and.returnValue "SomeDate"
momentMock = jasmine.createSpyObj('moment', ['format', 'startOf', 'endOf', 'subtract', 'add'])
spyOn(window,"moment").and.returnValue momentMock
momentMock.startOf.and.returnValue momentMock
momentMock.endOf.and.returnValue momentMock
momentMock.subtract.and.returnValue momentMock
momentMock.add.and.returnValue momentMock
momentMock.format.and.returnValue "SomeDate"
supplier = { id: 1, name: "Supplier" }
distributor = { id: 5, name: "Distributor" }
@@ -33,8 +37,8 @@ describe "LineItemsCtrl", ->
order = { id: 9, order_cycle: { id: 4 }, distributor: { id: 5 }, number: "R123456" }
lineItem = { id: 7, quantity: 3, order: { id: 9 }, supplier: { id: 1 } }
httpBackend.expectGET("/admin/orders.json?q%5Bcompleted_at_gt%5D=SomeDate&q%5Bcompleted_at_lt%5D=SomeDate&q%5Bcompleted_at_not_null%5D=true&q%5Bstate_not_eq%5D=canceled").respond [order]
httpBackend.expectGET("/admin/bulk_line_items.json?q%5Border%5D%5Bcompleted_at_gt%5D=SomeDate&q%5Border%5D%5Bcompleted_at_lt%5D=SomeDate&q%5Border%5D%5Bcompleted_at_not_null%5D=true&q%5Border%5D%5Bstate_not_eq%5D=canceled").respond [lineItem]
httpBackend.expectGET("/admin/orders.json?q%5Bcompleted_at_gteq%5D=SomeDate&q%5Bcompleted_at_lt%5D=SomeDate&q%5Bcompleted_at_not_null%5D=true&q%5Bstate_not_eq%5D=canceled").respond [order]
httpBackend.expectGET("/admin/bulk_line_items.json?q%5Border%5D%5Bcompleted_at_gteq%5D=SomeDate&q%5Border%5D%5Bcompleted_at_lt%5D=SomeDate&q%5Border%5D%5Bcompleted_at_not_null%5D=true&q%5Border%5D%5Bstate_not_eq%5D=canceled").respond [lineItem]
httpBackend.expectGET("/admin/enterprises/visible.json?ams_prefix=basic&q%5Bsells_in%5D%5B%5D=own&q%5Bsells_in%5D%5B%5D=any").respond [distributor]
httpBackend.expectGET("/admin/order_cycles.json?ams_prefix=basic&as=distributor&q%5Borders_close_at_gt%5D=SomeDate").respond [orderCycle]
httpBackend.expectGET("/admin/enterprises/visible.json?ams_prefix=basic&q%5Bis_primary_producer_eq%5D=true").respond [supplier]
@@ -332,34 +336,3 @@ describe "LineItemsCtrl", ->
]
scope.updateOnQuantity(scope.filteredLineItems[0])
expect(scope.filteredLineItems[0].final_weight_volume).toEqual 1000
describe "Auxiliary functions", ->
describe "getting a zero filled two digit number", ->
it "returns the number as a string if its value is greater than or equal to 10", ->
expect(twoDigitNumber(10)).toEqual "10"
expect(twoDigitNumber(15)).toEqual "15"
expect(twoDigitNumber(99)).toEqual "99"
it "returns the number formatted as a zero filled string if its value is less than 10", ->
expect(twoDigitNumber(0)).toEqual "00"
expect(twoDigitNumber(1)).toEqual "01"
expect(twoDigitNumber(9)).toEqual "09"
describe "formatting dates and times", ->
date = null
beforeEach ->
date = new Date
date.setYear(2010)
date.setMonth(4) # Zero indexed, so 4 is May
date.setDate(15)
date.setHours(5)
date.setMinutes(10)
date.setSeconds(30)
it "returns a date formatted as yyyy-mm-dd", ->
expect(formatDate(date)).toEqual "2010-05-15"
it "returns a time formatted as hh-MM:ss", ->
expect(formatTime(date)).toEqual "05:10:30"