Esempio n. 1
0
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public string Add(DllForService.Model.rdrecords32 model)
        {
            StringBuilder strSql  = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();

            if (model.AutoID != null)
            {
                strSql1.Append("AutoID,");
                strSql2.Append("" + model.AutoID + ",");
            }
            if (model.ID != null)
            {
                strSql1.Append("ID,");
                strSql2.Append("" + model.ID + ",");
            }
            if (model.cInvCode != null)
            {
                strSql1.Append("cInvCode,");
                strSql2.Append("'" + model.cInvCode + "',");
            }
            if (model.iNum != null)
            {
                strSql1.Append("iNum,");
                strSql2.Append("" + model.iNum + ",");
            }
            if (model.iQuantity != null)
            {
                strSql1.Append("iQuantity,");
                strSql2.Append("" + model.iQuantity + ",");
            }
            if (model.iUnitCost != null)
            {
                strSql1.Append("iUnitCost,");
                strSql2.Append("" + model.iUnitCost + ",");
            }
            if (model.iPrice != null)
            {
                strSql1.Append("iPrice,");
                strSql2.Append("" + model.iPrice + ",");
            }
            if (model.iAPrice != null)
            {
                strSql1.Append("iAPrice,");
                strSql2.Append("" + model.iAPrice + ",");
            }
            if (model.iPUnitCost != null)
            {
                strSql1.Append("iPUnitCost,");
                strSql2.Append("" + model.iPUnitCost + ",");
            }
            if (model.iPPrice != null)
            {
                strSql1.Append("iPPrice,");
                strSql2.Append("" + model.iPPrice + ",");
            }
            if (model.cBatch != null)
            {
                strSql1.Append("cBatch,");
                strSql2.Append("'" + model.cBatch + "',");
            }
            if (model.cVouchCode != null)
            {
                strSql1.Append("cVouchCode,");
                strSql2.Append("" + model.cVouchCode + ",");
            }
            if (model.cInVouchCode != null)
            {
                strSql1.Append("cInVouchCode,");
                strSql2.Append("'" + model.cInVouchCode + "',");
            }
            if (model.cinvouchtype != null)
            {
                strSql1.Append("cinvouchtype,");
                strSql2.Append("'" + model.cinvouchtype + "',");
            }
            if (model.iSOutQuantity != null)
            {
                strSql1.Append("iSOutQuantity,");
                strSql2.Append("" + model.iSOutQuantity + ",");
            }
            if (model.iSOutNum != null)
            {
                strSql1.Append("iSOutNum,");
                strSql2.Append("" + model.iSOutNum + ",");
            }
            if (model.coutvouchid != null)
            {
                strSql1.Append("coutvouchid,");
                strSql2.Append("" + model.coutvouchid + ",");
            }
            if (model.coutvouchtype != null)
            {
                strSql1.Append("coutvouchtype,");
                strSql2.Append("'" + model.coutvouchtype + "',");
            }
            if (model.iSRedOutQuantity != null)
            {
                strSql1.Append("iSRedOutQuantity,");
                strSql2.Append("" + model.iSRedOutQuantity + ",");
            }
            if (model.iSRedOutNum != null)
            {
                strSql1.Append("iSRedOutNum,");
                strSql2.Append("" + model.iSRedOutNum + ",");
            }
            if (model.cFree1 != null)
            {
                strSql1.Append("cFree1,");
                strSql2.Append("'" + model.cFree1 + "',");
            }
            if (model.cFree2 != null)
            {
                strSql1.Append("cFree2,");
                strSql2.Append("'" + model.cFree2 + "',");
            }
            if (model.iFlag != null)
            {
                strSql1.Append("iFlag,");
                strSql2.Append("" + model.iFlag + ",");
            }
            if (model.iFNum != null)
            {
                strSql1.Append("iFNum,");
                strSql2.Append("" + model.iFNum + ",");
            }
            if (model.iFQuantity != null)
            {
                strSql1.Append("iFQuantity,");
                strSql2.Append("" + model.iFQuantity + ",");
            }
            if (model.dVDate != null)
            {
                strSql1.Append("dVDate,");
                strSql2.Append("'" + model.dVDate + "',");
            }
            if (model.cPosition != null)
            {
                strSql1.Append("cPosition,");
                strSql2.Append("'" + model.cPosition + "',");
            }
            if (model.cDefine22 != null)
            {
                strSql1.Append("cDefine22,");
                strSql2.Append("'" + model.cDefine22 + "',");
            }
            if (model.cDefine23 != null)
            {
                strSql1.Append("cDefine23,");
                strSql2.Append("'" + model.cDefine23 + "',");
            }
            if (model.cDefine24 != null)
            {
                strSql1.Append("cDefine24,");
                strSql2.Append("'" + model.cDefine24 + "',");
            }
            if (model.cDefine25 != null)
            {
                strSql1.Append("cDefine25,");
                strSql2.Append("'" + model.cDefine25 + "',");
            }
            if (model.cDefine26 != null)
            {
                strSql1.Append("cDefine26,");
                strSql2.Append("" + model.cDefine26 + ",");
            }
            if (model.cDefine27 != null)
            {
                strSql1.Append("cDefine27,");
                strSql2.Append("" + model.cDefine27 + ",");
            }
            if (model.cItem_class != null)
            {
                strSql1.Append("cItem_class,");
                strSql2.Append("'" + model.cItem_class + "',");
            }
            if (model.cItemCode != null)
            {
                strSql1.Append("cItemCode,");
                strSql2.Append("'" + model.cItemCode + "',");
            }
            if (model.iDLsID != null)
            {
                strSql1.Append("iDLsID,");
                strSql2.Append("" + model.iDLsID + ",");
            }
            if (model.iSBsID != null)
            {
                strSql1.Append("iSBsID,");
                strSql2.Append("" + model.iSBsID + ",");
            }
            if (model.iSendQuantity != null)
            {
                strSql1.Append("iSendQuantity,");
                strSql2.Append("" + model.iSendQuantity + ",");
            }
            if (model.iSendNum != null)
            {
                strSql1.Append("iSendNum,");
                strSql2.Append("" + model.iSendNum + ",");
            }
            if (model.iEnsID != null)
            {
                strSql1.Append("iEnsID,");
                strSql2.Append("" + model.iEnsID + ",");
            }
            if (model.cName != null)
            {
                strSql1.Append("cName,");
                strSql2.Append("'" + model.cName + "',");
            }
            if (model.cItemCName != null)
            {
                strSql1.Append("cItemCName,");
                strSql2.Append("'" + model.cItemCName + "',");
            }
            if (model.cFree3 != null)
            {
                strSql1.Append("cFree3,");
                strSql2.Append("'" + model.cFree3 + "',");
            }
            if (model.cFree4 != null)
            {
                strSql1.Append("cFree4,");
                strSql2.Append("'" + model.cFree4 + "',");
            }
            if (model.cFree5 != null)
            {
                strSql1.Append("cFree5,");
                strSql2.Append("'" + model.cFree5 + "',");
            }
            if (model.cFree6 != null)
            {
                strSql1.Append("cFree6,");
                strSql2.Append("'" + model.cFree6 + "',");
            }
            if (model.cFree7 != null)
            {
                strSql1.Append("cFree7,");
                strSql2.Append("'" + model.cFree7 + "',");
            }
            if (model.cFree8 != null)
            {
                strSql1.Append("cFree8,");
                strSql2.Append("'" + model.cFree8 + "',");
            }
            if (model.cFree9 != null)
            {
                strSql1.Append("cFree9,");
                strSql2.Append("'" + model.cFree9 + "',");
            }
            if (model.cFree10 != null)
            {
                strSql1.Append("cFree10,");
                strSql2.Append("'" + model.cFree10 + "',");
            }
            if (model.cBarCode != null)
            {
                strSql1.Append("cBarCode,");
                strSql2.Append("'" + model.cBarCode + "',");
            }
            if (model.iNQuantity != null)
            {
                strSql1.Append("iNQuantity,");
                strSql2.Append("" + model.iNQuantity + ",");
            }
            if (model.iNNum != null)
            {
                strSql1.Append("iNNum,");
                strSql2.Append("" + model.iNNum + ",");
            }
            if (model.cAssUnit != null)
            {
                strSql1.Append("cAssUnit,");
                strSql2.Append("'" + model.cAssUnit + "',");
            }
            if (model.dMadeDate != null)
            {
                strSql1.Append("dMadeDate,");
                strSql2.Append("'" + model.dMadeDate + "',");
            }
            if (model.iMassDate != null)
            {
                strSql1.Append("iMassDate,");
                strSql2.Append("" + model.iMassDate + ",");
            }
            if (model.cDefine28 != null)
            {
                strSql1.Append("cDefine28,");
                strSql2.Append("'" + model.cDefine28 + "',");
            }
            if (model.cDefine29 != null)
            {
                strSql1.Append("cDefine29,");
                strSql2.Append("'" + model.cDefine29 + "',");
            }
            if (model.cDefine30 != null)
            {
                strSql1.Append("cDefine30,");
                strSql2.Append("'" + model.cDefine30 + "',");
            }
            if (model.cDefine31 != null)
            {
                strSql1.Append("cDefine31,");
                strSql2.Append("'" + model.cDefine31 + "',");
            }
            if (model.cDefine32 != null)
            {
                strSql1.Append("cDefine32,");
                strSql2.Append("'" + model.cDefine32 + "',");
            }
            if (model.cDefine33 != null)
            {
                strSql1.Append("cDefine33,");
                strSql2.Append("'" + model.cDefine33 + "',");
            }
            if (model.cDefine34 != null)
            {
                strSql1.Append("cDefine34,");
                strSql2.Append("" + model.cDefine34 + ",");
            }
            if (model.cDefine35 != null)
            {
                strSql1.Append("cDefine35,");
                strSql2.Append("" + model.cDefine35 + ",");
            }
            if (model.cDefine36 != null)
            {
                strSql1.Append("cDefine36,");
                strSql2.Append("'" + model.cDefine36 + "',");
            }
            if (model.cDefine37 != null)
            {
                strSql1.Append("cDefine37,");
                strSql2.Append("'" + model.cDefine37 + "',");
            }
            if (model.iCheckIds != null)
            {
                strSql1.Append("iCheckIds,");
                strSql2.Append("" + model.iCheckIds + ",");
            }
            if (model.cBVencode != null)
            {
                strSql1.Append("cBVencode,");
                strSql2.Append("'" + model.cBVencode + "',");
            }
            if (model.bGsp != null)
            {
                strSql1.Append("bGsp,");
                strSql2.Append("" + (model.bGsp ? 1 : 0) + ",");
            }
            if (model.cGspState != null)
            {
                strSql1.Append("cGspState,");
                strSql2.Append("'" + model.cGspState + "',");
            }
            if (model.cCheckCode != null)
            {
                strSql1.Append("cCheckCode,");
                strSql2.Append("'" + model.cCheckCode + "',");
            }
            if (model.iCheckIdBaks != null)
            {
                strSql1.Append("iCheckIdBaks,");
                strSql2.Append("" + model.iCheckIdBaks + ",");
            }
            if (model.cRejectCode != null)
            {
                strSql1.Append("cRejectCode,");
                strSql2.Append("'" + model.cRejectCode + "',");
            }
            if (model.iRejectIds != null)
            {
                strSql1.Append("iRejectIds,");
                strSql2.Append("" + model.iRejectIds + ",");
            }
            if (model.cCheckPersonCode != null)
            {
                strSql1.Append("cCheckPersonCode,");
                strSql2.Append("'" + model.cCheckPersonCode + "',");
            }
            if (model.dCheckDate != null)
            {
                strSql1.Append("dCheckDate,");
                strSql2.Append("'" + model.dCheckDate + "',");
            }
            if (model.cMassUnit != null)
            {
                strSql1.Append("cMassUnit,");
                strSql2.Append("" + model.cMassUnit + ",");
            }
            if (model.iRefundInspectFlag != null)
            {
                strSql1.Append("iRefundInspectFlag,");
                strSql2.Append("" + model.iRefundInspectFlag + ",");
            }
            if (model.strContractId != null)
            {
                strSql1.Append("strContractId,");
                strSql2.Append("'" + model.strContractId + "',");
            }
            if (model.strCode != null)
            {
                strSql1.Append("strCode,");
                strSql2.Append("'" + model.strCode + "',");
            }
            if (model.bChecked != null)
            {
                strSql1.Append("bChecked,");
                strSql2.Append("" + (model.bChecked ? 1 : 0) + ",");
            }
            if (model.iEqDID != null)
            {
                strSql1.Append("iEqDID,");
                strSql2.Append("" + model.iEqDID + ",");
            }
            if (model.bLPUseFree != null)
            {
                strSql1.Append("bLPUseFree,");
                strSql2.Append("" + (model.bLPUseFree ? 1 : 0) + ",");
            }
            if (model.iRSRowNO != null)
            {
                strSql1.Append("iRSRowNO,");
                strSql2.Append("" + model.iRSRowNO + ",");
            }
            if (model.iOriTrackID != null)
            {
                strSql1.Append("iOriTrackID,");
                strSql2.Append("" + model.iOriTrackID + ",");
            }
            if (model.coritracktype != null)
            {
                strSql1.Append("coritracktype,");
                strSql2.Append("'" + model.coritracktype + "',");
            }
            if (model.cbaccounter != null)
            {
                strSql1.Append("cbaccounter,");
                strSql2.Append("'" + model.cbaccounter + "',");
            }
            if (model.dbKeepDate != null)
            {
                strSql1.Append("dbKeepDate,");
                strSql2.Append("'" + model.dbKeepDate + "',");
            }
            if (model.bCosting != null)
            {
                strSql1.Append("bCosting,");
                strSql2.Append("" + (model.bCosting ? 1 : 0) + ",");
            }
            if (model.bVMIUsed != null)
            {
                strSql1.Append("bVMIUsed,");
                strSql2.Append("" + (model.bVMIUsed ? 1 : 0) + ",");
            }
            if (model.iVMISettleQuantity != null)
            {
                strSql1.Append("iVMISettleQuantity,");
                strSql2.Append("" + model.iVMISettleQuantity + ",");
            }
            if (model.iVMISettleNum != null)
            {
                strSql1.Append("iVMISettleNum,");
                strSql2.Append("" + model.iVMISettleNum + ",");
            }
            if (model.cvmivencode != null)
            {
                strSql1.Append("cvmivencode,");
                strSql2.Append("'" + model.cvmivencode + "',");
            }
            if (model.iInvSNCount != null)
            {
                strSql1.Append("iInvSNCount,");
                strSql2.Append("" + model.iInvSNCount + ",");
            }
            if (model.cwhpersoncode != null)
            {
                strSql1.Append("cwhpersoncode,");
                strSql2.Append("'" + model.cwhpersoncode + "',");
            }
            if (model.cwhpersonname != null)
            {
                strSql1.Append("cwhpersonname,");
                strSql2.Append("'" + model.cwhpersonname + "',");
            }
            if (model.cserviceoid != null)
            {
                strSql1.Append("cserviceoid,");
                strSql2.Append("'" + model.cserviceoid + "',");
            }
            if (model.cbserviceoid != null)
            {
                strSql1.Append("cbserviceoid,");
                strSql2.Append("'" + model.cbserviceoid + "',");
            }
            if (model.iinvexchrate != null)
            {
                strSql1.Append("iinvexchrate,");
                strSql2.Append("" + model.iinvexchrate + ",");
            }
            if (model.cbdlcode != null)
            {
                strSql1.Append("cbdlcode,");
                strSql2.Append("'" + model.cbdlcode + "',");
            }
            if (model.corufts != null)
            {
                strSql1.Append("corufts,");
                strSql2.Append("'" + model.corufts + "',");
            }
            if (model.strContractGUID != null)
            {
                strSql1.Append("strContractGUID,");
                strSql2.Append("'" + Guid.NewGuid().ToString() + "',");
            }
            if (model.iExpiratDateCalcu != null)
            {
                strSql1.Append("iExpiratDateCalcu,");
                strSql2.Append("" + model.iExpiratDateCalcu + ",");
            }
            if (model.cExpirationdate != null)
            {
                strSql1.Append("cExpirationdate,");
                strSql2.Append("'" + model.cExpirationdate + "',");
            }
            if (model.dExpirationdate != null)
            {
                strSql1.Append("dExpirationdate,");
                strSql2.Append("'" + model.dExpirationdate + "',");
            }
            if (model.cciqbookcode != null)
            {
                strSql1.Append("cciqbookcode,");
                strSql2.Append("'" + model.cciqbookcode + "',");
            }
            if (model.iBondedSumQty != null)
            {
                strSql1.Append("iBondedSumQty,");
                strSql2.Append("" + model.iBondedSumQty + ",");
            }
            if (model.ccusinvcode != null)
            {
                strSql1.Append("ccusinvcode,");
                strSql2.Append("'" + model.ccusinvcode + "',");
            }
            if (model.ccusinvname != null)
            {
                strSql1.Append("ccusinvname,");
                strSql2.Append("'" + model.ccusinvname + "',");
            }
            if (model.iorderdid != null)
            {
                strSql1.Append("iorderdid,");
                strSql2.Append("" + model.iorderdid + ",");
            }
            if (model.iordertype != null)
            {
                strSql1.Append("iordertype,");
                strSql2.Append("" + model.iordertype + ",");
            }
            if (model.iordercode != null)
            {
                strSql1.Append("iordercode,");
                strSql2.Append("'" + model.iordercode + "',");
            }
            if (model.iorderseq != null)
            {
                strSql1.Append("iorderseq,");
                strSql2.Append("" + model.iorderseq + ",");
            }
            if (model.ipesodid != null)
            {
                strSql1.Append("ipesodid,");
                strSql2.Append("'" + model.ipesodid + "',");
            }
            if (model.ipesotype != null)
            {
                strSql1.Append("ipesotype,");
                strSql2.Append("" + model.ipesotype + ",");
            }
            if (model.cpesocode != null)
            {
                strSql1.Append("cpesocode,");
                strSql2.Append("'" + model.cpesocode + "',");
            }
            if (model.ipesoseq != null)
            {
                strSql1.Append("ipesoseq,");
                strSql2.Append("" + model.ipesoseq + ",");
            }
            if (model.isodid != null)
            {
                strSql1.Append("isodid,");
                strSql2.Append("'" + model.isodid + "',");
            }
            if (model.isotype != null)
            {
                strSql1.Append("isotype,");
                strSql2.Append("" + model.isotype + ",");
            }
            if (model.csocode != null)
            {
                strSql1.Append("csocode,");
                strSql2.Append("'" + model.csocode + "',");
            }
            if (model.isoseq != null)
            {
                strSql1.Append("isoseq,");
                strSql2.Append("" + model.isoseq + ",");
            }
            if (model.cBatchProperty1 != null)
            {
                strSql1.Append("cBatchProperty1,");
                strSql2.Append("" + model.cBatchProperty1 + ",");
            }
            if (model.cBatchProperty2 != null)
            {
                strSql1.Append("cBatchProperty2,");
                strSql2.Append("" + model.cBatchProperty2 + ",");
            }
            if (model.cBatchProperty3 != null)
            {
                strSql1.Append("cBatchProperty3,");
                strSql2.Append("" + model.cBatchProperty3 + ",");
            }
            if (model.cBatchProperty4 != null)
            {
                strSql1.Append("cBatchProperty4,");
                strSql2.Append("" + model.cBatchProperty4 + ",");
            }
            if (model.cBatchProperty5 != null)
            {
                strSql1.Append("cBatchProperty5,");
                strSql2.Append("" + model.cBatchProperty5 + ",");
            }
            if (model.cBatchProperty6 != null)
            {
                strSql1.Append("cBatchProperty6,");
                strSql2.Append("'" + model.cBatchProperty6 + "',");
            }
            if (model.cBatchProperty7 != null)
            {
                strSql1.Append("cBatchProperty7,");
                strSql2.Append("'" + model.cBatchProperty7 + "',");
            }
            if (model.cBatchProperty8 != null)
            {
                strSql1.Append("cBatchProperty8,");
                strSql2.Append("'" + model.cBatchProperty8 + "',");
            }
            if (model.cBatchProperty9 != null)
            {
                strSql1.Append("cBatchProperty9,");
                strSql2.Append("'" + model.cBatchProperty9 + "',");
            }
            if (model.cBatchProperty10 != null)
            {
                strSql1.Append("cBatchProperty10,");
                strSql2.Append("'" + model.cBatchProperty10 + "',");
            }
            if (model.cbMemo != null)
            {
                strSql1.Append("cbMemo,");
                strSql2.Append("'" + model.cbMemo + "',");
            }
            if (model.irowno != null)
            {
                strSql1.Append("irowno,");
                strSql2.Append("" + model.irowno + ",");
            }
            if (model.strowguid != null)
            {
                strSql1.Append("strowguid,");
                strSql2.Append("'" + Guid.NewGuid().ToString() + "',");
            }
            if (model.rowufts != null)
            {
                strSql1.Append("rowufts,");
                strSql2.Append("" + model.rowufts + ",");
            }
            if (model.ipreuseqty != null)
            {
                strSql1.Append("ipreuseqty,");
                strSql2.Append("" + model.ipreuseqty + ",");
            }
            if (model.ipreuseinum != null)
            {
                strSql1.Append("ipreuseinum,");
                strSql2.Append("" + model.ipreuseinum + ",");
            }
            if (model.iDebitIDs != null)
            {
                strSql1.Append("iDebitIDs,");
                strSql2.Append("" + model.iDebitIDs + ",");
            }
            if (model.fsettleqty != null)
            {
                strSql1.Append("fsettleqty,");
                strSql2.Append("" + model.fsettleqty + ",");
            }
            if (model.fretqtywkp != null)
            {
                strSql1.Append("fretqtywkp,");
                strSql2.Append("" + model.fretqtywkp + ",");
            }
            if (model.fretqtyykp != null)
            {
                strSql1.Append("fretqtyykp,");
                strSql2.Append("" + model.fretqtyykp + ",");
            }
            if (model.cbsysbarcode != null)
            {
                strSql1.Append("cbsysbarcode,");
                strSql2.Append("'" + model.cbsysbarcode + "',");
            }
            if (model.bIAcreatebill != null)
            {
                strSql1.Append("bIAcreatebill,");
                strSql2.Append("" + (model.bIAcreatebill ? 1 : 0) + ",");
            }
            if (model.bsaleoutcreatebill != null)
            {
                strSql1.Append("bsaleoutcreatebill,");
                strSql2.Append("" + (model.bsaleoutcreatebill ? 1 : 0) + ",");
            }
            if (model.isaleoutid != null)
            {
                strSql1.Append("isaleoutid,");
                strSql2.Append("" + model.isaleoutid + ",");
            }
            if (model.bneedbill != null)
            {
                strSql1.Append("bneedbill,");
                strSql2.Append("" + (model.bneedbill ? 1 : 0) + ",");
            }
            if (model.iposflag != null)
            {
                strSql1.Append("iposflag,");
                strSql2.Append("" + model.iposflag + ",");
            }
            if (model.body_outid != null)
            {
                strSql1.Append("body_outid,");
                strSql2.Append("'" + Guid.NewGuid().ToString() + "',");
            }
            strSql.Append("insert into rdrecords32(");
            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());
        }
