1 package com.srv4pos.server.api.sale;
2
3 import com.srv4pos.server.api.infrastructure.EmptyConnectionWriter;
4 import com.srv4pos.server.api.infrastructure.HttpMethod;
5 import com.srv4pos.server.api.infrastructure.IdInfo;
6 import com.srv4pos.server.api.infrastructure.StringConnectionWriter;
7 import com.srv4pos.server.api.infrastructure.credentials.Credentials;
8 import com.srv4pos.server.api.infrastructure.exceptions.enums.UnprocessableEntityErrorType;
9 import com.srv4pos.server.api.infrastructure.http.HttpHelper;
10 import com.srv4pos.server.api.infrastructure.http.Transport;
11 import com.srv4pos.server.api.infrastructure.request.StringResponse;
12
13 import java.net.URL;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17
18 import static com.srv4pos.server.api.infrastructure.http.HttpHelper.CONTENT_TYPE_APPLICATION_JSON;
19 import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestString;
20 import static java.lang.String.format;
21
22
23
24
25
26 public class SaleService {
27 private URL url;
28 private Credentials credentials;
29 private Transport transport;
30
31
32
33
34
35
36
37
38 public SaleService(URL url, Credentials credentials, Transport transport) {
39 this.url = url;
40 this.credentials = credentials;
41 this.transport = transport;
42 }
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public long create(int sellerVersion, int registrationVersion, SaleInfo saleInfo) {
59 return IdInfo.fromJsonToIdInfo(requestString(
60 format("%s/sales", saleInfo.getSellerIdentifier()),
61 HttpMethod.POST,
62 url,
63 credentials.toAuthentication(),
64 HttpHelper.toETag(sellerVersion, registrationVersion),
65 null,
66 new StringConnectionWriter(saleInfo.toJson()),
67 CONTENT_TYPE_APPLICATION_JSON,
68 transport
69 ).getContent()).getId();
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84 public List<SaleShortInfo> list(String sellerId, String period, Integer firstResult,
85 Integer maxResults, String like,
86 SaleInfo.Fields orderBy,
87 Boolean orderDesc) {
88
89 Map<String, String> params = new HashMap<String, String>();
90
91 if (orderBy != null) {
92 params.put("orderBy", orderBy.name());
93 }
94 if (orderDesc != null) {
95 params.put("orderDesc", orderDesc.toString());
96 }
97 if (firstResult != null) {
98 params.put("firstResult", Integer.toString(firstResult));
99 }
100 if (maxResults != null) {
101 params.put("maxResults", Integer.toString(maxResults));
102 }
103 if (like != null) {
104 params.put("like", like);
105 }
106 if (period != null) {
107 params.put("period", period);
108 }
109
110 StringResponse response = requestString(
111 format("%s/sales-minified", sellerId),
112 HttpMethod.GET,
113 url,
114 credentials.toAuthentication(),
115 null,
116 params,
117 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
118 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
119 transport
120 );
121 return response == null ? null : (List<SaleShortInfo>) SaleShortInfo.fromJsonArrayToSaleShortInfoes(response.getContent());
122 }
123
124
125
126
127
128
129
130
131
132
133
134 public SaleStatsInfo
135 stats(String sellerId, String period, String cashRegisterName) {
136 Map<String, String> params = new HashMap<String, String>();
137 if (cashRegisterName != null) {
138 params.put("cashRegisterName", cashRegisterName);
139 }
140 if (period != null) {
141 params.put("period", period);
142 }
143
144 StringResponse response = requestString(
145 format("%s/sales/stats", sellerId),
146 HttpMethod.GET,
147 url,
148 credentials.toAuthentication(),
149 null,
150 params,
151 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
152 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
153 transport
154 );
155 return response == null ? null : SaleStatsInfo.fromJsonToSaleStatsInfo(response.getContent());
156 }
157
158
159
160
161
162
163
164
165 public List<SaleInfo> listDiff(int versionFrom, int versionTo) {
166 StringResponse response = requestString(
167 String.format("%s/sales-diff/%s/%s", credentials.getSellerId(), versionFrom, versionTo),
168 HttpMethod.GET,
169 url,
170 credentials.toAuthentication(),
171 null,
172 null,
173 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
174 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
175 transport
176 );
177 return (List<SaleInfo>) SaleInfo.fromJsonArrayToSaleInfoes(response.getContent());
178 }
179 }