Subversion Repositories SmartDukaan

Rev

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

Rev 21886 Rev 21896
Line 4... Line 4...
4
import java.io.ByteArrayOutputStream;
4
import java.io.ByteArrayOutputStream;
5
import java.io.InputStream;
5
import java.io.InputStream;
6
import java.util.ArrayList;
6
import java.util.ArrayList;
7
import java.util.HashMap;
7
import java.util.HashMap;
8
import java.util.HashSet;
8
import java.util.HashSet;
9
import java.util.Iterator;
-
 
10
import java.util.List;
9
import java.util.List;
11
import java.util.Map;
10
import java.util.Map;
12
import java.util.Set;
11
import java.util.Set;
13
 
12
 
14
import javax.servlet.http.HttpServletRequest;
13
import javax.servlet.http.HttpServletRequest;
15
import javax.servlet.http.HttpServletResponse;
14
import javax.servlet.http.HttpServletResponse;
16
import javax.transaction.Transactional;
15
import javax.transaction.Transactional;
17
 
16
 
18
import org.json.JSONObject;
-
 
19
import org.slf4j.Logger;
17
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
18
import org.slf4j.LoggerFactory;
21
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.beans.factory.annotation.Autowired;
22
import org.springframework.core.io.InputStreamResource;
20
import org.springframework.core.io.InputStreamResource;
23
import org.springframework.http.HttpHeaders;
21
import org.springframework.http.HttpHeaders;
24
import org.springframework.http.HttpStatus;
22
import org.springframework.http.HttpStatus;
25
import org.springframework.http.MediaType;
23
import org.springframework.http.MediaType;
26
import org.springframework.http.ResponseEntity;
24
import org.springframework.http.ResponseEntity;
27
import org.springframework.stereotype.Controller;
25
import org.springframework.stereotype.Controller;
28
import org.springframework.ui.Model;
-
 
29
import org.springframework.web.bind.annotation.RequestBody;
26
import org.springframework.web.bind.annotation.RequestBody;
30
import org.springframework.web.bind.annotation.RequestMapping;
27
import org.springframework.web.bind.annotation.RequestMapping;
31
import org.springframework.web.bind.annotation.RequestMethod;
28
import org.springframework.web.bind.annotation.RequestMethod;
32
import org.springframework.web.bind.annotation.RequestParam;
29
import org.springframework.web.bind.annotation.RequestParam;
33
 
30
 
34
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
31
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
35
import com.spice.profitmandi.common.model.CartFofo;
-
 
36
import com.spice.profitmandi.common.model.CustomAddress;
32
import com.spice.profitmandi.common.model.CustomAddress;
37
import com.spice.profitmandi.common.model.CustomCustomer;
33
import com.spice.profitmandi.common.model.CustomCustomer;
38
import com.spice.profitmandi.common.model.CustomFofoLineItem;
34
import com.spice.profitmandi.common.model.CustomFofoLineItem;
39
import com.spice.profitmandi.common.model.CustomFofoOrderItem;
35
import com.spice.profitmandi.common.model.CustomFofoOrderItem;
40
import com.spice.profitmandi.common.model.CustomRetailer;
36
import com.spice.profitmandi.common.model.CustomRetailer;
-
 
37
import com.spice.profitmandi.common.model.GstRate;
41
import com.spice.profitmandi.common.model.PdfModel;
38
import com.spice.profitmandi.common.model.PdfModel;
42
import com.spice.profitmandi.common.model.ProfitMandiConstants;
39
import com.spice.profitmandi.common.model.ProfitMandiConstants;
43
import com.spice.profitmandi.common.util.PdfUtils;
40
import com.spice.profitmandi.common.util.PdfUtils;
44
import com.spice.profitmandi.common.util.StringUtils;
41
import com.spice.profitmandi.common.util.StringUtils;
-
 
42
import com.spice.profitmandi.common.util.Utils;
45
import com.spice.profitmandi.dao.entity.catalog.Item;
43
import com.spice.profitmandi.dao.entity.catalog.Item;
46
import com.spice.profitmandi.dao.entity.dtr.Retailer;
44
import com.spice.profitmandi.dao.entity.dtr.Retailer;
47
import com.spice.profitmandi.dao.entity.dtr.User;
45
import com.spice.profitmandi.dao.entity.dtr.User;
48
import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;
46
import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;
49
import com.spice.profitmandi.dao.entity.fofo.Customer;
47
import com.spice.profitmandi.dao.entity.fofo.Customer;
50
import com.spice.profitmandi.dao.entity.fofo.CustomerAddress;
48
import com.spice.profitmandi.dao.entity.fofo.CustomerAddress;
-
 
49
import com.spice.profitmandi.dao.entity.fofo.FofoItemId;
51
import com.spice.profitmandi.dao.entity.fofo.FofoLineItem;
50
import com.spice.profitmandi.dao.entity.fofo.FofoLineItem;
52
import com.spice.profitmandi.dao.entity.fofo.FofoLineItemSerialNumber;
51
import com.spice.profitmandi.dao.entity.fofo.FofoLineItemSerialNumber;
53
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
52
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
54
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
53
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
55
import com.spice.profitmandi.dao.entity.fofo.InvoiceNumberGenerationSequence;
54
import com.spice.profitmandi.dao.entity.fofo.InvoiceNumberGenerationSequence;
56
import com.spice.profitmandi.dao.entity.fofo.PaymentOption;
55
import com.spice.profitmandi.dao.entity.fofo.PaymentOption;
57
import com.spice.profitmandi.dao.entity.fofo.ScanRecord;
56
import com.spice.profitmandi.dao.entity.fofo.ScanRecord;
58
import com.spice.profitmandi.dao.entity.transaction.LineItem;
-
 
59
import com.spice.profitmandi.dao.entity.transaction.Order;
-
 
60
import com.spice.profitmandi.dao.entity.user.Address;
57
import com.spice.profitmandi.dao.entity.user.Address;
61
import com.spice.profitmandi.dao.enumuration.fofo.ScanType;
58
import com.spice.profitmandi.dao.enumuration.fofo.ScanType;
62
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
59
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
63
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
60
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
64
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
61
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
Line 81... Line 78...
81
import com.spice.profitmandi.web.request.CustomPaymentOption;
78
import com.spice.profitmandi.web.request.CustomPaymentOption;
82
import com.spice.profitmandi.web.response.ItemIdQuantityAvailability;
79
import com.spice.profitmandi.web.response.ItemIdQuantityAvailability;
83
import com.spice.profitmandi.web.response.Quantity;
80
import com.spice.profitmandi.web.response.Quantity;
84
import com.spice.profitmandi.web.util.CookiesFetcher;
81
import com.spice.profitmandi.web.util.CookiesFetcher;
85
import com.spice.profitmandi.web.util.MVCResponseSender;
82
import com.spice.profitmandi.web.util.MVCResponseSender;
-
 
83
import com.sun.xml.internal.bind.v2.TODO;
86
 
84
 
87
@Controller
85
@Controller
88
@Transactional
86
@Transactional
89
public class OrderController {
87
public class OrderController {
90
 
88
 
91
	private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);
89
	private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);
92
 
90
	
93
	@Autowired
91
	@Autowired
94
	OrderRepository orderRepository;
92
	OrderRepository orderRepository;
95
 
93
	
96
	@Autowired
94
	@Autowired
97
	InventoryItemRepository inventoryItemRepository;
95
	InventoryItemRepository inventoryItemRepository;
98
 
96
	
99
	@Autowired
97
	@Autowired
100
	CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
98
	CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
101
 
99
	
102
	@Autowired
100
	@Autowired
103
	InvoiceNumberGenerationSequenceRepository invoiceNumberGenerationSequenceRepository;