Esempio n. 2
0
        public void CreateFHD()
        {
            try
            {
                StreamWriter(DateTime.Now.ToString("yyyyMMddHHmmss") + @"【Start】-----------------------------------------------------------");

                string sUFDatabase = GetConfigValue("UFDatabase");
                string sServer     = GetConfigValue("ServerInfo");
                string sSQLUid     = GetConfigValue("SQLUID");
                string sSQLPwd     = GetConfigValue("SQLPWD");

                string sConn = "server=" + sServer + ";uid=" + sSQLUid + " ;pwd=" + sSQLPwd + ";database=" + sUFDatabase + ";";

                string sAccID = sUFDatabase.Substring(7, 3);

                string sErr = "";

                int iCount = 0;

                //获得已审核,未关闭(含行关闭),累计发货数量为0 的订单
                //红字单据不考虑

                DbHelperSQL.connectionString = sConn;

                string    sSQL            = @"
SELECT     TOP (200) sType, StartTime, EndTime, Next_StartTime,GETDATE() AS dNowTime
FROM         _AutoService
where sType = 'DP'
";
                DataTable dt              = DbHelperSQL.Query(sSQL);
                DateTime  dStartTime      = Convert.ToDateTime(dt.Rows[0]["StartTime"]);
                DateTime  dEndTime        = Convert.ToDateTime(dt.Rows[0]["EndTime"]);
                DateTime  dNext_StartTime = Convert.ToDateTime(dt.Rows[0]["Next_StartTime"]);
                DateTime  dNowTime        = Convert.ToDateTime(dt.Rows[0]["dNowTime"]);

                if (dNext_StartTime > dNowTime)
                {
                    StreamWriter(DateTime.Now.ToString("yyyyMMddHHmmss") + "  运行时间未到");
                    return;
                }

                StreamWriter(DateTime.Now.ToString("yyyyMMddHHmmss") + "  开始运行");

                sSQL = "update _AutoService set StartTime = getdate() where sType = 'DP'";
                DbHelperSQL.ExecuteSql(sSQL);

                sSQL = @"
select distinct a.*
from SO_SOMain a inner join SO_SODetails b on a.id = b.id 
	inner join [dbo].[_DSSaleBaseInfo] c on c.cSTCode = a.cSTCode
where isnull(c.[bDS],0) = 1  and isnull(a.cCloser,'') = '' and isnull(a.cVerifier ,'') <> '' 
    and a.ID not in 
    (
        select id 
        from  SO_SODetails
        where  isnull(iFHQuantity ,0) <> 0 or isnull(cSCloser ,'') <> ''
	)
";
                //测试用语句
//                sSQL = @"
//select distinct a.*
//from SO_SOMain a inner join SO_SODetails b on a.id = b.id
//	inner join [dbo].[_DSSaleBaseInfo] c on c.cSTCode = a.cSTCode
//where isnull(c.[bDS],0) = 1  and isnull(a.cCloser,'') = '' and isnull(a.cVerifier ,'') <> ''
//    and a.cSOCode = '20171016000822'
//";
                DataTable dtSO = DbHelperSQL.Query(sSQL);
                StreamWriter(DateTime.Now.ToString("yyyyMMddHHmmss") + "  加载需要发货的销售订单");

                if (dtSO == null || dtSO.Rows.Count == 0)
                {
                    try
                    {
                        string sSQL2 = @"
update _AutoService set  EndTime = getdate(), Next_StartTime = DATEADD(MINUTE,5,GetDate()) 
where sType = 'DP'
";
                        DbHelperSQL.ExecuteSql(sSQL2);
                    }
                    catch { }
                    return;
                }
                StreamWriter(DateTime.Now.ToString("yyyyMMddHHmmss") + "  加载需要发货的销售订单,累计获得订单数量" + dtSO.Rows.Count);
                for (int i = 0; i < dtSO.Rows.Count; i++)
                {
                    sErr = "";

                    #region 发货单
                    SqlConnection conn = new SqlConnection(sConn);
                    conn.Open();
                    //启用事务
                    SqlTransaction tran = conn.BeginTransaction();


                    string sSOCode = dtSO.Rows[i]["cSOCode"].ToString().Trim();

                    try
                    {
                        sSQL = @"
select distinct a.*, a.cDefine12 as cWhCode,b.*,inv.cInvName,inv.cInvStd,isnull(inv.bInvBatch ,0) as bInvBatch,d.cRdCode
from SO_SOMain a inner join SO_SODetails b on a.id = b.id 
	inner join [dbo].[_DSSaleBaseInfo] c on c.cSTCode = a.cSTCode
    inner join Inventory inv on b.cInvCode = inv.cInvCode
    inner join SaleType d on d.cSTCode = a.cSTCode
where a.cSOCode = 'aaaaaaaa'
";
                        sSQL = sSQL.Replace("aaaaaaaa", sSOCode);
                        DataTable dtSOs = DbHelperSQL.ExecuteDataset(tran, CommandType.Text, sSQL).Tables[0];

                        string   sCusCode = dtSOs.Rows[0]["cCusCode"].ToString().Trim();
                        DateTime dtmSO    = BaseFunction.ReturnDate(dtSOs.Rows[0]["dDate"]);

                        sSQL = "select isnull(bflag_SA,0) as bflag from GL_mend where iYPeriod = '" + dtmSO.ToString("yyyyMM") + "'";
                        dt   = DbHelperSQL.ExecuteDataset(tran, CommandType.Text, sSQL).Tables[0];
                        if (dt == null || dt.Rows.Count == 0)
                        {
                            StreamWriter(DateTime.Now.ToString("yyyyMMddHHmmss") + "  单据日期已经结账1");
                            throw new Exception(sSOCode + "当前单据日期已经结帐");
                        }
                        int i结账 = BaseFunction.ReturnInt(dt.Rows[0]["bflag"]);
                        if (i结账 > 0)
                        {
                            StreamWriter(DateTime.Now.ToString("yyyyMMddHHmmss") + "  单据日期已经结账2");
                            throw new Exception(sSOCode + "当前单据日期已经结帐");
                        }

                        string sCode = "DP-" + sSOCode;

                        long lID        = -1;
                        long lIDDetails = -1;
                        sSQL = @"
                                            declare @p5 int
                                            set @p5=aaaaaa
                                            declare @p6 int
                                            set @p6=bbbbbb
                                            exec sp_GetId N'00',N'dddddd',N'DISPATCH',eeeeee,@p5 output,@p6 output,default
                                            select @p5, @p6
                                            ";
                        sSQL = sSQL.Replace("aaaaaa", lID.ToString());
                        sSQL = sSQL.Replace("bbbbbb", lIDDetails.ToString());
                        sSQL = sSQL.Replace("dddddd", sAccID);
                        sSQL = sSQL.Replace("eeeeee", dtSOs.Rows.Count.ToString());
                        dt   = DbHelperSQL.ExecuteDataset(tran, CommandType.Text, sSQL).Tables[0];

                        lID        = BaseFunction.ReturnLong(dt.Rows[0][0]);
                        lIDDetails = BaseFunction.ReturnLong(dt.Rows[0][1]) - dtSOs.Rows.Count;


                        Model.DispatchList mod = new DllForService.Model.DispatchList();
                        mod.DLID = lID;


                        mod.cDLCode    = sCode;
                        mod.cVouchType = "05";
                        mod.cRdCode    = dtSOs.Rows[0]["cRdCode"].ToString().Trim();
                        mod.cSTCode    = dtSOs.Rows[0]["cSTCode"].ToString().Trim();
                        mod.dDate      = dtmSO;
                        mod.bneedbill  = true;
                        mod.cDepCode   = dtSO.Rows[0]["cDepCode"].ToString().Trim();
                        //mod.cPersonCode = lookUpEditcPersonCode.EditValue.ToString();
                        mod.SBVID       = 0;
                        mod.cCusCode    = sCusCode;
                        mod.cexch_name  = dtSOs.Rows[0]["cexch_name"].ToString().Trim();
                        mod.iExchRate   = BaseFunction.ReturnDecimal(dtSOs.Rows[0]["iExchRate"]);
                        mod.iTaxRate    = BaseFunction.ReturnDecimal(dtSOs.Rows[0]["iTaxRate"]);
                        mod.bFirst      = false;
                        mod.bReturnFlag = false;
                        mod.bSettleAll  = false;
                        mod.cMemo       = dtSOs.Rows[0]["cMemo"].ToString().Trim();
                        mod.cBusType    = dtSOs.Rows[0]["cBusType"].ToString().Trim();
                        mod.cCusName    = dtSOs.Rows[0]["cCusName"].ToString().Trim();

                        mod.cDefine1 = dtSOs.Rows[0]["cDefine1"].ToString().Trim();
                        mod.cDefine2 = dtSOs.Rows[0]["cDefine1"].ToString().Trim();
                        mod.cDefine3 = dtSOs.Rows[0]["cDefine3"].ToString().Trim();

                        if (dtSOs.Rows[0]["cDefine4"].ToString().Trim() != "" && BaseFunction.ReturnDate(dtSOs.Rows[0]["cDefine4"]) > BaseFunction.ReturnDate("1901-01-01"))
                        {
                            mod.cDefine4 = BaseFunction.ReturnDate(dtSOs.Rows[0]["cDefine4"]);
                        }
                        if (dtSOs.Rows[0]["cDefine5"].ToString().Trim() != "")
                        {
                            mod.cDefine5 = BaseFunction.ReturnLong(dtSOs.Rows[0]["cDefine5"]);
                        }
                        if (dtSOs.Rows[0]["cDefine6"].ToString().Trim() != "" && BaseFunction.ReturnDate(dtSOs.Rows[0]["cDefine6"]) > BaseFunction.ReturnDate("1901-01-01"))
                        {
                            mod.cDefine6 = BaseFunction.ReturnDate(dtSOs.Rows[0]["cDefine6"]);
                        }
                        if (dtSOs.Rows[0]["cDefine7"].ToString().Trim() != "")
                        {
                            mod.cDefine7 = BaseFunction.ReturnDecimal(dtSOs.Rows[0]["cDefine7"]);
                        }
                        mod.cDefine8  = dtSOs.Rows[0]["cDefine8"].ToString().Trim();
                        mod.cDefine9  = dtSOs.Rows[0]["cDefine9"].ToString().Trim();
                        mod.cDefine10 = dtSOs.Rows[0]["cDefine10"].ToString().Trim();
                        mod.cDefine11 = dtSOs.Rows[0]["cDefine11"].ToString().Trim();
                        mod.cDefine12 = dtSOs.Rows[0]["cDefine12"].ToString().Trim();
                        mod.cDefine13 = dtSOs.Rows[0]["cDefine13"].ToString().Trim();
                        mod.cDefine14 = dtSOs.Rows[0]["cDefine14"].ToString().Trim();
                        if (dtSOs.Rows[0]["cDefine15"].ToString().Trim() != "")
                        {
                            mod.cDefine15 = BaseFunction.ReturnLong(dtSOs.Rows[0]["cDefine15"]);
                        }
                        if (dtSOs.Rows[0]["cDefine16"].ToString().Trim() != "")
                        {
                            mod.cDefine16 = BaseFunction.ReturnDecimal(dtSOs.Rows[0]["cDefine16"]);
                        }

                        mod.iVTid          = 71;
                        mod.cMaker         = "demo";
                        mod.cMemo          = dtSOs.Rows[0]["cMemo"].ToString().Trim();
                        mod.cVerifier      = "demo";
                        mod.dverifydate    = BaseFunction.ReturnDate(mod.dDate.ToString("yyyy-MM-dd"));
                        mod.dverifydate    = mod.dDate;
                        mod.dverifysystime = mod.dDate;
                        mod.cSaleOut       = sSOCode;
                        mod.cSysBarCode    = "||SA01|" + mod.cDLCode;

                        DAL.DispatchList dal = new DllForService.DAL.DispatchList();

                        sSQL = dal.Add(mod);
                        DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);

                        StreamWriter(DateTime.Now.ToString("yyyyMMddHHmmss") + "  单据表头生成成功:单据第" + (i + 1).ToString() + "");
                        int iRow = 0;

                        for (int j = 0; j < dtSOs.Rows.Count; j++)
                        {
                            decimal dQty = BaseFunction.ReturnDecimal(dtSOs.Rows[j]["iQuantity"]);
                            if (dQty <= 0)
                            {
                                continue;
                            }

                            string sInvCode = dtSOs.Rows[j]["cInvCode"].ToString().Trim();
                            string sWhCode  = dtSOs.Rows[j]["cWhCode"].ToString().Trim();

                            sSQL = "select sum(iQuantity) as iQty from CurrentStock where cInvCode = 'aaaaaaaa' and cWhCode = 'bbbbbbbb' ";
                            sSQL = sSQL.Replace("aaaaaaaa", sInvCode);
                            sSQL = sSQL.Replace("bbbbbbbb", sWhCode);
                            DataTable dtCurr = DbHelperSQL.ExecuteDataset(tran, CommandType.Text, sSQL).Tables[0];
                            if (dtCurr == null || dtCurr.Rows.Count == 0)
                            {
                                sErr = sErr + sCode + " There are not enough " + sInvCode + " in the warehouse " + sWhCode + "\n";
                                continue;
                            }

                            decimal dCurrSum = BaseFunction.ReturnDecimal(dtCurr.Rows[0]["iQty"]);
                            if (dCurrSum < dQty)
                            {
                                sErr = sErr + sCode + " There are not enough " + sInvCode + " in the warehouse " + sWhCode + "\n";
                                continue;
                            }

                            sSQL   = "select * from CurrentStock where isnull(iQuantity,0) > 0 and  cInvCode = 'aaaaaaaa' and cWhCode = 'bbbbbbbb' order by dVDate ";
                            sSQL   = sSQL.Replace("aaaaaaaa", sInvCode);
                            sSQL   = sSQL.Replace("bbbbbbbb", sWhCode);
                            dtCurr = DbHelperSQL.ExecuteDataset(tran, CommandType.Text, sSQL).Tables[0];

                            for (int k = 0; k < dtCurr.Rows.Count; k++)
                            {
                                if (dQty <= 0)
                                {
                                    break;
                                }

                                decimal dCurr = BaseFunction.ReturnDecimal(dtCurr.Rows[k]["iQuantity"]);

                                if (dCurr <= 0)
                                {
                                    break;
                                }

                                decimal dQtyNow = 0;
                                if (dCurr <= dQty)
                                {
                                    dQtyNow = dCurr;
                                    dQty    = dQty - dCurr;
                                }
                                else
                                {
                                    dQtyNow = dQty;
                                    dQty    = 0;
                                }

                                if (dQtyNow <= 0)
                                {
                                    break;
                                }

                                Model.DispatchLists mods = new DllForService.Model.DispatchLists();
                                lIDDetails        += 1;
                                mods.AutoID        = lIDDetails;
                                mods.DLID          = mod.DLID;
                                mods.cWhCode       = dtSOs.Rows[j]["cWhCode"].ToString().Trim();
                                mods.cInvCode      = sInvCode;
                                mods.cInvName      = dtSOs.Rows[j]["cInvName"].ToString().Trim();
                                mods.iQuantity     = dQtyNow;
                                mods.iQuotedPrice  = 0;
                                mods.iUnitPrice    = BaseFunction.ReturnDecimal(dtSOs.Rows[j]["iUnitPrice"]);
                                mods.iTaxUnitPrice = BaseFunction.ReturnDecimal(dtSOs.Rows[j]["iTaxUnitPrice"]);
                                mods.iMoney        = BaseFunction.ReturnDecimal(mods.iUnitPrice * mods.iQuantity, 2);
                                mods.iSum          = BaseFunction.ReturnDecimal(mods.iTaxUnitPrice * mods.iQuantity, 2);
                                mods.iTax          = mods.iSum - mods.iMoney;
                                mods.iDisCount     = 0;
                                mods.iNatUnitPrice = BaseFunction.ReturnDecimal(mods.iUnitPrice * mod.iExchRate);
                                mods.iNatMoney     = BaseFunction.ReturnDecimal(mods.iNatUnitPrice * mods.iQuantity, 2);
                                mods.iNatSum       = BaseFunction.ReturnDecimal(mods.iNatMoney * (1 + mod.iTaxRate / 100), 2);
                                mods.iNatTax       = mods.iNatSum - mods.iNatMoney;
                                mods.bSettleAll    = false;
                                mods.iDLsID        = lIDDetails;
                                mods.bcosting      = true;
                                mods.iSOsID        = BaseFunction.ReturnLong(dtSOs.Rows[j]["iSOsID"]);
                                mods.iTaxRate      = BaseFunction.ReturnDecimal(dtSOs.Rows[j]["iTaxRate"]);
                                iRow       += 1;
                                mods.irowno = iRow;
                                if (BaseFunction.ReturnBool(dtSOs.Rows[j]["bInvBatch"]))
                                {
                                    mods.cBatch = dtCurr.Rows[k]["cBatch"].ToString().Trim();
                                }


                                mods.cDefine22 = dtSOs.Rows[j]["cDefine22"].ToString().Trim();
                                mods.cDefine23 = dtSOs.Rows[j]["cDefine23"].ToString().Trim();
                                mods.cDefine24 = dtSOs.Rows[j]["cDefine24"].ToString().Trim();
                                mods.cDefine25 = dtSOs.Rows[j]["cDefine25"].ToString().Trim();

                                if (dtSOs.Rows[j]["cDefine26"].ToString().Trim() != "")
                                {
                                    mods.cDefine26 = BaseFunction.ReturnDecimal(dtSOs.Rows[j]["cDefine26"]);
                                }
                                if (dtSOs.Rows[j]["cDefine27"].ToString().Trim() != "")
                                {
                                    mods.cDefine27 = BaseFunction.ReturnDecimal(dtSOs.Rows[j]["cDefine27"]);
                                }
                                mods.cDefine28 = dtSOs.Rows[j]["cDefine28"].ToString().Trim();
                                mods.cDefine29 = dtSOs.Rows[j]["cDefine29"].ToString().Trim();
                                mods.cDefine30 = dtSOs.Rows[j]["cDefine30"].ToString().Trim();
                                mods.cDefine31 = dtSOs.Rows[j]["cDefine31"].ToString().Trim();
                                mods.cDefine32 = dtSOs.Rows[j]["cDefine32"].ToString().Trim();
                                mods.cDefine33 = dtSOs.Rows[j]["cDefine33"].ToString().Trim();
                                if (dtSOs.Rows[j]["cDefine34"].ToString().Trim() != "")
                                {
                                    mods.cDefine34 = BaseFunction.ReturnLong(dtSOs.Rows[j]["cDefine34"]);
                                }
                                if (dtSOs.Rows[j]["cDefine35"].ToString().Trim() != "")
                                {
                                    mods.cDefine35 = BaseFunction.ReturnLong(dtSOs.Rows[j]["cDefine35"]);
                                }
                                if (dtSOs.Rows[j]["cDefine36"].ToString().Trim() != "" && BaseFunction.ReturnDate(dtSOs.Rows[j]["cDefine36"]) > BaseFunction.ReturnDate("1901-01-01"))
                                {
                                    mods.cDefine36 = BaseFunction.ReturnDate(dtSOs.Rows[j]["cDefine36"]);
                                }
                                if (dtSOs.Rows[j]["cDefine37"].ToString().Trim() != "" && BaseFunction.ReturnDate(dtSOs.Rows[j]["cDefine37"]) > BaseFunction.ReturnDate("1901-01-01"))
                                {
                                    mods.cDefine37 = BaseFunction.ReturnDate(dtSOs.Rows[j]["cDefine37"]);
                                }
                                mods.bsaleprice = BaseFunction.ReturnBool(dtSOs.Rows[j]["bsaleprice"]);;

                                DAL.DispatchLists dals = new DllForService.DAL.DispatchLists();
                                sSQL    = dals.Add(mods);
                                iCount += DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);

                                sSQL = @"
if exists(select * from  CurrentStock where cInvCode = '@cInvCode' and cWhCode = '@cWhCode') 
	update  CurrentStock set fOutQuantity = isnull(fOutQuantity,0) + @dQty  where cInvCode = '@cInvCode' and cWhCode = '@cWhCode'  and isnull(cBatch,'') = @cBatch 
else 
	begin 
		declare @itemid varchar(20); 
		declare @iCount int;  
		select @iCount=count(itemid) from CurrentStock where cInvCode = '@cInvCode' and cWhCode = '@cWhCode' and isnull(cBatch,'') = @cBatch 
		if( @iCount > 0 ) 
			select @itemid=itemid from CurrentStock where cInvCode = '@cInvCode' and cWhCode = '@cWhCode' and isnull(cBatch,'') = @cBatch 
		else  
			select @itemid=max(itemid+1) from CurrentStock  
		
		insert into CurrentStock(cWhCode,cInvCode,fOutQuantity,itemid,cBatch)values('@cWhCode','@cInvCode', @dQty ,@itemid,@cBatch) 
	end
                        ";
                                sSQL = sSQL.Replace("@cInvCode", mods.cInvCode);
                                sSQL = sSQL.Replace("@cWhCode", mods.cWhCode);
                                sSQL = sSQL.Replace("@dQty", mods.iQuantity.ToString());
                                if (BaseFunction.ReturnBool(dtSOs.Rows[j]["bInvBatch"]))
                                {
                                    sSQL = sSQL.Replace("@cBatch", "'" + mods.cBatch + "'");
                                }
                                else
                                {
                                    sSQL = sSQL.Replace("@cBatch", "''");
                                }
                                DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);

                                sSQL = "update SO_SODetails set iFHQuantity = isnull(iFHQuantity,0) + " + mods.iQuantity + ",iFHMoney = isnull(iFHMoney,0) + " + mods.iSum + " where iSOsID = " + mods.iSOsID;
                                DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);


                                StreamWriter(DateTime.Now.ToString("yyyyMMddHHmmss") + "  单据表头生成成功:单据第" + (i + 1).ToString() + ",行" + (j + 1).ToString());
                            }
                        }


                        int iTest = iRow - dtSOs.Rows.Count;
                        sSQL = @"
