From 5c501cff25f304b90cbfa625fe4fc7e1f7cfd017 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Thu, 2 Feb 2012 11:40:25 +0530 Subject: [PATCH] roundNumber function, show cancelled in print format header, fixed load_from_db method of doclist --- js/legacy/form.compressed.js | 4 +-- js/legacy/utils/datatype.js | 9 +++++- js/legacy/widgets/autosuggest.js | 2 +- js/legacy/widgets/form/print_format.js | 18 ++++++++++++ js/legacy/wnf.compressed.js | 1 + py/webnotes/model/__init__.py | 39 ++++++++++++++++++++++++++ py/webnotes/model/doclist.py | 6 ++-- 7 files changed, 73 insertions(+), 6 deletions(-) diff --git a/js/legacy/form.compressed.js b/js/legacy/form.compressed.js index 01abfe9109..5eb922b23e 100644 --- a/js/legacy/form.compressed.js +++ b/js/legacy/form.compressed.js @@ -346,7 +346,7 @@ $.extend(_p,{show_dialog:function(){if(!_p.dialog){_p.make_dialog();} _p.dialog.show();},make_dialog:function(){var d=new Dialog(360,140,'Print Formats',[['HTML','Select'],['Check','No Letterhead'],['HTML','Buttons']]);$btn(d.widgets.Buttons,'Print',function(){_p.build(sel_val(cur_frm.print_sel),_p.go,d.widgets['No Letterhead'].checked);},{cssFloat:'right',marginBottom:'16px',marginLeft:'7px'},'green');$btn(d.widgets.Buttons,'Preview',function(){_p.build(sel_val(cur_frm.print_sel),_p.preview,d.widgets['No Letterhead'].checked);},{cssFloat:'right',marginBottom:'16px'},'');d.onshow=function(){var c=_p.dialog.widgets['Select'];if(c.cur_sel&&c.cur_sel.parentNode==c){c.removeChild(c.cur_sel);} c.appendChild(cur_frm.print_sel);c.cur_sel=cur_frm.print_sel;} _p.dialog=d;},formats:{},build:function(fmtname,onload,no_letterhead,only_body){args={fmtname:fmtname,onload:onload,no_letterhead:no_letterhead,only_body:only_body};if(!cur_frm){alert('No Document Selected');return;} -var doc=locals[cur_frm.doctype][cur_frm.docname];if(args.fmtname=='Standard'){args.onload(_p.render({body:_p.print_std(args.no_letterhead),style:_p.print_style,doc:doc,title:doc.name,no_letterhead:args.no_letterhead,only_body:args.only_body}));}else{if(!_p.formats[args.fmtname]){var build_args=args;$c(command='webnotes.widgets.form.get_print_format',args={'name':build_args.fmtname},fn=function(r,rt){_p.formats[build_args.fmtname]=r.message;build_args.onload(_p.render({body:_p.formats[build_args.fmtname],style:'',doc:doc,title:doc.name,no_letterhead:build_args.no_letterhead,only_body:build_args.only_body}));});}else{args.onload(_p.render({body:_p.formats[args.fmtname],style:'',doc:doc,title:doc.name,no_letterhead:args.no_letterhead,only_body:args.only_body}));}}},render:function(args){var container=document.createElement('div');var stat='';stat+=_p.show_draft(args);stat+=_p.show_archived(args);container.innerHTML=args.body;_p.show_letterhead(container,args);_p.run_embedded_js(container,args.doc);var style=_p.consolidate_css(container,args);_p.render_header_on_break(container,args);return _p.render_final(style,stat,container,args);},head_banner_format:function(){return"\ +var doc=locals[cur_frm.doctype][cur_frm.docname];if(args.fmtname=='Standard'){args.onload(_p.render({body:_p.print_std(args.no_letterhead),style:_p.print_style,doc:doc,title:doc.name,no_letterhead:args.no_letterhead,only_body:args.only_body}));}else{if(!_p.formats[args.fmtname]){var build_args=args;$c(command='webnotes.widgets.form.get_print_format',args={'name':build_args.fmtname},fn=function(r,rt){_p.formats[build_args.fmtname]=r.message;build_args.onload(_p.render({body:_p.formats[build_args.fmtname],style:'',doc:doc,title:doc.name,no_letterhead:build_args.no_letterhead,only_body:build_args.only_body}));});}else{args.onload(_p.render({body:_p.formats[args.fmtname],style:'',doc:doc,title:doc.name,no_letterhead:args.no_letterhead,only_body:args.only_body}));}}},render:function(args){var container=document.createElement('div');var stat='';stat+=_p.show_draft(args);stat+=_p.show_archived(args);stat+=_p.show_cancelled(args);container.innerHTML=args.body;_p.show_letterhead(container,args);_p.run_embedded_js(container,args.doc);var style=_p.consolidate_css(container,args);_p.render_header_on_break(container,args);return _p.render_final(style,stat,container,args);},head_banner_format:function(){return"\
\ {{DESCRIPTION}}\
"},show_draft:function(args){var is_doctype_submittable=0;var plist=locals['DocPerm'];for(var perm in plist){var p=plist[perm];if((p.parent==args.doc.doctype)&&(p.submit==1)){is_doctype_submittable=1;break;}} -if(args.doc&&cint(args.doc.docstatus)==0&&is_doctype_submittable){draft=_p.head_banner_format();draft=draft.replace("{{HEAD}}","DRAFT");draft=draft.replace("{{DESCRIPTION}}","This box will go away after the document is submitted.");return draft;}else{return"";}},show_archived:function(args){if(args.doc&&args.doc.__archived){archived=_p.head_banner_format();archived=archived.replace("{{HEAD}}","ARCHIVED");archived=archived.replace("{{DESCRIPTION}}","You must restore this document to make it editable.");}else{return"";}},consolidate_css:function(container,args){var body_style='';var style_list=container.getElementsByTagName('style');while(style_list&&style_list.length>0){for(i in style_list){if(style_list[i]&&style_list[i].innerHTML){body_style+=style_list[i].innerHTML;var parent=style_list[i].parentNode;if(parent){parent.removeChild(style_list[i]);}else{container.removeChild(style_list[i]);}}} +if(args.doc&&cint(args.doc.docstatus)==0&&is_doctype_submittable){draft=_p.head_banner_format();draft=draft.replace("{{HEAD}}","DRAFT");draft=draft.replace("{{DESCRIPTION}}","This box will go away after the document is submitted.");return draft;}else{return"";}},show_archived:function(args){if(args.doc&&args.doc.__archived){archived=_p.head_banner_format();archived=archived.replace("{{HEAD}}","ARCHIVED");archived=archived.replace("{{DESCRIPTION}}","You must restore this document to make it editable.");return archived;}else{return"";}},show_cancelled:function(args){if(args.doc&&args.doc.docstatus==2){cancelled=_p.head_banner_format();cancelled=cancelled.replace("{{HEAD}}","CANCELLED");cancelled=cancelled.replace("{{DESCRIPTION}}","You must amend this document to make it editable.");return cancelled;}else{return"";}},consolidate_css:function(container,args){var body_style='';var style_list=container.getElementsByTagName('style');while(style_list&&style_list.length>0){for(i in style_list){if(style_list[i]&&style_list[i].innerHTML){body_style+=style_list[i].innerHTML;var parent=style_list[i].parentNode;if(parent){parent.removeChild(style_list[i]);}else{container.removeChild(style_list[i]);}}} style_list=container.getElementsByTagName('style');} style_concat=(args.only_body?'':_p.def_print_style_body) +_p.def_print_style_other+args.style+body_style;return style_concat;},run_embedded_js:function(container,doc){var jslist=container.getElementsByTagName('script');while(jslist&&jslist.length>0){for(i in jslist){if(jslist[i]&&jslist[i].innerHTML){var code=jslist[i].innerHTML;var parent=jslist[i].parentNode;var span=$a(parent,'span');parent.replaceChild(span,jslist[i]);var val=code?eval(code):'';if(!val||typeof(val)=='object'){val='';} diff --git a/js/legacy/utils/datatype.js b/js/legacy/utils/datatype.js index 810d6549f1..4aa98d9a3a 100644 --- a/js/legacy/utils/datatype.js +++ b/js/legacy/utils/datatype.js @@ -318,4 +318,11 @@ function in_words(n) { } else { return bestguess; } -} \ No newline at end of file +} + + +function roundNumber(num, dec) { + var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec); + return result; +} + diff --git a/js/legacy/widgets/autosuggest.js b/js/legacy/widgets/autosuggest.js index 29f7728bd8..0a22b26d6c 100644 --- a/js/legacy/widgets/autosuggest.js +++ b/js/legacy/widgets/autosuggest.js @@ -559,4 +559,4 @@ $ce = function ( type, attr, cont, html ) else if (t == "object") ne.appendChild( cont ); return ne; -}; \ No newline at end of file +}; diff --git a/js/legacy/widgets/form/print_format.js b/js/legacy/widgets/form/print_format.js index 3dc19bf4fa..104b50683e 100644 --- a/js/legacy/widgets/form/print_format.js +++ b/js/legacy/widgets/form/print_format.js @@ -151,6 +151,7 @@ $.extend(_p, { // if draft/archived, show draft/archived banner stat += _p.show_draft(args); stat += _p.show_archived(args); + stat += _p.show_cancelled(args); // Append args.body's content as a child of container container.innerHTML = args.body; @@ -219,6 +220,23 @@ $.extend(_p, { archived = _p.head_banner_format(); archived = archived.replace("{{HEAD}}", "ARCHIVED"); archived = archived.replace("{{DESCRIPTION}}", "You must restore this document to make it editable."); + return archived; + } else { + return ""; + } + }, + + + /* + Check if doc is cancelled + Display cancelled in header if true + */ + show_cancelled: function(args) { + if(args.doc && args.doc.docstatus==2) { + cancelled = _p.head_banner_format(); + cancelled = cancelled.replace("{{HEAD}}", "CANCELLED"); + cancelled = cancelled.replace("{{DESCRIPTION}}", "You must amend this document to make it editable."); + return cancelled; } else { return ""; } diff --git a/js/legacy/wnf.compressed.js b/js/legacy/wnf.compressed.js index 96dcd9eabd..4ec778ddd4 100644 --- a/js/legacy/wnf.compressed.js +++ b/js/legacy/wnf.compressed.js @@ -63,6 +63,7 @@ remainder=n%1000000000}} if(remainder){if(remainder>=100)comma=',' else comma='' return bestguess+comma+' '+in_words(remainder);}else{return bestguess;}} +function roundNumber(num,dec){var result=Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);return result;} var appVer=navigator.appVersion.toLowerCase();var is_minor=parseFloat(appVer);var is_major=parseInt(is_minor);var iePos=appVer.indexOf('msie');if(iePos!=-1){is_minor=parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos))) is_major=parseInt(is_minor);} var isIE=(iePos!=-1);var isIE6=(isIE&&is_major<=6);var isIE7=(isIE&&is_major>=7);if(/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){var isFF=1;var ffversion=new Number(RegExp.$1) diff --git a/py/webnotes/model/__init__.py b/py/webnotes/model/__init__.py index 2448f5d5be..3a7c9e5d5b 100644 --- a/py/webnotes/model/__init__.py +++ b/py/webnotes/model/__init__.py @@ -219,3 +219,42 @@ def copytables(srctype, src, srcfield, tartype, tar, tarfield, srcfields, tarfie def db_exists(dt, dn): import webnotes return webnotes.conn.exists(dt, dn) + + +def delete_fields(args_dict, delete=0): + """ + Delete a field. + * Deletes record from `tabDocField` + * If not single doctype: Drops column from table + * If single, deletes record from `tabSingles` + + args_dict = { dt: [field names] } + """ + for dt in args_dict.keys(): + fields = args_dict[dt] + if not fields: continue + + webnotes.conn.sql("""\ + DELETE FROM `tabDocField` + WHERE parent=%s AND fieldname IN (%s) + """ % ('%s', ", ".join(['"' + f + '"' for f in fields])), dt) + + # Delete the data / column only if delete is specified + if not delete: continue + + is_single = webnotes.conn.sql("select issingle from tabDocType where name = '%s'" % dt) + is_single = is_single and webnotes.utils.cint(is_single[0][0]) or 0 + if is_single: + webnotes.conn.sql("""\ + DELETE FROM `tabSingles` + WHERE doctype=%s AND field IN (%s) + """ % ('%s', ", ".join(['"' + f + '"' for f in fields])), dt) + else: + existing_fields = webnotes.conn.sql("desc `tab%s`" % dt) + existing_fields = existing_fields and [e[0] for e in existing_fields] or [] + query = "ALTER TABLE `tab%s` " % dt + \ + ", ".join(["DROP COLUMN `%s`" % f for f in fields if f in existing_fields]) + webnotes.conn.commit() + webnotes.conn.sql(query) + + diff --git a/py/webnotes/model/doclist.py b/py/webnotes/model/doclist.py index 3d679604bf..9839991034 100644 --- a/py/webnotes/model/doclist.py +++ b/py/webnotes/model/doclist.py @@ -19,7 +19,7 @@ class DocList: if dt and dn: self.load_from_db(dt, dn) - def load_from_db(self, dt, dn): + def load_from_db(self, dt, dn, prefix='tab'): """ Load doclist from dt """ @@ -35,7 +35,9 @@ class DocList: for t in tablefields: doclist += getchildren(doc.name, t[0], t[1], dt, prefix=prefix) - self.docs = docs + self.docs = doclist + self.doc = doc + self.children = doclist[1:] def __iter__(self): """