blob: 3e9c261a20062d9012e9077909fc8ba8cff7f7fa [file] [log] [blame]
rohitwaghchaurea1064a62016-03-03 14:00:35 +05301// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2// License: GNU General Public License v3. See license.txt
3
4window.doc={{ doc.as_json() }};
5
6$(document).ready(function() {
7 new rfq();
8 doc.supplier = "{{ doc.supplier }}"
rohitwaghchaure62fea032016-03-30 13:24:42 +05309 doc.currency = "{{ doc.currency }}"
10 doc.number_format = "{{ doc.number_format }}"
11 doc.buying_price_list = "{{ doc.buying_price_list }}"
rohitwaghchaurea1064a62016-03-03 14:00:35 +053012});
13
14rfq = Class.extend({
15 init: function(){
16 this.onfocus_select_all();
17 this.change_qty();
18 this.change_rate();
19 this.terms();
20 this.submit_rfq();
21 },
22
23 onfocus_select_all: function(){
24 $("input").click(function(){
25 $(this).select();
26 })
27 },
28
29 change_qty: function(){
30 var me = this;
31 $('.rfq-items').on("change", ".rfq-qty", function(){
32 me.idx = parseFloat($(this).attr('data-idx'));
33 me.qty = parseFloat($(this).val());
34 me.rate = parseFloat($(repl('.rfq-rate[data-idx=%(idx)s]',{'idx': me.idx})).val());
35 me.update_qty_rate();
36 })
37 },
38
39 change_rate: function(){
40 var me = this;
41 $(".rfq-items").on("change", ".rfq-rate", function(){
42 me.idx = parseFloat($(this).attr('data-idx'));
43 me.rate = parseFloat($(this).val());
44 me.qty = parseFloat($(repl('.rfq-qty[data-idx=%(idx)s]',{'idx': me.idx})).val());
45 me.update_qty_rate();
46 })
47 },
48
49 terms: function(){
50 $(".terms").on("change", ".terms-feedback", function(){
51 doc.terms = $(this).val();
52 })
53 },
54
55 update_qty_rate: function(){
56 var me = this;
57 doc.grand_total = 0.0;
58 $.each(doc.items, function(idx, data){
59 if(data.idx == me.idx){
60 data.qty = me.qty;
61 data.rate = me.rate;
62 data.amount = (me.rate * me.qty) || 0.0;
rohitwaghchaure62fea032016-03-30 13:24:42 +053063 $(repl('.rfq-amount[data-idx=%(idx)s]',{'idx': me.idx})).text(format_number(data.amount, doc.number_format, 2));
rohitwaghchaurea1064a62016-03-03 14:00:35 +053064 }
65
66 doc.grand_total += flt(data.amount);
rohitwaghchaure62fea032016-03-30 13:24:42 +053067 $('.tax-grand-total').text(format_number(doc.grand_total, doc.number_format, 2));
rohitwaghchaurea1064a62016-03-03 14:00:35 +053068 })
69 },
70
71 submit_rfq: function(){
72 $('.btn-sm').click(function(){
73 frappe.freeze();
74 frappe.call({
75 type: "POST",
76 method: "erpnext.buying.doctype.request_for_quotation.request_for_quotation.create_supplier_quotation",
77 args: {
78 doc: doc
79 },
80 btn: this,
81 callback: function(r){
Rohit Waghchauref154c612016-07-06 17:26:15 +053082 $('.btn-sm').hide()
rohitwaghchaurea1064a62016-03-03 14:00:35 +053083 frappe.unfreeze();
84 }
85 })
86 })
87 }
88})