Manas Solanki | da486ee | 2018-07-06 12:36:57 +0530 | [diff] [blame] | 1 | // Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and contributors |
| 2 | // For license information, please see license.txt |
| 3 | |
| 4 | frappe.ready(function(){ |
| 5 | |
| 6 | var loyalty_points_input = document.getElementById("loyalty-point-to-redeem"); |
| 7 | var loyalty_points_status = document.getElementById("loyalty-points-status"); |
| 8 | loyalty_points_input.onblur = apply_loyalty_points; |
| 9 | |
| 10 | function apply_loyalty_points() { |
| 11 | var loyalty_points = parseInt(loyalty_points_input.value); |
| 12 | if (loyalty_points) { |
| 13 | frappe.call({ |
| 14 | method: "erpnext.accounts.doctype.loyalty_program.loyalty_program.get_redeemption_factor", |
| 15 | args: { |
| 16 | "customer": doc_info.customer |
| 17 | }, |
| 18 | callback: function(r) { |
| 19 | if (r) { |
| 20 | var message = "" |
| 21 | let loyalty_amount = flt(r.message*loyalty_points); |
| 22 | if (doc_info.grand_total && doc_info.grand_total < loyalty_amount) { |
| 23 | let redeemable_amount = parseInt(doc_info.grand_total/r.message); |
| 24 | message = "You can only redeem max " + redeemable_amount + " points in this order."; |
| 25 | frappe.msgprint(__(message)); |
| 26 | } else { |
| 27 | message = loyalty_points + " Loyalty Points of amount "+ loyalty_amount + " is applied." |
| 28 | frappe.msgprint(__(message)); |
| 29 | var remaining_amount = flt(doc_info.grand_total) - flt(loyalty_amount); |
| 30 | var payment_button = document.getElementById("pay-for-order"); |
| 31 | payment_button.innerHTML = __("Pay Remaining"); |
| 32 | payment_button.href = "/api/method/erpnext.accounts.doctype.payment_request.payment_request.make_payment_request?dn="+doc_info.doctype_name+"&dt="+doc_info.doctype+"&loyalty_points="+loyalty_points+"&submit_doc=1&order_type=Shopping Cart"; |
| 33 | } |
| 34 | loyalty_points_status.innerHTML = message; |
| 35 | } |
| 36 | } |
| 37 | }); |
| 38 | } |
| 39 | } |
| 40 | }) |