[FIX] edi loading
[odoo/odoo.git] / addons / edi / static / src / js / edi.js
1 openerp.edi = function(openerp) {
2 openerp.edi = {}
3
4 openerp.edi.EdiView = openerp.web.Widget.extend({
5     init: function(parent, db, token) {
6         this._super();
7         this.db = db;
8         this.token = token;
9         this.session = openerp.connection;
10         this.template = "EdiEmpty";
11         this.content = "";
12         this.sidebar = "";
13     },
14     start: function() {
15         this._super();
16         var self = this;
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);
19     },
20     on_document_loaded: function(docs){
21         this.doc = docs[0];
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);
27         }
28         if (openerp.web.qweb.templates[template_content]) {
29             this.content = openerp.web.qweb.render(template_content, param);
30         }
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);
39     },
40     on_document_failed: function(response) {
41         var self = this;
42         var params = {
43                 error: response,
44                 //TODO: should this be _t() wrapped?
45                 message: "Sorry, this document cannot be located. Perhaps the link you are using has expired?"
46         }
47         $(openerp.web.qweb.render("DialogWarning", params)).dialog({
48             title: "Document not found",
49             modal: true,
50         });
51     },
52     show_code: function($event) {
53         $('#oe_edi_download_code').toggle();
54     },
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;
59     },
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 + 
70                      "&amount=" + amount +
71                      "&currency_code=" + cur_code +
72                      "&button_subtype=services&no_note=1&bn=OpenERP_PayNow_" + cur_code;
73         return paypal_url;
74     },
75     toggle_choice: function(mode) {
76         return function($e) {
77             $('.oe_edi_nested_block_'+mode).hide();
78             $('.'+$e.target.id+'_nested').show();
79             return true;
80         }
81     },
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;
85     },
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');
91         if (!server_url) {
92             $edi_text_server_input.addClass('invalid');
93             return false;
94         }
95         var protocol = "http://";
96         if (server_url.toLowerCase().lastIndexOf('http', 0) == 0 ) {
97             protocol = '';
98         }
99         window.location = protocol + server_url + '/edi/import_url?url=' + encodeURIComponent(url_download);
100     },
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);
104     },
105     do_download: function(e){
106         window.location = this.get_download_url();
107     }
108 });
109
110 openerp.edi.edi_view = function (db, token) {
111     openerp.connection.bind().then(function () {
112         new openerp.edi.EdiView(null,db,token).appendTo($("body"));
113     });
114 }
115
116 openerp.edi.EdiImport = openerp.web.Widget.extend({
117     init: function(parent,url) {
118         this._super();
119         this.url = url;
120         var params = {};
121
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);
126     },
127     start: function() {
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'));
132         this.do_import();
133     },
134     do_import: function() {
135         this.rpc('/edi/import_edi_url', {url: this.url}, this.on_imported, this.on_imported_error);
136     },
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);
141             });
142         }
143         else {
144             $('<div>').dialog({
145                 modal: true,
146                 title: 'Import Successful!',
147                 buttons: {
148                     Ok: function() {
149                         $(this).dialog("close");
150                         window.location = "/web/webclient/home";
151                     }
152                 }
153             }).html('The document has been successfully imported!');
154         }
155     },
156     on_imported_error: function(response){
157         var self = this;
158         var msg = "Sorry, the document could not be imported.";
159         if (response.data.fault_code) {
160             msg += "\n Reason:" + response.data.fault_code;
161         }
162         var params = {error: response, message: msg};
163         $(openerp.web.qweb.render("CrashManagerWarning", params)).dialog({
164             title: "Document Import Notification",
165             modal: true,
166             buttons: {
167                 Ok: function() { $(this).dialog("close"); }
168             }
169         });
170     }
171 });
172
173 openerp.edi.edi_import = function (url) {
174     openerp.connection.bind().then(function () {
175         new openerp.edi.EdiImport(null,url).appendTo($("body"));
176     });
177 }
178
179 }
180 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: