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