Subversion Repositories SmartDukaan

Rev

Rev 32192 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
32145 tejbeer 1
$(function() {
2
	$(document).on('click', ".warehouse-create-purchase-order", function() {
3
		loadCreatePurchase("main-content");
4
	});
5
 
6
 
7
 
8
	$(document).on('click', ".purchaseorderitemview", function() {
9
 
10
		var vendorId = $('#vendorId').val();
11
		var warehouseId = $('#warehouseMap').val();
12
 
13
		console.log(warehouseId)
14
 
15
		if (warehouseId == null && vendorId === "") {
16
			alert("Field can't be empty");
17
			return;
18
		}
19
 
20
 
21
		if (warehouseId == null) {
22
			alert("please select warehouse");
23
			return;
24
		}
25
		if (vendorId === "") {
26
			alert("please select vendor");
27
			return;
28
		}
29
		doGetAjaxRequestHandler(context + "/addPurchaseItemView?warehouseId=" + warehouseId + "&vendorId=" + vendorId, function(response) {
30
			$(".createpurchaseordercontainer").html(response);
31
		});
32
	});
33
 
34
	var itemIds = [];
35
 
36
	$(document).on('click', ".addRowInPurchaseOrder", function() {
37
		var $html = $('<tr>  <td>  <input type="text" class="form-control typeaheaditem"   autocomplete="off"  placeholder="Search Model"/> </td> <td>  <input type="number" class="form-control qty"  name = "qty" placeholder="Quantity"/> </td><td>  <input type="number" class="form-control transferPrice"  disable placeholder="Transfer Price"/> </td>	<td>  <input type="number" class="form-control totalValue"  disable placeholder="Total Value"/> </td> <td> <input class="form-control btn btn-primary removeInPurchaseOrder" type="button" value="Remove"></td>  </tr> ');
38
 
39
 
40
		getItemAheadOptions($html.find('.typeaheaditem'), true, function(
41
			selectedItem) {
42
			var itemId = selectedItem.itemId;
43
 
44
			var vendorId = $('#vendorId').val();
45
 
46
			if (itemIds.indexOf(itemId) > -1) {
47
				alert("item already selected");
48
				$html.find('.typeaheaditem').val("")
49
 
50
				return false;
51
			}
52
 
53
			itemIds.push(itemId)
54
 
55
 
56
 
57
			doGetAjaxRequestHandler(context + "/getPricing?vendorId=" + vendorId + "&itemId=" + itemId, function(response) {
58
				console.log(response)
59
				if (response != "null") {
60
					var jsonObj = JSON.parse(response);
61
 
62
					console.log(jsonObj)
63
					$html.find('.transferPrice').val(jsonObj.transferPrice)
64
 
65
					$html.find('.transferPrice').data('itemid', itemId)
66
				} else {
67
 
68
					console.log(response)
69
					$html.find('.typeaheaditem').val("")
70
 
71
				}
72
			});
73
 
74
		});
75
		$('tbody').append($html);
76
 
77
	});
78
 
79
	$(document).on('click', '.createSendPurchaseOrder', function() {
80
 
81
		createPurchase(true);
82
 
83
	});
84
 
85
 
86
	$(document).on('click', '.createPurchaseOrder', function() {
87
 
88
 
89
		createPurchase(false);
90
	});
91
 
92
 
93
	$(document).on('change', '.qty', function() {
94
 
95
 
96
		var row = $(this).closest("tr");
97
		var qty = $(row).find(".qty").val();
98
		var transferPrice = $(row).find(".transferPrice").val();
99
 
100
		var totalVal = transferPrice * qty;
101
 
102
		var totalValue = $(row).find(".totalValue").val(totalVal);
103
 
104
		var purchaseOrderValue = 0;
105
 
106
		$("table > tbody > tr").each(function() {
107
 
108
			var totalValue = $(this).find(".totalValue").val();
109
			purchaseOrderValue += totalValue
110
 
111
		});
112
 
113
	});
114
 
115
 
116
 
117
	$(document).on("click", '.removeInPurchaseOrder', function() {
118
 
119
		var row = $(this).closest("tr");
120
		var itemId = $(this).find(".transferPrice").data('itemid');
121
		itemIds.splice(itemIds.indexOf(itemId), 1);
122
		row.remove();
123
	});
124
 
125
 
126
	$(document).on('click', '.warehouse-open-purchase-order', function() {
127
		loadOpenPurchase("main-content");
128
	});
129
 
130
 
131
 
132
	$(document).on('click', '.editViewPurchaseOrder', function() {
133
		var purchaseOrderId = $(this).data('poid');
134
		doGetAjaxRequestHandler(context + "/getEditPOByPurchaseId?purchaseId=" + purchaseOrderId, function(response) {
135
			$('#warehouseEditPurchaseContainer .modal-content').html(response);
136
		});
137
 
138
	});
139
 
140
	$(document).on('click', '.editPurchaseOrder', function() {
141
		var purchaseOrderId = $(this).data('poid');
142
 
143
		editPurchase(true, purchaseOrderId);
144
 
145
	});
146
 
147
 
148
	$(document).on('click', '.editSendPurchaseOrder', function() {
149
		var purchaseOrderId = $(this).data('poid');
150
 
151
 
152
		editPurchase(false, purchaseOrderId);
153
	});
154
 
155
	$(document).on('click', '.closePurchaseOrder', function() {
156
		var purchaseOrderId = $(this).data('poid');
157
		if (confirm("Are you sure you want to close the PO") == true) {
158
			doPostAjaxRequestHandler(context + "/closePuchaseOrder?purchaseId=" + purchaseOrderId,
159
				function(response) {
160
 
161
					console.log(response);
162
					if (response == 'true') {
163
						alert("successfully closed");
164
						loadOpenPurchase("main-content")
165
					}
166
				});
167
		}
168
	});
169
 
170
 
171
	$(document).on('click', '.warehouse-view-purchase-order', function() {
172
		doGetAjaxRequestHandler(context + "/viewPurchaseOrder", function(response) {
173
			$('#' + 'main-content').html(response);
174
		});
175
	});
176
 
177
 
178
 
179
	$(document).on('click', '.dateWisePo', function() {
180
 
181
		var startDate = getDatesFromPicker('input[name="duration"]').startDate;
182
		var endDate = getDatesFromPicker('input[name="duration"]').endDate
183
 
184
 
185
		doGetAjaxRequestHandler(context + "/getPurchaseOrders?startDate=" + startDate + "&endDate=" + endDate, function(response) {
186
			$('.purchaseorderviewcontainer').html(response);
187
 
188
		});
189
 
190
 
191
	});
192
 
193
 
194
 
195
	$(document).on('click', '.viewPurchaseOrderlineItem', function() {
196
		var purchaseOrderId = $(this).data('poid');
197
 
198
		doGetAjaxRequestHandler(context + "/getWarehouseLineItemByPurchaseId?purchaseId=" + purchaseOrderId, function(response) {
199
			$('#warehouseLineItem .modal-content').html(response);
200
 
201
		});
202
	});
203
 
204
 
205
 
206
});
207
 
