Subversion Repositories SmartDukaan

Rev

Rev 23085 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5591 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.inventory.controllers;
5
 
24922 amit.gupta 6
import java.util.ArrayList;
7
import java.util.List;
8
 
9
import org.apache.commons.lang.StringUtils;
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12
 
21873 amit.gupta 13
import in.shop2020.inventory.utils.Utils;
5945 mandeep.dh 14
import in.shop2020.model.v1.inventory.BillingType;
15
import in.shop2020.model.v1.inventory.InventoryType;
16
import in.shop2020.model.v1.inventory.Vendor;
17
import in.shop2020.model.v1.inventory.Warehouse;
18
import in.shop2020.model.v1.inventory.WarehouseType;
5591 mandeep.dh 19
import in.shop2020.purchase.PurchaseService.Client;
20
import in.shop2020.purchase.Supplier;
5945 mandeep.dh 21
import in.shop2020.thrift.clients.InventoryClient;
5591 mandeep.dh 22
import in.shop2020.thrift.clients.PurchaseClient;
23
import in.shop2020.utils.ConfigClientKeys;
24
 
25
/**
26
 * @author mandeep
27
 *
28
 */
29
public class SupplierController extends BaseController {
24922 amit.gupta 30
	private static Log logger = LogFactory.getLog(SupplierController.class);
31
	private String name;
32
	private String phone;
33
	private String fax;
34
	private String gstin;
35
 
36
	public String getGstin() {
21844 amit.gupta 37
		return gstin;
38
	}
39
 
40
	public void setGstin(String gstin) {
41
		this.gstin = gstin;
42
	}
43
 
44
	private String pan;
24922 amit.gupta 45
	private String headName;
46
	private String headDesignation;
47
	private String headEmail;
48
	private String contactName;
49
	private String contactPhone;
50
	private String contactFax;
51
	private String contactEmail;
52
	private String registeredAddress;
53
	private long stateId;
54
	private String stateIdVal;
55
	private long poValidityLimit;
56
	private String communicationAddress;
57
	private List<Long> billingWarehouseIds;
58
	private String tnc;
5591 mandeep.dh 59
 
24922 amit.gupta 60
	private String id;
61
	private List<Supplier> suppliers;
62
	private String errorMessage = "";
63
	private Supplier supplier;
5591 mandeep.dh 64
 
24922 amit.gupta 65
	public String index() {
66
		return INDEX;
67
	}
5591 mandeep.dh 68
 
24922 amit.gupta 69
	/*
70
	 * (non-Javadoc)
71
	 * 
72
	 * @see in.shop2020.inventory.controllers.BaseController#edit()
73
	 */
74
	@Override
75
	public String edit() {
76
		try {
77
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = new InventoryClient(
78
					ConfigClientKeys.inventory_service_server_host.toString(),
79
					ConfigClientKeys.inventory_service_server_port.toString()).getClient();
80
			Client purchaseClient = new PurchaseClient().getClient();
81
			supplier = purchaseClient.getSupplier(Long.valueOf(id));
82
			billingWarehouseIds = new ArrayList<Long>();
83
			List<Warehouse> warehouses = inventoryClient.getWarehouses(WarehouseType.OURS, InventoryType.GOOD,
84
					Long.valueOf(id), 0l, 0l);
85
			for (Warehouse warehouse : warehouses) {
86
				billingWarehouseIds.add(warehouse.getBillingWarehouseId());
87
			}
88
		} catch (Exception e) {
89
			errorMessage = "Error finding supplier: " + id + ": " + e.getMessage();
90
			logger.error("Could not find supplier: " + id, e);
91
		}
5591 mandeep.dh 92
 
24922 amit.gupta 93
		return super.edit();
94
	}
5591 mandeep.dh 95
 
24922 amit.gupta 96
	public String create() {
97
		try {
98
			if (name == null || ("").equalsIgnoreCase(name)) {
99
				throw new Exception("Supplier Name is Mandatory.");
100
			}
101
			if ((phone == null || ("").equalsIgnoreCase(phone)) && !StringUtils.isNumeric(phone)) {
102
				throw new Exception("Supplier Phone is Mandatory and Should be in digits only");
103
			}
104
			if (fax == null || ("").equalsIgnoreCase(fax)) {
105
				throw new Exception("Supplier Fax is Mandatory.");
106
			}
107
			if (gstin == null || ("").equalsIgnoreCase(gstin)) {
108
				throw new Exception("Supplier GSTIN is Mandatory.");
109
			}
110
			if (pan == null || ("").equalsIgnoreCase(pan)) {
111
				throw new Exception("Supplier PAN is Mandatory.");
112
			}
113
			if (headName == null || ("").equalsIgnoreCase(headName)) {
114
				throw new Exception("Supplier Head Name is Mandatory.");
115
			}
116
			if (headEmail == null || ("").equalsIgnoreCase(headEmail)) {
117
				throw new Exception("Supplier Head Email is Mandatory.");
118
			}
119
			if (contactName == null || ("").equalsIgnoreCase(contactName)) {
120
				throw new Exception("Supplier Contact Name is Mandatory.");
121
			}
122
			if (contactFax == null || ("").equalsIgnoreCase(contactFax)) {
123
				throw new Exception("Supplier Name is Mandatory.");
124
			}
125
			if (contactPhone == null || ("").equalsIgnoreCase(contactPhone)) {
126
				throw new Exception("Supplier Contact Phone is Mandatory.");
127
			}
128
			if (registeredAddress == null || ("").equalsIgnoreCase(registeredAddress)) {
129
				throw new Exception("Supplier Registered Address is Mandatory.");
130
			}
131
			if (communicationAddress == null || ("").equalsIgnoreCase(communicationAddress)) {
132
				throw new Exception("Supplier Communication Address is Mandatory.");
133
			}
134
			if (billingWarehouseIds == null || billingWarehouseIds.size() == 0) {
135
				throw new Exception("Atleast One Billing Warehouse is Mandatory.");
136
			}
137
			if (billingWarehouseIds == null || billingWarehouseIds.size() == 0) {
138
				throw new Exception("Atleast One Billing Warehouse is Mandatory.");
139
			}
140
			if (stateId == -1) {
141
				throw new Exception("State is Mandatory. Please select state");
142
			}
143
			if (poValidityLimit < 1) {
144
				throw new Exception("PO Validity Limit Must be greater than equal to 1.");
145
			}
146
			Supplier supplier = new Supplier();
147
			name = name.trim();
148
			supplier.setName(name);
149
			supplier.setCommunicationAddress(communicationAddress);
150
			supplier.setContactEmail(contactEmail);
151
			supplier.setContactFax(contactFax);
152
			supplier.setContactName(contactName);
153
			supplier.setContactPhone(contactPhone);
154
			supplier.setFax(fax);
155
			supplier.setHeadDesignation(headDesignation);
156
			supplier.setHeadEmail(headEmail);
157
			supplier.setHeadName(headName);
158
			supplier.setPan(pan);
159
			supplier.setPhone(phone);
160
			supplier.setRegisteredAddress(registeredAddress);
161
			// supplier.setTin(tin);
162
			supplier.setGstin(gstin);
163
			supplier.setStateId(stateId);
164
			supplier.setPoValidityLimit(poValidityLimit);
165
			supplier.setTnc(tnc);
166
			Client purchaseClient = new PurchaseClient().getClient();
167
			supplier = purchaseClient.addSupplier(supplier);
5591 mandeep.dh 168
 
24922 amit.gupta 169
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = new InventoryClient(
170
					ConfigClientKeys.inventory_service_server_host.toString(),
171
					ConfigClientKeys.inventory_service_server_port.toString()).getClient();
172
			Vendor vendor = new Vendor();
173
			vendor.setId(supplier.getId());
174
			vendor.setName(name);
175
			inventoryClient.addVendor(vendor);
176
			for (Long billingWarehouseId : billingWarehouseIds) {
177
				Warehouse billingWarehouse = inventoryClient.getWarehouse(billingWarehouseId);
178
				Warehouse warehouse = new Warehouse();
179
				warehouse.setVendor(vendor);
180
				warehouse.setStateId(billingWarehouse.getStateId());
181
				warehouse.setBillingType(BillingType.OURS);
182
				warehouse.setBillingWarehouseId(billingWarehouse.getId());
183
				warehouse.setDisplayName(
184
						StringUtils.join(new String[] { billingWarehouse.getDisplayName(), name, "G" }, '/'));
185
				warehouse.setInventoryType(InventoryType.GOOD);
186
				warehouse.setIsAvailabilityMonitored(false);
187
				warehouse.setLocation(billingWarehouse.getLocation());
188
				warehouse.setPincode(billingWarehouse.getPincode());
189
				warehouse.setLogisticsLocation(billingWarehouse.getLogisticsLocation());
190
				warehouse.setShippingWarehouseId(billingWarehouse.getId());
191
				warehouse.setGstin(gstin);
192
				warehouse.setTransferDelayInHours(0);
193
				warehouse.setWarehouseType(WarehouseType.OURS);
194
				inventoryClient.addWarehouse(warehouse);
5591 mandeep.dh 195
 
24922 amit.gupta 196
				warehouse.setDisplayName(
197
						StringUtils.join(new String[] { billingWarehouse.getDisplayName(), name, "B" }, '/'));
198
				warehouse.setInventoryType(InventoryType.BAD);
199
				inventoryClient.addWarehouse(warehouse);
5591 mandeep.dh 200
 
24922 amit.gupta 201
				warehouse.setWarehouseType(WarehouseType.THIRD_PARTY);
202
				warehouse.setDisplayName(StringUtils.join(new String[] { name, "G" }, '/'));
203
				warehouse.setInventoryType(InventoryType.GOOD);
204
				warehouse.setTransferDelayInHours(24);
205
				warehouse.setBillingType(null);
206
				warehouse.setBillingWarehouseId(0);
207
				warehouse.setShippingWarehouseId(0);
208
				inventoryClient.addWarehouse(warehouse);
209
				Utils.resetSuppliers();
210
			}
211
		} catch (Exception e) {
212
			errorMessage = "Error creating supplier: " + e.getMessage();
213
			logger.error("Error creating supplier", e);
214
		}
5591 mandeep.dh 215
 
24922 amit.gupta 216
		return index();
217
	}
5591 mandeep.dh 218
 
24922 amit.gupta 219
	public String update() {
220
		logger.info("TNC == " + this.tnc);
221
		String supplierName = id;
222
		try {
223
			Client purchaseClient = new PurchaseClient().getClient();
224
			Supplier supplier = purchaseClient.getSupplier(Long.valueOf(id));
225
			supplierName = supplier.getName();
226
			supplier.setName(name);
227
			supplier.setCommunicationAddress(communicationAddress);
228
			supplier.setContactEmail(contactEmail);
229
			supplier.setContactFax(contactFax);
230
			supplier.setContactName(contactName);
231
			supplier.setContactPhone(contactPhone);
232
			supplier.setFax(fax);
233
			supplier.setHeadDesignation(headDesignation);
234
			supplier.setHeadEmail(headEmail);
235
			supplier.setHeadName(headName);
236
			supplier.setPan(pan);
237
			supplier.setRegisteredAddress(registeredAddress);
238
			supplier.setGstin(gstin);
239
			supplier.setTnc(tnc);
240
			supplier.setStateId(supplier.getStateId());
241
			supplier.setPoValidityLimit(poValidityLimit);
242
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = new InventoryClient(
243
					ConfigClientKeys.inventory_service_server_host.toString(),
244
					ConfigClientKeys.inventory_service_server_port.toString()).getClient();
245
			Vendor vendor = inventoryClient.getVendor(supplier.getId());
246
			for (Long billingWarehouseId : billingWarehouseIds) {
247
				Warehouse billingWarehouse = inventoryClient.getWarehouse(billingWarehouseId);
248
				Warehouse warehouse = new Warehouse();
249
				warehouse.setVendor(vendor);
250
				warehouse.setStateId(billingWarehouse.getStateId());
251
				warehouse.setBillingType(BillingType.OURS);
252
				warehouse.setBillingWarehouseId(billingWarehouse.getId());
253
				warehouse.setDisplayName(
254
						StringUtils.join(new String[] { billingWarehouse.getDisplayName(), name, "G" }, '/'));
255
				warehouse.setInventoryType(InventoryType.GOOD);
256
				warehouse.setIsAvailabilityMonitored(false);
257
				warehouse.setLocation(billingWarehouse.getLocation());
258
				warehouse.setPincode(billingWarehouse.getPincode());
259
				warehouse.setLogisticsLocation(billingWarehouse.getLogisticsLocation());
260
				warehouse.setShippingWarehouseId(billingWarehouse.getId());
261
				warehouse.setGstin(gstin);
262
				warehouse.setTransferDelayInHours(0);
263
				warehouse.setWarehouseType(WarehouseType.OURS);
264
				inventoryClient.addWarehouse(warehouse);
5591 mandeep.dh 265
 
24922 amit.gupta 266
				warehouse.setDisplayName(
267
						StringUtils.join(new String[] { billingWarehouse.getDisplayName(), name, "B" }, '/'));
268
				warehouse.setInventoryType(InventoryType.BAD);
269
				inventoryClient.addWarehouse(warehouse);
5591 mandeep.dh 270
 
24922 amit.gupta 271
				warehouse.setWarehouseType(WarehouseType.THIRD_PARTY);
272
				warehouse.setDisplayName(StringUtils.join(new String[] { name, "G" }, '/'));
273
				warehouse.setInventoryType(InventoryType.GOOD);
274
				warehouse.setTransferDelayInHours(24);
275
				warehouse.setBillingType(null);
276
				warehouse.setBillingWarehouseId(0);
277
				warehouse.setShippingWarehouseId(0);
278
				inventoryClient.addWarehouse(warehouse);
279
				Utils.resetSuppliers();
280
			}
281
			purchaseClient.updateSupplier(supplier);
282
			Utils.resetSuppliers();
283
		} catch (Exception e) {
284
			errorMessage = "Error updating supplier: " + supplierName + ": " + e.getMessage();
285
			logger.error("Error updating supplier", e);
286
		}
287
		return index();
288
	}
5591 mandeep.dh 289
 
24922 amit.gupta 290
	public String getName() {
291
		return name;
292
	}
5591 mandeep.dh 293
 
24922 amit.gupta 294
	public void setName(String name) {
295
		this.name = name;
296
	}
5591 mandeep.dh 297
 
24922 amit.gupta 298
	public String getPhone() {
299
		return phone;
300
	}
7410 amar.kumar 301
 
24922 amit.gupta 302
	public void setPhone(String phone) {
303
		this.phone = phone;
304
	}
305
 
306
	public String getFax() {
307
		return fax;
308
	}
309
 
310
	public void setFax(String fax) {
311
		this.fax = fax;
312
	}
313
 
314
	public String getPan() {
315
		return pan;
316
	}
317
 
318
	public void setPan(String pan) {
319
		this.pan = pan;
320
	}
321
 
322
	public String getHeadName() {
323
		return headName;
324
	}
325
 
326
	public void setHeadName(String headName) {
327
		this.headName = headName;
328
	}
329
 
330
	public String getHeadDesignation() {
331
		return headDesignation;
332
	}
333
 
334
	public void setHeadDesignation(String headDesignation) {
335
		this.headDesignation = headDesignation;
336
	}
337
 
338
	public String getHeadEmail() {
339
		return headEmail;
340
	}
341
 
342
	public void setHeadEmail(String headEmail) {
343
		this.headEmail = headEmail;
344
	}
345
 
346
	public String getContactName() {
347
		return contactName;
348
	}
349
 
350
	public void setContactName(String contactName) {
351
		this.contactName = contactName;
352
	}
353
 
354
	public String getContactPhone() {
355
		return contactPhone;
356
	}
357
 
358
	public void setContactPhone(String contactPhone) {
359
		this.contactPhone = contactPhone;
360
	}
361
 
362
	public String getDisabled(long physicalWarehouseId) {
363
		return billingWarehouseIds.contains(physicalWarehouseId) ? "disabled" : "";
364
	}
365
 
366
	public String getContactFax() {
367
		return contactFax;
368
	}
369
 
370
	public void setContactFax(String contactFax) {
371
		this.contactFax = contactFax;
372
	}
373
 
374
	public String getContactEmail() {
375
		return contactEmail;
376
	}
377
 
378
	public void setContactEmail(String contactEmail) {
379
		this.contactEmail = contactEmail;
380
	}
381
 
382
	public String getRegisteredAddress() {
383
		return registeredAddress;
384
	}
385
 
386
	public void setRegisteredAddress(String registeredAddress) {
387
		this.registeredAddress = registeredAddress;
388
	}
389
 
390
	public String getCommunicationAddress() {
391
		return communicationAddress;
392
	}
393
 
394
	public void setCommunicationAddress(String communicationAddress) {
395
		this.communicationAddress = communicationAddress;
396
	}
397
 
398
	public String getId() {
399
		return id;
400
	}
401
 
402
	public void setId(String id) {
403
		this.id = id;
404
	}
405
 
406
	public List<Supplier> getSuppliers() {
407
		return Utils.getSuppliers();
408
	}
409
 
410
	public void setSuppliers(List<Supplier> suppliers) {
411
		this.suppliers = suppliers;
412
	}
413
 
414
	public String getErrorMessage() {
415
		return errorMessage;
416
	}
417
 
418
	public void setErrorMessage(String errorMessage) {
419
		this.errorMessage = errorMessage;
420
	}
421
 
422
	public Supplier getSupplier() {
423
		return supplier;
424
	}
425
 
426
	public void setSupplier(Supplier supplier) {
427
		this.supplier = supplier;
428
	}
429
 
7410 amar.kumar 430
	public List<Long> getBillingWarehouseIds() {
431
		return billingWarehouseIds;
432
	}
433
 
434
	public void setBillingWarehouseIds(List<Long> billingWarehouseIds) {
435
		this.billingWarehouseIds = billingWarehouseIds;
436
	}
10295 amar.kumar 437
 
24922 amit.gupta 438
	public long getStateId() {
10295 amar.kumar 439
		return stateId;
440
	}
441
 
24922 amit.gupta 442
	public String getStateName(long stateId) {
443
		return Utils.getStateIdMap().get(stateId).getStateName();
444
	}
445
 
10295 amar.kumar 446
	public void setStateId(long stateId) {
447
		this.stateId = stateId;
448
	}
14072 manish.sha 449
 
450
	public long getPoValidityLimit() {
451
		return poValidityLimit;
452
	}
453
 
454
	public void setPoValidityLimit(long poValidityLimit) {
455
		this.poValidityLimit = poValidityLimit;
456
	}
14090 manish.sha 457
 
458
	public String getStateIdVal() {
459
		return stateIdVal;
460
	}
461
 
462
	public void setStateIdVal(String stateIdVal) {
463
		this.stateIdVal = stateIdVal;
464
	}
23081 amit.gupta 465
 
466
	public void setTnc(String tnc) {
467
		this.tnc = tnc;
468
	}
469
 
470
	public String getTnc() {
471
		return tnc;
472
	}
24922 amit.gupta 473
 
5591 mandeep.dh 474
}