diff --git a/gkcore/views/api_reports.py b/gkcore/views/api_reports.py index 81d2cfd9b0b22ca1edd20db79c861f8d83054ea7..f06d6b1f4604f166f5b6bf267f28ea07be0b1caf 100644 --- a/gkcore/views/api_reports.py +++ b/gkcore/views/api_reports.py @@ -3845,14 +3845,13 @@ free replacement or sample are those which are excluded. taxcolumns = [] #sales register(flag = 0) if int(self.request.params["flag"]) == 0: - invquery = self.con.execute("select invid, invoiceno, invoicedate, custid, invoicetotal, contents, tax, freeqty, sourcestate, taxstate,taxflag,discount from invoice where orgcode=%d AND custid IN (select custid from customerandsupplier where orgcode=%d AND csflag=3) AND invoicedate >= '%s' AND invoicedate <= '%s' order by invoicedate"%(authDetails["orgcode"], authDetails["orgcode"], datetime.strptime(str(self.request.params["calculatefrom"]),"%d-%m-%Y").strftime('%Y-%m-%d'), datetime.strptime(str(self.request.params["calculateto"]),"%d-%m-%Y").strftime('%Y-%m-%d'))) + invquery = self.con.execute("select invid, invoiceno, invoicedate, custid, invoicetotal, contents, tax,cess ,freeqty, sourcestate, taxstate,taxflag,discount from invoice where orgcode=%d AND custid IN (select custid from customerandsupplier where orgcode=%d AND csflag=3) AND invoicedate >= '%s' AND invoicedate <= '%s' order by invoicedate"%(authDetails["orgcode"], authDetails["orgcode"], datetime.strptime(str(self.request.params["calculatefrom"]),"%d-%m-%Y").strftime('%Y-%m-%d'), datetime.strptime(str(self.request.params["calculateto"]),"%d-%m-%Y").strftime('%Y-%m-%d'))) #purchase register(flag = 1) elif int(self.request.params["flag"]) == 1: - invquery = self.con.execute("select invid, invoiceno, invoicedate, custid, invoicetotal, contents, tax, freeqty, taxstate,sourcestate,taxflag,discount from invoice where orgcode=%d AND custid IN (select custid from customerandsupplier where orgcode=%d AND csflag=19) AND invoicedate >= '%s' AND invoicedate <= '%s' order by invoicedate"%(authDetails["orgcode"], authDetails["orgcode"], datetime.strptime(str(self.request.params["calculatefrom"]),"%d-%m-%Y").strftime('%Y-%m-%d'), datetime.strptime(str(self.request.params["calculateto"]),"%d-%m-%Y").strftime('%Y-%m-%d'))) - - + invquery = self.con.execute("select invid, invoiceno, invoicedate, custid, invoicetotal, contents, tax, cess,freeqty, taxstate,sourcestate,taxflag,discount from invoice where orgcode=%d AND custid IN (select custid from customerandsupplier where orgcode=%d AND csflag=19) AND invoicedate >= '%s' AND invoicedate <= '%s' order by invoicedate"%(authDetails["orgcode"], authDetails["orgcode"], datetime.strptime(str(self.request.params["calculatefrom"]),"%d-%m-%Y").strftime('%Y-%m-%d'), datetime.strptime(str(self.request.params["calculateto"]),"%d-%m-%Y").strftime('%Y-%m-%d'))) + srno = 1 '''This totalrow dictionary is used for very last row of report which contains sum of all columns in report''' totalrow = {"grossamount":"0.00", "taxfree":"0.00", "tax": {}, "taxamount":{}} @@ -3862,7 +3861,6 @@ free replacement or sample are those which are excluded. try: disc = row["discount"] - if int(row["taxflag"]) == 7: destinationstate = row["taxstate"] destinationStateCode = getStateCode(row["taxstate"],self.con)["statecode"] @@ -3887,9 +3885,10 @@ free replacement or sample are those which are excluded. totalrow["grossamount"] = "%.2f"%(float(totalrow["grossamount"]) + float("%.2f"%row["invoicetotal"])) qty = 0.00 ppu = 0.00 - #taxrate is in percentage + #taxrate and cessrate are in percentage taxrate = 0.00 - #taxamount is net amount for some tax rate. eg. 2% tax on 200rs. This 200rs is taxamount + cessrate = 0.00 + #taxamount is net amount for some tax rate. eg. 2% tax on 200rs. This 200rs is taxamount, i.e. Taxable amount taxamount = 0.00 '''This taxdata dictionary has key as taxrate and value as amount of tax to be paid on this rate. eg. {"2.00": "2.80"}''' taxdata = {} @@ -3898,58 +3897,83 @@ free replacement or sample are those which are excluded. '''for each product in invoice. row["contents"] is JSONB which has format like this - {"22": {"20.00": "2"}, "61": {"100.00": "1"}} where 22 and 61 is productcode, {"20.00": "2"} here 20.00 is price per unit and quantity is 2. - The other JSONB field in each invoice is row["tax"]. Its format is {"22": "2.00", "61": "2.00"}. Here, 22 and 61 are products and 2.00 is tax applied on those products''' + The other JSONB field in each invoice is row["tax"]. Its format is {"22": "2.00", "61": "2.00"}. Here, 22 and 61 are products and 2.00 is tax applied on those products, similarly for CESS {"22":"0.05"} where 22 is productcode snd 0.05 is cess rate''' for pc in row["contents"].iterkeys(): - discamt = 0.00 taxrate = "%.2f"%float(row["tax"][pc]) if disc != None: discamt = float(disc[pc]) else: discamt = 0.00 - for pcprice in row["contents"][pc].iterkeys(): ppu = pcprice - #freeqty is subtracted + gspc = self.con.execute(select([product.c.gsflag]).where(product.c.productcode==pc)) flag = gspc.fetchone() if int(flag["gsflag"]) == 7: - qty = float(row["contents"][pc][pcprice]) - float(row["freeqty"][pc]) if row["freeqty"].has_key(pc) else 0.00 - taxamount = (float(ppu) - float(discamt)) * float(qty) - #(((float("%.2f"%float(ppu))) - (float("%.2f"%float(discamt)))) * float("%.2f"%float(qty)) + qty = float(row["contents"][pc][pcprice]) + taxamount = (float(ppu) * float(qty)) - float(discamt) + #(((float("%.2f"%float(ppu))) * float("%.2f"%float(qty))- (float("%.2f"%float(discamt)))) else: - taxamount = float(ppu) - float(discamt) - # (float("%.2f"%float(ppu))) - (float("%.2f"%float(discamt))) - + taxamount = float(ppu) - float(discamt) + # (float("%.2f"%float(ppu))) - (float("%.2f"%float(discamt))) + if taxrate == "0.00": invoicedata["taxfree"] = "%.2f"%((float("%.2f"%float(invoicedata["taxfree"])) + taxamount)) totalrow["taxfree"] = "%.2f"%(float(totalrow["taxfree"]) + taxamount) continue '''if taxrate appears in this invoice then update invoice tax and taxamount for that rate Otherwise create new entries in respective dictionaries of that invoice''' - if taxdata.has_key(str(taxrate)): - taxdata[taxrate]="%.2f"%(float(taxdata[taxrate]) + taxamount) - taxamountdata[taxrate]="%.2f"%(float(taxamountdata[taxrate]) + taxamount*float(taxrate)/100.00) - else: - taxdata.update({taxrate:"%.2f"%taxamount}) - taxamountdata.update({taxrate:"%.2f"%(taxamount*float(taxrate)/100.00)}) - '''if new taxrate appears(in all invoices), ie. we found this rate for the first time then add this column to taxcolumns and also create new entries in tax & taxamount dictionaries Otherwise update existing data''' - if taxrate not in taxcolumns: - taxcolumns.append(taxrate) - totalrow["taxamount"].update({taxrate:"%.2f"%float(taxamountdata[taxrate])}) - totalrow["tax"].update({taxrate:"%.2f"%taxamount}) - else: - totalrow["taxamount"][taxrate] = "%.2f"%(float(totalrow["taxamount"][taxrate]) + float(taxamount*float(taxrate)/100.00)) - totalrow["tax"][taxrate] = "%.2f"%(float(totalrow["tax"][taxrate]) + taxamount) + if taxrate != "0.00": + + if taxdata.has_key(str(taxrate)): + taxdata[taxrate]="%.2f"%(float(taxdata[taxrate]) + taxamount) + taxamountdata[taxrate]="%.2f"%(float(taxamountdata[taxrate]) + taxamount*float(taxrate)/100.00) + + else: + taxdata.update({taxrate:"%.2f"%taxamount}) + taxamountdata.update({taxrate:"%.2f"%(taxamount*float(taxrate)/100.00)}) + + '''if new taxrate appears(in all invoices), ie. we found this rate for the first time then add this column to taxcolumns and also create new entries in tax & taxamount dictionaries Otherwise update existing data''' + if taxrate not in taxcolumns: + taxcolumns.append(taxrate) + totalrow["taxamount"].update({taxrate:"%.2f"%float(taxamountdata[taxrate])}) + totalrow["tax"].update({taxrate:"%.2f"%taxamount}) + else: + totalrow["taxamount"][taxrate] = "%.2f"%(float(totalrow["taxamount"][taxrate]) + float(taxamount*float(taxrate)/100.00)) + totalrow["tax"][taxrate] = "%.2f"%(float(totalrow["tax"][taxrate]) + taxamount) + + if row["taxflag"] == 22: + continue + + if row["cess"] != None: + cessrate = "%.2f"%float(row["cess"][pc]) + if cessrate != "0.00": + if taxdata.has_key(str(cessrate)): + taxdata[cessrate]="%.2f"%(float(taxdata[cessrate]) + taxamount) + taxamountdata[cessrate]="%.2f"%(float(taxamountdata[cessrate]) + taxamount*float(cessrate)/100.00) + else: + taxdata.update({cessrate:"%.2f"%taxamount}) + taxamountdata.update({cessrate:"%.2f"%(taxamount*float(cessrate)/100.00)}) + + if cessrate not in taxcolumns: + taxcolumns.append(cessrate) + totalrow["taxamount"].update({cessrate:"%.2f"%float(taxamountdata[cessrate])}) + totalrow["tax"].update({cessrate:"%.2f"%taxamount}) + else: + totalrow["taxamount"][cessrate] = "%.2f"%(float(totalrow["taxamount"][cessrate]) + float(taxamount*float(cessrate)/100.00)) + totalrow["tax"][cessrate] = "%.2f"%(float(totalrow["tax"][cessrate]) + taxamount) + + invoicedata["tax"] = taxdata invoicedata["taxamount"] = taxamountdata spdata.append(invoicedata) srno += 1 except: pass - taxcolumns.sort(reverse=True) - + + taxcolumns.sort(key=float) return {"gkstatus":enumdict["Success"], "gkresult":spdata, "totalrow":totalrow, "taxcolumns":taxcolumns} except: return {"gkstatus":enumdict["ConnectionFailed"] }