Launchpad automatic translations update.
[odoo/odoo.git] / addons / sale / edi / sale_order.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Business Applications
5 #    Copyright (c) 2011-2012 OpenERP S.A. <http://openerp.com>
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21 from openerp.osv import osv, fields
22 from openerp.addons.edi import EDIMixin
23 from openerp.tools.translate import _
24
25 from urllib import urlencode
26
27 SALE_ORDER_LINE_EDI_STRUCT = {
28     'sequence': True,
29     'name': True,
30     #custom: 'date_planned'
31     'product_id': True,
32     'product_uom': True,
33     'price_unit': True,
34     #custom: 'product_qty'
35     'discount': True,
36
37     # fields used for web preview only - discarded on import
38     'price_subtotal': True,
39 }
40
41 SALE_ORDER_EDI_STRUCT = {
42     'name': True,
43     'origin': True,
44     'company_id': True, # -> to be changed into partner
45     #custom: 'partner_ref'
46     'date_order': True,
47     'partner_id': True,
48     #custom: 'partner_address'
49     #custom: 'notes'
50     'order_line': SALE_ORDER_LINE_EDI_STRUCT,
51
52     # fields used for web preview only - discarded on import
53     'amount_total': True,
54     'amount_untaxed': True,
55     'amount_tax': True,
56     'payment_term': True,
57     'order_policy': True,
58     'user_id': True,
59     'state': True,
60 }
61
62 class sale_order(osv.osv, EDIMixin):
63     _inherit = 'sale.order'
64
65     def edi_export(self, cr, uid, records, edi_struct=None, context=None):
66         """Exports a Sale order"""
67         edi_struct = dict(edi_struct or SALE_ORDER_EDI_STRUCT)
68         res_company = self.pool.get('res.company')
69         res_partner_obj = self.pool.get('res.partner')
70         edi_doc_list = []
71         for order in records:
72             # generate the main report
73             self._edi_generate_report_attachment(cr, uid, order, context=context)
74
75             # Get EDI doc based on struct. The result will also contain all metadata fields and attachments.
76             edi_doc = super(sale_order,self).edi_export(cr, uid, [order], edi_struct, context)[0]
77             edi_doc.update({
78                     # force trans-typing to purchase.order upon import
79                     '__import_model': 'purchase.order',
80                     '__import_module': 'purchase',
81
82                     'company_address': res_company.edi_export_address(cr, uid, order.company_id, context=context),
83                     'partner_address': res_partner_obj.edi_export(cr, uid, [order.partner_id], context=context)[0],
84
85                     'currency': self.pool.get('res.currency').edi_export(cr, uid, [order.pricelist_id.currency_id],
86                                                                          context=context)[0],
87                     'partner_ref': order.client_order_ref or False,
88                     'notes': order.note or False,
89             })
90             edi_doc_list.append(edi_doc)
91         return edi_doc_list
92
93     def _edi_import_company(self, cr, uid, edi_document, context=None):
94         # TODO: for multi-company setups, we currently import the document in the
95         #       user's current company, but we should perhaps foresee a way to select
96         #       the desired company among the user's allowed companies
97
98         self._edi_requires_attributes(('company_id','company_address'), edi_document)
99         res_partner = self.pool.get('res.partner')
100
101         xid, company_name = edi_document.pop('company_id')
102         # Retrofit address info into a unified partner info (changed in v7 - used to keep them separate)
103         company_address_edi = edi_document.pop('company_address')
104         company_address_edi['name'] = company_name
105         company_address_edi['is_company'] = True
106         company_address_edi['__import_model'] = 'res.partner'
107         company_address_edi['__id'] = xid  # override address ID, as of v7 they should be the same anyway
108         if company_address_edi.get('logo'):
109             company_address_edi['image'] = company_address_edi.pop('logo')
110         company_address_edi['customer'] = True
111         partner_id = res_partner.edi_import(cr, uid, company_address_edi, context=context)
112
113         # modify edi_document to refer to new partner
114         partner = res_partner.browse(cr, uid, partner_id, context=context)
115         partner_edi_m2o = self.edi_m2o(cr, uid, partner, context=context)
116         edi_document['partner_id'] = partner_edi_m2o
117         edi_document['partner_invoice_id'] = partner_edi_m2o
118         edi_document['partner_shipping_id'] = partner_edi_m2o
119
120         edi_document.pop('partner_address', None) # ignored, that's supposed to be our own address!
121         return partner_id
122
123     def _edi_get_pricelist(self, cr, uid, partner_id, currency, context=None):
124         # TODO: refactor into common place for purchase/sale, e.g. into product module
125         partner_model = self.pool.get('res.partner')
126         partner = partner_model.browse(cr, uid, partner_id, context=context)
127         pricelist = partner.property_product_pricelist
128         if not pricelist:
129             pricelist = self.pool.get('ir.model.data').get_object(cr, uid, 'product', 'list0', context=context)
130
131         if not pricelist.currency_id == currency:
132             # look for a pricelist with the right type and currency, or make a new one
133             pricelist_type = 'sale'
134             product_pricelist = self.pool.get('product.pricelist')
135             match_pricelist_ids = product_pricelist.search(cr, uid,[('type','=',pricelist_type),
136                                                                     ('currency_id','=',currency.id)])
137             if match_pricelist_ids:
138                 pricelist_id = match_pricelist_ids[0]
139             else:
140                 pricelist_name = _('EDI Pricelist (%s)') % (currency.name,)
141                 pricelist_id = product_pricelist.create(cr, uid, {'name': pricelist_name,
142                                                                   'type': pricelist_type,
143                                                                   'currency_id': currency.id,
144                                                                  })
145                 self.pool.get('product.pricelist.version').create(cr, uid, {'name': pricelist_name,
146                                                                             'pricelist_id': pricelist_id})
147             pricelist = product_pricelist.browse(cr, uid, pricelist_id)
148
149         return self.edi_m2o(cr, uid, pricelist, context=context)
150
151     def edi_import(self, cr, uid, edi_document, context=None):
152         self._edi_requires_attributes(('company_id','company_address','order_line','date_order','currency'), edi_document)
153
154         #import company as a new partner
155         partner_id = self._edi_import_company(cr, uid, edi_document, context=context)
156
157         # currency for rounding the discount calculations and for the pricelist
158         res_currency = self.pool.get('res.currency')
159         currency_info = edi_document.pop('currency')
160         currency_id = res_currency.edi_import(cr, uid, currency_info, context=context)
161         order_currency = res_currency.browse(cr, uid, currency_id)
162
163         partner_ref = edi_document.pop('partner_ref', False)
164         edi_document['client_order_ref'] = edi_document['name']
165         edi_document['name'] = partner_ref or edi_document['name']
166         edi_document['note'] = edi_document.pop('notes', False)
167         edi_document['pricelist_id'] = self._edi_get_pricelist(cr, uid, partner_id, order_currency, context=context)
168
169         # discard web preview fields, if present
170         edi_document.pop('amount_total', None)
171         edi_document.pop('amount_tax', None)
172         edi_document.pop('amount_untaxed', None)
173
174         order_lines = edi_document['order_line']
175         for order_line in order_lines:
176             self._edi_requires_attributes(('product_id', 'product_uom', 'product_qty', 'price_unit'), order_line)
177             order_line['product_uom_qty'] = order_line['product_qty']
178             del order_line['product_qty']
179
180             # discard web preview fields, if present
181             order_line.pop('price_subtotal', None)
182         return super(sale_order,self).edi_import(cr, uid, edi_document, context=context)
183
184     def _edi_paypal_url(self, cr, uid, ids, field, arg, context=None):
185         res = dict.fromkeys(ids, False)
186         for order in self.browse(cr, uid, ids, context=context):
187             if order.order_policy in ('prepaid', 'manual') and \
188                     order.company_id.paypal_account and order.state != 'draft':
189                 params = {
190                     "cmd": "_xclick",
191                     "business": order.company_id.paypal_account,
192                     "item_name": order.company_id.name + " Order " + order.name,
193                     "invoice": order.name,
194                     "amount": order.amount_total,
195                     "currency_code": order.pricelist_id.currency_id.name,
196                     "button_subtype": "services",
197                     "no_note": "1",
198                     "bn": "OpenERP_Order_PayNow_" + order.pricelist_id.currency_id.name,
199                 }
200                 res[order.id] = "https://www.paypal.com/cgi-bin/webscr?" + urlencode(params)
201         return res
202
203     _columns = {
204         'paypal_url': fields.function(_edi_paypal_url, type='char', string='Paypal Url'),
205     }
206
207
208 class sale_order_line(osv.osv, EDIMixin):
209     _inherit='sale.order.line'
210
211     def edi_export(self, cr, uid, records, edi_struct=None, context=None):
212         """Overridden to provide sale order line fields with the expected names
213            (sale and purchase orders have different column names)"""
214         edi_struct = dict(edi_struct or SALE_ORDER_LINE_EDI_STRUCT)
215         edi_doc_list = []
216         for line in records:
217             edi_doc = super(sale_order_line,self).edi_export(cr, uid, [line], edi_struct, context)[0]
218             edi_doc['__import_model'] = 'purchase.order.line'
219             edi_doc['product_qty'] = line.product_uom_qty
220             if line.product_uos:
221                 edi_doc.update(product_uom=line.product_uos,
222                                product_qty=line.product_uos_qty)
223
224             edi_doc_list.append(edi_doc)
225         return edi_doc_list
226
227 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: