Subversion Repositories SmartDukaan

Rev

Rev 34581 | 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();
34548 ranu 9
		var activationType = $(row).find("#activationType").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');
34581 ranu 124
	var warehouseId = $(this).data('warehouseid');
34449 ranu 125
	console.log('price ', price)
126
 
34581 ranu 127
	doGetAjaxRequestHandler(context + "/getPoCatalogsItems?warehouseId=" + warehouseId + "&price=" + price + "&catalogId=" + catalogId, function (response) {
34449 ranu 128
 
129
		$('#poItemsDetail .modal-content').html(response);
130
 
131
	});
132
});
133
 
34490 ranu 134
$(document).on('click', '.get-stock-info', function () {
135
	var $clickedTd = $(this);
136
	var $clickedTr = $clickedTd.closest('tr');
137
 
138
	var catalogId = $clickedTd.data('catalogid');
139
	var fofoId = $clickedTd.data('fofoid');
140
 
141
	// If already expanded, remove it
142
	if ($clickedTr.next().hasClass('expanded-row')) {
143
		$clickedTr.next().remove();
144
		return;
145
	}
146
 
147
	doGetAjaxRequestHandler(context + "/getItemsByCatalog?fofoId=" + fofoId + "&catalogId=" + catalogId, function (response) {
148
		console.log('response', response);
149
 
150
		// Remove old expanded rows
151
		$('.expanded-row').remove();
152
 
34495 ranu 153
		var expandHtml = '<tr class="expanded-row"><td colspan="10">' + buildStockInfoHtml(response.response) + '</td></tr>';
34490 ranu 154
 
155
		$clickedTr.after(expandHtml);
156
	});
157
});
158
 
34495 ranu 159
// function to build HTML when response is a LIST
160
function buildStockInfoHtml(responseList) {
161
	console.log("responseList {}", responseList);
162
	if (!Array.isArray(responseList) || responseList.length === 0) {
34490 ranu 163
		return '<div>No stock info available.</div>';
164
	}
165
 
34495 ranu 166
	var html = '<div style="padding:10px; background:#f9f9f9; border:1px solid #ccc;">';
167
	html += '<strong>Stock Details:</strong>';
35771 ranu 168
	html += '<span style="margin-left:15px; color:#dc3545; font-size:12px;"><i class="fa fa-circle"></i> Activated but not billed</span>';
34495 ranu 169
	html += '<table class="table table-bordered" style="margin-top:10px;">';
35771 ranu 170
	html += '<thead><tr><th>Item ID</th><th>Availability</th><th>Description</th><th>Status</th></tr></thead>';
34495 ranu 171
	html += '<tbody>';
34490 ranu 172
 
34495 ranu 173
	responseList.forEach(function (item) {
35771 ranu 174
		var rowStyle = item.hasActivatedImei ? 'style="background-color: #f8d7da; color: #721c24;"' : '';
175
		html += '<tr ' + rowStyle + '>';
34495 ranu 176
		html += '<td>' + item.itemId + '</td>';
177
		html += '<td>' + item.availability + '</td>';
178
		html += '<td>' + (item.itemDescription && item.itemDescription !== 0 ? item.itemDescription : 'Not Available') + '</td>';
35771 ranu 179
		html += '<td>';
180
		if (item.hasActivatedImei && item.activatedImeis && item.activatedImeis.length > 0) {
181
			html += '<span style="color:#dc3545; font-weight:bold;">Activated - Need to Bill (' + item.activatedImeis.length + ')</span>';
182
			html += '<div style="margin-top:5px; padding:5px; background:#fff; border:1px solid #dc3545; border-radius:4px;">';
183
			html += '<small><strong>IMEI(s):</strong></small>';
184
			item.activatedImeis.forEach(function (imei, index) {
185
				html += '<span style="display:inline-block; margin:2px 5px; padding:2px 6px; background:#f8d7da; border-radius:3px; font-size:11px;">' + imei + '</span>';
186
			});
187
			html += '</div>';
188
		} else {
189
			html += '-';
190
		}
191
		html += '</td>';
34495 ranu 192
		html += '</tr>';
193
	});
34490 ranu 194
 
34495 ranu 195
	html += '</tbody></table></div>';
34490 ranu 196
 
197
	return html;
198
}
199
 
200
 
201
 
34437 ranu 202
$(document).on('click', '#save-po-btn', function () {
203
	console.log("Save PO button clicked!");
204
	const fofoId = parseInt($('#fofo-id').val());
205
	console.log('fofoId:', fofoId);
206
	saveSuggestedPo(fofoId);
207
});
208
 
209
 
210
function saveSuggestedPo(fofoId) {
211
	var jsonObject = {};
212
	jsonObject.poIds = [];
213
 
214
	// Select <li> directly, not from inside <ul>
215
	$("#custom-cart-section li").each(function () {
34449 ranu 216
		const itemId = $(this).find(".dec-btn").data("id");
34437 ranu 217
		const qty = parseInt($(this).find(".cart-qty").text().trim());
218
 
34449 ranu 219
		if (!isNaN(itemId) && qty > 0) {
34437 ranu 220
			const item = {
34449 ranu 221
				itemId: itemId,
34437 ranu 222
				qty: qty
223
			};
224
			jsonObject.poIds.push(item);
225
		}
226
	});
227
 
228
	if (jsonObject.poIds.length === 0) {
229
		alert("Cart is empty!");
230
		return;
231
	}
232
 
233
	console.log("Sending PO Data:", jsonObject);
234
 
235
	if (confirm("Are you sure you want to save this Suggested PO?")) {
236
		doPostAjaxRequestWithJsonHandler(
237
			context + "/createSuggestedPo?fofoId=" + fofoId,
238
			JSON.stringify(jsonObject.poIds),
239
			function (response) {
240
				if (response === 'true' || response.includes("true")) {
241
					alert("Suggested PO created successfully!");
242
					cart = {};
243
					renderCart();
244
				} else {
245
					alert("Failed to create Suggested PO.");
246
				}
247
			}
248
		);
249
	}
250
}
251
 
252
 
253
 
254
 
255
 
256
 
257
 
258
 
259
 
260