[IMP] Tour: add tour in web module; tour became available in backend.
[odoo/odoo.git] / addons / purchase / edi / purchase_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
22 from openerp.osv import osv
23 from openerp.tools.translate import _
24 from openerp.addons.edi import EDIMixin
25
26 PURCHASE_ORDER_LINE_EDI_STRUCT = {
27     'name': True,
28     'date_planned': True,
29     'product_id': True,
30     'product_uom': True,
31     'price_unit': True,
32     'product_qty': True,
33
34     # fields used for web preview only - discarded on import
35     'price_subtotal': True,
36 }
37
38 PURCHASE_ORDER_EDI_STRUCT = {
39     'company_id': True, # -> to be changed into partner
40     'name': True,
41     'partner_ref': True,
42     'origin': True,
43     'date_order': True,
44     'partner_id': True,
45     #custom: 'partner_address',
46     'notes': True,
47     'order_line': PURCHASE_ORDER_LINE_EDI_STRUCT,
48     #custom: currency_id
49
50     # fields used for web preview only - discarded on import
51     'amount_total': True,
52     'amount_untaxed': True,
53     'amount_tax': True,
54     'state':True,
55 }
56
57 class purchase_order(osv.osv, EDIMixin):
58     _inherit = 'purchase.order'
59
60     def edi_export(self, cr, uid, records, edi_struct=None, context=None):
61         """Exports a purchase order"""
62         edi_struct = dict(edi_struct or PURCHASE_ORDER_EDI_STRUCT)
63         res_company = self.pool.get('res.company')
64         res_partner_obj = self.pool.get('res.partner')
65         edi_doc_list = []
66         for order in records:
67             # generate the main report
68             self._edi_generate_report_attachment(cr, uid, order, context=context)
69
70             # Get EDI doc based on struct. The result will also contain all metadata fields and attachments.
71             edi_doc = super(purchase_order,self).edi_export(cr, uid, [order], edi_struct, context)[0]
72             edi_doc.update({
73                     # force trans-typing to purchase.order upon import
74                     '__import_model': 'sale.order',
75                     '__import_module': 'sale',
76
77                     'company_address': res_company.edi_export_address(cr, uid, order.company_id, context=context),
78                     'partner_address': res_partner_obj.edi_export(cr, uid, [order.partner_id], context=context)[0],
79                     'currency': self.pool.get('res.currency').edi_export(cr, uid, [order.pricelist_id.currency_id],
80                                                                          context=context)[0],
81             })
82             if edi_doc.get('order_line'):
83                 for line in edi_doc['order_line']:
84                     line['__import_model'] = 'sale.order.line'
85             edi_doc_list.append(edi_doc)
86         return edi_doc_list
87
88     def edi_import_company(self, cr, uid, edi_document, context=None):
89         # TODO: for multi-company setups, we currently import the document in the
90         #       user's current company, but we should perhaps foresee a way to select
91         #       the desired company among the user's allowed companies
92
93         self._edi_requires_attributes(('company_id','company_address'), edi_document)
94         res_partner = self.pool.get('res.partner')
95
96         xid, company_name = edi_document.pop('company_id')
97         # Retrofit address info into a unified partner info (changed in v7 - used to keep them separate)
98         company_address_edi = edi_document.pop('company_address')
99         company_address_edi['name'] = company_name
100         company_address_edi['is_company'] = True
101         company_address_edi['__import_model'] = 'res.partner'
102         company_address_edi['__id'] = xid  # override address ID, as of v7 they should be the same anyway
103         if company_address_edi.get('logo'):
104             company_address_edi['image'] = company_address_edi.pop('logo')
105         company_address_edi['supplier'] = True
106         partner_id = res_partner.edi_import(cr, uid, company_address_edi, context=context)
107
108         # modify edi_document to refer to new partner
109         partner = res_partner.browse(cr, uid, partner_id, context=context)
110         partner_edi_m2o = self.edi_m2o(cr, uid, partner, context=context)
111         edi_document['partner_id'] = partner_edi_m2o
112         edi_document.pop('partner_address', None) # ignored, that's supposed to be our own address!
113         return partner_id
114
115     def _edi_get_pricelist(self, cr, uid, partner_id, currency, context=None):
116         # TODO: refactor into common place for purchase/sale, e.g. into product module
117         partner_model = self.pool.get('res.partner')
118         partner = partner_model.browse(cr, uid, partner_id, context=context)
119         pricelist = partner.property_product_pricelist_purchase
120         if not pricelist:
121             pricelist = self.pool.get('ir.model.data').get_object(cr, uid, 'purchase', 'list0', context=context)
122
123         if not pricelist.currency_id == currency:
124             # look for a pricelist with the right type and currency, or make a new one
125             pricelist_type = 'purchase'
126             product_pricelist = self.pool.get('product.pricelist')
127             match_pricelist_ids = product_pricelist.search(cr, uid,[('type','=',pricelist_type),
128                                                                     ('currency_id','=',currency.id)])
129             if match_pricelist_ids:
130                 pricelist_id = match_pricelist_ids[0]
131             else:
132                 pricelist_name = _('EDI Pricelist (%s)') % (currency.name,)
133                 pricelist_id = product_pricelist.create(cr, uid, {'name': pricelist_name,
134                                                                   'type': pricelist_type,
135                                                                   'currency_id': currency.id,
136                                                                  })
137                 self.pool.get('product.pricelist.version').create(cr, uid, {'name': pricelist_name,
138                                                                             'pricelist_id': pricelist_id})
139             pricelist = product_pricelist.browse(cr, uid, pricelist_id)
140
141         return self.edi_m2o(cr, uid, pricelist, context=context)
142
143     def _edi_get_location(self, cr, uid, partner_id, context=None):
144         partner_model = self.pool.get('res.partner')
145         partner = partner_model.browse(cr, uid, partner_id, context=context)
146         location = partner.property_stock_customer
147         if not location:
148             location = self.pool.get('ir.model.data').get_object(cr, uid, 'stock', 'stock_location_stock', context=context)
149         return self.edi_m2o(cr, uid, location, 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['partner_ref'] = edi_document['name']
165         edi_document['name'] = partner_ref or edi_document['name']
166         edi_document['pricelist_id'] = self._edi_get_pricelist(cr, uid, partner_id, order_currency, context=context)
167         edi_document['location_id'] = self._edi_get_location(cr, uid, partner_id, 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         edi_document.pop('payment_term', None)
174         edi_document.pop('order_policy', None)
175         edi_document.pop('user_id', None)
176
177         for order_line in edi_document['order_line']:
178             self._edi_requires_attributes(('date_planned', 'product_id', 'product_uom', 'product_qty', 'price_unit'), order_line)
179             # original sale order contains unit price and discount, but not final line price
180             discount = order_line.pop('discount', 0.0)
181             if discount:
182                 order_line['price_unit'] = res_currency.round(cr, uid, order_currency,
183                                                               (order_line['price_unit'] * (1 - (discount or 0.0) / 100.0)))
184             # sale order lines have sequence numbers, not purchase order lines
185             order_line.pop('sequence', None)
186
187             # discard web preview fields, if present
188             order_line.pop('price_subtotal', None)
189         return super(purchase_order,self).edi_import(cr, uid, edi_document, context=context)
190
191 class purchase_order_line(osv.osv, EDIMixin):
192     _inherit='purchase.order.line'
193