| 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;
|
| 22189 |
amit.gupta |
6 |
import java.lang.ProcessBuilder.Redirect;
|
| 21680 |
ashik.ali |
7 |
import java.util.ArrayList;
|
| 21985 |
kshitij.so |
8 |
import java.util.Collection;
|
| 21680 |
ashik.ali |
9 |
import java.util.HashMap;
|
| 21612 |
ashik.ali |
10 |
import java.util.HashSet;
|
| 21985 |
kshitij.so |
11 |
import java.util.Iterator;
|
| 21680 |
ashik.ali |
12 |
import java.util.List;
|
|
|
13 |
import java.util.Map;
|
| 21612 |
ashik.ali |
14 |
import java.util.Set;
|
|
|
15 |
|
|
|
16 |
import javax.servlet.http.HttpServletRequest;
|
| 21689 |
ashik.ali |
17 |
import javax.servlet.http.HttpServletResponse;
|
| 21612 |
ashik.ali |
18 |
|
| 21985 |
kshitij.so |
19 |
import org.json.JSONObject;
|
| 21612 |
ashik.ali |
20 |
import org.slf4j.Logger;
|
|
|
21 |
import org.slf4j.LoggerFactory;
|
|
|
22 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 21689 |
ashik.ali |
23 |
import org.springframework.core.io.InputStreamResource;
|
|
|
24 |
import org.springframework.http.HttpHeaders;
|
|
|
25 |
import org.springframework.http.HttpStatus;
|
|
|
26 |
import org.springframework.http.MediaType;
|
|
|
27 |
import org.springframework.http.ResponseEntity;
|
| 21612 |
ashik.ali |
28 |
import org.springframework.stereotype.Controller;
|
| 22064 |
ashik.ali |
29 |
import org.springframework.transaction.annotation.Transactional;
|
| 21985 |
kshitij.so |
30 |
import org.springframework.ui.Model;
|
| 21612 |
ashik.ali |
31 |
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
32 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
33 |
import org.springframework.web.bind.annotation.RequestMethod;
|
| 21689 |
ashik.ali |
34 |
import org.springframework.web.bind.annotation.RequestParam;
|
| 21612 |
ashik.ali |
35 |
|
|
|
36 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 21985 |
kshitij.so |
37 |
import com.spice.profitmandi.common.model.CartFofo;
|
| 21680 |
ashik.ali |
38 |
import com.spice.profitmandi.common.model.CustomAddress;
|
| 21689 |
ashik.ali |
39 |
import com.spice.profitmandi.common.model.CustomCustomer;
|
|
|
40 |
import com.spice.profitmandi.common.model.CustomFofoLineItem;
|
|
|
41 |
import com.spice.profitmandi.common.model.CustomFofoOrderItem;
|
|
|
42 |
import com.spice.profitmandi.common.model.CustomRetailer;
|
| 21896 |
ashik.ali |
43 |
import com.spice.profitmandi.common.model.GstRate;
|
| 21689 |
ashik.ali |
44 |
import com.spice.profitmandi.common.model.PdfModel;
|
| 21612 |
ashik.ali |
45 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 21689 |
ashik.ali |
46 |
import com.spice.profitmandi.common.util.PdfUtils;
|
| 21680 |
ashik.ali |
47 |
import com.spice.profitmandi.common.util.StringUtils;
|
| 21896 |
ashik.ali |
48 |
import com.spice.profitmandi.common.util.Utils;
|
| 21728 |
ashik.ali |
49 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
|
|
50 |
import com.spice.profitmandi.dao.entity.dtr.Retailer;
|
|
|
51 |
import com.spice.profitmandi.dao.entity.dtr.User;
|
|
|
52 |
import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;
|
|
|
53 |
import com.spice.profitmandi.dao.entity.fofo.Customer;
|
|
|
54 |
import com.spice.profitmandi.dao.entity.fofo.CustomerAddress;
|
| 21896 |
ashik.ali |
55 |
import com.spice.profitmandi.dao.entity.fofo.FofoItemId;
|
| 21728 |
ashik.ali |
56 |
import com.spice.profitmandi.dao.entity.fofo.FofoLineItem;
|
|
|
57 |
import com.spice.profitmandi.dao.entity.fofo.FofoLineItemSerialNumber;
|
|
|
58 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
|
|
59 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
|
|
60 |
import com.spice.profitmandi.dao.entity.fofo.InvoiceNumberGenerationSequence;
|
|
|
61 |
import com.spice.profitmandi.dao.entity.fofo.PaymentOption;
|
|
|
62 |
import com.spice.profitmandi.dao.entity.fofo.ScanRecord;
|
|
|
63 |
import com.spice.profitmandi.dao.entity.user.Address;
|
|
|
64 |
import com.spice.profitmandi.dao.enumuration.fofo.ScanType;
|
| 21883 |
kshitij.so |
65 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
| 21728 |
ashik.ali |
66 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
|
|
|
67 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
|
|
68 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
|
|
69 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
|
|
70 |
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
|
|
|
71 |
import com.spice.profitmandi.dao.repository.fofo.CustomerAddressRepository;
|
|
|
72 |
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
|
|
|
73 |
import com.spice.profitmandi.dao.repository.fofo.FofoLineItemRepository;
|
|
|
74 |
import com.spice.profitmandi.dao.repository.fofo.FofoLineItemSerialNumberRepository;
|
|
|
75 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
|
|
|
76 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
|
|
77 |
import com.spice.profitmandi.dao.repository.fofo.InvoiceNumberGenerationSequenceRepository;
|
|
|
78 |
import com.spice.profitmandi.dao.repository.fofo.PaymentOptionRepository;
|
|
|
79 |
import com.spice.profitmandi.dao.repository.fofo.ScanRecordRepository;
|
|
|
80 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
|
|
81 |
import com.spice.profitmandi.dao.repository.user.AddressRepository;
|
| 22069 |
ashik.ali |
82 |
import com.spice.profitmandi.service.pricing.PricingService;
|
| 22139 |
amit.gupta |
83 |
import com.spice.profitmandi.web.model.LoginDetails;
|
| 21612 |
ashik.ali |
84 |
import com.spice.profitmandi.web.request.CreateOrderRequest;
|
|
|
85 |
import com.spice.profitmandi.web.request.CustomPaymentOption;
|
| 21680 |
ashik.ali |
86 |
import com.spice.profitmandi.web.response.ItemIdQuantityAvailability;
|
|
|
87 |
import com.spice.profitmandi.web.response.Quantity;
|
| 22069 |
ashik.ali |
88 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
| 21612 |
ashik.ali |
89 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
|
|
90 |
|
| 21985 |
kshitij.so |
91 |
import in.shop2020.model.v1.catalog.ItemType;
|
|
|
92 |
|
| 21612 |
ashik.ali |
93 |
@Controller
|
| 22037 |
amit.gupta |
94 |
@Transactional(rollbackFor=Throwable.class)
|
| 21612 |
ashik.ali |
95 |
public class OrderController {
|
|
|
96 |
|
|
|
97 |
private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);
|
| 21985 |
kshitij.so |
98 |
|
| 21612 |
ashik.ali |
99 |
@Autowired
|
|
|
100 |
OrderRepository orderRepository;
|
| 21985 |
kshitij.so |
101 |
|
| 21612 |
ashik.ali |
102 |
@Autowired
|
|
|
103 |
InventoryItemRepository inventoryItemRepository;
|
| 21985 |
kshitij.so |
104 |
|
| 21612 |
ashik.ali |
105 |
@Autowired
|
| 21680 |
ashik.ali |
106 |
CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
|
| 21985 |
kshitij.so |
107 |
|
| 21680 |
ashik.ali |
108 |
@Autowired
|
|
|
109 |
InvoiceNumberGenerationSequenceRepository invoiceNumberGenerationSequenceRepository;
|
| 21985 |
kshitij.so |
110 |
|
| 21680 |
ashik.ali |
111 |
@Autowired
|
|
|
112 |
CustomerRepository customerRepository;
|
| 21985 |
kshitij.so |
113 |
|
| 21680 |
ashik.ali |
114 |
@Autowired
|
|
|
115 |
AddressRepository addressRepository;
|
| 21985 |
kshitij.so |
116 |
|
| 21680 |
ashik.ali |
117 |
@Autowired
|
|
|
118 |
FofoLineItemSerialNumberRepository fofoLineItemSerialNumberRepository;
|
| 21985 |
kshitij.so |
119 |
|
| 21680 |
ashik.ali |
120 |
@Autowired
|
|
|
121 |
FofoLineItemRepository fofoLineItemRepository;
|
| 21985 |
kshitij.so |
122 |
|
| 21680 |
ashik.ali |
123 |
@Autowired
|
|
|
124 |
PaymentOptionRepository paymentOptionRepository;
|
| 21985 |
kshitij.so |
125 |
|
| 21680 |
ashik.ali |
126 |
@Autowired
|
|
|
127 |
ScanRecordRepository scanRecordRepository;
|
| 21985 |
kshitij.so |
128 |
|
| 21680 |
ashik.ali |
129 |
@Autowired
|
|
|
130 |
FofoOrderRepository fofoOrderRepository;
|
| 21985 |
kshitij.so |
131 |
|
| 21680 |
ashik.ali |
132 |
@Autowired
|
| 21689 |
ashik.ali |
133 |
RetailerRepository retailerRepository;
|
| 21985 |
kshitij.so |
134 |
|
| 21689 |
ashik.ali |
135 |
@Autowired
|
|
|
136 |
UserRepository userRepository;
|
| 21985 |
kshitij.so |
137 |
|
| 21689 |
ashik.ali |
138 |
@Autowired
|
|
|
139 |
UserAccountRepository userAccountRepository;
|
| 21985 |
kshitij.so |
140 |
|
| 21689 |
ashik.ali |
141 |
@Autowired
|
|
|
142 |
RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
|
| 21985 |
kshitij.so |
143 |
|
| 21689 |
ashik.ali |
144 |
@Autowired
|
| 21711 |
ashik.ali |
145 |
CustomerAddressRepository customerAddressRepository;
|
| 21985 |
kshitij.so |
146 |
|
| 21711 |
ashik.ali |
147 |
@Autowired
|
| 21896 |
ashik.ali |
148 |
ItemRepository itemRepository;
|
| 21985 |
kshitij.so |
149 |
|
| 21896 |
ashik.ali |
150 |
@Autowired
|
| 21612 |
ashik.ali |
151 |
MVCResponseSender mvcResponseSender;
|
| 21985 |
kshitij.so |
152 |
|
| 21612 |
ashik.ali |
153 |
@Autowired
|
| 22069 |
ashik.ali |
154 |
CookiesProcessor cookiesProcessor;
|
|
|
155 |
|
|
|
156 |
@Autowired
|
|
|
157 |
PricingService pricingService;
|
| 22189 |
amit.gupta |
158 |
|
|
|
159 |
|
|
|
160 |
|
| 21985 |
kshitij.so |
161 |
|
|
|
162 |
@RequestMapping(value = "/order")
|
|
|
163 |
public String orderIndex(HttpServletRequest request, @RequestParam(name = "cartData") String cartData, Model model) throws Exception{
|
|
|
164 |
try {
|
| 22069 |
ashik.ali |
165 |
cookiesProcessor.getCookiesObject(request);
|
| 21985 |
kshitij.so |
166 |
} catch (ProfitMandiBusinessException e) {
|
|
|
167 |
model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));
|
|
|
168 |
return "response";
|
|
|
169 |
}
|
|
|
170 |
JSONObject cartObject = new JSONObject(cartData);
|
|
|
171 |
Iterator<?> keys = cartObject.keys();
|
|
|
172 |
|
|
|
173 |
Set<Integer> itemIds = new HashSet<>();
|
|
|
174 |
List<CartFofo> cartItems = new ArrayList<CartFofo>();
|
|
|
175 |
|
|
|
176 |
while( keys.hasNext() ) {
|
|
|
177 |
String key = (String)keys.next();
|
|
|
178 |
if ( cartObject.get(key) instanceof JSONObject ) {
|
|
|
179 |
System.out.println(cartObject.get(key));
|
|
|
180 |
}
|
|
|
181 |
CartFofo cf = new CartFofo();
|
|
|
182 |
cf.setItemId(cartObject.getJSONObject(key).getInt("itemId"));
|
|
|
183 |
cf.setQuantity(cartObject.getJSONObject(key).getInt("quantity"));
|
|
|
184 |
if (cf.getQuantity() <= 0){
|
|
|
185 |
continue;
|
|
|
186 |
}
|
|
|
187 |
cartItems.add(cf);
|
|
|
188 |
itemIds.add(cartObject.getJSONObject(key).getInt("itemId"));
|
|
|
189 |
}
|
|
|
190 |
Map<Integer, Item> itemMap = new HashMap<Integer, Item>();
|
|
|
191 |
if (itemIds.size() > 0){
|
|
|
192 |
List<Item> items = itemRepository.selectByIds(itemIds);
|
|
|
193 |
for (Item i : items){
|
|
|
194 |
itemMap.put(i.getId(), i);
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
for (CartFofo cf : cartItems){
|
|
|
198 |
Item i = itemMap.get(cf.getItemId());
|
|
|
199 |
if (i == null){
|
|
|
200 |
continue;
|
|
|
201 |
}
|
|
|
202 |
cf.setDisplayName(getValidName(i.getBrand())+" "+getValidName(i.getModelName())+" "+getValidName(i.getModelNumber())+" "+getValidName(i.getColor()).replaceAll("\\s+", " "));
|
|
|
203 |
cf.setItemType(i.getType());
|
|
|
204 |
}
|
|
|
205 |
model.addAttribute("cartObj", cartItems);
|
|
|
206 |
return "order-index";
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
private String getValidName(String name){
|
|
|
210 |
return name!=null?name:"";
|
|
|
211 |
}
|
| 21612 |
ashik.ali |
212 |
|
| 21985 |
kshitij.so |
213 |
@RequestMapping(value = "/get-order", method = RequestMethod.GET)
|
|
|
214 |
public String getOrder(HttpServletRequest request, @RequestParam(name = "orderId") int orderId, Model model) throws ProfitMandiBusinessException, Exception{
|
| 22139 |
amit.gupta |
215 |
LoginDetails fofoDetails;
|
| 21985 |
kshitij.so |
216 |
try {
|
| 22069 |
ashik.ali |
217 |
fofoDetails = cookiesProcessor.getCookiesObject(request);
|
| 21985 |
kshitij.so |
218 |
} catch (ProfitMandiBusinessException e) {
|
|
|
219 |
model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));
|
|
|
220 |
return "response";
|
|
|
221 |
}
|
|
|
222 |
FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndOrderId(fofoDetails.getFofoId(), orderId);
|
| 22098 |
kshitij.so |
223 |
List<FofoLineItem> fofoLineItems = fofoLineItemRepository.selectByOrderId(fofoOrder.getId());
|
|
|
224 |
CustomerAddress customerAddress = customerAddressRepository.selectById(fofoOrder.getCustomerAddressId());
|
|
|
225 |
List<PaymentOption> paymentOptions = paymentOptionRepository.selectByOrderId(fofoOrder.getId());
|
|
|
226 |
model.addAttribute("fofoOrder", fofoOrder);
|
|
|
227 |
model.addAttribute("fofoLineItems", fofoLineItems);
|
|
|
228 |
model.addAttribute("customerBillingAddress", getBillingAddress(customerAddress));
|
|
|
229 |
model.addAttribute("customerBillingAddressObj", customerAddress);
|
|
|
230 |
model.addAttribute("paymentOptions", paymentOptions);
|
|
|
231 |
return "order-details";
|
| 21985 |
kshitij.so |
232 |
}
|
|
|
233 |
|
| 22098 |
kshitij.so |
234 |
private String getBillingAddress(CustomerAddress customerAddress) {
|
|
|
235 |
String address = "";
|
|
|
236 |
if ((customerAddress.getLine1() != null) && (!customerAddress.getLine1().isEmpty())) {
|
|
|
237 |
address = address + customerAddress.getLine1();
|
|
|
238 |
address = address + ", ";
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
if ((customerAddress.getLine2() != null) && (!customerAddress.getLine2().isEmpty())) {
|
|
|
242 |
address = address + customerAddress.getLine2();
|
|
|
243 |
address = address + ", ";
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
if ((customerAddress.getLandmark() != null) && (!customerAddress.getLandmark().isEmpty())) {
|
|
|
247 |
address = address + customerAddress.getLandmark();
|
|
|
248 |
address = address + ", ";
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
if ((customerAddress.getCity() != null) && (!customerAddress.getCity().isEmpty())) {
|
|
|
252 |
address = address + customerAddress.getCity();
|
|
|
253 |
address = address + ", ";
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
if ((customerAddress.getState() != null) && (!customerAddress.getState().isEmpty())) {
|
|
|
257 |
address = address + customerAddress.getState();
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
if ((customerAddress.getPinCode() != null) && (!customerAddress.getPinCode().isEmpty())) {
|
|
|
261 |
address = address + "- " + customerAddress.getPinCode();
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
return address;
|
|
|
265 |
}
|
|
|
266 |
|
| 21985 |
kshitij.so |
267 |
@RequestMapping(value = "/create-order", method = RequestMethod.POST)
|
|
|
268 |
public String createOrder(HttpServletRequest request, @RequestBody CreateOrderRequest createOrderRequest, Model model) throws Throwable{
|
| 22064 |
ashik.ali |
269 |
LOGGER.info("request at uri {} body {}", request.getRequestURI(), createOrderRequest);
|
| 22139 |
amit.gupta |
270 |
LoginDetails fofoDetails;
|
| 21985 |
kshitij.so |
271 |
try {
|
| 22069 |
ashik.ali |
272 |
fofoDetails = cookiesProcessor.getCookiesObject(request);
|
| 21985 |
kshitij.so |
273 |
} catch (ProfitMandiBusinessException e) {
|
|
|
274 |
model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));
|
|
|
275 |
return "response";
|
|
|
276 |
}
|
| 21680 |
ashik.ali |
277 |
Set<Integer> itemIds = new HashSet<>();
|
| 21896 |
ashik.ali |
278 |
Map<Integer, Integer> itemIdQuantity = new HashMap<>(); //this is for error
|
|
|
279 |
Map<Integer, CustomFofoLineItem> customFofoLineItemMap = new HashMap<>();
|
|
|
280 |
Map<Integer, Float> lineItemPrice = new HashMap<>(); //this is for pricing error
|
|
|
281 |
float totalAmount = 0;
|
|
|
282 |
for(CustomFofoLineItem customFofoLineItem : createOrderRequest.getFofoLineItems()){
|
|
|
283 |
itemIds.add(customFofoLineItem.getItemId());
|
|
|
284 |
if(!customFofoLineItem.getSerialNumbers().isEmpty() && customFofoLineItem.getQuantity() != customFofoLineItem.getSerialNumbers().size()){
|
|
|
285 |
itemIdQuantity.put(customFofoLineItem.getItemId(), customFofoLineItem.getQuantity());
|
| 21680 |
ashik.ali |
286 |
}
|
| 21896 |
ashik.ali |
287 |
if(!(customFofoLineItem.getSellingPrice() > 0)){
|
|
|
288 |
lineItemPrice.put(customFofoLineItem.getItemId(), customFofoLineItem.getSellingPrice());
|
|
|
289 |
}else{
|
|
|
290 |
totalAmount = totalAmount + customFofoLineItem.getSellingPrice() * customFofoLineItem.getQuantity();
|
| 21680 |
ashik.ali |
291 |
}
|
| 21896 |
ashik.ali |
292 |
customFofoLineItemMap.put(customFofoLineItem.getItemId(), customFofoLineItem);
|
| 21680 |
ashik.ali |
293 |
}
|
|
|
294 |
if(!itemIdQuantity.isEmpty()){
|
|
|
295 |
// if item quantity does not match with given serialnumbers size
|
|
|
296 |
LOGGER.error("itemId's quantity should be equal to given serialnumber size {} ", itemIdQuantity);
|
|
|
297 |
return "error";
|
|
|
298 |
}
|
|
|
299 |
try{
|
|
|
300 |
this.validatePaymentOptionsAndTotalAmount(createOrderRequest.getPaymentOptions(), totalAmount);
|
|
|
301 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){
|
|
|
302 |
LOGGER.error("Error occured while validating payment options : ", profitMandiBusinessException);
|
|
|
303 |
return "error";
|
|
|
304 |
}
|
| 21896 |
ashik.ali |
305 |
if(!lineItemPrice.isEmpty()){
|
|
|
306 |
// given fofo line item price must be greater than zero
|
|
|
307 |
LOGGER.error("requested itemId's selling price must greater than 0");
|
|
|
308 |
return "error";
|
|
|
309 |
}
|
| 21985 |
kshitij.so |
310 |
|
| 21896 |
ashik.ali |
311 |
List<CurrentInventorySnapshot> currentInventorySnapshots = currentInventorySnapshotRepository.selectByFofoItemIds(fofoDetails.getFofoId(), itemIds);
|
|
|
312 |
if(itemIds.size() != currentInventorySnapshots.size()){
|
|
|
313 |
// error
|
|
|
314 |
}
|
|
|
315 |
List<ItemIdQuantityAvailability> itemIdQuantityAvailabilities = new ArrayList<>(); //this is for error
|
| 21985 |
kshitij.so |
316 |
LOGGER.info("currentInventorySnapshots "+currentInventorySnapshots);
|
| 21896 |
ashik.ali |
317 |
for(CurrentInventorySnapshot currentInventorySnapshot : currentInventorySnapshots){
|
|
|
318 |
CustomFofoLineItem customFofoLineItem = customFofoLineItemMap.get(currentInventorySnapshot.getId().getItemId());
|
| 21985 |
kshitij.so |
319 |
LOGGER.info("customFofoLineItem "+customFofoLineItem);
|
| 21896 |
ashik.ali |
320 |
if(customFofoLineItem.getQuantity() > currentInventorySnapshot.getAvailability()){
|
|
|
321 |
ItemIdQuantityAvailability itemIdQuantityAvailability = new ItemIdQuantityAvailability();
|
|
|
322 |
itemIdQuantityAvailability.setItemId(customFofoLineItem.getItemId());
|
|
|
323 |
Quantity quantity = new Quantity();
|
|
|
324 |
quantity.setAvailable(currentInventorySnapshot.getAvailability());
|
|
|
325 |
quantity.setRequested(customFofoLineItem.getQuantity());
|
|
|
326 |
itemIdQuantityAvailability.setQuantity(quantity);
|
|
|
327 |
itemIdQuantityAvailabilities.add(itemIdQuantityAvailability);
|
|
|
328 |
}
|
| 21985 |
kshitij.so |
329 |
|
| 21896 |
ashik.ali |
330 |
}
|
| 21985 |
kshitij.so |
331 |
|
|
|
332 |
|
|
|
333 |
|
| 21680 |
ashik.ali |
334 |
if(!itemIdQuantityAvailabilities.isEmpty()){
|
|
|
335 |
// itemIdQuantity request is not valid
|
|
|
336 |
LOGGER.error("Requested quantities should not be greater than currently available quantities {}", itemIdQuantityAvailabilities);
|
|
|
337 |
return "error";
|
|
|
338 |
}
|
| 21985 |
kshitij.so |
339 |
|
|
|
340 |
|
|
|
341 |
|
|
|
342 |
// Set<Integer> invalidItemIdSerialNumbers = new HashSet<>();
|
|
|
343 |
// Map<Integer, Set<String>> itemIdSerialNumbers = new HashMap<>();
|
|
|
344 |
// List<InventoryItem> inventoryItems = inventoryItemRepository.selectByFofoIdItemIds(fofoDetails.getFofoId(), itemIds); //change it
|
|
|
345 |
//
|
|
|
346 |
// Map<Integer, Float> itemIdPriceDropAmount = new HashMap<>();
|
|
|
347 |
//
|
|
|
348 |
// for(InventoryItem inventoryItem : inventoryItems){
|
|
|
349 |
// CustomFofoLineItem customFofoLineItem = customFofoLineItemMap.get(inventoryItem.getItemId());
|
|
|
350 |
// if(customFofoLineItem.getSerialNumbers().isEmpty()){
|
|
|
351 |
// if(!(inventoryItem.getSerialNumber() == null && inventoryItem.getSerialNumber().equals(""))){
|
|
|
352 |
// invalidItemIdSerialNumbers.add(inventoryItem.getItemId());
|
|
|
353 |
// }
|
|
|
354 |
// }
|
|
|
355 |
// if(!customFofoLineItem.getSerialNumbers().isEmpty()){
|
|
|
356 |
// if(!customFofoLineItem.getSerialNumbers().contains(inventoryItem.getSerialNumber())){
|
|
|
357 |
// if(!itemIdSerialNumbers.containsKey(customFofoLineItem.getItemId())){
|
|
|
358 |
// Set<String> serialNumbers = new HashSet<>();
|
|
|
359 |
// serialNumbers.add(inventoryItem.getSerialNumber());
|
|
|
360 |
// itemIdSerialNumbers.put(customFofoLineItem.getItemId(), serialNumbers);
|
|
|
361 |
// }else{
|
|
|
362 |
// itemIdSerialNumbers.get(customFofoLineItem.getItemId()).add(inventoryItem.getSerialNumber());
|
|
|
363 |
// }
|
|
|
364 |
// }
|
|
|
365 |
// itemIdPriceDropAmount.put(inventoryItem.getItemId(), inventoryItem.getUnitPrice() - inventoryItem.getPriceDropAmount());
|
|
|
366 |
// }
|
|
|
367 |
|
|
|
368 |
Map<Integer, Item> itemMap = new HashMap<Integer, Item>();
|
|
|
369 |
List<Item> items = itemRepository.selectByIds(itemIds);
|
|
|
370 |
for (Item i : items){
|
|
|
371 |
itemMap.put(i.getId(), i);
|
|
|
372 |
}
|
|
|
373 |
|
|
|
374 |
Set<Integer> nonSerializedItemIds = new HashSet<>();
|
|
|
375 |
Set<String> serialNumbers = new HashSet<>();
|
|
|
376 |
|
|
|
377 |
Collection<CustomFofoLineItem> lineItemsValues = customFofoLineItemMap.values();
|
|
|
378 |
for (CustomFofoLineItem cli : lineItemsValues){
|
|
|
379 |
|
|
|
380 |
Item item = itemMap.get(cli.getItemId());
|
|
|
381 |
if (item.getType().equals(ItemType.SERIALIZED)){
|
|
|
382 |
for (String s : cli.getSerialNumbers()){
|
|
|
383 |
serialNumbers.add(s);
|
| 21896 |
ashik.ali |
384 |
}
|
|
|
385 |
}
|
| 21985 |
kshitij.so |
386 |
else{
|
|
|
387 |
nonSerializedItemIds.add(cli.getItemId());
|
|
|
388 |
}
|
|
|
389 |
|
|
|
390 |
}
|
|
|
391 |
|
|
|
392 |
|
|
|
393 |
Map<Integer, List<InventoryItem>> serializedInventoryItemMap = new HashMap<Integer, List<InventoryItem>>();
|
|
|
394 |
Map<Integer, List<InventoryItem>> nonSerializedInventoryItemMap = new HashMap<Integer, List<InventoryItem>>();
|
|
|
395 |
Map<Integer, List<Float>> itemIdPriceDropAmount = new HashMap<>();
|
|
|
396 |
|
|
|
397 |
if (serialNumbers.size() > 0 ){
|
|
|
398 |
List<InventoryItem> serializedInventoryItems = inventoryItemRepository.selectByFofoIdSerialNumbers(fofoDetails.getFofoId(), serialNumbers);
|
|
|
399 |
for (InventoryItem it : serializedInventoryItems){
|
|
|
400 |
if (it.getGoodQuantity() == 1){
|
|
|
401 |
if (serializedInventoryItemMap.containsKey(it.getItemId())){
|
|
|
402 |
serializedInventoryItemMap.get(it.getItemId()).add(it);
|
|
|
403 |
itemIdPriceDropAmount.get(it.getItemId()).add(it.getUnitPrice() - (it.getPriceDropAmount()==null?0:it.getPriceDropAmount()));
|
| 21680 |
ashik.ali |
404 |
}
|
| 21985 |
kshitij.so |
405 |
else{
|
|
|
406 |
ArrayList<InventoryItem> tmp = new ArrayList<InventoryItem>();
|
|
|
407 |
tmp.add(it);
|
|
|
408 |
serializedInventoryItemMap.put(it.getItemId(), tmp);
|
|
|
409 |
ArrayList<Float> priceDropAmouts = new ArrayList<>();
|
|
|
410 |
priceDropAmouts.add(it.getUnitPrice() - (it.getPriceDropAmount()==null?0:it.getPriceDropAmount()));
|
|
|
411 |
itemIdPriceDropAmount.put(it.getItemId(), priceDropAmouts);
|
|
|
412 |
}
|
| 21680 |
ashik.ali |
413 |
}
|
|
|
414 |
}
|
|
|
415 |
}
|
| 21985 |
kshitij.so |
416 |
|
|
|
417 |
if (nonSerializedItemIds.size() > 0){
|
|
|
418 |
List<InventoryItem> nonSerializedInventoryItems = inventoryItemRepository.selectByFofoIdItemIds(fofoDetails.getFofoId(), nonSerializedItemIds);
|
|
|
419 |
for (InventoryItem it : nonSerializedInventoryItems){
|
|
|
420 |
if (it.getGoodQuantity() > 0){
|
|
|
421 |
if (nonSerializedInventoryItemMap.containsKey(it.getItemId())){
|
|
|
422 |
nonSerializedInventoryItemMap.get(it.getItemId()).add(it);
|
|
|
423 |
}
|
|
|
424 |
else{
|
|
|
425 |
ArrayList<InventoryItem> tmp = new ArrayList<InventoryItem>();
|
|
|
426 |
tmp.add(it);
|
|
|
427 |
nonSerializedInventoryItemMap.put(it.getItemId(), tmp);
|
|
|
428 |
}
|
|
|
429 |
}
|
|
|
430 |
}
|
|
|
431 |
}
|
|
|
432 |
|
|
|
433 |
List<Integer> invalidItemIdSerialNumbers = new ArrayList<Integer>();
|
|
|
434 |
List<Integer> itemIdSerialNumbers = new ArrayList<Integer>();
|
|
|
435 |
|
|
|
436 |
for (Item i : items){
|
|
|
437 |
CustomFofoLineItem customFofoLineItem = customFofoLineItemMap.get(i.getId());
|
|
|
438 |
if (i.getType().equals(ItemType.SERIALIZED)){
|
|
|
439 |
if (customFofoLineItem ==null || customFofoLineItem.getSerialNumbers().size() ==0){
|
|
|
440 |
invalidItemIdSerialNumbers.add(i.getId());
|
|
|
441 |
}
|
|
|
442 |
}
|
|
|
443 |
else{
|
|
|
444 |
if (customFofoLineItem == null || customFofoLineItem.getSerialNumbers().size() > 0 ){
|
|
|
445 |
itemIdSerialNumbers.add(i.getId());
|
|
|
446 |
}
|
|
|
447 |
}
|
|
|
448 |
}
|
|
|
449 |
|
| 21680 |
ashik.ali |
450 |
if(!invalidItemIdSerialNumbers.isEmpty()){
|
|
|
451 |
LOGGER.error("Invalid itemId's serialNumbers {}", invalidItemIdSerialNumbers);
|
|
|
452 |
// itemId's are serialized you are saying these are not serialized
|
|
|
453 |
return "error";
|
|
|
454 |
}
|
| 21985 |
kshitij.so |
455 |
|
| 21680 |
ashik.ali |
456 |
if(!itemIdSerialNumbers.isEmpty()){
|
|
|
457 |
LOGGER.error("Invalid itemId's serialNumbers {}", itemIdSerialNumbers);
|
|
|
458 |
// itemId's are non serialized you are saying these are serialized
|
|
|
459 |
return "error";
|
|
|
460 |
}
|
| 21985 |
kshitij.so |
461 |
|
| 21896 |
ashik.ali |
462 |
if(items.size() != itemIds.size()){
|
|
|
463 |
LOGGER.error("Requested ItemIds not found in catalog");
|
|
|
464 |
// invalid itemIds
|
|
|
465 |
return "error";
|
|
|
466 |
}
|
| 21985 |
kshitij.so |
467 |
|
|
|
468 |
Map<Integer, List<InventoryItem>> inventoryItemsToBill = new HashMap<Integer,List<InventoryItem>>();
|
|
|
469 |
Map<Integer, Integer> inventoryItemQuantityUsed = new HashMap<>(); //to keep track of inventoryitem quanity used for scan records insertion
|
|
|
470 |
|
|
|
471 |
//Lets reduce quantity and decide what inventory items to use.
|
|
|
472 |
for (Item i : items){
|
|
|
473 |
CustomFofoLineItem customFofoLineItem = customFofoLineItemMap.get(i.getId());
|
|
|
474 |
if (i.getType().equals(ItemType.SERIALIZED)){
|
|
|
475 |
//TODO:handle null
|
|
|
476 |
if (serializedInventoryItemMap.get(i.getId()) == null || customFofoLineItem.getSerialNumbers().size() != serializedInventoryItemMap.get(i.getId()).size()){
|
|
|
477 |
//not enough serial numbers
|
|
|
478 |
System.out.println("not enough serial numbers");
|
|
|
479 |
return "error";
|
|
|
480 |
}
|
|
|
481 |
List<InventoryItem> inventoryItemsSerializedserialized = serializedInventoryItemMap.get(i.getId());
|
|
|
482 |
for (InventoryItem it : inventoryItemsSerializedserialized){
|
|
|
483 |
it.setGoodQuantity(0);
|
|
|
484 |
inventoryItemQuantityUsed.put(it.getId(), 1);
|
|
|
485 |
}
|
|
|
486 |
inventoryItemsToBill.put(i.getId(), inventoryItemsSerializedserialized);
|
|
|
487 |
}
|
|
|
488 |
else{
|
|
|
489 |
List<InventoryItem> inventoryItemsNonSerialized = nonSerializedInventoryItemMap.get(i.getId());
|
|
|
490 |
int quantityToBill = customFofoLineItem.getQuantity();
|
|
|
491 |
int totalLeft = quantityToBill;
|
|
|
492 |
List<InventoryItem> inventoryItemsNonSerializedUsed = new ArrayList<InventoryItem>();
|
|
|
493 |
if (inventoryItemsNonSerialized!=null){
|
|
|
494 |
for (InventoryItem it : inventoryItemsNonSerialized){
|
|
|
495 |
if (totalLeft > 0){
|
|
|
496 |
int toUse = Math.min(totalLeft, it.getGoodQuantity());
|
|
|
497 |
inventoryItemQuantityUsed.put(it.getId(), toUse);
|
|
|
498 |
it.setGoodQuantity(it.getGoodQuantity() - toUse);
|
|
|
499 |
totalLeft = totalLeft - toUse;
|
|
|
500 |
inventoryItemsNonSerializedUsed.add(it);
|
|
|
501 |
}
|
|
|
502 |
}
|
|
|
503 |
}
|
|
|
504 |
|
|
|
505 |
if (totalLeft > 0){
|
|
|
506 |
//not enough quanity for non-serialized
|
|
|
507 |
System.out.println("not enough quanity for non-serialized");
|
|
|
508 |
return "error";
|
|
|
509 |
}
|
|
|
510 |
inventoryItemsToBill.put(i.getId(), inventoryItemsNonSerializedUsed);
|
|
|
511 |
}
|
|
|
512 |
}
|
| 22069 |
ashik.ali |
513 |
|
|
|
514 |
// mop price validation
|
| 22189 |
amit.gupta |
515 |
//TODO: Amit Gupta need to be commented out
|
|
|
516 |
/*Map<Integer, Float> invalidMopItemIdPriceMap = new HashMap<>();
|
| 22069 |
ashik.ali |
517 |
Map<Integer, Float> itemIdPrinceMap = pricingService.getPrice(itemIds, fofoDetails.getFofoId());
|
|
|
518 |
for(Map.Entry<Integer, Float> entry : itemIdPrinceMap.entrySet()){
|
|
|
519 |
CustomFofoLineItem customFofoLineItem = customFofoLineItemMap.get(entry.getKey());
|
|
|
520 |
if(customFofoLineItem.getSellingPrice() < entry.getValue()){
|
|
|
521 |
invalidMopItemIdPriceMap.put(entry.getKey(), customFofoLineItem.getSellingPrice());
|
|
|
522 |
}
|
|
|
523 |
}
|
|
|
524 |
|
|
|
525 |
if(invalidMopItemIdPriceMap.isEmpty()){
|
|
|
526 |
LOGGER.error("Invalid itemIds mop prices should be greater than mop prices {}", invalidMopItemIdPriceMap);
|
|
|
527 |
return "error";
|
| 22189 |
amit.gupta |
528 |
}*/
|
| 21985 |
kshitij.so |
529 |
|
| 21680 |
ashik.ali |
530 |
InvoiceNumberGenerationSequence invoiceNumberGenerationSequence = null;
|
|
|
531 |
try{
|
|
|
532 |
invoiceNumberGenerationSequence = invoiceNumberGenerationSequenceRepository.selectByFofoId(fofoDetails.getFofoId());
|
|
|
533 |
invoiceNumberGenerationSequence.setSequence(invoiceNumberGenerationSequence.getSequence() + 1);
|
|
|
534 |
invoiceNumberGenerationSequenceRepository.persist(invoiceNumberGenerationSequence);
|
|
|
535 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){
|
|
|
536 |
invoiceNumberGenerationSequence = new InvoiceNumberGenerationSequence();
|
|
|
537 |
invoiceNumberGenerationSequence.setFofoId(fofoDetails.getFofoId());
|
|
|
538 |
invoiceNumberGenerationSequence.setPrefix("INVOICE");
|
| 22028 |
ashik.ali |
539 |
invoiceNumberGenerationSequence.setSequence(1);
|
| 21680 |
ashik.ali |
540 |
invoiceNumberGenerationSequenceRepository.persist(invoiceNumberGenerationSequence);
|
|
|
541 |
}
|
| 21985 |
kshitij.so |
542 |
|
| 21680 |
ashik.ali |
543 |
CustomCustomer customCustomer = createOrderRequest.getCustomer();
|
| 21985 |
kshitij.so |
544 |
|
| 21680 |
ashik.ali |
545 |
if(!StringUtils.isValidEmailAddress(customCustomer.getEmailId())){
|
|
|
546 |
LOGGER.error("invalid customer emailId {} ", customCustomer.getEmailId());
|
|
|
547 |
return "error";
|
|
|
548 |
}
|
| 21985 |
kshitij.so |
549 |
|
| 21680 |
ashik.ali |
550 |
if(!StringUtils.isValidMobile(customCustomer.getMobileNumber())){
|
|
|
551 |
LOGGER.error("invalid customer mobileNumber {} ", customCustomer.getMobileNumber());
|
|
|
552 |
return "error";
|
|
|
553 |
}
|
| 21985 |
kshitij.so |
554 |
|
| 21680 |
ashik.ali |
555 |
Customer customer = null;
|
| 21985 |
kshitij.so |
556 |
try{
|
|
|
557 |
customer = customerRepository.selectByMobileNumber(customCustomer.getMobileNumber());
|
|
|
558 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){
|
|
|
559 |
LOGGER.error("Error : ", profitMandiBusinessException);
|
| 21680 |
ashik.ali |
560 |
customer = new Customer();
|
| 21883 |
kshitij.so |
561 |
customer.setName(customCustomer.getName());
|
| 21680 |
ashik.ali |
562 |
customer.setEmailId(customCustomer.getEmailId());
|
|
|
563 |
customer.setMobileNumber(customCustomer.getMobileNumber());
|
|
|
564 |
customerRepository.persist(customer);
|
|
|
565 |
}
|
| 21896 |
ashik.ali |
566 |
//TODO:Check if createOrderRequest contains addressId
|
| 21711 |
ashik.ali |
567 |
CustomerAddress customerAddress = this.createCustomerAddress(customCustomer.getAddress());
|
|
|
568 |
customerAddress.setCustomerId(customer.getId());
|
|
|
569 |
customerAddressRepository.persist(customerAddress);
|
| 21985 |
kshitij.so |
570 |
|
| 21680 |
ashik.ali |
571 |
FofoOrder fofoOrder = new FofoOrder();
|
|
|
572 |
fofoOrder.setCustomerId(customer.getId());
|
|
|
573 |
fofoOrder.setFofoId(fofoDetails.getFofoId());
|
|
|
574 |
fofoOrder.setInvoiceNumber(invoiceNumberGenerationSequence.getPrefix() + invoiceNumberGenerationSequence.getSequence());
|
|
|
575 |
fofoOrder.setTotalAmount(totalAmount);
|
| 21886 |
kshitij.so |
576 |
fofoOrder.setCustomerAddressId(customerAddress.getId());
|
| 21680 |
ashik.ali |
577 |
fofoOrderRepository.persist(fofoOrder);
|
| 21985 |
kshitij.so |
578 |
|
| 21680 |
ashik.ali |
579 |
for(CustomPaymentOption customPaymentOption : createOrderRequest.getPaymentOptions()){
|
|
|
580 |
PaymentOption paymentOption = new PaymentOption();
|
|
|
581 |
paymentOption.setOrderId(fofoOrder.getId());
|
|
|
582 |
paymentOption.setAmount(customPaymentOption.getAmount());
|
|
|
583 |
paymentOption.setType(customPaymentOption.getType());
|
|
|
584 |
paymentOptionRepository.persist(paymentOption);
|
|
|
585 |
}
|
| 21985 |
kshitij.so |
586 |
|
| 21896 |
ashik.ali |
587 |
Address retailerAddress = addressRepository.selectById(retailerRegisteredAddressRepository.selectAddressIdByRetailerId(fofoDetails.getFofoId()));
|
| 22064 |
ashik.ali |
588 |
Map<String, GstRate> gstRateMap = null;
|
|
|
589 |
if(retailerAddress.getState().equals(customerAddress.getState())){
|
|
|
590 |
gstRateMap = Utils.getGstRates(retailerAddress.getState());
|
|
|
591 |
}else{
|
|
|
592 |
LOGGER.info("inter gstRate = true");
|
|
|
593 |
gstRateMap = Utils.getInterGstRates();
|
|
|
594 |
}
|
|
|
595 |
LOGGER.info("gstRateMap {}", gstRateMap);
|
| 21680 |
ashik.ali |
596 |
for(CustomFofoLineItem customFofoLineItem : createOrderRequest.getFofoLineItems()){
|
|
|
597 |
FofoLineItem fofoLineItem = new FofoLineItem();
|
|
|
598 |
fofoLineItem.setItemId(customFofoLineItem.getItemId());
|
|
|
599 |
fofoLineItem.setQuantity(customFofoLineItem.getQuantity());
|
| 21690 |
ashik.ali |
600 |
fofoLineItem.setSellingPrice(customFofoLineItem.getSellingPrice());
|
| 21680 |
ashik.ali |
601 |
fofoLineItem.setOrderId(fofoOrder.getId());
|
|
|
602 |
fofoLineItem.setDp(customFofoLineItem.getSellingPrice());
|
| 21985 |
kshitij.so |
603 |
Item item = itemMap.get(customFofoLineItem.getItemId());
|
| 21896 |
ashik.ali |
604 |
GstRate gstRate = gstRateMap.get(item.getHsnCode());
|
|
|
605 |
fofoLineItem.setIgstRate(gstRate.getIgstRate());
|
|
|
606 |
fofoLineItem.setCgstRate(gstRate.getCgstRate());
|
|
|
607 |
fofoLineItem.setSgstRate(gstRate.getSgstRate());
|
| 21919 |
ashik.ali |
608 |
fofoLineItem.setHsnCode(gstRate.getHsnCode());
|
| 21985 |
kshitij.so |
609 |
List<Float> priceDropAmounts = itemIdPriceDropAmount.get(customFofoLineItem.getItemId());
|
|
|
610 |
float cost = 0;
|
|
|
611 |
if (priceDropAmounts!=null){
|
|
|
612 |
for (Float pda : priceDropAmounts){
|
|
|
613 |
cost = cost + pda;
|
|
|
614 |
}
|
|
|
615 |
}
|
|
|
616 |
else{
|
|
|
617 |
cost = customFofoLineItem.getSellingPrice()* customFofoLineItem.getQuantity();
|
|
|
618 |
}
|
|
|
619 |
fofoLineItem.setCost(cost);
|
| 21896 |
ashik.ali |
620 |
fofoLineItem.setBrand(item.getBrand());
|
|
|
621 |
fofoLineItem.setModelName(item.getModelName());
|
|
|
622 |
fofoLineItem.setModelNumber(item.getModelNumber());
|
|
|
623 |
fofoLineItem.setColor(item.getColor());
|
| 21680 |
ashik.ali |
624 |
fofoLineItemRepository.persist(fofoLineItem);
|
|
|
625 |
LOGGER.info("\n\n");
|
|
|
626 |
if(!customFofoLineItem.getSerialNumbers().isEmpty()){
|
|
|
627 |
for(String serialNumber : customFofoLineItem.getSerialNumbers()){
|
|
|
628 |
FofoLineItemSerialNumber fofoLineItemSerialNumber = new FofoLineItemSerialNumber();
|
|
|
629 |
fofoLineItemSerialNumber.setFofoLineItemId(fofoLineItem.getId());
|
|
|
630 |
fofoLineItemSerialNumber.setSerialNumber(serialNumber);
|
|
|
631 |
fofoLineItemSerialNumberRepository.persist(fofoLineItemSerialNumber);
|
|
|
632 |
}
|
|
|
633 |
}
|
| 21985 |
kshitij.so |
634 |
|
| 21896 |
ashik.ali |
635 |
for(CurrentInventorySnapshot currentInventorySnapshot : currentInventorySnapshots){
|
|
|
636 |
FofoItemId fofoItemId = new FofoItemId();
|
|
|
637 |
fofoItemId.setFofoId(fofoDetails.getFofoId());
|
|
|
638 |
fofoItemId.setItemId(fofoLineItem.getItemId());
|
|
|
639 |
if(currentInventorySnapshot.getId().equals(fofoItemId)){
|
|
|
640 |
currentInventorySnapshotRepository.updateAvailabilityByFofoItemId(fofoItemId, currentInventorySnapshot.getAvailability() - customFofoLineItem.getQuantity());
|
|
|
641 |
}
|
|
|
642 |
}
|
| 21985 |
kshitij.so |
643 |
List<InventoryItem> inventoryItems = inventoryItemsToBill.get(fofoLineItem.getItemId());
|
| 21680 |
ashik.ali |
644 |
for(InventoryItem inventoryItem : inventoryItems){
|
| 21985 |
kshitij.so |
645 |
inventoryItem.setLastScanType(ScanType.SALE);
|
|
|
646 |
inventoryItemRepository.persist(inventoryItem);
|
|
|
647 |
ScanRecord scanRecord = new ScanRecord();
|
|
|
648 |
scanRecord.setInventoryItemId(inventoryItem.getId());
|
|
|
649 |
scanRecord.setFofoId(fofoDetails.getFofoId());
|
|
|
650 |
//correct this
|
|
|
651 |
scanRecord.setQuantity(inventoryItemQuantityUsed.get(inventoryItem.getId()));
|
|
|
652 |
scanRecord.setType(ScanType.SALE);
|
|
|
653 |
scanRecordRepository.persist(scanRecord);
|
|
|
654 |
}
|
| 21680 |
ashik.ali |
655 |
}
|
| 22189 |
amit.gupta |
656 |
return "redirect:/get-order/?orderId="+fofoOrder.getId();
|
| 21612 |
ashik.ali |
657 |
}
|
| 21985 |
kshitij.so |
658 |
|
| 21711 |
ashik.ali |
659 |
private CustomerAddress createCustomerAddress(CustomAddress customAddress){
|
|
|
660 |
CustomerAddress customerAddress = new CustomerAddress();
|
|
|
661 |
customerAddress.setName(customAddress.getName());
|
|
|
662 |
customerAddress.setLine1(customAddress.getLine1());
|
|
|
663 |
customerAddress.setLine2(customAddress.getLine2());
|
|
|
664 |
customerAddress.setLandmark(customAddress.getLandmark());
|
|
|
665 |
customerAddress.setCity(customAddress.getCity());
|
|
|
666 |
customerAddress.setPinCode(customAddress.getPinCode());
|
|
|
667 |
customerAddress.setState(customAddress.getState());
|
|
|
668 |
customerAddress.setCountry(customAddress.getCountry());
|
|
|
669 |
customerAddress.setPhoneNumber(customAddress.getPhoneNumber());
|
|
|
670 |
return customerAddress;
|
| 21680 |
ashik.ali |
671 |
}
|
| 21985 |
kshitij.so |
672 |
|
| 21689 |
ashik.ali |
673 |
private CustomAddress createCustomAddress(Address address){
|
|
|
674 |
CustomAddress customAddress = new CustomAddress();
|
|
|
675 |
customAddress.setName(address.getName());
|
|
|
676 |
customAddress.setLine1(address.getLine1());
|
|
|
677 |
customAddress.setLine2(address.getLine2());
|
|
|
678 |
customAddress.setLandmark(address.getLandmark());
|
|
|
679 |
customAddress.setCity(address.getCity());
|
|
|
680 |
customAddress.setPinCode(address.getPinCode());
|
|
|
681 |
customAddress.setState(address.getState());
|
|
|
682 |
customAddress.setCountry(address.getCountry());
|
|
|
683 |
customAddress.setPhoneNumber(address.getPhoneNumber());
|
|
|
684 |
return customAddress;
|
| 21680 |
ashik.ali |
685 |
}
|
| 21985 |
kshitij.so |
686 |
|
| 21711 |
ashik.ali |
687 |
private CustomAddress createCustomAddress(CustomerAddress customerAddress){
|
|
|
688 |
CustomAddress customAddress = new CustomAddress();
|
|
|
689 |
customAddress.setName(customerAddress.getName());
|
|
|
690 |
customAddress.setLine1(customerAddress.getLine1());
|
|
|
691 |
customAddress.setLine2(customerAddress.getLine2());
|
|
|
692 |
customAddress.setLandmark(customerAddress.getLandmark());
|
|
|
693 |
customAddress.setCity(customerAddress.getCity());
|
|
|
694 |
customAddress.setPinCode(customerAddress.getPinCode());
|
|
|
695 |
customAddress.setState(customerAddress.getState());
|
|
|
696 |
customAddress.setCountry(customerAddress.getCountry());
|
|
|
697 |
customAddress.setPhoneNumber(customerAddress.getPhoneNumber());
|
|
|
698 |
return customAddress;
|
|
|
699 |
}
|
| 21985 |
kshitij.so |
700 |
|
| 21680 |
ashik.ali |
701 |
private void validatePaymentOptionsAndTotalAmount(Set<CustomPaymentOption> customPaymentOptions, float totalAmount) throws ProfitMandiBusinessException
|
| 21612 |
ashik.ali |
702 |
{
|
|
|
703 |
float calculatedAmount = 0;
|
|
|
704 |
Set<String> paymentOptionTypes = new HashSet<>();
|
|
|
705 |
for(CustomPaymentOption customPaymentOption : customPaymentOptions){
|
| 21680 |
ashik.ali |
706 |
if(paymentOptionTypes.contains(customPaymentOption.getType().name())){
|
| 21612 |
ashik.ali |
707 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.PAYMENT_OPTION_TYPE, customPaymentOption.getType().name(), "");
|
| 21680 |
ashik.ali |
708 |
}else{
|
|
|
709 |
paymentOptionTypes.add(customPaymentOption.getType().name());
|
|
|
710 |
calculatedAmount = calculatedAmount + customPaymentOption.getAmount();
|
| 21612 |
ashik.ali |
711 |
}
|
|
|
712 |
}
|
|
|
713 |
if(calculatedAmount != totalAmount){
|
|
|
714 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.PAYMENT_OPTION_CALCULATED_AMOUNT, calculatedAmount, "");
|
|
|
715 |
}
|
|
|
716 |
}
|
| 21985 |
kshitij.so |
717 |
|
|
|
718 |
|
| 21689 |
ashik.ali |
719 |
@RequestMapping(value = "/generateInvoice")
|
| 21896 |
ashik.ali |
720 |
public ResponseEntity<?> generateInvoice(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId) throws Throwable{
|
| 22064 |
ashik.ali |
721 |
LOGGER.info("Request received at url {} with params [{}={}] ", request.getRequestURI(), ProfitMandiConstants.ORDER_ID, orderId);
|
| 22139 |
amit.gupta |
722 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
| 22026 |
ashik.ali |
723 |
|
| 21689 |
ashik.ali |
724 |
FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndOrderId(fofoDetails.getFofoId(), orderId);
|
|
|
725 |
PdfModel pdfModel = new PdfModel();
|
|
|
726 |
pdfModel.setAuther("profitmandi");
|
|
|
727 |
pdfModel.setTitle("Retailer Invoice");
|
|
|
728 |
Customer customer = customerRepository.selectById(fofoOrder.getCustomerId());
|
|
|
729 |
CustomCustomer customCustomer = new CustomCustomer();
|
|
|
730 |
customCustomer.setName(customer.getName());
|
|
|
731 |
customCustomer.setEmailId(customer.getEmailId());
|
|
|
732 |
customCustomer.setMobileNumber(customer.getMobileNumber());
|
| 21711 |
ashik.ali |
733 |
CustomerAddress customerAddress = customerAddressRepository.selectById(fofoOrder.getCustomerAddressId());
|
| 21689 |
ashik.ali |
734 |
customCustomer.setAddress(this.createCustomAddress(customerAddress));
|
|
|
735 |
pdfModel.setCustomer(customCustomer);
|
|
|
736 |
pdfModel.setInvoiceNumber(fofoOrder.getInvoiceNumber());
|
| 21965 |
ashik.ali |
737 |
pdfModel.setTotalAmount(fofoOrder.getTotalAmount());
|
| 22026 |
ashik.ali |
738 |
|
| 21689 |
ashik.ali |
739 |
Retailer retailer = retailerRepository.selectById(fofoDetails.getFofoId());
|
|
|
740 |
User user = userRepository.selectById(userAccountRepository.selectUserIdByRetailerId(retailer.getId()));
|
|
|
741 |
CustomRetailer customRetailer = new CustomRetailer();
|
|
|
742 |
customRetailer.setBusinessName(retailer.getName());
|
|
|
743 |
customRetailer.setMobileNumber(user.getMobileNumber());
|
|
|
744 |
customRetailer.setTinNumber(retailer.getNumber());
|
|
|
745 |
Address retailerAddress = addressRepository.selectById(retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailer.getId()));
|
|
|
746 |
customRetailer.setAddress(this.createCustomAddress(retailerAddress));
|
|
|
747 |
pdfModel.setRetailer(customRetailer);
|
|
|
748 |
List<FofoLineItem> fofoLineItems = fofoLineItemRepository.selectByOrderId(fofoOrder.getId());
|
| 22026 |
ashik.ali |
749 |
|
| 21689 |
ashik.ali |
750 |
Set<CustomFofoOrderItem> customerFofoOrderItems = new HashSet<>();
|
|
|
751 |
for(FofoLineItem fofoLineItem : fofoLineItems){
|
|
|
752 |
CustomFofoOrderItem customFofoOrderItem = new CustomFofoOrderItem();
|
| 22026 |
ashik.ali |
753 |
float totalTaxRate = fofoLineItem.getIgstRate() + fofoLineItem.getSgstRate() + fofoLineItem.getCgstRate();
|
|
|
754 |
float taxableSellingPrice = fofoLineItem.getSellingPrice() / (1 + totalTaxRate / 100);
|
|
|
755 |
|
|
|
756 |
customFofoOrderItem.setAmount(fofoLineItem.getQuantity() * taxableSellingPrice);
|
| 21689 |
ashik.ali |
757 |
customFofoOrderItem.setDescription(fofoLineItem.getBrand() + " " + fofoLineItem.getModelName() + " " + fofoLineItem.getModelNumber() + " " + fofoLineItem.getColor());
|
| 22026 |
ashik.ali |
758 |
customFofoOrderItem.setRate(taxableSellingPrice);
|
| 21689 |
ashik.ali |
759 |
customFofoOrderItem.setQuantity(fofoLineItem.getQuantity());
|
| 22026 |
ashik.ali |
760 |
float igstAmount = (customFofoOrderItem.getAmount() * fofoLineItem.getIgstRate()) / 100;
|
|
|
761 |
float cgstAmount = (customFofoOrderItem.getAmount() * fofoLineItem.getCgstRate()) / 100;
|
|
|
762 |
float sgstAmount = (customFofoOrderItem.getAmount() * fofoLineItem.getSgstRate()) / 100;
|
| 21896 |
ashik.ali |
763 |
customFofoOrderItem.setIgstRate(fofoLineItem.getIgstRate());
|
| 22026 |
ashik.ali |
764 |
customFofoOrderItem.setIgstAmount(igstAmount);
|
|
|
765 |
customFofoOrderItem.setCgstRate(fofoLineItem.getCgstRate());
|
|
|
766 |
customFofoOrderItem.setCgstAmount(cgstAmount);
|
|
|
767 |
customFofoOrderItem.setSgstRate(fofoLineItem.getSgstRate());
|
|
|
768 |
customFofoOrderItem.setSgstAmount(sgstAmount);
|
| 21896 |
ashik.ali |
769 |
customFofoOrderItem.setHsnCode(fofoLineItem.getHsnCode());
|
| 21689 |
ashik.ali |
770 |
customFofoOrderItem.setSerialNumbers(String.join(", ",this.toSerialNumbers(fofoLineItem.getFofoLineItemSerialNumbers())));
|
|
|
771 |
customerFofoOrderItems.add(customFofoOrderItem);
|
|
|
772 |
}
|
|
|
773 |
pdfModel.setOrderItems(customerFofoOrderItems);
|
|
|
774 |
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
775 |
PdfUtils.generateAndWrite(pdfModel, byteArrayOutputStream);
|
|
|
776 |
//final MediaType mediaType=MediaType.parseMediaType(profilePhotoModel.getContentType().getValue());
|
|
|
777 |
LOGGER.info("Pdf Stream length {}", byteArrayOutputStream.toByteArray().length);
|
| 22026 |
ashik.ali |
778 |
final HttpHeaders headers=new HttpHeaders();
|
|
|
779 |
headers.setContentType(MediaType.APPLICATION_PDF);
|
| 22099 |
kshitij.so |
780 |
headers.set("Content-disposition", "inline; filename=invoice-" + fofoOrder.getInvoiceNumber() + ".pdf");
|
| 22026 |
ashik.ali |
781 |
headers.setContentLength(byteArrayOutputStream.toByteArray().length);
|
|
|
782 |
final InputStream inputStream=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
|
|
|
783 |
final InputStreamResource inputStreamResource=new InputStreamResource(inputStream);
|
|
|
784 |
return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
|
| 21689 |
ashik.ali |
785 |
}
|
| 21985 |
kshitij.so |
786 |
|
| 21689 |
ashik.ali |
787 |
private Set<String> toSerialNumbers(Set<FofoLineItemSerialNumber> fofoLineItemSerialNumbers){
|
|
|
788 |
Set<String> serialNumbers = new HashSet<>(fofoLineItemSerialNumbers.size());
|
|
|
789 |
for(FofoLineItemSerialNumber fofoLineItemSerialNumber : fofoLineItemSerialNumbers){
|
|
|
790 |
serialNumbers.add(fofoLineItemSerialNumber.getSerialNumber());
|
|
|
791 |
}
|
|
|
792 |
return serialNumbers;
|
|
|
793 |
}
|
| 21985 |
kshitij.so |
794 |
|
|
|
795 |
|
| 21612 |
ashik.ali |
796 |
}
|