101
	InvoiceNumberGenerationSequenceRepository invoiceNumberGenerationSequenceRepository;
104
 
102
	
105
	@Autowired
103
	@Autowired
106
	CustomerRepository customerRepository;
104
	CustomerRepository customerRepository;
107
 
105
	
108
	@Autowired
106
	@Autowired
109
	AddressRepository addressRepository;
107
	AddressRepository addressRepository;
110
 
108
	
111
	@Autowired
109
	@Autowired
112
	FofoLineItemSerialNumberRepository fofoLineItemSerialNumberRepository;
110
	FofoLineItemSerialNumberRepository fofoLineItemSerialNumberRepository;
113
 
111
	
114
	@Autowired
112
	@Autowired
115
	FofoLineItemRepository fofoLineItemRepository;
113
	FofoLineItemRepository fofoLineItemRepository;
116
 
114
	
117
	@Autowired
115
	@Autowired
118
	PaymentOptionRepository paymentOptionRepository;
116
	PaymentOptionRepository paymentOptionRepository;
119
 
117
	
120
	@Autowired
118
	@Autowired
121
	ScanRecordRepository scanRecordRepository;
119
	ScanRecordRepository scanRecordRepository;
122
 
120
	
123
	@Autowired
121
	@Autowired
124
	FofoOrderRepository fofoOrderRepository;
122
	FofoOrderRepository fofoOrderRepository;
125
 
123
	
126
	@Autowired
124
	@Autowired
127
	RetailerRepository retailerRepository;
125
	RetailerRepository retailerRepository;
128
 
126
	
129
	@Autowired
127
	@Autowired
130
	UserRepository userRepository;
128
	UserRepository userRepository;
131
 
129
	
132
	@Autowired
130
	@Autowired
133
	UserAccountRepository userAccountRepository;
131
	UserAccountRepository userAccountRepository;
134
 
-
 
135
	@Autowired
-
 
136
	ItemRepository itemRepository;
-
 
137
 
132
	
138
	@Autowired
133
	@Autowired
139
	RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
134
	RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
140
 
135
	
141
	@Autowired
136
	@Autowired
142
	CustomerAddressRepository customerAddressRepository;
137
	CustomerAddressRepository customerAddressRepository;
-
 
138
	
-
 
139
	@Autowired
-
 
140
	ItemRepository itemRepository;
143
 
141
	
144
	@Autowired
142
	@Autowired
145
	MVCResponseSender mvcResponseSender;
143
	MVCResponseSender mvcResponseSender;
146
 
144
	
147
	@Autowired
145
	@Autowired
148
	CookiesFetcher cookiesFetcher;
146
	CookiesFetcher cookiesFetcher;
149
 
-
 
150
	@RequestMapping(value = "/order")
-
 
151
	public String orderIndex(HttpServletRequest request, @RequestParam(name = "cartData") String cartData, Model model) throws Exception{
-
 
152
		FofoDetails fofoDetails;
-
 
153
		try {
-
 
154
			fofoDetails = cookiesFetcher.getCookiesObject(request);
-
 
155
		} catch (ProfitMandiBusinessException e) {
-
 
156
			model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));
-
 
157
			return "response";
-
 
158
		}
-
 
159
		JSONObject cartObject = new JSONObject(cartData);
-
 
160
		Iterator<?> keys = cartObject.keys();
-
 
161
 
-
 
162
		Set<Integer> itemIds = new HashSet<>();
-
 
163
		List<CartFofo> cartItems = new ArrayList<CartFofo>();
-
 
164
 
-
 
165
		while( keys.hasNext() ) {
-
 
166
			String key = (String)keys.next();
-
 
167
			if ( cartObject.get(key) instanceof JSONObject ) {
-
 
168
				System.out.println(cartObject.get(key));
-
 
169
			}
-
 
170
			CartFofo cf = new CartFofo();
-
 
171
			cf.setItemId(cartObject.getJSONObject(key).getInt("itemId"));
-
 
172
			cf.setQuantity(cartObject.getJSONObject(key).getInt("quantity"));
-
 
173
			cartItems.add(cf);
-
 
174
			itemIds.add(cartObject.getJSONObject(key).getInt("itemId"));
-
 
175
		}
-
 
176
		Map<Integer, Item>  itemMap = new HashMap<Integer, Item>();
-
 
177
		if (itemIds.size() > 0){
-
 
178
			List<Item> items = itemRepository.selectByIds(itemIds);
-
 
179
			for (Item i : items){
-
 
180
				itemMap.put(i.getId(), i);
-
 
181
			}
-
 
182
		}
-
 
183
		for (CartFofo cf : cartItems){
-
 
184
			Item i = itemMap.get(cf.getItemId());
-
 
185
			if (i == null){
-
 
186
				continue;
-
 
187
			}
-
 
188
			cf.setDisplayName(getValidName(i.getBrand())+" "+getValidName(i.getModelName())+" "+getValidName(i.getModelNumber())+" "+getValidName(i.getColor()).replaceAll("\\s+", " "));
-
 
189
			cf.setItemType(i.getType());
-
 
190
		}
-
 
191
		model.addAttribute("cartObj", cartItems);
-
 
192
		return "order-index";
-
 
193
	}
-
 
194
	
147
	
195
	private String getValidName(String name){
-
 
196
		return name!=null?name:"";
-
 
197
	}
-
 
198
 
-
 
199
	@RequestMapping(value = "/create-order", method = RequestMethod.POST)
148
	@RequestMapping(value = "/order", method = RequestMethod.POST)
