Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
30003 tejbeer 1
$(function() {
2
	$(document).on('click', ".partner-brandwise-detail", function() {
3
		loadDetails("main-content");
4
	});
5
 
30047 tejbeer 6
	$(document).on('change', "#activationType", function() {
30046 tejbeer 7
		var row = $(this).closest("tr");
30047 tejbeer 8
		var code = $(row).find("td:eq(1)").text();
34030 ranu 9
		var activationType = $(row).find("td:eq(24) option:selected").val();
30047 tejbeer 10
 
30046 tejbeer 11
		if (confirm("Are you sure ") == true) {
30047 tejbeer 12
			doPostAjaxRequestHandler(context + "/changeActivationType?code=" + code + "&activationType=" + activationType,
13
				function(response) {
14
 
15
					console.log(response);
16
 
17
					if (response == true) {
18
						loadDetails("main-content");
19
					}
20
 
21
				});
22
 
23
 
24
		}
30046 tejbeer 25
	});
30003 tejbeer 26
});
27
 
28
 
29
function loadDetails(domId) {
30
 
31
	doGetAjaxRequestHandler(context + "/getPartnersBrandWiseDetail",
32
		function(response) {
33
			$('#' + domId).html(response);
34
		});
35
}
36
 
30046 tejbeer 37
 
34437 ranu 38
// create suggested po cart related js start here
30046 tejbeer 39
 
34437 ranu 40
let cart = {};
41
 
42
function renderCart() {
43
	let cartHtml = '';
44
	let totalAmount = 0;
45
 
46
	// First loop through cart and calculate totalAmount
47
	for (const id in cart) {
48
		const item = cart[id];
49
		const itemTotal = item.qty * item.price;
50
		totalAmount += itemTotal;
51
	}
52
 
53
	// Add total at the top
54
	if (totalAmount > 0) {
55
		cartHtml += `<div style="text-align:right;font-weight:bold; margin-bottom: 10px;">Total: &#8377; ${totalAmount.toFixed(2)}</div><hr style="margin: 4px 0;border-top: 1px solid #000">`;
56
	}
57
 
58
	// Now loop again to render items
59
	for (const id in cart) {
60
		const item = cart[id];
61
		const itemTotal = item.qty * item.price;
62
 
63
		cartHtml += `
64
			<li style="display: flex; align-items: center; justify-content: space-between; padding: 5px 0;">
34449 ranu 65
				<div style="flex: 1; min-width: 200px;">${item.model} <br> <span style="color: gray;">- ${item.color}</div>
34437 ranu 66
				<div style="display: flex; align-items: center; gap: 5px;">
67
					<button class="dec-btn" data-id="${id}">-</button>
68
					<span class="cart-qty">${item.qty}</span>
69
					<button class="inc-btn" data-id="${id}">+</button>
70
					<button class="remove-btn btn btn-xs btn-danger" data-id="${id}">x</button>
71
				</div>
72
				<span style="width: 80px; text-align: right;">&#8377; ${itemTotal.toFixed(2)}</span>
73
			</li>`;
74
	}
75
 
76
	$('#custom-cart-section').html(cartHtml);
77
}
78
 
34449 ranu 79
$(document).on('click', '.po-radio-button', function () {
80
	const selectedRadio = $(this).find('input[type="radio"]');
34437 ranu 81
 
34449 ranu 82
	const itemId = selectedRadio.data('itemid');
83
	const model = selectedRadio.data('modelnumber');
84
	const color = selectedRadio.data('color');
85
	const price = parseFloat(selectedRadio.data('price')) || 0;
34437 ranu 86
 
34449 ranu 87
	const key = itemId;
88
 
89
	if (cart[key]) {
90
		cart[key].qty += 1;
34437 ranu 91
	} else {
34449 ranu 92
		cart[key] = {model, color, qty: 1, price};
34437 ranu 93
	}
94
 
95
	renderCart();
96
});
97
 
98
 
99
$(document).on('click', '.inc-btn', function () {
100
	const id = $(this).data('id');
101
	cart[id].qty += 1;
102
	renderCart();
103
});
104
 
105
$(document).on('click', '.dec-btn', function () {
106
	const id = $(this).data('id');
107
	if (cart[id].qty > 1) {
108
		cart[id].qty -= 1;
109
	} else {
110
		delete cart[id];
111
	}
112
	renderCart();
113
});
114
 