update  UFSystem..UA_Identity set iChildId = iChildId + {0} WHERE   (cAcc_Id = '001') AND (cVouchType = 'DISPATCH')
";
                        sSQL = string.Format(sSQL, iTest);
                        DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);



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

                        #region 销售出库单


                        StreamWriter(DateTime.Now.ToString("yyyyMMddHHmmss") + "  生成销售出库单,开始");

                        //获得发货单有多少仓库,每个仓库有几行记录出库
                        sSQL = @"
select  b.cWhCode,count(1) as iCou
from DispatchList a  inner join DispatchLists b on a.DLID = b.DLID
	inner join [dbo].[_DSSaleBaseInfo] c on c.cSTCode = a.cSTCode
    inner join Inventory inv on b.cInvCode = inv.cInvCode
    inner join SaleType d on d.cSTCode = a.cSTCode
where a.cDLCode = 'aaaaaa'
group by b.cWhCode 
";
                        sSQL = sSQL.Replace("aaaaaa", mod.cDLCode);
                        DataTable dtWh = DbHelperSQL.ExecuteDataset(tran, CommandType.Text, sSQL).Tables[0];

                        for (int iii = 0; iii < dtWh.Rows.Count; iii++)
                        {
                            string sRDCode = "";

                            sSQL = @"
select * 
from DispatchList a inner join DispatchLists b on a.DLID = b.DLID
	inner join SaleType c on c.cSTCode = a.cSTCode
    inner join Inventory inv on inv.cInvCode = b.cInvCode
where a.cDLCode = 'aaaaaa' and b.cWhCode = 'bbbbbb'
order by AutoID
";
                            sSQL = sSQL.Replace("aaaaaa", mod.cDLCode);
                            sSQL = sSQL.Replace("bbbbbb", dtWh.Rows[iii]["cWhCode"].ToString().Trim());
                            DataTable dtDispatchList = DbHelperSQL.ExecuteDataset(tran, CommandType.Text, sSQL).Tables[0];
                            if (dtDispatchList == null || dtDispatchList.Rows.Count == 0)
                            {
                                throw new Exception(sCode + " is not exists");
                            }

                            sSQL = "select isnull(bflag_ST,0) as bflag from GL_mend where iYPeriod = '" + mod.dDate.ToString("yyyyMM") + "'";
                            dt   = DbHelperSQL.ExecuteDataset(tran, CommandType.Text, sSQL).Tables[0];
                            if (dt == null || dt.Rows.Count == 0)
                            {
                                throw new Exception("Access module state failure");
                            }
                            i结账 = BaseFunction.ReturnInt(dt.Rows[0]["bflag"]);
                            if (i结账 > 0)
                            {
                                throw new Exception(mod.dDate.ToString("yyyy-MM") + " have checked out");
                            }

                            lID        = -1;
                            lIDDetails = -1;
                            sSQL       = @"
declare @p5 int
set @p5=aaaaaa
declare @p6 int
set @p6=bbbbbb
exec sp_GetId N'00',N'dddddd',N'rd',eeeeee,@p5 output,@p6 output,default
select @p5, @p6
                    ";
                            sSQL       = sSQL.Replace("aaaaaa", lID.ToString());
                            sSQL       = sSQL.Replace("bbbbbb", lIDDetails.ToString());
                            sSQL       = sSQL.Replace("dddddd", sAccID);
                            sSQL       = sSQL.Replace("eeeeee", dtWh.Rows[iii]["iCou"].ToString().Trim());
                            dt         = DbHelperSQL.ExecuteDataset(tran, CommandType.Text, sSQL).Tables[0];

                            lID        = BaseFunction.ReturnLong(dt.Rows[0][0]);
                            lIDDetails = BaseFunction.ReturnLong(dt.Rows[0][1]) - BaseFunction.ReturnInt(dtWh.Rows[iii]["iCou"]);

                            if (dt == null || dt.Rows.Count == 0)
                            {
                                throw new Exception(sCode + " is not exists");
                            }

                            Model.rdrecord32 modRD = new DllForService.Model.rdrecord32();
                            modRD.ID         = lID;
                            modRD.bRdFlag    = 0;
                            modRD.cVouchType = "32";
                            modRD.cBusType   = mod.cBusType;
                            modRD.cSource    = "发货单";
                            modRD.cBusCode   = mod.cDLCode;
                            modRD.cWhCode    = dtWh.Rows[iii]["cWhCode"].ToString().Trim();
                            modRD.dDate      = mod.dDate;
                            modRD.cCode      = "RD-" + modRD.cWhCode + "-" + sSOCode;


                            if (sRDCode == "")
                            {
                                sRDCode = modRD.cCode;
                            }
                            else
                            {
                                sRDCode = sRDCode + "," + modRD.cCode;
                            }

                            modRD.cDepCode         = mod.cDepCode;
                            modRD.cPersonCode      = mod.cPersonCode;
                            modRD.cSTCode          = mod.cSTCode;
                            modRD.cRdCode          = dtDispatchList.Rows[0]["cRdCode"].ToString().Trim();
                            modRD.cCusCode         = mod.cCusCode;
                            modRD.cDLCode          = mod.DLID;
                            modRD.bTransFlag       = false;
                            modRD.cMaker           = "demo";
                            modRD.cDefine1         = mod.cDefine1;
                            modRD.cDefine2         = mod.cDefine2;
                            modRD.cDefine3         = mod.cDefine3;
                            modRD.cDefine4         = mod.cDefine4;
                            modRD.cDefine5         = mod.cDefine5;
                            modRD.cDefine6         = mod.cDefine6;
                            modRD.cDefine7         = mod.cDefine7;
                            modRD.cDefine8         = mod.cDefine8;
                            modRD.cDefine9         = mod.cDefine9;
                            modRD.cDefine10        = mod.cDefine10;
                            modRD.cDefine11        = mod.cDefine11;
                            modRD.cDefine12        = mod.cDefine12;
                            modRD.cDefine13        = mod.cDefine13;
                            modRD.cDefine14        = mod.cDefine14;
                            modRD.cDefine15        = mod.cDefine15;
                            modRD.cDefine16        = mod.cDefine16;
                            modRD.bpufirst         = false;
                            modRD.biafirst         = false;
                            modRD.VT_ID            = 87;
                            modRD.bIsSTQc          = false;
                            modRD.bFromPreYear     = false;
                            modRD.bIsComplement    = 0;
                            modRD.iDiscountTaxType = 0;
                            modRD.ireturncount     = 0;
                            modRD.iverifystate     = 0;
                            modRD.iswfcontrolled   = 0;
                            modRD.dnmaketime       = mod.dverifysystime;
                            modRD.bredvouch        = 0;
                            modRD.iPrintCount      = 0;
                            modRD.cinvoicecompany  = modRD.cCusCode;
                            modRD.dnmaketime       = mod.dDate;
                            modRD.csysbarcode      = "||st32|" + modRD.cCode;
                            modRD.cHandler         = "demo";
                            modRD.dnverifytime     = mod.dverifysystime;
                            modRD.dVeriDate        = mod.dverifydate;

                            DAL.rdrecord32 dalRD = new DllForService.DAL.rdrecord32();
                            sSQL = dalRD.Add(modRD);
                            DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);

                            int iRDRow = 0;
                            for (int jj = 0; jj < dtDispatchList.Rows.Count; jj++)
                            {
                                if (dtDispatchList.Rows[jj]["cWhCode"].ToString().Trim() != modRD.cWhCode)
                                {
                                    continue;
                                }

                                decimal dOutSum = BaseFunction.ReturnDecimal(dtDispatchList.Rows[jj]["fOutQuantity"]);
                                if (dOutSum > 0)
                                {
                                    sErr = sErr + "Item No " + dtDispatchList.Rows[jj]["cInvCode"].ToString() + " No.Site list " + dtDispatchList.Rows[jj]["cDefine10"].ToString() + " is used\n";
                                    continue;
                                }

                                iRDRow += 1;
                                Model.rdrecords32 modRDs = new DllForService.Model.rdrecords32();
                                lIDDetails      += 1;
                                modRDs.AutoID    = lIDDetails;
                                modRDs.ID        = modRD.ID;
                                modRDs.cInvCode  = dtDispatchList.Rows[jj]["cInvCode"].ToString().Trim();
                                modRDs.iQuantity = BaseFunction.ReturnDecimal(dtDispatchList.Rows[jj]["iQuantity"]);
                                modRDs.iNum      = BaseFunction.ReturnDecimal(dtDispatchList.Rows[jj]["iNum"]);

                                sSQL = "select sum(iQuantity) as iQty from CurrentStock where cWhCode = 'aaaaaa' and cInvCode = 'bbbbbb'";
                                sSQL = sSQL.Replace("aaaaaa", modRD.cWhCode);
                                sSQL = sSQL.Replace("bbbbbb", modRDs.cInvCode);
                                DataTable dtCurr = DbHelperSQL.ExecuteDataset(tran, CommandType.Text, sSQL).Tables[0];
                                if (dtCurr == null || dtCurr.Rows.Count == 0)
                                {
                                    sErr = sErr + "Item No " + dtDispatchList.Rows[jj]["cInvCode"].ToString() + " not enough\n";
                                    continue;
                                }
                                decimal dCurr = BaseFunction.ReturnDecimal(dtCurr.Rows[0]["iQty"]);
                                if (dCurr < modRDs.iQuantity)
                                {
                                    sErr = sErr + "Item No  " + dtDispatchList.Rows[jj]["cInvCode"].ToString() + " not enough\n";
                                    continue;
                                }
                                //modRDs.iUnitCost = BaseFunction.ReturnDecimal(dtDispatchList.Rows[jj]["iUnitPrice"]);
                                //modRDs.iPrice = BaseFunction.ReturnDecimal(dtDispatchList.Rows[jj]["iMoney"]);
                                modRDs.iFlag              = 0;
                                modRDs.iDLsID             = BaseFunction.ReturnLong(dtDispatchList.Rows[jj]["iDLsID"]);
                                modRDs.iNQuantity         = modRDs.iQuantity;
                                modRDs.iNNum              = modRDs.iNum;
                                modRDs.bLPUseFree         = false;
                                modRDs.iRSRowNO           = 0;
                                modRDs.iOriTrackID        = 0;
                                modRDs.bCosting           = true;
                                modRDs.bVMIUsed           = false;
                                modRDs.cbdlcode           = sCode;
                                modRDs.iExpiratDateCalcu  = 0;
                                modRDs.ccusinvcode        = dtDispatchList.Rows[jj]["ccusinvcode"].ToString().Trim();
                                modRDs.iordertype         = 0;
                                modRDs.ipesotype          = 0;
                                modRDs.isotype            = 0;
                                modRDs.irowno             = iRDRow;
                                modRDs.bIAcreatebill      = false;
                                modRDs.bsaleoutcreatebill = false;
                                modRDs.bneedbill          = true;
                                modRDs.cBatch             = dtDispatchList.Rows[jj]["cBatch"].ToString().Trim();


                                modRDs.cDefine22 = dtDispatchList.Rows[jj]["cDefine22"].ToString().Trim();
                                modRDs.cDefine23 = dtDispatchList.Rows[jj]["cDefine23"].ToString().Trim();
                                modRDs.cDefine24 = dtDispatchList.Rows[jj]["cDefine24"].ToString().Trim();
                                modRDs.cDefine25 = dtDispatchList.Rows[jj]["cDefine25"].ToString().Trim();
                                if (dtDispatchList.Rows[jj]["cDefine26"].ToString().Trim() != "")
                                {
                                    modRDs.cDefine26 = BaseFunction.ReturnDecimal(dtDispatchList.Rows[jj]["cDefine26"]);
                                }
                                if (dtDispatchList.Rows[jj]["cDefine27"].ToString().Trim() != "")
                                {
                                    modRDs.cDefine27 = BaseFunction.ReturnDecimal(dtDispatchList.Rows[jj]["cDefine27"]);
                                }
                                modRDs.cDefine28 = dtDispatchList.Rows[jj]["cDefine28"].ToString().Trim();
                                modRDs.cDefine29 = dtDispatchList.Rows[jj]["cDefine29"].ToString().Trim();
                                modRDs.cDefine30 = dtDispatchList.Rows[jj]["cDefine30"].ToString().Trim();
                                modRDs.cDefine31 = dtDispatchList.Rows[jj]["cDefine31"].ToString().Trim();
                                modRDs.cDefine32 = dtDispatchList.Rows[jj]["cDefine32"].ToString().Trim();
                                modRDs.cDefine33 = dtDispatchList.Rows[jj]["cDefine33"].ToString().Trim();
                                if (dtDispatchList.Rows[jj]["cDefine34"].ToString().Trim() != "")
                                {
                                    modRDs.cDefine34 = BaseFunction.ReturnInt(dtDispatchList.Rows[jj]["cDefine34"]);
                                }
                                if (dtDispatchList.Rows[jj]["cDefine35"].ToString().Trim() != "")
                                {
                                    modRDs.cDefine35 = BaseFunction.ReturnInt(dtDispatchList.Rows[jj]["cDefine35"]);
                                }
                                if (dtDispatchList.Rows[jj]["cDefine36"].ToString().Trim() != "" && BaseFunction.ReturnDate(dtDispatchList.Rows[jj]["cDefine36"]) > BaseFunction.ReturnDate("1901-01-01"))
                                {
                                    modRDs.cDefine36 = BaseFunction.ReturnDate(dtDispatchList.Rows[jj]["cDefine36"]);
                                }
                                if (dtDispatchList.Rows[jj]["cDefine37"].ToString().Trim() != "" && BaseFunction.ReturnDate(dtDispatchList.Rows[jj]["cDefine37"]) > BaseFunction.ReturnDate("1901-01-01"))
                                {
                                    modRDs.cDefine37 = BaseFunction.ReturnDate(dtDispatchList.Rows[jj]["cDefine37"]);
                                }


                                DAL.rdrecords32 dals = new DllForService.DAL.rdrecords32();
                                sSQL = dals.Add(modRDs);
                                DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);

                                sSQL = @"