200
	public String createOrder(HttpServletRequest request, @RequestBody CreateOrderRequest createOrderRequest, Model model)  throws Throwable{
149
	public String createOrder(HttpServletRequest request, @RequestBody CreateOrderRequest createOrderRequest)  throws Throwable{
201
		//FofoDetails fofoDetails = cookiesFetcher.getCookiesObject(request);
150
		//FofoDetails fofoDetails = cookiesFetcher.getCookiesObject(request);
202
		LOGGER.info("/order request {}", createOrderRequest);
151
		LOGGER.info("/order request {}", createOrderRequest);
203
		FofoDetails fofoDetails = new FofoDetails();
152
		FofoDetails fofoDetails = new FofoDetails();
204
		fofoDetails.setFofoId(963490);
153
		fofoDetails.setFofoId(963490);
205
		Set<CustomFofoLineItem> fofoLineItems = createOrderRequest.getFofoLineItems();
-
 
206
		Set<Integer> itemIds = new HashSet<>();
154
		Set<Integer> itemIds = new HashSet<>();
207
		Set<Integer> nonSerializedItemIds = new HashSet<>();
155
		Map<Integer, Integer> itemIdQuantity = new HashMap<>(); //this is for error
208
		Map<Integer, Integer> itemIdQuantity = new HashMap<>();
156
		Map<Integer, CustomFofoLineItem> customFofoLineItemMap = new HashMap<>();
-
 
157
		Map<Integer, Float> lineItemPrice = new HashMap<>(); //this is for pricing error
-
 
158
		float totalAmount = 0;
209
		for(CustomFofoLineItem fofoLineItem : fofoLineItems){
159
		for(CustomFofoLineItem customFofoLineItem : createOrderRequest.getFofoLineItems()){
210
			itemIds.add(fofoLineItem.getItemId());
160
			itemIds.add(customFofoLineItem.getItemId());
211
			if(fofoLineItem.getSerialNumbers().size() > 0 && fofoLineItem.getQuantity() != fofoLineItem.getSerialNumbers().size()){
161
			if(!customFofoLineItem.getSerialNumbers().isEmpty() && customFofoLineItem.getQuantity() != customFofoLineItem.getSerialNumbers().size()){
212
				itemIdQuantity.put(fofoLineItem.getItemId(), fofoLineItem.getQuantity());
162
				itemIdQuantity.put(customFofoLineItem.getItemId(), customFofoLineItem.getQuantity());
213
			}
163
			}
214
			if(fofoLineItem.getSerialNumbers().isEmpty()){
164
			if(!(customFofoLineItem.getSellingPrice() > 0)){
215
				nonSerializedItemIds.add(fofoLineItem.getItemId());
165
				lineItemPrice.put(customFofoLineItem.getItemId(), customFofoLineItem.getSellingPrice());
-
 
166
			}else{
-
 
167
				totalAmount = totalAmount + customFofoLineItem.getSellingPrice() * customFofoLineItem.getQuantity();
216
			}
168
			}
-
 
169
			customFofoLineItemMap.put(customFofoLineItem.getItemId(), customFofoLineItem);
217
		}
170
		}
218
		if(!itemIdQuantity.isEmpty()){
171
		if(!itemIdQuantity.isEmpty()){
219
			// if item quantity does not match with given serialnumbers size
172
			// if item quantity does not match with given serialnumbers size
220
			LOGGER.error("itemId's quantity should be equal to given serialnumber size {} ", itemIdQuantity);
173
			LOGGER.error("itemId's quantity should be equal to given serialnumber size {} ", itemIdQuantity);
221
			return "error";
174
			return "error";
222
		}
175
		}
223
		float totalAmount = 0;
-
 
224
		Map<Integer, Float> lineItemPrice = new HashMap<>();
-
 
225
		List<CurrentInventorySnapshot> currentInventorySnapshots = currentInventorySnapshotRepository.selectByFofoItemIds(fofoDetails.getFofoId(), itemIds);
-
 
226
		List<ItemIdQuantityAvailability> itemIdQuantityAvailabilities = new ArrayList<>(); 
-
 
227
		for(CurrentInventorySnapshot currentInventorySnapshot : currentInventorySnapshots){
-
 
228
			for(CustomFofoLineItem fofoLineItem : fofoLineItems){
-
 
229
				if(currentInventorySnapshot.getId().getItemId() == fofoLineItem.getItemId()){
-
 
230
					if(fofoLineItem.getQuantity() > currentInventorySnapshot.getAvailability()){
-
 
231
						ItemIdQuantityAvailability itemIdQuantityAvailability = new ItemIdQuantityAvailability();
-
 
232
						itemIdQuantityAvailability.setItemId(fofoLineItem.getItemId());
-
 
233
						Quantity quantity = new Quantity();
-
 
234
						quantity.setAvailable(currentInventorySnapshot.getAvailability());
-
 
235
						quantity.setRequested(fofoLineItem.getQuantity());
-
 
236
						itemIdQuantityAvailability.setQuantity(quantity);
-
 
237
						itemIdQuantityAvailabilities.add(itemIdQuantityAvailability);
-
 
238
					}
-
 
239
					if(!(fofoLineItem.getSellingPrice() > 0)){
-
 
240
						lineItemPrice.put(fofoLineItem.getItemId(), fofoLineItem.getSellingPrice());
-
 
241
					}else{
-
 
242
						totalAmount = totalAmount + fofoLineItem.getSellingPrice() * fofoLineItem.getQuantity();
-
 
243
					}
-
 
244
				}
-
 
245
			}
-
 
246
		}
-
 
247
 
-
 
248
		try{
176
		try{
249
			this.validatePaymentOptionsAndTotalAmount(createOrderRequest.getPaymentOptions(), totalAmount);
177
			this.validatePaymentOptionsAndTotalAmount(createOrderRequest.getPaymentOptions(), totalAmount);
250
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
178
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
251
			LOGGER.error("Error occured while validating payment options : ", profitMandiBusinessException);
179
			LOGGER.error("Error occured while validating payment options : ", profitMandiBusinessException);
252
			return "error";
180
			return "error";
253
		}
181
		}
-
 
182
		if(!lineItemPrice.isEmpty()){
-
 
183
			// given fofo line item price must be greater than zero
-
 
184
			LOGGER.error("requested itemId's selling price must greater than 0");
-
 
185
			return "error";
-
 
186
		}
254
 
187
		
-
 
188
		List<CurrentInventorySnapshot> currentInventorySnapshots = currentInventorySnapshotRepository.selectByFofoItemIds(fofoDetails.getFofoId(), itemIds);
-
 
189
		if(itemIds.size() != currentInventorySnapshots.size()){
-
 
190
			// error
-
 
191
		}
-
 
192
		List<ItemIdQuantityAvailability> itemIdQuantityAvailabilities = new ArrayList<>(); //this is for error
-
 
193
		for(CurrentInventorySnapshot currentInventorySnapshot : currentInventorySnapshots){
-
 
194
			CustomFofoLineItem customFofoLineItem = customFofoLineItemMap.get(currentInventorySnapshot.getId().getItemId());
-
 
195
			if(customFofoLineItem.getQuantity() > currentInventorySnapshot.getAvailability()){
-
 
196
				ItemIdQuantityAvailability itemIdQuantityAvailability = new ItemIdQuantityAvailability();
-
 
197
				itemIdQuantityAvailability.setItemId(customFofoLineItem.getItemId());
-
 
198
				Quantity quantity = new Quantity();
-
 
199
				quantity.setAvailable(currentInventorySnapshot.getAvailability());
-
 
200
				quantity.setRequested(customFofoLineItem.getQuantity());
-
 
201
				itemIdQuantityAvailability.setQuantity(quantity);
-
 
202
				itemIdQuantityAvailabilities.add(itemIdQuantityAvailability);
-
 
203
			}
-
 
204
			
-
 
205
		}
-
 
206
		
-
 
207
		
-
 
208
		
255
		if(!itemIdQuantityAvailabilities.isEmpty()){
209
		if(!itemIdQuantityAvailabilities.isEmpty()){
256
			// itemIdQuantity request is not valid
210
			// itemIdQuantity request is not valid
257
			LOGGER.error("Requested quantities should not be greater than currently available quantities {}", itemIdQuantityAvailabilities);
211
			LOGGER.error("Requested quantities should not be greater than currently available quantities {}", itemIdQuantityAvailabilities);
258
			return "error";
212
			return "error";
259
		}
213
		}
260
 
214
		
261
		if(!lineItemPrice.isEmpty()){
-
 
262
			// given fofo line item price must be greater than zero
-
 
263
			LOGGER.error("requested itemId's selling price must greater thant 0");
-
 
264
			return "error";
-
 
265
		}
215
		
266
 
216
		
267
		Set<Integer> invalidItemIdSerialNumbers = new HashSet<>();
217
		Set<Integer> invalidItemIdSerialNumbers = new HashSet<>();
268
		Map<Integer, Set<String>> itemIdSerialNumbers = new HashMap<>();
218
		Map<Integer, Set<String>> itemIdSerialNumbers = new HashMap<>();
269
		List<InventoryItem> inventoryItems = inventoryItemRepository.selectByFofoIdItemIds(fofoDetails.getFofoId(), itemIds);
219
		List<InventoryItem> inventoryItems = inventoryItemRepository.selectByFofoIdItemIds(fofoDetails.getFofoId(), itemIds);
270
		if(inventoryItems.isEmpty()){
-
 
271
			// invalid item ids
-
 
272
			LOGGER.error("Requested itemId's are not available in inventoryItem");
-
 
273
			return "error";
-
 
274
		}
220
		
275
 
-
 
276
		Map<Integer, Float> itemIdPriceDropAmount = new HashMap<>();
221
		Map<Integer, Float> itemIdPriceDropAmount = new HashMap<>();
277
 
222
		
278
		for(InventoryItem inventoryItem : inventoryItems){
223
		for(InventoryItem inventoryItem : inventoryItems){
279
			for(CustomFofoLineItem fofoLineItem : fofoLineItems){
224
			CustomFofoLineItem customFofoLineItem = customFofoLineItemMap.get(inventoryItem.getItemId());
280
				if(inventoryItem.getItemId() == fofoLineItem.getItemId()){
-
 
281
					if(fofoLineItem.getSerialNumbers().isEmpty()){
225
			if(customFofoLineItem.getSerialNumbers().isEmpty()){
282
						if(!(inventoryItem.getSerialNumber() == null && inventoryItem.getSerialNumber().equals(""))){
226
				if(!(inventoryItem.getSerialNumber() == null && inventoryItem.getSerialNumber().equals(""))){
283
							invalidItemIdSerialNumbers.add(inventoryItem.getItemId());
227
					invalidItemIdSerialNumbers.add(inventoryItem.getItemId());
284
						}
228
				}
285
					}
229
			}
286
					if(!fofoLineItem.getSerialNumbers().isEmpty()){
230
			if(!customFofoLineItem.getSerialNumbers().isEmpty()){
287
						if(!fofoLineItem.getSerialNumbers().contains(inventoryItem.getSerialNumber())){
231
				if(!customFofoLineItem.getSerialNumbers().contains(inventoryItem.getSerialNumber())){
288
							if(!itemIdSerialNumbers.containsKey(fofoLineItem.getItemId())){
232
					if(!itemIdSerialNumbers.containsKey(customFofoLineItem.getItemId())){
289
								Set<String> serialNumbers = new HashSet<>();
233
						Set<String> serialNumbers = new HashSet<>();
290
								serialNumbers.add(inventoryItem.getSerialNumber());
234
						serialNumbers.add(inventoryItem.getSerialNumber());
291
								itemIdSerialNumbers.put(fofoLineItem.getItemId(), serialNumbers);
235
						itemIdSerialNumbers.put(customFofoLineItem.getItemId(), serialNumbers);
292
							}else{
236
					}else{
293
								itemIdSerialNumbers.get(fofoLineItem.getItemId()).add(inventoryItem.getSerialNumber());
237
						itemIdSerialNumbers.get(customFofoLineItem.getItemId()).add(inventoryItem.getSerialNumber());
294
							}
-
 
295
						}
-
 
296
					}
238
					}
297
					itemIdPriceDropAmount.put(inventoryItem.getItemId(), inventoryItem.getUnitPrice() - inventoryItem.getPriceDropAmount());
-
 
298
				}
239
				}
299
			}
240
			}
-
 
241
			itemIdPriceDropAmount.put(inventoryItem.getItemId(), inventoryItem.getUnitPrice() - inventoryItem.getPriceDropAmount());
300
		}
242
		}
301
 
243
		
302
		if(!invalidItemIdSerialNumbers.isEmpty()){
244
		if(!invalidItemIdSerialNumbers.isEmpty()){
303
			LOGGER.error("Invalid itemId's serialNumbers {}", invalidItemIdSerialNumbers);
245
			LOGGER.error("Invalid itemId's serialNumbers {}", invalidItemIdSerialNumbers);
304
			// itemId's are serialized you are saying these are not serialized
246
			// itemId's are serialized you are saying these are not serialized
305
			return "error";
247
			return "error";
306
		}
248
		}
307
 
249
		
308
		if(!itemIdSerialNumbers.isEmpty()){
250
		if(!itemIdSerialNumbers.isEmpty()){
309
			LOGGER.error("Invalid itemId's serialNumbers {}", itemIdSerialNumbers);
251
			LOGGER.error("Invalid itemId's serialNumbers {}", itemIdSerialNumbers);
310
			// itemId's are non serialized you are saying these are serialized
252
			// itemId's are non serialized you are saying these are serialized
311
			return "error";
253
			return "error";
312
		}
254
		}
-
 
255
		
-
 
256
		List<Item> items = itemRepository.selectByIds(itemIds);
-
 
257
		
-
 
258
		if(items.size() != itemIds.size()){
-
 
259
			LOGGER.error("Requested ItemIds not found in catalog");
-
 
260
			// invalid itemIds 
-
 
261
			return "error";
-
 
262
		}
-
 
263
		
-
 
264
		Map<Integer, Item> itemIdDetails = this.toItemMap(items);
-
 
265
		
313
		InvoiceNumberGenerationSequence invoiceNumberGenerationSequence = null;
266
		InvoiceNumberGenerationSequence invoiceNumberGenerationSequence = null;
314
		try{
267
		try{
315
			invoiceNumberGenerationSequence = invoiceNumberGenerationSequenceRepository.selectByFofoId(fofoDetails.getFofoId());
268
			invoiceNumberGenerationSequence = invoiceNumberGenerationSequenceRepository.selectByFofoId(fofoDetails.getFofoId());
316
			invoiceNumberGenerationSequence.setSequence(invoiceNumberGenerationSequence.getSequence() + 1);
269
			invoiceNumberGenerationSequence.setSequence(invoiceNumberGenerationSequence.getSequence() + 1);
317
			invoiceNumberGenerationSequenceRepository.persist(invoiceNumberGenerationSequence);
270
			invoiceNumberGenerationSequenceRepository.persist(invoiceNumberGenerationSequence);
Line 319... Line 272...
319
			invoiceNumberGenerationSequence = new InvoiceNumberGenerationSequence();
272
			invoiceNumberGenerationSequence = new InvoiceNumberGenerationSequence();
320
			invoiceNumberGenerationSequence.setFofoId(fofoDetails.getFofoId());
273
			invoiceNumberGenerationSequence.setFofoId(fofoDetails.getFofoId());
321
			invoiceNumberGenerationSequence.setPrefix("INVOICE");
274
			invoiceNumberGenerationSequence.setPrefix("INVOICE");
322
			invoiceNumberGenerationSequenceRepository.persist(invoiceNumberGenerationSequence);
275
			invoiceNumberGenerationSequenceRepository.persist(invoiceNumberGenerationSequence);
323
		}
276
		}
324
 
277
		
325
		CustomCustomer customCustomer = createOrderRequest.getCustomer();
278
		CustomCustomer customCustomer = createOrderRequest.getCustomer();
326
 
279
		
327
		if(!StringUtils.isValidEmailAddress(customCustomer.getEmailId())){
280
		if(!StringUtils.isValidEmailAddress(customCustomer.getEmailId())){
328
			LOGGER.error("invalid customer emailId {} ", customCustomer.getEmailId());
281
			LOGGER.error("invalid customer emailId {} ", customCustomer.getEmailId());
329
			return "error";
282
			return "error";
330
		}
283
		}
331
 
284
		
332
		if(!StringUtils.isValidMobile(customCustomer.getMobileNumber())){
285
		if(!StringUtils.isValidMobile(customCustomer.getMobileNumber())){
333
			LOGGER.error("invalid customer mobileNumber {} ", customCustomer.getMobileNumber());
286
			LOGGER.error("invalid customer mobileNumber {} ", customCustomer.getMobileNumber());
334
			return "error";
287
			return "error";
335
		}
288
		}
336
 
289
		
337
		Customer customer = null;
290
		Customer customer = null;
338
		try{
291
		try{
-
 
292
			//TODO:remove email id
339
			customer = customerRepository.selectByEmailIdAndMobileNumber(customCustomer.getEmailId(), customCustomer.getMobileNumber());
293
			customer = customerRepository.selectByEmailIdAndMobileNumber(customCustomer.getEmailId(), customCustomer.getMobileNumber());
340
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
294
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
341
			LOGGER.error("Error : ", profitMandiBusinessException);
295
			LOGGER.error("Error : ", profitMandiBusinessException);
342
			customer = new Customer();
296
			customer = new Customer();
343
			customer.setName(customCustomer.getName());
297
			customer.setName(customCustomer.getName());
344
			customer.setEmailId(customCustomer.getEmailId());
298
			customer.setEmailId(customCustomer.getEmailId());
345
			customer.setMobileNumber(customCustomer.getMobileNumber());
299
			customer.setMobileNumber(customCustomer.getMobileNumber());
346
			customerRepository.persist(customer);
300
			customerRepository.persist(customer);
347
		}
301
		}
-
 
302
		//TODO:Check if createOrderRequest contains addressId
348
		CustomerAddress customerAddress = this.createCustomerAddress(customCustomer.getAddress());
303
		CustomerAddress customerAddress = this.createCustomerAddress(customCustomer.getAddress());
349
		customerAddress.setCustomerId(customer.getId());
304
		customerAddress.setCustomerId(customer.getId());
350
		customerAddressRepository.persist(customerAddress);
305
		customerAddressRepository.persist(customerAddress);
351
 
306
		
352
		/*InvoiceNumberGenerationSequence invoiceNumberGenerationSequence = */
307
			/*InvoiceNumberGenerationSequence invoiceNumberGenerationSequence = */
353
 
308
		
354
 
309
		
355
		FofoOrder fofoOrder = new FofoOrder();
310
		FofoOrder fofoOrder = new FofoOrder();
356
		fofoOrder.setCustomerId(customer.getId());
311
		fofoOrder.setCustomerId(customer.getId());
357
		fofoOrder.setFofoId(fofoDetails.getFofoId());
312
		fofoOrder.setFofoId(fofoDetails.getFofoId());
358
		fofoOrder.setInvoiceNumber(invoiceNumberGenerationSequence.getPrefix() + invoiceNumberGenerationSequence.getSequence());
313
		fofoOrder.setInvoiceNumber(invoiceNumberGenerationSequence.getPrefix() + invoiceNumberGenerationSequence.getSequence());
359
		fofoOrder.setTotalAmount(totalAmount);
314
		fofoOrder.setTotalAmount(totalAmount);
360
		fofoOrder.setCustomerAddressId(customerAddress.getId());
315
		fofoOrder.setCustomerAddressId(customerAddress.getId());
361
		fofoOrderRepository.persist(fofoOrder);
316
		fofoOrderRepository.persist(fofoOrder);
362
 
317
		
363
		for(CustomPaymentOption customPaymentOption : createOrderRequest.getPaymentOptions()){
318
		for(CustomPaymentOption customPaymentOption : createOrderRequest.getPaymentOptions()){
364
			PaymentOption paymentOption = new PaymentOption();
319
			PaymentOption paymentOption = new PaymentOption();
365
			paymentOption.setOrderId(fofoOrder.getId());
320
			paymentOption.setOrderId(fofoOrder.getId());
366
			paymentOption.setAmount(customPaymentOption.getAmount());
321
			paymentOption.setAmount(customPaymentOption.getAmount());
367
			paymentOption.setType(customPaymentOption.getType());
322
			paymentOption.setType(customPaymentOption.getType());
368
			paymentOptionRepository.persist(paymentOption);
323
			paymentOptionRepository.persist(paymentOption);
369
		}
324
		}
370
 
-
 
371
//		List<Order> orders = orderRepository.selectByRetailerIdItemIds(fofoDetails.getFofoId(), itemIds);
-
 
372
//
-
 
373
//		Set<Item> items = new HashSet<>(itemIds.size());
-
 
374
//		for(Order order : orders){
-
 
375
//			LineItem lineItem = order.getLineItem();
-
 
376
//			items.add(lineItem.getItem());
-
 
377
//		}
-
 
378
//
-
 
379
//		if(items.size() != itemIds.size()){
-
 
380
//			LOGGER.error("Requested ItemIds not found in catalog");
-
 
381
//			// invalid itemIds 
-
 
382
//			return "error";
-
 
383
//		}
-
 
384
		
325
		
385
		List<Item> items = itemRepository.selectByIds(itemIds);
326
		Address retailerAddress = addressRepository.selectById(retailerRegisteredAddressRepository.selectAddressIdByRetailerId(fofoDetails.getFofoId()));
386
 
327
		//
387
		Map<Integer, Map<String, String>> itemIdDetails = this.toMap(items);
328
		Map<String, GstRate> gstRateMap = Utils.getGstRates(retailerAddress.getState());
388
 
-
 
389
		for(CustomFofoLineItem customFofoLineItem : createOrderRequest.getFofoLineItems()){
329
		for(CustomFofoLineItem customFofoLineItem : createOrderRequest.getFofoLineItems()){
390
			FofoLineItem fofoLineItem = new FofoLineItem();
330
			FofoLineItem fofoLineItem = new FofoLineItem();
391
			fofoLineItem.setItemId(customFofoLineItem.getItemId());
331
			fofoLineItem.setItemId(customFofoLineItem.getItemId());
392
			fofoLineItem.setQuantity(customFofoLineItem.getQuantity());
332
			fofoLineItem.setQuantity(customFofoLineItem.getQuantity());
393
			fofoLineItem.setSellingPrice(customFofoLineItem.getSellingPrice());
333
			fofoLineItem.setSellingPrice(customFofoLineItem.getSellingPrice());
394
			fofoLineItem.setOrderId(fofoOrder.getId());
334
			fofoLineItem.setOrderId(fofoOrder.getId());
395
			fofoLineItem.setDp(customFofoLineItem.getSellingPrice());
335
			fofoLineItem.setDp(customFofoLineItem.getSellingPrice());
-
 
336
			Item item = itemIdDetails.get(customFofoLineItem.getItemId());
-
 
337
			GstRate gstRate = gstRateMap.get(item.getHsnCode());
-
 
338
			fofoLineItem.setIgstRate(gstRate.getIgstRate());
396
			fofoLineItem.setTax(0);
339
			fofoLineItem.setCgstRate(gstRate.getCgstRate());
397
			fofoLineItem.setTaxRate(0);
340
			fofoLineItem.setSgstRate(gstRate.getSgstRate());
398
			fofoLineItem.setCost(itemIdPriceDropAmount.get(customFofoLineItem.getItemId()));
341
			fofoLineItem.setCost(itemIdPriceDropAmount.get(customFofoLineItem.getItemId()));
399
			Map<String, String> itemIdDetail = itemIdDetails.get(customFofoLineItem.getItemId());
-
 
400
			fofoLineItem.setBrand(itemIdDetail.get(ProfitMandiConstants.BRAND));
342
			fofoLineItem.setBrand(item.getBrand());
401
			fofoLineItem.setModelName(itemIdDetail.get(ProfitMandiConstants.MODEL_NAME));
343
			fofoLineItem.setModelName(item.getModelName());
402
			fofoLineItem.setModelNumber(itemIdDetail.get(ProfitMandiConstants.MODEL_NUMBER));
344
			fofoLineItem.setModelNumber(item.getModelNumber());
403
			fofoLineItem.setColor(itemIdDetail.get(ProfitMandiConstants.COLOR));
345
			fofoLineItem.setColor(item.getColor());
404
			fofoLineItemRepository.persist(fofoLineItem);
346
			fofoLineItemRepository.persist(fofoLineItem);
405
			LOGGER.info("\n\n");
347
			LOGGER.info("\n\n");
406
			if(!customFofoLineItem.getSerialNumbers().isEmpty()){
348
			if(!customFofoLineItem.getSerialNumbers().isEmpty()){
407
				for(String serialNumber : customFofoLineItem.getSerialNumbers()){
349
				for(String serialNumber : customFofoLineItem.getSerialNumbers()){
408
					FofoLineItemSerialNumber fofoLineItemSerialNumber = new FofoLineItemSerialNumber();
350
					FofoLineItemSerialNumber fofoLineItemSerialNumber = new FofoLineItemSerialNumber();
409
					fofoLineItemSerialNumber.setFofoLineItemId(fofoLineItem.getId());
351
					fofoLineItemSerialNumber.setFofoLineItemId(fofoLineItem.getId());
410
					fofoLineItemSerialNumber.setSerialNumber(serialNumber);
352
					fofoLineItemSerialNumber.setSerialNumber(serialNumber);
411
					fofoLineItemSerialNumberRepository.persist(fofoLineItemSerialNumber);
353
					fofoLineItemSerialNumberRepository.persist(fofoLineItemSerialNumber);
412
				}
354
				}
413
			}
355
			}
414
 
-
 
415
 
356
			
-
 
357
			for(CurrentInventorySnapshot currentInventorySnapshot : currentInventorySnapshots){
-
 
358
				FofoItemId fofoItemId = new FofoItemId();
-
 
359
				fofoItemId.setFofoId(fofoDetails.getFofoId());
-
 
360
				fofoItemId.setItemId(fofoLineItem.getItemId());
-
 
361
				if(currentInventorySnapshot.getId().equals(fofoItemId)){
-
 
362
					currentInventorySnapshotRepository.updateAvailabilityByFofoItemId(fofoItemId, currentInventorySnapshot.getAvailability() - customFofoLineItem.getQuantity());
416
 
363
				}
417
 
364
			}
418
			for(InventoryItem inventoryItem : inventoryItems){
365
			for(InventoryItem inventoryItem : inventoryItems){
419
				if(inventoryItem.getItemId() == fofoLineItem.getItemId()){
366
				if(inventoryItem.getItemId() == fofoLineItem.getItemId()){
420
					if(inventoryItem.getSerialNumber() == null || inventoryItem.getSerialNumber().isEmpty()){
367
					if(inventoryItem.getSerialNumber() == null || inventoryItem.getSerialNumber().isEmpty()){
421
						if(fofoLineItem.getQuantity() > 0)
368
						if(fofoLineItem.getQuantity() > 0)
422
							if(fofoLineItem.getQuantity() <= inventoryItem.getGoodQuantity()){
369
						if(fofoLineItem.getQuantity() <= inventoryItem.getGoodQuantity()){
423
								inventoryItem.setGoodQuantity(inventoryItem.getGoodQuantity() - fofoLineItem.getQuantity());
370
							inventoryItem.setGoodQuantity(inventoryItem.getGoodQuantity() - fofoLineItem.getQuantity());
424
								inventoryItem.setLastScanType(ScanType.SALE);
371
							inventoryItem.setLastScanType(ScanType.SALE);
425
								inventoryItemRepository.persist(inventoryItem);
372
							inventoryItemRepository.persist(inventoryItem);
426
								ScanRecord scanRecord = new ScanRecord();
373
							ScanRecord scanRecord = new ScanRecord();
427
								scanRecord.setInventoryItemId(inventoryItem.getId());
374
							scanRecord.setInventoryItemId(inventoryItem.getId());
428
								scanRecord.setFofoId(fofoDetails.getFofoId());
375
							scanRecord.setFofoId(fofoDetails.getFofoId());
429
								scanRecord.setQuantity(fofoLineItem.getQuantity());
376
							scanRecord.setQuantity(fofoLineItem.getQuantity());
430
								scanRecord.setType(ScanType.SALE);
377
							scanRecord.setType(ScanType.SALE);
431
								scanRecordRepository.persist(scanRecord);
378
							scanRecordRepository.persist(scanRecord);
432
							}else{
379
						}else{
433
								int availableGoodQuantity = inventoryItem.getGoodQuantity();
380
							int availableGoodQuantity = inventoryItem.getGoodQuantity();
434
								inventoryItem.setGoodQuantity(0);
381
							inventoryItem.setGoodQuantity(0);
435
								fofoLineItem.setQuantity(fofoLineItem.getQuantity() - availableGoodQuantity);
382
							fofoLineItem.setQuantity(fofoLineItem.getQuantity() - availableGoodQuantity);
436
								inventoryItem.setLastScanType(ScanType.SALE);
383
							inventoryItem.setLastScanType(ScanType.SALE);
437
								inventoryItemRepository.persist(inventoryItem);
384
							inventoryItemRepository.persist(inventoryItem);
438
								ScanRecord scanRecord = new ScanRecord();
385
							ScanRecord scanRecord = new ScanRecord();
439
								scanRecord.setInventoryItemId(inventoryItem.getId());
386
							scanRecord.setInventoryItemId(inventoryItem.getId());
440
								scanRecord.setFofoId(fofoDetails.getFofoId());
387
							scanRecord.setFofoId(fofoDetails.getFofoId());
441
								scanRecord.setQuantity(availableGoodQuantity);
388
							scanRecord.setQuantity(availableGoodQuantity);
442
								scanRecord.setType(ScanType.SALE);
389
							scanRecord.setType(ScanType.SALE);
443
								scanRecordRepository.persist(scanRecord);
390
							scanRecordRepository.persist(scanRecord);
444
							}
391
						}
445
					}else{
392
					}else{
446
						inventoryItem.setGoodQuantity(0);
393
						inventoryItem.setGoodQuantity(0);
447
						inventoryItem.setLastScanType(ScanType.SALE);
394
						inventoryItem.setLastScanType(ScanType.SALE);
448
						inventoryItemRepository.persist(inventoryItem);
395
						inventoryItemRepository.persist(inventoryItem);
449
						ScanRecord scanRecord = new ScanRecord();
396
						ScanRecord scanRecord = new ScanRecord();
Line 453... Line 400...
453
						scanRecord.setType(ScanType.SALE);
400
						scanRecord.setType(ScanType.SALE);
454
						scanRecordRepository.persist(scanRecord);
401
						scanRecordRepository.persist(scanRecord);
455
					}
402
					}
456
				}
403
				}
457
			}
404
			}
458
			
405
					
459
			//current inventory not updating
-
 
460
			//logic is flawed - check flow and handle error properly without persisiting
-
 
461
			//jugaad kaam @ ashik ali
-
 
462
 
-
 
463
		}
406
		}
464
		return null;
407
		return null;
465
	}
408
	}
