Ejemplo n.º 1
0
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public string Add(UFIDA.U8.UAP.CustomApp.ControlForm.Model.RdRecord01 model)
        {
            StringBuilder strSql  = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();

            if (model.ID != null)
            {
                strSql1.Append("ID,");
                strSql2.Append("" + model.ID + ",");
            }
            if (model.bRdFlag != null)
            {
                strSql1.Append("bRdFlag,");
                strSql2.Append("" + model.bRdFlag + ",");
            }
            if (model.cVouchType != null)
            {
                strSql1.Append("cVouchType,");
                strSql2.Append("'" + model.cVouchType + "',");
            }
            if (model.cBusType != null)
            {
                strSql1.Append("cBusType,");
                strSql2.Append("'" + model.cBusType + "',");
            }
            if (model.cSource != null)
            {
                strSql1.Append("cSource,");
                strSql2.Append("'" + model.cSource + "',");
            }
            if (model.cBusCode != null)
            {
                strSql1.Append("cBusCode,");
                strSql2.Append("'" + model.cBusCode + "',");
            }
            if (model.cWhCode != null)
            {
                strSql1.Append("cWhCode,");
                strSql2.Append("'" + model.cWhCode + "',");
            }
            if (model.dDate != null)
            {
                strSql1.Append("dDate,");
                strSql2.Append("'" + model.dDate + "',");
            }
            if (model.cCode != null)
            {
                strSql1.Append("cCode,");
                strSql2.Append("'" + model.cCode + "',");
            }
            if (model.cRdCode != null)
            {
                strSql1.Append("cRdCode,");
                strSql2.Append("'" + model.cRdCode + "',");
            }
            if (model.cDepCode != null)
            {
                strSql1.Append("cDepCode,");
                strSql2.Append("'" + model.cDepCode + "',");
            }
            if (model.cPersonCode != null)
            {
                strSql1.Append("cPersonCode,");
                strSql2.Append("'" + model.cPersonCode + "',");
            }
            if (model.cPTCode != null)
            {
                strSql1.Append("cPTCode,");
                strSql2.Append("'" + model.cPTCode + "',");
            }
            if (model.cSTCode != null)
            {
                strSql1.Append("cSTCode,");
                strSql2.Append("'" + model.cSTCode + "',");
            }
            if (model.cCusCode != null)
            {
                strSql1.Append("cCusCode,");
                strSql2.Append("'" + model.cCusCode + "',");
            }
            if (model.cVenCode != null)
            {
                strSql1.Append("cVenCode,");
                strSql2.Append("'" + model.cVenCode + "',");
            }
            if (model.cOrderCode != null)
            {
                strSql1.Append("cOrderCode,");
                strSql2.Append("'" + model.cOrderCode + "',");
            }
            if (model.cARVCode != null)
            {
                strSql1.Append("cARVCode,");
                strSql2.Append("'" + model.cARVCode + "',");
            }
            if (model.cBillCode != null)
            {
                strSql1.Append("cBillCode,");
                strSql2.Append("" + model.cBillCode + ",");
            }
            if (model.cDLCode != null)
            {
                strSql1.Append("cDLCode,");
                strSql2.Append("" + model.cDLCode + ",");
            }
            if (model.cProBatch != null)
            {
                strSql1.Append("cProBatch,");
                strSql2.Append("'" + model.cProBatch + "',");
            }
            if (model.cHandler != null)
            {
                strSql1.Append("cHandler,");
                strSql2.Append("'" + model.cHandler + "',");
            }
            if (model.cMemo != null)
            {
                strSql1.Append("cMemo,");
                strSql2.Append("'" + model.cMemo + "',");
            }
            if (model.bTransFlag != null)
            {
                strSql1.Append("bTransFlag,");
                strSql2.Append("" + (model.bTransFlag ? 1 : 0) + ",");
            }
            if (model.cAccounter != null)
            {
                strSql1.Append("cAccounter,");
                strSql2.Append("'" + model.cAccounter + "',");
            }
            if (model.cMaker != null)
            {
                strSql1.Append("cMaker,");
                strSql2.Append("'" + model.cMaker + "',");
            }
            if (model.cDefine1 != null)
            {
                strSql1.Append("cDefine1,");
                strSql2.Append("'" + model.cDefine1 + "',");
            }
            if (model.cDefine2 != null)
            {
                strSql1.Append("cDefine2,");
                strSql2.Append("'" + model.cDefine2 + "',");
            }
            if (model.cDefine3 != null)
            {
                strSql1.Append("cDefine3,");
                strSql2.Append("'" + model.cDefine3 + "',");
            }
            if (model.cDefine4 != null)
            {
                strSql1.Append("cDefine4,");
                strSql2.Append("'" + model.cDefine4 + "',");
            }
            if (model.cDefine5 != null)
            {
                strSql1.Append("cDefine5,");
                strSql2.Append("" + model.cDefine5 + ",");
            }
            if (model.cDefine6 != null)
            {
                strSql1.Append("cDefine6,");
                strSql2.Append("'" + model.cDefine6 + "',");
            }
            if (model.cDefine7 != null)
            {
                strSql1.Append("cDefine7,");
                strSql2.Append("" + model.cDefine7 + ",");
            }
            if (model.cDefine8 != null)
            {
                strSql1.Append("cDefine8,");
                strSql2.Append("'" + model.cDefine8 + "',");
            }
            if (model.cDefine9 != null)
            {
                strSql1.Append("cDefine9,");
                strSql2.Append("'" + model.cDefine9 + "',");
            }
            if (model.cDefine10 != null)
            {
                strSql1.Append("cDefine10,");
                strSql2.Append("'" + model.cDefine10 + "',");
            }
            if (model.dKeepDate != null)
            {
                strSql1.Append("dKeepDate,");
                strSql2.Append("'" + model.dKeepDate + "',");
            }
            if (model.dVeriDate != null)
            {
                strSql1.Append("dVeriDate,");
                strSql2.Append("'" + model.dVeriDate + "',");
            }
            if (model.bpufirst != null)
            {
                strSql1.Append("bpufirst,");
                strSql2.Append("" + (model.bpufirst ? 1 : 0) + ",");
            }
            if (model.biafirst != null)
            {
                strSql1.Append("biafirst,");
                strSql2.Append("" + (model.biafirst ? 1 : 0) + ",");
            }
            if (model.iMQuantity != null)
            {
                strSql1.Append("iMQuantity,");
                strSql2.Append("" + model.iMQuantity + ",");
            }
            if (model.dARVDate != null)
            {
                strSql1.Append("dARVDate,");
                strSql2.Append("'" + model.dARVDate + "',");
            }
            if (model.cChkCode != null)
            {
                strSql1.Append("cChkCode,");
                strSql2.Append("'" + model.cChkCode + "',");
            }
            if (model.dChkDate != null)
            {
                strSql1.Append("dChkDate,");
                strSql2.Append("'" + model.dChkDate + "',");
            }
            if (model.cChkPerson != null)
            {
                strSql1.Append("cChkPerson,");
                strSql2.Append("'" + model.cChkPerson + "',");
            }
            if (model.VT_ID != null)
            {
                strSql1.Append("VT_ID,");
                strSql2.Append("" + model.VT_ID + ",");
            }
            if (model.bIsSTQc != null)
            {
                strSql1.Append("bIsSTQc,");
                strSql2.Append("" + (model.bIsSTQc ? 1 : 0) + ",");
            }
            if (model.cDefine11 != null)
            {
                strSql1.Append("cDefine11,");
                strSql2.Append("'" + model.cDefine11 + "',");
            }
            if (model.cDefine12 != null)
            {
                strSql1.Append("cDefine12,");
                strSql2.Append("'" + model.cDefine12 + "',");
            }
            if (model.cDefine13 != null)
            {
                strSql1.Append("cDefine13,");
                strSql2.Append("'" + model.cDefine13 + "',");
            }
            if (model.cDefine14 != null)
            {
                strSql1.Append("cDefine14,");
                strSql2.Append("'" + model.cDefine14 + "',");
            }
            if (model.cDefine15 != null)
            {
                strSql1.Append("cDefine15,");
                strSql2.Append("" + model.cDefine15 + ",");
            }
            if (model.cDefine16 != null)
            {
                strSql1.Append("cDefine16,");
                strSql2.Append("" + model.cDefine16 + ",");
            }
            if (model.gspcheck != null)
            {
                strSql1.Append("gspcheck,");
                strSql2.Append("'" + model.gspcheck + "',");
            }
            if (model.ipurorderid != null)
            {
                strSql1.Append("ipurorderid,");
                strSql2.Append("" + model.ipurorderid + ",");
            }
            if (model.ipurarriveid != null)
            {
                strSql1.Append("ipurarriveid,");
                strSql2.Append("" + model.ipurarriveid + ",");
            }
            if (model.iarriveid != null)
            {
                strSql1.Append("iarriveid,");
                strSql2.Append("'" + model.iarriveid + "',");
            }
            if (model.isalebillid != null)
            {
                strSql1.Append("isalebillid,");
                strSql2.Append("'" + model.isalebillid + "',");
            }
            if (model.ufts != null)
            {
                strSql1.Append("ufts,");
                strSql2.Append("" + model.ufts + ",");
            }
            if (model.iTaxRate != null)
            {
                strSql1.Append("iTaxRate,");
                strSql2.Append("" + model.iTaxRate + ",");
            }
            if (model.iExchRate != null)
            {
                strSql1.Append("iExchRate,");
                strSql2.Append("" + model.iExchRate + ",");
            }
            if (model.cExch_Name != null)
            {
                strSql1.Append("cExch_Name,");
                strSql2.Append("'" + model.cExch_Name + "',");
            }
            if (model.bOMFirst != null)
            {
                strSql1.Append("bOMFirst,");
                strSql2.Append("" + (model.bOMFirst ? 1 : 0) + ",");
            }
            if (model.bFromPreYear != null)
            {
                strSql1.Append("bFromPreYear,");
                strSql2.Append("" + (model.bFromPreYear ? 1 : 0) + ",");
            }
            if (model.bIsLsQuery != null)
            {
                strSql1.Append("bIsLsQuery,");
                strSql2.Append("" + (model.bIsLsQuery ? 1 : 0) + ",");
            }
            if (model.bIsComplement != null)
            {
                strSql1.Append("bIsComplement,");
                strSql2.Append("" + model.bIsComplement + ",");
            }
            if (model.iDiscountTaxType != null)
            {
                strSql1.Append("iDiscountTaxType,");
                strSql2.Append("" + model.iDiscountTaxType + ",");
            }
            if (model.ireturncount != null)
            {
                strSql1.Append("ireturncount,");
                strSql2.Append("" + model.ireturncount + ",");
            }
            if (model.iverifystate != null)
            {
                strSql1.Append("iverifystate,");
                strSql2.Append("" + model.iverifystate + ",");
            }
            if (model.iswfcontrolled != null)
            {
                strSql1.Append("iswfcontrolled,");
                strSql2.Append("" + model.iswfcontrolled + ",");
            }
            if (model.cModifyPerson != null)
            {
                strSql1.Append("cModifyPerson,");
                strSql2.Append("'" + model.cModifyPerson + "',");
            }
            if (model.dModifyDate != null)
            {
                strSql1.Append("dModifyDate,");
                strSql2.Append("'" + model.dModifyDate + "',");
            }
            if (model.dnmaketime != null)
            {
                strSql1.Append("dnmaketime,");
                strSql2.Append("'" + model.dnmaketime + "',");
            }
            if (model.dnmodifytime != null)
            {
                strSql1.Append("dnmodifytime,");
                strSql2.Append("'" + model.dnmodifytime + "',");
            }
            if (model.dnverifytime != null)
            {
                strSql1.Append("dnverifytime,");
                strSql2.Append("'" + model.dnverifytime + "',");
            }
            if (model.bredvouch != null)
            {
                strSql1.Append("bredvouch,");
                strSql2.Append("" + model.bredvouch + ",");
            }
            if (model.cVenPUOMProtocol != null)
            {
                strSql1.Append("cVenPUOMProtocol,");
                strSql2.Append("'" + model.cVenPUOMProtocol + "',");
            }
            if (model.dCreditStart != null)
            {
                strSql1.Append("dCreditStart,");
                strSql2.Append("'" + model.dCreditStart + "',");
            }
            if (model.iCreditPeriod != null)
            {
                strSql1.Append("iCreditPeriod,");
                strSql2.Append("" + model.iCreditPeriod + ",");
            }
            if (model.dGatheringDate != null)
            {
                strSql1.Append("dGatheringDate,");
                strSql2.Append("'" + model.dGatheringDate + "',");
            }
            if (model.bCredit != null)
            {
                strSql1.Append("bCredit,");
                strSql2.Append("" + model.bCredit + ",");
            }
            if (model.iFlowId != null)
            {
                strSql1.Append("iFlowId,");
                strSql2.Append("" + model.iFlowId + ",");
            }
            if (model.cPZID != null)
            {
                strSql1.Append("cPZID,");
                strSql2.Append("'" + model.cPZID + "',");
            }
            if (model.cSourceLs != null)
            {
                strSql1.Append("cSourceLs,");
                strSql2.Append("'" + model.cSourceLs + "',");
            }
            if (model.cSourceCodeLs != null)
            {
                strSql1.Append("cSourceCodeLs,");
                strSql2.Append("'" + model.cSourceCodeLs + "',");
            }
            if (model.iPrintCount != null)
            {
                strSql1.Append("iPrintCount,");
                strSql2.Append("" + model.iPrintCount + ",");
            }
            if (model.csysbarcode != null)
            {
                strSql1.Append("csysbarcode,");
                strSql2.Append("'" + model.csysbarcode + "',");
            }
            if (model.cCurrentAuditor != null)
            {
                strSql1.Append("cCurrentAuditor,");
                strSql2.Append("'" + model.cCurrentAuditor + "',");
            }
            strSql.Append("insert into RdRecord01(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            return(strSql.ToString());
        }
Ejemplo n.º 2
0
        private void btnImprot_Click(object sender, EventArgs e)
        {
            try
            {
                gridViewPL.FocusedRowHandle -= 1;
                gridViewPL.FocusedRowHandle += 1;
            }
            catch { }

            try
            {
                if (txtCurrency.Text.Trim() == "")
                {
                    throw new Exception("Please set currency");
                }

                decimal dExch = BaseFunction.ReturnDecimal(txtExchangeRate.Text.Trim());
                if (dExch == 0)
                {
                    throw new Exception("Please set the exchange rate");
                }

                if (lookUpEditWarehouse.EditValue == null || lookUpEditWarehouse.Text.Trim() == "")
                {
                    lookUpEditWarehouse.Focus();
                    throw new Exception("Please choose warehouse");
                }

                int iCou = gridViewPL.RowCount;
                if (iCou == 0)
                {
                    throw new Exception("no data");
                }
                string        sErr   = "";
                int           iCount = 0;
                SqlConnection conn   = new SqlConnection(Conn);
                conn.Open();
                //启用事务
                SqlTransaction tran = conn.BeginTransaction();

                try
                {
                    DateTime dLogDate = BaseFunction.ReturnDate(sLogDate);

                    string sWhCode = lookUpEditWarehouse.Text.Trim();
                    string sRdCode = "101";

                    string    sSQL   = @"
select * from _Code where VouchType = 'RdRecord01'
";
                    DataTable dtCode = DbHelperSQL.ExecuteDataset(tran, CommandType.Text, sSQL).Tables[0];
                    if (dtCode != null && dtCode.Rows.Count > 0)
                    {
                        sRdCode = dtCode.Rows[0]["cRdCodeIn"].ToString().Trim();
                    }

                    //string sDate = dateEdit1.DateTime.ToString("yyyy-MM-dd");
                    string sDate = sLogDate;
                    sSQL = @"
select bflag_ST from gl_mend where iyear = aaaaaa and iperiod = bbbbbb
";
                    sSQL = sSQL.Replace("aaaaaa", dLogDate.Year.ToString());
                    sSQL = sSQL.Replace("bbbbbb", dLogDate.Month.ToString());
                    DataTable dtTemp = DbHelperSQL.ExecuteDataset(tran, CommandType.Text, sSQL).Tables[0];
                    if (BaseFunction.ReturnBool(dtTemp.Rows[0]["bflag_ST"]))
                    {
                        throw new Exception("The current date has been checked out");
                    }

                    string sDateTime = dateEditQuery.DateTime.ToString("yyyy-MM-dd");

//                    sSQL = @"
//select * from exch where cdate = 'aaaaaa'
//";
//                    sSQL = sSQL.Replace("aaaaaa", dtmBL.DateTime.ToString("yyyy.MM.dd"));
//                    DataTable dtExch = DbHelperSQL.ExecuteDataset(tran, CommandType.Text, sSQL).Tables[0];
//                    if (dtExch == null || dtExch.Rows.Count == 0)
//                    {
//                        throw new Exception("Please set exchange rate");
//                    }

                    long lID  = 0;
                    long lIDs = 0;
                    sSQL   = @"
declare @p5 int
set @p5=0
declare @p6 int
set @p6=0
exec sp_GetId N'',N'@AccID',N'rd',@iCou,@p5 output,@p6 output,default
select @p5, @p6
";
                    sSQL   = sSQL.Replace("@iCou", iCou.ToString());
                    sSQL   = sSQL.Replace("@AccID", sAccID);
                    dtTemp = DbHelperSQL.ExecuteDataset(tran, CommandType.Text, sSQL).Tables[0];
                    lID    = BaseFunction.ReturnLong(dtTemp.Rows[0][0]);
                    lIDs   = BaseFunction.ReturnLong(dtTemp.Rows[0][1]) - iCou;

                    sSQL   = @"
select * From VoucherHistory  with (ROWLOCK)  Where  CardNumber='24' and cContent='日期' and cSeed='aaaaaa'
";
                    sSQL   = sSQL.Replace("aaaaaa", dLogDate.ToString("yyyyMMdd"));
                    dtTemp = DbHelperSQL.ExecuteDataset(tran, CommandType.Text, sSQL).Tables[0];

                    long iCode = 0;
                    if (dtTemp != null && dtTemp.Rows.Count > 0)
                    {
                        iCode = BaseFunction.ReturnLong(dtTemp.Rows[0]["cNumber"]) + 1;
                        sSQL  = @"
update VoucherHistory set cNumber = aaaaaa where autoid = bbbbbb and CardNumber = '24'
";
                        sSQL  = sSQL.Replace("aaaaaa", iCode.ToString());
                        sSQL  = sSQL.Replace("bbbbbb", dtTemp.Rows[0]["AutoId"].ToString().Trim());
                        DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);
                    }
                    else
                    {
                        iCode = 1;
                        sSQL  = @"
insert into VoucherHistory(CardNumber, cContent, cContentRule, cSeed, cNumber, bEmpty)
values('24','日期','日','aaaaaa',1,0)
";
                        sSQL  = sSQL.Replace("aaaaaa", dLogDate.ToString("yyyyMMdd"));
                        DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);
                    }

                    sSQL = @"
select * from PO_POMain where cPOID = 'aaaaaa'
";
                    sSQL = sSQL.Replace("aaaaaa", gridViewPL.GetRowCellValue(0, gridColPONO).ToString().Trim());
                    DataTable dtPO = DbHelperSQL.ExecuteDataset(tran, CommandType.Text, sSQL).Tables[0];

                    Model.RdRecord01 mod = new UFIDA.U8.UAP.CustomApp.ControlForm.Model.RdRecord01();
                    mod.ID         = lID;
                    mod.bRdFlag    = 1;
                    mod.cVouchType = "01";
                    mod.cBusType   = "普通采购";
                    mod.cSource    = "采购订单";
                    //mod.cBusCode =
                    mod.cWhCode = sWhCode;               //在途仓库
                    mod.dDate   = dLogDate;

                    //DataRow[] drExch = dtExch.Select("cexch_name = '" + txtCurrency.Text.Trim() + "'");
                    //if (drExch.Length == 0)
                    //{
                    //    throw new Exception("Please set exchange rate");
                    //}
                    mod.iExchRate = dExch;

                    string sCode = iCode.ToString();
                    while (sCode.Length < 4)
                    {
                        sCode = "0" + sCode;
                    }
                    sCode        = "RC" + dLogDate.ToString("yyyyMMdd") + sCode;
                    mod.cCode    = sCode;
                    mod.cRdCode  = sRdCode;
                    mod.cDepCode = "LOG";
                    mod.cPTCode  = dtPO.Rows[0]["cPTCode"].ToString().Trim();
                    mod.cVenCode = dtPO.Rows[0]["cVenCode"].ToString().Trim();
                    //mod.cOrderCode = dtPO.Rows[0]["cPOID"].ToString().Trim();
                    mod.bTransFlag       = false;
                    mod.cMaker           = sUserName;
                    mod.cDefine10        = txtInvoiceNO.Text.Trim();
                    mod.bpufirst         = false;
                    mod.biafirst         = false;
                    mod.VT_ID            = 131313;
                    mod.bIsSTQc          = false;
                    mod.ipurorderid      = BaseFunction.ReturnLong(dtPO.Rows[0]["POID"]);
                    mod.iTaxRate         = BaseFunction.ReturnDecimal(dtPO.Rows[0]["iTaxRate"]);
                    mod.cExch_Name       = dtPO.Rows[0]["cExch_Name"].ToString().Trim();
                    mod.bOMFirst         = false;
                    mod.bFromPreYear     = false;
                    mod.bIsComplement    = 0;
                    mod.iDiscountTaxType = 0;
                    mod.ireturncount     = 0;
                    mod.iverifystate     = 0;
                    mod.iswfcontrolled   = 0;
                    mod.dnmaketime       = DateTime.Now;
                    mod.bredvouch        = 0;
                    mod.bCredit          = 0;
                    mod.iPrintCount      = 0;
                    mod.csysbarcode      = "||st01|" + mod.cCode;

                    mod.cDefine1  = txtBL.Text.Trim();
                    mod.cDefine10 = txtInvoiceNO.Text.Trim();

                    //导入单据审核
                    mod.dVeriDate    = mod.dDate;
                    mod.dnverifytime = DateTime.Now;
                    mod.cHandler     = mod.cMaker;

                    DAL.RdRecord01 dal = new UFIDA.U8.UAP.CustomApp.ControlForm.DAL.RdRecord01();
                    sSQL = dal.Add(mod);
                    DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);

                    for (int i = 0; i < gridViewPL.RowCount; i++)
                    {
                        if (!BaseFunction.ReturnBool(gridViewPL.GetRowCellValue(i, gridColSelected)))
                        {
                            continue;
                        }

                        //判断是否已经生成了采购入库单
                        long lRds01Autoid = BaseFunction.ReturnLong(gridViewPL.GetRowCellValue(i, gridColRds01ID));
                        if (lRds01Autoid > 0)
                        {
                            sSQL = @"
select autoid from Rdrecords01 where autoid = aaaaaaaa
";
                            sSQL = sSQL.Replace("aaaaaaaa", lRds01Autoid.ToString());
                            DataTable dtRds01 = DbHelperSQL.ExecuteDataset(tran, CommandType.Text, sSQL).Tables[0];

                            if (dtRds01 != null && dtRds01.Rows.Count > 0)
                            {
                                sErr = sErr + "Line " + (i + 1).ToString() + " is imported\n";
                                continue;
                            }
                        }

                        string sPONO    = gridViewPL.GetRowCellValue(i, gridColPONO).ToString().Trim();
                        string sInvCode = gridViewPL.GetRowCellValue(i, gridColPARTNO).ToString().Trim();

                        //同一个订单相同子件只能有一个
                        sSQL = @"
select a.cPOID,a.POID,a.cexch_name ,a.cPTCode,a.cVenCode ,a.cBusType ,a.cDepCode 
    ,b.ID,b.cInvCode,b.iQuantity,isnull(b.freceivedqty ,0) as freceivedqty ,b.irowno ,b.ivouchrowno ,isnull(b.fPoArrQuantity ,0) as fPoArrQuantity 
    ,isnull(b.iReceivedQTY ,0) as iReceivedQTY ,isnull(b.iInvQTY ,0) as iInvQTY ,b.iPerTaxRate ,isnull(b.iArrQTY ,0) as iArrQTY 
    ,b.iPerTaxRate 

from PO_Pomain a inner join PO_Podetails b on a.POID = b.POID
where a.cPOID = 'aaaaaa' and b.cInvCode = 'bbbbbb'
";
                        sSQL = sSQL.Replace("aaaaaa", sPONO);
                        sSQL = sSQL.Replace("bbbbbb", sInvCode);
                        DataTable dtPOs = DbHelperSQL.ExecuteDataset(tran, CommandType.Text, sSQL).Tables[0];
                        if (dtPOs == null || dtPOs.Rows.Count == 0)
                        {
                            throw new Exception("Not found the corresponding purchase order");
                        }

                        Model.rdrecords01 mods = new UFIDA.U8.UAP.CustomApp.ControlForm.Model.rdrecords01();
                        lIDs         += 1;
                        mods.AutoID   = lIDs;
                        mods.ID       = mod.ID;
                        mods.cInvCode = sInvCode;

                        decimal dQty    = BaseFunction.ReturnDecimal(gridViewPL.GetRowCellValue(i, gridColQUANTITY));
                        decimal dPOQty  = BaseFunction.ReturnDecimal(dtPOs.Rows[0]["iQuantity"]);
                        decimal dArrQty = BaseFunction.ReturnDecimal(dtPOs.Rows[0]["iReceivedQTY"]);

                        mods.iTaxRate     = BaseFunction.ReturnDecimal(dtPOs.Rows[0]["iPerTaxRate"]);
                        mods.iQuantity    = dQty;
                        mods.iOriCost     = BaseFunction.ReturnDecimal(gridViewPL.GetRowCellValue(i, gridColUnitPrice)); //原币无税单价
                        mods.iOriTaxCost  = BaseFunction.ReturnDecimal(mods.iOriCost * (1 + mods.iTaxRate / 100));       //原币含税单价
                        mods.iOriMoney    = BaseFunction.ReturnDecimal(mods.iOriCost * mods.iQuantity, 2);               //原币无税金额
                        mods.ioriSum      = BaseFunction.ReturnDecimal(mods.iOriTaxCost * mods.iQuantity, 2);            //原币含税金额
                        mods.iOriTaxPrice = mods.ioriSum - mods.iOriMoney;                                               //税额

                        mods.iUnitCost = BaseFunction.ReturnDecimal(mods.iOriCost * mod.iExchRate);                      //单价
                        mods.iPrice    = BaseFunction.ReturnDecimal(mods.iUnitCost * dQty, 2);                           //金额
                        mods.iSum      = BaseFunction.ReturnDecimal(mods.ioriSum * mod.iExchRate, 2);                    //本币价税合计
                        mods.iTaxPrice = mods.iSum - mods.iPrice;                                                        //本币税额
                        mods.bTaxCost  = true;

                        mods.fACost  = mods.iUnitCost;                                                                  //暂估单价
                        mods.iAPrice = mods.iSum;                                                                       //暂估金额

                        mods.iMoney = 0;                                                                                //累计结算金额

                        mods.iFlag             = 0;
                        mods.iSQuantity        = 0;
                        mods.iSNum             = 0;
                        mods.iPOsID            = BaseFunction.ReturnLong(dtPOs.Rows[0]["ID"]);
                        mods.iNQuantity        = dQty;
                        mods.chVencode         = dtPOs.Rows[0]["cVenCode"].ToString().Trim();
                        mods.cPOID             = dtPOs.Rows[0]["cPOID"].ToString().Trim();
                        mods.iMatSettleState   = 0;
                        mods.iBillSettleCount  = 0;
                        mods.bLPUseFree        = false;
                        mods.iOriTrackID       = 0;
                        mods.bCosting          = true;
                        mods.iExpiratDateCalcu = 0;
                        mods.isotype           = 0;
                        mods.irowno            = i + 1;
                        mods.cbsysbarcode      = "||st01|" + mod.cCode + "|" + mods.irowno.ToString();
                        mods.cDefine22         = gridViewPL.GetRowCellValue(i, gridColCONTAINERNO).ToString().Trim();
                        mods.cDefine23         = gridViewPL.GetRowCellValue(i, gridColBOXNO).ToString().Trim();
                        mods.cDefine24         = gridViewPL.GetRowCellValue(i, gridColCASENO).ToString().Trim();
                        mods.cDefine25         = gridViewPL.GetRowCellValue(i, gridColGWKGS).ToString().Trim();
                        mods.cDefine28         = gridViewPL.GetRowCellValue(i, gridColNWKGS).ToString().Trim();

                        DAL.rdrecords01 dals = new UFIDA.U8.UAP.CustomApp.ControlForm.DAL.rdrecords01();
                        sSQL    = dals.Add(mods);
                        iCount += DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);

                        sSQL = @"
if exists
    (select * from  CurrentStock where cInvCode = '@cInvCode' and cWhCode = '@cWhCode'
        and isnull(cFree1,'') = @cFree1 
        and isnull(cFree2,'') = @cFree2
        and isnull(cFree3,'') = @cFree3
        and isnull(cFree4,'') = @cFree4
        and isnull(cFree5,'') = @cFree5
        and isnull(cFree6,'') = @cFree6
        and isnull(cFree7,'') = @cFree7
        and isnull(cFree8,'') = @cFree8
        and isnull(cFree9,'') = @cFree9
        and isnull(cFree10,'') = @cFree10
        and isnull(cBatch,'') = @cBatch 
    )
    update  CurrentStock set iQuantity = isnull(iQuantity,0) + @iQuantity  
    where cInvCode = '@cInvCode' and cWhCode = '@cWhCode' 
        and isnull(cFree1,'') = @cFree1 
        and isnull(cFree2,'') = @cFree2
        and isnull(cFree3,'') = @cFree3
        and isnull(cFree4,'') = @cFree4
        and isnull(cFree5,'') = @cFree5
        and isnull(cFree6,'') = @cFree6
        and isnull(cFree7,'') = @cFree7
        and isnull(cFree8,'') = @cFree8
        and isnull(cFree9,'') = @cFree9
        and isnull(cFree10,'') = @cFree10
        and isnull(cBatch,'') = @cBatch 
else 
    begin 
        declare @itemid varchar(20); 
        declare @iCount int;  
        select @iCount=count(itemid) from CurrentStock where cInvCode = '@cInvCode';
        if( @iCount > 0 )
	        select @itemid=itemid from CurrentStock where cInvCode = '@cInvCode';
        else  
            select @itemid=max(itemid+1) from CurrentStock  
            insert into CurrentStock(cWhCode,cInvCode,iQuantity,itemid, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10,cBatch,iSoDid)
            values('@cWhCode','@cInvCode', @iQuantity,isnull(@itemid,1), @cFree1, @cFree2, @cFree3, @cFree4, @cFree5, @cFree6, @cFree7, @cFree8, @cFree9, @cFree10,@cBatch,'') 
    end
";
                        sSQL = sSQL.Replace("@cInvCode", mods.cInvCode);
                        sSQL = sSQL.Replace("@cWhCode", mod.cWhCode);
                        sSQL = sSQL.Replace("@iQuantity", mods.iQuantity.ToString());
                        sSQL = sSQL.Replace("@iNum", mods.iNum.ToString());
                        sSQL = sSQL.Replace("@cFree10", mods.cFree10 == null ? "''" : "'" + mods.cFree10 + "'");
                        sSQL = sSQL.Replace("@cFree1", mods.cFree1 == null ? "''" : "'" + mods.cFree1 + "'");
                        sSQL = sSQL.Replace("@cFree2", mods.cFree2 == null ? "''" : "'" + mods.cFree2 + "'");
                        sSQL = sSQL.Replace("@cFree3", mods.cFree3 == null ? "''" : "'" + mods.cFree3 + "'");
                        sSQL = sSQL.Replace("@cFree4", mods.cFree4 == null ? "''" : "'" + mods.cFree4 + "'");
                        sSQL = sSQL.Replace("@cFree5", mods.cFree5 == null ? "''" : "'" + mods.cFree5 + "'");
                        sSQL = sSQL.Replace("@cFree6", mods.cFree6 == null ? "''" : "'" + mods.cFree6 + "'");
                        sSQL = sSQL.Replace("@cFree7", mods.cFree7 == null ? "''" : "'" + mods.cFree7 + "'");
                        sSQL = sSQL.Replace("@cFree8", mods.cFree8 == null ? "''" : "'" + mods.cFree8 + "'");
                        sSQL = sSQL.Replace("@cFree9", mods.cFree9 == null ? "''" : "'" + mods.cFree9 + "'");
                        sSQL = sSQL.Replace("@cBatch", mods.cBatch == null ? "''" : "'" + mods.cBatch + "'");
                        DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);

                        //回写导入中间表 Rdrecords10_Import
                        sSQL = @"
