---drop procedure [dbo].[PR_WoolinteBillIndex];
---2013-01-09  Wesley219  创建储存过程 
---订单加入赠品标记
alter procedure [dbo].[PR_WoolinteBillIndex](@BillNumberID varchar(100)) AS
  Declare @State varchar(50);
  Declare @BillType varchar(50),@BillTypeID varchar(50),@KTypeID_ varchar(100),@VchCode varchar(50),@BillDate varchar(50),@PriceTrack varchar(2),@PriceTrackOrPDAPrice varchar(50);
  Declare @Num int,@PTypeID_ varchar(100),@UintNum varchar(50),@PDAQty numeric(12,2),@UintRate numeric(12,2),@CostMode int,@dlyQty numeric(18,4);
  Declare @GNumberPDate varchar(50),@UserProductName varchar(100),@BillComment varchar(100),@DeviceName varchar(100);
  Declare @BillName varchar(150),@UserTypeID varchar(150),@UserFullName varchar(150),@BillCode varchar(150);
  Declare @SerialIsUsed varchar(50),@PreferenceMoney numeric(12,2);
     DECLARE @loginname varchar(100);DECLARE @summary varchar(2000);
      DECLARE @statee varchar(100);
            DECLARE @typee varchar(100);--判断是否审核订单
                
     begin tran
  SET @summary=''
        Select @BillType=BillType,@BillTypeID=BillTypeID,@BillDate=BillDate,@KTypeID_=KTypeID_,@State=State,
        @PriceTrack=PriceTrack,@BillName=BillName,@UserTypeID=UserTypeID,@UserFullName=UserFullName,@DeviceName=DeviceName ,@loginname=LoginName,
        @BillCode=BillCode,@PreferenceMoney=PreferenceMoney
        From Woolinte_BillIndex where BillNumberID=@BillNumberID;
-------======================修改单据   先删除原先单据在提交
     declare @ifeditbill varchar(100);
     declare @ordercodee varchar(500);
     set @ifeditbill='' ;set @ordercodee=''
     select  @ifeditbill=IfEditBill,@ordercodee=ordercode  From Woolinte_BillIndex where BillNumberID=@BillNumberID;
     if @ifeditbill='1'
     begin
        if(@BillType='SaleOrder' or @BillType='BuyOrder')
begin
  SELECT @summary=Comment from  dlyndxorder where vchcode=@ordercodee
delete from dlyndxorder where vchcode=@ordercodee
          delete from bakdlyorder where vchcode=@ordercodee
end
else
begin
  SELECT @summary=Comment from  dlyndx where vchcode=@ordercodee
         delete from dlyndx where vchcode=@ordercodee
          delete from bakdly where vchcode=@ordercodee