466
 
409
	
467
 
410
	
468
	private Map<Integer, Map<String, String>> toMap(List<Item> items){
411
	private Map<Integer, Item> toItemMap(List<Item> items){
469
		Map<Integer, Map<String, String>> map = new HashMap<>();
412
		Map<Integer, Item> map = new HashMap<>();
470
		for(Item item : items){
413
		for(Item item : items){
471
			Map<String, String> detailMap = new HashMap<>();
-
 
472
			detailMap.put(ProfitMandiConstants.BRAND, item.getBrand());
-
 
473
			detailMap.put(ProfitMandiConstants.MODEL_NAME, item.getModelName());
-
 
474
			detailMap.put(ProfitMandiConstants.MODEL_NUMBER, item.getModelNumber());
-
 
475
			detailMap.put(ProfitMandiConstants.COLOR, item.getColor());
-
 
476
			map.put(item.getId(), detailMap);
414
			map.put(item.getId(), item);
477
		}
415
		}
478
		return map;
416
		return map;
479
	}
417
	}
480
 
418
	
481
	/*private Address createAddress(CustomAddress customAddress){
419
	/*private Address createAddress(CustomAddress customAddress){
482
		Address address = new Address();
420
		Address address = new Address();
483
		address.setName(customAddress.getName());
421
		address.setName(customAddress.getName());
484
		address.setLine1(customAddress.getLine1());
422
		address.setLine1(customAddress.getLine1());
485
		address.setLine2(customAddress.getLine2());
423
		address.setLine2(customAddress.getLine2());
Line 490... Line 428...
490
		address.setCountry(customAddress.getCountry());
428
		address.setCountry(customAddress.getCountry());
491
		address.setPhoneNumber(customAddress.getPhoneNumber());
429
		address.setPhoneNumber(customAddress.getPhoneNumber());
492
		address.setEnabled(true);
430
		address.setEnabled(true);
493
		return address;
431
		return address;
494
	}*/
