[IMP] account: improve edi import of invoice to split in different stuff

[IMP] sale: edi import process and correct EDI struct on exort
[IMP] purchase: edi import process and correct EDI struct on export

bzr revid: hmo@tinyerp.com-20110913125923-2gbhgd1vzyd2qrwy
This commit is contained in:
Harry (OpenERP) 2011-09-13 18:29:23 +05:30
parent 047cac6f10
commit 4dc536c8ce
9 changed files with 362 additions and 378 deletions

View File

@ -66,20 +66,9 @@ class account_invoice(osv.osv, ir_edi.edi):
'base_amount': True,
'tax_amount': True,
},
#'paid': True,
}
partner_pool = self.pool.get('res.partner')
partner_address_pool = self.pool.get('res.partner.address')
company_address_dict = {
'street': True,
'street2': True,
'zip': True,
'city': True,
'state_id': True,
'country_id': True,
'email': True,
'phone': True,
}
company_pool = self.pool.get('res.company')
edi_doc_list = []
for invoice in records:
# Get EDI doc based on struct. The result will also contain all metadata fields and attachments.
@ -89,26 +78,10 @@ class account_invoice(osv.osv, ir_edi.edi):
edi_doc = edi_doc[0]
# Add company info and address
res = partner_pool.address_get(cr, uid, [invoice.company_id.partner_id.id], ['contact', 'invoice'])
contact_addr_id = res['contact']
invoice_addr_id = res['invoice']
address = partner_address_pool.browse(cr, uid, invoice_addr_id, context=context)
edi_company_address_dict = {}
for key, value in company_address_dict.items():
if not value:
continue
address_rec = getattr(address, key)
if not address_rec:
continue
if key.endswith('_id'):
address_rec = self.edi_m2o(cr, uid, address_rec, context=context)
edi_company_address_dict[key] = address_rec
edi_company_document = company_pool.edi_export_address(cr, uid, [invoice.company_id], context=context)[invoice.company_id.id]
edi_doc.update({
'company_address': edi_company_address_dict,
#'company_logo': inv_comp.logo,#TODO
#'paid': inv_comp.paid, #TODO
'company_address': edi_company_document['company_address'],
#'company_logo': edi_company_document['company_logo'],#TODO
})
edi_doc_list.append(edi_doc)
return edi_doc_list
@ -158,54 +131,30 @@ class account_invoice(osv.osv, ir_edi.edi):
return account
def edi_import_company(self, cr, uid, edi_document, context=None):
# import company as a new partner, if type==in then supplier=1, else customer=1
# partner_id field is modified to point to the new partner
# company_address data used to add address to new partner
partner_pool = self.pool.get('res.partner')
partner_address_pool = self.pool.get('res.partner.address')
edi_company_address = edi_document['company_address']
edi_partner_id = edi_document['partner_id']
company_name = edi_document['company_id'][1]
partner_pool = self.pool.get('res.partner')
company_pool = self.pool.get('res.company')
# import company as a new partner, if type==in then supplier=1, else customer=1
# company_address data used to add address to new partner
invoice_type = edi_document['type']
state_id = edi_company_address.get('state_id', False)
state_name = state_id and state_id[1]
country_id = edi_company_address.get('country_id', False)
country_name = country_id and country_id[1]
country_id = country_name and self.edi_import_relation(cr, uid, 'res.country', country_name, context=context) or False
state_id = state_name and self.edi_import_relation(cr, uid, 'res.country.state', state_name,
values={'country_id': country_id, 'code': state_name}, context=context) or False
address_value = {
'street': edi_company_address.get('street', False),
'street2': edi_company_address.get('street2', False),
'zip': edi_company_address.get('zip', False),
'city': edi_company_address.get('city', False),
'state_id': state_id,
'country_id': country_id,
'email': edi_company_address.get('email', False),
'phone': edi_company_address.get('phone', False),
}
partner_value = {'name': company_name}
partner_value = {}
if invoice_type in ('out_invoice', 'in_refund'):
partner_value.update({'customer': True, 'supplier': False})
partner_value.update({'customer': True})
if invoice_type in ('in_invoice', 'out_refund'):
partner_value.update({'customer': False, 'supplier': True})
partner_id = partner_pool.create(cr, uid, partner_value, context=context)
address_value.update({'partner_id': partner_id})
address_id = partner_address_pool.create(cr, uid, address_value, context=context)
partner_value.update({'supplier': True})
partner_id = company_pool.edi_import_as_partner(cr, uid, edi_document, values=partner_value, context=context)
# partner_id field is modified to point to the new partner
res = partner_pool.address_get(cr, uid, [partner_id], ['contact', 'invoice'])
address_id = res['invoice']
partner = partner_pool.browse(cr, uid, partner_id, context=context)
edi_document['partner_id'] = self.edi_m2o(cr, uid, partner, context=context)
partner_address = partner_address_pool.browse(cr, uid, address_id, context=context)
edi_document['partner_id'] = self.edi_m2o(cr, uid, partner, context=context)
edi_document['address_invoice_id'] = self.edi_m2o(cr, uid, partner_address, context=context)
return partner.id
del edi_document['company_id']
return partner_id
def edi_import(self, cr, uid, edi_document, context=None):
""" During import, invoices will import the company that is provided in the invoice as
@ -251,10 +200,7 @@ class account_invoice(osv.osv, ir_edi.edi):
# internal number: reset to False, auto-generated
edi_document['internal_number'] = False
# company should set by default so delete company data from edi Document
del edi_document['company_address']
del edi_document['company_id']
# journal_id: should be selected based on type: simply put the 'type' in the context when calling create(), will be selected correctly
journal = self.get_invoice_journal(cr, uid, invoice_type, context=context)
@ -281,7 +227,6 @@ class account_invoice(osv.osv, ir_edi.edi):
edi_tax_line['manual'] = True
# TODO :=> payment_term: if set, create a default one based on name...
return super(account_invoice,self).edi_import(cr, uid, edi_document, context=context)
account_invoice()
@ -294,3 +239,5 @@ class account_invoice_tax(osv.osv, ir_edi.edi):
_inherit = "account.invoice.tax"
account_invoice_tax()

View File

@ -56,7 +56,7 @@
wf_service.trg_validate(uid, 'account.invoice',invoices.id,'invoice_open', cr)
-
I Tesing of EDI functionality. First I export customer invoice from my company than import that invoice into customer company
I Testing of EDI functionality. First I export customer invoice from my company than import that invoice into customer company
-
!python {model: ir.edi.document}: |
invoice_pool = self.pool.get('account.invoice')
@ -64,9 +64,60 @@
tokens = self.export_edi(cr, uid, [invoice])
assert tokens, 'Token is not generated'
document = self.get_document(cr, uid, tokens[0])
a = self.import_edi(cr, uid, edi_document = document)
assert a, 'Invoice is not imported'
-
I import of EDI document of custmer invoice
-
!python {model: ir.edi.document}: |
invoice_pool = self.pool.get('account.invoice')
edi_document = {
"internal_number": "SAJ/2011/002",
"company_address": {
"city": "Gerompont",
"zip": "1367",
"__last_update": False,
"country_id": ["b22acf7a-ddcd-11e0-a4db-701a04e25543:base.be", "Belgium"],
"__id": "b22acf7a-ddcd-11e0-a4db-701a04e25543:base.main_address",
"phone": "(+32).81.81.37.00",
"street": "Chaussee de Namur 40"
},
"company_id": ["b22acf7a-ddcd-11e0-a4db-701a04e25543:account.res_company_test11", "Thomson pvt. ltd."],
"currency_id": ["b22acf7a-ddcd-11e0-a4db-701a04e25543:base.EUR", "EUR (\u20ac)"],
"address_invoice_id": ["b22acf7a-ddcd-11e0-a4db-701a04e25543:base.res_partner_address_11", "Sebastien LANGE, France, Alencon, 1 place de l'\u00c9glise"],
"partner_id": ["b22acf7a-ddcd-11e0-a4db-701a04e25543:account.res_partner_test20", "Junjun wala"],
"__attachments": [],
"__module": "account",
"amount_total": 1010.0,
"date_invoice": "2011-06-22",
"amount_untaxed": 10.0,
"name": "selling product",
"__model": "account.invoice",
"__last_update": False,
"tax_line": [{
"amount": 1000.0,
"manual": True,
"__id": "b22acf7a-ddcd-11e0-a4db-701a04e25543:b22acf7a-ddcd-11e0-a4db-701a04e25543:account.account_invoice_tax-4g4EutbiEMVl",
"name": "sale tax",
"__last_update": False
}],
"__id": "b22acf7a-ddcd-11e0-a4db-701a04e25543:account.customer_invoice_test",
"amount_tax": 1000.0,
"__version": [6, 1],
"type": "out_invoice",
"invoice_line": [{
"uos_id": ["b22acf7a-ddcd-11e0-a4db-701a04e25543:product.product_uom_unit", "PCE"],
"name": "basic pc",
"__last_update": False,
"price_unit": 10.0,
"price_subtotal": 10.0,
"__id": "b22acf7a-ddcd-11e0-a4db-701a04e25543:b22acf7a-ddcd-11e0-a4db-701a04e25543:account.account_invoice_line-1RP3so-u2vV4",
"product_id": ["b22acf7a-ddcd-11e0-a4db-701a04e25543:product.product_product_pc1", "[PC1] Basic PC"],
"quantity": 1.0
}]
}
invoice_id = invoice_pool.edi_import(cr, uid, edi_document, context=context)
invoice_new = invoice_pool.browse(cr, uid, invoice_id, context=context)
assert invoice_id, 'Invoice is not imported'
-
I Checking the out invoice become in invoice or not after import
-
@ -75,8 +126,7 @@
invoice_old = self.browse(cr, uid, ref("customer_invoice_test"))
new_partner_id = self.pool.get('res.partner').name_search(cr, uid, invoice_old.company_id.name)
assert new_partner_id, 'Partner is not created of Supplier'
ids = self.search(cr, uid, [('partner_id','=',new_partner_id[0][0]),('reference','=',invoice_old.internal_number)])
ids = self.search(cr, uid, [('partner_id','=',new_partner_id[0][0]),('reference','=',"SAJ/2011/002")])
assert ids, 'Invoice does not have created of party'
invoice_new = self.browse(cr, uid, ids[0])
assert invoice_new.reference == invoice_old.internal_number, "internal number is not stored in reference"

View File

@ -22,58 +22,48 @@
from osv import fields, osv, orm
from base.ir import ir_edi
from tools.translate import _
from datetime import date
from datetime import date, datetime, timedelta
class purchase_order(osv.osv, ir_edi.edi):
_inherit = 'purchase.order'
def edi_export(self, cr, uid, records, edi_struct=None, context=None):
"""Exports a supplier or customer invoice"""
"""Exports a purchase order"""
edi_struct = {
'company_id': True, # -> to be changed into partner
'name': True,
'date_order': True,
'partner_id': True,
'partner_address_id': True, #only one address needed
#SO: 'partner_order_id'
#PO: 'partner_address_id'
'pricelist_id': True,
'notes': True,
#SO: 'note'
#PO: 'notes'
'amount_total': True,
'amount_tax': True,
'amount_untaxed': True,
'order_line': {
'name': True,
'origin': True,
'date_order': True,
'date_approve': True,
'partner_id': True,
'partner_address_id': True,
'dest_address_id': True,
'warehouse_id': True,
'location_id': True,
'pricelist_id': True,
'validator' : True,
'amount_tax': True,
'amount_total': True,
'amount_untaxed': True,
'order_line': {
'name': True,
'product_qty': True,
'date_planned': True,
'taxes_id': True,
'product_uom': True,
'product_id': True,
'move_dest_id': True,
'price_unit': True,
'order_id': True,
'invoiced': True,
'price_subtotal': True,
},
'invoice_ids': True,
'shipped': True,
'company_id': True,
}
partner_pool = self.pool.get('res.partner')
partner_address_pool = self.pool.get('res.partner.address')
company_address_dict = {
'street': True,
'street2': True,
'zip': True,
'city': True,
'state_id': True,
'country_id': True,
'email': True,
'phone': True,
'date_planned': True,
#SO: 'delay' : 'date_approve' - 'date_planned'
#PO: 'date_planned': 'date_approve' + 'delay'
'product_id': True,
'product_uom': True,
'price_unit': True,
'product_qty': True,
#SO: 'product_uom_qty'
#PO: 'product_qty'
'notes': True,
}
}
company_pool = self.pool.get('res.company')
edi_doc_list = []
for order in records:
# Get EDI doc based on struct. The result will also contain all metadata fields and attachments.
@ -83,107 +73,80 @@ class purchase_order(osv.osv, ir_edi.edi):
edi_doc = edi_doc[0]
# Add company info and address
res = partner_pool.address_get(cr, uid, [order.company_id.partner_id.id], ['contact', 'order'])
contact_addr_id = res['contact']
invoice_addr_id = res['order']
address = partner_address_pool.browse(cr, uid, invoice_addr_id, context=context)
edi_company_address_dict = {}
for key, value in company_address_dict.items():
if not value:
continue
address_rec = getattr(address, key)
if not address_rec:
continue
if key.endswith('_id'):
address_rec = self.edi_m2o(cr, uid, address_rec, context=context)
edi_company_address_dict[key] = address_rec
edi_company_document = company_pool.edi_export_address(cr, uid, [order.company_id], context=context)[order.company_id.id]
edi_doc.update({
'company_address': edi_company_address_dict,
#'company_logo': inv_comp.logo,#TODO
#'paid': inv_comp.paid, #TODO
'company_address': edi_company_document['company_address'],
#'company_logo': edi_company_document['company_logo'],#TODO
})
edi_doc['__model'] = 'sale.order'
edi_doc_list.append(edi_doc)
return edi_doc_list
def edi_import(self, cr, uid, edi_document, context=None):
partner_pool = self.pool.get('res.partner')
def edi_import_company(self, cr, uid, edi_document, context=None):
partner_address_pool = self.pool.get('res.partner.address')
model_data_pool = self.pool.get('ir.model.data')
product_pool = self.pool.get('product.product')
product_categ_pool = self.pool.get('product.category')
partner_pool = self.pool.get('res.partner')
company_pool = self.pool.get('res.company')
country_pool = self.pool.get('res.country')
state_pool = self.pool.get('res.country.state')
account_journal_pool = self.pool.get('account.journal')
invoice_line_pool = self.pool.get('account.invoice.line')
account_pool = self.pool.get('account.account')
stock = self.pool.get('stock.location')
tax_id = []
account_id = []
partner_id = None
company_id = None
# import company as a new partner, supplier=1.
# company_address data used to add address to new partner
partner_value = {'customer': True}
partner_id = company_pool.edi_import_as_partner(cr, uid, edi_document, values=partner_value, context=context)
# partner_id field is modified to point to the new partner
res = partner_pool.address_get(cr, uid, [partner_id], ['contact', 'invoice'])
address_id = res['invoice']
partner = partner_pool.browse(cr, uid, partner_id, context=context)
partner_address = partner_address_pool.browse(cr, uid, address_id, context=context)
edi_document['partner_id'] = self.edi_m2o(cr, uid, partner, context=context)
edi_document['partner_address_id'] = self.edi_m2o(cr, uid, partner_address, context=context)
del edi_document['company_id']
return partner_id
def edi_get_pricelist(self, cr, uid, partner_id, context=None):
# return value = ["724f93ec-ddd0-11e0-88ec-701a04e25543:product.list0", "Public Pricelist (EUR)"]
partner_model = self.pool.get('res.partner')
partner = partner_model.browse(cr, uid, partner_id, context=context)
pricelist = partner.property_product_pricelist_purchase
if not pricelist:
pricelist = self.pool.get('ir.model.data').get_object(cr, uid, 'purchase', 'list0', context=context)
return self.edi_m2o(cr, uid, pricelist, context=context)
def edi_get_location(self, cr, uid, partner_id, context=None):
# return value = ["724f93ec-ddd0-11e0-88ec-701a04e25543:stock.stock_location_stock", "Stock"]
partner_model = self.pool.get('res.partner')
partner = partner_model.browse(cr, uid, partner_id, context=context)
location = partner.property_stock_customer
if not location:
location = self.pool.get('ir.model.data').get_object(cr, uid, 'stock', 'stock_location_stock', context=context)
return self.edi_m2o(cr, uid, location, context=context)
def edi_import(self, cr, uid, edi_document, context=None):
if context is None:
context = {}
print edi_document
# import company as a new partner, if type==in then supplier=1, else customer=1
# partner_id field is modified to point to the new partner
# company_address data used to add address to new partner
edi_company_address = edi_document['company_address']
edi_partner_id = edi_document['partner_id']
company_name = edi_document['shop_id'][1]
state_id = edi_company_address.get('state_id', False)
state_name = state_id and state_id[1]
country_id = edi_company_address.get('country_id', False)
country_name = country_id and country_id[1]
country_id = country_name and self.edi_import_relation(cr, uid, 'res.country', country_name, context=context) or False
state_id = state_name and self.edi_import_relation(cr, uid, 'res.country.state', state_name,
values={'country_id': country_id, 'code': state_name}, context=context) or False
address_value = {
'street': edi_company_address.get('street', False),
'street2': edi_company_address.get('street2', False),
'zip': edi_company_address.get('zip', False),
'city': edi_company_address.get('city', False),
'state_id': state_id,
'country_id': country_id,
'email': edi_company_address.get('email', False),
'phone': edi_company_address.get('phone', False),
}
partner_value = {'name': company_name}
partner_value.update({'customer': False, 'supplier': True})
partner_id = partner_pool.create(cr, uid, partner_value, context=context)
address_value.update({'partner_id': partner_id})
address_id = partner_address_pool.create(cr, uid, address_value, context=context)
partner_address = partner_address_pool.browse(cr, uid, address_id, context=context)
edi_document.update({'partner_address_id':self.edi_m2o(cr, uid, partner_address, context=context)})
partner = partner_pool.browse(cr, uid, partner_id, context=context)
edi_document['partner_id'] = self.edi_m2o(cr, uid, partner, context=context)
location_id = stock.search(cr, uid,[('name','=','Stock')])
location = stock.browse(cr, uid, location_id[0])
edi_document.update({'dest_address_id': edi_document['partner_shipping_id'],'location_id': self.edi_m2o(cr, uid, location, context=context)})
for line in range(len(edi_document['order_line'])):
product_qty = edi_document['order_line'][line]['product_uom_qty']
edi_document['order_line'][line].update({'product_qty': product_qty,'taxes_id':edi_document['order_line'][line]['tax_id']})
# all fields are converted for purchase order import so unnecessary fields are deleted
delete_key = ['sequence','procurement_id','product_uom_qty','company_address','shop_id','create_date','picking_policy','order_policy','partner_order_id','partner_shipping_id','invoice_quantity','partner_invoice_id','price_subtotal','date_confirm']
for key in delete_key:
if edi_document.has_key(key):
del edi_document[key]
else:
for document in edi_document['order_line']:
if document.has_key(key):
del document[key]
print "in the edi_purchase import",edi_document
model = edi_document['__model']
assert model == 'sale.order', _('Could not import purchase order')
edi_document['__model'] = self._name
#import company as a new partner
partner_id = self.edi_import_company(cr, uid, edi_document, context=context)
edi_document['partner_ref'] = edi_document['name']
edi_document['notes'] = edi_document.get('note', False)
edi_document['pricelist_id'] = self.edi_get_pricelist(cr, uid, partner_id, context=context)
edi_document['location_id'] = self.edi_get_location(cr, uid, partner_id, context=context)
for order_line in edi_document['order_line']:
order_line['product_qty'] = order_line['product_uom_qty']
date_order = datetime.strptime(edi_document['date_order'], "%Y-%m-%d")
delay = order_line.get('delay', 0.0)
order_line['date_planned'] = (date_order + timedelta(days=delay)).strftime("%Y-%m-%d")
# price_unit = price_unit - discount
discount = order_line.get('discount', 0.0)
price_unit = order_line['price_unit']
if discount:
price_unit = price_unit * (1 - (discount or 0.0) / 100.0)
order_line['price_unit'] = price_unit
return super(purchase_order,self).edi_import(cr, uid, edi_document, context=context)
purchase_order()
class purchase_order_line(osv.osv, ir_edi.edi):

View File

@ -19,9 +19,9 @@ if not object.partner_id.opt_out: self.pool.get('email.template').generate_mail(
<!--calling multi action in workflow.activity -->
<record id="purchase.act_confirmed" model="workflow.activity">
<!--<record id="purchase.act_confirmed" model="workflow.activity">
<field name="action_id" ref="ir_actions_server_edi_purchase"/>
</record>
</record> -->
<!--Email template -->

View File

@ -5,7 +5,7 @@
name: jones white
supplier: False
customer: True
opt_out: False
opt_out: True
-
I create one Purchase Order
-
@ -30,18 +30,56 @@
import netsvc
wf_service = netsvc.LocalService("workflow")
wf_service.trg_validate(uid, 'purchase.order',orders.id,'approved', cr)
-
I Tesing of EDI functionality. First I export Purchase Order from my company than import that Order into customer company
-
!python {model: ir.edi.document}: |
order_pool = self.pool.get('purchase.order')
purchase_id = order_pool.search(cr, uid, ref("purchase_order_test"))
orders = order_pool.browse(cr, uid, purchase_id)
import json
order = order_pool.browse(cr, uid, ref("purchase_order_test"))
tokens = self.export_edi(cr, uid, [orders])
tokens = self.export_edi(cr, uid, [order])
assert tokens, 'Token is not generated'
document = self.get_document(cr, uid, tokens[0])
document = json.loads(document)
document[0]["__model"] = "sale.order"
document = json.dumps(document)
a = self.import_edi(cr, uid, edi_document = document)
-
I import purchase order from EDI Document of sale order
-
!python {model: ir.edi.document}: |
purchase_order_pool = self.pool.get('purchase.order')
edi_document = {
"order_line": [{
"product_uom_qty": 1.0,
"name": "basic pc",
"product_uom": ["724f93ec-ddd0-11e0-88ec-701a04e25543:product.product_uom_unit", "PCE"],
"sequence": 10,
"price_unit": 150.0,
"__last_update": False,
"__id": "724f93ec-ddd0-11e0-88ec-701a04e25543:724f93ec-ddd0-11e0-88ec-701a04e25543:sale.sale_order_line-LXEqeuI-SSP0",
"product_id": ["724f93ec-ddd0-11e0-88ec-701a04e25543:product.product_product_pc1", "[PC1] Basic PC"]
}],
"order_policy": "manual",
"company_address": {
"city": "Gerompont",
"zip": "1367",
"__last_update": False,
"country_id": ["724f93ec-ddd0-11e0-88ec-701a04e25543:base.be", "Belgium"],
"__id": "724f93ec-ddd0-11e0-88ec-701a04e25543:base.main_address",
"phone": "(+32).81.81.37.00",
"street": "Chaussee de Namur 40"
},
"partner_order_id": ["724f93ec-ddd0-11e0-88ec-701a04e25543:base.res_partner_address_3", "Thomas Passot, Belgium, Louvain-la-Neuve, Rue de l'Angelique, 1"],
"__id": "724f93ec-ddd0-11e0-88ec-701a04e25543:sale.sale_order_test",
"date_order": "2011-09-13",
"partner_id": ["724f93ec-ddd0-11e0-88ec-701a04e25543:sale.res_partner_test22", "Junjun wala"],
"__attachments": [],
"__module": "sale",
"amount_total": 150.0,
"amount_untaxed": 150.0,
"name": "SO008",
"__model": "sale.order",
"__last_update": False,
"company_id": ["724f93ec-ddd0-11e0-88ec-701a04e25543:base.main_company", "OpenERP S.A."],
"__version": [6, 1],
"pricelist_id": ["724f93ec-ddd0-11e0-88ec-701a04e25543:product.list0", "Public Pricelist (EUR)"]
}
new_purchase_order_id = purchase_order_pool.edi_import(cr, uid, edi_document, context=context)
assert new_purchase_order_id, 'Purchase order is not imported'

View File

@ -89,6 +89,7 @@ Dashboard for Sales Manager that includes:
],
'demo_xml': ['sale_demo.xml'],
'test': [
'test/edi_sale_order.yml',
'test/data_test.yml',
'test/manual_order_policy.yml',
'test/prepaid_order_policy.yml',
@ -100,7 +101,7 @@ Dashboard for Sales Manager that includes:
'test/invoice_on_ordered_qty.yml',
'test/invoice_on_shipped_qty.yml',
'test/sale_report.yml',
'test/edi_sale_order.yml',
],
'installable': True,
'active': False,

View File

@ -22,7 +22,7 @@
from osv import fields, osv, orm
from base.ir import ir_edi
from tools.translate import _
from datetime import date
from datetime import date, datetime
class sale_order(osv.osv, ir_edi.edi):
_inherit = 'sale.order'
@ -30,61 +30,44 @@ class sale_order(osv.osv, ir_edi.edi):
def edi_export(self, cr, uid, records, edi_struct=None, context=None):
"""Exports a Sale order"""
edi_struct = {
'company_id': True, # -> to be changed into partner
'name': True,
'date_order': True,
'partner_id': True,
'partner_order_id': True, #only one address needed
#SO: 'partner_order_id'
#PO: 'partner_address_id'
'pricelist_id': True,
'note': True,
#SO: 'note'
#PO: 'notes'
'amount_total': True,
'amount_tax': True,
'amount_untaxed': True,
'order_line': {
'sequence': True,
#SO: yes
#PO: No
'name': True,
'origin' : True,
'picking_policy': True,
'order_policy': True,
'client_order_ref': True,
'date_order': True,
'partner_id': True,
'note': True,
'amount_untaxed': True,
'payment_term': True,
'amount_tax': True,
'amount_total': True,
'amount_untaxed': True,
'pricelist_id': True,
'incoterm': True,
'partner_order_id': True,
'partner_invoice_id': True,
'partner_shipping_id': True,
'shipped': True,
'picked_rate': True,
'invoice_quantity': True,
'invoiced': True,
'invoiced_rate': True,
'order_line': {
'product_uos_qty': True,
'product_uom': True,
'sequence': True,
'price_unit': True,
'product_uom_qty': True,
'discount': True,
'product_uos': True,
'invoiced': True,
'delay': True,
'name': True,
'notes': True,
'product_id': True,
'th_weight': True,
'product_packaging': True,
'tax_id': True,
},
}
partner_pool = self.pool.get('res.partner')
partner_address_pool = self.pool.get('res.partner.address')
company_address_dict = {
'street': True,
'street2': True,
'zip': True,
'city': True,
'state_id': True,
'country_id': True,
'email': True,
'phone': True,
'delay': True,
#SO: 'delay' : 'date_approve' - 'date_planned'
#PO: 'date_planned': 'date_approve' + 'delay'
'product_id': True,
'product_uom': True,
'price_unit': True,
'product_uom_qty': True,
#SO: 'product_uom_qty'
#PO: 'product_qty'
'discount': True,
#SO: yes
#PO: No
'notes': True,
}
}
company_pool = self.pool.get('res.company')
edi_doc_list = []
for order in records:
# Get EDI doc based on struct. The result will also contain all metadata fields and attachments.
@ -94,101 +77,67 @@ class sale_order(osv.osv, ir_edi.edi):
edi_doc = edi_doc[0]
# Add company info and address
res = partner_pool.address_get(cr, uid, [order.shop_id.company_id.partner_id.id], ['contact', 'order'])
contact_addr_id = res['contact']
invoice_addr_id = res['order']
address = partner_address_pool.browse(cr, uid, invoice_addr_id, context=context)
edi_company_address_dict = {}
for key, value in company_address_dict.items():
if not value:
continue
address_rec = getattr(address, key)
if not address_rec:
continue
if key.endswith('_id'):
address_rec = self.edi_m2o(cr, uid, address_rec, context=context)
edi_company_address_dict[key] = address_rec
edi_company_document = company_pool.edi_export_address(cr, uid, [order.company_id], context=context)[order.company_id.id]
edi_doc.update({
'company_address': edi_company_address_dict,
'company_address': edi_company_document['company_address'],
#'company_logo': edi_company_document['company_logo'],#TODO
})
edi_doc_list.append(edi_doc)
return edi_doc_list
def edi_import(self, cr, uid, edi_document, context=None):
partner_pool = self.pool.get('res.partner')
def edi_import_company(self, cr, uid, edi_document, context=None):
partner_address_pool = self.pool.get('res.partner.address')
model_data_pool = self.pool.get('ir.model.data')
product_pool = self.pool.get('product.product')
product_categ_pool = self.pool.get('product.category')
partner_pool = self.pool.get('res.partner')
company_pool = self.pool.get('res.company')
country_pool = self.pool.get('res.country')
state_pool = self.pool.get('res.country.state')
tax_id = []
account_id = []
partner_id = None
company_id = None
# import company as a new partner, supplier=1.
# company_address data used to add address to new partner
partner_value = {'supplier': True}
partner_id = company_pool.edi_import_as_partner(cr, uid, edi_document, values=partner_value, context=context)
# partner_id field is modified to point to the new partner
res = partner_pool.address_get(cr, uid, [partner_id], ['contact', 'invoice'])
address_id = res['invoice']
partner = partner_pool.browse(cr, uid, partner_id, context=context)
partner_address = partner_address_pool.browse(cr, uid, address_id, context=context)
edi_document['partner_id'] = self.edi_m2o(cr, uid, partner, context=context)
edi_document['partner_order_id'] = self.edi_m2o(cr, uid, partner_address, context=context)
edi_document['partner_invoice_id'] = edi_document['partner_order_id']
edi_document['partner_shipping_id'] = edi_document['partner_order_id']
del edi_document['company_id']
return partner_id
def edi_get_pricelist(self, cr, uid, partner_id, context=None):
# value = ["724f93ec-ddd0-11e0-88ec-701a04e25543:product.list0", "Public Pricelist (EUR)"]
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)
return self.edi_m2o(cr, uid, pricelist, context=context)
def edi_import(self, cr, uid, edi_document, context=None):
if context is None:
context = {}
# import company as a new partner, if type==in then supplier=1, else customer=1
# partner_id field is modified to point to the new partner
# company_address data used to add address to new partner
edi_company_address = edi_document['company_address']
edi_partner_id = edi_document['partner_id']
company_name = edi_document['company_id'][1]
state_id = edi_company_address.get('state_id', False)
state_name = state_id and state_id[1]
country_id = edi_company_address.get('country_id', False)
country_name = country_id and country_id[1]
model = edi_document['__model']
assert model == 'purchase.order', _('Could not import sale order')
edi_document['__model'] = self._name
#import company as a new partner
partner_id = self.edi_import_company(cr, uid, edi_document, context=context)
country_id = country_name and self.edi_import_relation(cr, uid, 'res.country', country_name, context=context) or False
state_id = state_name and self.edi_import_relation(cr, uid, 'res.country.state', state_name,
values={'country_id': country_id, 'code': state_name}, context=context) or False
address_value = {
'street': edi_company_address.get('street', False),
'street2': edi_company_address.get('street2', False),
'zip': edi_company_address.get('zip', False),
'city': edi_company_address.get('city', False),
'state_id': state_id,
'country_id': country_id,
'email': edi_company_address.get('email', False),
'phone': edi_company_address.get('phone', False),
}
partner_value = {'name': company_name}
partner_value.update({'customer': True, 'supplier': False})
partner_id = partner_pool.create(cr, uid, partner_value, context=context)
address_value.update({'partner_id': partner_id})
address_id = partner_address_pool.create(cr, uid, address_value, context=context)
partner_address = partner_address_pool.browse(cr, uid, [address_id], context=context)
partner_address_id = self.edi_o2m(cr, uid, [partner_address], context=context)
partner = partner_pool.browse(cr, uid, partner_id, context=context)
edi_document['partner_id'] = self.edi_m2o(cr, uid, partner, context=context)
edi_document.update({
'partner_invoice_id': partner_address_id,
'partner_order_id': partner_address_id,
'partner_shipping_id': partner_address_id,
#'product_uom_qty': edi_document['order_line'][0]['product_qty'],
'delay': 10,
})
# all fields are converted for sale order import so unnecessary fields are deleted
del edi_document['order_line'][0]['date_planned']
del edi_document['order_line'][0]['product_qty']
del edi_document['date_approve']
del edi_document['validator']
del edi_document['location_id']
del edi_document['partner_address_id']
del edi_document['company_address']
del edi_document['company_id']
del edi_document['warehouse_id']
date_order = edi_document.get('date_order', False)
edi_document['client_order_ref'] = edi_document['name']
edi_document['note'] = edi_document.get('notes', False)
edi_document['pricelist_id'] = self.edi_get_pricelist(cr, uid, partner_id, context=context)
order_lines = edi_document['order_line']
for order_line in order_lines:
order_line['product_uom_qty'] = order_line['product_qty']
date_order = datetime.strptime(date_order, "%Y-%m-%d")
date_planned = datetime.strptime(order_line['date_planned'], "%Y-%m-%d")
order_line['delay'] = (date_planned - date_order).days
return super(sale_order,self).edi_import(cr, uid, edi_document, context=context)
sale_order()

View File

@ -177,6 +177,7 @@
category_id:
- res_partner_category_customers0
name: Cleartrail
opt_out: True
-
I create contact address for Cleartrail.
-

View File

@ -5,7 +5,7 @@
name: Junjun wala
supplier: False
customer: True
opt_out: False
opt_out: True
-
I create one Sale Order
-
@ -35,17 +35,52 @@
I Tesing of EDI functionality. First I export Sale Order from my company than import that Order into customer company
-
!python {model: ir.edi.document}: |
import json
invoice_pool = self.pool.get('sale.order')
orders = invoice_pool.browse(cr, uid, ref("sale_order_test"))
sale_order_pool = self.pool.get('sale.order')
orders = sale_order_pool.browse(cr, uid, ref("sale_order_test"))
tokens = self.export_edi(cr, uid, [orders])
assert tokens, 'Token is not generated'
document = self.get_document(cr, uid, tokens[0])
document = json.loads(document)
document[0]["__model"] = "purchase.order"
document = json.dumps(document)
#a = self.import_edi(cr, uid, edi_document = document)
#assert a, 'Invoice is not imported'
-
I import Sale order from EDI Document of purchase order
-
!python {model: ir.edi.document}: |
sale_order_pool = self.pool.get('sale.order')
edi_document = {
"order_line": [{
"name": "basic pc",
"product_uom": ["5af1272e-dd26-11e0-b65e-701a04e25543:product.product_uom_unit", "PCE"],
"date_planned": "2011-08-31",
"price_unit": 150.0,
"__last_update": False,
"__id": "5af1272e-dd26-11e0-b65e-701a04e25543:purchase.purchase_order_line-AlhsVDZGoKvJ",
"product_qty": 1.0,
"product_id": ["5af1272e-dd26-11e0-b65e-701a04e25543:product.product_product_pc1", "[PC1] Basic PC"]
}],
"partner_address_id": ["5af1272e-dd26-11e0-b65e-701a04e25543:base.res_partner_address_11", "Sebastien LANGE, France, Alencon, 1 place de l'\u00c9glise"],
"__id": "5af1272e-dd26-11e0-b65e-701a04e25543:purchase.purchase_order_test",
"date_order": "2011-09-12",
"partner_id": ["5af1272e-dd26-11e0-b65e-701a04e25543:purchase.res_partner_test20", "jones white"],
"__attachments": [],
"__module": "purchase",
"amount_total": 150.0,
"company_address": {
"phone": "(+32).81.81.37.00",
"street": "Chaussee de Namur 40",
"city": "Gerompont",
"zip": "1367",
"__last_update": False,
"country_id": ["5af1272e-dd26-11e0-b65e-701a04e25543:base.be", "Belgium"],
"__id": "5af1272e-dd26-11e0-b65e-701a04e25543:base.main_address",
},
"amount_untaxed": 150.0,
"name": "PO00011",
"__model": "purchase.order",
"__last_update": False,
"company_id": ["5af1272e-dd26-11e0-b65e-701a04e25543:base.main_company", "OpenERP S.A."],
"__version": [6, 1],
"pricelist_id": ["5af1272e-dd26-11e0-b65e-701a04e25543:product.list0", "Public Pricelist (EUR)"]
}
new_sale_order_id = sale_order_pool.edi_import(cr, uid, edi_document, context=context)
assert new_sale_order_id, 'Sale order is not imported'