unit U_CKProductBCP_CKKCHZ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridBandedTableView, cxGridDBBandedTableView,
  cxGridLevel, cxClasses, cxControls, cxGridCustomView, cxGridDBTableView,
  cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxDropDownEdit, RM_Common, RM_Class,
  RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, cxCheckBox, Menus,
  MovePanel, cxSplitter, BtnEdit, cxTextEdit, cxPC, cxCalendar,
  cxButtonEdit;

type
  TfrmCKProductBCP_CKKCHZ = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    CDS_Print: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    MovePanel2: TMovePanel;
    CDS_CX: TClientDataSet;
    CDS_DH: TClientDataSet;
    ADOQueryDH: TADOQuery;
    ADOQueryPrint: TADOQuery;
    ClientDataSet0: TClientDataSet;
    DataSource0: TDataSource;
    cxTabControl1: TcxTabControl;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v2MXKCQty: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Panel1: TPanel;
    Label3: TLabel;
    Label13: TLabel;
    SPName: TEdit;
    SPCF: TEdit;
    v1Qty: TcxGridDBColumn;
    v1CKQty: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label2: TLabel;
    Label4: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    v1CKPS: TcxGridDBColumn;
    v1PIQty: TcxGridDBColumn;
    v1MXKCPiQty: TcxGridDBColumn;
    v1ZJCKDate: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    Label1: TLabel;
    Label5: TLabel;
    SPMF: TEdit;
    SPKZ: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBCKCXClick(Sender: TObject);
    procedure C_CodeNameChange(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
  private
    procedure InitGrid();
    procedure CPInitgrid();
    procedure PBInitgrid();
    function  YSData(Order_Main10:TClientDataSet):Boolean;
    { Private declarations }
  public
    FYear:string;
    { Public declarations }
  end;

var
  frmCKProductBCP_CKKCHZ: TfrmCKProductBCP_CKKCHZ;

implementation
uses
  U_DataLink,U_Fun, U_ZdyAttachGYS;

{$R *.dfm}
procedure TfrmCKProductBCP_CKKCHZ.PBInitgrid();
begin
  try
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.SPID,A.CRTime,A.CRType,A.SPName,A.SPSpec,A.SPCF,A.SPMF,A.SPKZ,A.FromFactoryName,A.ToFactoryName ');
      sql.add(',A.RCGangNo,A.MXKCPiQty,A.MXKCQty,A.QtyUnit,Cast('''' as varchar(30)) MJType,A.Note');
      sql.Add(',ConNoHZ=[dbo].[F_Get_Order_SubStr](A.ORDMainIdRK,''ConNoHZ'') ');
      sql.add(',PBOrderNO=(select ConNo from Cloth_Main JM where JM.MainId=A.SCMainIdRK)');
      sql.Add(' ,OrderNo=(select OrderNo from JYOrder_Main JM where JM.MainId=A.OrdMainIdRK )');
      sql.Add(' ,PRTColorNo=(select PRTColorNo from JYOrder_Sub JM where JM.SubId=A.OrdSubIdRK )');
      sql.Add(' ,PRTColor=(select PRTColor from JYOrder_Sub JM where JM.SubId=A.OrdSubIdRK )');
      sql.Add(' ,PRTHX=(select PRTHX from JYOrder_Sub JM where JM.SubId=A.OrdSubIdRK )');
      sql.Add(' from CK_SXPB_CR A where A.CRFlag=''入库'' and MXKCQty>0');
      sql.add(' and isnull(A.CKName,'''')=''坯布'' ');
      sql.add(' union all ');
      sql.Add(' select C.Mainid SPID,C.CRTime,C.CRType,SPName=(select Top 1 B.C_CodeName from Cloth_Sub B where B.Mainid=C.Mainid)');
      sql.add(',cast('''' as varchar(50)) SPSpec,cast('''' as varchar(50)) SPCF ');
      sql.add(' ,SPMF=(select Top 1 cast(B.MFQty as varchar(50)) from Cloth_Sub B where B.Mainid=C.Mainid)');
      sql.add(' ,SPKZ=(select Top 1 cast(B.KZQty as varchar(50)) from Cloth_Sub B where B.Mainid=C.Mainid)');
      sql.add(',cast(''飞美仓库'' as varchar(50)) FromFactoryName,cast(''飞美仓库'' as varchar(50)) ToFactoryName');
      sql.add(',cast('''' as varchar(50)) RCGangNo,MXKCPiQty=Count(*),MXKCQty=Sum(KGQty),cast(''KG'' as varchar(5)) QtyUnit,C.CPType MJType,C.Note');
      sql.Add(',ConNoHZ=[dbo].[F_Get_Order_SubStr]((select Top 1 J.Mainid from JYOrder_Main J where J.OrderNo=C.ToOrderNo),''ConNoHZ'') ');
      sql.add(',PBOrderNO=(select ConNo from Cloth_Main JM where JM.MainId=C.Mainid)');
      sql.Add(' ,OrderNo=(select OrderNo from JYOrder_Main JM where JM.OrderNO=C.ToOrderNo)');
      sql.Add(' ,PRTColorNo=(cast('''' as varchar(50)))');
      sql.Add(' ,PRTColor=(cast('''' as varchar(50)))');
      sql.Add(' ,PRTHX=(cast('''' as varchar(50)))');
      sql.Add(' from CK_PBCP_KC KC inner join CK_PBCP_CR C on KC.CRID=C.CRID and C.CRType=''检验入库''');
      sql.Add(' where KC.KCQty>0 or KC.KCKGQty>0 ');
      sql.add(' group by C.Mainid,C.CRTime,C.CRType,C.CPType,C.ToOrderNo,C.Note');
      open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally;
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCKProductBCP_CKKCHZ.FormDestroy(Sender: TObject);
begin
  frmCKProductBCP_CKKCHZ:=nil;
end;

procedure TfrmCKProductBCP_CKKCHZ.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmCKProductBCP_CKKCHZ.CPinitgrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.add('select DD.PrtCodeName SPName,DD.SOrdDefStr4,DD.PRTspec SPSpec,DD.PRTColor,DD.PRTColorNo,DD.PRTHX,B.factoryName,B.fromFactoryName,EE.ConNo ConNoHZ,DD.StyleNo ');
      sql.Add(',DD.PRTMF SPMF,DD.PRTKZ SPKZ,OrderNo=(select C.OrderNo from JYOrder_Main C where C.Mainid=A.Mainid)');
      sql.add(',A.Mjstr4 RCGangNo,A.MJType,case when isnull(Sum(MJLen),0)=0 then ''KG'' else A.MJTypeOther end as QtyUnit,case when isnull(Sum(MJLen),0)=0 then sum(MJMaoZ) else Sum(MJLen) end as MXKCQty');
      sql.add(',MXKCPiQty=Count(A.MJID),sum(A.MJQty4) MJQty4,sum(A.MJMaoZ) MJMaoZ ');
      //疵点sql.Add(',CDQty=(select SUM(CDqty) from WFB_MJJY_CD X inner join WFB_MJJY C on X.MJID=C.MJID where C.APID=A.APID and C.MJType=A.MJType and C.Filler=A.Filler and C.MJStr2=A.MJStr2) ');
      //疵点sql.add(',SumQty=(select SUM(CDBeg) from WFB_MJJY_CD X inner join WFB_MJJY C on X.MJID=C.MJID where C.APID=A.APID and C.MJType=A.MJType and C.Filler=A.Filler and C.MJStr2=A.MJStr2) ');
      sql.add('from WFB_MJJY A ');
      sql.add('inner join CK_SXPB_CR B on B.SPID=A.APID ');
      sql.add('inner join JYOrderCon_Sub DD on DD.Subid=A.ConID ');
      sql.add('inner join JYOrderCon_Main EE on EE.Mainid=DD.Mainid where 1=1 ');
      sql.add('and A.MJStr2=''未入库'' ');
      sql.add('group by DD.PrtCodeName,DD.SOrdDefStr4,DD.PRTspec,DD.PRTColor,DD.PRTColorNo,DD.PRTHX,B.factoryName,B.fromFactoryName,EE.ConNo,DD.StyleNo,DD.PRTMF,DD.PRTKZ');
      sql.add(',A.Mainid,A.Mjstr4,A.MJType,A.MJTypeOther ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCKProductBCP_CKKCHZ.InitGrid();
var FMXKCPiqty,FMXKCQty:Double;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      close;
      Sql.Clear;
      SQL.Add('select Sum(MXKCPiQty) MXKCPiQty,Sum(MXKCQty) MXKCQty ');
      SQL.Add('from CK_SXPB_CR A where A.CRFlag=''入库'' ');
      sql.Add(' and A.CRTime<'''+trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
      if SPName.Text<>'' then
      sql.Add(' and A.SPName like '''+'%'+trim(SPName.Text)+'%'+'''');
      if SPCF.Text<>'' then
      sql.Add(' and A.SPCF like '''+'%'+trim(SPCF.Text)+'%'+'''');
      if SPMF.Text<>'' then
      sql.Add(' and A.SPMF like '''+'%'+trim(SPMF.Text)+'%'+'''');
      if SPKZ.Text<>'' then
      sql.Add(' and A.SPKZ like '''+'%'+trim(SPKZ.Text)+'%'+'''');
      if cxTabControl1.TabIndex=0 then
      sql.add(' and isnull(A.CKName,'''')=''坯布'' ');
      if cxTabControl1.TabIndex=1 then
      sql.add(' and isnull(A.CKName,'''')=''外加工'' ');
      if cxTabControl1.TabIndex=2 then
      sql.add(' and isnull(A.CKName,'''')=''待检布'' ');
      if cxTabControl1.TabIndex=3 then
      sql.add(' and isnull(A.CKName,'''')=''成品'' ');
      open;
    end;
    FMXKCPiqty:=ADOQueryMain.fieldbyname('MXKCPiQty').AsFloat;
    FMXKCQty:=ADOQueryMain.fieldbyname('MXKCQty').AsFloat;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      sql.Add(',CKQty=(select Sum(Qty) from CK_SXPB_CR B where B.CKName=A.CKName and CRFlag=''出库'' and B.FZSPID=A.SPID)');
      sql.Add(',CKPS=(select Sum(PiQty) from CK_SXPB_CR B where B.CKName=A.CKName and CRFlag=''出库'' and B.FZSPID=A.SPID)');
      sql.Add(',ZJCKDate=(select Top 1 B.CRTime from CK_SXPB_CR B where B.CKName=A.CKName and CRFlag=''出库'' and B.FZSPID=A.SPID order by B.CRTime desc)');
      sql.Add(' from CK_SXPB_CR A where A.CRFlag=''入库'' ');
      sql.Add(' and A.CRTime>='''+trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
      sql.Add(' and A.CRTime<'''+trim(FormatDateTime('yyyy-MM-dd',endDate.Date+1))+'''');
      if SPName.Text<>'' then
      sql.Add(' and A.SPName like '''+'%'+trim(SPName.Text)+'%'+'''');
      if SPMF.Text<>'' then
      sql.Add(' and A.SPMF like '''+'%'+trim(SPMF.Text)+'%'+'''');
      if SPKZ.Text<>'' then
      sql.Add(' and A.SPKZ like '''+'%'+trim(SPKZ.Text)+'%'+'''');
      if SPCF.Text<>'' then
      sql.Add(' and A.SPCF like '''+'%'+trim(SPCF.Text)+'%'+'''');
      if cxTabControl1.TabIndex=0 then
      sql.add(' and isnull(A.CKName,'''')=''坯布'' ');
      if cxTabControl1.TabIndex=1 then
      sql.add(' and isnull(A.CKName,'''')=''外加工'' ');
      if cxTabControl1.TabIndex=2 then
      sql.add(' and isnull(A.CKName,'''')=''待检布'' ');
      if cxTabControl1.TabIndex=3 then
      sql.add(' and isnull(A.CKName,'''')=''成品'' ');
      sql.Add(' and (isnull(A.MXKCPiQty,0)<>0 or isnull(A.MXKCQty,0)<>0)');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    with CDS_Main do
    begin
      Append;
      FieldByName('CRTime').Value:=BegDate.Date;
      FieldByName('SPName').Value:='上期库存';
      FieldByName('SPSpec').Value:='上期库存';
      FieldByName('MXKCPiQty').Value:=FMXKCPiqty;
      FieldByName('MXKCQty').Value:=FMXKCQty;
      post;
    end;
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally;
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCKProductBCP_CKKCHZ.TBRafreshClick(Sender: TObject);
begin
  Toolbar1.SetFocus;
  initgrid();
end;

procedure TfrmCKProductBCP_CKKCHZ.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  end;
end;

procedure TfrmCKProductBCP_CKKCHZ.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('仓库出库列表FY12',Tv1,'成品仓库');
  Close;
end;

procedure TfrmCKProductBCP_CKKCHZ.FormShow(Sender: TObject);
begin
  ReadCxGrid('仓库出库列表FY12',Tv1,'成品仓库');
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime-30;
  InitGrid();
end;

procedure TfrmCKProductBCP_CKKCHZ.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  TcxGridToExcel('坯布出库列表',cxGrid2);
  //SelExportData(Tv0,ADOQueryMain,'');
end;

procedure TfrmCKProductBCP_CKKCHZ.TBFindClick(Sender: TObject);
begin
  initgrid();
end;

procedure TfrmCKProductBCP_CKKCHZ.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCP_CKKCHZ.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,True);
end;

procedure TfrmCKProductBCP_CKKCHZ.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,False);
end;

procedure TfrmCKProductBCP_CKKCHZ.TBCKCXClick(Sender: TObject);
var
  FFMainId,FPrice,FHZ:String;
begin
  if CDS_Main.IsEmpty then Exit;
  
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要撤销数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryDH do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select SubId=Cast('''' as varchar(20)) ,SHCompany=Cast('''' as varchar(40)),HZ=Cast('''' as varchar(100)) ');
    Open;
  end;
  SCreateCDS20(ADOQueryDH,CDS_DH);
  SInitCDSData20(ADOQueryDH,CDS_DH);
  if CDS_DH.IsEmpty=False then
  begin
    CDS_DH.Delete;
  end;
  MovePanel2.Visible:=True;
  MovePanel2.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      //First;
      while CDS_Main.Locate('SSel',True,[])=True do
      begin
        if Trim(FHZ)='' then
        begin
          with CDS_DH do
          begin
            Append;
            FieldByName('SubId').Value:=Trim(CDS_Main.fieldbyname('MainId').AsString);
            FieldByName('SHCompany').Value:=Trim(CDS_Main.fieldbyname('SHCompany').AsString);
            FieldByName('HZ').Value:=Trim(CDS_Main.fieldbyname('SubId').AsString)+Trim(CDS_Main.fieldbyname('SHCompany').AsString);
            Post;
          end;
          FHZ:=Trim(CDS_Main.fieldbyname('MainId').AsString)+Trim(CDS_Main.fieldbyname('SHCompany').AsString);
        end else
        begin
          if Trim(FHZ)<>Trim(CDS_Main.fieldbyname('MainId').AsString)+Trim(CDS_Main.fieldbyname('SHCompany').AsString) then
          begin
            with CDS_DH do
            begin
              Append;
              FieldByName('SubId').Value:=Trim(CDS_Main.fieldbyname('MainId').AsString);
              FieldByName('SHCompany').Value:=Trim(CDS_Main.fieldbyname('SHCompany').AsString);
              FieldByName('HZ').Value:=Trim(CDS_Main.fieldbyname('MainId').AsString)+Trim(CDS_Main.fieldbyname('SHCompany').AsString);
              Post;
            end;
            FHZ:=Trim(CDS_Main.fieldbyname('MainId').AsString)+Trim(CDS_Main.fieldbyname('SHCompany').AsString);
          end;
        end;
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('insert into CK_PBCP_CR_Log select * from CK_PBCP_CR where BCID='''+Trim(CDS_Main.fieldbyname('BCID').AsString)+'''');
            sql.Add(' update CK_PBCP_CR_Log Set Note='''+Trim(DName)+'''+convert(varchar(20),GETDATE(),120)');
            sql.Add(' where BCID='''+Trim(CDS_Main.fieldbyname('BCID').AsString)+'''');
            sql.Add('delete CK_PBCP_CR where BCID='''+Trim(CDS_Main.fieldbyname('BCID').AsString)+'''');
            sql.Add('UPdate CK_PBCP_KC Set KCKgQty=(select KgQty from CK_PBCP_CR A where  A.CRID=CK_PBCP_KC.CRID and A.CRType=''检验入库'') ');
            sql.Add(',KCQty=(select Qty from CK_PBCP_CR A where  A.CRID=CK_PBCP_KC.CRID and A.CRType=''检验入库'') ');
            SQL.Add(' where CRID='+CDS_Main.fieldbyname('CRID').AsString);
            ExecSQL;
          end;

          CDS_Main.Delete;
        end;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    MovePanel2.Visible:=False;
    Exit;
  except
    MovePanel2.Visible:=False;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销异常！','提示',0);
  end;
end;

function TfrmCKProductBCP_CKKCHZ.YSData(Order_Main10:TClientDataSet):Boolean;
var
  CRID,YFID,Price,PriceUnit,OrderUnit:String;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select Top 1 * from JYOrder_Sub Where Mainid='''+Trim(Order_Main10.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  PriceUnit:=Trim(ADOQueryTemp.fieldbyname('PriceUnit').AsString);
  OrderUnit:=Trim(ADOQueryTemp.fieldbyname('OrderUnit').AsString);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(Order_Main10.fieldbyname('KHName').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    CRID:=ADOQueryTemp.fieldbyname('CRID').AsString;
  end else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CRID set CRID=CRID+1');
      sql.Add('select * from YF_Money_CRID ');
      Open;
    end;
    CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('CRID').Value:=StrToInt(CRID);
      FieldByName('FactoryName').Value:=Trim(Order_Main10.fieldbyname('KHName').AsString);
      FieldByName('ZdyStr1').Value:='应收收';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where FactoryName='''+Trim(Order_Main10.fieldbyname('KHName').AsString)+'''');
    sql.Add(' and YFTypeId='''+Trim(Order_Main10.fieldbyname('Mainid').AsString)+'''');
    sql.Add(' and Price='''+Trim(Order_Main10.fieldbyname('PRTPrice').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    if GetLSNo(ADOQueryCmd,YFID,'CS','YF_Money_CR',3,1)=False then
    begin
      Application.MessageBox('取坯布应付最大号失败!','提示',0);
      Exit;
    end;
  end else
  begin
    YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update YF_Money_CR Set Qty=(select isnull(Sum(Qty),0) from CK_BanCP_CR A  ');
    sql.Add(' inner join JYOrder_Sub B on A.SubId=B.SubId where');
    SQL.Add(' A.MainId=YF_Money_CR.YFTypeId and A.CRType=''正常出库'' and CPType=''正品''  ');
    SQL.Add(' and B.PRTPrice=YF_Money_CR.Price)');
    sql.Add(',PS=(select isnull(count(*),0) from CK_BanCP_CR A  ');
    sql.Add(' inner join JYOrder_Sub B on A.SubId=B.SubId where');
    SQL.Add(' A.MainId=YF_Money_CR.YFTypeId and A.CRType=''正常出库'' and CPType=''正品''  ');
    SQL.Add(' and B.PRTPrice=YF_Money_CR.Price )');
    sql.Add(' where YFTypeId='''+Trim(Order_Main10.fieldbyname('Mainid').AsString)+'''');
    sql.Add(' and Price='+Order_Main10.fieldbyname('PRTPrice').AsString);
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty*HuiLv');
    sql.Add(' where YFID='''+Trim(YFID)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID='+CRID);
    ExecSQL;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where YFId='''+Trim(YFID)+'''');
    Open;
  end;
  if ADOQueryTemp.FieldByName('Qty').Value=0 then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from YF_Money_CR where YFID='''+Trim(YFID)+'''');
      ExecSQL;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where CRId='+CRID);
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from YF_Money_KC where CRId='+CRID);
      ExecSQL;
    end;
  end;
  Result:=True;
end;

procedure TfrmCKProductBCP_CKKCHZ.C_CodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCP_CKKCHZ.RadioGroup1Click(Sender: TObject);
begin
  initgrid();
end;

procedure TfrmCKProductBCP_CKKCHZ.cxTabControl1Change(
  Sender: TObject);
begin
  Toolbar1.SetFocus;
  InitGrid();
end;

end.
