Subversion Repositories SmartDukaan

Rev

Rev 21896 | Rev 21965 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 21896 Rev 21919
Line 10... Line 10...
10
import java.util.Map;
10
import java.util.Map;
11
import java.util.Set;
11
import java.util.Set;
12
 
12
 
13
import javax.servlet.http.HttpServletRequest;
13
import javax.servlet.http.HttpServletRequest;
14
import javax.servlet.http.HttpServletResponse;
14
import javax.servlet.http.HttpServletResponse;
15
import javax.transaction.Transactional;
-
 
16
 
15
 
17
import org.slf4j.Logger;
16
import org.slf4j.Logger;
18
import org.slf4j.LoggerFactory;
17
import org.slf4j.LoggerFactory;
19
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.core.io.InputStreamResource;
19
import org.springframework.core.io.InputStreamResource;
21
import org.springframework.http.HttpHeaders;
20
import org.springframework.http.HttpHeaders;
22
import org.springframework.http.HttpStatus;
21
import org.springframework.http.HttpStatus;
23
import org.springframework.http.MediaType;
22
import org.springframework.http.MediaType;
24
import org.springframework.http.ResponseEntity;
23
import org.springframework.http.ResponseEntity;
25
import org.springframework.stereotype.Controller;
24
import org.springframework.stereotype.Controller;
-
 
25
import org.springframework.transaction.annotation.Transactional;
26
import org.springframework.web.bind.annotation.RequestBody;
26
import org.springframework.web.bind.annotation.RequestBody;
27
import org.springframework.web.bind.annotation.RequestMapping;
27
import org.springframework.web.bind.annotation.RequestMapping;
28
import org.springframework.web.bind.annotation.RequestMethod;
28
import org.springframework.web.bind.annotation.RequestMethod;
29
import org.springframework.web.bind.annotation.RequestParam;
29
import org.springframework.web.bind.annotation.RequestParam;
30
 
30
 
Line 78... Line 78...
78
import com.spice.profitmandi.web.request.CustomPaymentOption;
78
import com.spice.profitmandi.web.request.CustomPaymentOption;
79
import com.spice.profitmandi.web.response.ItemIdQuantityAvailability;
79
import com.spice.profitmandi.web.response.ItemIdQuantityAvailability;
80
import com.spice.profitmandi.web.response.Quantity;
80
import com.spice.profitmandi.web.response.Quantity;
81
import com.spice.profitmandi.web.util.CookiesFetcher;
81
import com.spice.profitmandi.web.util.CookiesFetcher;
82
import com.spice.profitmandi.web.util.MVCResponseSender;
82
import com.spice.profitmandi.web.util.MVCResponseSender;
83
import com.sun.xml.internal.bind.v2.TODO;
-
 
84
 
83
 
85
@Controller
84
@Controller
86
@Transactional
85
@Transactional(rollbackFor = Throwable.class)
87
public class OrderController {
86
public class OrderController {
88
 
87
 
89
	private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);
88
	private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);
90
	
89
	
91
	@Autowired
90
	@Autowired
Line 149... Line 148...
149
	public String createOrder(HttpServletRequest request, @RequestBody CreateOrderRequest createOrderRequest)  throws Throwable{
148
	public String createOrder(HttpServletRequest request, @RequestBody CreateOrderRequest createOrderRequest)  throws Throwable{
150
		//FofoDetails fofoDetails = cookiesFetcher.getCookiesObject(request);
149
		//FofoDetails fofoDetails = cookiesFetcher.getCookiesObject(request);
151
		LOGGER.info("/order request {}", createOrderRequest);
150
		LOGGER.info("/order request {}", createOrderRequest);
152
		FofoDetails fofoDetails = new FofoDetails();
151
		FofoDetails fofoDetails = new FofoDetails();
153
		fofoDetails.setFofoId(963490);
152
		fofoDetails.setFofoId(963490);
-
 
153
		return this.createOrder(createOrderRequest, fofoDetails);
-
 
154
	}
-
 
155
	
-
 
156
	private String createOrder(CreateOrderRequest createOrderRequest, FofoDetails fofoDetails) throws ProfitMandiBusinessException{
154
		Set<Integer> itemIds = new HashSet<>();
157
		Set<Integer> itemIds = new HashSet<>();
155
		Map<Integer, Integer> itemIdQuantity = new HashMap<>(); //this is for error
158
		Map<Integer, Integer> itemIdQuantity = new HashMap<>(); //this is for error
156
		Map<Integer, CustomFofoLineItem> customFofoLineItemMap = new HashMap<>();
159
		Map<Integer, CustomFofoLineItem> customFofoLineItemMap = new HashMap<>();
157
		Map<Integer, Float> lineItemPrice = new HashMap<>(); //this is for pricing error
160
		Map<Integer, Float> lineItemPrice = new HashMap<>(); //this is for pricing error
158
		float totalAmount = 0;
161
		float totalAmount = 0;
Line 286... Line 289...
286
			LOGGER.error("invalid customer mobileNumber {} ", customCustomer.getMobileNumber());
289
			LOGGER.error("invalid customer mobileNumber {} ", customCustomer.getMobileNumber());
287
			return "error";
290
			return "error";
288
		}
291
		}
