public bool saveOrder(bool bDraft) { bool bRet = true; clsSqlFactory hSql = new clsSqlFactory(); clsBaseUtility objUtil = new clsBaseUtility(); clsCurrExchange objCurr = new clsCurrExchange(); objCurr.Init(); String strSql = ""; try { if (bDraft == false) { strSql = "select V1 from " + objUtil.getTable("CORW", UnitId) + " where CODAID='VLASKUTYY' and C1=? "; bRet = bRet && hSql.NewCommand(strSql); hSql.Com.Parameters.AddWithValue("BTYPE", BTYPE); hSql.ExecuteReader(); if (hSql.Read()) { int NuseId = hSql.Reader.GetInt32(0); strSql = "update " + objUtil.getTable("NUSE", UnitId) + " set RECNO=RECNO+1 where NUSEID=? "; bRet = bRet && hSql.NewCommand(strSql); hSql.Com.Parameters.AddWithValue("NUSEID", NuseId); hSql.ExecuteNonQuery(); strSql = "select RECNO from " + objUtil.getTable("NUSE", UnitId) + " where NUSEID=? "; bRet = bRet && hSql.NewCommand(strSql); hSql.Com.Parameters.AddWithValue("NUSEID", NuseId); hSql.ExecuteReader(); if (hSql.Read()) { SRECNO = hSql.Reader.GetInt32(0); } else { bDraft = true; } } else { bDraft = true; } } strSql = "select top 1 isnull(SSALID,0), isnull(SORDNO,0) from " + objUtil.getTable("SSALSEED", UnitId); bRet = bRet && hSql.ExecuteReader(strSql); if (bRet && hSql.Read()) { SSALID = hSql.Reader.GetInt32(0) + 1; SORDNO = hSql.Reader.GetInt32(1) + 1; strSql = "update " + objUtil.getTable("SSALSEED", UnitId) + " set SSALID = SSALID + 1, SORDNO = SORDNO + 1 "; bRet = bRet && hSql.ExecuteNonQuery(strSql); } strSql = "insert into " + objUtil.getTable("SSAL", UnitId) + "(CREATED,SSALID, CUSTNO, SMANID, DEPT, STATUS,RECTYPE, RECORDID, SORDNO, STYPE, " + " DIVISION, ISDIVIDED, BOPRIOR, OUSRSID, CDTRAN,NOTE,RDATE,EXIDNO,VEHIID)" + " values (getdate(),?, ?, ?,?, ?, ?, 0, ?, ?, 0, 0, 0, ?, 0, ?,?,?,?) "; bRet = bRet && hSql.NewCommand(strSql); hSql.Com.Parameters.AddWithValue("SSALID", SSALID); hSql.Com.Parameters.AddWithValue("CUSTNO", CustNo); hSql.Com.Parameters.AddWithValue("SMANID", SmanId); if (DeptId != null) { hSql.Com.Parameters.AddWithValue("DEPT", DeptId); } else { hSql.Com.Parameters.AddWithValue("DEPT", DBNull.Value); } if (bDraft == true) { hSql.Com.Parameters.AddWithValue("STATUS", 'A'); hSql.Com.Parameters.AddWithValue("RECTYPE", DBNull.Value); } else { hSql.Com.Parameters.AddWithValue("STATUS", 'S'); if (BTYPE == "0") { hSql.Com.Parameters.AddWithValue("RECTYPE", 'E'); } else { hSql.Com.Parameters.AddWithValue("RECTYPE", 'N'); } } hSql.Com.Parameters.AddWithValue("SORDNO", SORDNO); hSql.Com.Parameters.AddWithValue("STYPE", SalesType); hSql.Com.Parameters.AddWithValue("OUSRSID", OUserId); hSql.Com.Parameters.AddWithValue("NOTE", Note); hSql.Com.Parameters.AddWithValue("RDATE", DELD.ToString("yyyy-MM-dd")); hSql.Com.Parameters.AddWithValue("EXIDNO", ExtOrderId); hSql.Com.Parameters.AddWithValue("VEHIID", VehiId); bRet = bRet && hSql.ExecuteNonQuery(); strSql = "insert into " + objUtil.getTable("SBIL", UnitId) + "(SSALID, SRECNO, CUSTNO, LCUSTNO, DCUSTNO, " + " REFE, DDEL, DWP, PDEL," + " CURRCD, BTYPE,BILLD,BILLD_WITH_TIME,HSMANID,DELD,TPCODE,TPTIME)" + " values (?, ?, ?, ?, ?," + " ?, 0, 0, 1, " + "?,?,?,?,?,?,?,?) "; bRet = bRet && hSql.NewCommand(strSql); hSql.Com.Parameters.AddWithValue("SSALID", SSALID); hSql.Com.Parameters.AddWithValue("SRECNO", SRECNO); hSql.Com.Parameters.AddWithValue("CUSTNO", CustNo); hSql.Com.Parameters.AddWithValue("LCUSTNO", CustNo); hSql.Com.Parameters.AddWithValue("DCUSTNO", DCustNo); hSql.Com.Parameters.AddWithValue("REFE", Note.Length > 45 ? Note.Substring(0, 45) : Note); hSql.Com.Parameters.AddWithValue("CURRCD", objCurr.BaseCurrency); hSql.Com.Parameters.AddWithValue("BTYPE", BTYPE); if (bDraft == true) { hSql.Com.Parameters.AddWithValue("BILLD", DBNull.Value); hSql.Com.Parameters.AddWithValue("BILLD_WITH_TIME", DBNull.Value); hSql.Com.Parameters.AddWithValue("HSMANID", DBNull.Value); hSql.Com.Parameters.AddWithValue("DELD", DBNull.Value); } else { hSql.Com.Parameters.AddWithValue("BILLD", new DateTime(BILLD.Year, BILLD.Month, BILLD.Day)); hSql.Com.Parameters.AddWithValue("BILLD_WITH_TIME", DateTime.Now); hSql.Com.Parameters.AddWithValue("HSMANID", SmanId); hSql.Com.Parameters.AddWithValue("DELD", new DateTime(DELD.Year, DELD.Month, DELD.Day)); } hSql.Com.Parameters.AddWithValue("TPCODE", TPCODE); hSql.Com.Parameters.AddWithValue("TPTIME", TPTIME); bRet = bRet && hSql.ExecuteNonQuery(); strSql = "update a set a.AGRP = b.AGRP, a.CUSTNAME = ltrim(isnull(b.FNAME,'')+' ' +isnull(b.LNAME,'')) from " + objUtil.getTable("SBIL", UnitId) + " a, CUST b where a.SSALID=? and a.CUSTNO = b.CUSTNO "; bRet = bRet && hSql.NewCommand(strSql); hSql.Com.Parameters.AddWithValue("SSALID", SSALID); bRet = bRet && hSql.ExecuteNonQuery(); strSql = "update a set a.DADDR1 = b.ADDR1, a.DADDR2 = b.ADDR2,a.DCTRYCD=b.CTRYCD,a.DCOUNTRY=b.COUNTRY," + " a.DNAME= ltrim(isnull(b.FNAME,'')+' ' +isnull(b.LNAME,'')), a.DPO=b.PO,a.DPOSTCD=b.POSTCD,a.DADDR2E=b.ADDR2E from " + objUtil.getTable("SBIL", UnitId) + " a, CUST b where a.SSALID=? and a.DCUSTNO = b.CUSTNO "; bRet = bRet && hSql.NewCommand(strSql); hSql.Com.Parameters.AddWithValue("SSALID", SSALID); bRet = bRet && hSql.ExecuteNonQuery(); strSql = "insert into ZSC_ContractInvoice(ContractOID,InvoiceNo,SSALID,UnitId,InvoiceType,Created,Modified) select " + "?,?,SSALID,?,0,getdate(),getdate() from " + objUtil.getTable("SBIL", UnitId) + " where SSALID=?"; bRet = bRet && hSql.NewCommand(strSql); hSql.Com.Parameters.AddWithValue("ContractOID", ContractOID); hSql.Com.Parameters.AddWithValue("InvoiceNo", InvoiceSeqNr); hSql.Com.Parameters.AddWithValue("UnitId", UnitId); hSql.Com.Parameters.AddWithValue("SSALID", SSALID); bRet = bRet && hSql.ExecuteNonQuery(); strSql = "update ZSC_Contract set LastInvoiceDate = ?,NextInvoiceDate=? where OID=?"; bRet = bRet && hSql.NewCommand(strSql); hSql.Com.Parameters.AddWithValue("CBILLD", CBILLD); hSql.Com.Parameters.AddWithValue("NBILLD", NBILLD); hSql.Com.Parameters.AddWithValue("ContractOID", ContractOID); bRet = bRet && hSql.ExecuteNonQuery(); clsTaxHandling objTax = new clsTaxHandling(); objTax.Init(6); foreach (SCInvoiceItem objRow in InvItems) { strSql = "insert into " + objUtil.getTable("SROW", UnitId) + "(CREATED,SSALID, SROWID, SRECNO, SMANID, ITEMNO, SUPLNO, BUYPR, " + " DISCPC, ITEM, SUPL, NAME, NUM, RNO, RTYPE, RSUM, UNITPR, VATCD, " + " ONDEDNUM, ORDNUM, IGROUPID,NOTE,RINFO,EXIDNO) values (getdate(),?,?,?,?,?,?,?," + " ?,?,?,?,?,?,?,?,?,?," + "0,?,?,?,?,?)"; bRet = bRet && hSql.NewCommand(strSql); hSql.Com.Parameters.AddWithValue("SSALID", SSALID); hSql.Com.Parameters.AddWithValue("SROWID", DBNull.Value); hSql.Com.Parameters.AddWithValue("SRECNO", SRECNO); hSql.Com.Parameters.AddWithValue("SMANID", SmanId); hSql.Com.Parameters.AddWithValue("ITEMNO", DBNull.Value); hSql.Com.Parameters.AddWithValue("SUPLNO", DBNull.Value); hSql.Com.Parameters.AddWithValue("BUYPR", DBNull.Value); hSql.Com.Parameters.AddWithValue("DISCPC", DBNull.Value); hSql.Com.Parameters.AddWithValue("ITEM", DBNull.Value); hSql.Com.Parameters.AddWithValue("SUPL", DBNull.Value); hSql.Com.Parameters.AddWithValue("NAME", DBNull.Value); hSql.Com.Parameters.AddWithValue("NUM", DBNull.Value); hSql.Com.Parameters.AddWithValue("RNO", DBNull.Value); hSql.Com.Parameters.AddWithValue("RTYPE", DBNull.Value); hSql.Com.Parameters.AddWithValue("RSUM", DBNull.Value); hSql.Com.Parameters.AddWithValue("UNITPR", DBNull.Value); hSql.Com.Parameters.AddWithValue("VATCD", DBNull.Value); hSql.Com.Parameters.AddWithValue("ORDNUM", DBNull.Value); hSql.Com.Parameters.AddWithValue("IGROUPID", DBNull.Value); hSql.Com.Parameters.AddWithValue("NOTE", DBNull.Value); hSql.Com.Parameters.AddWithValue("RINFO", DBNull.Value); hSql.Com.Parameters.AddWithValue("EXIDNO", DBNull.Value); if (objRow.RTYPE != 8) { hSql.Com.Parameters["SROWID"].Value = objRow.SROWID; hSql.Com.Parameters["ITEMNO"].Value = objRow.ITEMNO; hSql.Com.Parameters["SUPLNO"].Value = objRow.SUPLNO; hSql.Com.Parameters["BUYPR"].Value = objRow.BUYPR; hSql.Com.Parameters["DISCPC"].Value = objRow.DISCPC; hSql.Com.Parameters["ITEM"].Value = objRow.ITEMNO; hSql.Com.Parameters["SUPL"].Value = objRow.SUPLNO; hSql.Com.Parameters["NAME"].Value = objRow.NAME; hSql.Com.Parameters["NUM"].Value = objRow.NUM; hSql.Com.Parameters["RNO"].Value = objRow.SROWID; hSql.Com.Parameters["RTYPE"].Value = objRow.RTYPE; hSql.Com.Parameters["RSUM"].Value = objRow.RSUM; hSql.Com.Parameters["UNITPR"].Value = objRow.UNITPR; hSql.Com.Parameters["VATCD"].Value = objRow.VATCD; hSql.Com.Parameters["ORDNUM"].Value = objRow.NUM; if (objRow.IGROUPID > 0) { hSql.Com.Parameters["IGROUPID"].Value = objRow.IGROUPID; } hSql.Com.Parameters["EXIDNO"].Value = objRow.EXIDNO; } else { hSql.Com.Parameters["SROWID"].Value = objRow.SROWID; hSql.Com.Parameters["NAME"].Value = objRow.NAME; hSql.Com.Parameters["RNO"].Value = objRow.SROWID; hSql.Com.Parameters["RTYPE"].Value = objRow.RTYPE; hSql.Com.Parameters["NOTE"].Value = objRow.NOTE; if ((objRow.RINFO != null) && (objRow.RINFO != "")) { hSql.Com.Parameters["RINFO"].Value = objRow.RINFO; } hSql.Com.Parameters["EXIDNO"].Value = objRow.EXIDNO; } hSql.ExecuteNonQuery(); } hSql.Commit(); } catch (Exception ex) { hSql.Rollback(); throw ex; } finally { hSql.Close(); } return(bRet); }
private void addInvoiceRows(Contract objContract, ref SCInvoice objInv, bool bCapital) { clsSqlFactory hSql = new clsSqlFactory(); int RowId = objInv.InvItems.Count; Decimal nInvoiceSum = 0; Decimal nInvoiceOrigSum = 0; Decimal nInvoiceOrigBuyPr = 0; int nPayPeriod = 1; clsTaxHandling objTax = new clsTaxHandling(); objTax.Init(2); String RowVatCd = getVatCode(objTax, objInv.BTYPE); SCInvoiceItem objRow; if (objContract.IsInvoiceDetail == false) { RowId++; objRow = new SCInvoiceItem(); objRow.SROWID = RowId; objRow.NAME = "~1"; objRow.RTYPE = 8; objRow.NOTE = "CONTRACT NO " + objContract.ContractNo.ToString(); objRow.EXIDNO = objContract.ContractNo; objInv.InvItems.Add(objRow); } switch (objContract.ContractCostData.CostBasis.strValue1) { case CostBasisType.Monthly: nInvoiceSum = objContract.ContractCostData.CostBasedOnService - objContract.ContractCapitalData.CapitalMonthAmount; break; case CostBasisType.KmOrHour: break; case CostBasisType.KmOrHourWithLump: nInvoiceSum = objContract.ContractCostData.CostMonthBasis - objContract.ContractCapitalData.CapitalMonthAmount; break; default: break; } switch (objContract.ContractPaymentData.PaymentPeriod.strValue1) { case PaymentPeriodType.Quarterly: nPayPeriod = 3; break; case PaymentPeriodType.HalfYear: nPayPeriod = 6; break; case PaymentPeriodType.Yearly: nPayPeriod = 12; break; default: break; } if (bCapital) { nInvoiceSum = objContract.ContractCapitalData.CapitalMonthAmount * nPayPeriod; nInvoiceSum = (Decimal)objTax.Add(nInvoiceSum, RowVatCd, objInv.BILLD); } else { if (objInv.Payer != "") { nInvoiceSum = 0; } nInvoiceSum = (Decimal)objTax.Add(nInvoiceSum, RowVatCd, objInv.BILLD); nInvoiceSum = nInvoiceSum * nPayPeriod; } foreach (ContractOption objCat in objContract.listContractOptions) { if ((objCat.PartNr != null) && (objCat.PartNr != "") && (((objCat.PartialPayer == objInv.Payer) && (bCapital == false)) || ((objCat.PartialPayer == "") && (bCapital == true)) ) ) { RowId++; objRow = new SCInvoiceItem(); objRow.ITEMNO = objCat.PartNr; if (objInv.PartPostFix != "") { hSql.NewCommand("select 1 from ITEM where ITEMNO=? and SUPLNO=? "); hSql.Com.Parameters.Add("ITEMNO", objRow.ITEMNO + objInv.PartPostFix); hSql.Com.Parameters.Add("SUPLNO", objCat.PartSuplNo); hSql.ExecuteReader(); if (hSql.Read()) { objRow.ITEMNO += objInv.PartPostFix; } } objRow.SUPLNO = objCat.PartSuplNo; objRow.NAME = objCat.Name; objRow.RTYPE = 2; objRow.SROWID = RowId; objRow.BUYPR = (Decimal)objTax.Add(objCat.PurchasePr, RowVatCd, objInv.BILLD); objRow.DISCPC = 0; objRow.NUM = 1; objRow.NUM = objRow.NUM * nPayPeriod; objRow.UNITPR = (Decimal)objTax.Add(objCat.SalePr, RowVatCd, objInv.BILLD); objRow.RSUM = (decimal)objRow.NUM * objRow.UNITPR; objRow.IGROUPID = -1; objRow.VATCD = RowVatCd; nInvoiceOrigSum += objRow.RSUM; nInvoiceOrigBuyPr += objRow.BUYPR; objRow.EXIDNO = objContract.ContractNo; objInv.InvItems.Add(objRow); if ((objInv.Payer != "") && (bCapital == false)) { nInvoiceSum += objRow.RSUM; } } } if (objContract.IsInvoiceDetail == false) { RowId++; objRow = new SCInvoiceItem(); objRow.SROWID = RowId; objRow.NAME = "~1"; objRow.RTYPE = 8; objRow.NOTE = "-----------------------"; objRow.EXIDNO = objContract.ContractNo; objInv.InvItems.Add(objRow); } //adapt row sum if ((nInvoiceOrigSum != 0) && (objInv.Payer == "") || (bCapital == true)) { for (int i = 0; i < objInv.InvItems.Count; i++) { if (objInv.InvItems[i].RTYPE != 8) { Decimal nOrigRSUM = objInv.InvItems[i].RSUM; objInv.InvItems[i].RSUM = nOrigRSUM * nInvoiceSum / nInvoiceOrigSum; objInv.InvItems[i].BUYPR = objInv.InvItems[i].BUYPR * nInvoiceSum / nInvoiceOrigSum; if (nInvoiceOrigSum > nInvoiceSum) { if (nOrigRSUM != 0) { objInv.InvItems[i].DISCPC = (nOrigRSUM - objInv.InvItems[i].RSUM) / nOrigRSUM; } } else { objInv.InvItems[i].UNITPR = objInv.InvItems[i].RSUM / objInv.InvItems[i].NUM; } } } } hSql.Close(); }