if exists(select * from  CurrentStock where cInvCode = '@cInvCode' and cWhCode = '@cWhCode') 
	update  CurrentStock set fOutQuantity = isnull(fOutQuantity,0) - @dQty ,iQuantity = isnull(iQuantity,0) - @dQty  where cInvCode = '@cInvCode' and cWhCode = '@cWhCode'  and isnull(cBatch,'') = @cBatch 
else 
	begin 
		declare @itemid varchar(20); 
		declare @iCount int;  
		select @iCount=count(itemid) from CurrentStock where cInvCode = '@cInvCode' and cWhCode = '@cWhCode' and isnull(cBatch,'') = @cBatch 
		if( @iCount > 0 ) 
			select @itemid=itemid from CurrentStock where cInvCode = '@cInvCode' and cWhCode = '@cWhCode' and isnull(cBatch,'') = @cBatch 
		else  
			select @itemid=max(itemid+1) from CurrentStock  
		
		insert into CurrentStock(cWhCode,cInvCode,fOutQuantity,iQuantity,itemid,cBatch)values('@cWhCode','@cInvCode', -1 * @dQty, -1 * @dQty ,@itemid,@cBatch) 
	end
";
                                sSQL = sSQL.Replace("@cInvCode", modRDs.cInvCode);
                                sSQL = sSQL.Replace("@cWhCode", modRD.cWhCode);
                                sSQL = sSQL.Replace("@dQty", modRDs.iQuantity.ToString());
                                if (BaseFunction.ReturnBool(dtDispatchList.Rows[jj]["bInvBatch"]))
                                {
                                    sSQL = sSQL.Replace("@cBatch", "'" + modRDs.cBatch + "'");
                                }
                                else
                                {
                                    sSQL = sSQL.Replace("@cBatch", "''");
                                }
                                DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);

                                sSQL = "update DispatchLists set fOutQuantity = iQuantity where Autoid = " + dtDispatchList.Rows[jj]["Autoid"].ToString().Trim();
                                DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);

                                sSQL = @"
