Advertisement
waflessnet

get id_product and id_product_atribute PS 1.5 with reference

Aug 28th, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. #!/usr/local/bin/python
  2. import csv
  3. import re
  4. import os
  5. import MySQLdb as mdb
  6. class Stock:
  7.     def __init__(self,orden_ref,fileA):
  8.         self.conex('10.xxx.xxx.xxx','usuario','password','basededatos')
  9.         self.fileA=fileA
  10.         self.orden_ref=orden_ref
  11.        
  12.     def getFileCsv(self):
  13.        
  14.         carpeta=os.path.dirname(__file__)
  15.         carpeta= str(carpeta) +'/'+str(self.fileA)
  16.         reader = csv.reader(open(carpeta,'rb'),delimiter=',')
  17.         for index,row in enumerate(reader):
  18.             if (row[0] or row[0].strip() !='') and (row[1] or row[1].strip() !='') and (row[2] or row[2].strip() !=''):
  19.                 precio=re.sub("\D","",row[1])
  20.                 stock=re.sub("\D","",row[2])
  21.                 articulo=row[0]
  22.                 _product=self.getProductID(articulo)
  23.                 if _product['id_product']:
  24.                     linea='"'+str(self.orden_ref)+'";'+str(_product['id_product'])+';'+str(_product['id_attr'])+';'+str(precio)+';'+str(stock)+";;;;;\n"
  25.                     self.createFile(linea)
  26.                     print ' INDEX:'+str(index)+' Articulo:'+str(articulo)+' Precio :'+str(precio)+' Stock:'+str(stock)+' idProducto:'+str(_product['id_product'])+' Attr: '+str(_product['id_attr'])
  27.             else:
  28.                 print 'Linea:'+str(index+1)+' Tiene inconsistencia'    
  29.    
  30.    
  31.     #def getDetailsProduct(self,referece):
  32.        
  33.     def insertDetailSupply(self):
  34.         print 'era por si no podia corregir /controller/admin/AdminImportControler.php'
  35.        
  36.     def createFile(self,linea):
  37.         filename="PS_"+str(self.fileA)
  38.         try:
  39.             if os.path.exists(filename):
  40.                 file_= open(filename,"a")
  41.                 file_.write(linea)
  42.                 file_.close()
  43.             else:
  44.                 file_= open(filename,"w")
  45.                 file_.write(linea)
  46.                 file_.close()
  47.                
  48.         except IOError:
  49.             print 'Error IO'
  50.                
  51.     def conex(self,host,user,passw,db):
  52.         try:
  53.             con=mdb.connect(host,user,passw,db)
  54.             self.cur =con.cursor()
  55.         except mdb.Error, e:
  56.             print "Error %d: %s" % (e.args[0],e.args[1])
  57.             sys.exit(1)
  58.  
  59.     def Qexec(self,sql):
  60.         try:
  61.             self.cur.execute(sql)
  62.             resp=self.cur.fetchall()
  63.         except Exception, e:
  64.             resp=None
  65.         return resp
  66.     def getProductID(self,ref):
  67.         sql="""
  68.        SELECT pr.id_product,pr_a.id_product_attribute
  69.        FROM ps_product pr
  70.        LEFT JOIN ps_product_attribute pr_a ON (pr.id_product=pr_a.id_product)
  71.        WHERE (pr.reference is not null or pr_a.reference is not null)
  72.        AND pr.reference=%s
  73.        """%ref
  74.         idr=self.Qexec(sql)
  75.         if idr:
  76.             if idr[0][1]:
  77.                 return {'id_product':int(idr[0][0]),'id_attr':int(idr[0][1])}
  78.             else:
  79.                 return {'id_product':int(idr[0][0]),'id_attr':''}
  80.         else :
  81.             return {'id_product':'','id_attr':''}
  82.          
  83.     def getTabPs_Product(self):
  84.         sql="select * from ps_product"
  85.         print self.Qexec(sql)
  86.    
  87. st=Stock('Ref_0007','archivoconreferencia.csv')
  88. st.getFileCsv()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement