Commit 3196de68 authored by Romain Courteaud's avatar Romain Courteaud

creation


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@203 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent f24a436f
## Script (Python) "SalesOrder_importEdi"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=import_file, segmentation_strategique='2003-2004', incoterm='DAF',delivery_mode='Transporteur', order_type='Approvisionnement', travel_duration=0, batch_mode=0,**kw
##title=
##
# import d'un fichier EDI
# format de fichier attendu
# Traduction .rec du fichier Edifact par NY Net
from DateTime import DateTime
# link between NYNet alias table and python import script
edi_import_script_dict = {
# 'NYNet alias table name' : import script
'CAR-ORD':context.SalesOrder_importEdiCarrefour,
'ORDAUCHAAUCHAN':context.SalesOrder_importEdiAuchan,
'ORDAUCHA':context.SalesOrder_importEdiAuchan
}
try:
# read the NY Net file
file_line_list = import_file.readlines()
alias_table_name = string.split(file_line_list[0].split(':',1)[1])[0]
import_script = edi_import_script_dict[alias_table_name]
except:
return None
def setLog(item, comment):
item_comment = item.getComment()
if item.comment != None:
item.setComment(item_comment + comment)
else:
item.setComment(comment)
# create a new sales order
item_module = context.getPortalObject().commande_vente
my_id = str(context.getObject().generateNewId())
context.portal_types.constructContent(
type_name = 'Sales Order',
container = item_module,
id = my_id,
date_reception = DateTime()
)
sales_order = item_module[my_id]
# set some fields
sales_order.setCommandeOrigine('EDI')
#sales_order.setComment('Commentaires générés par l import EDI du fichier: \n')
setLog(sales_order ,'Commentaires générés par l import EDI du fichier ' + import_file.filename + ' :\n')
setLog(sales_order ,'Sales Order ID: ' + my_id + '\n')
sales_order.setSegmentationStrategique(segmentation_strategique)
sales_order.setDeliveryMode(delivery_mode)
sales_order.setIncoterm(incoterm)
sales_order.setOrderType(order_type)
# set the source administration
local_user = container.portal_membership.getAuthenticatedMember()
local_user_name = string.replace(local_user.getUserName(), ' ', '_')
local_persons = sales_order.item_by_title_sql_search(title = local_user_name, portal_type = 'Person')
if len(local_persons) > 0:
sales_order.setSourceAdministration(local_persons[0].relative_url)
# some useful functions ...
def link_with_organisation(code_ean13, link_function, portal_type_name):
result = context.item_by_ean13_code_sql_search(organisation_ean = code_ean13, portal_type = portal_type_name)
try:
if len(result) == 1:
link_function( result[0].relative_url )
else:
raise IndexError
except IndexError:
setLog(sales_order, 'Erreur sur le code EAN d une societe:\n\tCode EAN: ' + code_ean13 + '\n')
def modele_search(code_ean13):
result = context.item_by_ean13_code_sql_search(organisation_ean = code_ean13, portal_type = 'Set Mapped Value')
try:
if len(result) == 1:
result_object = result[0].getObject()
else:
raise IndexError
except IndexError:
result_object = None
else:
return result_object
# dictionnary of those functions, in order to give them to the import script
useful_functions = {
'modele_search':modele_search,
'link_with_organisation':link_with_organisation
}
request = context.REQUEST
# item of products_list: tuple (product_ean13_code, [[quantity, price], ...])
products_list = []
# read each line of the file
for file_line in file_line_list :
# save the line in the comment
#item_module[my_id].setComment(sales_order.getComment()+file_line)
# get the line header
sub_line_list = file_line.split(':',1)
line_header = sub_line_list[0]
# separate the arguments
line_item_list = string.split(sub_line_list[1])
# call the adequat function
try:
import_script(line_header, line_item_list, sales_order, products_list , useful_functions)
except KeyError:
#sales_order.setComment(sales_order.getComment() + 'Erreur sur la lecture d une ligne:\n\t' + file_line )
setLog(sales_order, 'Erreur sur la lecture d une ligne:\n\t' + file_line )
# create a dictionary of the desired resource
# { modele_relative_url : [ ( [ predicate_value_list ] , quantity , price ), (...), ...] , ...}
desired_lines = {}
for product in products_list:
try:
# must be a 'Set Mapped Value'
product_item = modele_search(product[0]).getObject()
except:
setLog(sales_order, 'Erreur sur un modèle ! \n\tCode EAN: ' + product[0] + '\n')
else:
# parent: must be a 'Variated Reference'
father_uid = product_item.getParentUid()
father_obj = context.portal_catalog.getObject(father_uid)
# grand parent: must be a 'Modele'
grand_father_uid = father_obj.getParentUid()
grand_father_obj = context.portal_catalog.getObject(grand_father_uid)
grand_father_url = grand_father_obj.getRelativeUrl()
# calculate the number of piece
total_quantity = 0
price = '0'
for qty in product[1]:
price = qty[1]
total_quantity += string.atoi( qty[0] )
total_quantity = "%i" % total_quantity
if grand_father_url in desired_lines.keys():
# test if the predicateValueList exists
predicate_value_list = product_item.getPredicateValueList()
trouve = 0
for tuple in desired_lines[ grand_father_url ]:
if predicate_value_list == tuple[0]:
setLog(sales_order, 'Erreur sur un modèle: 2 codes EAN represente le meme modele: \n\t ' + grand_father_url + ' ' )
for predicate_value in predicate_value_list:
setLog(sales_order, predicate_value + ' ' )
setLog(sales_order, '\n' )
trouve = 1
if trouve == 0:
# predicate_value_list : variante de la SetMappedValue
desired_lines[ grand_father_url ].append((product_item.getPredicateValueList(), total_quantity , price))
else:
desired_lines[ grand_father_url ] = [ ((product_item.getPredicateValueList(), total_quantity , price )) ]
for modele_relative_url in desired_lines.keys():
# compute variation_base_category_list and variation_category_list for this line
line_variation_base_category_dict = {}
line_variation_category_list = []
for my_tuple in desired_lines[ modele_relative_url ] :
for variation_item in my_tuple[0] :
if not variation_item in line_variation_category_list :
line_variation_category_list.append(variation_item)
variation_base_category_items = variation_item.split('/')
if len(variation_base_category_items) > 0 :
line_variation_base_category_dict[variation_base_category_items[0]] = 1
line_variation_base_category_list = line_variation_base_category_dict.keys()
# construct new sales order lines
sales_order_line_id = str(sales_order.generateNewId())
# sur le folder, newContent
sales_order_line = sales_order.newContent(
portal_type = "Sales Order Line",
resource = modele_relative_url,
id = sales_order_line_id,
comment = ''
)
sales_order_line.setResource(modele_relative_url)
sales_order_line.setVariationBaseCategoryList(line_variation_base_category_list)
sales_order_line.setVariationCategoryList(line_variation_category_list)
#sales_order_line_cell_list = sales_order_line.contentValues()
sales_order_line_cell_list = sales_order_line.objectValues()
for my_tuple in desired_lines[ modele_relative_url ] :
quantity_updated = 0
for sales_order_line_cell in sales_order_line_cell_list :
if sales_order_line_cell.test(context.asContext(categories=my_tuple[0])):
sales_order_line_cell.setTargetQuantity(my_tuple[1])
sales_order_line_cell.setPrice(my_tuple[2])
sales_order_line_cell.flushActivity(invoke=1)
quantity_updated = 1
break
# if no cell according to variation_category_list was found
# or no variation at all, we update the container_line
if not quantity_updated :
sales_order_line.setTargetQuantity(my_tuple[1])
sales_order_line.flushActivity(invoke=1)
# set the target start date
if sales_order.getTargetStartDate() == None:
try:
sales_order.setTargetStartDate( sales_order.getTargetStopDate() - travel_duration )
except TypeError:
None
setLog(sales_order, 'Fin des commentaires générés par l import EDI.\n')
sales_order.flushActivity(invoke=1)
# try to apply a sale condition
sales_order.sales_order_apply_condition(my_id, 1)
# and this is the end ....
if batch_mode:
return sales_order.getComment()
else:
redirect_url = '%s?%s' % ( item_module.absolute_url()+'/'+my_id+'/'+'view', 'portal_status_message=Commande+Vente+créée.')
request[ 'RESPONSE' ].redirect( redirect_url )
## Script (Python) "SalesOrder_importEdiAuchan"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=line_header, argument, item_sales_order, products_list, useful_functions, **kw
##title=
##
# import d'un fichier EDI Auchan
# format de fichier attendu
# Traduction .rec du fichier Edifact par NY Net
def read_START(argument, item_sales_order, products_list ):
item_sales_order.setGroup('Auchan International/Auchan France')
"""
# recuperation du client et du destinataire
functions = (
(argument[2], item_sales_order.setSourceSection),
(argument[3], item_sales_order.setDestinationDecision)
)
for tuple in functions:
useful_functions['link_with_organisation'](tuple[0], tuple[1], 'Organisation')
"""
def read_ALC_DEDUCTION_FRAIS(argument, item_sales_order, products_list ):
None
def read_BGM_TYPE_DOCUMENT(argument, item_sales_order, products_list ):
# nothing interesting to get
None
def read_BGM_NUMERO_DOCUMENT(argument, item_sales_order, products_list ):
item_sales_order.setDestinationReference(argument[0])
def read_CNT_TOTAL_CONTROLE(argument, item_sales_order, products_list ):
None
def read_CUX_MONNAIE(argument, item_sales_order, products_list ):
# nothing interesting to get
item_sales_order.setPriceCurrency('devise/'+'EUR')
None
def read_DTM_DATE_COMMANDE(argument, item_sales_order, products_list ):
# nothing interesting to get
None
def read_DTM_DATE_DEPART(argument, item_sales_order, products_list ):
date = argument[1][:4] + '/' + argument[1][4:6] + '/' + argument[1][6:8]
item_sales_order.setTargetStopDate(date)
def read_DTM_DATE_LIVRAISON(argument, item_sales_order, products_list ):
date = argument[1][:4] + '/' + argument[1][4:6] + '/' + argument[1][6:8]
item_sales_order.setTargetStopDate(date)
def read_DTM_DATE_REFERENCE(argument, item_sales_order, products_list ):
None
def read_LIN_ARTICLE(argument, item_sales_order, products_list ):
# get ean13 code
products_list.append( (argument[0], []) )
def read_LOC_EMPLACEMENT(argument, item_sales_order, products_list ):
None
def read_MEA_MESURES(argument, item_sales_order, products_list ):
None
def read_MOA_MONTANT_REMISE(argument, item_sales_order, products_list ):
None
def read_NAD_ACHETEUR(argument, item_sales_order, products_list ):
#useful_functions['link_with_organisation'](argument[0], item_sales_order.setDestinationPayment , 'Organisation')
useful_functions['link_with_organisation'](argument[0], item_sales_order.setDestinationDecision, 'Organisation')
def read_NAD_FOURNISSEUR(argument, item_sales_order, products_list ):
useful_functions['link_with_organisation'](argument[0], item_sales_order.setSourceSection, 'Organisation')
def read_NAD_LIEU_LIVRAISON(argument, item_sales_order, products_list ):
# code EAN inconnu de Coramy, solution temporaire ...
item_sales_order.setDestination(item_sales_order.getDestinationDecision())
#useful_functions['link_with_organisation'](argument[0], item_sales_order.setDestination, 'Organisation')
def read_PAC_EMBALLAGE(argument, item_sales_order, products_list ):
None
def read_PCD_POURCENTAGE(argument, item_sales_order, products_list ):
None
def read_PIA_COMPLEMENT_PRODUI(argument, item_sales_order, products_list ):
None
def read_PRI_PRIX_UNIT_NET(argument, item_sales_order, products_list ):
list_tuple = products_list[ len(products_list) - 1 ][1]
list_tuple[ len(list_tuple) - 1 ][1] = argument[0]
def read_QTY_PAR_COMBIEN(argument, item_sales_order, products_list ):
None
def read_QTY_QUANTITE_CDE(argument, item_sales_order, products_list ):
#products_list[ len(products_list) - 1 ][1].append( (argument[0],) )
products_list[ len(products_list) - 1 ][1].append( [argument[0],None] )
def read_QTY_QUANTITE_GRATUITE(argument, item_sales_order, products_list ):
None
def read_RFF_REFERENCE_OPERATI(argument, item_sales_order, products_list ):
None
def read_TDT_TRANSPORT(argument, item_sales_order, products_list ):
None
def read_TOD_CONDITION_LIVRAIS(argument, item_sales_order, products_list ):
None
def read_UNH_ENTETE_COMMANDE(argument, item_sales_order, products_list ):
# nothing interesting to get
None
def read_UNS_SECTION_MESSAGE(argument, item_sales_order, products_list ):
None
# dictionary of the functions
functions_list = {
'START':read_START,
'ALC_DEDUCTION_FRAIS__':read_ALC_DEDUCTION_FRAIS,
'BGM_TYPE_DOCUMENT____':read_BGM_TYPE_DOCUMENT,
'BGM_NUMERO_DOCUMENT__':read_BGM_NUMERO_DOCUMENT,
'CNT_TOTAL_CONTROLE___':read_CNT_TOTAL_CONTROLE,
'CUX_MONNAIE__________':read_CUX_MONNAIE,
'DTM_DATE_COMMANDE____':read_DTM_DATE_COMMANDE,
'DTM_DATE_DEPART______':read_DTM_DATE_DEPART,
'DTM_DATE_LIVRAISON___':read_DTM_DATE_LIVRAISON,
'DTM_DATE_REFERENCE___':read_DTM_DATE_REFERENCE,
'LIN_ARTICLE__________':read_LIN_ARTICLE,
'LOC_EMPLACEMENT______':read_LOC_EMPLACEMENT,
'MEA_MESURES__________':read_MEA_MESURES,
'MOA_MONTANT_REMISE___':read_MOA_MONTANT_REMISE,
'NAD_ACHETEUR_________':read_NAD_ACHETEUR,
'NAD_FOURNISSEUR______':read_NAD_FOURNISSEUR,
'NAD_LIEU_LIVRAISON___':read_NAD_LIEU_LIVRAISON,
'PAC_EMBALLAGE________':read_PAC_EMBALLAGE,
'PCD_POURCENTAGE______':read_PCD_POURCENTAGE,
'PIA_COMPLEMENT_PRODUI':read_PIA_COMPLEMENT_PRODUI,
'PRI_PRIX_UNIT_NET____':read_PRI_PRIX_UNIT_NET,
'QTY_PAR_COMBIEN______':read_QTY_PAR_COMBIEN,
'QTY_QUANTITE_CDE_____':read_QTY_QUANTITE_CDE,
'QTY_QUANTITE_GRATUITE':read_QTY_QUANTITE_GRATUITE,
'RFF_REFERENCE_OPERATI':read_RFF_REFERENCE_OPERATI,
'TDT_TRANSPORT________':read_TDT_TRANSPORT,
'TOD_CONDITION_LIVRAIS':read_TOD_CONDITION_LIVRAIS,
'UNH_ENTETE_COMMANDE__':read_UNH_ENTETE_COMMANDE,
'UNS_SECTION_MESSAGE__':read_UNS_SECTION_MESSAGE
}
# execute a function
functions_list[line_header](argument, item_sales_order, products_list )
## Script (Python) "SalesOrder_importEdiCarrefour"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=line_header, argument, item_sales_order, products_list, useful_functions, **kw
##title=
##
# import d'un fichier EDI Carrefour
# format de fichier attendu
# Traduction .rec du fichier Edifact par NY Net
def read_START(argument, item_sales_order, products_list ):
item_sales_order.setGroup('Carrefour International/Carrefour France')
# recuperation du client et du destinataire
functions = (
(argument[2], item_sales_order.setSourceSection),
(argument[3], item_sales_order.setDestinationDecision)
)
for tuple in functions:
useful_functions['link_with_organisation'](tuple[0], tuple[1], 'Organisation')
def read_UNH(argument, item_sales_order, products_list ):
None
def read_BGM(argument, item_sales_order, products_list ):
item_sales_order.setDestinationReference(argument[1])
def read_DTM(argument, item_sales_order, products_list ):
date = argument[1][:4] + '/' + argument[1][4:6] + '/' + argument[1][6:8]
case = {
'2':item_sales_order.setTargetStopDate,
# date livraison demandee
'137':None,
# date du document
'200':None
# date enlevement cargaison
}
if case[argument[0]] != None:
case[argument[0]](date)
def read_NAD(argument, item_sales_order, products_list ):
case = {
#'BY':item_sales_order.setDestinationPayment ,
'BY':item_sales_order.setDestinationDecision,
# acheteur
'SU':item_sales_order.setSourceSection ,
# fournisseur
# source_section
'DP':item_sales_order.setDestination
# intervenant a livrer
# destination
}
useful_functions['link_with_organisation'](argument[1], case[argument[0]], 'Organisation')
def read_CUX(argument, item_sales_order, products_list ):
if argument[2] == '9':
item_sales_order.setPriceCurrency('devise/'+argument[1])
def read_TDT(argument, item_sales_order, products_list ):
#item_sales_order.setDeliveryMode(.....)
# nothing interesting to get
None
def read_LIN(argument, item_sales_order, products_list ):
# get ean13 code
products_list.append( (argument[0], []) )
None
def read_IMD(argument, item_sales_order, products_list ):
# nothing interesting to get
None
def read_QTY(argument, item_sales_order, products_list ):
products_list[ len(products_list) - 1 ][1].append( [argument[1], None] )
def read_PRI(argument, item_sales_order, products_list ):
list_tuple = products_list[ len(products_list) - 1 ][1]
list_tuple[ len(list_tuple) - 1 ][1] = argument[0]
def read_UNS(argument, item_sales_order, products_list ):
None
# dictionary of the functions
functions_list = {
'START':read_START,
'UNH':read_UNH,
'BGM':read_BGM,
'DTM':read_DTM,
'NAD':read_NAD,
'CUX':read_CUX,
'TDT':read_TDT,
'LIN':read_LIN,
'IMD':read_IMD,
'QTY':read_QTY,
'PRI':read_PRI,
'UNS':read_UNS
}
# execute a function
functions_list[line_header](argument, item_sales_order, products_list )
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment