Skip to content

Commit f591745

Browse files
committed
add methods for generating returns and shipment reports
1 parent cf9eae4 commit f591745

File tree

3 files changed

+320
-4
lines changed

3 files changed

+320
-4
lines changed

ENDPOINTS.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@
155155
- [x] Report details
156156
- [x] Reports list
157157
- [x] Products report
158-
- [ ] Stocks report
159-
- [ ] Report on products movement
160-
- [ ] Returns report
161-
- [ ] Shipment report
158+
- [x] Stocks report
159+
- [x] Report on products movement
160+
- [x] Returns report
161+
- [x] Shipment report
162162
- [x] Financial report
163163
- [ ] Issue a report on discounted products
164164
- [ ] Report on discounted products

ozon/reports.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,168 @@ func (c Reports) GetStocks(params *GetStocksReportParams) (*GetStocksReportRespo
304304

305305
return resp, nil
306306
}
307+
308+
type GetProductsMovementReportParams struct {
309+
// Date from which the data will be in the report
310+
DateFrom time.Time `json:"date_from"`
311+
312+
// Date up to which the data will be in the report
313+
DateTo time.Time `json:"date_to"`
314+
315+
// Default: "DEFAULT"
316+
// Response language:
317+
// - RU — Russian
318+
// - EN — English
319+
Language string `json:"language" default:"DEFAULT"`
320+
}
321+
322+
type GetProductsMovementReportResponse struct {
323+
core.CommonResponse
324+
325+
// Method result
326+
Result struct {
327+
// Unique report identifier
328+
Code string `json:"code"`
329+
} `json:"result"`
330+
}
331+
332+
// Report with complete information on products, as well as the number of products with statuses:
333+
// - products with defects or in inventory,
334+
// - products in transit between the fulfillment centers,
335+
// - products in delivery,
336+
// - products to be sold
337+
func (c Reports) GetProductsMovement(params *GetProductsMovementReportParams) (*GetProductsMovementReportResponse, error) {
338+
url := "/v1/report/products/movement/create"
339+
340+
resp := &GetProductsMovementReportResponse{}
341+
342+
response, err := c.client.Request(http.MethodPost, url, params, resp)
343+
if err != nil {
344+
return nil, err
345+
}
346+
response.CopyCommonResponse(&resp.CommonResponse)
347+
348+
return resp, nil
349+
}
350+
351+
type GetReturnsReportParams struct {
352+
// Filter
353+
Filter GetReturnsReportsFilter `json:"filter"`
354+
355+
// Default: "DEFAULT"
356+
// Response language:
357+
// - RU — Russian
358+
// - EN — English
359+
Language string `json:"language" default:"DEFAULT"`
360+
}
361+
362+
type GetReturnsReportsFilter struct {
363+
// Order delivery scheme: fbs — delivery from seller's warehouse
364+
DeliverySchema string `json:"delivery_schema"`
365+
366+
// Order identifier
367+
OrderId int64 `json:"order_id"`
368+
369+
// Order status
370+
Status string `json:"status"`
371+
}
372+
373+
type GetReturnsReportResponse struct {
374+
core.CommonResponse
375+
376+
// Method result
377+
Result struct {
378+
// Unique report identifier
379+
Code string `json:"code"`
380+
} `json:"result"`
381+
}
382+
383+
// The report contains information about returned products that were accepted from the customer, ready for pickup, or delivered to the seller.
384+
//
385+
// The method is only suitable for orders shipped from the seller's warehouse
386+
func (c Reports) GetReturns(params *GetReturnsReportParams) (*GetReturnsReportResponse, error) {
387+
url := "/v1/report/returns/create"
388+
389+
resp := &GetReturnsReportResponse{}
390+
391+
response, err := c.client.Request(http.MethodPost, url, params, resp)
392+
if err != nil {
393+
return nil, err
394+
}
395+
response.CopyCommonResponse(&resp.CommonResponse)
396+
397+
return resp, nil
398+
}
399+
400+
type GetShipmentReportParams struct {
401+
// Filter
402+
Filter GetShipmentReportFilter `json:"filter"`
403+
404+
// Default: "DEFAULT"
405+
// Response language:
406+
// - RU — Russian
407+
// - EN — English
408+
Language string `json:"language"`
409+
}
410+
411+
type GetShipmentReportFilter struct {
412+
// Cancellation reason identifier
413+
CancelReasonId []int64 `json:"cancel_reason_id"`
414+
415+
// Work scheme: FBO or FBS.
416+
//
417+
// To get an FBO scheme report, pass fbo in this parameter. For an FBS scheme report pass fbs
418+
DeliverySchema []string `json:"delivery_schema"`
419+
420+
// Product identifier
421+
OfferId string `json:"offer_id"`
422+
423+
// Order processing start date and time
424+
ProcessedAtFrom time.Time `json:"processed_at_from"`
425+
426+
// Time when the order appeared in your personal account
427+
ProcessedAtTo time.Time `json:"processed_at_to"`
428+
429+
// Product identifier in the Ozon system, SKU
430+
SKU []int64 `json:"sku"`
431+
432+
// Status text
433+
StatusAlias []string `json:"status_alias"`
434+
435+
// Numerical status
436+
Statuses []int64 `json:"statused"`
437+
438+
// Product name
439+
Title string `json:"title"`
440+
}
441+
442+
type GetShipmentReportResponse struct {
443+
core.CommonResponse
444+
445+
// Method result
446+
Result struct {
447+
// Unique report identifier
448+
Code string `json:"code"`
449+
} `json:"result"`
450+
}
451+
452+
// Shipment report with orders details:
453+
// - order statuses
454+
// - processing start date
455+
// - order numbers
456+
// - shipment numbers
457+
// - shipment costs
458+
// - shipments contents
459+
func (c Reports) GetShipment(params *GetShipmentReportParams) (*GetShipmentReportResponse, error) {
460+
url := "/v1/report/postings/create"
461+
462+
resp := &GetShipmentReportResponse{}
463+
464+
response, err := c.client.Request(http.MethodPost, url, params, resp)
465+
if err != nil {
466+
return nil, err
467+
}
468+
response.CopyCommonResponse(&resp.CommonResponse)
469+
470+
return resp, nil
471+
}

