如何使用 QuickBooks API 获取完整的按产品/服务详细销售情况报告?

问题描述 投票:0回答:1

我有下面的Python代码试图获取

Sales by Product/Service Detail
报告数据。

在文档下:https://developer.intuit.com/app/developer/qbo/docs/api/accounting/report-entities/SalesByProduct

看起来终点将是

[ItemSales]
,尽管 API 文档的终点是
[SalesByProduct]
,这令人困惑。

如何更改下面的代码才能提取所有默认指标:

  • 产品/服务
  • 日期
  • 交易类型
  • 数量
  • 客户姓名
  • 备注/说明
  • 数量
  • 价格
  • 金额
  • 平衡

我附上了运行

Sales by Product/Service Detail
报告时报告在 QuickBooks UI 下的外观的屏幕截图。

此外,我希望能够提取所有数据,目前我只获得 96 个对象,但当我通过 UI 运行报告时,我获得 2315 行。报告 API 中有分页机制吗?如果是这样,我该如何实施?

注意:当我在 UI 下运行报告时,我没有使用任何分组值来查看项目行级别的数据,这是否意味着我不应该使用 [summarize_column_by] 属性

def fetch_sales_by_product(access_token):

  url = f'https://quickbooks.api.intuit.com/v3/company/11111111111/reports/ItemSales' 
  headers = {
    'Authorization': f'Bearer {access_token}',
    'Accept': 'application/json'
  }

  params = {
    'start_date': '2021-01-01',
    'end_date': '2024-04-01',
    'summarize_column_by': 'ProductsAndServices'
  }

  all_data = []
  counter = 1

  while True:
    response = requests.get(url, headers=headers, params=params)
    print(f"{response.status_code=}")
    if response.status_code != 200:
      raise Exception(f"Failed to fetch report: {response.text}" )
    data = response.json()
    all_data.extend(data['Rows']['Row'])
    counter += 1

  return all_data

python quickbooks quickbooks-online
1个回答
0
投票

修改提供的 Python 代码以提取所有默认指标(产品/服务、日期、交易类型、数量、客户名称、备忘录/描述、数量、费率、金额、余额)并处理分页以检索所有数据QuickBooks 中的 Sales by Product/Service Detail 报告,您需要进行以下调整:

  1. 根据以下内容将端点 URL 更改为 /reports/SalesByProduct QuickBooks API 文档。
  2. 删除summary_column_by参数,因为你想得到 项目行级别的数据而不是对其进行汇总。
  3. 通过检查是否存在来实现分页 响应中的 next_page_url 并使用页面更新参数 参数来获取下一页数据。
  4. 确保妥善处理潜在的错误和异常。

这是修改后的Python代码:

import requests

def fetch_sales_by_product(access_token): url = 'https://quickbooks.api.intuit.com/v3/company/11111111111/reports/SalesByProduct' 标题= { '授权': f'承载{access_token}', '接受': '应用程序/json' }

params = {
    'start_date': '2021-01-01',
    'end_date': '2024-04-01'
}

all_data = []

while True:
    response = requests.get(url, headers=headers, params=params)
    print(f"Status code: {response.status_code}")

    if response.status_code != 200:
        raise Exception(f"Failed to fetch report: {response.text}")

    data = response.json()
    all_data.extend(data['Rows']['Row'])

    # Check for pagination
    if 'next_page_url' in data:
        url = data['next_page_url']
        # Extracting page number from the next_page_url
        page_number = int(url.split('=')[-1])
        # Update params with the page number
        params['page'] = page_number
    else:
        break

return all_data

此修改后的代码现在应该通过处理分页而不汇总数据,正确从 QuickBooks 中的按产品/服务详细信息销售报告中获取所有数据。此外,它还确保 API 请求期间任何潜在问题的错误处理。

© www.soinside.com 2019 - 2024. All rights reserved.