Subversion Repositories SmartDukaan

Rev

Rev 34030 | Rev 34449 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 34030 Rev 34437
Line 34... Line 34...
34
			$('#' + domId).html(response);
34
			$('#' + domId).html(response);
35
		});
35
		});
36
}
36
}
37
 
37
 
38
 
38
 
-
 
39
// create suggested po cart related js start here
-
 
40
 
-
 
41
let cart = {};
-
 
42
 
-
 
43
function renderCart() {
-
 
44
	let cartHtml = '';
-
 
45
	let totalAmount = 0;
-
 
46
 
-
 
47
	// First loop through cart and calculate totalAmount
-
 
48
	for (const id in cart) {
-
 
49
		const item = cart[id];
-
 
50
		const itemTotal = item.qty * item.price;
-
 
51
		totalAmount += itemTotal;
-
 
52
	}
-
 
53
 
-
 
54
	// Add total at the top
-
 
55
	if (totalAmount > 0) {
-
 
56
		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">`;
-
 
57
	}
-
 
58
 
-
 
59
	// Now loop again to render items
-
 
60
	for (const id in cart) {
-
 
61
		const item = cart[id];
-
 
62
		const itemTotal = item.qty * item.price;
-
 
63
 
-
 
64
		cartHtml += `
-
 
65
			<li style="display: flex; align-items: center; justify-content: space-between; padding: 5px 0;">
-
 
66
				<div style="flex: 1; min-width: 200px;">${item.model}</div>
-
 
67
				<div style="display: flex; align-items: center; gap: 5px;">
-
 
68
					<button class="dec-btn" data-id="${id}">-</button>
-
 
69
					<span class="cart-qty">${item.qty}</span>
-
 
70
					<button class="inc-btn" data-id="${id}">+</button>
-
 
71
					<button class="remove-btn btn btn-xs btn-danger" data-id="${id}">x</button>
-
 
72
				</div>
-
 
73
				<span style="width: 80px; text-align: right;">&#8377; ${itemTotal.toFixed(2)}</span>
-
 
74
			</li>`;
-
 
75
	}
-
 
76
 
-
 
77
	$('#custom-cart-section').html(cartHtml);
-
 
78
}
-
 
79
 
-
 
80
 
-
 
81
$(document).on('click', '.add-to-cart-btn', function () {
-
 
82
	const catalogId = $(this).data('catalogid');
-
 
83
	const model = $(this).data('model');
-
 
84
	const price = parseFloat($(this).data('price')) || 0;
-
 
85
 
-
 
86
	if (cart[catalogId]) {
-
 
87
		cart[catalogId].qty += 1;
-
 
88
	} else {
-
 
89
		cart[catalogId] = {model, qty: 1, price};
-
 
90
	}
-
 
91
 
-
 
92
	renderCart();
-
 
93
});
-
 
94
 
-
 
95
 
-
 
96
$(document).on('click', '.inc-btn', function () {
-
 
97
	const id = $(this).data('id');
-
 
98
	cart[id].qty += 1;
-
 
99
	renderCart();
-
 
100
});
-
 
101
 
-
 
102
$(document).on('click', '.dec-btn', function () {
-
 
103
	const id = $(this).data('id');
-
 
104
	if (cart[id].qty > 1) {
-
 
105
		cart[id].qty -= 1;
-
 
106
	} else {
-
 
107
		delete cart[id];
-
 
108
	}
-
 
109
	renderCart();
-
 
110
});
-
 
111
 
-
 
112
$(document).on('click', '.remove-btn', function () {
-
 
113
	const id = $(this).data('id');
-
 
114
	delete cart[id];
-
 
115
	renderCart();
-
 
116
});
-
 
117
 
-
 
118
$(document).on('click', '#save-po-btn', function () {
-
 
119
	console.log("Save PO button clicked!");
-
 
120
	const fofoId = parseInt($('#fofo-id').val());
-
 
121
	console.log('fofoId:', fofoId);
-
 
122
	saveSuggestedPo(fofoId);
-
 
123
});
-
 
124
 
-
 
125
 
-
 
126
function saveSuggestedPo(fofoId) {
-
 
127
	var jsonObject = {};
-
 
128
	jsonObject.poIds = [];
-
 
129
 
-
 
130
	// Select <li> directly, not from inside <ul>
-
 
131
	$("#custom-cart-section li").each(function () {
-
 
132
		const catalogId = $(this).find(".dec-btn").data("id");
-
 
133
		const qty = parseInt($(this).find(".cart-qty").text().trim());
-
 
134
 
-
 
135
		if (!isNaN(catalogId) && qty > 0) {
-
 
136
			const item = {
-
 
137
				catalogId: catalogId,
-
 
138
				qty: qty
-
 
139
			};
-
 
140
			jsonObject.poIds.push(item);
-
 
141
		}
-
 
142
	});
-
 
143
 
-
 
144
	if (jsonObject.poIds.length === 0) {
-
 
145
		alert("Cart is empty!");
-
 
146
		return;
-
 
147
	}
-
 
148
 
-
 
149
	console.log("Sending PO Data:", jsonObject);
-
 
150
 
-
 
151
	if (confirm("Are you sure you want to save this Suggested PO?")) {
-
 
152
		doPostAjaxRequestWithJsonHandler(
-
 
153
			context + "/createSuggestedPo?fofoId=" + fofoId,
-
 
154
			JSON.stringify(jsonObject.poIds),
-
 
155
			function (response) {
-
 
156
				if (response === 'true' || response.includes("true")) {
-
 
157
					alert("Suggested PO created successfully!");
-
 
158
					cart = {};
-
 
159
					renderCart();
-
 
160
				} else {
-
 
161
					alert("Failed to create Suggested PO.");
-
 
162
				}
-
 
163
			}
-
 
164
		);
-
 
165
	}
-
 
166
}
-
 
167
 
-
 
168
 
-
 
169
 
-
 
170
 
-
 
171
 
-
 
172
 
-
 
173
 
-
 
174
 
-
 
175
 
39
 
176