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 make_po(self,args): net_tot=0.00 po=Document('Purchase Order') po.transaction_date=nowdate() po.price_list_currency='INR' po.currency='INR' po.buying_price_list='Standard Buying' po.customer_detail=webnotes.conn.get_value("Customer",{"innoworth_id":(args['Customer Id']).strip()},"name") po.customer_name=webnotes.conn.get_value("Customer",{"innoworth_id":(args['Customer Id']).strip()},"customer_name") if (args['Territory']).strip(): supplier=sql("select name from tabSupplier where territory='"+(args['Territory']).strip()+"'",as_list=1) if len(supplier)>=1: po.supplier=supplier[0][0] po.status='Draft' po.company='InnoWorth' po.conversion_rate=1.00 po.fiscal_year=webnotes.conn.get_value("Global Defaults", None, "current_fiscal_year") po.customer_address_data=webnotes.conn.get_value("Address",{"customer":po.customer_detail},"name") po.customer_address=get_address_display(po.customer_address_data) po.docstatus=0 po.plc_conversion_rate=1 po.save() for data_c in args['Child']: if (data_c['Item Name']).strip() and (data_c['Qty']).strip(): pre_doc=webnotes.conn.sql("select a.parent as parent,ifnull(a.ordered_qty,0) as ordered_qty,a.name as name,a.qty as qty from `tabMaterial Request Item` a ,`tabMaterial Request` b where a.item_code='"+(data_c['Item Name']).strip()+"' and a.parent=b.name and b.docstatus=1 and a.qty<>ifnull(a.ordered_qty,0) and a.qty<>0",as_list=1) item_details=webnotes.conn.sql("select default_warehouse,item_name,stock_uom,description from tabItem where name='"+(data_c['Item Name']).strip()+"'",as_list=1) order_qty=cstr((data_c['Qty']).strip()) i=0 for s in pre_doc: pi_qty=webnotes.conn.sql("select ifnull(sum(qty),0) from `tabPurchase Order Item` where prevdoc_docname='"+pre_doc[i][0]+"' and prevdoc_detail_docname='"+pre_doc[i][2]+"' and docstatus=0",as_list=1) if pi_qty: available_qty=cstr(cint(pre_doc[i][3])-cint(pre_doc[i][1])-cint(pi_qty[0][0])) else: available_qty=cstr(cint(pre_doc[i][3])-cint(pre_doc[i][1])) if cint(available_qty) >= cint(order_qty): child_data=[] child_data.append([(data_c['Item Name']).strip(),order_qty,(args['Required Date']).strip(),pre_doc[i][0],item_details[0][0],item_details[0][1],item_details[0][2],item_details[0][3],pre_doc[i][2],po.name]) value1=self.create_child_po(child_data) net_tot=cstr(flt(net_tot)+flt(value1)) order_qty=0 break; else: child_data=[] if cint(available_qty)>0: child_data.append([(data_c['Item Name']).strip(),available_qty,(args['Required Date']).strip(),pre_doc[i][0],item_details[0][0],item_details[0][1],item_details[0][2],item_details[0][3],pre_doc[i][2],po.name]) value2=self.create_child_po(child_data) net_tot=cstr(flt(net_tot)+flt(value2)) order_qty=cint(order_qty)-cint(available_qty) i=i+1 if cint(order_qty)!=0: child_data=[] child_data.append([(data_c['Item Name']).strip(),order_qty,(args['Required Date']).strip(),item_details[0][0],item_details[0][1],item_details[0][2],item_details[0][3],po.name]) value3=self.create_material_request(child_data) net_tot=cstr(flt(net_tot)+flt(value3)) po_=Document('Purchase Order',po.name) po_.net_total_import=net_tot po_.grand_total_import=net_tot po_.rounded_total=net_tot po_.save() return {"Purchase Order":po.name}
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
def make_so(self,args): # coding: utf-8 net_tot=0.00 so=Document('Sales Order') so.transaction_date=nowdate() so.price_list_currency='INR' so.currency='INR' so.selling_price_list='Standard Selling' so.customer=webnotes.conn.get_value("Customer",{"innoworth_id":(args['Customer Id']).strip()},"name") so.customer_name=webnotes.conn.get_value("Customer",{"innoworth_id":(args['Customer Id']).strip()},"customer_name") so.delivery_date=(args['Required Date']).strip() so.company='InnoWorth' so.conversion_rate=1.00 so.customer_group='Individual' if (args['Territory']).strip(): so.territory=(args['Territory']).strip() so.charge=webnotes.conn.get_value("Applicable Territory",{"territory":so.territory},"parent") so.fiscal_year=webnotes.conn.get_value("Global Defaults", None, "current_fiscal_year") so.customer_address=webnotes.conn.get_value("Address",{"customer":so.customer},"name") so.address_display=get_address_display(so.customer_address) so.contact_person=webnotes.conn.get_value("Contact",{"customer":so.customer},"name") so.docstatus=1 so.status='Submitted' so.plc_conversion_rate=1 so.save() html="" sr=0 item_list=[] for j in args['Child']: sr=sr+1 soi=Document('Sales Order Item') soi.item_code=(j['Item Name']).strip() soi.qty=(j['Qty']).strip() item_details=webnotes.conn.sql("select default_warehouse,item_name,stock_uom,description from tabItem where name='"+(j['Item Name']).strip()+"'",as_list=1) soi.reserved_warehouse=item_details[0][0] soi.item_name=item_details[0][1] soi.stock_uom=item_details[0][2] soi.description=item_details[0][3] rate=webnotes.conn.sql("select ref_rate from `tabItem Price` where price_list='Standard Selling' and item_code='"+(j['Item Name']).strip()+"'",as_list=1) if rate: soi.ref_rate=rate[0][0] soi.export_rate=rate[0][0] else: soi.ref_rate=1 soi.export_rate=1 soi.export_amount=cstr(flt(soi.ref_rate)*flt((j['Qty']).strip())) item_list.append({"item_code":soi.item_code,"export_amt":soi.export_amount}) net_tot=cstr(flt(net_tot)+flt(soi.export_amount)) soi.parentfield='sales_order_details' soi.parenttype='Sales Order' soi.docstatus=1 soi.parent=so.name soi.save(new=1) html+=("<tr><td style='border:1px solid rgb(153, 153, 153);word-wrap: break-word;'>"+cstr(sr)+"</td><td style='border:1px solid rgb(153, 153, 153);word-wrap: break-word;'>"+cstr(soi.item_code)+"</td><td style='border:1px solid rgb(153, 153, 153);word-wrap: break-word;'>"+cstr(soi.description)+"</td><td style='border:1px solid rgb(153, 153, 153);word-wrap: break-word;text-align:right;'><div>"+cstr(soi.qty)+"</div></td><td style='border:1px solid rgb(153, 153, 153);word-wrap: break-word;'>"+cstr(soi.stock_uom)+"</td><td style='border:1px solid rgb(153, 153, 153);word-wrap: break-word;'><div style='text-align:right'>₹ "+cstr(soi.ref_rate)+"</div></td><td style='border:1px solid rgb(153, 153, 153);word-wrap: break-word;'><div style='text-align: right'>₹ "+cstr(soi.export_amount)+"</div></td></tr>") data=[] data.append({"item_code":soi.item_code,"so_qty":soi.qty,"proj_qty":(0-flt(soi.qty)),"warehouse":soi.reserved_warehouse,"bin_iqty":"","bin_pqty":"Bin.projected_qty","type":"so"}) self.update_bin(data) tax_total=0.00 if so.charge: tax_total=self.create_tax(so.charge,so.name,item_list,'Sales Order','Sales Taxes and Charges',net_tot) so_=Document("Sales Order",so.name) so_.net_total_export=net_tot so_.other_charges_total_export=cstr(tax_total) so_.grand_total_export=cstr(flt(so_.net_total_export)+flt(so_.other_charges_total_export)) so_.rounded_total_export=cstr(round(flt(so_.grand_total_export))) so_.save() a=html_data({"posting_date":datetime.datetime.strptime(nowdate(),'%Y-%m-%d').strftime('%d/%m/%Y'),"due_date":datetime.datetime.strptime(so.delivery_date,'%Y-%m-%d').strftime('%d/%m/%Y'),"customer_name":so.customer_name,"net_total":net_tot,"grand_total":net_tot,"rounded_total":net_tot,"table_data":html,"date_1":"Sales Order Date","date_2":"Expected Delivery Date","doctype":"Sales Order","doctype_no":so.name,"company":so.company,"addr_name":"Customer Address","address":so.address_display,"tax_detail":""}) file_path_=attach_file(a,[so.name,"Selling/Kirana","Sales Order"]) return {"Sales Order":so.name,"File Copy":file_path_}