432
	}*/
495
 
433
	
496
	private CustomerAddress createCustomerAddress(CustomAddress customAddress){
434
	private CustomerAddress createCustomerAddress(CustomAddress customAddress){
497
		CustomerAddress customerAddress = new CustomerAddress();
435
		CustomerAddress customerAddress = new CustomerAddress();
498
		customerAddress.setName(customAddress.getName());
436
		customerAddress.setName(customAddress.getName());
499
		customerAddress.setLine1(customAddress.getLine1());
437
		customerAddress.setLine1(customAddress.getLine1());
500
		customerAddress.setLine2(customAddress.getLine2());
438
		customerAddress.setLine2(customAddress.getLine2());
Line 504... Line 442...
504
		customerAddress.setState(customAddress.getState());
442
		customerAddress.setState(customAddress.getState());
505
		customerAddress.setCountry(customAddress.getCountry());
443
		customerAddress.setCountry(customAddress.getCountry());
506
		customerAddress.setPhoneNumber(customAddress.getPhoneNumber());
444
		customerAddress.setPhoneNumber(customAddress.getPhoneNumber());
507
		return customerAddress;
445
		return customerAddress;
508
	}
446
	}
509
 
447
	
510
	private CustomAddress createCustomAddress(Address address){
448
	private CustomAddress createCustomAddress(Address address){
511
		CustomAddress customAddress = new CustomAddress();
449
		CustomAddress customAddress = new CustomAddress();
512
		customAddress.setName(address.getName());
450
		customAddress.setName(address.getName());
513
		customAddress.setLine1(address.getLine1());
451
		customAddress.setLine1(address.getLine1());
514
		customAddress.setLine2(address.getLine2());
452
		customAddress.setLine2(address.getLine2());
Line 518... Line 456...
518
		customAddress.setState(address.getState());
456
		customAddress.setState(address.getState());
519
		customAddress.setCountry(address.getCountry());
457
		customAddress.setCountry(address.getCountry());
520
		customAddress.setPhoneNumber(address.getPhoneNumber());
458
		customAddress.setPhoneNumber(address.getPhoneNumber());
521
		return customAddress;
459
		return customAddress;
522
	}
460
	}