update RdRecords01_Import set Rds01ID = AAAAAA
WHERE AUTOID = BBBBBB
";
                        sSQL = sSQL.Replace("AAAAAA", mods.AutoID.ToString());
                        sSQL = sSQL.Replace("BBBBBB", gridViewPL.GetRowCellValue(i, gridColAutoID).ToString());
                        int iRet = DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);
                        if (iRet != 1)
                        {
                            sErr = sErr + "Row " + (i + 1) + " update dl err\n";
                        }

                        //回写采购订单
                        sSQL = @"
update PO_Podetails set iReceivedQTY = isnull(iReceivedQTY,0) + aaaaaa
where ID = bbbbbb
";
                        sSQL = sSQL.Replace("aaaaaa", mods.iQuantity.ToString());
                        sSQL = sSQL.Replace("bbbbbb", mods.iPOsID.ToString());
                        DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);
                    }

                    sSQL = @"
exec ST_SaveForStock N'01',N'aaaaaa',1,0 ,1
";
                    sSQL = sSQL.Replace("aaaaaa", mod.ID.ToString());
                    DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);

                    sSQL = @"
exec ST_SaveForTrackStock N'01',N'aaaaaa', 0 ,1
";
                    sSQL = sSQL.Replace("aaaaaa", mod.ID.ToString());
                    DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);

                    sSQL = @"
exec IA_SP_WriteUnAccountVouchForST 'aaaaaa',N'01'
";
                    sSQL = sSQL.Replace("aaaaaa", mod.ID.ToString());
                    DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);

                    if (sErr != "")
                    {
                        throw new Exception(sErr);
                    }

                    if (iCount > 0)
                    {
                        tran.Commit();
                        MessageBox.Show("Success:" + mod.cCode);

                        SetTxtNull();
                    }
                    else
                    {
                        throw new Exception("Save failed,no data");
                    }
                }
                catch (Exception error)
                {
                    tran.Rollback();
                    throw new Exception(error.Message);
                }
            }
            catch (Exception ee)
            {
                FrmMsgBox frm = new FrmMsgBox();
                frm.richTextBox1.Text = ee.Message;
                frm.ShowDialog();
            }
        }