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