diff --git a/tap_quickbooks/quickbooks/reportstreams/ProfitAndLossDetailReport.py b/tap_quickbooks/quickbooks/reportstreams/ProfitAndLossDetailReport.py index d8c50b8..5a7dfd9 100644 --- a/tap_quickbooks/quickbooks/reportstreams/ProfitAndLossDetailReport.py +++ b/tap_quickbooks/quickbooks/reportstreams/ProfitAndLossDetailReport.py @@ -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): @@ -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] @@ -106,23 +107,25 @@ def sync(self, catalog_entry): start_date = self.start_date.date() delta = 364 - while start_datedatetime.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. @@ -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 @@ -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) @@ -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