end
UPDATE Woolinte_BillIndex SET State='1' where BillNumberID=@BillNumberID;
SET @State ='1'
     end
     ---===================================================
	
           IF @BillType='BuyOrder'
	BEGIN
            set @statee='';
            set @typee='';
            SELECT @typee=VchType from InputLevel where VchType=7
            END
          IF @BillType='SaleOrder'
            BEGIN
            set @statee='';
            set @typee='';
            SELECT @typee=VchType from InputLevel where VchType=8
            END
            
                
        if @State=1
         begin
            update Woolinte_BillIndex_List set KTypeID_=KTypeID2_ where UsedType='1'
         ---读取系统设置，是否取价格跟踪，无价格跟踪的取实开单价。
         Select @PriceTrackOrPDAPrice=TypeValues From Woolinte_Sysdata where TypeName='BillPriceTrackOrPDAPrice';
         ----产品名称
         Select @UserProductName=IsNull(TypeValues,'') From Woolinte_Sysdata where TypeName='ProductName';
         ----单据备注
         Set @BillComment=@summary+'时间:'+Convert(varchar(100),GetDate(),120)+';业务员:'+ @loginname +';【管家婆物联通】开单生成';
         
         ----序列号启用的判断
         Select @SerialIsUsed=TypeValues From Woolinte_Sysdata where TypeName='UniqueIsUsed';
         set @SerialIsUsed=isnull(@SerialIsUsed,'');
         Select @GNumberPDate=TypeValues From Woolinte_Sysdata where TypeName='GNumberPDate';
         
          ---批次成本，数据处理，启用批次，并且是出库类单据
         if (@GNumberPDate='是' and ((@BillType='Sale') or (@BillType='SaleBackChange')) )
         begin
             Update Woolinte_BillIndex_List Set CostPrice= price,Woolinte_BillIndex_List.ProduceDate=gs.OutFactoryDate FROM  GoodsStocks GS,Woolinte_BillIndex_List bl 
           WHERE GS.PtypeId=bl.PTypeID_ AND GS.KtypeId=@KTypeID_ AND GS.JobNumber=BL.GoodsNumber   and gs.GoodsOrder=bl.GoodSorder
           and BillNumberID=@BillNumberID
                if(@@ERROR<>0)
            goto Error1 
         end
        ----if (@SerialIsUsed<>'是')
        ---- begin
        ----   if(@@ERROR<>0)
        ----     goto Error1 
        ---- end
         
       /*  Update Woolinte_BillIndex_List Set Serial='' where BillNumberID=@BillNumberID;
          if(@@ERROR<>0)
             goto Error1 
              */        
		 ----=======================================================================
		  Declare @MONTH varchar(20),@DAY varchar(20),@BillFlag varchar(20),@number varchar(100)
          Set @MONTH=substring(@BillDate,6,2)
          Set @DAY=substring(@BillDate,9,2)
          Set @number='';
          ----=======================================================================
          IF LEN(@BillCode)<>0
          BEGIN
          Set @number=@BillCode;
          END
          ELSE
          BEGIN
          ----获取单据编号
          exec PR_Woolinte_GetBillCode @BillType,@BillTypeID,'00001',@BillDate,'',@MONTH,@DAY,@number output
          if(@@ERROR<>0)
             goto Error1 
             END
         ----=======================================================================
         ----物联通单位关联
         Update Woolinte_Billindex_List Set UintNum=XP.OrdID+1,UintRate=XP.URate
         From xw_PtypeUnit XP,Woolinte_Units WU
         where Woolinte_Billindex_List.PTypeID_=XP.PTypeID and Woolinte_Billindex_List.UnitID_=WU.UnitID_
         and WU.FullName=XP.Unit1 and billnumberid=@BillNumberID;
         
         if(@@ERROR<>0)
             goto Error1 
         ----=======================================================================
         
         Update  Woolinte_BillIndex_List set PDADistPrice=PDAPrice*PDADiscount where billnumberid=@BillNumberID;
         if(@@ERROR<>0)
            goto Error1 
          ----取开单零售价多单位
          Update Woolinte_BillIndex_List set RetailPrice=xw_P_PtypePrice.Price From xw_P_PtypePrice 
          WHere Woolinte_BillIndex_List.PTypeID_=xw_P_PtypePrice.PTypeID 
          and xw_P_PtypePrice.PRTypeID='0001' and UintNum=xw_P_PtypePrice.UnitID+1 and billnumberid=@BillNumberID;
         if(@@ERROR<>0)
            goto Error1 
           ---如果是提交取价格跟踪后，没有价格跟踪取开单价的 或者非价格跟踪
         if (@PriceTrackOrPDAPrice='是' or @PriceTrack='0')
         begin
           Update  Woolinte_BillIndex_List set Unitprice=PDAPrice,Discount=PDADiscount where billnumberid=@BillNumberID 
         if(@@ERROR<>0)
            goto Error1 
         end
                
                  
         if(@BillType='Sale' or @BillType='SaleBack'  or @BillType='SaleOrder' or @BillType='Retail')
         begin
            if(@PriceTrack='1')
            begin
           ---销售单据取价格跟踪
           
         Update  Woolinte_BillIndex_List set Unitprice=b.price
         From Woolinte_BillIndex a,xw_P_PriceFollow b 
         where  a.billnumberid=@BillNumberID  and Woolinte_BillIndex_List.billnumberid = a.billnumberid 
         And a.BTypeID_ = b.BTypeID and b.PRTypeID='0007'
         and Woolinte_BillIndex_List.PTypeID_=b.PTypeID and Woolinte_BillIndex_List.uintnum=b.UnitID+1 
         and (a.billtype='Sale' or a.billtype='SaleBack' or a.billtype='SaleOrder' or @BillType='Retail') AND Woolinte_BillIndex_List.DetailType='0'
                
         Update  Woolinte_BillIndex_List set Woolinte_BillIndex_List.Discount=b.price 
         From Woolinte_BillIndex a,xw_P_PriceFollow b 
         where  a.billnumberid=@BillNumberID  and Woolinte_BillIndex_List.billnumberid = a.billnumberid 
         And a.BTypeID_ = b.BTypeID and b.PRTypeID='0008'
         and Woolinte_BillIndex_List.PTypeID_=b.PTypeID and Woolinte_BillIndex_List.uintnum=b.UnitID+1 
         and (a.billtype='Sale' or a.billtype='SaleBack' or a.billtype='SaleOrder' or @BillType='Retail')  AND Woolinte_BillIndex_List.DetailType='0'
        if(@@ERROR<>0)
            goto Error1 
            end
         end
         if(@BillType='Buy' or @BillType='BuyBack' or @BillType='BuyOrder')
         begin
            if(@PriceTrack='1')
            begin
           ---进货类单据取价格跟踪
           
         Update  Woolinte_BillIndex_List set Unitprice=b.price
         From Woolinte_BillIndex a,xw_P_PriceFollow b 
         where  a.billnumberid=@BillNumberID  and Woolinte_BillIndex_List.billnumberid = a.billnumberid 
         And a.BTypeID_ = b.BTypeID and b.PRTypeID='0005'
         and Woolinte_BillIndex_List.PTypeID_=b.PTypeID and Woolinte_BillIndex_List.uintnum=b.UnitID+1
         and (a.billtype='Buy' or a.billtype='BuyBack' or a.billtype='BuyOrder') 
                
         Update  Woolinte_BillIndex_List set Woolinte_BillIndex_List.Discount=b.price 
         From Woolinte_BillIndex a,xw_P_PriceFollow b 
         where  a.billnumberid=@BillNumberID  and Woolinte_BillIndex_List.billnumberid = a.billnumberid 
         And a.BTypeID_ = b.BTypeID and b.PRTypeID='0006'
         and Woolinte_BillIndex_List.PTypeID_=b.PTypeID and Woolinte_BillIndex_List.uintnum=b.UnitID+1 
          and (a.billtype='Buy' or a.billtype='BuyBack' or a.billtype='BuyOrder') 
         
            
        if(@@ERROR<>0)
            goto Error1 
            end 
         end 
         
       
         		     
		     
        IF (@BillType='SameMove' OR @BillType='OtherOutBill'  or  @BillType='OtherInBill')
		begin         
         Update Woolinte_BillIndex_List Set PDADistPrice =G.Price*Woolinte_BillIndex_List.UintRate,Unitprice=G.Price*Woolinte_BillIndex_List.UintRate
         From Woolinte_BillIndex A,GoodsStocks G where Woolinte_BillIndex_List.billnumberid=A.billnumberid
         and Woolinte_BillIndex_List.PTypeID_=G.PTypeID and G.KTypeID=A.KTypeID_ and A.billnumberid=@BillNumberID;
          if(@@ERROR<>0)
            goto Error1 
         end 
 
 
         ---更新计算表体的，参与计算的合计金额
       --  Update Woolinte_BillIndex_List set BillTotal=BillTotal,BillPrice=Unitprice/uintrate,BillQty=uintrate*PDAQty,
      --   BillDistPrice=(Unitprice/uintrate)*Discount where billnumberid=@BillNumberID;  
          Update Woolinte_BillIndex_List set BillTotal=BillTotal,BillPrice= case when cast(uintrate as DECIMAL(10,2))='0.00' then Unitprice else  Unitprice/uintrate END,BillQty=uintrate*PDAQty,
         BillDistPrice=case when cast(uintrate as DECIMAL(10,2))='0.00' then Unitprice*Discount else  (Unitprice/uintrate)*Discount END where billnumberid=@BillNumberID;  
         
        ---赋值表头的合计金额
       -- Update Woolinte_BillIndex set DlySumTotal=Sum_Total From Woolinte_BillIndex a ,(Select BillNumberID,sum(PDAPrice*PDADiscount*PDAQty) as --Sum_Total
	  --  From Woolinte_BillIndex_List where billnumberid=@BillNumberID group by billnumberid ) b where a.billnumberid=b.billnumberid
		-- if(@@ERROR<>0)
                --   goto Error1 
                         ---赋值表头的合计金额      销售退换货单金额处理
         if @BillType='SaleBackChange'
         begin
             Update Woolinte_BillIndex set DlySumTotal=Sum_Total From Woolinte_BillIndex a ,( select BillNumberID,sum((case WHEN usedtype='2'  THEN Sum_Total else '0' end)-(case WHEN usedtype='1'  THEN Sum_Total else '0' end)) Sum_Total from (
        Select BillNumberID,  usedtype, sum(PDAPrice*PDADiscount*PDAQty) as Sum_Total
	    From Woolinte_BillIndex_List where billnumberid=@BillNumberID 
	    group by billnumberid,UsedType)hh    group by billnumberid ) b where a.billnumberid=b.billnumberid
		 if(@@ERROR<>0)
                   goto Error1 
          end
         else 
         BEGIN
        Update Woolinte_BillIndex set DlySumTotal=Sum_Total From Woolinte_BillIndex a ,(Select BillNumberID,sum(PDAPrice*PDADiscount*PDAQty) as Sum_Total
	    From Woolinte_BillIndex_List where billnumberid=@BillNumberID group by billnumberid ) b where a.billnumberid=b.billnumberid
		 if(@@ERROR<>0)
                   goto Error1 
             END 
		 if @BillType='SaleBackChange'
		 begin
		 set @BillTypeID='142'
		 insert into dlyndx(date,number,vchtype,summary,Comment,BTypeID,ETypeID,KTypeID,ktypeid2,period,redword,redold,inputno,draft,total,billtype, 
                               gatheringdate,deptid,postno,yearperiod,ifcheck) 
                                 Select billdate,@number,@BillTypeID,Comment,@BillComment,BTypeID_,ETypeID_,KTypeID2_,KTypeID_,
                                  '0','F','F',UserTypeID,'1',DlySumTotal,'0',billdate,DTypeID_,UserTypeID,'1','' From Woolinte_BillIndex where billnumberid=@BillNumberID 
           if(@@ERROR<>0)
                   goto Error1 
           Select @VchCode=@@IDENTITY;
		 end