115
$(document).on('click', '.remove-btn', function () {
116
	const id = $(this).data('id');
117
	delete cart[id];
118
	renderCart();
119
});
120
 
34449 ranu 121
$(document).on('click', '.add-to-cart-btn', function () {
122
	var price = $(this).data('price');
123
	var catalogId = $(this).data('catalogid');
124
	console.log('price ', price)
125
 
126
	doGetAjaxRequestHandler(context + "/getPoCatalogsItems?price=" + price + "&catalogId=" + catalogId, function (response) {
127
 
128
		$('#poItemsDetail .modal-content').html(response);
129
 
130
	});
131
});
132
 
34490 ranu 133
$(document).on('click', '.get-stock-info', function () {
134
	var $clickedTd = $(this);
135
	var $clickedTr = $clickedTd.closest('tr');
136
 
137
	var catalogId = $clickedTd.data('catalogid');
138
	var fofoId = $clickedTd.data('fofoid');
139
 
140
	// If already expanded, remove it
141
	if ($clickedTr.next().hasClass('expanded-row')) {
142
		$clickedTr.next().remove();
143
		return;
144
	}
145
 
146
	doGetAjaxRequestHandler(context + "/getItemsByCatalog?fofoId=" + fofoId + "&catalogId=" + catalogId, function (response) {
147
		console.log('response', response);
148
 
149
		// Remove old expanded rows
150
		$('.expanded-row').remove();
151
 
152
		var expandHtml = '<tr class="expanded-row"><td colspan="10">' + buildStockInfoHtml(response) + '</td></tr>';
153
 
154
		$clickedTr.after(expandHtml);
155
	});
156
});
157
 
158
function buildStockInfoHtml(response) {
159
	if (!response) {
160
		return '<div>No stock info available.</div>';
161
	}
162
 
163
	// if it's a single object
164
	var html = '<div style="padding:10px; background:#f1f1f1; border:1px solid #ccc;">';
165
	html += '<strong>Stock Info:</strong><br>';
166
	html += '<ul>';
167
	html += '<li><strong>Item ID:</strong> ' + response.itemId + '</li>';
168
	html += '<li><strong>Availability:</strong> ' + response.availability + '</li>';
169
 
170
	if (response.itemDescription && response.itemDescription !== 0) {
171
		html += '<li><strong>Description:</strong> ' + response.itemDescription + '</li>';
172
	} else {
173
		html += '<li><strong>Description:</strong> Not available</li>';
174
	}
175
 
176
	html += '</ul></div>';
177
 
178
	return html;
179
}
180
 
181
 
182
 
34437 ranu 183
$(document).on('click', '#save-po-btn', function () {
184
	console.log("Save PO button clicked!");
185
	const fofoId = parseInt($('#fofo-id').val());
186
	console.log('fofoId:', fofoId);
187
	saveSuggestedPo(fofoId);
188
});
189
 
190
 
191
function saveSuggestedPo(fofoId) {
192
	var jsonObject = {};
193
	jsonObject.poIds = [];
194
 
195
	// Select <li> directly, not from inside <ul>
196
	$("#custom-cart-section li").each(function () {
34449 ranu 197
		const itemId = $(this).find(".dec-btn").data("id");
34437 ranu 198
		const qty = parseInt($(this).find(".cart-qty").text().trim());
199
 
34449 ranu 200
		if (!isNaN(itemId) && qty > 0) {
34437 ranu 201
			const item = {
34449 ranu 202
				itemId: itemId,
34437 ranu 203
				qty: qty
204
			};
205
			jsonObject.poIds.push(item);
206
		}
207
	});
208
 
209
	if (jsonObject.poIds.length === 0) {
210
		alert("Cart is empty!");
211
		return;
212
	}
213
 
214
	console.log("Sending PO Data:", jsonObject);
215
 
216
	if (confirm("Are you sure you want to save this Suggested PO?")) {
217
		doPostAjaxRequestWithJsonHandler(
218
			context + "/createSuggestedPo?fofoId=" + fofoId,
219
			JSON.stringify(jsonObject.poIds),
220
			function (response) {
221
				if (response === 'true' || response.includes("true")) {
222
					alert("Suggested PO created successfully!");
223
					cart = {};
224
					renderCart();
225
				} else {
226
					alert("Failed to create Suggested PO.");
227
				}
228
			}
229
		);
230
	}
231
}
232
 
233
 
234
 
235
 
236
 
237
 
238
 
239
 
240
 
241