289
		
292
		
290
		Customer customer = null;
293
		Customer customer = null;
-
 
294
		boolean foundCustomer = false;
-
 
295
		if(customCustomer.getCustomerAddressId() > 0){
291
		try{
296
			try{
-
 
297
				customer = customerRepository.selectById(customCustomer.getCustomerAddressId());
-
 
298
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
-
 
299
				LOGGER.error("Error : ", profitMandiBusinessException);
292
			//TODO:remove email id
300
				foundCustomer = true;
-
 
301
			}
-
 
302
		}else{
-
 
303
			try{
293
			customer = customerRepository.selectByEmailIdAndMobileNumber(customCustomer.getEmailId(), customCustomer.getMobileNumber());
304
				customer = customerRepository.selectByMobileNumber(customCustomer.getMobileNumber());
294
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
305
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
295
			LOGGER.error("Error : ", profitMandiBusinessException);
306
				LOGGER.error("Error : ", profitMandiBusinessException);
-
 
307
				foundCustomer = true;
-
 
308
			}
-
 
309
		}
-
 
310
		LOGGER.error("Error occured : {}", foundCustomer);
-
 
311
		if(foundCustomer){
296
			customer = new Customer();
312
			customer = new Customer();
297
			customer.setName(customCustomer.getName());
313
			customer.setName(customCustomer.getName());
298
			customer.setEmailId(customCustomer.getEmailId());
314
			customer.setEmailId(customCustomer.getEmailId());
299
			customer.setMobileNumber(customCustomer.getMobileNumber());
315
			customer.setMobileNumber(customCustomer.getMobileNumber());
300
			customerRepository.persist(customer);
316
			customerRepository.persist(customer);
Line 322... Line 338...
322
			paymentOption.setType(customPaymentOption.getType());
338
			paymentOption.setType(customPaymentOption.getType());
323
			paymentOptionRepository.persist(paymentOption);
339
			paymentOptionRepository.persist(paymentOption);
324
		}
340
		}
325
		
341
		
326
		Address retailerAddress = addressRepository.selectById(retailerRegisteredAddressRepository.selectAddressIdByRetailerId(fofoDetails.getFofoId()));
342
		Address retailerAddress = addressRepository.selectById(retailerRegisteredAddressRepository.selectAddressIdByRetailerId(fofoDetails.getFofoId()));
327
		//
343
		
-
 
344
		Map<String, GstRate> gstRateMap = null;
-
 
345
		if(retailerAddress.getState().equals(customerAddress.getState())){
-
 
346
			LOGGER.info("inter gstRate = true");
-
 
347
			gstRateMap = Utils.getInterGstRates();
-
 
348
		}else{
328
		Map<String, GstRate> gstRateMap = Utils.getGstRates(retailerAddress.getState());
349
			gstRateMap = Utils.getGstRates(retailerAddress.getState());
-
 
350
		}
-
 
351
		LOGGER.info("gstRateMap {}", gstRateMap);