208
function loadCreatePurchase(domId) {
209
	doGetAjaxRequestHandler(context + "/warehousePurchaseOrder", function(response) {
210
		$('#' + domId).html(response);
211
	});
212
}
213
 
214
function loadOpenPurchase(domId) {
215
 
216
	doGetAjaxRequestHandler(context + "/getOpenPurchaseOrder", function(response) {
217
		$('#' + domId).html(response);
218
	});
219
}
220
 
221
function createPurchase(sendPo) {
222
	var purchaseOrder = {};
223
 
224
	var vendorId = $('#vendorId').val();
225
 
226
	var warehouseId = $('#warehouseMap').val();
227
 
228
 
229
	var items = []
230
	$("table > tbody > tr").each(function() {
231
		var purchaseOrderJson = {};
232
 
233
		var itemId = $(this).find(".transferPrice").data('itemid');
234
		var qty = $(this).find(".qty").val();
235
		var transferPrice = $(this).find(".transferPrice").val();
236
		var totalValue = $(this).find(".totalValue").val();
237
 
238
 
239
		purchaseOrderJson['itemId'] = itemId;
240
		purchaseOrderJson['qty'] = qty
241
		purchaseOrderJson['totalValue'] = totalValue
242
		purchaseOrderJson['transferPrice'] = transferPrice
243
 
244
		items.push(purchaseOrderJson)
245
	});
246
 
247
 
248
	for (var i = 0; i < items.length; i++) {
249
		console.log(items[i])
250
 
251
		var itemId = items[i].itemId
252
		var qty = items[i].qty
253
		var transferPrice = items[i].transferPrice
254
		var totalValue = items[i].totalValue
255
 
256
		if (itemId === "" && qty === "" && transferPrice === "" && totalValue === "") {
257
			alert("Field can't be empty");
258
			return false;
259
		}
260
 
261
 
262
		if (itemId === "") {
263
			alert("please select item");
264
			return false;
265
		}
266
		if (qty === "") {
267
			alert("please choose qty");
268
			return false;
269
		}
270
 
271
		if (transferPrice === "") {
272
			alert("Transfer Price can't be empty");
273
			return false;
274
		}
275
		if (totalValue === "") {
276
			alert("Total value can't be empty");
277
			return false;
278
		}
279
 
280
 
281
 
282
	}
283
 
284
 
285
 
286
 
287
	purchaseOrder['vendorId'] = vendorId;
288
	purchaseOrder['warehouseId'] = warehouseId
289
	purchaseOrder['sendPo'] = sendPo
290
 
291
	purchaseOrder['items'] = items
292
 
293
	console.log(purchaseOrder)
294
 
295
 
296
 
297
	if (confirm("Are you sure you want to create purchase order") == true) {
298
 
299
		doPostAjaxRequestWithJsonHandler(context
300
			+ "/createPurchaseOrder", JSON
301
				.stringify(purchaseOrder), function(response) {
302
					if (response == 'true') {
303
						alert("successfully created");
304
					}
305
				});
306
	}
307
 
308
}
309
 
