diff --git a/app/assets/javascripts/application.js.coffee b/app/assets/javascripts/application.js.coffee
index d57ce35b3..7835f2cd1 100644
--- a/app/assets/javascripts/application.js.coffee
+++ b/app/assets/javascripts/application.js.coffee
@@ -39,6 +39,25 @@ diffFromSequence = (str, separator = "of") ->
return undefined
return
+# Added for subtracting years from date
+# Handles also leap years
+yearsFromDate = (input, years, format = false) ->
+ months = (years * 12) - 1
+
+ date = new Date(
+ input.getFullYear(),
+ input.getMonth() + months,
+ Math.min(
+ input.getDate(),
+ new Date(input.getFullYear(), input.getMonth() + months + 1, 0).getDate()
+ )
+ )
+
+ if format
+ date.toLocaleDateString('en-GB')
+ else
+ date
+
# Conditional latest year
# If from 6th of September to December -> then previous year
# If from January to 6th of September -> then current year
@@ -290,29 +309,13 @@ jQuery ->
[fy_latest_day, fy_latest_month, fy_latest_year] = getLatestFinancialYearParts()
- start_input = $(".js-started-trading")
- start_day = start_input.find("input.js-date-input-day").val()
- start_month = start_input.find("input.js-date-input-month").val()
- start_year = start_input.find("input.js-date-input-year").val()
-
if !fy_latest_day || !fy_latest_month || !fy_latest_year
$(".js-year-end").addClass("show-default")
else
$(".js-year-end").each ->
year = parseInt(fy_latest_year) + parseInt($(this).attr("data-year").substr(0, 1)) - parseInt($(this).attr("data-year").substr(-1, 1))
-
$(this).addClass("show-both")
-
- if !start_year || !start_month || !start_month
- $(this).find(".js-year-text").text("Year ended #{fy_latest_day}/#{fy_latest_month}/#{year}")
- else
- d_min = new Date(start_year, parseInt(start_month) - 1, parseInt(start_day))
- d_actual = new Date(year, parseInt(fy_latest_month) - 1, parseInt(fy_latest_day))
-
- if (d_actual >= d_min)
- $(this).find(".js-year-text").text("Year ended #{fy_latest_day}/#{fy_latest_month}/#{year}")
- else
- $(this).find(".js-year-text").html("
")
+ $(this).find(".js-year-text").text("Year ended #{fy_latest_day}/#{fy_latest_month}/#{year}")
else
# Year has changed, use what they've inputted
$(".js-financial-conditional > .by-years-wrapper").each ->
@@ -328,6 +331,7 @@ jQuery ->
if !all_years_value
$(this).find(".js-year-end").each ->
diff = diffFromSequence($(this).attr("data-year"))
+ fy_count = $(".js-financial-year-changed-dates .by-years-wrapper.show-question .js-year-end").length
fy_input = $(".js-financial-year-changed-dates .by-years-wrapper.show-question .js-year-end[data-year-diff='#{diff}']").closest(".js-fy-entries").find(".govuk-date-input")
fy_day = fy_input.find(".js-fy-day").val()
fy_month = fy_input.find(".js-fy-month").val()
@@ -335,8 +339,27 @@ jQuery ->
$(this).addClass("show-both")
- if !fy_day || !fy_month || !fy_year
+ if fy_input.length > 0 && (!fy_day || !fy_month || !fy_year)
$(this).find(".js-year-text").html("
")
+ else if fy_input.length == 0
+ # how many years to deduct from first FY if it's filled
+ # current diff is always equeal or higher than no. of years
+ surplus = parseInt(diff) + 1 - fy_count
+
+ # the input from where we should start counting down ~> first FY after company started trading
+ # this is just to get selector for the values
+ diff = fy_count - 1
+
+ fy_input = $(".js-financial-year-changed-dates .by-years-wrapper.show-question .js-year-end[data-year-diff='#{diff}']").closest(".js-fy-entries").find(".govuk-date-input")
+
+ fy_day = fy_input.find(".js-fy-day").val()
+ fy_month = fy_input.find(".js-fy-month").val()
+ fy_year = fy_input.find(".js-fy-year").val()
+
+ if !fy_day || !fy_month || !fy_year
+ $(this).find(".js-year-text").html("
")
+ else
+ $(this).find(".js-year-text").text("Year ended #{yearsFromDate(new Date(fy_year, fy_month, fy_day), -(surplus), true)}")
else
$(this).find(".js-year-text").text("Year ended #{fy_day}/#{fy_month}/#{fy_year}")
else
diff --git a/app/form_pointers/financial_summary_pointer.rb b/app/form_pointers/financial_summary_pointer.rb
index c0e56ea83..a5d5f2cdb 100644
--- a/app/form_pointers/financial_summary_pointer.rb
+++ b/app/form_pointers/financial_summary_pointer.rb
@@ -59,16 +59,38 @@ def fill_missing_dates
input.values.each_with_object([]) do |x, acc|
d = dates.dup
- length = x.detect(&:first).values.flatten.size
+ length = x.detect(&:first).values.flatten(1).size
diff = ::Utils::Diff.calc(dates.size, length, abs: false)
- d.shift(diff) if diff && diff.positive?
+ # If the diff between no. of dates & no. of elements (think of cells in the row) is bigger,
+ # we cut the dates, so we don't go over the amount of cells
+ if diff
+ d.shift(diff) if diff.positive?
+
+ # this should only happen for innovation applications, when innovation was launched prior
+ # to company started trading
+ # we then calculate dates as - 1 year from the previous year
+ if diff.negative?
+ diff.abs.times do
+ date_to_calculate_from = d.first
+ if Utils::Date.valid?(date_to_calculate_from)
+ date = Date.parse(date_to_calculate_from).years_ago(1).strftime("%d/%m/%Y")
+ d.unshift(date)
+ else
+ d.unshift(nil)
+ end
+ end
+ end
+ end
if dates_changed
+ # if dates changed, `financial_year_changed_dates` is then the first element
+ # we remove it
idx = x.index { |h| h.keys[0] == :financial_year_changed_dates }
x.delete_at(idx) if idx
end
+ # and push `dates` as first element into the hash
x.unshift(Hash[:dates, d])
acc << x
diff --git a/app/form_pointers/financial_table.rb b/app/form_pointers/financial_table.rb
index 3c31cb4e0..8c552625e 100644
--- a/app/form_pointers/financial_table.rb
+++ b/app/form_pointers/financial_table.rb
@@ -27,8 +27,18 @@ def financial_table_changed_dates_headers
#
if form_answer.innovation?
diff = ::Utils::Diff.calc(innovation_years_number, res.size, abs: false) || 0
- diff.times { res.unshift("") } if diff.positive?
+
res.shift(diff.abs) if diff.negative?
+
+ diff.times do
+ date_to_calculate_from = res.first
+ if Utils::Date.valid?(date_to_calculate_from)
+ date = Date.parse(date_to_calculate_from).years_ago(1).strftime("%d/%m/%Y")
+ res.unshift(date)
+ else
+ res.unshift(nil)
+ end
+ end if diff.positive?
end
if res.any?(&:present?)
diff --git a/app/pdf_generators/qae_pdf_forms/custom_questions/by_year.rb b/app/pdf_generators/qae_pdf_forms/custom_questions/by_year.rb
index d5f0633dc..7bff94379 100644
--- a/app/pdf_generators/qae_pdf_forms/custom_questions/by_year.rb
+++ b/app/pdf_generators/qae_pdf_forms/custom_questions/by_year.rb
@@ -60,8 +60,6 @@ def financial_dates_year_headers(**opts)
financial_table_headers.each.with_index(1) do |item, idx|
frmt = if !::Utils::Date.valid?(item)
FORMATTED_FINANCIAL_YEAR_WITHOUT_DATE
- elsif force_format_without_date?(item)
- FORMATTED_FINANCIAL_YEAR_WITHOUT_DATE
else
opts.dig(:format)
end
@@ -118,18 +116,4 @@ def latest_year_label(with_month_check = true)
[day, month, year]
end
-
- def force_format_without_date?(value)
- doc = form_pdf.filled_answers
-
- date = [:day, :month, :year].each_with_object([]) do |part, memo|
- memo << doc.dig("#{:started_trading}_#{part}")
- end.join("/")
-
- if ::Utils::Date.valid?(date) && ::Utils::Date.valid?(value)
- Date.parse(value).before?(Date.parse(date))
- else
- !::Utils::Date.valid?(value)
- end
- end
end
diff --git a/spec/form_pointers/financial_summary_pointer_spec.rb b/spec/form_pointers/financial_summary_pointer_spec.rb
index f77099797..8c722f7ae 100644
--- a/spec/form_pointers/financial_summary_pointer_spec.rb
+++ b/spec/form_pointers/financial_summary_pointer_spec.rb
@@ -36,7 +36,7 @@
]
end
- it "creeates correct data summary with filled dates" do
+ it "creates correct data summary with filled dates" do
pointer = FinancialSummaryPointer.new(form_answer)
allow(pointer).to receive(:data) { data }
@@ -51,7 +51,7 @@
{:name => "employees_1of2", :value => "10"},
{:name => "employees_2of2", :value => "12"},
] },
- { :dates => [nil, nil, "07/07/2021", "06/09/2022"]},
+ { :dates => ["07/07/2019", "07/07/2020", "07/07/2021", "06/09/2022"]},
{ :sales => [
{:name => "sales_1of4", :value => "10"},
{:name => "sales_2of4", :value => "10"},
@@ -90,7 +90,7 @@
]
end
- it "creeates correct data summary with filled dates" do
+ it "creates correct data summary with filled dates" do
pointer = FinancialSummaryPointer.new(form_answer)
allow(pointer).to receive(:data) { data }
@@ -136,7 +136,7 @@
]
end
- it "creeates correct data summary with filled dates" do
+ it "creates correct data summary with filled dates" do
pointer = FinancialSummaryPointer.new(form_answer)
allow(pointer).to receive(:data) { data }
@@ -152,7 +152,61 @@
{:name => "employees_1of2", :value => "10"},
{:name => "employees_2of2", :value => "12"},
] },
+ { :dates => ["02/01/2020", "02/01/2021", "02/01/2022", "02/01/2023"]},
+ { :sales => [
+ {:name => "sales_1of4", :value => "10"},
+ {:name => "sales_2of4", :value => "10"},
+ {:name => "sales_3of4", :value => "10"},
+ {:name => "sales_4of4", :value => "10"},
+ ] },
+ ]
+ )
+ end
+
+ it "creates correct data summary with filled dates" do
+ pointer = FinancialSummaryPointer.new(form_answer)
+
+ allow(pointer).to receive(:data) { data }
+ allow(pointer).to receive(:partitioned_hash) { partitioned_hash }
+ allow(pointer).to receive(:fetch_financial_year_dates) { [["02/01/2022", "02/01/2023"], false] }
+
+ expect(pointer.summary_data).to eq(
+ [
{ :dates => [nil, nil, "02/01/2022", "02/01/2023"]},
+ { :employees => [
+ {:name => nil, :value => nil},
+ {:name => nil, :value => nil},
+ {:name => "employees_1of2", :value => "10"},
+ {:name => "employees_2of2", :value => "12"},
+ ] },
+ { :dates => ["02/01/2020", "02/01/2021", "02/01/2022", "02/01/2023"]},
+ { :sales => [
+ {:name => "sales_1of4", :value => "10"},
+ {:name => "sales_2of4", :value => "10"},
+ {:name => "sales_3of4", :value => "10"},
+ {:name => "sales_4of4", :value => "10"},
+ ] },
+ ]
+ )
+ end
+
+ it "handles leap years" do
+ pointer = FinancialSummaryPointer.new(form_answer)
+
+ allow(pointer).to receive(:data) { data }
+ allow(pointer).to receive(:partitioned_hash) { partitioned_hash }
+ allow(pointer).to receive(:fetch_financial_year_dates) { [["29/02/2024", "01/05/2025"], false] }
+
+ expect(pointer.summary_data).to eq(
+ [
+ { :dates => [nil, nil, "29/02/2024", "01/05/2025"]},
+ { :employees => [
+ {:name => nil, :value => nil},
+ {:name => nil, :value => nil},
+ {:name => "employees_1of2", :value => "10"},
+ {:name => "employees_2of2", :value => "12"},
+ ] },
+ { :dates => ["28/02/2022", "28/02/2023", "29/02/2024", "01/05/2025"]},
{ :sales => [
{:name => "sales_1of4", :value => "10"},
{:name => "sales_2of4", :value => "10"},
@@ -182,33 +236,6 @@
},
]
end
-
- it "creeates correct data summary with filled dates" do
- pointer = FinancialSummaryPointer.new(form_answer)
-
- allow(pointer).to receive(:data) { data }
- allow(pointer).to receive(:partitioned_hash) { partitioned_hash }
- allow(pointer).to receive(:fetch_financial_year_dates) { [["02/01/2020", "02/01/2021", "02/01/2022", "02/01/2023"], false] }
-
- expect(pointer.summary_data).to eq(
- [
- { :dates => ["02/01/2020", "02/01/2021", "02/01/2022", "02/01/2023"]},
- { :employees => [
- {:name => "employees_1of4", :value => "10"},
- {:name => "employees_2of4", :value => "10"},
- {:name => "employees_3of4", :value => "10"},
- {:name => "employees_4of4", :value => "10"},
- ] },
- { :dates => [nil, nil, "02/01/2022", "02/01/2023"]},
- { :sales => [
- {:name => nil, :value => nil},
- {:name => nil, :value => nil},
- {:name => "sales_1of2", :value => "10"},
- {:name => "sales_2of2", :value => "10"},
- ] },
- ]
- )
- end
end
end
end