329
		for(CustomFofoLineItem customFofoLineItem : createOrderRequest.getFofoLineItems()){
352
		for(CustomFofoLineItem customFofoLineItem : createOrderRequest.getFofoLineItems()){
330
			FofoLineItem fofoLineItem = new FofoLineItem();
353
			FofoLineItem fofoLineItem = new FofoLineItem();
331
			fofoLineItem.setItemId(customFofoLineItem.getItemId());
354
			fofoLineItem.setItemId(customFofoLineItem.getItemId());
332
			fofoLineItem.setQuantity(customFofoLineItem.getQuantity());
355
			fofoLineItem.setQuantity(customFofoLineItem.getQuantity());
333
			fofoLineItem.setSellingPrice(customFofoLineItem.getSellingPrice());
356
			fofoLineItem.setSellingPrice(customFofoLineItem.getSellingPrice());
Line 336... Line 359...
336
			Item item = itemIdDetails.get(customFofoLineItem.getItemId());
359
			Item item = itemIdDetails.get(customFofoLineItem.getItemId());
337
			GstRate gstRate = gstRateMap.get(item.getHsnCode());
360
			GstRate gstRate = gstRateMap.get(item.getHsnCode());
338
			fofoLineItem.setIgstRate(gstRate.getIgstRate());
361
			fofoLineItem.setIgstRate(gstRate.getIgstRate());
339
			fofoLineItem.setCgstRate(gstRate.getCgstRate());
362
			fofoLineItem.setCgstRate(gstRate.getCgstRate());
340
			fofoLineItem.setSgstRate(gstRate.getSgstRate());
363
			fofoLineItem.setSgstRate(gstRate.getSgstRate());
-
 
364
			fofoLineItem.setHsnCode(gstRate.getHsnCode());
341
			fofoLineItem.setCost(itemIdPriceDropAmount.get(customFofoLineItem.getItemId()));
365
			fofoLineItem.setCost(itemIdPriceDropAmount.get(customFofoLineItem.getItemId()));
342
			fofoLineItem.setBrand(item.getBrand());
366
			fofoLineItem.setBrand(item.getBrand());
343
			fofoLineItem.setModelName(item.getModelName());
367
			fofoLineItem.setModelName(item.getModelName());
344
			fofoLineItem.setModelNumber(item.getModelNumber());
368
			fofoLineItem.setModelNumber(item.getModelNumber());
345
			fofoLineItem.setColor(item.getColor());
369
			fofoLineItem.setColor(item.getColor());
Line 399... Line 423...
399
						scanRecord.setQuantity(1);
423
						scanRecord.setQuantity(1);
400
						scanRecord.setType(ScanType.SALE);
424
						scanRecord.setType(ScanType.SALE);
401
						scanRecordRepository.persist(scanRecord);
425
						scanRecordRepository.persist(scanRecord);
402
					}
426
					}
403
				}
427
				}
404
			}
-
 
405
					
428
			}					
406
		}
429
		}
407
		return null;
430
		return null;
408
	}
431
	}
409
	
432
	
410
	
433
	
Line 509... Line 532...
509
		customCustomer.setMobileNumber(customer.getMobileNumber());
532
		customCustomer.setMobileNumber(customer.getMobileNumber());
510
		CustomerAddress customerAddress = customerAddressRepository.selectById(fofoOrder.getCustomerAddressId()); 
533
		CustomerAddress customerAddress = customerAddressRepository.selectById(fofoOrder.getCustomerAddressId()); 
511
		customCustomer.setAddress(this.createCustomAddress(customerAddress));
534
		customCustomer.setAddress(this.createCustomAddress(customerAddress));
512
		pdfModel.setCustomer(customCustomer);
535
		pdfModel.setCustomer(customCustomer);
513
		pdfModel.setInvoiceNumber(fofoOrder.getInvoiceNumber());
536
		pdfModel.setInvoiceNumber(fofoOrder.getInvoiceNumber());
514
		pdfModel.setTotalAmount(fofoOrder.getTotalAmount());
537
		//pdfModel.setTotalAmount(fofoOrder.getTotalAmount());
515
		
538
		
516
		Retailer retailer = retailerRepository.selectById(fofoDetails.getFofoId());
539
		Retailer retailer = retailerRepository.selectById(fofoDetails.getFofoId());
517
		User user = userRepository.selectById(userAccountRepository.selectUserIdByRetailerId(retailer.getId()));
540
		User user = userRepository.selectById(userAccountRepository.selectUserIdByRetailerId(retailer.getId()));
518
		CustomRetailer customRetailer = new CustomRetailer();
541
		CustomRetailer customRetailer = new CustomRetailer();
519
		customRetailer.setBusinessName(retailer.getName());
542
		customRetailer.setBusinessName(retailer.getName());
Line 527... Line 550...
527
		Set<CustomFofoOrderItem> customerFofoOrderItems = new HashSet<>();
550
		Set<CustomFofoOrderItem> customerFofoOrderItems = new HashSet<>();
528
		for(FofoLineItem fofoLineItem : fofoLineItems){
551
		for(FofoLineItem fofoLineItem : fofoLineItems){
529
			CustomFofoOrderItem customFofoOrderItem = new CustomFofoOrderItem();
552
			CustomFofoOrderItem customFofoOrderItem = new CustomFofoOrderItem();
530
			customFofoOrderItem.setAmount(fofoLineItem.getQuantity() * fofoLineItem.getSellingPrice());
553
			customFofoOrderItem.setAmount(fofoLineItem.getQuantity() * fofoLineItem.getSellingPrice());
531
			customFofoOrderItem.setDescription(fofoLineItem.getBrand() + " " + fofoLineItem.getModelName() + " " + fofoLineItem.getModelNumber() + " " + fofoLineItem.getColor());
554
			customFofoOrderItem.setDescription(fofoLineItem.getBrand() + " " + fofoLineItem.getModelName() + " " + fofoLineItem.getModelNumber() + " " + fofoLineItem.getColor());
-
 
555
			float totalTaxRate = fofoLineItem.getIgstRate() + fofoLineItem.getSgstRate() + fofoLineItem.getCgstRate();
-
 
556
			float taxableAmount = customFofoOrderItem.getAmount() / (1 + (totalTaxRate / 100));
532
			float itemTotal = customFofoOrderItem.getAmount();
557
			//float itemTotal = customFofoOrderItem.getAmount() + taxableAmount;
-
 
558
			//LOGGER.info("totalTaxRate ={}, taxableAmount ={}, itemTotal ={}", totalTaxRate, taxableAmount, itemTotal);
533
			customFofoOrderItem.setRate(fofoLineItem.getSellingPrice());
559
			customFofoOrderItem.setRate(fofoLineItem.getSellingPrice());
534
			customFofoOrderItem.setQuantity(fofoLineItem.getQuantity());
560
			customFofoOrderItem.setQuantity(fofoLineItem.getQuantity());
-
 
561
			float igstAmount = (taxableAmount * fofoLineItem.getIgstRate()) / 100;
-
 
562
			float cgstAmount = (taxableAmount * fofoLineItem.getCgstRate()) / 100;
-
 
563
			float sgstAmount = (taxableAmount * fofoLineItem.getSgstRate()) / 100;
-
 
564
			LOGGER.info("igst [{},{}], cgst [{},{}], sgst[{},{}]", fofoLineItem.getIgstRate(), igstAmount, fofoLineItem.getCgstRate(), cgstAmount, fofoLineItem.getSgstRate(), sgstAmount);
535
			customFofoOrderItem.setIgstRate(fofoLineItem.getIgstRate());
565
			customFofoOrderItem.setIgstRate(fofoLineItem.getIgstRate());
-
 
566
			customFofoOrderItem.setIgstAmount(igstAmount);
536
			customFofoOrderItem.setCgstRate(fofoLineItem.getIgstRate());
567
			customFofoOrderItem.setCgstRate(fofoLineItem.getCgstRate());
-
 
568
			customFofoOrderItem.setCgstAmount(cgstAmount);
537
			customFofoOrderItem.setSgstRate(fofoLineItem.getIgstRate());
569
			customFofoOrderItem.setSgstRate(fofoLineItem.getSgstRate());
-
 
570
			customFofoOrderItem.setSgstAmount(sgstAmount);
538
			customFofoOrderItem.setHsnCode(fofoLineItem.getHsnCode());
571
			customFofoOrderItem.setHsnCode(fofoLineItem.getHsnCode());
539
			customFofoOrderItem.setItemTotal(itemTotal);
572
			customFofoOrderItem.setItemTotal(customFofoOrderItem.getAmount() + igstAmount + cgstAmount + sgstAmount);
540
			customFofoOrderItem.setSerialNumbers(String.join(", ",this.toSerialNumbers(fofoLineItem.getFofoLineItemSerialNumbers())));
573
			customFofoOrderItem.setSerialNumbers(String.join(", ",this.toSerialNumbers(fofoLineItem.getFofoLineItemSerialNumbers())));
541
			customerFofoOrderItems.add(customFofoOrderItem);
574
			customerFofoOrderItems.add(customFofoOrderItem);
-
 
575
			pdfModel.setTotalAmount(pdfModel.getTotalAmount() + customFofoOrderItem.getItemTotal());
542
		}
576
		}
543
		pdfModel.setOrderItems(customerFofoOrderItems);
577
		pdfModel.setOrderItems(customerFofoOrderItems);
544
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
578
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
545
		PdfUtils.generateAndWrite(pdfModel, byteArrayOutputStream);
579
		PdfUtils.generateAndWrite(pdfModel, byteArrayOutputStream);
546
		//final MediaType mediaType=MediaType.parseMediaType(profilePhotoModel.getContentType().getValue());
580
		//final MediaType mediaType=MediaType.parseMediaType(profilePhotoModel.getContentType().getValue());
547
		LOGGER.info("Pdf Stream length {}", byteArrayOutputStream.toByteArray().length);
581
		LOGGER.info("Pdf Stream length {}", byteArrayOutputStream.toByteArray().length);
548
        final HttpHeaders headers=new HttpHeaders();
582
        final HttpHeaders headers=new HttpHeaders();
549
        headers.setContentType(MediaType.APPLICATION_PDF);
583
        headers.setContentType(MediaType.APPLICATION_PDF);
550
        //headers.set("Content-Disposition", "Order-pdf-"+custom);
584
        headers.set("Content-Disposition", "Order-"+orderId);
551
        headers.setContentLength(byteArrayOutputStream.toByteArray().length);
585
        headers.setContentLength(byteArrayOutputStream.toByteArray().length);
552
        final InputStream inputStream=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
586
        final InputStream inputStream=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
553
        final InputStreamResource inputStreamResource=new InputStreamResource(inputStream);
587
        final InputStreamResource inputStreamResource=new InputStreamResource(inputStream);
554
        return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
588
        return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
555
	}
589
	}