def create_invoice(auth_key,invoice_type,customer,serial_number,dattime,_type='POST'): login =[] loginObj = {} if len(auth_key[1:-1])<=0 or len(dattime[1:-1])<=0 or len(customer[1:-1])<=0 or len(serial_number[1:-1])<=0 : loginObj['status']='401' loginObj['error']='Incomplete data to generate Sales Invoice , Please provide token no , Datetime,customer and serial no' return loginObj qr="select name from `tabauth keys` where auth_key="+auth_key res=webnotes.conn.sql(qr) rgn=webnotes.conn.sql("select region from `tabFranchise` where contact_email='"+res[0][0]+"'") zz=serial_number[1:-1].count(' ') xx=serial_number[1:-1].replace('[','').split(' ') #xxx=xx.replace(' ','\n') yy="select item_code,item_name,description from `tabSerial No` where name='"+cstr(xx[0])+"'" zzz=webnotes.conn.sql(yy) if not zzz: loginObj['status']='402' loginObj['error']='Invalid serails no, please try againg' return loginObj pp="select ref_rate from `tabItem Price` where price_list='Standard Selling' and item_code='"+zzz[0][0]+"'" ppp=webnotes.conn.sql(pp) if res: from webnotes.model.doc import Document d = Document('Sales Invoice') d.customer=customer[1:-1] d.customer_name=customer[1:-1] d.region=rgn[0][0] d.posting_date=dattime[1:-1] d.due_date=dattime[1:-1] d.selling_price_list='Standard Selling' d.currency='INR' d.territory='India' if ppp: d.net_total_export=ppp[0][0]*zz d.grand_total_export=ppp[0][0]*zz d.rounded_total_export=ppp[0][0]*zz d.outstanding_amount=ppp[0][0]*zz d.plc_conversion_rate=1 from webnotes.utils import nowdate from accounts.utils import get_fiscal_year today = nowdate() d.fiscal_year=get_fiscal_year(today)[0] d.debit_to=customer[1:-1]+" - P" d.is_pos=1 d.cash_bank_account='Cash - P' d.save() webnotes.conn.commit() e=Document('Sales Invoice Item') e.item_code=zzz[0][0] e.item_name=zzz[0][1] e.description=zzz[0][2] e.qty=zz e.stock_uom='Nos' if ppp: e.ref_rate=ppp[0][0] e.export_rate=ppp[0][0] e.export_amount='0' e.income_account='Sales - P' e.cost_center='Main - P' e.serial_no_=serial_number[1:-1].replace('[','').replace(']','') e.parent=d.name e.parenttype='Sales Invoice' e.parentfield='entries' e.save() webnotes.conn.commit() key={} key['invoice_id_id']=d.name login.append(key) loginObj['status']='200' loginObj['invoice']=login return loginObj else: loginObj['status']='401' loginObj['error']='invalid token please contact administrator' return loginObj
def create_in(data,_type='POST'): from webnotes.model.doc import Document from webnotes.utils import nowdate,add_months data1=json.loads(data) auth_key=data1['token'] invoice_type=data1['invoice_type'] customer=data1['customer'] dattime=data1['datetime'] item_details=data1['item_details'] login =[] loginObj = {} amt=0 qt=0 srl='' cc='' zz='' #return data if len(auth_key[1:-1]) <=0 or len(dattime[1:-1]) <=0 or len(customer[1:-1])<=0 : loginObj['status']='401' loginObj['error']='Incomplete data to generate Sales Invoice , Please provide token no , Datetime,customer and serial no' return loginObj for ff in item_details: a=ff['barcode'] c="select item_code,warehouse from `tabSerial No` where name='"+a[:-1]+"'" cc=webnotes.conn.sql(c) #webnotes.errprint(c) #webnotes.errprint(cc) amt=cint(amt)+cint(ff['rate']) qt+=1 srl+=a[:-2].replace(',','\n') #return cc if cc : qr="select name from `tabauth keys` where auth_key='"+auth_key+"'" #return qr res=webnotes.conn.sql(qr) if res : rr="select region from `tabFranchise` where contact_email='"+res[0][0]+"'" r1=webnotes.conn.sql(rr) if r1[0][0]==cc[0][1] : #webnotes.errprint("same") pass else: #webnotes.errprint("different warehouse") pass zz+="'"+a[:-1].replace(',','')+"'," else: if invoice_type=='CUSTOMER': l = Document('Customer Details',customer) l.customer_name=customer l.save() webnotes.conn.commit() for ll in item_details: m = Document('Customer Data') m.serial_no=a[:-1] m.parent=l.name m.parenttype='Customer Details' m.parentfield='customer_data' m.save(new=1) webnotes.conn.commit() qrt="select name from `tabauth keys` where auth_key='"+auth_key+"'" res=webnotes.conn.sql(qrt) net=0 if res: a="select name from tabCustomer where name='"+customer+"'" b=webnotes.conn.sql(a) if b: a='' else: d = Document('Customer') d.customer_name=customer d.name=customer d.save(new=1) webnotes.conn.commit() qr="select name from `tabauth keys` where auth_key='"+auth_key+"'" #webnotes.errprint(qr) res=webnotes.conn.sql(qr) rgn='' if res : rr="select region from `tabFranchise` where contact_email='"+res[0][0]+"'" #webnotes.errprint(qr) r1=webnotes.conn.sql(rr,as_list=1) #webnotes.errprint(r1[0][0]) d = Document('Sales Invoice') d.customer=customer d.customer_name=customer d.posting_date=dattime[1:-1] d.due_date=dattime[1:-1] d.remarks='Invalid QR code' d.selling_price_list='Standard Selling' d.currency='INR' if r1: d.territory=r1[0][0] d.region=r1[0][0] d.net_total_export=0 d.grand_total_export=0 d.rounded_total_export=0 d.plc_conversion_rate=1 from webnotes.utils import nowdate from accounts.utils import get_fiscal_year today = nowdate() d.fiscal_year=get_fiscal_year(today)[0] d.debit_to=customer[1:-1]+" - P" d.is_pos=1 d.cash_bank_account='Cash - P' d.docstatus=1 d.save(new=1) webnotes.conn.commit() e=Document('Sales Invoice Item') e.description="Invalid QR code" e.qty='1' e.stock_uom='Nos' e.serial_no_=a[:-1] e.parent=d.name e.parenttype='Sales Invoice' e.parentfield='entries' e.save(new=1) webnotes.conn.commit() key={} key['invoice_id']=d.name login.append(key) loginObj['status']='200' loginObj['invoice']=login return loginObj #loginObj['status']='400' #loginObj['error']='invalid serial no (QR Code) please contact administrator' #return loginObj z=zz[:-1] u="select distinct item_code from `tabSerial No` where name in ("+z+")" s=webnotes.conn.sql(u,as_list=1) qty_dict={} rate_dict={} srl_dict={} for key in s: qty_dict.setdefault(key[0],0) rate_dict.setdefault(key[0],0) srl_dict.setdefault(key[0],'') for sss in item_details: a=sss['barcode'] c="select item_code from `tabSerial No` where name='"+a[:-1]+"'" cc=webnotes.conn.sql(c) d=cc and cc[0][0] or '' qty_dict[d]=cint(qty_dict[d])+1 rate_dict[d]=cint(rate_dict[d])+cint(sss['rate']) srl_dict[d]=srl_dict[d]+a[:-1]+"\n" if invoice_type=='CUSTOMER': #a="select name from `tabCustomer Details`where name like '"+customer+"-%'" #b=webnotes.conn.sql(a) #webnotes.errprint(a) #if b: #a='' #else: #webnotes.errprint("else "+a) l = Document('Customer Details',customer) l.customer_name=customer l.save() webnotes.conn.commit() for ll in item_details: m = Document('Customer Data') #.serial_no=ll['barcode'] #webnotes.errprint(a[:-1]) m.serial_no=a[:-1] #m.start_date=nowdate() m.parent=l.name m.parenttype='Customer Details' m.parentfield='customer_data' m.save(new=1) #webnotes.errprint("before g1") g1="update `tabSerial No` set warehouse='',delivery_date=CURDATE(),status='Delivered',customer='"+customer+"' where name='"+cstr(ll['barcode']).replace(',','')+"'" webnotes.conn.sql(g1) webnotes.conn.commit() else: for gg in item_details: g=gg['barcode'] #webnotes.errprint("in else gg") #webnotes.errprint(g[:-2]) g1="update `tabSerial No` set warehouse='',delivery_date=CURDATE(),status='Delivered',customer='"+customer+"' where name='"+g[:-1]+"'" webnotes.conn.sql(g1) webnotes.conn.commit() for hh in item_details: h=hh['barcode'] g=h[:-2] i="select item_code from `tabSerial No` where name='"+g+"'" ii=webnotes.conn.sql(i) qrt="select name from `tabauth keys` where auth_key='"+auth_key+"'" res=webnotes.conn.sql(qrt) net=0 if res: a="select name from tabCustomer where name='"+customer+"'" #webnotes.errprint(a) b=webnotes.conn.sql(a) #webnotes.errprint(b) if b: a='' else: #webnotes.errprint("else") d = Document('Customer') d.customer_name=customer d.name=customer d.save(new=1) webnotes.conn.commit() d = Document('Sales Invoice') d.customer=customer d.customer_name=customer d.posting_date=dattime[1:-1] d.due_date=dattime[1:-1] d.selling_price_list='Standard Selling' d.currency='INR' d.territory=r1[0][0] d.net_total_export=0 d.grand_total_export=0 d.rounded_total_export=0 d.plc_conversion_rate=1 d.region=r1[0][0] from webnotes.utils import nowdate from accounts.utils import get_fiscal_year today = nowdate() d.fiscal_year=get_fiscal_year(today)[0] d.debit_to=customer[1:-1]+" - P" d.is_pos=1 d.cash_bank_account='Cash - P' d.save(new=1) webnotes.conn.commit() for x in qty_dict.keys(): e=Document('Sales Invoice Item') e.item_code=x e.item_name=x e.description=x e.qty=qty_dict[x] e.stock_uom='Nos' e.ref_rate=cint(rate_dict[x])/cint(qty_dict[x]) e.export_rate=cint(rate_dict[x])/cint(qty_dict[x]) e.export_amount=cint(rate_dict[x]) e.income_account='Sales - P' e.cost_center='Main - P' e.serial_no_=srl_dict[x] e.parent=d.name e.parenttype='Sales Invoice' e.parentfield='entries' e.save(new=1) net+=cint(rate_dict[x]) qrs="select warranty_period from tabItem where name='"+x+"'" #webnotes.errprint(qrs) ress=webnotes.conn.sql(qrs) #webnotes.errprint(ress[0][0]) if ress: exdt=add_months(cstr(nowdate()),cint(ress[0][0])) else: exdt='null' #webnotes.errprint(cstr(srl_dict[x])) #webnotes.errprint(g) g2="update `tabSerial No` set warehouse='', warranty_expiry_date='"+cstr(exdt)+"',delivery_date=CURDATE(),status='Delivered',customer='"+customer+"' where name='"+cstr(h[:-1])+"'" #webnotes.errprint(g2) #webnotes.errprint("in x in qty dict") webnotes.conn.sql(g2) webnotes.conn.commit() d = Document('Sales Invoice',d.name) d.net_total_export=net d.net_total=net d.grand_total_export=net d.rounded_total_export=net d.outstanding_amount=net d.docstatus=1 d.save() webnotes.conn.commit() key={} key['invoice_id']=d.name login.append(key) loginObj['status']='200' loginObj['invoice']=login return loginObj else: loginObj['status']='401' loginObj['error']='invalid token please contact administrator' return loginObj