Subversion Repositories SmartDukaan

Rev

Rev 36447 | Rev 37543 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
23506 amit.gupta 1
$(function() {
27754 amit.gupta 2
	$(document).on('click', "a.view-returns", function() {
23638 amit.gupta 3
		viewReturnStores("main-content");
23506 amit.gupta 4
	});
27754 amit.gupta 5
	$(document).on('click', "a.pending-returns", function() {
23506 amit.gupta 6
		viewPendingReturns("main-content");
7
	});
27754 amit.gupta 8
	$(document).on('click', "a.approved-returns", function() {
23506 amit.gupta 9
		viewApprovedReturns("main-content");
10
	});
11
 
27754 amit.gupta 12
	$(document).on('click', "a.view-debit-note", function() {
23539 amit.gupta 13
		viewDebitNotes("main-content");
14
	});
15
 
36316 aman 16
	$(document).on('click', "a.unsettled-purchase-returns", function () {
17
		doAjaxRequestHandler(context + "/return/unsettled", "GET", function (response) {
18
			$('#main-content').html(response);
19
		});
20
	});
21
 
22
	$(document).on('click', "a.create-purchase-return", function () {
23
		doAjaxRequestHandler(context + "/return/unsettled/new", "GET", function (response) {
24
			$('#main-content').html(response);
25
		});
26
	});
27
 
27754 amit.gupta 28
	$(document).on('click', "a.wh-display-name", function() {
23506 amit.gupta 29
		var whId = $(this).closest('tr').data('id');
30
		$("#approved-returns-container").find("tr.filter").hide().filter('[data-id="' + whId + '"]').show();
23540 amit.gupta 31
		$(this).closest('table').find('tr').removeClass('warning');
23506 amit.gupta 32
		$(this).closest('tr').addClass('warning');
33
	});
23539 amit.gupta 34
 
27754 amit.gupta 35
	$(document).on('click', "a.debit-note", function() {
23539 amit.gupta 36
		var debitNoteId = $(this).closest('tr').data('id');
37
		$("#debit-note-items").find("tr.filter").hide().filter('[data-id="' + debitNoteId + '"]').show();
29937 amit.gupta 38
		$(this).closest('table').find('tr').removeClass('alert-warning');
39
		$(this).closest('tr').addClass('alert-warning');
23539 amit.gupta 40
	});
35998 amit 41
 
42
 
27754 amit.gupta 43
	$(document).on('click', "a.request-return", function() {
23506 amit.gupta 44
		if(confirm("Are you sure?")) {
45
			createReturnRequest($(this).data("inventoryitemid"), $(this));
46
		}
47
	});
27754 amit.gupta 48
	$(document).on('click', "a.approve-return", function() {
23506 amit.gupta 49
		if(confirm("Are you sure to Approve?")) {
23638 amit.gupta 50
			approveReturnRequest($(this).closest('td').data("returnid"), $(this).closest('td'));
23506 amit.gupta 51
		}
52
	});
27754 amit.gupta 53
	$(document).on('click', "a.deny-return", function() {
23506 amit.gupta 54
		if(confirm("Are you sure to Deny?")) {
23644 amit.gupta 55
			denyReturnRequest($(this).closest('td').data("returnid"), $(this).closest('td'));
23506 amit.gupta 56
		}
57
	});
35998 amit 58
 
27763 tejbeer 59
	$(document).on('click', '#inventory-container a', function (){
23638 amit.gupta 60
		itemId=$(this).data('id');
34142 tejus.loha 61
		if(!fofoId){
62
			fofoId =$('select[name="fofo-users"]').val();
63
			console.log("fofo Id -"+fofoId);
64
		}
23638 amit.gupta 65
		doAjaxRequestHandler(context+"/return/inventory/" + fofoId + "/" + itemId, "GET", function(response){
66
			returnsTable.rows().remove().rows.add($(response)).draw(false);
67
		});
68
	});
35998 amit 69
 
70
	$(document).on('click', '.vendor_return', function () {
71
		doAjaxRequestHandler(context + "/return/to/vendor", "GET", function (response) {
72
			$('#main-content').html(response);
73
		});
74
	});
75
 
76
	// Invoice Return
77
	$(document).on('click', 'a.invoice-return', function () {
78
		doAjaxRequestHandler(context + "/return/invoice", "GET", function (response) {
79
			$('#main-content').html(response);
80
		});
81
	});
82
 
83
	// Search by Debit Note Number
84
	$(document).on('click', '#search-debit-note-btn', function () {
85
		var debitNoteNumber = $('#debit-note-number-input').val();
86
		if (!debitNoteNumber) {
87
			alert("Please enter a debit note number");
88
			return;
89
		}
90
		doAjaxRequestHandler(context + "/return/search/debit-note?debitNoteNumber=" + encodeURIComponent(debitNoteNumber), "GET", function (response) {
91
			$('#invoice-return-results').html(response);
92
		});
93
	});
94
 
95
	$(document).on('click', '#return-invoice-button', function () {
96
		var invoiceNumber = $('#invoice-number-input').val();
97
		if (!invoiceNumber) {
98
			alert("Please enter an invoice number");
99
			return;
100
		}
101
		doAjaxRequestHandler(context + "/return/invoice/search?invoiceNumber=" + encodeURIComponent(invoiceNumber), "GET", function (response) {
102
			$('#invoice-return-results').html(response);
103
		});
104
	});
105
 
36408 amit 106
	// Date-range filter
107
	$(document).on('click', '#invoice-return-date-apply', function () {
108
		var fromDate = $('#invoice-return-from-date').val();
109
		var toDate = $('#invoice-return-to-date').val();
110
		if (!fromDate || !toDate) {
111
			alert("Please pick both From and To dates");
112
			return;
113
		}
114
		if (fromDate > toDate) {
115
			alert("From date must be on or before To date");
116
			return;
117
		}
118
		doAjaxRequestHandler(context + "/return/invoice?fromDate=" + encodeURIComponent(fromDate)
119
				+ "&toDate=" + encodeURIComponent(toDate), "GET", function (response) {
120
			$('#main-content').html(response);
121
		});
122
	});
123
 
35998 amit 124
	// Debit Note Details
125
	$(document).on('click', '.debit-note-details', function () {
126
		var debitNoteId = $(this).data("debitnote-id");
127
		doAjaxRequestHandler(context + "/return/debit-note/details/" + debitNoteId, "GET", function (response) {
128
			$('#main-content').html(response);
129
		});
130
	});
131
 
132
	// Receive Debit Note
133
	$(document).on('click', '.receive-debit-note', function () {
134
		var debitNoteId = $(this).data("debitnote-id");
135
		doAjaxRequestHandler(context + "/return/debit-note/receive/" + debitNoteId, "GET", function (response) {
136
			$('#main-content').html(response);
137
		});
138
	});
139
 
37365 amit 140
	// A row whose selected condition differs from what the partner declared is a disagreement,
141
	// not a correction — the server rejects the whole debit note for it.
142
	function hasConditionMismatch() {
143
		var mismatch = false;
144
		$('#receive-debit-note-form tr[data-item-id]').each(function () {
145
			var select = $(this).find('.return-condition');
146
			if (select.val() !== select.data('declared')) {
147
				mismatch = true;
148
			}
149
		});
150
		return mismatch;
151
	}
152
 
35998 amit 153
	// Show/hide DOA certificate section based on BAD selection on mobile items
154
	$(document).on('change', '.return-condition', function () {
37365 amit 155
		var mismatch = hasConditionMismatch();
156
 
157
		// On a mismatch the receipt never happens, so the DOA certificate is not collected.
158
		if (mismatch) {
159
			$('#condition-mismatch-section').show();
160
			$('#doa-certificate-section').hide();
161
			$('#submit-receive').removeClass('btn-primary').addClass('btn-danger').text('Reject Return');
162
			return;
163
		}
164
		$('#condition-mismatch-section').hide();
165
		$('#submit-receive').removeClass('btn-danger').addClass('btn-primary').text('Confirm Receipt');
166
 
35998 amit 167
		var hasMobileBad = false;
168
		$('#receive-debit-note-form tr[data-category-id]').each(function () {
169
			var categoryId = $(this).data('category-id');
170
			var condition = $(this).find('.return-condition').val();
171
			if (condition === 'BAD' && categoryId == 10006) {
172
				hasMobileBad = true;
173
			}
174
		});
175
		if (hasMobileBad) {
176
			$('#doa-certificate-section').show();
177
		} else {
178
			$('#doa-certificate-section').hide();
179
		}
180
	});
181
 
182
	// Submit receive debit note form
183
	$(document).on('submit', '#receive-debit-note-form', function (e) {
184
		e.preventDefault();
185
 
37365 amit 186
		var mismatch = hasConditionMismatch();
187
 
188
		// Validate remarks: mandatory for BAD rows, and for any row whose condition was changed —
189
		// the remark is what the partner and the escalation chain see as the reason for rejection.
35998 amit 190
		var valid = true;
191
		$(this).find('tr[data-item-id]').each(function () {
37365 amit 192
			var select = $(this).find('.return-condition');
193
			var condition = select.val();
35998 amit 194
			var remark = $(this).find('.remark-field').val();
37365 amit 195
			if (condition !== select.data('declared') && !remark) {
196
				alert("Remark is required when you change the condition — it is sent to the partner as the rejection reason");
197
				valid = false;
198
				return false;
199
			}
35998 amit 200
			if (condition === 'BAD' && !remark) {
201
				alert("Remark is required for BAD/DOA returns");
202
				valid = false;
203
				return false;
204
			}
205
		});
206
		if (!valid) return;
207
 
37365 amit 208
		var debitNoteId = $(this).data('debitnote-id');
209
		var form = $(this);
210
 
211
		if (mismatch) {
212
			if (!confirm("The condition you selected does not match what the partner declared.\n\n"
213
					+ "This will REJECT the entire debit note and notify the partner, Logistics, RBM and Sales. "
214
					+ "No refund will be raised.\n\nProceed?")) {
215
				return;
35998 amit 216
			}
37365 amit 217
		} else {
218
			// Check DOA certificate if mobile BAD items exist
219
			var hasMobileBad = false;
220
			$(this).find('tr[data-category-id]').each(function () {
221
				if ($(this).data('category-id') == 10006 && $(this).find('.return-condition').val() === 'BAD') {
222
					hasMobileBad = true;
223
				}
224
			});
225
			if (hasMobileBad && !$('input[name="doaCertificateValid"]:checked').val()) {
226
				alert("Please select DOA certificate validity");
227
				return;
228
			}
229
			if (!confirm("Are you sure you want to confirm receipt for this debit note?")) {
230
				return;
231
			}
35998 amit 232
		}
233
 
37365 amit 234
		$.ajax({
235
			url: context + "/return/debit-note/receive/" + debitNoteId,
236
			type: "POST",
237
			data: form.serialize(),
238
			success: function (response) {
239
				if (response === 'true' || response.indexOf('true') >= 0) {
240
					alert(mismatch
241
						? "Debit note rejected due to condition mismatch. Partner and escalation chain notified."
242
						: "Debit note items received successfully");
243
					doAjaxRequestHandler(context + "/return/invoice", "GET", function (resp) {
244
						$('#main-content').html(resp);
245
					});
246
				} else {
247
					alert((mismatch ? "Rejection failed: " : "Receipt failed: ") + response);
35998 amit 248
				}
37365 amit 249
			},
250
			error: function (xhr) {
251
				alert("Error: " + xhr.responseText);
252
			}
253
		});
35998 amit 254
	});
255
 
256
	// Refund Debit Note - opens refund view with receipt details
257
	$(document).on('click', '.refund-debit-note', function () {
258
		var debitNoteId = $(this).data("debitnote-id");
259
		doAjaxRequestHandler(context + "/return/debit-note/refund/" + debitNoteId, "GET", function (response) {
260
			$('#main-content').html(response);
261
		});
262
	});
263
 
264
	// Confirm refund with finance remark
265
	$(document).on('click', '.refund-debit-note-confirm', function () {
266
		var debitNoteId = $(this).data("debitnote-id");
267
		var financeRemark = $('#finance-remark').val();
268
		if (confirm("Are you sure you want to process refund for debit note " + debitNoteId + "?")) {
269
			doAjaxRequestHandler(context + "/return/debit-note/refund/" + debitNoteId + "?financeRemark=" + encodeURIComponent(financeRemark), "PUT", function (response) {
270
				if (response === 'true') {
271
					alert("Debit note refunded successfully");
272
					doAjaxRequestHandler(context + "/return/invoice", "GET", function (resp) {
273
						$('#main-content').html(resp);
274
					});
275
				} else {
276
					alert("Refund failed: " + response);
277
				}
278
			});
279
		}
280
	});
281
 
282
	// Reject debit note return
283
	$(document).on('click', '.reject-debit-note-confirm', function () {
284
		var debitNoteId = $(this).data("debitnote-id");
285
		var rejectRemark = $('#reject-remark').val();
286
		if (!rejectRemark) {
287
			alert("Please enter a reject remark");
288
			return;
289
		}
290
		if (confirm("Are you sure you want to reject return for debit note " + debitNoteId + "? This will reverse all scans and quantities.")) {
291
			doAjaxRequestHandler(context + "/return/debit-note/reject/" + debitNoteId + "?rejectRemark=" + encodeURIComponent(rejectRemark), "PUT", function (response) {
292
				if (response === 'true') {
293
					alert("Return rejected successfully");
294
					doAjaxRequestHandler(context + "/return/invoice", "GET", function (resp) {
295
						$('#main-content').html(resp);
296
					});
297
				} else {
298
					alert("Rejection failed: " + response);
299
				}
300
			});
301
		}
302
	});
36022 amit 303
 
36027 amit 304
	// Approve Invoice Return (finance approval for shipped invoices)
305
	$(document).on('click', '.approve-invoice-return', function () {
306
		var proId = $(this).data("pro-id");
307
		if (confirm("Are you sure you want to approve and refund this invoice return?")) {
308
			doAjaxRequestHandler(context + "/return/invoice/approve/" + proId, "PUT", function (response) {
309
				if (response === 'true') {
310
					alert("Invoice return approved and refunded successfully");
311
					doAjaxRequestHandler(context + "/return/invoice", "GET", function (resp) {
312
						$('#main-content').html(resp);
313
					});
314
				} else {
315
					alert("Approval failed: " + response);
316
				}
317
			});
318
		}
319
	});
320
 
36408 amit 321
	// Reject Invoice Return — sale stands, no inventory/wallet/GST action
322
	$(document).on('click', '.reject-invoice-return', function () {
323
		var proId = $(this).data("pro-id");
324
		var rejectRemark = prompt("Reason for rejecting this invoice return? (sale will stand, no refund will be issued)");
325
		if (!rejectRemark) {
326
			return;
327
		}
328
		if (confirm("Reject invoice return and let the sale stand? This cannot be undone.")) {
329
			doAjaxRequestHandler(context + "/return/invoice/reject/" + proId + "?rejectRemark=" + encodeURIComponent(rejectRemark), "PUT", function (response) {
330
				if (response === 'true') {
331
					alert("Invoice return rejected");
332
					doAjaxRequestHandler(context + "/return/invoice", "GET", function (resp) {
333
						$('#main-content').html(resp);
334
					});
335
				} else {
336
					alert("Rejection failed: " + response);
337
				}
338
			});
339
		}
340
	});
341
 
36447 amit 342
	// Acknowledge rejected return — retailer confirms goods received back
343
	$(document).on('click', '.acknowledge-rejected-return', function () {
344
		var debitNoteId = $(this).data("debitnote-id");
345
		if (confirm("Confirm that you have received the rejected goods back? This will restore your inventory.")) {
346
			doAjaxRequestHandler(context + "/return/debit-note/reject/acknowledge/" + debitNoteId, "PUT", function (response) {
347
				if (response === 'true') {
348
					alert("Goods acknowledged. Inventory restored.");
349
					doAjaxRequestHandler(context + "/return/invoice", "GET", function (resp) {
350
						$('#main-content').html(resp);
351
					});
352
				} else {
353
					alert("Acknowledgment failed: " + response);
354
				}
355
			});
356
		}
357
	});
358
 
36022 amit 359
	// Process Invoice Return (non-GRN'd)
360
	$(document).on('click', '.process-invoice-return', function () {
361
		var invoiceNumber = $(this).data("invoice");
362
		var remark = $('#invoice-return-remark').val();
363
		if (confirm("Are you sure you want to process return for invoice " + invoiceNumber + "?")) {
364
			doAjaxRequestHandler(context + "/return/invoice/process?invoiceNumber=" + encodeURIComponent(invoiceNumber) + "&remark=" + encodeURIComponent(remark), "PUT", function (response) {
365
				if (response === 'true') {
366
					alert("Invoice return processed successfully");
367
					doAjaxRequestHandler(context + "/return/invoice", "GET", function (resp) {
368
						$('#main-content').html(resp);
369
					});
370
				} else {
371
					alert("Return failed: " + response);
372
				}
373
			});
374
		}
375
	});
23506 amit.gupta 376
});
377
 
23638 amit.gupta 378
function viewReturnStores(domId){
379
	doAjaxRequestHandler(context+"/return/stores", "GET", function(response){
23506 amit.gupta 380
		$('#' + domId).html(response);
381
	});
382
}
383
 
384
function viewPendingReturns(domId){
385
	doAjaxRequestHandler(context+"/return/pending", "GET", function(response){
386
		$('#' + domId).html(response);
387
	});
388
}
389
 
390
function viewApprovedReturns(domId){
391
	doAjaxRequestHandler(context+"/return/approved", "GET", function(response){
392
		$('#' + domId).html(response);
393
		$("#approved-returns").find('a:first').click();
394
	});
395
}
396
 
23539 amit.gupta 397
function viewDebitNotes(domId){
398
	doAjaxRequestHandler(context+"/return/debit-note-created", "GET", function(response){
399
		$('#' + domId).html(response);
400
		$("#debit-notes").find('a:first').click();
401
	});
402
}
403
 
23506 amit.gupta 404
function createReturnRequest(inventoryItemId, container){
405
	doAjaxRequestHandler(context+"/return/inventory/" + inventoryItemId, "PUT", function(response){
406
		if(response=='true') {
407
			container.closest('td').html('Return requested');
408
		}
409
	});
410
}
411
 
