public string DoSave(DsPurchase.RETURNMASTRow drow, DsPurchase.RETURNDETAILDataTable returndetail)
        {
            var dm = new DataManager();
            string retval;
            SqlTransaction tran = null;
            var isnew = false;
            try
            {
                var cn = new SqlConnection(_constr);
                cn.Open();
                tran = cn.BeginTransaction(IsolationLevel.ReadUncommitted);
                var ds = new DsPurchase();
                var dsStock = new DsInventory();
                var dt = ds.RETURNMAST;
                var taPurMain = new RETURNMASTTableAdapter {Connection = {ConnectionString = _constr}};
                var taPurDetail = new RETURNDETAILTableAdapter();
     
    
                var taStockMaster = new STOCKMASTERTableAdapter {Connection = {ConnectionString = _constr}};
                var taapar = new LedgersTableAdapters.APARMAINTableAdapter();
                var taledger = new LedgersTableAdapters.LEDGERSTableAdapter();

                taStockMaster.Fill(dsStock.STOCKMASTER);

                taPurMain.Fill(ds.RETURNMAST);



                // Assaign Transaction
                taPurMain.AssignConnection(cn, tran);
                taPurDetail.AssignConnection(cn, tran);
                taStockMaster.AssignConnection(cn, tran);

                taapar.AssignConnection(cn, tran);
                taledger.AssignConnection(cn, tran);

                var dr = dt.FindByRETURNID(drow.RETURNID);
                if (dr != null)
                { dr.BeginEdit(); }
                else
                { dr = (DsPurchase.RETURNMASTRow)dt.NewRow(); }
                if (string.IsNullOrEmpty(drow.RETURNID))
                {
                    var autoid = dm.GetAutoId(2);
                    autoid++;
                    Utils.Retinvid = dm.CreateFormatedAutoNo(autoid);
                    dr.RETURNID = Utils.Retinvid;
                    dr.USERID = Utils.Userid;
                    dr.COMPANY = Utils.Company;
                    dr.TRANDATE = System.DateTime.Now;
                    isnew = true;
                }
                else
                {
                    Utils.Retinvid = drow.RETURNID;
                    dr.EDATE = DateTime.Now;
                    dr.EUSERID = Utils.Userid;
                }
                dr.SUPID = drow.SUPID;
                dr.SUPNAME = drow.SUPNAME;
                dr.INVDATE = drow.INVDATE;
                dr.INVNO = drow.INVNO;
                dr.SPDISCOUNT = drow.SPDISCOUNT;
                dr.WHID = drow.WHID;
                dr.ADJVALUE = drow.ADJVALUE;
                dr.NETVALUE = drow.NETVALUE;
                dr.HOLD = drow.HOLD;

                if (isnew)
                {
                    dt.Rows.Add(dr);
                    taPurMain.Update(dt);
                }
                else
                {
                    dr.EndEdit();
                    taPurMain.Update(dr);
                }
                foreach (DataRow purd in returndetail.Rows)
                {
                    if ((purd.RowState != DataRowState.Deleted))
                        purd["RETURNID"] = Utils.Retinvid;
                }
                taPurDetail.Update(returndetail);
                //STOCK
                if (!dr.HOLD)
                {
                   // Masters.StockMaster.UpdateQtyInStockDataSet(ref dsStock, returndetail, true);
                   // taStockMaster.Update(dsStock.STOCKMASTER);
                    Masters.StockMaster.UpdateQtyInStockDataSet(ref cn,ref tran, returndetail, true);
                //LEDGER
                    taapar.Insert("S", dr.SUPID, DateTime.Now.Date, dr.INVDATE, Utils.Retinvid, dr.INVNO, -dr.NETVALUE, 0, -dr.NETVALUE, Utils.Company, 2, Utils.Userid, 0);
                    taledger.Insert("S", dr.SUPID, DateTime.Now.Date, dr.INVDATE, Utils.Retinvid, dr.INVNO, dr.NETVALUE,0, 2, Utils.Company, Utils.Userid, "Purchase Retun");
                }
                dm.UpdateAutoNum(2);
                tran.Commit();
                retval = "1";
            }
            catch (Exception ex)
            {
                if (tran != null)
                    tran.Rollback();
                if (isnew)
                    Utils.Retinvid = string.Empty;
                retval = ex.ToString();
            }
            return retval;
        }
        public string DoSavePurchaseInvoiceInRemote(DsPurchase.PURINVMAINRow mainrow, DsPurchase.PURINVDETAILDataTable purinvdetail)
        {
            var dm = new DataManager();
            string retval="0";
            SqlTransaction tran = null;
          //var possql = new SqlAsyncConnectionString();
            var cn = new SqlConnection(_constr);
            try
            {
                cn.Open();
                tran = cn.BeginTransaction(IsolationLevel.ReadUncommitted);
                var ds = new DsPurchase();
                var dt = ds.PURINVMAIN;
                var dtdetail = ds.PURINVDETAIL;

                

                var taPurMain = new PURINVMAINTableAdapter();

                var taPurDetail = new PURINVDETAILTableAdapter();

                var taapar = new LedgersTableAdapters.APARMAINTableAdapter();
                var taledger = new LedgersTableAdapters.LEDGERSTableAdapter();

                taPurMain.Connection.ConnectionString = _constr;
                taPurMain.FillByPurid(dt, mainrow.PURINVID);

                   // Assaign Transaction
                taPurMain.AssignConnection(cn, tran);
                taPurDetail.AssignConnection(cn, tran);
                
                taapar.AssignConnection(cn, tran);
                taledger.AssignConnection(cn, tran);
                DsPurchase.PURINVMAINRow dr = null;
               
                if (dt.Rows.Count == 0)
                {
                    dr = (DsPurchase.PURINVMAINRow) dt.NewRow();
                    dr.PURINVID = mainrow.PURINVID;
                    dr.USERID = mainrow.USERID;
                    dr.COMPANY = mainrow.COMPANY;
                    dr.UDATE = mainrow.UDATE;
                    if (!mainrow.IsEDATENull())
                        dr.EDATE = mainrow.EDATE;
                    if (!mainrow.IsEUSERIDNull())
                        dr.EUSERID = mainrow.EUSERID;
                    dr.SUPID = mainrow.SUPID;
                    dr.SUPNAME = mainrow.SUPNAME;
                    dr.INVDATE = mainrow.INVDATE;
                    dr.INVNO = mainrow.INVNO;
                    dr.DUEDAYS = mainrow.DUEDAYS;
                    dr.SPDISCOUNT = mainrow.SPDISCOUNT;
                    dr.WHID = mainrow.WHID;
                    dr.ADJVALUE = mainrow.ADJVALUE;
                    dr.NETVALUE = mainrow.NETVALUE;
                    dr.HOLD = mainrow.HOLD;
                    dt.Rows.Add(dr);
                    taPurMain.Update(dt);
                    //LEDGER
                    taapar.Insert("S", dr.SUPID, mainrow.UDATE, dr.INVDATE, mainrow.PURINVID, dr.INVNO, dr.NETVALUE, 0, dr.NETVALUE, Utils.Company, 1, Utils.Userid, 0);
                    taledger.Insert("S", dr.SUPID, DateTime.Now.Date, dr.INVDATE, mainrow.PURINVID, dr.INVNO, 0,dr.NETVALUE, 1, Utils.Company, Utils.Userid, "Purchase Invoice");
                    foreach (DsPurchase.PURINVDETAILRow ditemdetarow in purinvdetail)
                    {
                        var newdetrow = dtdetail.NewRow();
                        foreach (DataColumn dc in purinvdetail.Columns)
                        {
                            if (!dc.ReadOnly)
                            {
                                newdetrow[dc.ColumnName] = ditemdetarow[dc.ColumnName];
                            }
                        }
                        dtdetail.Rows.Add(newdetrow);
                    }
                    taPurDetail.Update(dtdetail);
                }
                else
                {
               
                    dr = dt.FindByPURINVID(mainrow.PURINVID);
                    var cmd = "UPDATE [PURINVMAIN] SET  SUPID = '" + dr.SUPID + "' ,SUPNAME = '" + dr.SUPNAME +
                              "', INVNO = '" + dr.INVNO + "' ,INVDATE  = '" + dr.INVDATE.ToString("yyyy-MM-dd HH:mm:ss") + "', DUEDAYS] = " + dr.DUEDAYS + " WHERE  PURINVID ='" + dr.PURINVID + "'";
                var sqlcmd2 = new SqlCommand();
                    sqlcmd2.Connection = cn;
                    sqlcmd2.Transaction = tran;
                    sqlcmd2.CommandText = cmd;


                    cmd = "update APARMAIN set REFNO = '" + dr.SUPID + "',INVOICEDATE ='" + dr.INVDATE.ToString("yyyy-MM-dd HH:mm:ss") + "',invoiceno= '" + dr.INVNO + "'  where SOURCEID=1 and  TYPE='S' AND VOUCHERNO='" + dr.PURINVID + "'";
                    var sqlcmd = new SqlCommand();
                    sqlcmd.Connection = cn;
                    sqlcmd.Transaction = tran;
                    sqlcmd.CommandText = cmd;
                    
                    sqlcmd.ExecuteNonQuery();
                    cmd = "update LEDGERS set REFNO = '" + dr.SUPID + "',INVOICEDATE = '" + dr.INVDATE.ToString("yyyy-MM-dd HH:mm:ss") + "' ,invoiceno= '" + dr.INVNO + "'   where  SORUCE =1 and LEDGERTYPE='S' AND VOUCHERNO= '" + dr.PURINVID + "'";
                    var sqlcmd1 = new SqlCommand();
                    sqlcmd1.Connection = cn;
                    sqlcmd1.Transaction = tran;
                    sqlcmd1.CommandText = cmd;
                    sqlcmd1.ExecuteNonQuery();
                }
                tran.Commit();
                retval = "1";
            }
            catch (Exception ex)
            {
                if (cn != null)
                {
                    if (tran != null)
                        tran.Rollback();
                }
                retval = ex.ToString();
            }
            return retval; 
        }
        public string DoSavePurchaseInvoiceReturnInRemote(DsPurchase.RETURNMASTRow drow, DsPurchase.RETURNDETAILDataTable returndetail)
        {
            var dm = new DataManager();
            string retval="0";
            SqlTransaction tran = null;
            try
            {
                var cn = new SqlConnection(_constr);
                cn.Open();
                tran = cn.BeginTransaction(IsolationLevel.ReadUncommitted);
                var ds = new DsPurchase();
                var dt = ds.RETURNMAST;
                var dtdetail = ds.RETURNDETAIL;

                var taPurMain = new RETURNMASTTableAdapter ();
                var taPurDetail = new RETURNDETAILTableAdapter();
                var taapar = new LedgersTableAdapters.APARMAINTableAdapter();
                var taledger = new LedgersTableAdapters.LEDGERSTableAdapter();
  
                // Assaign Transaction
                taPurMain.AssignConnection(cn, tran);
                taPurDetail.AssignConnection(cn, tran);
                taapar.AssignConnection(cn, tran);
                taledger.AssignConnection(cn, tran);
               var  dr = (DsPurchase.RETURNMASTRow)dt.NewRow();  
                    dr.RETURNID = drow.RETURNID;
                    dr.USERID = drow.USERID;
                    dr.COMPANY = drow.COMPANY;
                    dr.TRANDATE = drow.TRANDATE;
                if(!dr.IsEDATENull())
                    dr.EDATE =  dr.EDATE;
                if(!dr.IsEUSERIDNull())
                    dr.EUSERID = dr.EUSERID;
                    dr.SUPID = drow.SUPID;
                    dr.SUPNAME = drow.SUPNAME;
                    dr.INVDATE = drow.INVDATE;
                    dr.INVNO = drow.INVNO;
                    dr.SPDISCOUNT = drow.SPDISCOUNT;
                    dr.WHID = drow.WHID;
                    dr.ADJVALUE = drow.ADJVALUE;
                    dr.NETVALUE = drow.NETVALUE;
                    dr.HOLD = drow.HOLD;
                    dt.Rows.Add(dr);
                    taPurMain.Update(dt);
                //LEDGER
                    taapar.Insert("S", dr.SUPID, drow.TRANDATE, dr.INVDATE, drow.RETURNID, dr.INVNO, -dr.NETVALUE, 0, -dr.NETVALUE, Utils.Company, 2, Utils.Userid, 0);
                    taledger.Insert("S", dr.SUPID, drow.TRANDATE, dr.INVDATE, drow.RETURNID, dr.INVNO, dr.NETVALUE, 0, 2, Utils.Company, Utils.Userid, "Purchase Retun");

                    foreach (DsPurchase.RETURNDETAILRow ditemdetarow in returndetail)
                    {
                        var newdetrow = dtdetail.NewRow();
                        foreach (DataColumn dc in returndetail.Columns)
                        {
                            if (!dc.ReadOnly)
                            {
                                newdetrow[dc.ColumnName] = ditemdetarow[dc.ColumnName];
                            }
                        }
                        dtdetail.Rows.Add(newdetrow);
                    }
                    taPurDetail.Update(dtdetail);
                tran.Commit();
                retval = "1";
            }
            catch (Exception ex)
            {
                if (tran != null)
                    tran.Rollback();
                retval = ex.ToString();
            }
            return retval;
        }
        public string DoSave(Ledgers.ARAPRECEIPTSRow drmain,Ledgers.ARAPRECEIPTSDETAILSDataTable dt)
        {
            SqlTransaction tran = null;
            var isnew = false;
            string retval = string.Empty;
            try
            {
                var cn = new SqlConnection (_constr);
                cn.Open();
                tran = cn.BeginTransaction(IsolationLevel.ReadUncommitted);

                var taPurPaid = new ARAPRECEIPTSTableAdapter {Connection = {ConnectionString = _constr}};

                var dtmain = new Ledgers.ARAPRECEIPTSDataTable();
                var ds = taPurPaid.Fill(dtmain);

                var taPurPaidDet = new ARAPRECEIPTSDETAILSTableAdapter();

                var taApAr = new LedgersTableAdapters.APARMAINTableAdapter();

                var taLedger = new LedgersTableAdapters.LEDGERSTableAdapter();
                taLedger.AssignConnection(cn, tran);

                taPurPaid.AssignConnection(cn,tran);
                var dm = new DataManager();
                taPurPaidDet.AssignConnection(cn,tran);
                  

                var dr = dtmain.FindByARAPTRANID(drmain.ARAPTRANID);
                if (dr != null)
                {
                    dr.BeginEdit();
                }
                else
                {
                    dr = (Ledgers.ARAPRECEIPTSRow) dtmain.NewRow();
                    Utils.PurPayment = dm.GetAutoId(19).ToString("D6");
                    drmain.ARAPTRANID =Utils.PurPayment;
                    dr.ARAPTRANID = Utils.PurPayment;
                    isnew = true;
                }
                dr.AMOUNT = drmain.AMOUNT;
                dr.TDATE = drmain.TDATE;
                dr.BANKID = drmain.BANKID;
                dr.POSTED = false;
                string cmdtxt = string.Empty;
                foreach (Ledgers.ARAPRECEIPTSDETAILSRow drdet in dt.Rows)
                {
                    if (drdet.RowState != DataRowState.Deleted)
                    {
                        drdet.ARAPTRANID = dr.ARAPTRANID;
                        cmdtxt = "update APARMAIN set PAIDAMOUNT = " + drdet.AppliedAmt +
                                     ", BALANCE = 0 where  TRANID = " + drdet.TRANID;
                            var cmd = new SqlCommand(cmdtxt, cn, tran);
                            cmd.ExecuteNonQuery();
                           taLedger.Insert("S", drdet.REFNO, DateTime.Now.Date, drdet.INVOICEDATE, dr.ARAPTRANID,drdet.INVOICENO, drdet.INVAMOUNT,0, 18, Utils.Company, Utils.Userid, "Purchase Payment");
                    }
                }
                if (isnew)
                {
                    dtmain.Rows.Add(dr);
                    taPurPaid.Update(dtmain);
                }
                else
                {
                    dr.EndEdit();
                    taPurPaid.Update(dr); 
                }
                taPurPaidDet.Update(dt);
                tran.Commit();
                dm.UpdateAutoNum(19);

                retval = "1";
            }
            catch (Exception ex)
            {
                tran.Rollback();
                retval = ex.ToString();
            }
            return retval;
        }
 public int DoUpdateInv(string pur,string supid,string supname,string invno,DateTime invdate,int duedays)
 {
     var ta = new PURINVMAINTableAdapter {Connection = {ConnectionString = _constr}};
     var taapar = new LedgersTableAdapters.APARMAINTableAdapter { Connection = { ConnectionString = _constr } };
     var taled = new LedgersTableAdapters.LEDGERSTableAdapter { Connection = { ConnectionString = _constr } };
     taapar.UpdateInvoiceDet(supid, invdate, invno, Utils.Company, pur);
     taled.UpdateInvoiceDet(supid, invdate, invno, Utils.Company, pur);
   return  ta.UpdateInvDetails(supid, supname, invno, invdate, duedays, Utils.Userid, DateTime.Now, pur);
 }
        public string DoSave(DsPurchase.PURINVMAINRow drow, DsPurchase.PURINVDETAILDataTable purinvdetail,bool issale=false, string tostore = "")
        {
            var dm = new DataManager();
            string retval;
            SqlTransaction tran = null;
            var isnew = false;
            try
            {
                var cn = new SqlConnection(_constr);
                cn.Open();
                tran = cn.BeginTransaction(IsolationLevel.ReadUncommitted);
                var ds = new DsPurchase();
                var dt = ds.PURINVMAIN;

                var taPurMain = new PURINVMAINTableAdapter();
                var taPurDetail = new PURINVDETAILTableAdapter();
                var taStockMaster = new STOCKMASTERTableAdapter();

                var taPriceMaster = new DsMasterTableAdapters.PRICEMASTERTableAdapter();

                var taItemMaster = new DsMasterTableAdapters.ITEMMASTTableAdapter();

                var taapar = new LedgersTableAdapters.APARMAINTableAdapter();
                var taledger = new LedgersTableAdapters.LEDGERSTableAdapter();

                taPurMain.Connection.ConnectionString = _constr;

                taPurMain.FillByPurid(ds.PURINVMAIN, drow.PURINVID);

            //  taPurMain.Fill(ds.PURINVMAIN);
           //   Assaign Transaction

                taPurMain.AssignConnection(cn, tran);
                taPurDetail.AssignConnection(cn, tran);
                taStockMaster.AssignConnection(cn, tran);
                taPriceMaster.AssignConnection(cn, tran);

                taItemMaster.AssignConnection(cn, tran);
                taapar.AssignConnection(cn, tran);
                taledger.AssignConnection(cn, tran);

                var dr = dt.FindByPURINVID(drow.PURINVID);
                if (dr != null)
                { dr.BeginEdit(); }
                else
                { dr = (DsPurchase.PURINVMAINRow)dt.NewRow(); }
                if (string.IsNullOrEmpty(drow.PURINVID))
                {
                    var autoid = dm.GetAutoId(1);
                    autoid++;
                    Utils.Purinvid = dm.CreateFormatedAutoNo(autoid);
                    dr.PURINVID = Utils.Purinvid;
                    dr.USERID = Utils.Userid;
                    dr.COMPANY = Utils.Company;
                    dr.UDATE = DateTime.Now;
                    isnew = true;
                }
                else
                {
                    Utils.Purinvid = drow.PURINVID;
                    dr.EDATE = DateTime.Now;
                    dr.EUSERID = Utils.Userid;
                }
                dr.SUPID = drow.SUPID;
                dr.SUPNAME = drow.SUPNAME;
                dr.INVDATE = drow.INVDATE;
                dr.INVNO = drow.INVNO;
                dr.DUEDAYS = drow.DUEDAYS;
                dr.SPDISCOUNT = drow.SPDISCOUNT;
                dr.WHID = drow.WHID;
                dr.ADJVALUE = drow.ADJVALUE;
                dr.NETVALUE = drow.NETVALUE;
                dr.HOLD = drow.HOLD;
                if (isnew)
                {
                    dt.Rows.Add(dr);
                    taPurMain.Update(dt);
                }
                else
                {
                    dr.EndEdit();
                    taPurMain.Update(dr);
                }
                //bool issale = false;//string tostore = string.Empty;

                //STOCK
                if (!drow.HOLD)
                {
                    if (issale)
                    {
                        new StockMaster().CreateStock(ref cn, ref tran, purinvdetail, drow.WHID, "S", true, false);
                    }
                    else
                    {
                        new StockMaster().CreateStock(ref cn, ref tran, purinvdetail, drow.WHID, "P", true, false);
                    }
               //Price Master
                    taPriceMaster.Update(new PriceMaster().UpdatePriceMaster(purinvdetail, drow.SUPID));
               //UPDATE LASTPRICE,LASTEXPIRY,LASTCOSTPRICE 

                //LEDGER
                    taapar.Insert("S", dr.SUPID, DateTime.Now.Date, dr.INVDATE, Utils.Purinvid, dr.INVNO, dr.NETVALUE, 0,
                             dr.NETVALUE, Utils.Company, 1, Utils.Userid, 0);
                    taledger.Insert("S", dr.SUPID, DateTime.Now.Date, dr.INVDATE, Utils.Purinvid, dr.INVNO, 0,
                                dr.NETVALUE, 1, Utils.Company, Utils.Userid,"Purchase Invoice");
                }

                foreach (DsPurchase.PURINVDETAILRow purd in purinvdetail.Rows)
                {
                    if ((purd.RowState != DataRowState.Deleted))
                    {
                        purd.PURINVID = Utils.Purinvid;
                        if (!purd.IsISSUEFORSALENull())
                        {
                            issale = purd.ISSUEFORSALE;
                            tostore = purd.TOSTORE;
                        }
                        // Item Master Price
                        if (!drow.HOLD)
                        {
                            taItemMaster.UpdateCPSP(purd.SPRICE, purd.CPRICE, purd.ITEMNO);
                        }
                    }
                }
                taPurDetail.Update(purinvdetail);
                tran.Commit();
                dm.UpdateAutoNum(1);
                retval = "1";
            }
            catch (Exception ex)
            {
                if (tran != null)
                    tran.Rollback();
                if (isnew)
                    Utils.Purinvid = string.Empty;
                retval = ex.ToString();
            }
            return retval;
        }
Beispiel #7
0
        public string DoSave()
        {
            string retval = "0";
            SqlTransaction tran = null;
            try
            {
                var cn = new SqlConnection { ConnectionString = _constr };
                cn.Open();
                tran = cn.BeginTransaction(IsolationLevel.ReadUncommitted);
                var dm = new DataManager();
                var dt = new Ledgers.APARMAINDataTable();
                var ta = new LedgersTableAdapters.APARMAINTableAdapter {Connection = {ConnectionString = _constr}};
                ta.Fill(dt);
                var taleder = new LedgersTableAdapters.LEDGERSTableAdapter();
                taleder.AssignConnection(cn,tran);
                ta.AssignConnection(cn, tran);

                var tasalesamtdetails = new LedgersTableAdapters.SalesAmountDetailsTableAdapter();

                tasalesamtdetails.AssignConnection(cn, tran);

                
                var autoid =  dm.GetAutoId(17);
                autoid++;
                Utils.CustomerReceipt = dm.CreateFormatedAutoNo(autoid);
                ta.Update(AparmainData);
                var bl = new SalesManger().GetInvoiceNo(17);
                taleder.Insert("C", customerno, trandate,
                                    trandate, Utils.CustomerReceipt,
                                    Utils.CustomerReceipt,0 , AppliedAmt, 17, Utils.Company,
                                    Utils.Userid, "Customer Receipt");
                tasalesamtdetails.Insert(trandate, null, customerno, "C", amountype, AppliedAmt, shiftcode, false, Utils.Company,userid, false, null);

                tran.Commit();
                retval = "1";
            }
            catch (Exception ex)
            {
                retval = ex.ToString();
                if (tran != null) tran.Rollback();
            }
            return retval;

        }
Beispiel #8
0
        public string DoSave()
        {
            SqlTransaction tran = null;
            var retval = "0";
            try
            {
                var dsStockWithItemcodes = GetStockTableWithItemCodes(salesinvdetailData);
                var cn = new SqlConnection(_constr);
                cn.Open();
                tran = cn.BeginTransaction(IsolationLevel.ReadUncommitted);
                var ds = new DsInventory();

                var dt = ds.SALESMAIN;
                var tasalMain = new DsInventoryTableAdapters.SALESMAINTableAdapter();
                var tasalDetail = new DsInventoryTableAdapters.SALESINVDETAILTableAdapter();
                var tasalesamtdetails = new LedgersTableAdapters.SalesAmountDetailsTableAdapter();

                tasalesamtdetails.AssignConnection(cn, tran);
                tasalMain.Connection.ConnectionString = _constr;

               // tasalMain.Fill(dt);
                tasalMain.FillBySINVNO(dt,salesmainRow.SINVNO);
                tasalMain.AssignConnection(cn, tran);
                tasalDetail.AssignConnection(cn, tran);

                var dr = dt.FindBySINVNO(salesmainRow.SINVNO);
                bool isnew = false;
                if (dr != null)
                {
                    dr.BeginEdit();
                    dr.EndEdit();
                }
                else
                {
                    dr = (DsInventory.SALESMAINRow) dt.NewRow();
                    dr.ItemArray = salesmainRow.ItemArray;
                    isnew = true;
                }
                if (isnew)
                {
                    dt.Rows.Add(dr);
                    tasalMain.Update(dt);
                }
                else
                {
                    tasalMain.Update(salesmainRow);
                }
                //salesmainRow.SALESTYPE 
            
                if (salesmainRow.SALESTYPE == "I" || salesmainRow.SALESTYPE == "C")
                {
                    var taaparleder = new LedgersTableAdapters.APARMAINTableAdapter();
                    var taledger = new LedgersTableAdapters.LEDGERSTableAdapter();
                    taledger.AssignConnection(cn, tran);
                    taaparleder.AssignConnection(cn, tran);
                    var netamt = 0m;
                    var totaldiscper = 0m;
                    if (salesmainRow.SALESTYPE == "I")
                    {
                        totaldiscper = salesmainRow.CUSTOMERFIXDISC ;
                        netamt = salesmainRow.CREDITAMT;
                    }
                    else
                    {
                        netamt = salesmainRow.CREDITAMT;
                        totaldiscper = salesmainRow.CUSTOMERFIXDISC ;
                    }
                    if (netamt > 0)
                    {
                        string cdno = "";
                        if (!salesmainRow.IsCARDNONull())
                            cdno = salesmainRow.CARDNO.ToString();
                        taaparleder.Insert("C", salesmainRow.CUSTOMERID, salesmainRow.TRANDATE,
                                           salesmainRow.TRANDATE,
                                           cdno, salesmainRow.SINVNO, netamt, 0, netamt,
                                           salesmainRow.COMPANY,
                                           3, salesmainRow.SALESMAN, totaldiscper);

                        taledger.Insert("C", salesmainRow.CUSTOMERID, salesmainRow.TRANDATE,
                                        salesmainRow.TRANDATE, cdno,
                                        salesmainRow.SINVNO, netamt, 0, 3, salesmainRow.COMPANY,
                                        Utils.Userid, "Sales Invoice");
                    }
                }
                foreach (DsInventory.SALESINVDETAILRow  drstk in salesinvdetailData.Rows)
                {
                    if(!drstk.IsSTOCKIDNull())
                    {
                        if(drstk.STOCKID > 0)
                        {
                            if (drstk.ISRETURN)
                            {
                                 UpdateStockQty(ref cn, ref tran, drstk.STOCKID, (drstk.QTY * -1), drstk);
                            }
                            else
                            {
                                 UpdateStockQty(ref cn, ref tran, drstk.STOCKID, -drstk.QTY, drstk);
                            }
                        }
                        else
                        {
                            if (drstk.ISRETURN)
                            {
                                AddNewItemItemInStock(ref cn, ref tran, drstk, dsStockWithItemcodes);
                            }
                        }
                    }
                }
                tasalDetail.Update(salesinvdetailData);
                tasalesamtdetails.Update(SalesAmount);

                tran.Commit();
                retval = "1";
            }
            catch (Exception ex)
            {
                retval = ex.ToString();
                tran.Rollback();
            }
            return retval;
        }