container: lib_taxes: optimize record printing, add checks and logging

- When applicable, as the stream progresses, exit each successive gawk
  iteration when no applicable records are present (no taxable events
  found for the given tag in the given year).

  Previously, when no records were found, a reports file was produced
  with either a single header-only row or junk data that resulted from
  the assumption there would be real data.

  A preexisting hack prevented the junk data from being written after
  production but this commit will resolve that hack by preventing the
  printing of junk data while also preventing the production of a
  header-only file.

- Print warnings when no taxable events are found for the given year.

- Add checks:
  * Only verify patched files if the patched files are not empty.
  * Empty variable checks.
This commit is contained in:
2025-01-10 18:35:50 -08:00
parent a1eff15e8b
commit 08c1de5d9d

View File

@@ -446,6 +446,8 @@ function lib_taxes::__taxes_print()
printf "\n"
}
{
if (!NR) { exit }
Date = $1
Action = $2
Account = $3
@@ -625,6 +627,9 @@ function lib_taxes::__taxes_print()
END { print prev }' \
| gawk -M -v PREC=100 \
'{
# NOTE: if previous only produces OFS, this will exit
if (!$NF) { exit }
# If more than triplet form, support quadruple form
# TODO: support more than 4 if necessary
if ($13 == $10)
@@ -839,7 +844,7 @@ function lib_taxes::__taxes_print()
print $0
exit
}
}' | grep -vE "(,BUY,,,,USD,0,,|,,,,,,,,,,,0.000000000000000000)" # TODO: HACK
}'
fi
#
@@ -861,6 +866,8 @@ function lib_taxes::__taxes_print()
| sort -u \
| gawk \
'{
if (!NR) { exit }
txid = $1; FeeCurrency = $5; Fee = $6
a[$1]=a[$1] ? a[$1] OFS txid OFS FeeCurrency OFS Fee : $0
} END { for(i in a) {print a[i]} }' FS=, OFS=, \
@@ -956,12 +963,24 @@ function lib_taxes::__taxes_print()
if (NR == 1)
{
Header=$0
print Header
}
# Save non-header row(s)
if ($0 != Header)
{
print
Data[i++] = $0
}
}
END {
# Only print header if non-header row(s) exist
if (length(Data))
{
print Header
}
# Print all non-header row(s)
for (i=0; i < length(Data); i++)
{
print Data[i]
}
}' FS=, OFS=,
# WARNING:
# - Do not run unique (sort -ru)! There are legitimate income/trade entries
@@ -1026,8 +1045,14 @@ function lib_taxes::__taxes_write()
lib_utils::print_custom " \e[32m├─\e[34m\e[1;3m ${_arg_tag} (full)\e[0m\n"
local _base_path="${_tax_root_dir}/${_arg_tag}/${_tax_year}_${_arg_tag}"
lib_taxes::__taxes_print "$_tax_year" "$_arg_tag" >"${_base_path}-${_ext_full}"
local _out_file="${_base_path}-${_ext_full}"
lib_taxes::__taxes_print "$_tax_year" "$_arg_tag" >"${_out_file}"
lib_utils::catch $?
if [[ ! -f "$_out_file" || ! -s "$_out_file" ]]; then
lib_utils::print_warning "Nothing generated for '${_arg_tag}' (no taxable event found for the year ${global_arg_year})"
fi
done
# Patch transparent (full) reports
@@ -1207,6 +1232,9 @@ function lib_taxes::__reports_patch()
# Verify success of patches
#
lib_utils::print_custom " \e[32m│\e[0m\t\e[37;2m ... Verifying patches (full) ...\e[0m\n"
lib_utils::print_custom " \e[32m│\e[0m\n"
lib_taxes::__reports_patch_verify "$_income" "${_spends_tags[@]}" "${_trades_tags[@]}"
lib_taxes::__reports_patch_verify "$_spends" "${_trades_tags[@]}" "${_income_tags[@]}"
lib_taxes::__reports_patch_verify "$_trades" "${_income_tags[@]}" "${_spends_tags[@]}"
@@ -1217,16 +1245,20 @@ function lib_taxes::__reports_patch_verify()
local _file="$1"
local _tags=("${@:2}")
[ ! -f "$_file" ] && lib_utils::die_fatal "$_file not found"
[[ -z "$_file" || -z "${_tags[*]}" ]] && lib_utils::die_fatal
xsv select "Action" "$_file" \
| sort -u \
| tail -n +2 \
| while read _line; do
for _tag in "${_tags[@]}"; do
[[ "$_line" != "$_tag" ]] || lib_utils::die_fatal "Bad entry in $_file"
[ ! -f "$_file" ] && lib_utils::die_fatal "File not found: '${_file}'"
if [ -s "$_file" ]; then
xsv select "Action" "$_file" \
| sort -u \
| tail -n +2 \
| while read _line; do
for _tag in "${_tags[@]}"; do
[[ "$_line" != "$_tag" ]] || lib_utils::die_fatal "Bad entry in $_file"
done
done
done
fi
}
#