insert into [IA_ST_UnAccountVouch32]( IDUN, IDSUN, cVouTypeUN, cBustypeUN)
values({0},{1},'32','普通销售')
";
                                sSQL = string.Format(sSQL, modRD.ID, modRDs.AutoID);
                                DbHelperSQL.ExecuteNonQuery(tran, CommandType.Text, sSQL);
                            }

                            CreateLog(sConn, sSOCode, mod.cDLCode, sRDCode, "Success", "");
                        }

                        StreamWriter(DateTime.Now.ToString("yyyyMMddHHmmss") + "  生成销售出库单,完成");

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

                        #endregion

                        tran.Commit();
                    }
                    catch (Exception ee)
                    {
                        StreamWriter(DateTime.Now.ToString("yyyyMMddHHmmss") + "  " + ee.Message.ToString());
                        CreateLog(sConn, sSOCode, "", "", "Error", ee.Message);
                        tran.Rollback();
                    }

                    #endregion
                }
            }
            catch (Exception ee)
            {
                StreamWriter(DateTime.Now.ToString("yyyyMMddHHmmss") + "  " + ee.Message.ToString());

                throw new Exception(ee.Message);
            }
            finally
            {
                StreamWriter(DateTime.Now.ToString("yyyyMMddHHmmss") + @"【End】-----------------------------------------------------------");
                StreamWriter("");
                StreamWriter("");
            }
        }