1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Business Applications
5 # Copyright (c) 2011-2012 OpenERP S.A. <http://openerp.com>
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.
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.
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/>.
20 ##############################################################################
22 from openerp.osv import osv
23 from openerp.tools.translate import _
24 from openerp.addons.edi import EDIMixin
26 PURCHASE_ORDER_LINE_EDI_STRUCT = {
34 # fields used for web preview only - discarded on import
35 'price_subtotal': True,
38 PURCHASE_ORDER_EDI_STRUCT = {
39 'company_id': True, # -> to be changed into partner
45 #custom: 'partner_address',
47 'order_line': PURCHASE_ORDER_LINE_EDI_STRUCT,
50 # fields used for web preview only - discarded on import
52 'amount_untaxed': True,
57 class purchase_order(osv.osv, EDIMixin):
58 _inherit = 'purchase.order'
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')
67 # generate the main report
68 self._edi_generate_report_attachment(cr, uid, order, context=context)
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]
73 # force trans-typing to purchase.order upon import
74 '__import_model': 'sale.order',
75 '__import_module': 'sale',
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],
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)
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
93 self._edi_requires_attributes(('company_id','company_address'), edi_document)
94 res_partner = self.pool.get('res.partner')
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)
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!
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
121 pricelist = self.pool.get('ir.model.data').get_object(cr, uid, 'purchase', 'list0', context=context)
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]
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,
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)
141 return self.edi_m2o(cr, uid, pricelist, context=context)
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
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)
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)
154 #import company as a new partner
155 partner_id = self.edi_import_company(cr, uid, edi_document, context=context)
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)
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)
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)
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)
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)
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)
191 class purchase_order_line(osv.osv, EDIMixin):
192 _inherit='purchase.order.line'