2011-10-06 11:32:13 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Business Applications
|
2012-10-11 15:20:08 +00:00
|
|
|
# Copyright (c) 2011-2012 OpenERP S.A. <http://openerp.com>
|
2011-10-06 11:32:13 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
2012-12-14 11:31:32 +00:00
|
|
|
from openerp.osv import osv, fields
|
2012-12-06 14:56:32 +00:00
|
|
|
from openerp.addons.edi import EDIMixin
|
2012-12-14 11:31:32 +00:00
|
|
|
from openerp.tools.translate import _
|
2012-12-17 14:43:06 +00:00
|
|
|
|
2014-05-20 08:16:17 +00:00
|
|
|
from werkzeug import url_encode
|
2011-10-06 11:32:13 +00:00
|
|
|
|
|
|
|
SALE_ORDER_LINE_EDI_STRUCT = {
|
|
|
|
'sequence': True,
|
|
|
|
'name': True,
|
2011-10-06 16:35:09 +00:00
|
|
|
#custom: 'date_planned'
|
2011-10-06 11:32:13 +00:00
|
|
|
'product_id': True,
|
|
|
|
'product_uom': True,
|
|
|
|
'price_unit': True,
|
|
|
|
#custom: 'product_qty'
|
|
|
|
'discount': True,
|
2011-10-26 00:01:10 +00:00
|
|
|
|
|
|
|
# fields used for web preview only - discarded on import
|
|
|
|
'price_subtotal': True,
|
2011-10-06 11:32:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SALE_ORDER_EDI_STRUCT = {
|
|
|
|
'name': True,
|
|
|
|
'origin': True,
|
|
|
|
'company_id': True, # -> to be changed into partner
|
|
|
|
#custom: 'partner_ref'
|
|
|
|
'date_order': True,
|
|
|
|
'partner_id': True,
|
|
|
|
#custom: 'partner_address'
|
|
|
|
#custom: 'notes'
|
2011-10-13 16:56:00 +00:00
|
|
|
'order_line': SALE_ORDER_LINE_EDI_STRUCT,
|
|
|
|
|
|
|
|
# fields used for web preview only - discarded on import
|
|
|
|
'amount_total': True,
|
|
|
|
'amount_untaxed': True,
|
|
|
|
'amount_tax': True,
|
2011-10-26 00:01:10 +00:00
|
|
|
'payment_term': True,
|
2011-11-02 23:52:24 +00:00
|
|
|
'order_policy': True,
|
2011-11-02 23:58:41 +00:00
|
|
|
'user_id': True,
|
2012-05-25 13:35:36 +00:00
|
|
|
'state': True,
|
2011-10-06 11:32:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class sale_order(osv.osv, EDIMixin):
|
|
|
|
_inherit = 'sale.order'
|
|
|
|
|
|
|
|
def edi_export(self, cr, uid, records, edi_struct=None, context=None):
|
|
|
|
"""Exports a Sale order"""
|
|
|
|
edi_struct = dict(edi_struct or SALE_ORDER_EDI_STRUCT)
|
|
|
|
res_company = self.pool.get('res.company')
|
2012-03-30 09:08:37 +00:00
|
|
|
res_partner_obj = self.pool.get('res.partner')
|
2011-10-06 11:32:13 +00:00
|
|
|
edi_doc_list = []
|
|
|
|
for order in records:
|
2011-10-10 14:38:06 +00:00
|
|
|
# generate the main report
|
|
|
|
self._edi_generate_report_attachment(cr, uid, order, context=context)
|
|
|
|
|
2011-10-06 11:32:13 +00:00
|
|
|
# Get EDI doc based on struct. The result will also contain all metadata fields and attachments.
|
|
|
|
edi_doc = super(sale_order,self).edi_export(cr, uid, [order], edi_struct, context)[0]
|
|
|
|
edi_doc.update({
|
|
|
|
# force trans-typing to purchase.order upon import
|
2011-11-03 14:56:03 +00:00
|
|
|
'__import_model': 'purchase.order',
|
2011-10-06 11:32:13 +00:00
|
|
|
'__import_module': 'purchase',
|
|
|
|
|
|
|
|
'company_address': res_company.edi_export_address(cr, uid, order.company_id, context=context),
|
2012-04-02 15:07:47 +00:00
|
|
|
'partner_address': res_partner_obj.edi_export(cr, uid, [order.partner_id], context=context)[0],
|
2011-10-06 11:32:13 +00:00
|
|
|
|
2011-10-24 14:44:07 +00:00
|
|
|
'currency': self.pool.get('res.currency').edi_export(cr, uid, [order.pricelist_id.currency_id],
|
|
|
|
context=context)[0],
|
2011-10-06 11:32:13 +00:00
|
|
|
'partner_ref': order.client_order_ref or False,
|
|
|
|
'notes': order.note or False,
|
|
|
|
})
|
|
|
|
edi_doc_list.append(edi_doc)
|
|
|
|
return edi_doc_list
|
|
|
|
|
|
|
|
def _edi_import_company(self, cr, uid, edi_document, context=None):
|
|
|
|
# TODO: for multi-company setups, we currently import the document in the
|
|
|
|
# user's current company, but we should perhaps foresee a way to select
|
|
|
|
# the desired company among the user's allowed companies
|
|
|
|
|
|
|
|
self._edi_requires_attributes(('company_id','company_address'), edi_document)
|
2012-10-18 15:21:29 +00:00
|
|
|
res_partner = self.pool.get('res.partner')
|
|
|
|
|
|
|
|
xid, company_name = edi_document.pop('company_id')
|
|
|
|
# Retrofit address info into a unified partner info (changed in v7 - used to keep them separate)
|
|
|
|
company_address_edi = edi_document.pop('company_address')
|
|
|
|
company_address_edi['name'] = company_name
|
|
|
|
company_address_edi['is_company'] = True
|
|
|
|
company_address_edi['__import_model'] = 'res.partner'
|
|
|
|
company_address_edi['__id'] = xid # override address ID, as of v7 they should be the same anyway
|
|
|
|
if company_address_edi.get('logo'):
|
|
|
|
company_address_edi['image'] = company_address_edi.pop('logo')
|
|
|
|
company_address_edi['customer'] = True
|
|
|
|
partner_id = res_partner.edi_import(cr, uid, company_address_edi, context=context)
|
|
|
|
|
|
|
|
# modify edi_document to refer to new partner
|
|
|
|
partner = res_partner.browse(cr, uid, partner_id, context=context)
|
|
|
|
partner_edi_m2o = self.edi_m2o(cr, uid, partner, context=context)
|
|
|
|
edi_document['partner_id'] = partner_edi_m2o
|
|
|
|
edi_document['partner_invoice_id'] = partner_edi_m2o
|
|
|
|
edi_document['partner_shipping_id'] = partner_edi_m2o
|
|
|
|
|
|
|
|
edi_document.pop('partner_address', None) # ignored, that's supposed to be our own address!
|
|
|
|
return partner_id
|
2011-10-06 11:32:13 +00:00
|
|
|
|
2011-10-06 16:35:09 +00:00
|
|
|
def _edi_get_pricelist(self, cr, uid, partner_id, currency, context=None):
|
|
|
|
# TODO: refactor into common place for purchase/sale, e.g. into product module
|
2011-10-06 11:32:13 +00:00
|
|
|
partner_model = self.pool.get('res.partner')
|
|
|
|
partner = partner_model.browse(cr, uid, partner_id, context=context)
|
|
|
|
pricelist = partner.property_product_pricelist
|
|
|
|
if not pricelist:
|
|
|
|
pricelist = self.pool.get('ir.model.data').get_object(cr, uid, 'product', 'list0', context=context)
|
2011-10-06 16:35:09 +00:00
|
|
|
|
|
|
|
if not pricelist.currency_id == currency:
|
|
|
|
# look for a pricelist with the right type and currency, or make a new one
|
|
|
|
pricelist_type = 'sale'
|
|
|
|
product_pricelist = self.pool.get('product.pricelist')
|
|
|
|
match_pricelist_ids = product_pricelist.search(cr, uid,[('type','=',pricelist_type),
|
|
|
|
('currency_id','=',currency.id)])
|
|
|
|
if match_pricelist_ids:
|
|
|
|
pricelist_id = match_pricelist_ids[0]
|
|
|
|
else:
|
|
|
|
pricelist_name = _('EDI Pricelist (%s)') % (currency.name,)
|
|
|
|
pricelist_id = product_pricelist.create(cr, uid, {'name': pricelist_name,
|
|
|
|
'type': pricelist_type,
|
|
|
|
'currency_id': currency.id,
|
|
|
|
})
|
|
|
|
self.pool.get('product.pricelist.version').create(cr, uid, {'name': pricelist_name,
|
|
|
|
'pricelist_id': pricelist_id})
|
|
|
|
pricelist = product_pricelist.browse(cr, uid, pricelist_id)
|
|
|
|
|
2011-10-06 11:32:13 +00:00
|
|
|
return self.edi_m2o(cr, uid, pricelist, context=context)
|
|
|
|
|
|
|
|
def edi_import(self, cr, uid, edi_document, context=None):
|
2011-10-24 14:44:07 +00:00
|
|
|
self._edi_requires_attributes(('company_id','company_address','order_line','date_order','currency'), edi_document)
|
2011-10-06 11:32:13 +00:00
|
|
|
|
|
|
|
#import company as a new partner
|
|
|
|
partner_id = self._edi_import_company(cr, uid, edi_document, context=context)
|
|
|
|
|
2011-10-06 16:35:09 +00:00
|
|
|
# currency for rounding the discount calculations and for the pricelist
|
|
|
|
res_currency = self.pool.get('res.currency')
|
2011-10-24 14:44:07 +00:00
|
|
|
currency_info = edi_document.pop('currency')
|
|
|
|
currency_id = res_currency.edi_import(cr, uid, currency_info, context=context)
|
2011-10-06 16:35:09 +00:00
|
|
|
order_currency = res_currency.browse(cr, uid, currency_id)
|
|
|
|
|
|
|
|
partner_ref = edi_document.pop('partner_ref', False)
|
|
|
|
edi_document['client_order_ref'] = edi_document['name']
|
|
|
|
edi_document['name'] = partner_ref or edi_document['name']
|
2011-10-06 11:32:13 +00:00
|
|
|
edi_document['note'] = edi_document.pop('notes', False)
|
2011-10-06 16:35:09 +00:00
|
|
|
edi_document['pricelist_id'] = self._edi_get_pricelist(cr, uid, partner_id, order_currency, context=context)
|
2011-10-13 16:56:00 +00:00
|
|
|
|
|
|
|
# discard web preview fields, if present
|
|
|
|
edi_document.pop('amount_total', None)
|
|
|
|
edi_document.pop('amount_tax', None)
|
|
|
|
edi_document.pop('amount_untaxed', None)
|
|
|
|
|
2011-10-06 11:32:13 +00:00
|
|
|
order_lines = edi_document['order_line']
|
|
|
|
for order_line in order_lines:
|
2012-10-11 15:20:08 +00:00
|
|
|
self._edi_requires_attributes(('product_id', 'product_uom', 'product_qty', 'price_unit'), order_line)
|
2011-10-06 11:32:13 +00:00
|
|
|
order_line['product_uom_qty'] = order_line['product_qty']
|
|
|
|
del order_line['product_qty']
|
2011-10-26 00:01:10 +00:00
|
|
|
|
|
|
|
# discard web preview fields, if present
|
|
|
|
order_line.pop('price_subtotal', None)
|
2011-10-06 11:32:13 +00:00
|
|
|
return super(sale_order,self).edi_import(cr, uid, edi_document, context=context)
|
|
|
|
|
2012-12-14 11:31:32 +00:00
|
|
|
def _edi_paypal_url(self, cr, uid, ids, field, arg, context=None):
|
|
|
|
res = dict.fromkeys(ids, False)
|
|
|
|
for order in self.browse(cr, uid, ids, context=context):
|
|
|
|
if order.order_policy in ('prepaid', 'manual') and \
|
|
|
|
order.company_id.paypal_account and order.state != 'draft':
|
|
|
|
params = {
|
|
|
|
"cmd": "_xclick",
|
|
|
|
"business": order.company_id.paypal_account,
|
|
|
|
"item_name": order.company_id.name + " Order " + order.name,
|
|
|
|
"invoice": order.name,
|
|
|
|
"amount": order.amount_total,
|
|
|
|
"currency_code": order.pricelist_id.currency_id.name,
|
|
|
|
"button_subtype": "services",
|
|
|
|
"no_note": "1",
|
|
|
|
"bn": "OpenERP_Order_PayNow_" + order.pricelist_id.currency_id.name,
|
|
|
|
}
|
2014-05-20 08:16:17 +00:00
|
|
|
res[order.id] = "https://www.paypal.com/cgi-bin/webscr?" + url_encode(params)
|
2012-12-14 11:31:32 +00:00
|
|
|
return res
|
|
|
|
|
|
|
|
_columns = {
|
|
|
|
'paypal_url': fields.function(_edi_paypal_url, type='char', string='Paypal Url'),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-06 11:32:13 +00:00
|
|
|
class sale_order_line(osv.osv, EDIMixin):
|
|
|
|
_inherit='sale.order.line'
|
|
|
|
|
|
|
|
def edi_export(self, cr, uid, records, edi_struct=None, context=None):
|
2011-10-10 14:38:06 +00:00
|
|
|
"""Overridden to provide sale order line fields with the expected names
|
2011-10-06 16:35:09 +00:00
|
|
|
(sale and purchase orders have different column names)"""
|
2011-10-06 11:32:13 +00:00
|
|
|
edi_struct = dict(edi_struct or SALE_ORDER_LINE_EDI_STRUCT)
|
|
|
|
edi_doc_list = []
|
|
|
|
for line in records:
|
|
|
|
edi_doc = super(sale_order_line,self).edi_export(cr, uid, [line], edi_struct, context)[0]
|
2011-11-05 01:56:31 +00:00
|
|
|
edi_doc['__import_model'] = 'purchase.order.line'
|
2011-10-06 11:32:13 +00:00
|
|
|
edi_doc['product_qty'] = line.product_uom_qty
|
|
|
|
if line.product_uos:
|
|
|
|
edi_doc.update(product_uom=line.product_uos,
|
|
|
|
product_qty=line.product_uos_qty)
|
|
|
|
|
|
|
|
edi_doc_list.append(edi_doc)
|
|
|
|
return edi_doc_list
|
|
|
|
|
2012-04-02 12:36:51 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|