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