View Javadoc
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   * Service for reaching Sale entity.
28   * <p>User: Pavel Abizyaev, Date: 11.09.2014 18:52</p>
29   */
30  public class SaleService {
31      private URL url;
32      private Credentials credentials;
33      private Transport transport;
34  
35      /**
36       * Constructs the service.
37       *
38       * @param url         of the server
39       * @param credentials to work with the server
40       * @param transport   to know the method to reach the server
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       * Creates or modifies sale on the server.
65       *
66       * @param sellerVersion expected latest version of seller
67       * @param saleInfo      data to put
68       * @return generated unique identifier
69       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException     if version mismatch
70       * @throws com.srv4pos.server.api.exceptions.UnprocessableEntityJsonException see:
71       *                                                                            {@link UnprocessableEntityErrorType#DAY_IS_CLOSED},
72       *                                                                            {@link UnprocessableEntityErrorType#DAY_IS_NOT_CLOSED},
73       *                                                                            {@link UnprocessableEntityErrorType#RECEIPT_IS_PRINTED_ALREADY},
74       *                                                                            {@link UnprocessableEntityErrorType#REPORT_IS_NOT_PRINTED}
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       * Returns list of entities.
92       *
93       * @param sellerId         filters by seller
94       * @param period           filters by date period
95       * @param firstResult      when pagination starts or null
96       * @param maxResults       amount of items per page or null
97       * @param like             search string filter or null
98       * @param orderBy          field to order by, possible values are "IDENTIFIER", "NAME", "VAT"
99       * @param orderDesc        field to direction of ordering
100      * @param cashRegisterName name of cash register
101      * @return list of entities which exists on current seller or null if list of entities is not modified since version
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      * Returns sales stats filtered by seller, cash register name and period.
152      *
153      * @param sellerId         filters by seller
154      * @param period           filters by date period
155      * @param cashRegisterName filters by cash register name
156      * @return {@link com.srv4pos.server.api.sale.SaleStatsInfo} with stats.
157      * @throws com.srv4pos.server.api.exceptions.TypeMismatchJsonException if {@code period} {@link java.lang.String} isn't convertible to
158      *                                                                     {@link java.util.Date}.
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      * Returns list of item changes between two versions.
185      *
186      * @param versionFrom version from included
187      * @param versionTo   version to included
188      * @return list of items to be added
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 }