Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/python
- import csv
- import re
- import os
- import MySQLdb as mdb
- class Stock:
- def __init__(self,orden_ref,fileA):
- self.conex('10.xxx.xxx.xxx','usuario','password','basededatos')
- self.fileA=fileA
- self.orden_ref=orden_ref
- def getFileCsv(self):
- carpeta=os.path.dirname(__file__)
- carpeta= str(carpeta) +'/'+str(self.fileA)
- reader = csv.reader(open(carpeta,'rb'),delimiter=',')
- for index,row in enumerate(reader):
- if (row[0] or row[0].strip() !='') and (row[1] or row[1].strip() !='') and (row[2] or row[2].strip() !=''):
- precio=re.sub("\D","",row[1])
- stock=re.sub("\D","",row[2])
- articulo=row[0]
- _product=self.getProductID(articulo)
- if _product['id_product']:
- linea='"'+str(self.orden_ref)+'";'+str(_product['id_product'])+';'+str(_product['id_attr'])+';'+str(precio)+';'+str(stock)+";;;;;\n"
- self.createFile(linea)
- print ' INDEX:'+str(index)+' Articulo:'+str(articulo)+' Precio :'+str(precio)+' Stock:'+str(stock)+' idProducto:'+str(_product['id_product'])+' Attr: '+str(_product['id_attr'])
- else:
- print 'Linea:'+str(index+1)+' Tiene inconsistencia'
- #def getDetailsProduct(self,referece):
- def insertDetailSupply(self):
- print 'era por si no podia corregir /controller/admin/AdminImportControler.php'
- def createFile(self,linea):
- filename="PS_"+str(self.fileA)
- try:
- if os.path.exists(filename):
- file_= open(filename,"a")
- file_.write(linea)
- file_.close()
- else:
- file_= open(filename,"w")
- file_.write(linea)
- file_.close()
- except IOError:
- print 'Error IO'
- def conex(self,host,user,passw,db):
- try:
- con=mdb.connect(host,user,passw,db)
- self.cur =con.cursor()
- except mdb.Error, e:
- print "Error %d: %s" % (e.args[0],e.args[1])
- sys.exit(1)
- def Qexec(self,sql):
- try:
- self.cur.execute(sql)
- resp=self.cur.fetchall()
- except Exception, e:
- resp=None
- return resp
- def getProductID(self,ref):
- sql="""
- SELECT pr.id_product,pr_a.id_product_attribute
- FROM ps_product pr
- LEFT JOIN ps_product_attribute pr_a ON (pr.id_product=pr_a.id_product)
- WHERE (pr.reference is not null or pr_a.reference is not null)
- AND pr.reference=%s
- """%ref
- idr=self.Qexec(sql)
- if idr:
- if idr[0][1]:
- return {'id_product':int(idr[0][0]),'id_attr':int(idr[0][1])}
- else:
- return {'id_product':int(idr[0][0]),'id_attr':''}
- else :
- return {'id_product':'','id_attr':''}
- def getTabPs_Product(self):
- sql="select * from ps_product"
- print self.Qexec(sql)
- st=Stock('Ref_0007','archivoconreferencia.csv')
- st.getFileCsv()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement