Subversion Repositories SmartDukaan

Rev

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

Rev 22866 Rev 22889
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
-
 
3
import java.io.ByteArrayOutputStream;
-
 
4
import java.io.File;
-
 
5
import java.io.FileOutputStream;
-
 
6
import java.io.IOException;
-
 
7
import java.io.OutputStream;
-
 
8
import java.text.MessageFormat;
-
 
9
import java.time.LocalDate;
-
 
10
import java.time.format.DateTimeFormatter;
-
 
11
import java.util.Arrays;
-
 
12
import java.util.HashSet;
-
 
13
import java.util.List;
-
 
14
import java.util.Map;
-
 
15
import java.util.Set;
-
 
16
 
-
 
17
import javax.mail.internet.InternetAddress;
-
 
18
import javax.mail.internet.MimeMessage;
3
import javax.servlet.http.HttpServletRequest;
19
import javax.servlet.http.HttpServletRequest;
4
 
20
 
-
 
21
import org.apache.commons.io.FileUtils;
5
import org.slf4j.Logger;
22
import org.slf4j.Logger;
6
import org.slf4j.LoggerFactory;
23
import org.slf4j.LoggerFactory;
7
import org.springframework.beans.factory.annotation.Autowired;
24
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.http.ResponseEntity;
25
import org.springframework.http.ResponseEntity;
-
 
26
import org.springframework.mail.javamail.JavaMailSender;
-
 
27
import org.springframework.mail.javamail.MimeMessageHelper;
9
import org.springframework.stereotype.Controller;
28
import org.springframework.stereotype.Controller;
10
import org.springframework.transaction.annotation.Transactional;
29
import org.springframework.transaction.annotation.Transactional;
-
 
30
import org.springframework.web.bind.annotation.RequestBody;
11
import org.springframework.web.bind.annotation.RequestMapping;
31
import org.springframework.web.bind.annotation.RequestMapping;
12
import org.springframework.web.bind.annotation.RequestMethod;
32
import org.springframework.web.bind.annotation.RequestMethod;
13
 
33
 
-
 
34
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
-
 
35
import com.spice.profitmandi.common.model.CustomAddress;
-
 
36
import com.spice.profitmandi.common.model.CustomCustomer;
-
 
37
import com.spice.profitmandi.common.model.CustomInsurancePolicy;
-
 
38
import com.spice.profitmandi.common.model.CustomOrderItem;
-
 
39
import com.spice.profitmandi.common.model.CustomRetailer;
-
 
40
import com.spice.profitmandi.common.model.GadgetCopsDocumentInsuranceModel;
-
 
41
import com.spice.profitmandi.common.model.PdfModel;
14
import com.spice.profitmandi.common.model.ProfitMandiConstants;
42
import com.spice.profitmandi.common.model.ProfitMandiConstants;
-
 
43
import com.spice.profitmandi.common.util.InsuranceUtils;
-
 
44
import com.spice.profitmandi.common.util.PdfUtils;
-
 
45
import com.spice.profitmandi.common.util.StringUtils;
-
 
46
import com.spice.profitmandi.common.util.Utils;
15
import com.spice.profitmandi.common.web.util.ResponseSender;
47
import com.spice.profitmandi.common.web.util.ResponseSender;
-
 
48
import com.spice.profitmandi.dao.entity.dtr.Document;
-
 
49
import com.spice.profitmandi.dao.entity.dtr.GadgetCopsInsuranceCalc;
-
 
50
import com.spice.profitmandi.dao.entity.dtr.InsurancePolicy;
-
 
51
import com.spice.profitmandi.dao.entity.dtr.InsuranceProvider;
-
 
52
import com.spice.profitmandi.dao.entity.dtr.PolicyNumberGenerationSequence;
-
 
53
import com.spice.profitmandi.dao.entity.dtr.ThirdPartyInvoiceSequence;
-
 
54
import com.spice.profitmandi.dao.enumuration.dtr.ThirdParty;
-
 
55
import com.spice.profitmandi.dao.model.UserCart;
-
 
56
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
16
import com.spice.profitmandi.dao.repository.dtr.GadgetCopsInsuranceCalcRepository;
57
import com.spice.profitmandi.dao.repository.dtr.GadgetCopsInsuranceCalcRepository;
-
 
58
import com.spice.profitmandi.dao.repository.dtr.InsurancePolicyRepository;
-
 
59
import com.spice.profitmandi.dao.repository.dtr.InsuranceProviderRepository;
-
 
60
import com.spice.profitmandi.dao.repository.dtr.PolicyNumberGenerationSequenceRepository;
-
 
61
import com.spice.profitmandi.dao.repository.dtr.ThirdPartyInvoiceSequenceRepository;
-
 
62
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
-
 
63
import com.spice.profitmandi.dao.repository.fofo.InvoiceNumberGenerationSequenceRepository;
-
 
64
import com.spice.profitmandi.service.pricing.PricingService;
-
 
65
import com.spice.profitmandi.service.wallet.WalletService;
-
 
66
 
-
 
67
import in.shop2020.model.v1.order.WalletReferenceType;
17
 
68
 
18
@Controller
69
@Controller
19
@Transactional(rollbackFor=Throwable.class)
70
@Transactional(rollbackFor = Throwable.class)
20
public class InsuranceController {
71
public class InsuranceController {
-
 
72
	private static final Logger LOGGER = LoggerFactory.getLogger(InsuranceController.class);
-
 
73
	private static final String gadgetCopsFilePath = "/GadgetCops";
-
 
74
 
-
 
75
	@Autowired
-
 
76
	private GadgetCopsInsuranceCalcRepository gadgetCopsInsuranceCalcRepository;
-
 
77
 
-
 
78
	@RequestMapping(value = ProfitMandiConstants.URL_INSURANCE_GADGET_COPS_MAPPING, method = RequestMethod.GET)
-
 
79
	public ResponseEntity<?> getById(HttpServletRequest request) {
-
 
80
		LOGGER.info("requested url : " + request.getRequestURL().toString());
-
 
81
		return responseSender.ok(gadgetCopsInsuranceCalcRepository.selectAll());
-
 
82
	}
21
 
83
 
22
	@Autowired
84
	@Autowired
23
	ResponseSender<?> responseSender;
85
	ResponseSender<?> responseSender;
-
 
86
 
-
 
87
	@Autowired
-
 
88
	ThirdPartyInvoiceSequenceRepository thirdPartyInvoiceSequenceRepository;
-
 
89
 
-
 
90
	@Autowired
-
 
91
	DocumentRepository documentRepository;
-
 
92
 
-
 
93
	@Autowired
-
 
94
	InvoiceNumberGenerationSequenceRepository invoiceNumberGenerationSequenceRepository;
24
	
95
	
-
 
96
	@Autowired
25
	private static final Logger LOGGER = LoggerFactory.getLogger(InsuranceController.class);
97
	InsurancePolicyRepository insurancePolicyRepository;
-
 
98
 
-
 
99
	@Autowired
-
 
100
	PricingService pricingService;
26
	
101
	
27
	@Autowired
102
	@Autowired
-
 
103
	JavaMailSender mailSender;
-
 
104
 
-
 
105
	@Autowired
28
	private GadgetCopsInsuranceCalcRepository gadgetCopsInsuranceCalcRepository;
106
	InsuranceProviderRepository insuranceProviderRepository;
-
 
107
 
-
 
108
	@Autowired
-
 
109
	UserAccountRepository userAccountRepository;
29
	
110
	
-
 
111
	@Autowired
-
 
112
	WalletService walletService;
-
 
113
 
-
 
114
	@Autowired
-
 
115
	PolicyNumberGenerationSequenceRepository policyNumberGenerationSequenceRepository;
-
 
116
 
-
 
117
	@Autowired
-
 
118
 
30
	@RequestMapping(value = ProfitMandiConstants.URL_INSURANCE_GADGET_COPS_MAPPING, method=RequestMethod.GET)
119
	@RequestMapping(value = ProfitMandiConstants.URL_DAMAGE_INSURANCE, method = RequestMethod.POST)
31
	public ResponseEntity<?> getById(HttpServletRequest request){
120
	public ResponseEntity<?> createTheftProtection(HttpServletRequest request,
-
 
121
			@RequestBody GadgetCopsDocumentInsuranceModel insuranceModel) throws Throwable {
-
 
122
		if (insuranceModel.validate()) {
-
 
123
			int userId = (int) request.getAttribute("userId");
-
 
124
			UserCart uc = userAccountRepository.getUserCart(userId);
-
 
125
			PolicyNumberGenerationSequence policyNumberGenerationSequence = null;
-
 
126
			try {
-
 
127
				policyNumberGenerationSequence = policyNumberGenerationSequenceRepository.select();
-
 
128
				policyNumberGenerationSequence.setSequence(policyNumberGenerationSequence.getSequence() + 1);
-
 
129
				policyNumberGenerationSequenceRepository.persist(policyNumberGenerationSequence);
-
 
130
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
-
 
131
				policyNumberGenerationSequence = new PolicyNumberGenerationSequence();
-
 
132
				policyNumberGenerationSequence.setSequence(1);
-
 
133
				policyNumberGenerationSequenceRepository.persist(policyNumberGenerationSequence);
-
 
134
			}
-
 
135
			String policyNumber = StringUtils.generatePolicyNumber(ProfitMandiConstants.POLICY_NUMBER_PREFIX,
-
 
136
					policyNumberGenerationSequence.getSequence());
-
 
137
 
-
 
138
			InsuranceProvider insuranceProvider = insuranceProviderRepository
-
 
139
					.selectByName(ProfitMandiConstants.GADGET_COPS);
-
 
140
			Set<Float> devicePriceSet = new HashSet<>();
-
 
141
			devicePriceSet.add(insuranceModel.getPrice());
-
 
142
			Map<Float, GadgetCopsInsuranceCalc> insurancePricesMap = pricingService.getInsurancePrices(devicePriceSet,
-
 
143
					ProfitMandiConstants.GADGET_COPS);
-
 
144
			InsurancePolicy insurancePolicy = new InsurancePolicy();
-
 
145
			insurancePolicy.setInvoiceNumber(insuranceModel.getInvoiceNumber());
-
 
146
			insurancePolicy.setRetailerId(uc.getUserId());
-
 
147
			insurancePolicy.setPurchaseAmount(insurancePricesMap.get(insuranceModel.getPrice()).getDealerPrice());
-
 
148
			insurancePolicy.setSaleAmount(insurancePricesMap.get(insuranceModel.getPrice()).getSellingPrice());
-
 
149
			insurancePolicy.setSellingPrice(insuranceModel.getPrice());
-
 
150
			insurancePolicy.setSerialNumber(insuranceModel.getSerialNumber());
-
 
151
			insurancePolicy.setModelName(insuranceModel.getModelName());
-
 
152
			insurancePolicy.setBrand(insuranceModel.getBrand());
-
 
153
			insurancePolicy.setPolicyNumber(policyNumber);
-
 
154
			insurancePolicy.setProviderId(insuranceProvider.getId());
-
 
155
			insurancePolicy.setCustomerFirstName(insuranceModel.getCustomerFirstName());
-
 
156
			insurancePolicy.setCustomerLastName(insuranceModel.getCustomerFirstName());
-
 
157
			insurancePolicy.setCustomerMobileNumber(insuranceModel.getCustomerMobileNumber());
-
 
158
			insurancePolicy.setCustomerEmailId(insuranceModel.getCustomerEmailId());
-
 
159
			insurancePolicy.setCustomerDateOfBirth(insuranceModel.getCustomerDateOfBirth());
-
 
160
			insurancePolicy.setCustomerAddress1(insuranceModel.getCustomerAddress1());
-
 
161
			insurancePolicy.setCustomerAddress2(insuranceModel.getCustomerAddress2());
-
 
162
			insurancePolicy.setCustomerCity(insuranceModel.getCustomerCity());
-
 
163
			insurancePolicy.setCustomerPinCode(insuranceModel.getCustomerPinCode());
-
 
164
			insurancePolicy.setCustomerState(insuranceModel.getCustomerState());
-
 
165
			insurancePolicyRepository.persist(insurancePolicy);
-
 
166
			
-
 
167
			try{
-
 
168
				InsuranceUtils.submitToGadgetCops(insuranceModel);
-
 
169
				insurancePolicy.setPosted(true);
-
 
170
			}catch (ProfitMandiBusinessException profitMandiBusinessException) {
-
 
171
				LOGGER.info("Unable to submit insurance policy details to {}", insuranceProvider.getName(), profitMandiBusinessException);
-
 
172
			}
-
 
173
			insurancePolicyRepository.persist(insurancePolicy);			
-
 
174
			
-
 
175
			walletService.consumeAmountFromWallet(uc.getUserId(), insurancePolicy.getId(), WalletReferenceType.DAMAGE_PROTECTION, 
-
 
176
					"Purchased Damage Protection policy for " + insurancePolicy.getCustomerFirstName() , insurancePolicy.getSaleAmount());
-
 
177
 
-
 
178
			PdfModel pdfModel = this.getInvoicePdfModel(insurancePolicy);
-
 
179
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
-
 
180
			PdfUtils.generateAndWrite(pdfModel, byteArrayOutputStream);
-
 
181
 
-
 
182
			Document deviceDocument = documentRepository.selectById(insuranceModel.getDeviceImageDocumentId());
-
 
183
			File deviceImg = new File(deviceDocument.getPath() + deviceDocument.getName());
-
 
184
			Document invoiceDocument = documentRepository.selectById(insuranceModel.getDeviceImageDocumentId());
-
 
185
			File deviceInvoiceImg = new File(invoiceDocument.getPath() + invoiceDocument.getName());
-
 
186
 
-
 
187
			String messageText = MessageFormat.format(
-
 
188
					"Dear {0}, Thank you for purchasing Damage Protection Plan. Your Policy number is {1}",
-
 
189
					insurancePolicy.getCustomerFirstName(), insurancePolicy.getPolicyNumber());
-
 
190
			Utils.sendSms(messageText, insurancePolicy.getCustomerMobileNumber());
-
 
191
			List<File> attachments = this.savePolicyDocs(insurancePolicy.getPolicyNumber(), byteArrayOutputStream, deviceImg, deviceInvoiceImg);
-
 
192
			messageText += "\n Please find your Invoice, and documents submitted by you.";
-
 
193
			String subject = "Your Gadget Cop damage Protection";
-
 
194
			this.sendMailWithAttachments(insuranceModel.getCustomerEmailId() , subject, messageText, attachments);
-
 
195
		}
32
		LOGGER.info("requested url : "+request.getRequestURL().toString());
196
		LOGGER.info("requested url : " + request.getRequestURL().toString());
33
		return responseSender.ok(gadgetCopsInsuranceCalcRepository.selectAll());
197
		return responseSender.ok(true);
34
	}
198
	}
35
	
199
	
-
 
200
	private void sendMailWithAttachments(String email, String subject, String body, List<File> attachments) throws Exception {
-
 
201
 
-
 
202
    	MimeMessage message = mailSender.createMimeMessage();
-
 
203
    	MimeMessageHelper helper = new MimeMessageHelper(message);
-
 
204
    	helper.setSubject("New Contact Us message");
-
 
205
    	helper.setText(body);
-
 
206
    	String[] cc = {"amit.gupta@shop2020.in"};
-
 
207
    	//String[] cc = {"amit.gupta@shop2020.in"};
-
 
208
    	helper.setCc(cc);
-
 
209
    	InternetAddress senderAddress = new InternetAddress("noreply@profitmandi.com", "ProfitMandi Admin");
-
 
210
    	helper.setTo("help@profitmandi.com");
-
 
211
    	helper.setFrom(senderAddress);
-
 
212
    	for (File file : attachments) {
-
 
213
    		helper.addAttachment(file.getName(), file);
-
 
214
    	}
-
 
215
    	mailSender.send(message);
-
 
216
		
-
 
217
	}
-
 
218
 
-
 
219
	private List<File> savePolicyDocs(String policyNumber, ByteArrayOutputStream pdfInvoiceStream, File deviceImg,
-
 
220
			File deviceInvoiceImg) throws Exception {
-
 
221
		// save file to gadgetCops/policyname
-
 
222
		String policyFolderPath = gadgetCopsFilePath + File.separator + policyNumber;
-
 
223
		File destDeviceInvoiceImg = new File(policyFolderPath + File.separator + "deviceInvoice.jpg");
-
 
224
		File destDeviceImg = new File(policyFolderPath + File.separator + "device.jpg");
-
 
225
		FileUtils.copyFile(deviceImg, destDeviceImg);
-
 
226
		FileUtils.copyFile(deviceInvoiceImg, destDeviceInvoiceImg);
-
 
227
 
-
 
228
		OutputStream outStream = null;
-
 
229
		try {
-
 
230
			outStream = new FileOutputStream(policyFolderPath + File.separator + "invoice.pdf");
-
 
231
			pdfInvoiceStream.writeTo(outStream);
-
 
232
		} catch (IOException e) {
-
 
233
			e.printStackTrace();
-
 
234
		} finally {
-
 
235
			outStream.close();
-
 
236
			pdfInvoiceStream.close();
-
 
237
		}
-
 
238
		
-
 
239
		File pdfInvoice = new File(policyFolderPath + File.separator + "invoice.pdf");
-
 
240
		return Arrays.asList(pdfInvoice, destDeviceImg, destDeviceInvoiceImg);
-
 
241
	}
-
 
242
 
-
 
243
	public String getFormattedDate(LocalDate localDate) {
-
 
244
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-YYYY HH:mm");
-
 
245
		return localDate.format(formatter);
-
 
246
	}
-
 
247
 
-
 
248
	private PdfModel getInvoicePdfModel(InsurancePolicy insurancePolicy) throws ProfitMandiBusinessException {
-
 
249
 
-
 
250
		PdfModel pdfModel = new PdfModel();
-
 
251
		pdfModel.setAuther("profitmandi");
-
 
252
		pdfModel.setTitle("Retailer Invoice");
-
 
253
		pdfModel.setInvoiceDate(getFormattedDate(LocalDate.now()));
-
 
254
 
-
 
255
		// insurance calculation
-
 
256
		Set<CustomInsurancePolicy> customInsurancePolicies = new HashSet<>();
-
 
257
		final float totalInsuranceTaxRate = 18;
-
 
258
 
-
 
259
		float taxableInsurancePrice = insurancePolicy.getSaleAmount() / (1 + totalInsuranceTaxRate / 100);
-
 
260
		CustomInsurancePolicy customInsurancePolicy = new CustomInsurancePolicy();
-
 
261
		customInsurancePolicy.setDescription("Damage Protection Plan for device IMEI #"
-
 
262
				+ insurancePolicy.getSerialNumber() + "\n Certificate No. " + insurancePolicy.getPolicyNumber());
-
 
263
		customInsurancePolicy.setHsnCode("998716");
-
 
264
		customInsurancePolicy.setRate(taxableInsurancePrice);
-
 
265
		customInsurancePolicy.setIgstRate(18);
-
 
266
		customInsurancePolicy.setIgstAmount(taxableInsurancePrice * 18 / 100);
-
 
267
		customInsurancePolicy.setCgstRate(9);
-
 
268
		customInsurancePolicy.setCgstAmount(taxableInsurancePrice * 9 / 100);
-
 
269
		customInsurancePolicy.setSgstRate(9);
-
 
270
		customInsurancePolicy.setSgstAmount(taxableInsurancePrice * 9 / 100);
-
 
271
		customInsurancePolicy.setNetAmount(insurancePolicy.getSaleAmount());
-
 
272
		customInsurancePolicies.add(customInsurancePolicy);
-
 
273
 
-
 
274
		pdfModel.setInsurancePolicies(customInsurancePolicies);
-
 
275
		CustomCustomer customCustomer = new CustomCustomer();
-
 
276
		customCustomer.setFirstName(insurancePolicy.getCustomerFirstName());
-
 
277
		customCustomer.setLastName(insurancePolicy.getCustomerLastName());
-
 
278
		customCustomer.setEmailId(insurancePolicy.getCustomerEmailId());
-
 
279
		customCustomer.setMobileNumber(insurancePolicy.getCustomerMobileNumber());
-
 
280
 
-
 
281
		CustomAddress customAddress = new CustomAddress();
-
 
282
		customAddress.setName(insurancePolicy.getCustomerFirstName() + " " + insurancePolicy.getCustomerLastName());
-
 
283
		customAddress.setLine1(insurancePolicy.getCustomerAddress1());
-
 
284
		customAddress.setLine2(insurancePolicy.getCustomerAddress2());
-
 
285
		customAddress.setLandmark("");
-
 
286
		customAddress.setCity(insurancePolicy.getCustomerCity());
-
 
287
		customAddress.setPinCode(insurancePolicy.getCustomerPinCode());
-
 
288
		customAddress.setState(insurancePolicy.getCustomerState());
-
 
289
		customAddress.setPhoneNumber(insurancePolicy.getCustomerMobileNumber());
-
 
290
		customCustomer.setAddress(customAddress);
-
 
291
		pdfModel.setCustomer(customCustomer);
-
 
292
 
-
 
293
		// TODO get invoice number for damageProtection provider
-
 
294
		pdfModel.setInvoiceNumber(thirdPartyInvoiceSequenceRepository.getNextSequence(ThirdParty.GADGET_COP));
-
 
295
		pdfModel.setTotalAmount(gadgetCopsInsuranceCalcRepository.selectByPrice(insurancePolicy.getSellingPrice()));
-
 
296
 
-
 
297
		// Here bill is generated on behalf of GadgetCop
-
 
298
		ThirdPartyInvoiceSequence damageProtectionProvider = thirdPartyInvoiceSequenceRepository
-
 
299
				.selectByThirdParty(ThirdParty.GADGET_COP);
-
 
300
		// Gadget Cop
-
 
301
		// Gadget cop mobile
-
 
302
		CustomRetailer customRetailer = new CustomRetailer();
-
 
303
		customRetailer.setBusinessName(damageProtectionProvider.getName());
-
 
304
		customRetailer.setMobileNumber(damageProtectionProvider.getMobileNumber());
-
 
305
		customRetailer.setGstNumber(damageProtectionProvider.getGstNumber());
-
 
306
		CustomAddress providerAddress = new CustomAddress();
-
 
307
		providerAddress.setCity(damageProtectionProvider.getCity());
-
 
308
		providerAddress.setLine1(damageProtectionProvider.getLine1());
-
 
309
		providerAddress.setLine2(damageProtectionProvider.getLine2());
-
 
310
		providerAddress.setPinCode(damageProtectionProvider.getPin());
-
 
311
		providerAddress.setPhoneNumber(damageProtectionProvider.getMobileNumber());
-
 
312
		providerAddress.setState(damageProtectionProvider.getState());
-
 
313
		pdfModel.setRetailer(customRetailer);
-
 
314
 
-
 
315
		Set<CustomOrderItem> customerFofoOrderItems = new HashSet<>();
-
 
316
		pdfModel.setOrderItems(customerFofoOrderItems);
-
 
317
		return pdfModel;
-
 
318
	}
-
 
319
 
36
}
320
}