310
 
311
function editPurchase(sendPo, poid) {
312
	var editpurchaseOrder = {}
313
	var items = []
314
	$("#purchase-edit-lineitem > tbody > tr").each(function() {
315
		var purchaseOrderJson = {};
316
		var itemId = $(this).find("td:eq(0)").text();
317
 
318
		var qty = $(this).find(".editqty").val();
319
 
320
		purchaseOrderJson['itemId'] = itemId;
321
		purchaseOrderJson['qty'] = qty
322
 
323
		items.push(purchaseOrderJson)
324
	});
325
 
326
 
327
	for (var i = 0; i < items.length; i++) {
328
		console.log(items[i])
329
 
330
		var qty = items[i].qty
331
 
332
		if (qty === "") {
333
			alert("please choose qty");
334
			return false;
335
		}
336
 
337
		if (qty < 0) {
338
			alert("Quantity should be greater than 0");
339
			return false;
340
		}
341
 
342
 
343
	}
344
 
345
	editpurchaseOrder['purchaseOrderId'] = poid;
346
	editpurchaseOrder['sendPo'] = sendPo
347
	editpurchaseOrder['items'] = items
348
 
349
 
350
	if (confirm("Are you sure you want to edit purchase order") == true) {
351
 
352
		doPostAjaxRequestWithJsonHandler(context
353
			+ "/editPurchaseOrder", JSON
354
				.stringify(editpurchaseOrder), function(response) {
355
					if (response == 'true') {
356
						alert("successfully created");
357
					}
358
				});
359
	}
360
 
361
}
362
 
363