From 0a3eaddf22110bc144a436f7039db2f41a6bab64 Mon Sep 17 00:00:00 2001 From: diPhantxm Date: Thu, 16 Mar 2023 20:00:56 +0300 Subject: [PATCH] add method for getting financial report --- ENDPOINTS.md | 2 +- ozon/reports.go | 77 ++++++++++++++++++++++++++++++++++++++++++++ ozon/reports_test.go | 70 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 145 insertions(+), 4 deletions(-) diff --git a/ENDPOINTS.md b/ENDPOINTS.md index 7cee169..8227f75 100644 --- a/ENDPOINTS.md +++ b/ENDPOINTS.md @@ -160,7 +160,7 @@ - [ ] Report on products movement - [ ] Returns report - [ ] Shipment report -- [ ] Financial report +- [x] Financial report - [ ] Issue a report on discounted products - [ ] Report on discounted products - [ ] List of reports on discounted products diff --git a/ozon/reports.go b/ozon/reports.go index d1a3645..49fe001 100644 --- a/ozon/reports.go +++ b/ozon/reports.go @@ -148,3 +148,80 @@ func (c Reports) GetReportDetails(params *GetReportDetailsParams) (*GetReportDet return resp, nil } + +type GetFinancialReportParams struct { + // Report generation period + Date GetFinancialReportDatePeriod `json:"date"` + + // Number of the page returned in the request + Page int64 `json:"page"` + + // Number of items on the page + PageSize int64 `json:"page_size"` +} + +type GetFinancialReportDatePeriod struct { + // Date from which the report is calculated + From time.Time `json:"from"` + + // Date up to which the report is calculated + To time.Time `json:"to"` +} + +type GetFinancialReportResponse struct { + core.CommonResponse + + // Method result + Result struct { + // Reports list + CashFlows []struct { + // Period data + Period struct { + // Period identifier + Id int64 `json:"id"` + + // Period start + Begin time.Time `json:"begin"` + + // Period end + End time.Time `json:"end"` + } `json:"period"` + + // Sum of sold products prices + OrdersAmount float64 `json:"order_amount"` + + // Sum of returned products prices + ReturnsAmount float64 `json:"returns_amount"` + + // Ozon sales commission + CommissionAmount float64 `json:"commission_amount"` + + // Additional services cost + ServicesAmount float64 `json:"services_amount"` + + // Logistic services cost + ItemDeliveryAndReturnAmount float64 `json:"item_delivery_and_return_amount"` + + // Code of the currency used to calculate the commissions + CurrencyCode string `json:"currency_code"` + } `json:"cash_flows"` + + // Number of pages with reports + PageCount int64 `json:"page_count"` + } `json:"result"` +} + +// Returns information about a created report by its identifier +func (c Reports) GetFinancial(params *GetFinancialReportParams) (*GetFinancialReportResponse, error) { + url := "/v1/finance/cash-flow-statement/list" + + resp := &GetFinancialReportResponse{} + + response, err := c.client.Request(http.MethodPost, url, params, resp) + if err != nil { + return nil, err + } + response.CopyCommonResponse(&resp.CommonResponse) + + return resp, nil +} diff --git a/ozon/reports_test.go b/ozon/reports_test.go index 50878f7..b65ca85 100644 --- a/ozon/reports_test.go +++ b/ozon/reports_test.go @@ -89,9 +89,7 @@ func TestGetReportDetails(t *testing.T) { { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, - &GetReportDetailsParams{ - - }, + &GetReportDetailsParams{}, `{ "result": { "code": "257bf213-ca57-405c-8edf-41d2ce22decf", @@ -129,3 +127,69 @@ func TestGetReportDetails(t *testing.T) { } } } + +func TestGetFinancial(t *testing.T) { + tests := []struct { + statusCode int + headers map[string]string + params *GetFinancialReportParams + response string + }{ + // Test Ok + { + http.StatusOK, + map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, + &GetFinancialReportParams{ + Date: GetFinancialReportDatePeriod{ + From: core.TimeFromString(t, "2006-01-02T15:04:05Z", "2022-01-01T00:00:00.000Z"), + To: core.TimeFromString(t, "2006-01-02T15:04:05Z", "2022-12-31T00:00:00.000Z"), + }, + Page: 1, + PageSize: 1, + }, + `{ + "result": { + "cash_flows": [ + { + "period": { + "id": 11567022278500, + "begin": "2022-08-01T00:00:00Z", + "end": "2022-08-15T00:00:00Z" + }, + "orders_amount": 1000, + "returns_amount": -3000, + "commission_amount": 1437, + "services_amount": 8471.28, + "item_delivery_and_return_amount": 1991, + "currency_code": "RUB" + } + ], + "page_count": 15 + } + }`, + }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + &GetFinancialReportParams{}, + `{ + "code": 16, + "message": "Client-Id and Api-Key headers are required" + }`, + }, + } + + for _, test := range tests { + c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers)) + + resp, err := c.Reports().GetFinancial(test.params) + if err != nil { + t.Error(err) + } + + if resp.StatusCode != test.statusCode { + t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode) + } + } +}