| 21612 |
ashik.ali |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
| 21689 |
ashik.ali |
3 |
import java.io.ByteArrayInputStream;
|
|
|
4 |
import java.io.ByteArrayOutputStream;
|
|
|
5 |
import java.io.InputStream;
|
| 21680 |
ashik.ali |
6 |
import java.util.ArrayList;
|
|
|
7 |
import java.util.HashMap;
|
| 21612 |
ashik.ali |
8 |
import java.util.HashSet;
|
| 21680 |
ashik.ali |
9 |
import java.util.List;
|
|
|
10 |
import java.util.Map;
|
| 21612 |
ashik.ali |
11 |
import java.util.Set;
|
|
|
12 |
|
|
|
13 |
import javax.servlet.http.HttpServletRequest;
|
| 21689 |
ashik.ali |
14 |
import javax.servlet.http.HttpServletResponse;
|
| 21680 |
ashik.ali |
15 |
import javax.transaction.Transactional;
|
| 21612 |
ashik.ali |
16 |
|
| 21701 |
ashik.ali |
17 |
import org.apache.commons.lang.WordUtils;
|
| 21612 |
ashik.ali |
18 |
import org.slf4j.Logger;
|
|
|
19 |
import org.slf4j.LoggerFactory;
|
|
|
20 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 21689 |
ashik.ali |
21 |
import org.springframework.core.io.InputStreamResource;
|
|
|
22 |
import org.springframework.http.HttpHeaders;
|
|
|
23 |
import org.springframework.http.HttpStatus;
|
|
|
24 |
import org.springframework.http.MediaType;
|
|
|
25 |
import org.springframework.http.ResponseEntity;
|
| 21612 |
ashik.ali |
26 |
import org.springframework.stereotype.Controller;
|
|
|
27 |
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
28 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
29 |
import org.springframework.web.bind.annotation.RequestMethod;
|
| 21689 |
ashik.ali |
30 |
import org.springframework.web.bind.annotation.RequestParam;
|
| 21612 |
ashik.ali |
31 |
|
|
|
32 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 21680 |
ashik.ali |
33 |
import com.spice.profitmandi.common.model.CustomAddress;
|
| 21689 |
ashik.ali |
34 |
import com.spice.profitmandi.common.model.CustomCustomer;
|
|
|
35 |
import com.spice.profitmandi.common.model.CustomFofoLineItem;
|
|
|
36 |
import com.spice.profitmandi.common.model.CustomFofoOrderItem;
|
|
|
37 |
import com.spice.profitmandi.common.model.CustomRetailer;
|
|
|
38 |
import com.spice.profitmandi.common.model.PdfModel;
|
| 21612 |
ashik.ali |
39 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 21689 |
ashik.ali |
40 |
import com.spice.profitmandi.common.util.PdfUtils;
|
| 21680 |
ashik.ali |
41 |
import com.spice.profitmandi.common.util.StringUtils;
|
|
|
42 |
import com.spice.profitmandi.dao.entity.Address;
|
|
|
43 |
import com.spice.profitmandi.dao.entity.CurrentInventorySnapshot;
|
|
|
44 |
import com.spice.profitmandi.dao.entity.Customer;
|
|
|
45 |
import com.spice.profitmandi.dao.entity.FofoLineItem;
|
|
|
46 |
import com.spice.profitmandi.dao.entity.FofoLineItemSerialNumber;
|
|
|
47 |
import com.spice.profitmandi.dao.entity.FofoOrder;
|
|
|
48 |
import com.spice.profitmandi.dao.entity.InventoryItem;
|
|
|
49 |
import com.spice.profitmandi.dao.entity.InvoiceNumberGenerationSequence;
|
|
|
50 |
import com.spice.profitmandi.dao.entity.Item;
|
|
|
51 |
import com.spice.profitmandi.dao.entity.LineItem;
|
|
|
52 |
import com.spice.profitmandi.dao.entity.Order;
|
|
|
53 |
import com.spice.profitmandi.dao.entity.PaymentOption;
|
| 21689 |
ashik.ali |
54 |
import com.spice.profitmandi.dao.entity.Retailer;
|
| 21680 |
ashik.ali |
55 |
import com.spice.profitmandi.dao.entity.ScanRecord;
|
| 21689 |
ashik.ali |
56 |
import com.spice.profitmandi.dao.entity.User;
|
| 21680 |
ashik.ali |
57 |
import com.spice.profitmandi.dao.enumuration.ScanType;
|
|
|
58 |
import com.spice.profitmandi.dao.repository.AddressRepository;
|
|
|
59 |
import com.spice.profitmandi.dao.repository.CurrentInventorySnapshotRepository;
|
|
|
60 |
import com.spice.profitmandi.dao.repository.CustomerRepository;
|
|
|
61 |
import com.spice.profitmandi.dao.repository.FofoLineItemRepository;
|
|
|
62 |
import com.spice.profitmandi.dao.repository.FofoLineItemSerialNumberRepository;
|
|
|
63 |
import com.spice.profitmandi.dao.repository.FofoOrderRepository;
|
| 21612 |
ashik.ali |
64 |
import com.spice.profitmandi.dao.repository.InventoryItemRepository;
|
| 21680 |
ashik.ali |
65 |
import com.spice.profitmandi.dao.repository.InvoiceNumberGenerationSequenceRepository;
|
| 21612 |
ashik.ali |
66 |
import com.spice.profitmandi.dao.repository.OrderRepository;
|
| 21680 |
ashik.ali |
67 |
import com.spice.profitmandi.dao.repository.PaymentOptionRepository;
|
| 21689 |
ashik.ali |
68 |
import com.spice.profitmandi.dao.repository.RetailerRegisteredAddressRepository;
|
|
|
69 |
import com.spice.profitmandi.dao.repository.RetailerRepository;
|
| 21680 |
ashik.ali |
70 |
import com.spice.profitmandi.dao.repository.ScanRecordRepository;
|
| 21689 |
ashik.ali |
71 |
import com.spice.profitmandi.dao.repository.UserAccountRepository;
|
|
|
72 |
import com.spice.profitmandi.dao.repository.UserRepository;
|
| 21612 |
ashik.ali |
73 |
import com.spice.profitmandi.web.model.FofoDetails;
|
|
|
74 |
import com.spice.profitmandi.web.request.CreateOrderRequest;
|
|
|
75 |
import com.spice.profitmandi.web.request.CustomPaymentOption;
|
| 21680 |
ashik.ali |
76 |
import com.spice.profitmandi.web.response.ItemIdQuantityAvailability;
|
|
|
77 |
import com.spice.profitmandi.web.response.Quantity;
|
| 21612 |
ashik.ali |
78 |
import com.spice.profitmandi.web.util.CookiesFetcher;
|
|
|
79 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
|
|
80 |
|
|
|
81 |
@Controller
|
| 21680 |
ashik.ali |
82 |
@Transactional
|
| 21612 |
ashik.ali |
83 |
public class OrderController {
|
|
|
84 |
|
|
|
85 |
private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);
|
|
|
86 |
|
|
|
87 |
@Autowired
|
|
|
88 |
OrderRepository orderRepository;
|
|
|
89 |
|
|
|
90 |
@Autowired
|
|
|
91 |
InventoryItemRepository inventoryItemRepository;
|
|
|
92 |
|
|
|
93 |
@Autowired
|
| 21680 |
ashik.ali |
94 |
CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
|
|
|
95 |
|
|
|
96 |
@Autowired
|
|
|
97 |
InvoiceNumberGenerationSequenceRepository invoiceNumberGenerationSequenceRepository;
|
|
|
98 |
|
|
|
99 |
@Autowired
|
|
|
100 |
CustomerRepository customerRepository;
|
|
|
101 |
|
|
|
102 |
@Autowired
|
|
|
103 |
AddressRepository addressRepository;
|
|
|
104 |
|
|
|
105 |
@Autowired
|
|
|
106 |
FofoLineItemSerialNumberRepository fofoLineItemSerialNumberRepository;
|
|
|
107 |
|
|
|
108 |
@Autowired
|
|
|
109 |
FofoLineItemRepository fofoLineItemRepository;
|
|
|
110 |
|
|
|
111 |
@Autowired
|
|
|
112 |
PaymentOptionRepository paymentOptionRepository;
|
|
|
113 |
|
|
|
114 |
@Autowired
|
|
|
115 |
ScanRecordRepository scanRecordRepository;
|
|
|
116 |
|
|
|
117 |
@Autowired
|
|
|
118 |
FofoOrderRepository fofoOrderRepository;
|
|
|
119 |
|
|
|
120 |
@Autowired
|
| 21689 |
ashik.ali |
121 |
RetailerRepository retailerRepository;
|
|
|
122 |
|
|
|
123 |
@Autowired
|
|
|
124 |
UserRepository userRepository;
|
|
|
125 |
|
|
|
126 |
@Autowired
|
|
|
127 |
UserAccountRepository userAccountRepository;
|
|
|
128 |
|
|
|
129 |
@Autowired
|
|
|
130 |
RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
|
|
|
131 |
|
|
|
132 |
@Autowired
|
| 21612 |
ashik.ali |
133 |
MVCResponseSender mvcResponseSender;
|
|
|
134 |
|
|
|
135 |
@Autowired
|
|
|
136 |
CookiesFetcher cookiesFetcher;
|
|
|
137 |
|
|
|
138 |
@RequestMapping(value = "/order", method = RequestMethod.POST)
|
| 21680 |
ashik.ali |
139 |
public String createOrder(HttpServletRequest request, @RequestBody CreateOrderRequest createOrderRequest) throws Throwable{
|
|
|
140 |
//FofoDetails fofoDetails = cookiesFetcher.getCookiesObject(request);
|
|
|
141 |
LOGGER.info("/order request {}", createOrderRequest);
|
|
|
142 |
FofoDetails fofoDetails = new FofoDetails();
|
|
|
143 |
fofoDetails.setFofoId(963490);
|
|
|
144 |
Set<CustomFofoLineItem> fofoLineItems = createOrderRequest.getFofoLineItems();
|
|
|
145 |
Set<Integer> itemIds = new HashSet<>();
|
|
|
146 |
Set<Integer> nonSerializedItemIds = new HashSet<>();
|
|
|
147 |
Map<Integer, Integer> itemIdQuantity = new HashMap<>();
|
|
|
148 |
for(CustomFofoLineItem fofoLineItem : fofoLineItems){
|
|
|
149 |
itemIds.add(fofoLineItem.getItemId());
|
|
|
150 |
if(fofoLineItem.getSerialNumbers().size() > 0 && fofoLineItem.getQuantity() != fofoLineItem.getSerialNumbers().size()){
|
|
|
151 |
itemIdQuantity.put(fofoLineItem.getItemId(), fofoLineItem.getQuantity());
|
|
|
152 |
}
|
|
|
153 |
if(fofoLineItem.getSerialNumbers().isEmpty()){
|
|
|
154 |
nonSerializedItemIds.add(fofoLineItem.getItemId());
|
|
|
155 |
}
|
|
|
156 |
}
|
|
|
157 |
if(!itemIdQuantity.isEmpty()){
|
|
|
158 |
// if item quantity does not match with given serialnumbers size
|
|
|
159 |
LOGGER.error("itemId's quantity should be equal to given serialnumber size {} ", itemIdQuantity);
|
|
|
160 |
return "error";
|
|
|
161 |
}
|
|
|
162 |
float totalAmount = 0;
|
|
|
163 |
Map<Integer, Float> lineItemPrice = new HashMap<>();
|
|
|
164 |
List<CurrentInventorySnapshot> currentInventorySnapshots = currentInventorySnapshotRepository.selectByFofoItemIds(fofoDetails.getFofoId(), itemIds);
|
|
|
165 |
List<ItemIdQuantityAvailability> itemIdQuantityAvailabilities = new ArrayList<>();
|
|
|
166 |
for(CurrentInventorySnapshot currentInventorySnapshot : currentInventorySnapshots){
|
|
|
167 |
for(CustomFofoLineItem fofoLineItem : fofoLineItems){
|
|
|
168 |
if(currentInventorySnapshot.getId().getItemId() == fofoLineItem.getItemId()){
|
|
|
169 |
if(fofoLineItem.getQuantity() > currentInventorySnapshot.getAvailability()){
|
|
|
170 |
ItemIdQuantityAvailability itemIdQuantityAvailability = new ItemIdQuantityAvailability();
|
|
|
171 |
itemIdQuantityAvailability.setItemId(fofoLineItem.getItemId());
|
|
|
172 |
Quantity quantity = new Quantity();
|
|
|
173 |
quantity.setAvailable(currentInventorySnapshot.getAvailability());
|
|
|
174 |
quantity.setRequested(fofoLineItem.getQuantity());
|
|
|
175 |
itemIdQuantityAvailability.setQuantity(quantity);
|
|
|
176 |
itemIdQuantityAvailabilities.add(itemIdQuantityAvailability);
|
|
|
177 |
}
|
|
|
178 |
}
|
|
|
179 |
if(!(fofoLineItem.getSellingPrice() > 0)){
|
|
|
180 |
lineItemPrice.put(fofoLineItem.getItemId(), fofoLineItem.getSellingPrice());
|
|
|
181 |
}else{
|
| 21689 |
ashik.ali |
182 |
totalAmount = totalAmount + fofoLineItem.getSellingPrice() * fofoLineItem.getQuantity();
|
| 21680 |
ashik.ali |
183 |
}
|
|
|
184 |
}
|
|
|
185 |
}
|
| 21612 |
ashik.ali |
186 |
|
| 21680 |
ashik.ali |
187 |
try{
|
|
|
188 |
this.validatePaymentOptionsAndTotalAmount(createOrderRequest.getPaymentOptions(), totalAmount);
|
|
|
189 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){
|
|
|
190 |
LOGGER.error("Error occured while validating payment options : ", profitMandiBusinessException);
|
|
|
191 |
return "error";
|
|
|
192 |
}
|
| 21689 |
ashik.ali |
193 |
|
| 21680 |
ashik.ali |
194 |
if(!itemIdQuantityAvailabilities.isEmpty()){
|
|
|
195 |
// itemIdQuantity request is not valid
|
|
|
196 |
LOGGER.error("Requested quantities should not be greater than currently available quantities {}", itemIdQuantityAvailabilities);
|
|
|
197 |
return "error";
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
if(!lineItemPrice.isEmpty()){
|
|
|
201 |
// given fofo line item price must be greater than zero
|
|
|
202 |
LOGGER.error("requested itemId's selling price must greater thant 0");
|
|
|
203 |
return "error";
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
Set<Integer> invalidItemIdSerialNumbers = new HashSet<>();
|
|
|
207 |
Map<Integer, Set<String>> itemIdSerialNumbers = new HashMap<>();
|
|
|
208 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectByFofoIdItemIds(fofoDetails.getFofoId(), itemIds);
|
|
|
209 |
if(inventoryItems.isEmpty()){
|
|
|
210 |
// invalid item ids
|
|
|
211 |
LOGGER.error("Requested itemId's are not available in inventoryItem");
|
|
|
212 |
return "error";
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
for(InventoryItem inventoryItem : inventoryItems){
|
|
|
216 |
for(CustomFofoLineItem fofoLineItem : fofoLineItems){
|
|
|
217 |
if(inventoryItem.getItemId() == fofoLineItem.getItemId()){
|
|
|
218 |
if(fofoLineItem.getSerialNumbers().isEmpty()){
|
|
|
219 |
if(!(inventoryItem.getSerialNumber() == null && inventoryItem.getSerialNumber().equals(""))){
|
|
|
220 |
invalidItemIdSerialNumbers.add(inventoryItem.getItemId());
|
|
|
221 |
}
|
|
|
222 |
}
|
|
|
223 |
if(!fofoLineItem.getSerialNumbers().isEmpty()){
|
|
|
224 |
if(!fofoLineItem.getSerialNumbers().contains(inventoryItem.getSerialNumber())){
|
|
|
225 |
if(!itemIdSerialNumbers.containsKey(fofoLineItem.getItemId())){
|
|
|
226 |
Set<String> serialNumbers = new HashSet<>();
|
|
|
227 |
serialNumbers.add(inventoryItem.getSerialNumber());
|
|
|
228 |
itemIdSerialNumbers.put(fofoLineItem.getItemId(), serialNumbers);
|
|
|
229 |
}else{
|
|
|
230 |
itemIdSerialNumbers.get(fofoLineItem.getItemId()).add(inventoryItem.getSerialNumber());
|
|
|
231 |
}
|
|
|
232 |
}
|
|
|
233 |
}
|
|
|
234 |
}
|
|
|
235 |
}
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
if(!invalidItemIdSerialNumbers.isEmpty()){
|
|
|
239 |
LOGGER.error("Invalid itemId's serialNumbers {}", invalidItemIdSerialNumbers);
|
|
|
240 |
// itemId's are serialized you are saying these are not serialized
|
|
|
241 |
return "error";
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
if(!itemIdSerialNumbers.isEmpty()){
|
|
|
245 |
LOGGER.error("Invalid itemId's serialNumbers {}", itemIdSerialNumbers);
|
|
|
246 |
// itemId's are non serialized you are saying these are serialized
|
|
|
247 |
return "error";
|
|
|
248 |
}
|
|
|
249 |
InvoiceNumberGenerationSequence invoiceNumberGenerationSequence = null;
|
|
|
250 |
try{
|
|
|
251 |
invoiceNumberGenerationSequence = invoiceNumberGenerationSequenceRepository.selectByFofoId(fofoDetails.getFofoId());
|
|
|
252 |
invoiceNumberGenerationSequence.setSequence(invoiceNumberGenerationSequence.getSequence() + 1);
|
|
|
253 |
invoiceNumberGenerationSequenceRepository.persist(invoiceNumberGenerationSequence);
|
|
|
254 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){
|
|
|
255 |
invoiceNumberGenerationSequence = new InvoiceNumberGenerationSequence();
|
|
|
256 |
invoiceNumberGenerationSequence.setFofoId(fofoDetails.getFofoId());
|
|
|
257 |
invoiceNumberGenerationSequence.setPrefix("INVOICE");
|
|
|
258 |
invoiceNumberGenerationSequenceRepository.persist(invoiceNumberGenerationSequence);
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
CustomCustomer customCustomer = createOrderRequest.getCustomer();
|
|
|
262 |
|
|
|
263 |
if(!StringUtils.isValidEmailAddress(customCustomer.getEmailId())){
|
|
|
264 |
LOGGER.error("invalid customer emailId {} ", customCustomer.getEmailId());
|
|
|
265 |
return "error";
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
if(!StringUtils.isValidMobile(customCustomer.getMobileNumber())){
|
|
|
269 |
LOGGER.error("invalid customer mobileNumber {} ", customCustomer.getMobileNumber());
|
|
|
270 |
return "error";
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
Address address = this.createAddress(customCustomer.getAddress());
|
|
|
274 |
addressRepository.persist(address);
|
|
|
275 |
|
|
|
276 |
|
|
|
277 |
Customer customer = null;
|
|
|
278 |
try{
|
|
|
279 |
customer = customerRepository.selectByEmailIdAndMobileNumber(customCustomer.getEmailId(), customCustomer.getMobileNumber());
|
|
|
280 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){
|
|
|
281 |
LOGGER.error("Error : ", profitMandiBusinessException);
|
|
|
282 |
customer = new Customer();
|
| 21689 |
ashik.ali |
283 |
customCustomer.setName(customCustomer.getName());
|
| 21680 |
ashik.ali |
284 |
customer.setEmailId(customCustomer.getEmailId());
|
|
|
285 |
customer.setMobileNumber(customCustomer.getMobileNumber());
|
|
|
286 |
customer.setAddressId(address.getId());
|
|
|
287 |
customerRepository.persist(customer);
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
|
|
|
291 |
/*InvoiceNumberGenerationSequence invoiceNumberGenerationSequence = */
|
|
|
292 |
|
|
|
293 |
|
|
|
294 |
FofoOrder fofoOrder = new FofoOrder();
|
|
|
295 |
fofoOrder.setCustomerId(customer.getId());
|
|
|
296 |
fofoOrder.setFofoId(fofoDetails.getFofoId());
|
|
|
297 |
fofoOrder.setInvoiceNumber(invoiceNumberGenerationSequence.getPrefix() + invoiceNumberGenerationSequence.getSequence());
|
|
|
298 |
fofoOrder.setTotalAmount(totalAmount);
|
|
|
299 |
fofoOrderRepository.persist(fofoOrder);
|
|
|
300 |
|
|
|
301 |
for(CustomPaymentOption customPaymentOption : createOrderRequest.getPaymentOptions()){
|
|
|
302 |
PaymentOption paymentOption = new PaymentOption();
|
|
|
303 |
paymentOption.setOrderId(fofoOrder.getId());
|
|
|
304 |
paymentOption.setAmount(customPaymentOption.getAmount());
|
|
|
305 |
paymentOption.setType(customPaymentOption.getType());
|
|
|
306 |
paymentOptionRepository.persist(paymentOption);
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
List<Order> orders = orderRepository.selectByRetailerIdItemIds(fofoDetails.getFofoId(), itemIds);
|
|
|
310 |
|
|
|
311 |
Set<Item> items = new HashSet<>(itemIds.size());
|
|
|
312 |
for(Order order : orders){
|
|
|
313 |
LineItem lineItem = order.getLineItem();
|
|
|
314 |
items.add(lineItem.getItem());
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
if(items.size() != itemIds.size()){
|
|
|
318 |
LOGGER.error("Requested ItemIds not found in catalog");
|
|
|
319 |
// invalid itemIds
|
|
|
320 |
return "error";
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
Map<Integer, Map<String, String>> itemIdDetails = this.toMap(items);
|
|
|
324 |
|
|
|
325 |
for(CustomFofoLineItem customFofoLineItem : createOrderRequest.getFofoLineItems()){
|
|
|
326 |
FofoLineItem fofoLineItem = new FofoLineItem();
|
|
|
327 |
fofoLineItem.setItemId(customFofoLineItem.getItemId());
|
|
|
328 |
fofoLineItem.setQuantity(customFofoLineItem.getQuantity());
|
| 21690 |
ashik.ali |
329 |
fofoLineItem.setSellingPrice(customFofoLineItem.getSellingPrice());
|
| 21680 |
ashik.ali |
330 |
fofoLineItem.setOrderId(fofoOrder.getId());
|
|
|
331 |
fofoLineItem.setDp(customFofoLineItem.getSellingPrice());
|
| 21689 |
ashik.ali |
332 |
fofoLineItem.setTax(0);
|
|
|
333 |
fofoLineItem.setTaxRate(0);
|
| 21680 |
ashik.ali |
334 |
Map<String, String> itemIdDetail = itemIdDetails.get(customFofoLineItem.getItemId());
|
|
|
335 |
fofoLineItem.setBrand(itemIdDetail.get(ProfitMandiConstants.BRAND));
|
|
|
336 |
fofoLineItem.setModelName(itemIdDetail.get(ProfitMandiConstants.MODEL_NAME));
|
|
|
337 |
fofoLineItem.setModelNumber(itemIdDetail.get(ProfitMandiConstants.MODEL_NUMBER));
|
|
|
338 |
fofoLineItem.setColor(itemIdDetail.get(ProfitMandiConstants.COLOR));
|
|
|
339 |
fofoLineItemRepository.persist(fofoLineItem);
|
|
|
340 |
LOGGER.info("\n\n");
|
|
|
341 |
if(!customFofoLineItem.getSerialNumbers().isEmpty()){
|
|
|
342 |
for(String serialNumber : customFofoLineItem.getSerialNumbers()){
|
|
|
343 |
FofoLineItemSerialNumber fofoLineItemSerialNumber = new FofoLineItemSerialNumber();
|
|
|
344 |
fofoLineItemSerialNumber.setFofoLineItemId(fofoLineItem.getId());
|
|
|
345 |
fofoLineItemSerialNumber.setSerialNumber(serialNumber);
|
|
|
346 |
fofoLineItemSerialNumberRepository.persist(fofoLineItemSerialNumber);
|
|
|
347 |
}
|
|
|
348 |
}
|
|
|
349 |
|
|
|
350 |
|
|
|
351 |
|
|
|
352 |
|
|
|
353 |
for(InventoryItem inventoryItem : inventoryItems){
|
|
|
354 |
if(inventoryItem.getItemId() == fofoLineItem.getItemId()){
|
|
|
355 |
if(inventoryItem.getSerialNumber() == null || inventoryItem.getSerialNumber().isEmpty()){
|
|
|
356 |
if(fofoLineItem.getQuantity() > 0)
|
|
|
357 |
if(fofoLineItem.getQuantity() <= inventoryItem.getGoodQuantity()){
|
|
|
358 |
inventoryItem.setGoodQuantity(inventoryItem.getGoodQuantity() - fofoLineItem.getQuantity());
|
|
|
359 |
inventoryItem.setLastScanType(ScanType.SALE);
|
|
|
360 |
inventoryItemRepository.persist(inventoryItem);
|
|
|
361 |
ScanRecord scanRecord = new ScanRecord();
|
|
|
362 |
scanRecord.setInventoryItemId(inventoryItem.getId());
|
|
|
363 |
scanRecord.setFofoId(fofoDetails.getFofoId());
|
|
|
364 |
scanRecord.setQuantity(fofoLineItem.getQuantity());
|
|
|
365 |
scanRecord.setType(ScanType.SALE);
|
|
|
366 |
scanRecordRepository.persist(scanRecord);
|
|
|
367 |
}else{
|
|
|
368 |
int availableGoodQuantity = inventoryItem.getGoodQuantity();
|
|
|
369 |
inventoryItem.setGoodQuantity(0);
|
|
|
370 |
fofoLineItem.setQuantity(fofoLineItem.getQuantity() - availableGoodQuantity);
|
|
|
371 |
inventoryItem.setLastScanType(ScanType.SALE);
|
|
|
372 |
inventoryItemRepository.persist(inventoryItem);
|
|
|
373 |
ScanRecord scanRecord = new ScanRecord();
|
|
|
374 |
scanRecord.setInventoryItemId(inventoryItem.getId());
|
|
|
375 |
scanRecord.setFofoId(fofoDetails.getFofoId());
|
|
|
376 |
scanRecord.setQuantity(availableGoodQuantity);
|
|
|
377 |
scanRecord.setType(ScanType.SALE);
|
|
|
378 |
scanRecordRepository.persist(scanRecord);
|
|
|
379 |
}
|
|
|
380 |
}else{
|
|
|
381 |
inventoryItem.setGoodQuantity(0);
|
|
|
382 |
inventoryItem.setLastScanType(ScanType.SALE);
|
|
|
383 |
inventoryItemRepository.persist(inventoryItem);
|
|
|
384 |
ScanRecord scanRecord = new ScanRecord();
|
|
|
385 |
scanRecord.setInventoryItemId(inventoryItem.getId());
|
|
|
386 |
scanRecord.setFofoId(fofoDetails.getFofoId());
|
|
|
387 |
scanRecord.setQuantity(1);
|
|
|
388 |
scanRecord.setType(ScanType.SALE);
|
|
|
389 |
scanRecordRepository.persist(scanRecord);
|
|
|
390 |
}
|
|
|
391 |
}
|
|
|
392 |
}
|
|
|
393 |
|
|
|
394 |
}
|
| 21612 |
ashik.ali |
395 |
return null;
|
|
|
396 |
}
|
|
|
397 |
|
| 21680 |
ashik.ali |
398 |
|
|
|
399 |
private Map<Integer, Map<String, String>> toMap(Set<Item> items){
|
|
|
400 |
Map<Integer, Map<String, String>> map = new HashMap<>();
|
|
|
401 |
for(Item item : items){
|
|
|
402 |
Map<String, String> detailMap = new HashMap<>();
|
|
|
403 |
detailMap.put(ProfitMandiConstants.BRAND, item.getBrand());
|
|
|
404 |
detailMap.put(ProfitMandiConstants.MODEL_NAME, item.getModelName());
|
|
|
405 |
detailMap.put(ProfitMandiConstants.MODEL_NUMBER, item.getModelNumber());
|
|
|
406 |
detailMap.put(ProfitMandiConstants.COLOR, item.getColor());
|
|
|
407 |
map.put(item.getId(), detailMap);
|
|
|
408 |
}
|
|
|
409 |
return map;
|
|
|
410 |
}
|
|
|
411 |
|
|
|
412 |
private Address createAddress(CustomAddress customAddress){
|
|
|
413 |
Address address = new Address();
|
|
|
414 |
address.setName(customAddress.getName());
|
|
|
415 |
address.setLine1(customAddress.getLine1());
|
|
|
416 |
address.setLine2(customAddress.getLine2());
|
|
|
417 |
address.setLandmark(customAddress.getLandmark());
|
|
|
418 |
address.setCity(customAddress.getCity());
|
|
|
419 |
address.setPinCode(customAddress.getPinCode());
|
|
|
420 |
address.setState(customAddress.getState());
|
|
|
421 |
address.setCountry(customAddress.getCountry());
|
|
|
422 |
address.setPhoneNumber(customAddress.getPhoneNumber());
|
|
|
423 |
address.setEnabled(true);
|
|
|
424 |
return address;
|
|
|
425 |
}
|
|
|
426 |
|
| 21689 |
ashik.ali |
427 |
private CustomAddress createCustomAddress(Address address){
|
|
|
428 |
CustomAddress customAddress = new CustomAddress();
|
|
|
429 |
customAddress.setName(address.getName());
|
|
|
430 |
customAddress.setLine1(address.getLine1());
|
|
|
431 |
customAddress.setLine2(address.getLine2());
|
|
|
432 |
customAddress.setLandmark(address.getLandmark());
|
|
|
433 |
customAddress.setCity(address.getCity());
|
|
|
434 |
customAddress.setPinCode(address.getPinCode());
|
|
|
435 |
customAddress.setState(address.getState());
|
|
|
436 |
customAddress.setCountry(address.getCountry());
|
|
|
437 |
customAddress.setPhoneNumber(address.getPhoneNumber());
|
|
|
438 |
return customAddress;
|
| 21680 |
ashik.ali |
439 |
}
|
|
|
440 |
|
|
|
441 |
private void validatePaymentOptionsAndTotalAmount(Set<CustomPaymentOption> customPaymentOptions, float totalAmount) throws ProfitMandiBusinessException
|
| 21612 |
ashik.ali |
442 |
{
|
|
|
443 |
float calculatedAmount = 0;
|
|
|
444 |
Set<String> paymentOptionTypes = new HashSet<>();
|
|
|
445 |
for(CustomPaymentOption customPaymentOption : customPaymentOptions){
|
| 21680 |
ashik.ali |
446 |
if(paymentOptionTypes.contains(customPaymentOption.getType().name())){
|
| 21612 |
ashik.ali |
447 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.PAYMENT_OPTION_TYPE, customPaymentOption.getType().name(), "");
|
| 21680 |
ashik.ali |
448 |
}else{
|
|
|
449 |
paymentOptionTypes.add(customPaymentOption.getType().name());
|
|
|
450 |
calculatedAmount = calculatedAmount + customPaymentOption.getAmount();
|
| 21612 |
ashik.ali |
451 |
}
|
|
|
452 |
}
|
|
|
453 |
if(calculatedAmount != totalAmount){
|
|
|
454 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.PAYMENT_OPTION_CALCULATED_AMOUNT, calculatedAmount, "");
|
|
|
455 |
}
|
|
|
456 |
}
|
|
|
457 |
|
| 21689 |
ashik.ali |
458 |
|
|
|
459 |
@RequestMapping(value = "/generateInvoice")
|
|
|
460 |
public ResponseEntity<?> generateInvoice(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId) throws Throwable{
|
|
|
461 |
LOGGER.info("Request received at url {} with params {} ", request.getRequestURI(), request.getParameterMap());
|
|
|
462 |
//FofoDetails fofoDetails = cookiesFetcher.getCookiesObject(request);
|
| 21701 |
ashik.ali |
463 |
|
| 21689 |
ashik.ali |
464 |
FofoDetails fofoDetails = new FofoDetails();
|
|
|
465 |
fofoDetails.setFofoId(963490);
|
|
|
466 |
FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndOrderId(fofoDetails.getFofoId(), orderId);
|
|
|
467 |
PdfModel pdfModel = new PdfModel();
|
|
|
468 |
pdfModel.setAuther("profitmandi");
|
|
|
469 |
pdfModel.setTitle("Retailer Invoice");
|
|
|
470 |
Customer customer = customerRepository.selectById(fofoOrder.getCustomerId());
|
|
|
471 |
CustomCustomer customCustomer = new CustomCustomer();
|
|
|
472 |
customCustomer.setName(customer.getName());
|
|
|
473 |
customCustomer.setEmailId(customer.getEmailId());
|
|
|
474 |
customCustomer.setMobileNumber(customer.getMobileNumber());
|
|
|
475 |
Address customerAddress = addressRepository.selectById(customer.getAddressId());
|
|
|
476 |
customCustomer.setAddress(this.createCustomAddress(customerAddress));
|
|
|
477 |
pdfModel.setCustomer(customCustomer);
|
|
|
478 |
pdfModel.setInvoiceNumber(fofoOrder.getInvoiceNumber());
|
|
|
479 |
pdfModel.setTotalAmount(fofoOrder.getTotalAmount());
|
|
|
480 |
|
|
|
481 |
Retailer retailer = retailerRepository.selectById(fofoDetails.getFofoId());
|
|
|
482 |
User user = userRepository.selectById(userAccountRepository.selectUserIdByRetailerId(retailer.getId()));
|
|
|
483 |
CustomRetailer customRetailer = new CustomRetailer();
|
|
|
484 |
customRetailer.setBusinessName(retailer.getName());
|
|
|
485 |
customRetailer.setMobileNumber(user.getMobileNumber());
|
|
|
486 |
customRetailer.setTinNumber(retailer.getNumber());
|
|
|
487 |
Address retailerAddress = addressRepository.selectById(retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailer.getId()));
|
|
|
488 |
customRetailer.setAddress(this.createCustomAddress(retailerAddress));
|
|
|
489 |
pdfModel.setRetailer(customRetailer);
|
|
|
490 |
List<FofoLineItem> fofoLineItems = fofoLineItemRepository.selectByOrderId(fofoOrder.getId());
|
|
|
491 |
|
|
|
492 |
Set<CustomFofoOrderItem> customerFofoOrderItems = new HashSet<>();
|
|
|
493 |
for(FofoLineItem fofoLineItem : fofoLineItems){
|
|
|
494 |
CustomFofoOrderItem customFofoOrderItem = new CustomFofoOrderItem();
|
|
|
495 |
customFofoOrderItem.setAmount(fofoLineItem.getQuantity() * fofoLineItem.getSellingPrice());
|
|
|
496 |
customFofoOrderItem.setDescription(fofoLineItem.getBrand() + " " + fofoLineItem.getModelName() + " " + fofoLineItem.getModelNumber() + " " + fofoLineItem.getColor());
|
|
|
497 |
float itemTotal = customFofoOrderItem.getAmount() - fofoLineItem.getTax();
|
|
|
498 |
customFofoOrderItem.setRate(fofoLineItem.getSellingPrice());
|
|
|
499 |
customFofoOrderItem.setQuantity(fofoLineItem.getQuantity());
|
|
|
500 |
customFofoOrderItem.setTax(fofoLineItem.getTax());
|
|
|
501 |
customFofoOrderItem.setTaxRate(fofoLineItem.getTaxRate());
|
|
|
502 |
customFofoOrderItem.setItemTotal(itemTotal);
|
|
|
503 |
customFofoOrderItem.setSerialNumbers(String.join(", ",this.toSerialNumbers(fofoLineItem.getFofoLineItemSerialNumbers())));
|
|
|
504 |
customerFofoOrderItems.add(customFofoOrderItem);
|
|
|
505 |
}
|
|
|
506 |
pdfModel.setOrderItems(customerFofoOrderItems);
|
|
|
507 |
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
508 |
PdfUtils.generateAndWrite(pdfModel, byteArrayOutputStream);
|
|
|
509 |
//final MediaType mediaType=MediaType.parseMediaType(profilePhotoModel.getContentType().getValue());
|
|
|
510 |
LOGGER.info("Pdf Stream length {}", byteArrayOutputStream.toByteArray().length);
|
|
|
511 |
final HttpHeaders headers=new HttpHeaders();
|
|
|
512 |
headers.setContentType(MediaType.APPLICATION_PDF);
|
|
|
513 |
//headers.set("Content-Disposition", "retailerInvoice.pdf");
|
|
|
514 |
headers.setContentLength(byteArrayOutputStream.toByteArray().length);
|
|
|
515 |
final InputStream inputStream=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
|
|
|
516 |
final InputStreamResource inputStreamResource=new InputStreamResource(inputStream);
|
|
|
517 |
return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
|
|
|
518 |
}
|
|
|
519 |
|
|
|
520 |
private Set<String> toSerialNumbers(Set<FofoLineItemSerialNumber> fofoLineItemSerialNumbers){
|
|
|
521 |
Set<String> serialNumbers = new HashSet<>(fofoLineItemSerialNumbers.size());
|
|
|
522 |
for(FofoLineItemSerialNumber fofoLineItemSerialNumber : fofoLineItemSerialNumbers){
|
|
|
523 |
serialNumbers.add(fofoLineItemSerialNumber.getSerialNumber());
|
|
|
524 |
}
|
|
|
525 |
return serialNumbers;
|
|
|
526 |
}
|
| 21612 |
ashik.ali |
527 |
}
|