refactored client side Model and Meta - frappe/frappe#478

This commit is contained in:
Rushabh Mehta 2014-03-26 18:24:30 +05:30
parent 5c6b34a84c
commit aa355af2cf
31 changed files with 53 additions and 77 deletions

View File

@ -87,7 +87,7 @@ cur_frm.cscript.add_toolbar_buttons = function(doc) {
}
cur_frm.cscript.convert_to_ledger = function(doc, cdt, cdn) {
return $c_obj(cur_frm.get_doclist(),'convert_group_to_ledger','',function(r,rt) {
return $c_obj(cur_frm.doc,'convert_group_to_ledger','',function(r,rt) {
if(r.message == 1) {
cur_frm.refresh();
}
@ -95,7 +95,7 @@ cur_frm.cscript.convert_to_ledger = function(doc, cdt, cdn) {
}
cur_frm.cscript.convert_to_group = function(doc, cdt, cdn) {
return $c_obj(cur_frm.get_doclist(),'convert_ledger_to_group','',function(r,rt) {
return $c_obj(cur_frm.doc,'convert_ledger_to_group','',function(r,rt) {
if(r.message == 1) {
cur_frm.refresh();
}

View File

@ -7,8 +7,7 @@ cur_frm.cscript.onload = function(doc,cdt,cdn){
refresh_field('budget_distribution_details');
}
return $c('runserverobj',args={'method' : 'get_months', 'docs' :
frappe.model.compress(make_doclist(doc.doctype, doc.name))},callback1);
return $c('runserverobj',args={'method':'get_months', 'docs':doc}, callback1);
}
}

View File

@ -70,7 +70,7 @@ cur_frm.cscript.hide_unhide_group_ledger = function(doc) {
}
cur_frm.cscript.convert_to_ledger = function(doc, cdt, cdn) {
return $c_obj(cur_frm.get_doclist(),'convert_group_to_ledger','',function(r,rt) {
return $c_obj(cur_frm.doc,'convert_group_to_ledger','',function(r,rt) {
if(r.message == 1) {
cur_frm.refresh();
}
@ -78,7 +78,7 @@ cur_frm.cscript.convert_to_ledger = function(doc, cdt, cdn) {
}
cur_frm.cscript.convert_to_group = function(doc, cdt, cdn) {
return $c_obj(cur_frm.get_doclist(),'convert_ledger_to_group','',function(r,rt) {
return $c_obj(cur_frm.doc,'convert_ledger_to_group','',function(r,rt) {
if(r.message == 1) {
cur_frm.refresh();
}

View File

@ -12,8 +12,7 @@ erpnext.accounts.JournalVoucher = frappe.ui.form.Controller.extend({
load_defaults: function() {
if(this.frm.doc.__islocal && this.frm.doc.company) {
frappe.model.set_default_values(this.frm.doc);
$.each(frappe.model.get_doclist(this.frm.doc.doctype,
this.frm.doc.name, {parentfield: "entries"}), function(i, jvd) {
$.each(this.frm.doc.entries, function(i, jvd) {
frappe.model.set_default_values(jvd);
}
);
@ -156,7 +155,7 @@ cur_frm.cscript.credit = function(doc,dt,dn) { cur_frm.cscript.update_totals(doc
cur_frm.cscript.get_balance = function(doc,dt,dn) {
cur_frm.cscript.update_totals(doc);
return $c_obj(make_doclist(dt,dn), 'get_balance', '', function(r, rt){
return $c_obj(cur_frm.doc, 'get_balance', '', function(r, rt){
cur_frm.refresh();
});
}
@ -194,8 +193,7 @@ cur_frm.cscript.voucher_type = function(doc, cdt, cdn) {
cur_frm.set_df_property("cheque_no", "reqd", doc.voucher_type=="Bank Voucher");
cur_frm.set_df_property("cheque_date", "reqd", doc.voucher_type=="Bank Voucher");
if(frappe.model.get("Journal Voucher Detail", {"parent":doc.name}).length!==0 // too late
|| !doc.company) // too early
if((doc.entries || []).length!==0 || !doc.company) // too early
return;
var update_jv_details = function(doc, r) {

View File

@ -2,7 +2,7 @@
// License: GNU General Public License v3. See license.txt
cur_frm.cscript.onload = function(doc,cdt,cdn){
return $c_obj(make_doclist(cdt,cdn),'get_series','',function(r,rt){
return $c_obj(cur_frm.doc, 'get_series','',function(r,rt){
if(r.message) set_field_options('naming_series', r.message);
});

View File

@ -68,7 +68,7 @@ erpnext.accounts.SalesInvoiceController = erpnext.selling.SellingController.exte
if(cint(doc.update_stock)!=1) {
// show Make Delivery Note button only if Sales Invoice is not created from Delivery Note
var from_delivery_note = false;
from_delivery_note = cur_frm.get_doclist({parentfield: "entries"})
from_delivery_note = cur_frm.doc.entries
.some(function(item) {
return item.delivery_note ? true : false;
});

View File

@ -156,7 +156,7 @@ erpnext.buying.BuyingController = erpnext.TransactionController.extend({
project_name: function(doc, cdt, cdn) {
var item = frappe.model.get_doc(cdt, cdn);
if(item.project_name) {
$.each(frappe.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name, {parentfield: this.fname}),
$.each(this.frm.doc[this.fname],
function(i, other_item) {
if(!other_item.project_name) {
other_item.project_name = item.project_name;

View File

@ -128,7 +128,7 @@ cur_frm.fields_dict['po_details'].grid.get_field('project_name').get_query = fun
}
cur_frm.cscript.get_last_purchase_rate = function(doc, cdt, cdn){
return $c_obj(make_doclist(doc.doctype, doc.name), 'get_last_purchase_rate', '', function(r, rt) {
return $c_obj(doc, 'get_last_purchase_rate', '', function(r, rt) {
refresh_field(cur_frm.cscript.fname);
var doc = locals[cdt][cdn];
cur_frm.cscript.calc_amount( doc, 2);
@ -140,7 +140,7 @@ cur_frm.cscript['Stop Purchase Order'] = function() {
var check = confirm(frappe._("Do you really want to STOP ") + doc.name);
if (check) {
return $c('runserverobj', args={'method':'update_status', 'arg': 'Stopped', 'docs': frappe.model.compress(make_doclist(doc.doctype, doc.name))}, function(r,rt) {
return $c('runserverobj', args={'method':'update_status', 'arg': 'Stopped', 'docs':doc}, function(r,rt) {
cur_frm.refresh();
});
}
@ -151,7 +151,7 @@ cur_frm.cscript['Unstop Purchase Order'] = function() {
var check = confirm(frappe._("Do you really want to UNSTOP ") + doc.name);
if (check) {
return $c('runserverobj', args={'method':'update_status', 'arg': 'Submitted', 'docs': frappe.model.compress(make_doclist(doc.doctype, doc.name))}, function(r,rt) {
return $c('runserverobj', args={'method':'update_status', 'arg': 'Submitted', 'docs':doc}, function(r,rt) {
cur_frm.refresh();
});
}

View File

@ -68,7 +68,7 @@ erpnext.hr.EmployeeController = frappe.ui.form.Controller.extend({
Go to the active Salary Structure and set \"Is Active\" = \"No\""));
} else if(!r.exc) {
frappe.model.map({
source: frappe.model.get_doclist(me.frm.doc.doctype, me.frm.doc.name),
source: me.frm.doc,
target: "Salary Structure"
});
}

View File

@ -125,7 +125,7 @@ cur_frm.cscript.validate = function(doc) {
cur_frm.cscript.calculate_total = function(doc,cdt,cdn){
doc.total_claimed_amount = 0;
doc.total_sanctioned_amount = 0;
$.each(frappe.model.get("Expense Claim Detail", {parent:doc.name}), function(i, d) {
$.each((doc.expense_voucher_details || []), function(i, d) {
doc.total_claimed_amount += d.claim_amount;
if(d.sanctioned_amount==null) {
d.sanctioned_amount = d.claim_amount;

View File

@ -9,7 +9,7 @@ cur_frm.cscript.onload = function(doc, dt, dn){
}
cur_frm.cscript.to_date = function(doc, cdt, cdn) {
return $c('runserverobj', args={'method':'to_date_validation','docs':frappe.model.compress(make_doclist(doc.doctype, doc.name))},
return $c('runserverobj', args={'method':'to_date_validation','docs':doc},
function(r, rt) {
var doc = locals[cdt][cdn];
if (r.message) {

View File

@ -15,7 +15,7 @@ cur_frm.cscript.create_salary_slip = function(doc, cdt, cdn) {
if (r.message)
display_activity_log(r.message);
}
return $c('runserverobj', args={'method':'create_sal_slip','docs':frappe.model.compress(make_doclist (cdt, cdn))},callback);
return $c('runserverobj', args={'method':'create_sal_slip','docs':doc},callback);
}
cur_frm.cscript.submit_salary_slip = function(doc, cdt, cdn) {
@ -25,7 +25,7 @@ cur_frm.cscript.submit_salary_slip = function(doc, cdt, cdn) {
if (r.message)
display_activity_log(r.message);
}
return $c('runserverobj', args={'method':'submit_salary_slip','docs':frappe.model.compress(make_doclist (cdt, cdn))},callback);
return $c('runserverobj', args={'method':'submit_salary_slip','docs':doc},callback);
}
}
@ -59,5 +59,5 @@ cur_frm.cscript.make_jv = function(doc, dt, dn) {
loaddoc('Journal Voucher', jv.name);
}
return $c_obj(make_doclist(dt, dn), 'get_acc_details', '', call_back);
return $c_obj(doc, 'get_acc_details', '', call_back);
}

View File

@ -21,7 +21,7 @@ cur_frm.cscript.onload = function(doc,dt,dn){
// Get leave details
//---------------------------------------------------------------------
cur_frm.cscript.fiscal_year = function(doc,dt,dn){
return $c_obj(make_doclist(doc.doctype,doc.name), 'get_emp_and_leave_details','',function(r, rt) {
return $c_obj(doc, 'get_emp_and_leave_details','',function(r, rt) {
var doc = locals[dt][dn];
cur_frm.refresh();
calculate_all(doc, dt, dn);
@ -32,7 +32,7 @@ cur_frm.cscript.month = cur_frm.cscript.employee = cur_frm.cscript.fiscal_year;
cur_frm.cscript.leave_without_pay = function(doc,dt,dn){
if (doc.employee && doc.fiscal_year && doc.month) {
return $c_obj(make_doclist(doc.doctype,doc.name), 'get_leave_details',doc.leave_without_pay,function(r, rt) {
return $c_obj(doc, 'get_leave_details',doc.leave_without_pay,function(r, rt) {
var doc = locals[dt][dn];
cur_frm.refresh();
calculate_all(doc, dt, dn);

View File

@ -7,7 +7,7 @@ cur_frm.cscript.onload = function(doc, dt, dn){
e_tbl = getchildren('Salary Structure Earning', doc.name, 'earning_details', doc.doctype);
d_tbl = getchildren('Salary Structure Deduction', doc.name, 'deduction_details', doc.doctype);
if (e_tbl.length == 0 && d_tbl.length == 0)
return $c_obj(make_doclist(doc.doctype,doc.name),'make_earn_ded_table','', function(r, rt) { refresh_many(['earning_details', 'deduction_details']);});
return $c_obj(doc,'make_earn_ded_table','', function(r, rt) { refresh_many(['earning_details', 'deduction_details']);});
}
cur_frm.cscript.refresh = function(doc, dt, dn){

View File

@ -70,7 +70,7 @@ cur_frm.cscript['Stop Production Order'] = function() {
var doc = cur_frm.doc;
var check = confirm(frappe._("Do you really want to stop production order: " + doc.name));
if (check) {
return $c_obj(make_doclist(doc.doctype, doc.name), 'stop_unstop', 'Stopped', function(r, rt) {cur_frm.refresh();});
return $c_obj(doc, 'stop_unstop', 'Stopped', function(r, rt) {cur_frm.refresh();});
}
}
@ -78,7 +78,7 @@ cur_frm.cscript['Unstop Production Order'] = function() {
var doc = cur_frm.doc;
var check = confirm(frappe._("Do really want to unstop production order: " + doc.name));
if (check)
return $c_obj(make_doclist(doc.doctype, doc.name), 'stop_unstop', 'Unstopped', function(r, rt) {cur_frm.refresh();});
return $c_obj(doc, 'stop_unstop', 'Unstopped', function(r, rt) {cur_frm.refresh();});
}
cur_frm.cscript['Transfer Raw Materials'] = function() {

View File

@ -25,9 +25,9 @@ cur_frm.cscript.item_code = function(doc,cdt,cdn) {
}
cur_frm.cscript.download_materials_required = function(doc, cdt, cdn) {
return $c_obj(make_doclist(cdt, cdn), 'validate_data', '', function(r, rt) {
return $c_obj(doc, 'validate_data', '', function(r, rt) {
if (!r['exc'])
$c_obj_csv(make_doclist(cdt, cdn), 'download_raw_materials', '', '');
$c_obj_csv(doc, 'download_raw_materials', '', '');
});
}

View File

@ -30,9 +30,8 @@ $.extend(cur_frm.cscript, {
}
},
make_invoice: function() {
var doc = cur_frm.doc;
frappe.model.map({
source: frappe.model.get_doclist(doc.doctype, doc.name),
source: cur_frm.doc,
target: "Sales Invoice"
});
}

View File

@ -46,7 +46,6 @@ erpnext.TransactionController = erpnext.stock.StockController.extend({
freeze: true,
callback: function(r) {
// remove this call when using client side mapper
me.set_default_values();
me.set_dynamic_labels();
me.calculate_taxes_and_totals();
}
@ -192,18 +191,7 @@ erpnext.TransactionController = erpnext.stock.StockController.extend({
validate: function() {
this.calculate_taxes_and_totals();
},
set_default_values: function() {
$.each(frappe.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) {
var updated = frappe.model.set_default_values(doc);
if(doc.parentfield) {
refresh_field(doc.parentfield);
} else {
refresh_field(updated);
}
});
},
company: function() {
if(this.frm.doc.company && this.frm.fields_dict.currency) {
var company_currency = this.get_company_currency();
@ -517,13 +505,11 @@ erpnext.TransactionController = erpnext.stock.StockController.extend({
},
get_item_doclist: function() {
return frappe.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
{parentfield: this.fname});
return this.frm.doc[this.fname] || [];
},
get_tax_doclist: function() {
return frappe.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
{parentfield: this.other_fname});
return this.frm.doc[this.other_fname] || [];
},
validate_conversion_rate: function() {
@ -716,8 +702,7 @@ erpnext.TransactionController = erpnext.stock.StockController.extend({
calculate_total_advance: function(parenttype, advance_parentfield) {
if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
var advance_doclist = frappe.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
{parentfield: advance_parentfield});
var advance_doclist = this.frm.doc[advance_parentfield] || [];
this.frm.doc.total_advance = flt(frappe.utils.sum(
$.map(advance_doclist, function(adv) { return adv.allocated_amount })
), precision("total_advance"));

View File

@ -29,8 +29,7 @@ $.extend(erpnext, {
},
add_applicable_territory: function() {
if(cur_frm.doc.__islocal &&
frappe.model.get_doclist(cur_frm.doc.doctype, cur_frm.doc.name).length === 1) {
if(cur_frm.doc.__islocal && (cur_frm.doc.valid_for_territories || []).length===0) {
var default_territory = frappe.defaults.get_user_default("territory");
if(default_territory) {
var territory = frappe.model.add_child(cur_frm.doc, "Applicable Territory",

View File

@ -162,7 +162,7 @@ cur_frm.cscript['Stop Sales Order'] = function() {
if (check) {
return $c('runserverobj', {
'method':'stop_sales_order',
'docs': frappe.model.compress(make_doclist(doc.doctype, doc.name))
'docs': doc
}, function(r,rt) {
cur_frm.refresh();
});
@ -177,7 +177,7 @@ cur_frm.cscript['Unstop Sales Order'] = function() {
if (check) {
return $c('runserverobj', {
'method':'unstop_sales_order',
'docs': frappe.model.compress(make_doclist(doc.doctype, doc.name))
'docs': doc
}, function(r,rt) {
cur_frm.refresh();
});

View File

@ -98,7 +98,7 @@ erpnext.selling.SellingController = erpnext.TransactionController.extend({
this.frm.toggle_display("customer_name",
(this.frm.doc.customer_name && this.frm.doc.customer_name!==this.frm.doc.customer));
if(this.frm.fields_dict.packing_details) {
var packing_list_exists = this.frm.get_doclist({parentfield: "packing_details"}).length;
var packing_list_exists = (this.frm.doc.packing_details || []).length;
this.frm.toggle_display("packing_list", packing_list_exists ? true : false);
}
},
@ -434,8 +434,7 @@ erpnext.selling.SellingController = erpnext.TransactionController.extend({
calculate_contribution: function() {
var me = this;
$.each(frappe.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
{parentfield: "sales_team"}), function(i, sales_person) {
$.each(this.frm.doc.doctype.sales_team || [], function(i, sales_person) {
frappe.model.round_floats_in(sales_person);
if(sales_person.allocated_percentage) {
sales_person.allocated_amount = flt(
@ -545,8 +544,8 @@ erpnext.selling.SellingController = erpnext.TransactionController.extend({
// toggle columns
var item_grid = this.frm.fields_dict[this.fname].grid;
var show = (this.frm.doc.currency != company_currency) ||
(frappe.model.get_doclist(cur_frm.doctype, cur_frm.docname,
{parentfield: "other_charges", included_in_print_rate: 1}).length);
(cur_frm.doc.other_charges.filter(
function(d) { return d.included_in_print_rate===1}).length);
$.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
if(frappe.meta.get_docfield(item_grid.doctype, fname))

View File

@ -2,7 +2,7 @@
// License: GNU General Public License v3. See license.txt
cur_frm.cscript.get_states=function(doc, dt, dn) {
return $c('runserverobj', args={'method': 'check_state', 'docs':frappe.model.compress(make_doclist(doc.doctype, doc.name))},
return $c('runserverobj', args={'method': 'check_state', 'docs':doc},
function(r, rt) {
if(r.message)
set_field_options('state', r.message);

View File

@ -9,7 +9,7 @@ cur_frm.cscript.refresh = function(doc, dt, dn) {
cur_frm.add_custom_button(frappe._('View Now'), function() {
doc = locals[dt][dn];
if(doc.__unsaved != 1) {
return $c_obj(make_doclist(dt, dn), 'get_digest_msg', '', function(r, rt) {
return $c_obj(doc, 'get_digest_msg', '', function(r, rt) {
if(r.exc) {
msgprint(err_msg);
console.log(r.exc);
@ -32,7 +32,7 @@ cur_frm.cscript.refresh = function(doc, dt, dn) {
cur_frm.add_custom_button(frappe._('Send Now'), function() {
doc = locals[dt][dn];
if(doc.__unsaved != 1) {
return $c_obj(make_doclist(dt, dn), 'send', '', function(r, rt) {
return $c_obj(doc, 'send', '', function(r, rt) {
if(r.exc) {
msgprint(err_msg);
console.log(r.exc);
@ -49,7 +49,7 @@ cur_frm.cscript.refresh = function(doc, dt, dn) {
cur_frm.cscript.addremove_recipients = function(doc, dt, dn) {
// Get user list
return $c_obj(make_doclist(dt, dn), 'get_users', '', function(r, rt) {
return $c_obj(doc, 'get_users', '', function(r, rt) {
if(r.exc) {
msgprint(r.exc);
} else {

View File

@ -18,7 +18,7 @@ $.extend(cur_frm.cscript, {
},
validate: function(doc, cdt, cdn) {
return $c_obj(make_doclist(cdt, cdn), 'get_defaults', '', function(r, rt){
return $c_obj(doc, 'get_defaults', '', function(r, rt){
sys_defaults = r.message;
});
},

View File

@ -29,7 +29,7 @@ cur_frm.cscript.select_doc_for_series = function(doc, cdt, cdn) {
}
if(doc.select_doc_for_series)
return $c_obj(make_doclist(doc.doctype, doc.name),'get_options','',callback);
return $c_obj(doc,'get_options','',callback);
}
cur_frm.cscript.update = function() {

View File

@ -8,6 +8,6 @@ cur_frm.cscript.select_transaction = function(doc, cdt, cdn) {
doc.custom_message = r.message;
refresh_field('custom_message');
}
return $c_obj(make_doclist(cdt, cdn),'get_message',doc.select_transaction, callback)
return $c_obj(doc,'get_message',doc.select_transaction, callback)
}
}

View File

@ -20,8 +20,7 @@ erpnext.stock.DeliveryNoteController = erpnext.selling.SellingController.extend(
if(!doc.__billing_complete && doc.docstatus==1) {
// show Make Invoice button only if Delivery Note is not created from Sales Invoice
var from_sales_invoice = false;
from_sales_invoice = cur_frm.get_doclist({parentfield: "delivery_note_details"})
.some(function(item) {
from_sales_invoice = cur_frm.doc.delivery_note_details.some(function(item) {
return item.against_sales_invoice ? true : false;
});

View File

@ -151,8 +151,7 @@ cur_frm.fields_dict.item_supplier_details.grid.get_field("supplier").get_query =
cur_frm.cscript.copy_from_item_group = function(doc) {
frappe.model.with_doc("Item Group", doc.item_group, function() {
$.each(frappe.model.get("Item Website Specification", {parent:doc.item_group}),
function(i, d) {
$.each((doc.item_website_specifications || []), function(i, d) {
var n = frappe.model.add_child(doc, "Item Website Specification",
"item_website_specifications");
n.label = d.label;

View File

@ -80,7 +80,7 @@ erpnext.buying.MaterialRequestController = erpnext.buying.BuyingController.exten
schedule_date: function(doc, cdt, cdn) {
var val = locals[cdt][cdn].schedule_date;
if(val) {
$.each(frappe.model.get("Material Request Item", { parent: cur_frm.doc.name }), function(i, d) {
$.each((doc.indent_details || []), function(i, d) {
if(!d.schedule_date) {
d.schedule_date = val;
}
@ -172,7 +172,7 @@ cur_frm.cscript['Stop Material Request'] = function() {
var check = confirm(frappe._("Do you really want to STOP this Material Request?"));
if (check) {
return $c('runserverobj', args={'method':'update_status', 'arg': 'Stopped', 'docs': frappe.model.compress(make_doclist(doc.doctype, doc.name))}, function(r,rt) {
return $c('runserverobj', args={'method':'update_status', 'arg': 'Stopped', 'docs': doc}, function(r,rt) {
cur_frm.refresh();
});
}
@ -183,7 +183,7 @@ cur_frm.cscript['Unstop Material Request'] = function(){
var check = confirm(frappe._("Do you really want to UNSTOP this Material Request?"));
if (check) {
return $c('runserverobj', args={'method':'update_status', 'arg': 'Submitted','docs': frappe.model.compress(make_doclist(doc.doctype, doc.name))}, function(r,rt) {
return $c('runserverobj', args={'method':'update_status', 'arg': 'Submitted','docs': doc}, function(r,rt) {
cur_frm.refresh();
});
}

View File

@ -94,8 +94,7 @@ cur_frm.cscript.periodicity = function(doc, cdt, cdn){
cur_frm.cscript.generate_schedule = function(doc, cdt, cdn) {
if (!doc.__islocal) {
return $c('runserverobj', args={'method':'generate_schedule',
'docs':frappe.model.compress(make_doclist(cdt,cdn))},
return $c('runserverobj', args={'method':'generate_schedule', 'docs':doc},
function(r, rt) {
refresh_field('maintenance_schedule_detail');
});

View File

@ -17,7 +17,7 @@ cur_frm.cscript.refresh = function(doc) {
if(!doc.__islocal && !cint(doc.email_sent) && !doc.__unsaved
&& inList(frappe.boot.user.can_write, doc.doctype)) {
cur_frm.add_custom_button(frappe._('Send'), function() {
return $c_obj(make_doclist(doc.doctype, doc.name), 'send_emails', '', function(r) {
return $c_obj(doc, 'send_emails', '', function(r) {
cur_frm.refresh();
});
})