ozon/reports_test.go

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,154 @@ func TestGetStocksReport(t *testing.T) {
291291
}
292292
}
293293
}
294+
295+
func TestGetProductsMovementReport(t *testing.T) {
296+
t.Parallel()
297+
298+
tests := []struct {
299+
statusCode int
300+
headers map[string]string
301+
params *GetProductsMovementReportParams
302+
response string
303+
}{
304+
// Test Ok
305+
{
306+
http.StatusOK,
307+
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
308+
&GetProductsMovementReportParams{
309+
DateFrom: core.TimeFromString(t, "2006-01-02T15:04:05Z", "2020-08-01T14:15:22Z"),
310+
DateTo: core.TimeFromString(t, "2006-01-02T15:04:05Z", "2020-08-01T14:15:22Z"),
311+
},
312+
`{
313+
"result": {
314+
"code": "h56f4917-1346-4e64-9d90-с6e736c1e07c"
315+
}
316+
}`,
317+
},
318+
// Test No Client-Id or Api-Key
319+
{
320+
http.StatusUnauthorized,
321+
map[string]string{},
322+
&GetProductsMovementReportParams{},
323+
`{
324+
"code": 16,
325+
"message": "Client-Id and Api-Key headers are required"
326+
}`,
327+
},
328+
}
329+
330+
for _, test := range tests {
331+
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
332+
333+
resp, err := c.Reports().GetProductsMovement(test.params)
334+
if err != nil {
335+
t.Error(err)
336+
}
337+
338+
if resp.StatusCode != test.statusCode {
339+
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
340+
}
341+
}
342+
}
343+
344+
func TestGetReturnsReport(t *testing.T) {
345+
t.Parallel()
346+
347+
tests := []struct {
348+
statusCode int
349+
headers map[string]string
350+
params *GetReturnsReportParams
351+
response string
352+
}{
353+
// Test Ok
354+
{
355+
http.StatusOK,
356+
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
357+
&GetReturnsReportParams{
358+
Filter: GetReturnsReportsFilter{
359+
DeliverySchema: "fbs",
360+
},
361+
},
362+
`{
363+
"result": {
364+
"code": "d55f4517-8347-4e24-9d93-d6e736c1c07c"
365+
}
366+
}`,
367+
},
368+
// Test No Client-Id or Api-Key
369+
{
370+
http.StatusUnauthorized,
371+
map[string]string{},
372+
&GetReturnsReportParams{},
373+
`{
374+
"code": 16,
375+
"message": "Client-Id and Api-Key headers are required"
376+
}`,
377+
},
378+
}
379+
380+
for _, test := range tests {
381+
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
382+
383+
resp, err := c.Reports().GetReturns(test.params)
384+
if err != nil {
385+
t.Error(err)
386+
}
387+
388+
if resp.StatusCode != test.statusCode {
389+
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
390+
}
391+
}
392+
}
393+
394+
func TestGetShipmentReport(t *testing.T) {
395+
t.Parallel()
396+
397+
tests := []struct {
398+
statusCode int
399+
headers map[string]string
400+
params *GetShipmentReportParams
401+
response string
402+
}{
403+
// Test Ok
404+
{
405+
http.StatusOK,
406+
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
407+
&GetShipmentReportParams{
408+
Filter: GetShipmentReportFilter{
409+
DeliverySchema: []string{"fbs", "fbo", "crossborder"},
410+
ProcessedAtFrom: core.TimeFromString(t, "2006-01-02T15:04:05Z", "2021-09-02T17:10:54.861Z"),
411+
ProcessedAtTo: core.TimeFromString(t, "2006-01-02T15:04:05Z", "2021-11-02T17:10:54.861Z"),
412+
},
413+
},
414+
`{
415+
"result": {
416+
"code": "d55f4517-8347-4e24-9d93-d6e736c1c07c"
417+
}
418+
}`,
419+
},
420+
// Test No Client-Id or Api-Key
421+
{
422+
http.StatusUnauthorized,
423+
map[string]string{},
424+
&GetShipmentReportParams{},
425+
`{
426+
"code": 16,
427+
"message": "Client-Id and Api-Key headers are required"
428+
}`,
429+
},
430+
}
431+
432+
for _, test := range tests {
433+
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
434+
435+
resp, err := c.Reports().GetShipment(test.params)
436+
if err != nil {
437+
t.Error(err)
438+
}
439+
440+
if resp.StatusCode != test.statusCode {
441+
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
442+
}
443+
}
444+
}

0 commit comments

Comments
 (0)