523
 
461
	
524
	private CustomAddress createCustomAddress(CustomerAddress customerAddress){
462
	private CustomAddress createCustomAddress(CustomerAddress customerAddress){
525
		CustomAddress customAddress = new CustomAddress();
463
		CustomAddress customAddress = new CustomAddress();
526
		customAddress.setName(customerAddress.getName());
464
		customAddress.setName(customerAddress.getName());
527
		customAddress.setLine1(customerAddress.getLine1());
465
		customAddress.setLine1(customerAddress.getLine1());
528
		customAddress.setLine2(customerAddress.getLine2());
466
		customAddress.setLine2(customerAddress.getLine2());
Line 532... Line 470...
532
		customAddress.setState(customerAddress.getState());
470
		customAddress.setState(customerAddress.getState());
533
		customAddress.setCountry(customerAddress.getCountry());
471
		customAddress.setCountry(customerAddress.getCountry());
534
		customAddress.setPhoneNumber(customerAddress.getPhoneNumber());
472
		customAddress.setPhoneNumber(customerAddress.getPhoneNumber());
535
		return customAddress;
473
		return customAddress;
536
	}
474
	}
537
 
475
	
538
	private void validatePaymentOptionsAndTotalAmount(Set<CustomPaymentOption> customPaymentOptions, float totalAmount) throws ProfitMandiBusinessException
