Skip to content

Commit

Permalink
Merge pull request #2453 from bitzesty/financial-years-pre-filling
Browse files Browse the repository at this point in the history
Logic change for FY pre-filling [production]
  • Loading branch information
dreamfall authored Aug 8, 2023
2 parents 5f42c01 + 608df8f commit ef88afb
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 68 deletions.
59 changes: 41 additions & 18 deletions app/assets/javascripts/application.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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("<br style='visibility:hidden'>")
$(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 ->
Expand All @@ -328,15 +331,35 @@ 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()
fy_year = fy_input.find(".js-fy-year").val()

$(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("<br style='visibility:hidden'>")
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("<br style='visibility:hidden'>")
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
Expand Down
26 changes: 24 additions & 2 deletions app/form_pointers/financial_summary_pointer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion app/form_pointers/financial_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
Expand Down
16 changes: 0 additions & 16 deletions app/pdf_generators/qae_pdf_forms/custom_questions/by_year.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
89 changes: 58 additions & 31 deletions spec/form_pointers/financial_summary_pointer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"},
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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 }
Expand All @@ -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"},
Expand Down Expand Up @@ -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

0 comments on commit ef88afb

Please sign in to comment.