blob: f72bcc447c651b1ff90db490a991a52069425921 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302// License: GNU General Public License v3. See license.txt
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05303frappe.provide("erpnext");
Anand Doshie65e6212012-11-19 15:43:18 +05304
Anand Doshibdee6e02013-07-09 13:03:39 +05305$.extend(erpnext, {
6 get_currency: function(company) {
7 if(!company && cur_frm)
8 company = cur_frm.doc.company;
9 if(company)
Rushabh Mehta66d52b52014-03-27 14:17:33 +053010 return frappe.get_doc(":Company", company).default_currency || frappe.boot.sysdefaults.currency;
Anand Doshibdee6e02013-07-09 13:03:39 +053011 else
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053012 return frappe.boot.sysdefaults.currency;
Anand Doshibdee6e02013-07-09 13:03:39 +053013 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053014
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +053015 get_fiscal_year: function(company, date, fn) {
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +053016 frappe.call({
17 type:"GET",
18 method: "erpnext.accounts.utils.get_fiscal_year",
19 args: {
Neil Trini Lasradoe6b14a42014-10-07 12:50:17 +053020 "company": company,
21 "date": date,
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +053022 "verbose": '0'
23 },
24 callback: function(r) {
Neil Trini Lasrado565d9862014-10-07 18:26:39 +053025 if (r.message) cur_frm.set_value("fiscal_year", r.message[0]);
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +053026 if (fn) fn();
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +053027 }
28 });
29 },
30
Anand Doshi8bde7f92014-04-24 18:11:49 +053031 toggle_naming_series: function() {
Rushabh Mehtac38fc712014-04-16 17:21:25 +053032 if(cur_frm.fields_dict.naming_series) {
Anand Doshibdee6e02013-07-09 13:03:39 +053033 cur_frm.toggle_display("naming_series", cur_frm.doc.__islocal?true:false);
34 }
35 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053036
Anand Doshibdee6e02013-07-09 13:03:39 +053037 hide_company: function() {
38 if(cur_frm.fields_dict.company) {
Rushabh Mehta89418412013-10-07 18:22:29 +053039 var companies = Object.keys(locals[":Company"] || {});
Anand Doshibdee6e02013-07-09 13:03:39 +053040 if(companies.length === 1) {
41 if(!cur_frm.doc.company) cur_frm.set_value("company", companies[0]);
42 cur_frm.toggle_display("company", false);
43 }
44 }
Anand Doshi1ffc5b52013-07-15 18:09:43 +053045 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053046
Akhilesh Darjeee158cea2013-10-31 19:50:51 +053047 add_applicable_territory: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +053048 if(cur_frm.doc.__islocal && (cur_frm.doc.valid_for_territories || []).length===0) {
Nabin Hait73083dc2014-03-14 15:10:42 +053049 var default_territory = frappe.defaults.get_user_default("territory");
Nabin Hait05b28eb2014-02-19 11:04:32 +053050 if(default_territory) {
Rushabh Mehtac38fc712014-04-16 17:21:25 +053051 var territory = frappe.model.add_child(cur_frm.doc, "Applicable Territory",
Nabin Hait05b28eb2014-02-19 11:04:32 +053052 "valid_for_territories");
53 territory.territory = default_territory;
54 }
Rushabh Mehtac38fc712014-04-16 17:21:25 +053055
Anand Doshi1ffc5b52013-07-15 18:09:43 +053056 }
57 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053058
Rushabh Mehta62030e02013-08-14 18:37:28 +053059 setup_serial_no: function(grid_row) {
Rushabh Mehtac38fc712014-04-16 17:21:25 +053060 if(!grid_row.fields_dict.serial_no ||
Anand Doshif83cf582013-08-20 15:36:10 +053061 grid_row.fields_dict.serial_no.get_status()!=="Write") return;
Rushabh Mehtac38fc712014-04-16 17:21:25 +053062
Pratik Vyasb52618c2014-04-14 16:25:30 +053063 var $btn = $('<button class="btn btn-sm btn-default">'+__("Add Serial No")+'</button>')
Rushabh Mehta62030e02013-08-14 18:37:28 +053064 .appendTo($("<div>")
Rushabh Mehta7d8b1892013-11-08 16:27:18 +053065 .css({"margin-bottom": "10px", "margin-left": "15px"})
Rushabh Mehta62030e02013-08-14 18:37:28 +053066 .appendTo(grid_row.fields_dict.serial_no.$wrapper));
Rushabh Mehtac38fc712014-04-16 17:21:25 +053067
Rushabh Mehta62030e02013-08-14 18:37:28 +053068 $btn.on("click", function() {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053069 var d = new frappe.ui.Dialog({
Pratik Vyasb52618c2014-04-14 16:25:30 +053070 title: __("Add Serial No"),
Rushabh Mehta62030e02013-08-14 18:37:28 +053071 fields: [
72 {
73 "fieldtype": "Link",
74 "options": "Serial No",
Pratik Vyasb52618c2014-04-14 16:25:30 +053075 "label": __("Serial No"),
Rushabh Mehta62030e02013-08-14 18:37:28 +053076 "get_query": {
77 item_code: grid_row.doc.item_code,
78 warehouse: grid_row.doc.warehouse
79 }
80 },
81 {
82 "fieldtype": "Button",
Pratik Vyasb52618c2014-04-14 16:25:30 +053083 "label": __("Add")
Rushabh Mehta62030e02013-08-14 18:37:28 +053084 }
85 ]
86 });
Rushabh Mehtac38fc712014-04-16 17:21:25 +053087
Rushabh Mehta62030e02013-08-14 18:37:28 +053088 d.get_input("add").on("click", function() {
89 var serial_no = d.get_value("serial_no");
90 if(serial_no) {
91 var val = (grid_row.doc.serial_no || "").split("\n").concat([serial_no]).join("\n");
92 grid_row.fields_dict.serial_no.set_model_value(val.trim());
93 }
94 d.hide();
95 return false;
96 });
Rushabh Mehtac38fc712014-04-16 17:21:25 +053097
Rushabh Mehta62030e02013-08-14 18:37:28 +053098 d.show();
99 });
100 }
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530101});