| 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;
|
| 22244 |
ashik.ali |
6 |
import java.time.LocalDateTime;
|
| 21680 |
ashik.ali |
7 |
import java.util.ArrayList;
|
| 21612 |
ashik.ali |
8 |
import java.util.HashSet;
|
| 21680 |
ashik.ali |
9 |
import java.util.List;
|
|
|
10 |
import java.util.Map;
|
| 21612 |
ashik.ali |
11 |
import java.util.Set;
|
|
|
12 |
|
|
|
13 |
import javax.servlet.http.HttpServletRequest;
|
| 21689 |
ashik.ali |
14 |
import javax.servlet.http.HttpServletResponse;
|
| 21612 |
ashik.ali |
15 |
|
|
|
16 |
import org.slf4j.Logger;
|
|
|
17 |
import org.slf4j.LoggerFactory;
|
|
|
18 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 21689 |
ashik.ali |
19 |
import org.springframework.core.io.InputStreamResource;
|
|
|
20 |
import org.springframework.http.HttpHeaders;
|
|
|
21 |
import org.springframework.http.HttpStatus;
|
|
|
22 |
import org.springframework.http.MediaType;
|
|
|
23 |
import org.springframework.http.ResponseEntity;
|
| 21612 |
ashik.ali |
24 |
import org.springframework.stereotype.Controller;
|
| 22064 |
ashik.ali |
25 |
import org.springframework.transaction.annotation.Transactional;
|
| 21985 |
kshitij.so |
26 |
import org.springframework.ui.Model;
|
| 21612 |
ashik.ali |
27 |
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
28 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
29 |
import org.springframework.web.bind.annotation.RequestMethod;
|
| 21689 |
ashik.ali |
30 |
import org.springframework.web.bind.annotation.RequestParam;
|
| 21612 |
ashik.ali |
31 |
|
|
|
32 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 21985 |
kshitij.so |
33 |
import com.spice.profitmandi.common.model.CartFofo;
|
| 22860 |
ashik.ali |
34 |
import com.spice.profitmandi.common.model.CreateOrderRequest;
|
| 22660 |
ashik.ali |
35 |
import com.spice.profitmandi.common.model.PdfModel;
|
| 22581 |
ashik.ali |
36 |
import com.spice.profitmandi.common.model.PriceModel;
|
| 21612 |
ashik.ali |
37 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 21689 |
ashik.ali |
38 |
import com.spice.profitmandi.common.util.PdfUtils;
|
| 21680 |
ashik.ali |
39 |
import com.spice.profitmandi.common.util.StringUtils;
|
| 22244 |
ashik.ali |
40 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
| 22217 |
ashik.ali |
41 |
import com.spice.profitmandi.dao.entity.dtr.InsurancePolicy;
|
| 21728 |
ashik.ali |
42 |
import com.spice.profitmandi.dao.entity.fofo.Customer;
|
|
|
43 |
import com.spice.profitmandi.dao.entity.fofo.CustomerAddress;
|
| 22927 |
ashik.ali |
44 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
| 22860 |
ashik.ali |
45 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
|
| 21728 |
ashik.ali |
46 |
import com.spice.profitmandi.dao.entity.fofo.PaymentOption;
|
| 22217 |
ashik.ali |
47 |
import com.spice.profitmandi.dao.repository.dtr.InsurancePolicyRepository;
|
| 21728 |
ashik.ali |
48 |
import com.spice.profitmandi.dao.repository.fofo.CustomerAddressRepository;
|
|
|
49 |
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
|
| 22860 |
ashik.ali |
50 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
|
| 21728 |
ashik.ali |
51 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
|
|
|
52 |
import com.spice.profitmandi.dao.repository.fofo.PaymentOptionRepository;
|
| 22927 |
ashik.ali |
53 |
import com.spice.profitmandi.service.inventory.OrderService;
|
| 22069 |
ashik.ali |
54 |
import com.spice.profitmandi.service.pricing.PricingService;
|
| 22139 |
amit.gupta |
55 |
import com.spice.profitmandi.web.model.LoginDetails;
|
| 22069 |
ashik.ali |
56 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
| 21612 |
ashik.ali |
57 |
|
|
|
58 |
@Controller
|
| 22037 |
amit.gupta |
59 |
@Transactional(rollbackFor=Throwable.class)
|
| 21612 |
ashik.ali |
60 |
public class OrderController {
|
|
|
61 |
|
|
|
62 |
private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);
|
| 21985 |
kshitij.so |
63 |
|
| 21612 |
ashik.ali |
64 |
@Autowired
|
| 22860 |
ashik.ali |
65 |
private CustomerRepository customerRepository;
|
| 21985 |
kshitij.so |
66 |
|
| 21612 |
ashik.ali |
67 |
@Autowired
|
| 22860 |
ashik.ali |
68 |
private FofoOrderItemRepository fofoLineItemRepository;
|
| 21985 |
kshitij.so |
69 |
|
| 21612 |
ashik.ali |
70 |
@Autowired
|
| 22860 |
ashik.ali |
71 |
private PaymentOptionRepository paymentOptionRepository;
|
| 21985 |
kshitij.so |
72 |
|
| 21680 |
ashik.ali |
73 |
@Autowired
|
| 22860 |
ashik.ali |
74 |
private FofoOrderRepository fofoOrderRepository;
|
| 21985 |
kshitij.so |
75 |
|
| 21680 |
ashik.ali |
76 |
@Autowired
|
| 22860 |
ashik.ali |
77 |
private CustomerAddressRepository customerAddressRepository;
|
| 22217 |
ashik.ali |
78 |
|
|
|
79 |
@Autowired
|
| 22860 |
ashik.ali |
80 |
private InsurancePolicyRepository insurancePolicyRepository;
|
| 21985 |
kshitij.so |
81 |
|
| 21896 |
ashik.ali |
82 |
@Autowired
|
| 22860 |
ashik.ali |
83 |
private CookiesProcessor cookiesProcessor;
|
| 22069 |
ashik.ali |
84 |
|
|
|
85 |
@Autowired
|
| 22860 |
ashik.ali |
86 |
private PricingService pricingService;
|
| 22244 |
ashik.ali |
87 |
|
| 22354 |
ashik.ali |
88 |
@Autowired
|
| 22860 |
ashik.ali |
89 |
private OrderService orderService;
|
| 22244 |
ashik.ali |
90 |
|
| 22354 |
ashik.ali |
91 |
|
|
|
92 |
@Autowired
|
| 22860 |
ashik.ali |
93 |
private ResponseSender<?> responseSender;
|
| 22689 |
amit.gupta |
94 |
|
| 21985 |
kshitij.so |
95 |
@RequestMapping(value = "/order")
|
| 22927 |
ashik.ali |
96 |
public String orderIndex(HttpServletRequest request, @RequestParam(name = "cartData") String cartData, Model model) throws ProfitMandiBusinessException{
|
|
|
97 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);;
|
|
|
98 |
|
| 22860 |
ashik.ali |
99 |
List<CartFofo> cartItems = orderService.cartCheckout(cartData);
|
|
|
100 |
Set<Integer> itemIds = new HashSet<>();
|
|
|
101 |
for(CartFofo cartFofo : cartItems){
|
|
|
102 |
itemIds.add(cartFofo.getItemId());
|
| 21985 |
kshitij.so |
103 |
}
|
| 22860 |
ashik.ali |
104 |
Map<Integer, PriceModel> mopPriceMap = pricingService.getPurchasePriceMopPriceNotFound(itemIds, loginDetails.getFofoId());
|
|
|
105 |
LOGGER.info("mopPriceMap {}", mopPriceMap);
|
|
|
106 |
model.addAttribute("cartObj", cartItems);
|
|
|
107 |
model.addAttribute("mopPriceMap", mopPriceMap);
|
|
|
108 |
return "order-index";
|
| 21985 |
kshitij.so |
109 |
}
|
| 22660 |
ashik.ali |
110 |
|
| 21612 |
ashik.ali |
111 |
|
| 22244 |
ashik.ali |
112 |
@RequestMapping(value = "/insurancePrices", method = RequestMethod.GET)
|
| 22927 |
ashik.ali |
113 |
public ResponseEntity<?> getInsurancePrices(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PRICE) float price) throws ProfitMandiBusinessException{
|
| 22244 |
ashik.ali |
114 |
LOGGER.info("Request received at url : {}", request.getRequestURI());
|
| 22927 |
ashik.ali |
115 |
Set<Float> prices = new HashSet<>();
|
|
|
116 |
prices.add(price);
|
|
|
117 |
return responseSender.ok(pricingService.getInsurancePrices(prices, ProfitMandiConstants.GADGET_COPS));
|
| 22244 |
ashik.ali |
118 |
}
|
|
|
119 |
|
|
|
120 |
|
| 21985 |
kshitij.so |
121 |
@RequestMapping(value = "/get-order", method = RequestMethod.GET)
|
| 22927 |
ashik.ali |
122 |
public String getOrder(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId, Model model) throws ProfitMandiBusinessException{
|
|
|
123 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
124 |
FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndOrderId(fofoDetails.getFofoId(), orderId);
|
|
|
125 |
List<FofoOrderItem> fofoLineItems = fofoLineItemRepository.selectByOrderId(fofoOrder.getId());
|
|
|
126 |
CustomerAddress customerAddress = customerAddressRepository.selectById(fofoOrder.getCustomerAddressId());
|
|
|
127 |
Customer customer = customerRepository.selectById(fofoOrder.getCustomerId());
|
|
|
128 |
customerAddress.setPhoneNumber(customer.getMobileNumber());
|
|
|
129 |
List<PaymentOption> paymentOptions = paymentOptionRepository.selectByOrderId(fofoOrder.getId());
|
|
|
130 |
List<InsurancePolicy> insurancePolicies = insurancePolicyRepository.selectByRetailerIdInvoiceNumber(fofoOrder.getFofoId(), fofoOrder.getInvoiceNumber());
|
|
|
131 |
model.addAttribute("fofoOrder", fofoOrder);
|
|
|
132 |
model.addAttribute("fofoLineItems", fofoLineItems);
|
|
|
133 |
model.addAttribute("customerBillingAddress", orderService.getBillingAddress(customerAddress));
|
|
|
134 |
model.addAttribute("customerBillingAddressObj", customerAddress);
|
|
|
135 |
model.addAttribute("paymentOptions", paymentOptions);
|
|
|
136 |
model.addAttribute("insurancePolicies", insurancePolicies);
|
|
|
137 |
return "order-details";
|
| 21985 |
kshitij.so |
138 |
}
|
| 22244 |
ashik.ali |
139 |
|
|
|
140 |
|
|
|
141 |
@RequestMapping(value = "/saleDetails", method = RequestMethod.GET)
|
| 22927 |
ashik.ali |
142 |
public String getSaleDetails(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId, Model model) throws ProfitMandiBusinessException{
|
|
|
143 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
144 |
|
|
|
145 |
FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndOrderId(fofoDetails.getFofoId(), orderId);
|
|
|
146 |
List<FofoOrderItem> fofoLineItems = fofoLineItemRepository.selectByOrderId(fofoOrder.getId());
|
|
|
147 |
CustomerAddress customerAddress = customerAddressRepository.selectById(fofoOrder.getCustomerAddressId());
|
|
|
148 |
List<PaymentOption> paymentOptions = paymentOptionRepository.selectByOrderId(fofoOrder.getId());
|
|
|
149 |
List<InsurancePolicy> insurancePolicies = insurancePolicyRepository.selectByRetailerIdInvoiceNumber(fofoOrder.getFofoId(), fofoOrder.getInvoiceNumber());
|
|
|
150 |
model.addAttribute("fofoOrder", fofoOrder);
|
|
|
151 |
model.addAttribute("fofoLineItems", fofoLineItems);
|
|
|
152 |
model.addAttribute("customerBillingAddress", orderService.getBillingAddress(customerAddress));
|
|
|
153 |
model.addAttribute("customerBillingAddressObj", customerAddress);
|
|
|
154 |
model.addAttribute("paymentOptions", paymentOptions);
|
|
|
155 |
model.addAttribute("insurancePolicies", insurancePolicies);
|
|
|
156 |
return "sale-details";
|
| 22244 |
ashik.ali |
157 |
}
|
|
|
158 |
|
| 21985 |
kshitij.so |
159 |
|
|
|
160 |
@RequestMapping(value = "/create-order", method = RequestMethod.POST)
|
| 22927 |
ashik.ali |
161 |
public String createOrder(HttpServletRequest request, @RequestBody CreateOrderRequest createOrderRequest, Model model) throws ProfitMandiBusinessException{
|
| 22064 |
ashik.ali |
162 |
LOGGER.info("request at uri {} body {}", request.getRequestURI(), createOrderRequest);
|
| 22927 |
ashik.ali |
163 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
164 |
|
| 22860 |
ashik.ali |
165 |
int fofoOrderId = orderService.createOrder(createOrderRequest, fofoDetails.getFofoId());
|
| 22280 |
ashik.ali |
166 |
LOGGER.info("Order has been created successfully...");
|
| 22860 |
ashik.ali |
167 |
return "redirect:/get-order/?orderId="+fofoOrderId;
|
| 21612 |
ashik.ali |
168 |
}
|
| 21985 |
kshitij.so |
169 |
|
|
|
170 |
|
| 21689 |
ashik.ali |
171 |
@RequestMapping(value = "/generateInvoice")
|
| 22927 |
ashik.ali |
172 |
public ResponseEntity<?> generateInvoice(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId) throws ProfitMandiBusinessException{
|
| 22064 |
ashik.ali |
173 |
LOGGER.info("Request received at url {} with params [{}={}] ", request.getRequestURI(), ProfitMandiConstants.ORDER_ID, orderId);
|
| 22139 |
amit.gupta |
174 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
| 22860 |
ashik.ali |
175 |
|
|
|
176 |
PdfModel pdfModel = orderService.getInvoicePdfModel(fofoDetails.getFofoId(), orderId);
|
| 22026 |
ashik.ali |
177 |
|
| 21689 |
ashik.ali |
178 |
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
179 |
PdfUtils.generateAndWrite(pdfModel, byteArrayOutputStream);
|
|
|
180 |
LOGGER.info("Pdf Stream length {}", byteArrayOutputStream.toByteArray().length);
|
| 22026 |
ashik.ali |
181 |
final HttpHeaders headers=new HttpHeaders();
|
|
|
182 |
headers.setContentType(MediaType.APPLICATION_PDF);
|
| 22860 |
ashik.ali |
183 |
headers.set("Content-disposition", "inline; filename=invoice-" + pdfModel.getInvoiceNumber() + ".pdf");
|
| 22026 |
ashik.ali |
184 |
headers.setContentLength(byteArrayOutputStream.toByteArray().length);
|
|
|
185 |
final InputStream inputStream=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
|
|
|
186 |
final InputStreamResource inputStreamResource=new InputStreamResource(inputStream);
|
|
|
187 |
return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
|
| 21689 |
ashik.ali |
188 |
}
|
| 22244 |
ashik.ali |
189 |
|
|
|
190 |
@RequestMapping(value = "/saleHistory")
|
| 22927 |
ashik.ali |
191 |
public String saleHistory(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.START_TIME, required = false) String startTimeString, @RequestParam(name = ProfitMandiConstants.END_TIME, required = false) String endTimeString, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, @RequestParam(name = ProfitMandiConstants.INVOICE_NUMBER, defaultValue = "") String invoiceNumber, @RequestParam(name = "searchType",defaultValue="") String searchType, Model model) throws ProfitMandiBusinessException{
|
|
|
192 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
| 22244 |
ashik.ali |
193 |
|
|
|
194 |
LocalDateTime startDateTime = StringUtils.toDateTime(startTimeString);
|
|
|
195 |
LocalDateTime endDateTime = StringUtils.toDateTime(endTimeString);
|
|
|
196 |
long countItems = 0;
|
|
|
197 |
List<FofoOrder> fofoOrders = new ArrayList<>();
|
| 22284 |
ashik.ali |
198 |
if(searchType.equalsIgnoreCase(ProfitMandiConstants.INVOICE_NUMBER) && !invoiceNumber.isEmpty()){
|
| 22244 |
ashik.ali |
199 |
try {
|
|
|
200 |
FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndInvoiceNumber(loginDetails.getFofoId(), invoiceNumber);
|
|
|
201 |
fofoOrders.add(fofoOrder);
|
|
|
202 |
} catch (ProfitMandiBusinessException e) {
|
|
|
203 |
LOGGER.info("Sale history not found : ", e);
|
|
|
204 |
}
|
|
|
205 |
}else{
|
| 22860 |
ashik.ali |
206 |
fofoOrders = fofoOrderRepository.selectByFofoId(loginDetails.getFofoId(), startDateTime, endDateTime, offset, limit);
|
| 22244 |
ashik.ali |
207 |
countItems = fofoOrderRepository.selectCount(loginDetails.getFofoId(), startDateTime, endDateTime, invoiceNumber, searchType);
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
model.addAttribute("saleHistories", fofoOrders);
|
| 22551 |
ashik.ali |
211 |
model.addAttribute("start", offset + 1);
|
|
|
212 |
model.addAttribute("size", countItems);
|
|
|
213 |
model.addAttribute("searchType", searchType);
|
| 22244 |
ashik.ali |
214 |
model.addAttribute(ProfitMandiConstants.START_TIME, startTimeString);
|
|
|
215 |
model.addAttribute(ProfitMandiConstants.END_TIME, endTimeString);
|
|
|
216 |
if (fofoOrders.size() < limit){
|
| 22551 |
ashik.ali |
217 |
model.addAttribute("end", offset + fofoOrders.size());
|
| 22244 |
ashik.ali |
218 |
}
|
|
|
219 |
else{
|
| 22551 |
ashik.ali |
220 |
model.addAttribute("end", offset + limit);
|
| 22244 |
ashik.ali |
221 |
}
|
|
|
222 |
|
|
|
223 |
return "sale-history";
|
|
|
224 |
}
|
| 22291 |
ashik.ali |
225 |
|
|
|
226 |
|
|
|
227 |
@RequestMapping(value = "/getPaginatedSaleHistory")
|
| 22927 |
ashik.ali |
228 |
public String getSaleHistoryPaginated(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.START_TIME, required = false) String startTimeString, @RequestParam(name = ProfitMandiConstants.END_TIME, required = false) String endTimeString, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue="10") int limit, @RequestParam(name = ProfitMandiConstants.INVOICE_NUMBER, defaultValue="") String invoiceNumber, @RequestParam(name = "searchType", defaultValue = "") String searchType, Model model) throws ProfitMandiBusinessException{
|
|
|
229 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
230 |
|
| 22291 |
ashik.ali |
231 |
LocalDateTime startDateTime = StringUtils.toDateTime(startTimeString);
|
|
|
232 |
LocalDateTime endDateTime = StringUtils.toDateTime(endTimeString);
|
| 21985 |
kshitij.so |
233 |
|
| 22291 |
ashik.ali |
234 |
List<FofoOrder> saleHistories = fofoOrderRepository.selectByFofoId(loginDetails.getFofoId(), startDateTime, endDateTime, offset, limit);
|
|
|
235 |
model.addAttribute("saleHistories", saleHistories);
|
|
|
236 |
return "sale-history-paginated";
|
|
|
237 |
}
|
|
|
238 |
|
| 21612 |
ashik.ali |
239 |
}
|