1 openerp.edi = function(openerp) {
4 openerp.edi.EdiView = openerp.web.Widget.extend({
5 init: function(parent, db, token) {
9 this.session = openerp.connection;
10 this.template = "EdiEmpty";
17 var param = {"db": self.db, "token": self.token};
18 return self.rpc('/edi/get_edi_document', param, this.on_document_loaded, this.on_document_failed);
20 on_document_loaded: function(docs){
22 var template_content = "Edi." + this.doc.__model + ".content";
23 var template_sidebar = "Edi." + this.doc.__model + ".sidebar";
24 var param = {"widget":this, "doc":this.doc};
25 if (openerp.web.qweb.templates[template_sidebar]) {
26 this.sidebar = openerp.web.qweb.render(template_sidebar, param);
28 if (openerp.web.qweb.templates[template_content]) {
29 this.content = openerp.web.qweb.render(template_content, param);
31 this.$element.html(openerp.web.qweb.render("EdiView", param));
32 this.$element.find('button.oe_edi_action_print').bind('click', this.do_print);
33 this.$element.find('button#oe_edi_import_existing').bind('click', this.do_import_existing);
34 this.$element.find('button#oe_edi_import_create').bind('click', this.do_import_create);
35 this.$element.find('button#oe_edi_download').bind('click', this.do_download);
36 this.$element.find('.oe_edi_import_choice, .oe_edi_import_choice_label').bind('click', this.toggle_choice('import'));
37 this.$element.find('.oe_edi_pay_choice, .oe_edi_pay_choice_label').bind('click', this.toggle_choice('pay'));
38 this.$element.find('#oe_edi_download_show_code').bind('click', this.show_code);
40 on_document_failed: function(response) {
44 //TODO: should this be _t() wrapped?
45 message: "Sorry, this document cannot be located. Perhaps the link you are using has expired?"
47 $(openerp.web.qweb.render("DialogWarning", params)).dialog({
48 title: "Document not found",
52 show_code: function($event) {
53 $('#oe_edi_download_code').toggle();
55 get_download_url: function() {
56 var l = window.location;
57 var url_prefix = l.protocol + '//' + l.host;
58 return url_prefix +'/edi/download?db=' + this.db + '&token=' + this.token;
60 get_paypal_url: function(document_type, ref_field) {
61 var comp_name = encodeURIComponent(this.doc.company_id[1]);
62 var doc_ref = encodeURIComponent(this.doc[ref_field]);
63 var paypal_account = encodeURIComponent(this.doc.company_address.paypal_account);
64 var amount = encodeURIComponent(this.doc.amount_total);
65 var cur_code = encodeURIComponent(this.doc.currency.code);
66 var paypal_url = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick" +
67 "&business=" + paypal_account +
68 "&item_name=" + document_type + "%20" + comp_name + "%20" + doc_ref +
69 "&invoice=" + doc_ref +
71 "¤cy_code=" + cur_code +
72 "&button_subtype=services&no_note=1&bn=OpenERP_PayNow_" + cur_code;
75 toggle_choice: function(mode) {
77 $('.oe_edi_nested_block_'+mode).hide();
78 $('.'+$e.target.id+'_nested').show();
82 do_print: function(e){
83 var l = window.location;
84 window.location = l.protocol + '//' + l.host + "/edi/download_attachment?db=" + this.db + "&token=" + this.token;
86 do_import_existing: function(e) {
87 var url_download = this.get_download_url();
88 var $edi_text_server_input = this.$element.find('#oe_edi_txt_server_url');
89 var server_url = $edi_text_server_input.val();
90 $edi_text_server_input.removeClass('invalid');
92 $edi_text_server_input.addClass('invalid');
95 var protocol = "http://";
96 if (server_url.toLowerCase().lastIndexOf('http', 0) == 0 ) {
99 window.location = protocol + server_url + '/edi/import_url?url=' + encodeURIComponent(url_download);
101 do_import_create: function(e){
102 var url_download = this.get_download_url();
103 window.location = "https://cc.my.openerp.com/odms/create_edi?url=" + encodeURIComponent(url_download);
105 do_download: function(e){
106 window.location = this.get_download_url();
110 openerp.edi.edi_view = function (db, token) {
111 openerp.connection.bind().then(function () {
112 new openerp.edi.EdiView(null,db,token).appendTo($("body"));
116 openerp.edi.EdiImport = openerp.web.Widget.extend({
117 init: function(parent,url) {
122 this.template = "EdiImport";
123 this.session = openerp.connection;
124 this.login = new openerp.web.Login(this);
125 this.header = new openerp.web.Header(this);
128 // TODO fix by explicitly asking login if needed
129 //this.session.on_session_invalid.add_last(this.do_ask_login);
130 this.header.appendTo($("#oe_header"));
131 this.login.appendTo($('#oe_login'));
134 do_import: function() {
135 this.rpc('/edi/import_edi_url', {url: this.url}, this.on_imported, this.on_imported_error);
137 on_imported: function(response) {
138 if ('action' in response) {
139 this.rpc("/web/session/save_session_action", {the_action: response.action}, function(key) {
140 window.location = "/web/webclient/home?debug=1&s_action="+encodeURIComponent(key);
146 title: 'Import Successful!',
149 $(this).dialog("close");
150 window.location = "/web/webclient/home";
153 }).html('The document has been successfully imported!');
156 on_imported_error: function(response){
158 var msg = "Sorry, the document could not be imported.";
159 if (response.data.fault_code) {
160 msg += "\n Reason:" + response.data.fault_code;
162 var params = {error: response, message: msg};
163 $(openerp.web.qweb.render("CrashManagerWarning", params)).dialog({
164 title: "Document Import Notification",
167 Ok: function() { $(this).dialog("close"); }
173 openerp.edi.edi_import = function (url) {
174 openerp.connection.bind().then(function () {
175 new openerp.edi.EdiImport(null,url).appendTo($("body"));
180 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: