Skip to content

Commit

Permalink
Handle empty string parsing for balance and amount values for PnL rep…
Browse files Browse the repository at this point in the history
…ort (#80)
  • Loading branch information
xacadil authored Nov 21, 2023
1 parent 88790a2 commit debbee7
Showing 1 changed file with 56 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
LOGGER = singer.get_logger()
NUMBER_OF_PERIODS = 3


class ProfitAndLossDetailReport(QuickbooksStream):
tap_stream_id: ClassVar[str] = 'ProfitAndLossDetailReport'
stream: ClassVar[str] = 'ProfitAndLossDetailReport'
tap_stream_id: ClassVar[str] = "ProfitAndLossDetailReport"
stream: ClassVar[str] = "ProfitAndLossDetailReport"
key_properties: ClassVar[List[str]] = []
replication_method: ClassVar[str] = 'FULL_TABLE'
replication_method: ClassVar[str] = "FULL_TABLE"
current_account = {}

def __init__(self, qb, start_date, state_passed,pnl_adjusted_gain_loss=None):
Expand All @@ -35,11 +36,11 @@ def _get_column_metadata(self, resp):

def _recursive_row_search(self, row, output, categories):
row_group = row.get("Rows")
if row.get("type")=="Section":
if row.get("type") == "Section":
if row.get("Header", {}).get("ColData", [{}]):
if row.get("Header", {}).get("ColData", [{}])[0].get("id"):
self.current_account = row.get("Header", {}).get("ColData", [{}])[0]
if 'ColData' in list(row.keys()):
if "ColData" in list(row.keys()):
# Write the row
data = row.get("ColData")
values = [column for column in data]
Expand Down Expand Up @@ -106,23 +107,25 @@ def sync(self, catalog_entry):
start_date = self.start_date.date()
delta = 364

while start_date<datetime.date.today():
while start_date < datetime.date.today():
LOGGER.info(f"Starting full sync of P&L")
end_date = (start_date + datetime.timedelta(delta))
if end_date>datetime.date.today():
end_date = start_date + datetime.timedelta(delta)
if end_date > datetime.date.today():
end_date = datetime.date.today()

params = {
"start_date": start_date.strftime("%Y-%m-%d"),
"end_date": end_date.strftime("%Y-%m-%d"),
"accounting_method": "Accrual",
"columns": ",".join(cols)
"columns": ",".join(cols),
}
if self.pnl_adjusted_gain_loss:
params.update({"adjusted_gain_loss":self.pnl_adjusted_gain_loss})

LOGGER.info(f"Fetch Journal Report for period {params['start_date']} to {params['end_date']}")
resp = self._get(report_entity='ProfitAndLossDetail', params=params)
LOGGER.info(
f"Fetch Journal Report for period {params['start_date']} to {params['end_date']}"
)
resp = self._get(report_entity="ProfitAndLossDetail", params=params)
start_date = end_date + datetime.timedelta(1)

# Get column metadata.
Expand Down Expand Up @@ -156,13 +159,29 @@ def sync(self, catalog_entry):
cleansed_row[f"{k}Id"] = v.get("id")
else:
cleansed_row[k] = v

cleansed_row["Amount"] = float(cleansed_row.get("Amount")) if cleansed_row.get("Amount") else None
cleansed_row["Balance"] = float(cleansed_row.get("Balance")) if cleansed_row.get("Amount") else None
cleansed_row["SyncTimestampUtc"] = singer.utils.strftime(singer.utils.now(), "%Y-%m-%dT%H:%M:%SZ")
if cleansed_row.get('Date'):
try:
cleansed_row["Amount"] = (
float(cleansed_row.get("Amount"))
if cleansed_row.get("Amount")
else None
)
except:
cleansed_row["Amount"] = None
try:
cleansed_row["Balance"] = (
float(cleansed_row.get("Balance"))
if cleansed_row.get("Amount")
else None
)
except:
cleansed_row["Balance"] = None

cleansed_row["SyncTimestampUtc"] = singer.utils.strftime(
singer.utils.now(), "%Y-%m-%dT%H:%M:%SZ"
)
if cleansed_row.get("Date"):
try:
cleansed_row["Date"] = parse(cleansed_row['Date'])
cleansed_row["Date"] = parse(cleansed_row["Date"])
except:
continue

Expand All @@ -177,11 +196,13 @@ def sync(self, catalog_entry):
"start_date": start_date.strftime("%Y-%m-%d"),
"end_date": end_date.strftime("%Y-%m-%d"),
"accounting_method": "Accrual",
"columns": ",".join(cols)
"columns": ",".join(cols),
}

LOGGER.info(f"Fetch Journal Report for period {params['start_date']} to {params['end_date']}")
resp = self._get(report_entity='ProfitAndLossDetail', params=params)
LOGGER.info(
f"Fetch Journal Report for period {params['start_date']} to {params['end_date']}"
)
resp = self._get(report_entity="ProfitAndLossDetail", params=params)

# Get column metadata.
columns = self._get_column_metadata(resp)
Expand Down Expand Up @@ -216,11 +237,21 @@ def sync(self, catalog_entry):
else:
cleansed_row[k] = v

cleansed_row["Amount"] = float(cleansed_row.get("Amount")) if cleansed_row.get("Amount") else None
cleansed_row["Balance"] = float(cleansed_row.get("Balance")) if cleansed_row.get("Amount") else None
cleansed_row["SyncTimestampUtc"] = singer.utils.strftime(singer.utils.now(), "%Y-%m-%dT%H:%M:%SZ")
if cleansed_row.get('Date'):
cleansed_row["Date"] = parse(cleansed_row['Date'])
cleansed_row["Amount"] = (
float(cleansed_row.get("Amount"))
if cleansed_row.get("Amount")
else None
)
cleansed_row["Balance"] = (
float(cleansed_row.get("Balance"))
if cleansed_row.get("Amount")
else None
)
cleansed_row["SyncTimestampUtc"] = singer.utils.strftime(
singer.utils.now(), "%Y-%m-%dT%H:%M:%SZ"
)
if cleansed_row.get("Date"):
cleansed_row["Date"] = parse(cleansed_row["Date"])

yield cleansed_row

Expand Down

0 comments on commit debbee7

Please sign in to comment.