476
	private void validatePaymentOptionsAndTotalAmount(Set<CustomPaymentOption> customPaymentOptions, float totalAmount) throws ProfitMandiBusinessException
539
	{
477
	{
540
		float calculatedAmount = 0;
478
		float calculatedAmount = 0;
541
		Set<String> paymentOptionTypes = new HashSet<>();
479
		Set<String> paymentOptionTypes = new HashSet<>();
542
		for(CustomPaymentOption customPaymentOption : customPaymentOptions){
480
		for(CustomPaymentOption customPaymentOption : customPaymentOptions){
Line 545... Line 483...
545
			}else{
483
			}else{
546
				paymentOptionTypes.add(customPaymentOption.getType().name());
484
				paymentOptionTypes.add(customPaymentOption.getType().name());
547
				calculatedAmount = calculatedAmount + customPaymentOption.getAmount();
485
				calculatedAmount = calculatedAmount + customPaymentOption.getAmount();
548
			}
486
			}
549
		}
487
		}
550
		
-
 
551
		LOGGER.info("calculatedAmount "+calculatedAmount);
-
 
552
		LOGGER.info("totalAmount "+totalAmount);
-
 
553
		
-
 
554
		if(calculatedAmount != totalAmount){
488
		if(calculatedAmount != totalAmount){
555
			throw new ProfitMandiBusinessException(ProfitMandiConstants.PAYMENT_OPTION_CALCULATED_AMOUNT, calculatedAmount, "");
489
			throw new ProfitMandiBusinessException(ProfitMandiConstants.PAYMENT_OPTION_CALCULATED_AMOUNT, calculatedAmount, "");
556
		}
490
		}
557
	}
491
	}
558
 
492
	
559
 
493
	
560
	@RequestMapping(value = "/generateInvoice")
494
	@RequestMapping(value = "/generateInvoice")
561
	public ResponseEntity<?> generateInvoice(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId, Model model) throws Throwable{
495
	public ResponseEntity<?> generateInvoice(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId) throws Throwable{
562
		LOGGER.info("Request received at url {} with params {} ", request.getRequestURI(), request.getParameterMap());
496
		LOGGER.info("Request received at url {} with params {} ", request.getRequestURI(), request.getParameterMap());
563
		//FofoDetails fofoDetails = cookiesFetcher.getCookiesObject(request);
497
		//FofoDetails fofoDetails = cookiesFetcher.getCookiesObject(request);
564
 
498
		
565
		FofoDetails fofoDetails = new FofoDetails();
499
		FofoDetails fofoDetails = new FofoDetails();
566
		fofoDetails.setFofoId(963490);
500
		fofoDetails.setFofoId(963490);
567
		FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndOrderId(fofoDetails.getFofoId(), orderId);
501
		FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndOrderId(fofoDetails.getFofoId(), orderId);
568
		PdfModel pdfModel = new PdfModel();
502
		PdfModel pdfModel = new PdfModel();
569
		pdfModel.setAuther("profitmandi");
503
		pdfModel.setAuther("profitmandi");
Line 576... Line 510...
576
		CustomerAddress customerAddress = customerAddressRepository.selectById(fofoOrder.getCustomerAddressId()); 
510
		CustomerAddress customerAddress = customerAddressRepository.selectById(fofoOrder.getCustomerAddressId()); 
577
		customCustomer.setAddress(this.createCustomAddress(customerAddress));
511
		customCustomer.setAddress(this.createCustomAddress(customerAddress));
578
		pdfModel.setCustomer(customCustomer);
512
		pdfModel.setCustomer(customCustomer);
579
		pdfModel.setInvoiceNumber(fofoOrder.getInvoiceNumber());
513
		pdfModel.setInvoiceNumber(fofoOrder.getInvoiceNumber());
580
		pdfModel.setTotalAmount(fofoOrder.getTotalAmount());
514
		pdfModel.setTotalAmount(fofoOrder.getTotalAmount());
581
 
515
		
582
		Retailer retailer = retailerRepository.selectById(fofoDetails.getFofoId());
516
		Retailer retailer = retailerRepository.selectById(fofoDetails.getFofoId());
583
		User user = userRepository.selectById(userAccountRepository.selectUserIdByRetailerId(retailer.getId()));
517
		User user = userRepository.selectById(userAccountRepository.selectUserIdByRetailerId(retailer.getId()));
584
		CustomRetailer customRetailer = new CustomRetailer();
518
		CustomRetailer customRetailer = new CustomRetailer();
585
		customRetailer.setBusinessName(retailer.getName());
519
		customRetailer.setBusinessName(retailer.getName());
586
		customRetailer.setMobileNumber(user.getMobileNumber());
520
		customRetailer.setMobileNumber(user.getMobileNumber());
587
		customRetailer.setTinNumber(retailer.getNumber());
521
		customRetailer.setTinNumber(retailer.getNumber());
588
		Address retailerAddress = addressRepository.selectById(retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailer.getId()));
522
		Address retailerAddress = addressRepository.selectById(retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailer.getId()));
