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.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   * Service for reaching Sale entity.
25   * <p>User: Pavel Abizyaev, Date: 11.09.2014 18:52</p>
26   */
27  public class SaleService {
28      private URL url;
29      private Credentials credentials;
30      private Transport transport;
31  
32      /**
33       * Constructs the service.
34       *
35       * @param url         of the server
36       * @param credentials to work with the server
37       * @param transport   to know the method to reach the server
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       * Create sale on the server.
47       *
48       * @param sellerVersion expected latest version of seller
49       * @param saleInfo      data to put
50       * @return generated unique identifier
51       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException     if version mismatch
52       * @throws com.srv4pos.server.api.exceptions.UnprocessableEntityJsonException see:
53       *                                                                            {@link UnprocessableEntityErrorType#DAY_IS_CLOSED},
54       *                                                                            {@link UnprocessableEntityErrorType#DAY_IS_NOT_CLOSED},
55       *                                                                            {@link UnprocessableEntityErrorType#RECEIPT_IS_PRINTED_ALREADY},
56       *                                                                            {@link UnprocessableEntityErrorType#REPORT_IS_NOT_PRINTED}
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       * Creates or modifies sale on the server.
74       *
75       * @param sellerVersion expected latest version of seller
76       * @param saleInfo      data to put
77       * @return generated unique identifier
78       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException     if version mismatch
79       * @throws com.srv4pos.server.api.exceptions.UnprocessableEntityJsonException see:
80       *                                                                            {@link UnprocessableEntityErrorType#DAY_IS_CLOSED},
81       *                                                                            {@link UnprocessableEntityErrorType#DAY_IS_NOT_CLOSED},
82       *                                                                            {@link UnprocessableEntityErrorType#RECEIPT_IS_PRINTED_ALREADY},
83       *                                                                            {@link UnprocessableEntityErrorType#REPORT_IS_NOT_PRINTED}
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      * Returns list of entities.
101      *
102      * @param sellerId         filters by seller
103      * @param period           filters by date period
104      * @param firstResult      when pagination starts or null
105      * @param maxResults       amount of items per page or null
106      * @param like             search string filter or null
107      * @param orderBy          field to order by, possible values are "IDENTIFIER", "NAME", "VAT"
108      * @param orderDesc        field to direction of ordering
109      * @param cashRegisterName name of cash register
110      * @return list of entities which exists on current seller or null if list of entities is not modified since version
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      * Returns sales stats filtered by seller, cash register name and period.
157      *
158      * @param sellerId         filters by seller
159      * @param period           filters by date period
160      * @param cashRegisterName filters by cash register name
161      * @return {@link com.srv4pos.server.api.sale.SaleStatsInfo} with stats.
162      * @throws com.srv4pos.server.api.exceptions.TypeMismatchJsonException if {@code period} {@link java.lang.String} isn't convertible to
163      *                                                                     {@link java.util.Date}.
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      * Returns list of item changes between two versions.
190      *
191      * @param versionFrom version from included
192      * @param versionTo   version to included
193      * @return list of items to be added
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 }