blob: b56c416dbde61535f6d5009822adb2cccb9f2946 [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();
Rohit Waghchaurea26f6852016-10-03 19:05:18 +053021 this.navigate_quotations();
rohitwaghchaurea1064a62016-03-03 14:00:35 +053022 },
23
24 onfocus_select_all: function(){
25 $("input").click(function(){
26 $(this).select();
27 })
28 },
29
30 change_qty: function(){
31 var me = this;
32 $('.rfq-items').on("change", ".rfq-qty", function(){
33 me.idx = parseFloat($(this).attr('data-idx'));
Rohit Waghchaureb34ba6b2016-09-01 18:01:27 +053034 me.qty = parseFloat($(this).val()) || 0;
rohitwaghchaurea1064a62016-03-03 14:00:35 +053035 me.rate = parseFloat($(repl('.rfq-rate[data-idx=%(idx)s]',{'idx': me.idx})).val());
36 me.update_qty_rate();
mbauskar22cedeb2017-04-17 12:24:11 +053037 $(this).val(format_number(me.qty, doc.number_format, 2));
rohitwaghchaurea1064a62016-03-03 14:00:35 +053038 })
39 },
40
41 change_rate: function(){
42 var me = this;
43 $(".rfq-items").on("change", ".rfq-rate", function(){
44 me.idx = parseFloat($(this).attr('data-idx'));
Rohit Waghchaureb34ba6b2016-09-01 18:01:27 +053045 me.rate = parseFloat($(this).val()) || 0;
rohitwaghchaurea1064a62016-03-03 14:00:35 +053046 me.qty = parseFloat($(repl('.rfq-qty[data-idx=%(idx)s]',{'idx': me.idx})).val());
47 me.update_qty_rate();
mbauskar22cedeb2017-04-17 12:24:11 +053048 $(this).val(format_number(me.rate, doc.number_format, 2));
rohitwaghchaurea1064a62016-03-03 14:00:35 +053049 })
50 },
51
52 terms: function(){
53 $(".terms").on("change", ".terms-feedback", function(){
54 doc.terms = $(this).val();
55 })
56 },
57
58 update_qty_rate: function(){
59 var me = this;
60 doc.grand_total = 0.0;
61 $.each(doc.items, function(idx, data){
62 if(data.idx == me.idx){
63 data.qty = me.qty;
64 data.rate = me.rate;
65 data.amount = (me.rate * me.qty) || 0.0;
rohitwaghchaure62fea032016-03-30 13:24:42 +053066 $(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 +053067 }
68
69 doc.grand_total += flt(data.amount);
rohitwaghchaure62fea032016-03-30 13:24:42 +053070 $('.tax-grand-total').text(format_number(doc.grand_total, doc.number_format, 2));
rohitwaghchaurea1064a62016-03-03 14:00:35 +053071 })
72 },
73
74 submit_rfq: function(){
75 $('.btn-sm').click(function(){
76 frappe.freeze();
77 frappe.call({
78 type: "POST",
79 method: "erpnext.buying.doctype.request_for_quotation.request_for_quotation.create_supplier_quotation",
80 args: {
81 doc: doc
82 },
83 btn: this,
84 callback: function(r){
rohitwaghchaurea1064a62016-03-03 14:00:35 +053085 frappe.unfreeze();
Rohit Waghchaure21499e82016-09-21 16:49:58 +053086 if(r.message){
87 $('.btn-sm').hide()
Brown-Harry Bomad3fa1912017-09-27 08:23:38 +010088 window.location.href = "/supplier-quotations/" + encodeURIComponent(r.message);
Rohit Waghchaure21499e82016-09-21 16:49:58 +053089 }
rohitwaghchaurea1064a62016-03-03 14:00:35 +053090 }
91 })
92 })
Rohit Waghchaurea26f6852016-10-03 19:05:18 +053093 },
94
95 navigate_quotations: function() {
96 $('.quotations').click(function(){
97 name = $(this).attr('idx')
98 window.location.href = "/quotations/" + encodeURIComponent(name);
99 })
rohitwaghchaurea1064a62016-03-03 14:00:35 +0530100 }
101})