412
function approveReturnRequest(inventoryItemId, container){
413
	doAjaxRequestHandler(context+"/return/inventory/approve/" + inventoryItemId, "PUT", function(response){
414
		if(response=='true') {
415
			container.closest('td').html('Return Approved');
416
		}
417
	});
418
}
419
 
420
function denyReturnRequest(inventoryItemId, container){
421
	doAjaxRequestHandler(context+"/return/inventory/deny/" + inventoryItemId, "PUT", function(response){
422
		if(response=='true') {
423
			container.closest('td').html('Return denied');
424
		}
425
	});
426
}
427
 
428
function viewDebitNote(warehouseId){
429
	doAjaxRequestHandler(context+"/return/debit-note/view/" + warehouseId, "GET", function(response){
430
		//Download PDF code
431
	});
432
}
433
 
434
function generateDebitNote(warehouseId){
435
	doAjaxRequestHandler(context+"/return/debit-note/generate/" + warehouseId, "PUT", function(response){
436
		if(response=='true') {
35998 amit 437
			//Download the pdf
23506 amit.gupta 438
		}
35998 amit 439
	});
23506 amit.gupta 440
}
441
 
442
function getDebitNote(debitNotedId) {
443
	doAjaxRequestHandler(context+"/return/debit-note/" + debitNotedId, "GET", function(response){
35998 amit 444
 
23506 amit.gupta 445
	});
446
}