[FIX] working urls
[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 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 datetime import datetime, timedelta
23 from dateutil.relativedelta import relativedelta
24
25 from osv import fields, osv, orm
26 from edi import EDIMixin
27 from tools import DEFAULT_SERVER_DATE_FORMAT
28 from tools.translate import _
29
30 PURCHASE_ORDER_LINE_EDI_STRUCT = {
31     'name': True,
32     'date_planned': True,
33     'product_id': True,
34     'product_uom': True,
35     'price_unit': True,
36     'product_qty': True,
37     'notes': True,
38
39     # fields used for web preview only - discarded on import
40     'price_subtotal': True,
41 }
42
43 PURCHASE_ORDER_EDI_STRUCT = {
44     'company_id': True, # -> to be changed into partner
45     'name': True,
46     'partner_ref': True,
47     'origin': True,
48     'date_order': True,
49     'partner_id': True,
50     #custom: 'partner_address',
51     'notes': True,
52     'order_line': PURCHASE_ORDER_LINE_EDI_STRUCT,
53     #custom: currency_id
54
55     # fields used for web preview only - discarded on import
56     'amount_total': True,
57     'amount_untaxed': True,
58     'amount_tax': True,
59 }
60
61 class purchase_order(osv.osv, EDIMixin):
62     _inherit = 'purchase.order'
63
64     def edi_export(self, cr, uid, records, edi_struct=None, context=None):
65         """Exports a purchase order"""
66         edi_struct = dict(edi_struct or PURCHASE_ORDER_EDI_STRUCT)
67         res_company = self.pool.get('res.company')
68         res_partner_address = self.pool.get('res.partner.address')
69         edi_doc_list = []
70         for order in records:
71             # generate the main report
72             self._edi_generate_report_attachment(cr, uid, order, context=context)
73
74             # Get EDI doc based on struct. The result will also contain all metadata fields and attachments.
75             edi_doc = super(purchase_order,self).edi_export(cr, uid, [order], edi_struct, context)[0]
76             edi_doc.update({
77                     # force trans-typing to purchase.order upon import
78                     '__import_model': 'sale.order',
79                     '__import_module': 'sale',
80
81                     'company_address': res_company.edi_export_address(cr, uid, order.company_id, context=context),
82                     'partner_address': res_partner_address.edi_export(cr, uid, [order.partner_address_id], context=context)[0],
83                     'currency': self.pool.get('res.currency').edi_export(cr, uid, [order.pricelist_id.currency_id],
84                                                                          context=context)[0],
85             })
86             if edi_doc.get('order_line'):
87                 for line in edi_doc['order_line']:
88                     line['__import_model'] = 'sale.order.line'
89             edi_doc_list.append(edi_doc)
90         return edi_doc_list
91
92     def edi_import_company(self, cr, uid, edi_document, context=None):
93         # TODO: for multi-company setups, we currently import the document in the
94         #       user's current company, but we should perhaps foresee a way to select
95         #       the desired company among the user's allowed companies
96
97         self._edi_requires_attributes(('company_id','company_address'), edi_document)
98         res_partner_address = self.pool.get('res.partner.address')
99         res_partner = self.pool.get('res.partner')
100
101         # imported company = as a new partner
102         src_company_id, src_company_name = edi_document.pop('company_id')
103         partner_id = self.edi_import_relation(cr, uid, 'res.partner', src_company_name,
104                                               src_company_id, context=context)
105         partner_value = {'customer': True}
106         res_partner.write(cr, uid, [partner_id], partner_value, context=context)
107
108         # imported company_address = new partner address
109         address_info = edi_document.pop('company_address')
110         address_info['partner_id'] = (src_company_id, src_company_name)
111         address_info['type'] = 'default'
112         address_id = res_partner_address.edi_import(cr, uid, address_info, context=context)
113
114         # modify edi_document to refer to new partner/address
115         partner_address = res_partner_address.browse(cr, uid, address_id, context=context)
116         edi_document['partner_id'] = (src_company_id, src_company_name)
117         edi_document.pop('partner_address', False) # ignored
118         edi_document['partner_address_id'] = self.edi_m2o(cr, uid, partner_address, context=context)
119
120         return partner_id
121
122     def _edi_get_pricelist(self, cr, uid, partner_id, currency, context=None):
123         # TODO: refactor into common place for purchase/sale, e.g. into product module
124         partner_model = self.pool.get('res.partner')
125         partner = partner_model.browse(cr, uid, partner_id, context=context)
126         pricelist = partner.property_product_pricelist_purchase
127         if not pricelist:
128             pricelist = self.pool.get('ir.model.data').get_object(cr, uid, 'purchase', 'list0', context=context)
129
130         if not pricelist.currency_id == currency:
131             # look for a pricelist with the right type and currency, or make a new one
132             pricelist_type = 'purchase'
133             product_pricelist = self.pool.get('product.pricelist')
134             match_pricelist_ids = product_pricelist.search(cr, uid,[('type','=',pricelist_type),
135                                                                     ('currency_id','=',currency.id)])
136             if match_pricelist_ids:
137                 pricelist_id = match_pricelist_ids[0]
138             else:
139                 pricelist_name = _('EDI Pricelist (%s)') % (currency.name,)
140                 pricelist_id = product_pricelist.create(cr, uid, {'name': pricelist_name,
141                                                                   'type': pricelist_type,
142                                                                   'currency_id': currency.id,
143                                                                  })
144                 self.pool.get('product.pricelist.version').create(cr, uid, {'name': pricelist_name,
145                                                                             'pricelist_id': pricelist_id})
146             pricelist = product_pricelist.browse(cr, uid, pricelist_id)
147
148         return self.edi_m2o(cr, uid, pricelist, context=context)
149
150     def _edi_get_location(self, cr, uid, partner_id, context=None):
151         partner_model = self.pool.get('res.partner')
152         partner = partner_model.browse(cr, uid, partner_id, context=context)
153         location = partner.property_stock_customer
154         if not location:
155             location = self.pool.get('ir.model.data').get_object(cr, uid, 'stock', 'stock_location_stock', context=context)
156         return self.edi_m2o(cr, uid, location, context=context)
157
158     def edi_import(self, cr, uid, edi_document, context=None):
159         self._edi_requires_attributes(('company_id','company_address','order_line','date_order','currency'), edi_document)
160
161         #import company as a new partner
162         partner_id = self.edi_import_company(cr, uid, edi_document, context=context)
163
164         # currency for rounding the discount calculations and for the pricelist
165         res_currency = self.pool.get('res.currency')
166         currency_info = edi_document.pop('currency')
167         currency_id = res_currency.edi_import(cr, uid, currency_info, context=context)
168         order_currency = res_currency.browse(cr, uid, currency_id)
169
170         partner_ref = edi_document.pop('partner_ref', False)
171         edi_document['partner_ref'] = edi_document['name']
172         edi_document['name'] = partner_ref or edi_document['name']
173         edi_document['pricelist_id'] = self._edi_get_pricelist(cr, uid, partner_id, order_currency, context=context)
174         edi_document['location_id'] = self._edi_get_location(cr, uid, partner_id, context=context)
175
176         # discard web preview fields, if present
177         edi_document.pop('amount_total', None)
178         edi_document.pop('amount_tax', None)
179         edi_document.pop('amount_untaxed', None)
180         edi_document.pop('payment_term', None)
181         edi_document.pop('order_policy', None)
182         edi_document.pop('user_id', None)
183
184         for order_line in edi_document['order_line']:
185             self._edi_requires_attributes(('date_planned', 'product_id', 'product_uom', 'product_qty', 'price_unit'), order_line)
186             # original sale order contains unit price and discount, but not final line price
187             discount = order_line.pop('discount', 0.0)
188             if discount:
189                 order_line['price_unit'] = res_currency.round(cr, uid, order_currency,
190                                                               (order_line['price_unit'] * (1 - (discount or 0.0) / 100.0)))
191             # sale order lines have sequence numbers, not purchase order lines
192             order_line.pop('sequence', None)
193
194             # discard web preview fields, if present
195             order_line.pop('price_subtotal', None)
196         return super(purchase_order,self).edi_import(cr, uid, edi_document, context=context)
197
198 class purchase_order_line(osv.osv, EDIMixin):
199     _inherit='purchase.order.line'
200
201
202 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: