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.IdentifierInfo;
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.StreamResponse;
12 import com.srv4pos.server.api.infrastructure.request.StringResponse;
13
14 import java.io.InputStream;
15 import java.net.URL;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 import static com.srv4pos.server.api.infrastructure.http.HttpHelper.CONTENT_TYPE_APPLICATION_JSON;
21 import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestStream;
22 import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestString;
23 import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestVoid;
24 import static java.lang.String.format;
25
26
27
28
29
30 public class SaleService {
31 private URL url;
32 private Credentials credentials;
33 private Transport transport;
34
35
36
37
38
39
40
41
42 public SaleService(URL url, Credentials credentials, Transport transport) {
43 this.url = url;
44 this.credentials = credentials;
45 this.transport = transport;
46 }
47
48 @Deprecated
49 public String create(int sellerVersion, SaleInfo saleInfo) {
50 return IdentifierInfo.fromJsonToIdentifierInfo(requestString(
51 format("%s/sales", saleInfo.getSellerIdentifier()),
52 HttpMethod.POST,
53 url,
54 credentials.toAuthentication(),
55 HttpHelper.integerToETag(sellerVersion),
56 null,
57 new StringConnectionWriter(saleInfo.toJson()),
58 CONTENT_TYPE_APPLICATION_JSON,
59 transport
60 ).getContent()).getIdentifier();
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public void put(int sellerVersion, SaleInfo saleInfo) {
77 requestVoid(
78 format("%s/sales/%s", saleInfo.getSellerIdentifier(), saleInfo.getIdentifier()),
79 HttpMethod.PUT,
80 url,
81 credentials.toAuthentication(),
82 HttpHelper.integerToETag(sellerVersion),
83 null,
84 new StringConnectionWriter(saleInfo.toJson()),
85 CONTENT_TYPE_APPLICATION_JSON,
86 transport
87 );
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 public List<SaleShortInfo> list(String sellerId, String period, Integer firstResult,
104 Integer maxResults, String like,
105 SaleInfo.Fields orderBy,
106 Boolean orderDesc, String cashRegisterName) {
107
108 Map<String, String> params = new HashMap<String, String>();
109
110 if (orderBy != null) {
111 params.put("orderBy", orderBy.name());
112 }
113 if (orderDesc != null) {
114 params.put("orderDesc", orderDesc.toString());
115 }
116 if (firstResult != null) {
117 params.put("firstResult", Integer.toString(firstResult));
118 }
119 if (maxResults != null) {
120 params.put("maxResults", Integer.toString(maxResults));
121 }
122 if (like != null) {
123 params.put("like", like);
124 }
125 if (period != null) {
126 params.put("period", period);
127 }
128 if (cashRegisterName != null) {
129 params.put("cashRegisterName", cashRegisterName);
130 }
131
132 StringResponse response = requestString(
133 format("%s/sales-minified", sellerId),
134 HttpMethod.GET,
135 url,
136 credentials.toAuthentication(),
137 null,
138 params,
139 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
140 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
141 transport
142 );
143 return response == null ? null : (List<SaleShortInfo>) SaleShortInfo.fromJsonArrayToSaleShortInfoes(response.getContent());
144 }
145
146 public List<SaleShortInfo> list(String sellerId, String cashRegisterName) {
147 return list(sellerId, null, null, null, null, null, null, cashRegisterName);
148 }
149
150
151
152
153
154
155
156
157
158
159
160 public SaleStatsInfo stats(String sellerId, String period, String cashRegisterName) {
161 Map<String, String> params = new HashMap<String, String>();
162 if (cashRegisterName != null) {
163 params.put("cashRegisterName", cashRegisterName);
164 }
165 if (period != null) {
166 params.put("period", period);
167 }
168
169 StringResponse response = requestString(
170 format("%s/sales/stats", sellerId),
171 HttpMethod.GET,
172 url,
173 credentials.toAuthentication(),
174 null,
175 params,
176 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
177 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
178 transport
179 );
180 return response == null ? null : SaleStatsInfo.fromJsonToSaleStatsInfo(response.getContent());
181 }
182
183
184
185
186
187
188
189
190 public List<SaleInfo> listDiff(String cashRegisterName, int versionFrom, int versionTo) {
191 StringResponse response = requestString(
192 String.format("%s/registrations/%s/sales-diff/%s/%s", credentials.getSellerId(), cashRegisterName, versionFrom, versionTo),
193 HttpMethod.GET,
194 url,
195 credentials.toAuthentication(),
196 null,
197 null,
198 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
199 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
200 transport
201 );
202 return (List<SaleInfo>) SaleInfo.fromJsonArrayToSaleInfoes(response.getContent());
203 }
204
205 public PaymentTypesStatsInfo statsByPaymentType(String sellerId, String period, String cashRegisterName) {
206 Map<String, String> params = new HashMap<String, String>();
207 if (cashRegisterName != null) {
208 params.put("cashRegisterName", cashRegisterName);
209 }
210 if (period != null) {
211 params.put("period", period);
212 }
213
214 StringResponse response = requestString(
215 format("%s/sales/stats/payment-type", sellerId),
216 HttpMethod.GET,
217 url,
218 credentials.toAuthentication(),
219 null,
220 params,
221 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
222 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
223 transport
224 );
225 return response == null ? null : PaymentTypesStatsInfo.fromJsonToPaymentTypesStatsInfo(response.getContent());
226 }
227
228 public InputStream csvStats(String sellerId, String period, String cashRegisterName) {
229 HashMap<String, String> params = new HashMap<String, String>();
230 if (period != null) {
231 params.put("period", period);
232 }
233 if (cashRegisterName != null) {
234 params.put("cashRegisterName", cashRegisterName);
235 }
236 final StreamResponse streamResponse = requestStream(
237 format("%s/sales/stats/csv", sellerId),
238 HttpMethod.GET,
239 url,
240 credentials.toAuthentication(),
241 null,
242 params,
243 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
244 HttpHelper.CONTENT_TYPE_CSV,
245 transport
246 );
247 return streamResponse != null ? streamResponse.getContent() : null;
248 }
249 }