589
		customRetailer.setAddress(this.createCustomAddress(retailerAddress));
523
		customRetailer.setAddress(this.createCustomAddress(retailerAddress));
590
		pdfModel.setRetailer(customRetailer);
524
		pdfModel.setRetailer(customRetailer);
591
		List<FofoLineItem> fofoLineItems = fofoLineItemRepository.selectByOrderId(fofoOrder.getId());
525
		List<FofoLineItem> fofoLineItems = fofoLineItemRepository.selectByOrderId(fofoOrder.getId());
592
 
526
		
593
		Set<CustomFofoOrderItem> customerFofoOrderItems = new HashSet<>();
527
		Set<CustomFofoOrderItem> customerFofoOrderItems = new HashSet<>();
594
		for(FofoLineItem fofoLineItem : fofoLineItems){
528
		for(FofoLineItem fofoLineItem : fofoLineItems){
595
			CustomFofoOrderItem customFofoOrderItem = new CustomFofoOrderItem();
529
			CustomFofoOrderItem customFofoOrderItem = new CustomFofoOrderItem();
596
			customFofoOrderItem.setAmount(fofoLineItem.getQuantity() * fofoLineItem.getSellingPrice());
530
			customFofoOrderItem.setAmount(fofoLineItem.getQuantity() * fofoLineItem.getSellingPrice());
597
			customFofoOrderItem.setDescription(fofoLineItem.getBrand() + " " + fofoLineItem.getModelName() + " " + fofoLineItem.getModelNumber() + " " + fofoLineItem.getColor());
531
			customFofoOrderItem.setDescription(fofoLineItem.getBrand() + " " + fofoLineItem.getModelName() + " " + fofoLineItem.getModelNumber() + " " + fofoLineItem.getColor());
598
			float itemTotal = customFofoOrderItem.getAmount() - fofoLineItem.getTax();
532
			float itemTotal = customFofoOrderItem.getAmount();
599
			customFofoOrderItem.setRate(fofoLineItem.getSellingPrice());
533
			customFofoOrderItem.setRate(fofoLineItem.getSellingPrice());
600
			customFofoOrderItem.setQuantity(fofoLineItem.getQuantity());
534
			customFofoOrderItem.setQuantity(fofoLineItem.getQuantity());
-
 
535
			customFofoOrderItem.setIgstRate(fofoLineItem.getIgstRate());
601
			customFofoOrderItem.setTax(fofoLineItem.getTax());
536
			customFofoOrderItem.setCgstRate(fofoLineItem.getIgstRate());
602
			customFofoOrderItem.setTaxRate(fofoLineItem.getTaxRate());
537
			customFofoOrderItem.setSgstRate(fofoLineItem.getIgstRate());
-
 
538
			customFofoOrderItem.setHsnCode(fofoLineItem.getHsnCode());
603
			customFofoOrderItem.setItemTotal(itemTotal);
539
			customFofoOrderItem.setItemTotal(itemTotal);
604
			customFofoOrderItem.setSerialNumbers(String.join(", ",this.toSerialNumbers(fofoLineItem.getFofoLineItemSerialNumbers())));
540
			customFofoOrderItem.setSerialNumbers(String.join(", ",this.toSerialNumbers(fofoLineItem.getFofoLineItemSerialNumbers())));
605
			customerFofoOrderItems.add(customFofoOrderItem);
541
			customerFofoOrderItems.add(customFofoOrderItem);
606
		}
542
		}
607
		pdfModel.setOrderItems(customerFofoOrderItems);
543
		pdfModel.setOrderItems(customerFofoOrderItems);
608
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
544
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
609
		PdfUtils.generateAndWrite(pdfModel, byteArrayOutputStream);
545
		PdfUtils.generateAndWrite(pdfModel, byteArrayOutputStream);
610
		//final MediaType mediaType=MediaType.parseMediaType(profilePhotoModel.getContentType().getValue());
546
		//final MediaType mediaType=MediaType.parseMediaType(profilePhotoModel.getContentType().getValue());
611
		LOGGER.info("Pdf Stream length {}", byteArrayOutputStream.toByteArray().length);
547
		LOGGER.info("Pdf Stream length {}", byteArrayOutputStream.toByteArray().length);
612
		final HttpHeaders headers=new HttpHeaders();
548
        final HttpHeaders headers=new HttpHeaders();
613
		headers.setContentType(MediaType.APPLICATION_PDF);
549
        headers.setContentType(MediaType.APPLICATION_PDF);
614
		//headers.set("Content-Disposition", "retailerInvoice.pdf");
550
        //headers.set("Content-Disposition", "Order-pdf-"+custom);
615
		headers.setContentLength(byteArrayOutputStream.toByteArray().length);
551
        headers.setContentLength(byteArrayOutputStream.toByteArray().length);
616
		final InputStream inputStream=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
552
        final InputStream inputStream=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
617
		final InputStreamResource inputStreamResource=new InputStreamResource(inputStream);
553
        final InputStreamResource inputStreamResource=new InputStreamResource(inputStream);
618
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
554
        return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
619
	}
555
	}
620
 
556
	
621
	private Set<String> toSerialNumbers(Set<FofoLineItemSerialNumber> fofoLineItemSerialNumbers){
557
	private Set<String> toSerialNumbers(Set<FofoLineItemSerialNumber> fofoLineItemSerialNumbers){
622
		Set<String> serialNumbers = new HashSet<>(fofoLineItemSerialNumbers.size());
558
		Set<String> serialNumbers = new HashSet<>(fofoLineItemSerialNumbers.size());
623
		for(FofoLineItemSerialNumber fofoLineItemSerialNumber : fofoLineItemSerialNumbers){
559
		for(FofoLineItemSerialNumber fofoLineItemSerialNumber : fofoLineItemSerialNumbers){
624
			serialNumbers.add(fofoLineItemSerialNumber.getSerialNumber());
560
			serialNumbers.add(fofoLineItemSerialNumber.getSerialNumber());
625
		}
561
		}
626
		return serialNumbers;
562
		return serialNumbers;
627
	}
563
	}
-
 
564
	
-
 
565
	
628
}
566
}