if ( @BillType='Retail')
		 begin
		 set @BillTypeID='12'
		 insert into dlyndx(date,number,vchtype,summary,Comment,BTypeID,ETypeID,KTypeID,period,redword,redold,inputno,draft,total,billtype, 
                               gatheringdate,deptid,postno,yearperiod,ifcheck,VipCardID) 
                                 Select billdate,@number,@BillTypeID,Comment,@BillComment,BTypeID_,ETypeID_,KTypeID_, 
                                  '0','F','F',UserTypeID,'1',DlySumTotal,@BillTypeID,billdate,DTypeID_,UserTypeID,'1','',case WHEN vipcardid='' THEN '0' ELSE vipcardid end From Woolinte_BillIndex where billnumberid=@BillNumberID 
           if(@@ERROR<>0)
                   goto Error1 
           Select @VchCode=@@IDENTITY;
		 end
 if (@BillType='Retail')
          begin
          insert into bakdly(vchcode,aTypeID,BTypeID,ETypeID,KTypeID,KTypeID2,PTypeID,qty,discount,discountprice,price,total,taxprice,Comment,date, 
          usedtype,tax_total,discounttotal,vchtype,costmode,Redword,Unit,assqty,assprice,assdiscountprice,asstaxprice,Unitrate,RetailPrice,InPrice,InTotal,Memo,PStutas,Blockno,ProDate,usefulenddate,costprice,OrderCode,OrderDlyCode)
          Select @VchCode,'0000100001',BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_,Woolinte_BillIndex.KTypeID2_,PTypeID_,Sum(BillQty),Woolinte_BillIndex_List.Discount,
          BillDistPrice,BillPrice,sum(PDAQty*PDAPrice), BillDistPrice,Woolinte_BillIndex_List.Comment,billdate,'1',Sum(PDADistPrice*PDAQty),
          Sum(PDADistPrice*PDAQty),@BillTypeID,'0','F',uintNum-1, 
          Sum(PDAQty),Unitprice, UnitPrice*Woolinte_BillIndex_List.Discount,UnitPrice*Woolinte_BillIndex_List.Discount,uintrate,Woolinte_BillIndex_List.RetailPrice,
          Woolinte_BillIndex_List.CostPrice,Sum(Woolinte_BillIndex_List.CostPrice*BillQty),Woolinte_BillIndex.Num,dbo.Woolinte_BillIndex_List.DetailType,dbo.Woolinte_BillIndex_List.GoodsNumber,dbo.Woolinte_BillIndex_List.ProduceDate,Woolinte_BillIndex_List.validdate,dbo.Woolinte_BillIndex_List.CostPrice,CASE when LEN(RTRIM(Woolinte_BillIndex_List.OrderID))=0 THEN '-1' WHEN Woolinte_BillIndex_List.OrderID='0' THEN '0' ELSE Woolinte_BillIndex_List.OrderID end,
          isnull(br.DlyOrder,'0')
          From Woolinte_BillIndex,Woolinte_BillIndex_List
          left join BakDlyOrder br on br.ptypeid=Woolinte_BillIndex_List.PTypeID_ and CAST(br.vchcode AS VARCHAR)=Woolinte_BillIndex_List.OrderID
          where Woolinte_BillIndex_List.billnumberid=Woolinte_BillIndex.billnumberid  
          and Woolinte_BillIndex.billnumberid=@BillNumberID 
          Group By BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_,Woolinte_BillIndex.KTypeID2_,PTypeID_,Woolinte_BillIndex_List.Discount,Woolinte_BillIndex_List.OrderID,
          BillDistPrice,BillPrice,Woolinte_BillIndex_List.Comment,billdate,uintNum,Unitprice,UintRate,br.DlyOrder,
          Woolinte_BillIndex_List.RetailPrice,Woolinte_BillIndex_List.CostPrice,Woolinte_BillIndex.Num, PDADistPrice,UnitPrice,Woolinte_BillIndex_List.Discount,Woolinte_BillIndex_List.validdate,dbo.Woolinte_BillIndex_List.DetailType,Woolinte_BillIndex_List.DetailRowID,dbo.Woolinte_BillIndex_List.GoodsNumber,dbo.Woolinte_BillIndex_List.ProduceDate,dbo.Woolinte_BillIndex_List.CostPrice,Woolinte_BillIndex_List.BillDistTotal ORDER BY Woolinte_BillIndex_List.DetailRowID;
          
          
          if(@@ERROR<>0)
                goto Error1
          end
		 
		 if @BillType='Sale'
		 begin
		 set @BillTypeID='11'
		 insert into dlyndx(date,number,vchtype,summary,Comment,BTypeID,ETypeID,KTypeID,period,redword,redold,inputno,draft,total,billtype, 
                               gatheringdate,deptid,postno,yearperiod,ifcheck,VipCardID,SaveCardID) 
                                 Select billdate,@number,@BillTypeID,Comment,@BillComment,BTypeID_,ETypeID_,KTypeID_, 
                                '0','F','F',UserTypeID,'1',DlySumTotal,'0',billdate,DTypeID_,UserTypeID,'1','',case WHEN vipcardid='' THEN '0' ELSE vipcardid end,case WHEN vipcardid='' THEN '0' ELSE vipcardid end From Woolinte_BillIndex where billnumberid=@BillNumberID 

           if(@@ERROR<>0)
                   goto Error1 
           Select @VchCode=@@IDENTITY;
		 end
		 if @BillType='Buy'
		 begin
		 
		    Update Woolinte_BillIndex_List Set CostPrice=BillDistPrice where @BillNumberID=@BillNumberID;
		    if(@@ERROR<>0)
                   goto Error1 
                   
		   set @BillTypeID='34'
		   insert into dlyndx(date,number,vchtype,summary,Comment,BTypeID,ETypeID,KTypeID,period,redword,redold,inputno,draft,total,billtype, 
                                gatheringdate,deptid,postno,yearperiod,ifcheck) Select billdate,@number,@BillTypeID,Comment,@BillComment,BTypeID_,ETypeID_,KTypeID_, 
                                '0','F','F',UserTypeID,'1',DlySumTotal,'0',billdate,DTypeID_,UserTypeID,'1','' From Woolinte_BillIndex where  billnumberid=@BillNumberID 
           if(@@ERROR<>0)
                   goto Error1 
            Select @VchCode=@@IDENTITY;
		 end
		 if @BillType='SaleBack'
		 begin
		    SET @BillTypeID='45'
		    insert into dlyndx(date,number,vchtype,summary,Comment,BTypeID,ETypeID,KTypeID,period,redword,redold,inputno,draft,total,billtype, 
                              gatheringdate,deptid,postno,yearperiod,ifcheck) Select billdate,@number,@BillTypeID,Comment,@BillComment,BTypeID_,ETypeID_,KTypeID_,
                               '0','F','F',UserTypeID,'1',DlySumTotal,'0',billdate,DTypeID_,UserTypeID,'1','' From Woolinte_BillIndex where  billnumberid=@BillNumberID 
            if(@@ERROR<>0)
                   goto Error1 
            Select @VchCode=@@IDENTITY;
		 end
		 if @BillType='BuyBack'
		  begin
		     SET @BillTypeID='6'
		     insert into dlyndx(date,number,vchtype,summary,Comment,BTypeID,ETypeID,KTypeID,period,redword,redold,inputno,draft,total,billtype,
                                gatheringdate,deptid,postno,yearperiod,ifcheck) Select billdate,@number,@BillTypeID,Comment,@BillComment,BTypeID_,ETypeID_,KTypeID_,
                             '0','F','F',UserTypeID,'1',DlySumTotal,'0',billdate,DTypeID_,UserTypeID,'1','' From Woolinte_BillIndex where  billnumberid=@BillNumberID 
             if(@@ERROR<>0)
                   goto Error1 
              Select @VchCode=@@IDENTITY;
		  END
		  if @BillType='Largess'
		  begin
		     SET @BillTypeID='139'
		     insert into dlyndx(date,number,vchtype,summary,Comment,BTypeID,ETypeID,KTypeID,period,redword,redold,inputno,draft,total,billtype, 
             gatheringdate,deptid,postno,yearperiod,ifcheck) Select billdate,@number,@BillTypeID,Comment,@BillComment,BTypeID_,ETypeID_,KTypeID_, 
             '0','F','F',UserTypeID,'1',DlySumTotal,'0',billdate,DTypeID_,UserTypeID,'1','' From Woolinte_BillIndex where  billnumberid=@BillNumberID 
              if(@@ERROR<>0)
                   goto Error1 
              Select @VchCode=@@IDENTITY;
		  END
		  if @BillType='SaleOrder'
		  begin
		  SET @BillTypeID='8'
		 
             select @statee= count(*) from InputLevel where vchtype = 8
             if(@statee='0')
             BEGIN
             set @statee='-1';
             set @typee='2';
             end
             ELSE
               BEGIN
             set @statee='0';
             set @typee='-1';
             end
		  insert into DlyNdxOrder(date,number,vchtype,summary,Comment,BTypeID,ETypeID,KTypeID,period,redword,redold,inputno,draft,total,billtype, 
                              todate,deptid,yearperiod,ifcheck,VchtypeState,AuditState) 
                 Select billdate,@number,@BillTypeID,Comment,@BillComment,BTypeID_,ETypeID_,KTypeID_,'0','F','F',UserTypeID,'0',DlySumTotal,@BillTypeID, 
                 convert(varchar(10),LoginDate,120),DTypeID_,'2','',@typee,@statee From Woolinte_BillIndex where  billnumberid=@BillNumberID 
                 if(@@ERROR<>0)
                   goto Error1 
                  Select @VchCode=@@IDENTITY;
		  END
		  if @BillType='BuyOrder'
		  begin
		    SET @BillTypeID='7'
		     select @statee= count(*) from InputLevel where vchtype = @BillTypeID
             if(@statee='0')
             BEGIN
             set @statee='-1';
             end
             ELSE
               BEGIN
             set @statee='0';
             end
		    insert into DlyNdxOrder(date,number,vchtype,summary,Comment,BTypeID,ETypeID,KTypeID,period,redword,redold,inputno,draft,total,billtype, 
                 todate,deptid,yearperiod,ifcheck,AuditState)
             Select billdate,@number,@BillTypeID,Comment,@BillComment,BTypeID_,ETypeID_,KTypeID_,'0','F','F',UserTypeID,'0',DlySumTotal,@BillTypeID, 
             convert(varchar(10),LoginDate,120),DTypeID_,'2','',@statee From Woolinte_BillIndex where billnumberid=@BillNumberID 
            if(@@ERROR<>0)
                   goto Error1 
            Select @VchCode=@@IDENTITY;
		  end
		   ---收款单
		   if (@BillType='CollectionBill')
		   begin
		   set @BillTypeID='4'
           END
            ---付款单
           if (@BillType='PayBill')
		   begin
		   set @BillTypeID='66'
           END
           
           --现金费用单
           if (@BillType='CashBill')
		   begin
	       set @BillTypeID='35'
		   END
		 
  --一般费用单
           if (@BillType='MoneyBill')
		   begin
	       set @BillTypeID='36'
		   END
		  if (@BillType='CollectionBill' or @BillType='CashBill' or @BillType='PayBill' or  @BillType='MoneyBill' )
		  begin
		     insert into dlyndx(date,number,vchtype,summary,Comment,BTypeID,ETypeID,KTypeID,period,redword,redold,inputno,draft,total,billtype, 
             gatheringdate,deptid,postno,yearperiod,ifcheck) Select billdate,@number,@BillTypeID,Woolinte_BillIndex.Comment,@BillComment,BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_, 
             '0','F','F',UserTypeID,'1',sum(Woolinte_BillIndex_List.BillPrice),'0',billdate,DTypeID_,UserTypeID,'1','' From Woolinte_BillIndex,Woolinte_BillIndex_List where  Woolinte_BillIndex.billnumberid=@BillNumberID and Woolinte_BillIndex.BillNumberID=Woolinte_BillIndex_List.BillNumberID
              group by billdate,Woolinte_BillIndex.Comment,BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_,UserTypeID,DTypeID_,UserTypeID
              if(@@ERROR<>0)
                   goto Error1 
              Select @VchCode=@@IDENTITY;
		  END
		  
		  IF @BillType='SameMove'
		  begin
		     SET @BillTypeID='17'
 --------插入的第8.9字段对调下，管家婆10.1 中出入库仓库一致
            insert into dlyndx(date,number,vchtype,summary,Comment,BTypeID,ETypeID,KTypeID,KTypeID2,period,redword,redold,inputno,draft,total,billtype, 
             gatheringdate,deptid,postno,yearperiod,ifcheck) Select billdate,@number,@BillTypeID,Comment,@BillComment,
             BTypeID_,ETypeID_,KTypeID2_, KTypeID_,'0','F','F',UserTypeID,'1',DlySumTotal,@BillTypeID,billdate,DTypeID_,UserTypeID,'1','' From Woolinte_BillIndex 
             where  billnumberid=@BillNumberID 
              if(@@ERROR<>0)
                   goto Error1 
              Select @VchCode=@@IDENTITY;
		  END
		  
		  IF @BillType='DistinctMove'
		  begin
		     SET @BillTypeID='21'
		     
		     Update Woolinte_BillIndex_List Set CostPrice=PDADistPrice where @BillNumberID=@BillNumberID;
		     if(@@ERROR<>0)
              goto Error1 
                   
		     insert into dlyndx(date,number,vchtype,summary,Comment,BTypeID,ETypeID,KTypeID,KTypeID2,period,redword,redold,inputno,draft,total,billtype, 
             gatheringdate,deptid,postno,yearperiod,ifcheck) Select billdate,@number,@BillTypeID,Comment,@BillComment,
             BTypeID_,ETypeID_,KTypeID_,KTypeID2_,'0','F','F',UserTypeID,'1',DlySumTotal,'0',billdate,DTypeID_,UserTypeID,
             '1','' From Woolinte_BillIndex where  billnumberid=@BillNumberID 
              if(@@ERROR<>0)
                   goto Error1 
              Select @VchCode=@@IDENTITY;
		  END
		  
		 		  
		  IF @BillType='OtherOutBill'
		  begin
		     SET @BillTypeID='139'
		     
		     insert into dlyndx(date,number,vchtype,summary,Comment,BTypeID,ETypeID,KTypeID,KTypeID2,period,redword,redold,inputno,draft,total,billtype, 
             gatheringdate,deptid,postno,yearperiod,ifcheck,atypeid) 
             Select billdate,@number,@BillTypeID,Comment,@BillComment,
             BTypeID_,ETypeID_,KTypeID_,KTypeID2_,'0','F','F',UserTypeID,'1','0','0',
             billdate,DTypeID_,UserTypeID,'1','','000040000200002' From Woolinte_BillIndex where  billnumberid=@BillNumberID 
              if(@@ERROR<>0)
                   goto Error1 
              Select @VchCode=@@IDENTITY;
		  END
		  
		  if @BillType='OtherOutBill' 
          begin
          insert into bakdly(vchcode,aTypeID,BTypeID,ETypeID,KTypeID,KTypeID2,PTypeID,qty,discount,discountprice,price,total,taxprice,Comment,date, 
          usedtype,tax_total,discounttotal,vchtype,costmode,Redword,Unit,
          assqty,assprice,assdiscountprice,asstaxprice,Unitrate,RetailPrice,InPrice,InTotal,Memo)
          Select @VchCode,'0000100001',BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_,Woolinte_BillIndex.KTypeID2_,PTypeID_,Sum(BillQty),Discount,PDADistPrice,BillPrice,sum(PDAQty*PDAPrice), PDADistPrice,Woolinte_BillIndex_List.Comment,billdate,
          '1',Sum(PDADistPrice*PDAQty),Sum(PDADistPrice*PDAQty),@BillTypeID,'0','F',uintNum-1, 
          Sum(PDAQty),Unitprice, UnitPrice*Discount,UnitPrice*Discount,uintrate,Woolinte_BillIndex_List.RetailPrice,
          CostPrice,Sum(CostPrice*BillQty),Woolinte_BillIndex.Num
          From Woolinte_BillIndex,Woolinte_BillIndex_List where Woolinte_BillIndex_List.billnumberid=Woolinte_BillIndex.billnumberid  
          and Woolinte_BillIndex.billnumberid=@BillNumberID 
          Group By BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_,Woolinte_BillIndex.KTypeID2_,PTypeID_,Discount,
          BillDistPrice,BillPrice,Woolinte_BillIndex_List.Comment,billdate,uintNum,Unitprice,UintRate,PDADistPrice,
          Woolinte_BillIndex_List.RetailPrice,CostPrice,Woolinte_BillIndex.Num, UnitPrice,Discount,DetailRowID order by DetailRowID ASC;
          
          
          if(@@ERROR<>0)
                goto Error1 
          END

 IF @BillType='OtherInBill'
		  begin
		     SET @BillTypeID='126'
		     
		     insert into dlyndx(date,number,vchtype,summary,Comment,BTypeID,ETypeID,KTypeID,KTypeID2,period,redword,redold,inputno,draft,total,billtype, 
             gatheringdate,deptid,postno,yearperiod,ifcheck,atypeid) 
             Select billdate,@number,@BillTypeID,Comment,@BillComment,
             BTypeID_,ETypeID_,KTypeID_,KTypeID2_,'0','F','F',UserTypeID,'1','0','0',
             billdate,DTypeID_,UserTypeID,'1','','000030000200002' From Woolinte_BillIndex where  billnumberid=@BillNumberID 
              if(@@ERROR<>0)
                   goto Error1 
              Select @VchCode=@@IDENTITY;
		  END
		  
		  if @BillType='OtherInBill' 
          begin
          insert into bakdly(vchcode,aTypeID,BTypeID,ETypeID,KTypeID,KTypeID2,PTypeID,qty,discount,discountprice,price,total,taxprice,Comment,date, 
          usedtype,tax_total,discounttotal,vchtype,costmode,Redword,Unit,
          assqty,assprice,assdiscountprice,asstaxprice,Unitrate,RetailPrice,InPrice,InTotal,Memo)
          Select @VchCode,'0000100001',BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_,Woolinte_BillIndex.KTypeID2_,PTypeID_,Sum(BillQty),Discount,PDADistPrice,BillPrice,sum(PDAQty*PDAPrice), PDADistPrice,Woolinte_BillIndex_List.Comment,billdate,
          '1',Sum(PDADistPrice*PDAQty),Sum(PDADistPrice*PDAQty),@BillTypeID,'0','F',uintNum-1, 
          Sum(PDAQty),Unitprice, UnitPrice*Discount,UnitPrice*Discount,uintrate,Woolinte_BillIndex_List.RetailPrice,
          CostPrice,Sum(CostPrice*BillQty),Woolinte_BillIndex.Num
          From Woolinte_BillIndex,Woolinte_BillIndex_List where Woolinte_BillIndex_List.billnumberid=Woolinte_BillIndex.billnumberid  
          and Woolinte_BillIndex.billnumberid=@BillNumberID 
          Group By BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_,Woolinte_BillIndex.KTypeID2_,PTypeID_,Discount,
          BillDistPrice,BillPrice,Woolinte_BillIndex_List.Comment,billdate,uintNum,Unitprice,UintRate,PDADistPrice,
          Woolinte_BillIndex_List.RetailPrice,CostPrice,Woolinte_BillIndex.Num, UnitPrice,Discount,DetailRowID order by DetailRowID ASC;
          
          
          if(@@ERROR<>0)
                goto Error1 
          END
		 
		  
		 if (@BillType='CollectionBill' or @BillType='CashBill' or @BillType='PayBill' or  @BillType='MoneyBill' )
          begin
          insert into bakdly(vchcode,aTypeID,BTypeID,ETypeID,KTypeID,KTypeID2,PTypeID,qty,discount,discountprice,price,total,taxprice,Comment,date, 
          usedtype,tax_total,discounttotal,vchtype,costmode,Redword,Unit,assqty,assprice,assdiscountprice,asstaxprice,Unitrate,RetailPrice,InPrice,InTotal,Memo)
          Select @VchCode,PTypeID_,BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_,Woolinte_BillIndex.KTypeID2_,PTypeID_,'1',Discount,
          PDADistPrice,BillPrice,Sum(1*BillPrice), PDADistPrice,Woolinte_BillIndex_List.Comment,billdate,'1',Sum(BillDistPrice*1),
          Sum(BillDistPrice*1),@BillTypeID,'0','F',uintNum-1, 
          Sum(PDAQty),Unitprice,UnitPrice*Discount,UnitPrice*Discount,uintrate,Woolinte_BillIndex_List.RetailPrice,
          CostPrice,Sum(CostPrice*1),Woolinte_BillIndex.Num
          From Woolinte_BillIndex,Woolinte_BillIndex_List where Woolinte_BillIndex_List.billnumberid=Woolinte_BillIndex.billnumberid  
          and Woolinte_BillIndex.billnumberid=@BillNumberID 
          Group By BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_,Woolinte_BillIndex.KTypeID2_,PTypeID_,Discount,
          BillDistPrice,BillPrice,Woolinte_BillIndex_List.Comment,billdate,uintNum,Unitprice,UintRate,PDADistPrice,
          Woolinte_BillIndex_List.RetailPrice,CostPrice,Woolinte_BillIndex.Num,UnitPrice,Discount,Woolinte_BillIndex_List.DetailRowID ORDER BY Woolinte_BillIndex_List.DetailRowID
          
           END
		  
		  if (@BillType='Sale')
          begin
        insert into bakdly(vchcode,aTypeID,BTypeID,ETypeID,KTypeID,KTypeID2,PTypeID,qty,discount,discountprice,price,total,taxprice,Comment,date, 
          usedtype,tax_total,discounttotal,vchtype,costmode,Redword,Unit,assqty,assprice,assdiscountprice,asstaxprice,Unitrate,RetailPrice,InPrice,InTotal,Memo,PStutas,Blockno,ProDate,usefulenddate,
          costprice,OrderCode,OrderDlyCode,VipCardId,ordervchtype,ubarcode)
          Select @VchCode,'0000100001',BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_,Woolinte_BillIndex.KTypeID2_,PTypeID_,Sum(BillQty),Woolinte_BillIndex_List.Discount,
          unitPrice*Pdadiscount,unitPrice,sum(PDAQty*unitPrice),unitPrice*Pdadiscount,Woolinte_BillIndex_List.Comment,billdate,'1',Sum(unitPrice*Pdadiscount*PDAQty),
          Sum(unitPrice*Pdadiscount*PDAQty),@BillTypeID,'0','F',uintNum-1, 
          Sum(PDAQty),Unitprice, UnitPrice*Woolinte_BillIndex_List.Discount,UnitPrice*Woolinte_BillIndex_List.Discount,uintrate,Woolinte_BillIndex_List.RetailPrice,
          Woolinte_BillIndex_List.CostPrice,Sum(Woolinte_BillIndex_List.CostPrice*BillQty),Woolinte_BillIndex.Num,dbo.Woolinte_BillIndex_List.DetailType,dbo.Woolinte_BillIndex_List.GoodsNumber,dbo.Woolinte_BillIndex_List.ProduceDate,woolinte_billindex_list.validdate,dbo.Woolinte_BillIndex_List.CostPrice,
          Woolinte_BillIndex.ordercode, CASE when LEN(RTRIM(Woolinte_BillIndex_List.OrderID))=0 THEN '-1' WHEN Woolinte_BillIndex_List.OrderID='0' THEN '0' ELSE Woolinte_BillIndex_List.OrderID end,
           case WHEN Woolinte_BillIndex.vipcardid='' THEN '0' ELSE vipcardid end,'8',isnull(xw_PtypeBarCode.barcode,'0') 
          From Woolinte_BillIndex,Woolinte_BillIndex_List
          left join BakDlyOrder br on br.ptypeid=Woolinte_BillIndex_List.PTypeID_  and DetailType=br.PStutas and CAST(br.vchcode AS VARCHAR)=Woolinte_BillIndex_List.OrderID
          left join
           xw_PtypeBarCode on  xw_PtypeBarCode.ptypeid=PTypeID_ and unitid=uintNum-1 and ordid='0'
           where Woolinte_BillIndex_List.billnumberid=Woolinte_BillIndex.billnumberid  
          and Woolinte_BillIndex.billnumberid=@BillNumberID 
          Group By BTypeID_,Woolinte_BillIndex.ordercode,ETypeID_,Woolinte_BillIndex.KTypeID_,woolinte_billindex_list.validdate,Woolinte_BillIndex.KTypeID2_,PTypeID_,Woolinte_BillIndex_List.Discount,Woolinte_BillIndex_List.OrderID,
          BillDistPrice,BillPrice,Woolinte_BillIndex_List.Comment,billdate,uintNum,Unitprice,UintRate,br.DlyOrder,
          Woolinte_BillIndex_List.RetailPrice,Woolinte_BillIndex.vipcardid,Woolinte_BillIndex_List.CostPrice,Woolinte_BillIndex.Num, UnitPrice,Woolinte_BillIndex_List.Discount,dbo.Woolinte_BillIndex_List.DetailType,DetailRowID ,PDADistPrice,
          dbo.Woolinte_BillIndex_List.GoodsNumber,dbo.Woolinte_BillIndex_List.ProduceDate,xw_PtypeBarCode.barcode ,dbo.Woolinte_BillIndex_List.CostPrice,BillDistTotal,BillTotal,PDADiscount
          order by DetailRowID ASC;
          
          
          if(@@ERROR<>0)
                goto Error1
          end
		  
		  
		 if @BillType='SameMove' 
          begin
          insert into bakdly(vchcode,aTypeID,BTypeID,ETypeID,KTypeID,KTypeID2,PTypeID,qty,discount,discountprice,price,total,taxprice,Comment,date, 
          usedtype,tax_total,discounttotal,vchtype,costmode,Redword,Unit,assqty,assprice,assdiscountprice,asstaxprice,Unitrate,RetailPrice,InPrice,InTotal,Memo,Blockno,ProDate,usefulenddate,costtotal,costprice)
          Select @VchCode,'0000100001',BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID2_,Woolinte_BillIndex.KTypeID_,PTypeID_,Sum(BillQty),Discount,
          PDADistPrice,PDADistPrice,sum(PDAQty*PDADistPrice), PDADistPrice,Woolinte_BillIndex_List.Comment,billdate,'1',sum(PDAQty*PDADistPrice),
          Sum(PDADistPrice*PDAQty),@BillTypeID,'0','F',uintNum-1, 
          Sum(PDAQty),Unitprice, UnitPrice*Discount,UnitPrice*Discount,uintrate,Woolinte_BillIndex_List.RetailPrice,
          CostPrice,Sum(CostPrice*BillQty),Woolinte_BillIndex.Num,Woolinte_BillIndex_List.GoodsNumber,dbo.Woolinte_BillIndex_List.ProduceDate,woolinte_billindex_list.validdate,sum(PDAQty*PDADistPrice), PDADistPrice
          From Woolinte_BillIndex,Woolinte_BillIndex_List where Woolinte_BillIndex_List.billnumberid=Woolinte_BillIndex.billnumberid  
          and Woolinte_BillIndex.billnumberid=@BillNumberID 
          Group By BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_,Woolinte_BillIndex.KTypeID2_,PTypeID_,Discount,
          BillDistPrice,BillPrice,Woolinte_BillIndex_List.Comment,billdate,uintNum,Unitprice,UintRate,PDADistPrice,
          Woolinte_BillIndex_List.RetailPrice,CostPrice,Woolinte_BillIndex.Num, UnitPrice,Discount,Woolinte_BillIndex_List.GoodsNumber,dbo.Woolinte_BillIndex_List.ProduceDate,woolinte_billindex_list.validdate,woolinte_billindex_list.validdate,Woolinte_BillIndex_List.DetailRowID,BillTotal,BillDistTotal ORDER BY Woolinte_BillIndex_List.DetailRowID;
          
          
          if(@@ERROR<>0)
                goto Error1 
                END
		  
		  if (@BillType='SaleBackChange')
          begin
           insert into bakdly
 (vchcode,aTypeID,BTypeID,ETypeID,KTypeID,KTypeID2,PTypeID,qty,discount,discountprice,price,total,taxprice,
 Comment,date,usedtype,tax_total,discounttotal,vchtype,costmode,Redword,Unit,assqty,assprice,assdiscountprice,asstaxprice,Unitrate,
 RetailPrice,InPrice,InTotal,Memo,PStutas,Blockno,ProDate,usefulenddate,costprice,costtotal,OrderCode,OrderDlyCode,VipCardId,ordervchtype,ubarcode)
 
 Select @VchCode,'0000100001',BTypeID_,ETypeID_,Woolinte_BillIndex_list.KTypeID_,Woolinte_BillIndex_list.KTypeID2_,PTypeID_,
 Sum(BillQty),Woolinte_BillIndex_List.Discount,unitPrice*Pdadiscount,unitPrice,sum(PDAQty*unitPrice), unitPrice*Pdadiscount,Woolinte_BillIndex_List.Comment,
 billdate,dbo.Woolinte_BillIndex_List.UsedType,Sum(unitPrice*Pdadiscount*PDAQty),Sum(unitPrice*Pdadiscount*PDAQty),@BillTypeID,
 '0','F',uintNum-1,Sum(PDAQty),Unitprice, UnitPrice*Woolinte_BillIndex_List.Discount,UnitPrice*Woolinte_BillIndex_List.Discount,uintrate,Woolinte_BillIndex_List.RetailPrice,Woolinte_BillIndex_List.CostPrice,
 Sum(Woolinte_BillIndex_List.CostPrice*BillQty),Woolinte_BillIndex.Num,DetailType,
