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