dbo.Woolinte_BillIndex_List.GoodsNumber,dbo.Woolinte_BillIndex_List.ProduceDate,woolinte_billindex_list.validdate,
dbo.Woolinte_BillIndex_List.CostPrice,dbo.Woolinte_BillIndex_List.CostPrice*Sum(PDAQty),Woolinte_BillIndex.ordercode, 
CASE when LEN(RTRIM(Woolinte_BillIndex_List.OrderID))=0 THEN '-1' WHEN Woolinte_BillIndex_List.OrderID='0'
 THEN '0' ELSE Woolinte_BillIndex_List.OrderID end,
case WHEN Woolinte_BillIndex.vipcardid='' THEN '0' ELSE vipcardid end,'8',isnull(xw_PtypeBarCode.barcode,'') 
From Woolinte_BillIndex,Woolinte_BillIndex_List
left join BakDlyOrder br on br.ptypeid=Woolinte_BillIndex_List.PTypeID_  and DetailType=br.PStutas and CAST(br.vchcode AS VARCHAR)=Woolinte_BillIndex_List.OrderID
left join xw_PtypeBarCode on  xw_PtypeBarCode.ptypeid=PTypeID_ and unitid=uintNum-1 and ordid='0'
where Woolinte_BillIndex_List.billnumberid=Woolinte_BillIndex.billnumberid  
and Woolinte_BillIndex.billnumberid=@BillNumberID 
Group By dbo.Woolinte_BillIndex_List.UsedType,Woolinte_BillIndex_list.KTypeID_,Woolinte_BillIndex_list.KTypeID2_,BTypeID_,Woolinte_BillIndex.ordercode,ETypeID_,Woolinte_BillIndex.KTypeID_,woolinte_billindex_list.validdate,Woolinte_BillIndex.KTypeID2_,PTypeID_,Woolinte_BillIndex_List.Discount,Woolinte_BillIndex_List.OrderID,
BillDistPrice,BillPrice,Woolinte_BillIndex_List.Comment,billdate,uintNum,Unitprice,UintRate,br.DlyOrder,
Woolinte_BillIndex_List.RetailPrice,Woolinte_BillIndex.vipcardid,Woolinte_BillIndex_List.CostPrice,Woolinte_BillIndex.Num, UnitPrice,Woolinte_BillIndex_List.Discount,dbo.Woolinte_BillIndex_List.DetailType,DetailRowID ,PDADistPrice,
dbo.Woolinte_BillIndex_List.GoodsNumber,dbo.Woolinte_BillIndex_List.ProduceDate,xw_PtypeBarCode.barcode ,dbo.Woolinte_BillIndex_List.CostPrice,BillDistTotal,BillTotal,PDADiscount
order by DetailRowID ASC;

          if(@@ERROR<>0)
                goto Error1 
                END
  if (@BillType='Buy')
          begin
        
          insert into bakdly(vchcode,aTypeID,BTypeID,ETypeID,KTypeID,KTypeID2,PTypeID,qty,discount,discountprice,price,total,taxprice,Comment,date, 
          usedtype,tax_total,discounttotal,vchtype,costmode,Redword,Unit,assqty,assprice,assdiscountprice,asstaxprice,Unitrate,RetailPrice,InPrice,InTotal,Memo,PStutas,Blockno,ProDate,costprice,OrderCode,OrderDlyCode,UsefulEndDate)
          Select @VchCode,'0000100001',BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_,Woolinte_BillIndex.KTypeID2_,PTypeID_,Sum(BillQty),Woolinte_BillIndex_List.Discount,
          unitPrice*Pdadiscount,unitPrice,sum(PDAQty*unitPrice),unitPrice*Pdadiscount,Woolinte_BillIndex_List.Comment,billdate,'1',Sum(unitPrice*Pdadiscount*PDAQty),
         Sum(unitPrice*Pdadiscount*PDAQty),@BillTypeID,'0','F',uintNum-1, 
          Sum(PDAQty),Unitprice,UnitPrice*Woolinte_BillIndex_List.Discount,UnitPrice*Woolinte_BillIndex_List.Discount,uintrate,Woolinte_BillIndex_List.RetailPrice,
          Woolinte_BillIndex_List.CostPrice,Sum(Woolinte_BillIndex_List.CostPrice*BillQty),Woolinte_BillIndex.Num,
          dbo.Woolinte_BillIndex_List.DetailType,dbo.Woolinte_BillIndex_List.GoodsNumber,dbo.Woolinte_BillIndex_List.ProduceDate,dbo.Woolinte_BillIndex_List.CostPrice,CASE when LEN(RTRIM(Woolinte_BillIndex_List.OrderID))=0 THEN '-1' WHEN Woolinte_BillIndex_List.OrderID='0' THEN '0' ELSE Woolinte_BillIndex_List.OrderID end,
          isnull(br.DlyOrder,'0'),ValidDate
          From Woolinte_BillIndex,Woolinte_BillIndex_List
          left join BakDlyOrder br on br.ptypeid=Woolinte_BillIndex_List.PTypeID_  and DetailType=br.PStutas and CAST(br.vchcode AS VARCHAR)=Woolinte_BillIndex_List.OrderID
          where Woolinte_BillIndex_List.billnumberid=Woolinte_BillIndex.billnumberid  
          and Woolinte_BillIndex.billnumberid=@BillNumberID 
           Group By Pdadiscount,BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_,Woolinte_BillIndex.KTypeID2_,PTypeID_,Woolinte_BillIndex_List.Discount,Woolinte_BillIndex_List.OrderID,
          BillDistPrice,BillPrice,Woolinte_BillIndex_List.Comment,billdate,uintNum,Unitprice,UintRate,br.DlyOrder,
          Woolinte_BillIndex_List.RetailPrice,Woolinte_BillIndex_List.CostPrice,Woolinte_BillIndex.Num, PDADistPrice,UnitPrice,Woolinte_BillIndex_List.Discount,dbo.Woolinte_BillIndex_List.DetailType,DetailRowID ,BillTotal,BillDistTotal
       ,ValidDate   ,dbo.Woolinte_BillIndex_List.GoodsNumber,dbo.Woolinte_BillIndex_List.ProduceDate,dbo.Woolinte_BillIndex_List.CostPrice
          
          if(@@ERROR<>0)
                goto Error1 
                END
		  
		  if ( @BillType='SaleBack' or @BillType='BuyBack' or @BillType='Largess'
		      Or @BillType='DistinctMove')
          begin
        
          insert into bakdly(vchcode,aTypeID,BTypeID,ETypeID,KTypeID,KTypeID2,PTypeID,qty,discount,discountprice,price,total,taxprice,Comment,date, 
          usedtype,tax_total,discounttotal,vchtype,costmode,Redword,Unit,assqty,assprice,assdiscountprice,asstaxprice,Unitrate,RetailPrice,InPrice,InTotal,Memo,PStutas,Blockno,usefulenddate,ProDate,costprice,OrderCode,OrderDlyCode)
          Select @VchCode,'0000100001',BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_,Woolinte_BillIndex.KTypeID2_,PTypeID_,Sum(BillQty),Woolinte_BillIndex_List.Discount,
          unitPrice*Pdadiscount,unitPrice,sum(PDAQty*unitPrice), unitPrice*Pdadiscount,Woolinte_BillIndex_List.Comment,billdate,'1',Sum(unitPrice*Pdadiscount*PDAQty),
         Sum(unitPrice*Pdadiscount*PDAQty),@BillTypeID,'0','F',uintNum-1, 
          Sum(PDAQty),Unitprice, UnitPrice*Woolinte_BillIndex_List.Discount,UnitPrice*Woolinte_BillIndex_List.Discount,uintrate,Woolinte_BillIndex_List.RetailPrice,
          Woolinte_BillIndex_List.CostPrice,Sum(Woolinte_BillIndex_List.CostPrice*BillQty),Woolinte_BillIndex.Num,
           dbo.Woolinte_BillIndex_List.DetailType,dbo.Woolinte_BillIndex_List.GoodsNumber,woolinte_billindex_list.validdate,dbo.Woolinte_BillIndex_List.ProduceDate,dbo.Woolinte_BillIndex_List.CostPrice,CASE when LEN(RTRIM(Woolinte_BillIndex_List.OrderID))=0 THEN '-1' WHEN Woolinte_BillIndex_List.OrderID='0' THEN '0' ELSE Woolinte_BillIndex_List.OrderID end,
          isnull(br.DlyOrder,'0')
          From Woolinte_BillIndex,Woolinte_BillIndex_List
            left join BakDlyOrder br on br.ptypeid=Woolinte_BillIndex_List.PTypeID_  and DetailType=br.PStutas and CAST(br.vchcode AS VARCHAR)=Woolinte_BillIndex_List.OrderID
          where Woolinte_BillIndex_List.billnumberid=Woolinte_BillIndex.billnumberid  
          and Woolinte_BillIndex.billnumberid=@BillNumberID 
           Group By PDADiscount,BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_,Woolinte_BillIndex.KTypeID2_,PTypeID_,Woolinte_BillIndex_List.Discount,Woolinte_BillIndex_List.OrderID,
          BillDistPrice,BillPrice,Woolinte_BillIndex_List.Comment,billdate,uintNum,Unitprice,UintRate,br.DlyOrder,
          Woolinte_BillIndex_List.RetailPrice,Woolinte_BillIndex_List.CostPrice,Woolinte_BillIndex.Num, PDADistPrice,UnitPrice,Woolinte_BillIndex_List.Discount,dbo.Woolinte_BillIndex_List.DetailType,DetailRowID ,BillTotal,BillDistTotal
          ,dbo.Woolinte_BillIndex_List.GoodsNumber,dbo.Woolinte_BillIndex_List.ProduceDate,woolinte_billindex_list.validdate,dbo.Woolinte_BillIndex_List.CostPrice
          
          if(@@ERROR<>0)
                goto Error1 
                END
          ---===============================================
          ----序列号功能
        IF (@SerialIsUsed='是')
         begin
         -----序列号表身关联更新
         Update Woolinte_BillIndex_List Set DlyOrder=bakdly.DlyOrder
         From bakdly,Woolinte_BillIndex B where B.BillNumberID=Woolinte_BillIndex_List.BillNumberID 
         and bakdly.Memo=B.Num and Woolinte_BillIndex_List.PTypeID_=bakdly.PTypeID
         AND BillPrice=price and BillDistPrice=DiscountPrice and bakdly.Comment=Woolinte_BillIndex_List.Comment 
         and Sign(bakdly.assqty)=Sign(Woolinte_BillIndex_List.PDAQty) and B.BillNumberID=@BillNumberID;
         
         if(@@ERROR<>0)
                goto Error1
          Exec PR_Woolinte_PTypeSerial @BillNumberID;
          if(@@ERROR<>0)
                goto Error1
                
         ---写入序列号表
         Insert Into Sn_Bakdly(Vchcode,Dlyorder,PTypeID,KTypeID,DlyDate,SnNo,SnDemo,SnNoId)
         Select @VchCode,B.Dlyorder,B.PTypeID_,A.KTypeID_,A.billdate,C.Serial,'','0'
         From Woolinte_BillIndex A,Woolinte_BillIndex_List B,TBPTypeSerial C 
         where A.BillNumberID=B.BillNumberID and B.Dlyorder=C.Dlyorder
         Group By B.Dlyorder,B.PTypeID_,A.KTypeID_,A.billdate,C.Serial;
         
         if(@@ERROR<>0)
                goto Error1
               
         end        
         ---===============================================
         Update bakdly Set Memo='' where vchcode=@VchCode;
         if(@@ERROR<>0)
                goto Error1 
                
          ---===============================================
          --销售单收款功能和销售退货单付款功能
          IF @BillType='Sale' Or @BillType='SaleBack' or  @BillType='Buy' or @BillType='MoneyBill'
          BEGIN
          insert into bakdly(vchcode,aTypeID,BTypeID,ETypeID,KTypeID,PTypeID,qty,discount,discountprice,price,total,taxprice,Comment,date, 
          usedtype,tax_total,discounttotal,vchtype,costmode,Redword,Unit,assqty,assprice,assdiscountprice,asstaxprice,Unitrate,RetailPrice)
          Select @VchCode,PayATypeID_,BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_,'','0','0','0','0',PayMoney, 
          '0','',billdate,'2','0','0',@BillTypeID,'0','F','0','0','0','0','0','0','0'
          From Woolinte_BillIndex where Woolinte_BillIndex.billnumberid=@BillNumberID and PayATypeID_<>'' and PayMoney>=0;
         
          if(@@ERROR<>0)
                goto Error1 
          END
            IF  @BillType='SaleBackChange'
          BEGIN
          insert into bakdly(vchcode,aTypeID,BTypeID,ETypeID,KTypeID,PTypeID,qty,discount,discountprice,price,total,taxprice,Comment,date, 
          usedtype,tax_total,discounttotal,vchtype,costmode,Redword,Unit,assqty,assprice,assdiscountprice,asstaxprice,Unitrate,RetailPrice)
          Select @VchCode,PayATypeID_,BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_,'','0','0','0','0',PayMoney, 
          '0','',billdate,'3','0','0',@BillTypeID,'0','F','0','0','0','0','0','0','0'
          From Woolinte_BillIndex where Woolinte_BillIndex.billnumberid=@BillNumberID and PayATypeID_<>'' and PayMoney<>0;
         
          if(@@ERROR<>0)
                goto Error1 
          END
          --优惠金额 销售单
          IF @BillType='Sale'  OR @BillType='SaleBack' OR @BillType='CollectionBill'   or @BillType='PayBill'
           BEGIN
               IF @PreferenceMoney!='0'
               BEGIN
               insert into bakdly(vchcode,aTypeID,BTypeID,ETypeID,KTypeID,PTypeID,qty,discount,discountprice,price,total,taxprice,Comment,date, 
          usedtype,tax_total,discounttotal,vchtype,costmode,Redword,Unit,assqty,assprice,assdiscountprice,asstaxprice,Unitrate,RetailPrice)
          Select @VchCode,'000040000390000',BTypeID_,ETypeID_,'','','0','0','0','0',PreferenceMoney, 
          '0','',billdate,'2','0','0',@BillTypeID,'0','F','0','0','0','0','0','0','0'
          From Woolinte_BillIndex where Woolinte_BillIndex.billnumberid=@BillNumberID;
         
                if(@@ERROR<>0)
                goto Error1 
               END
           END
               
 IF  @BillType='SaleBackChange'
           BEGIN
               IF @PreferenceMoney!='0'
               BEGIN
               insert into bakdly(vchcode,aTypeID,BTypeID,ETypeID,KTypeID,PTypeID,qty,discount,discountprice,price,total,taxprice,Comment,date, 
          usedtype,tax_total,discounttotal,vchtype,costmode,Redword,Unit,assqty,assprice,assdiscountprice,asstaxprice,Unitrate,RetailPrice)
          Select @VchCode,'000040000390000',BTypeID_,ETypeID_,'','','0','0','0','0',PreferenceMoney, 
          '0','',billdate,'3','0','0',@BillTypeID,'0','F','0','0','0','0','0','0','0'
          From Woolinte_BillIndex where Woolinte_BillIndex.billnumberid=@BillNumberID;
         
                if(@@ERROR<>0)
                goto Error1 
               END
           END
          
             ----写入收款记录  
          if @BillType='SaleOrder' Or @BillType='BuyOrder'
           begin
              insert into BakDlyOrder(vchcode,aTypeID,BTypeID,ETypeID,KTypeID,PTypeID,qty,discount,discountprice,price,total,taxprice,Comment,date, 
              usedtype,tax_total,discounttotal,vchtype,costmode,Redword,Unit,assqty,assprice,assdiscountprice,asstaxprice,Unitrate,RetailPrice,PStutas,jhdate) 
              Select @VchCode,'0000100001',BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_,PTypeID_,BillQty,Discount,BillDistPrice,BillPrice,BillQty*BillPrice, 
              BillDistPrice,Woolinte_BillIndex_List.Comment,billdate,'1',BillDistPrice*BillQty,BillDistPrice*BillQty,@BillTypeID,'0','F',uintNum-1, 
              PDAQty,Unitprice, UnitPrice*Discount, UnitPrice*Discount,uintrate ,Woolinte_BillIndex_List.RetailPrice, Woolinte_BillIndex_List.DetailType,billdate
              From Woolinte_BillIndex,Woolinte_BillIndex_List where Woolinte_BillIndex_List.billnumberid=Woolinte_BillIndex.billnumberid  
             and Woolinte_BillIndex.billnumberid=@BillNumberID
 --销售单收款功能和销售退货单付款功能
         
          insert into BakDlyOrder(vchcode,aTypeID,BTypeID,ETypeID,KTypeID,PTypeID,qty,discount,discountprice,price,total,taxprice,Comment,date, 
          usedtype,tax_total,discounttotal,vchtype,costmode,Redword,Unit,assqty,assprice,assdiscountprice,asstaxprice,Unitrate,RetailPrice)
          Select @VchCode,PayATypeID_,BTypeID_,ETypeID_,Woolinte_BillIndex.KTypeID_,'','0','0','0','0',PayMoney, 
          '0','',billdate,'2','0','0',@BillTypeID,'0','F','0','0','0','0','0','0','0'
          From Woolinte_BillIndex where Woolinte_BillIndex.billnumberid=@BillNumberID and PayATypeID_<>'' and PayMoney>=0;
          
           if(@@ERROR<>0)
             goto Error1 
           end
           
           Insert Into Xw_Message(SendID,ReciveID,SendDate,MSG,MSGAddTion,MSGType,New,Deleted)
         Select @UserTypeID,A.ETypeID,Getdate(),'【'+ @UserProductName + '】-提醒您:'+ Char(13) + Char(13) 
         +'【'+ @UserFullName +'】开了一张单据编号为【'+ @number +'】 的'+@BillName +'!',
         '','0','1','0'  From LoginUser A;
         if(@@ERROR<>0)
         goto Error1

            Update Woolinte_BillIndex set billcode=@number,state='3',VchCode=@VchCode where billnumberid=@BillNumberID and deleted='0' and state='1';      
            Select '1' as ReturnValue;
        end
      commit tran
      return ;
  Error1:
        rollback tran
