unit U_CKProductBCPInList;

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, cxCheckBox, RM_Common,
  RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, Menus;

type
  TfrmCKProductBCPInList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    Label4: TLabel;
    MPRTCodeName: TEdit;
    PRTColor: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Label5: TLabel;
    orderNo: TEdit;
    Label6: TLabel;
    MJID: TEdit;
    Label8: TLabel;
    MPRTKZ: TEdit;
    Label9: TLabel;
    MPRTMF: TEdit;
    Label7: TLabel;
    CPType: TComboBox;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label10: TLabel;
    AOrdDefStr1: TEdit;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    TBRKCX: TToolButton;
    v1Column4: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    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 TBPrintClick(Sender: TObject);
    procedure orderNoChange(Sender: TObject);
    procedure orderNoKeyPress(Sender: TObject; var Key: Char);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBRKCXClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCKProductBCPInList: TfrmCKProductBCPInList;

implementation
uses
  U_DataLink,U_Fun;

{$R *.dfm}

procedure TfrmCKProductBCPInList.FormDestroy(Sender: TObject);
begin
  frmCKProductBCPInList:=nil;
end;

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

procedure TfrmCKProductBCPInList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp)
end;

procedure TfrmCKProductBCPInList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('select A.*,B.OrderNo,B.MPRTCodeName,C.PRTColor,C.PRTHX,B.MPRTMF,B.MPRTKZ,D.MJXH,D.MJQty4');
      sql.Add(',isnull(customerNoName,B.OrderNo) KHName');
      sql.Add(',E.AOrdDefStr1,D.MJQty2');
      sql.add('from CK_BanCP_CR A ');
      Sql.add(' inner join JYOrder_Main B on A.MainId=B.MainId');
      Sql.add(' inner join JYOrder_Sub C on A.SubId=C.SubId');
      Sql.add(' inner join WFB_MJJY D on A.MJId=D.MJId');
      sql.Add(' inner join JYOrder_Sub_AnPai E on A.APID=E.APID');
      sql.add('where A.CRTime>=:begdate and A.CRTime<:enddate');
      SQL.Add(' and CRType=''检验入库'' ');
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime));
      Parameters.ParamByName('enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1));
      Open;
      //ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCKProductBCPInList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

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

procedure TfrmCKProductBCPInList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('半成品仓库入库',Tv1,'半成品仓库');
  Close;
end;

procedure TfrmCKProductBCPInList.FormShow(Sender: TObject);
begin

  ReadCxGrid('半成品仓库入库',Tv1,'半成品仓库');
  if Trim(DParameters2)='管理' then
  begin
    TBRKCX.Visible:=True;
  end else
  begin
    v1Column4.Visible:=False;
  end;
  //InitGrid();
end;

procedure TfrmCKProductBCPInList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  TcxGridToExcel('入库列表',cxGrid2);
end;

procedure TfrmCKProductBCPInList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

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

procedure TfrmCKProductBCPInList.TBPrintClick(Sender: TObject);
var
  fPrintFile:String;
begin
  if CDS_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\入库码单.rmf' ;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select AA.* from (select A.*,');
    sql.Add('GangNo=(select AOrdDefStr1 from JYOrder_Sub_AnPai B where B.APID=A.APID),');
    sql.Add('OrderNo=(select OrderNo from JYOrder_Main B where B.MainID=A.MainID),');
    sql.Add('SOrddefstr4=(select SOrddefstr4 from JYOrder_Sub B where B.SubID=A.SubID),');
    sql.Add('PRTHX=(select PRTHX from JYOrder_Sub B where B.SubID=A.SubID),');
    SQL.Add('TaiTou=(select Top 1 Note from KH_Zdy B where B.ZdyName=DD.OrdDefStr2 and Type=''OrdDefStr2'' ),');
    sql.Add('MJXH=(select MJXH from WFB_MJJY B where B.MJID=A.MJID)');
    sql.Add(' from CK_BanCP_CR A');
    sql.Add(' inner join JYOrder_Main DD on A.MainId=DD.MainId');
    sql.Add(' where A.MainId='''+Trim(CDS_Main.fieldbyname('MainId').AsString)+'''');
    {if Trim(CDS_Main.FieldByName('CKOrdNo').AsString)<>'' then
    begin
      sql.Add(' and A.CKOrdNo='''+Trim(CDS_Main.FieldByName('CKOrdNo').AsString)+'''');
    end; }
    sql.Add(' and CRType=''检验入库''');
    SQL.Add(' and CPType=''正品'' )AA ');
    sql.Add(' order by SOrddefstr4,PRTHX,GangNo,MJXH ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.add('select * from(');
    sql.Add('select GangNo,OrderNo,SOrddefstr4,PRTHX,Sum(Qty) Qty,Count(*) PS ');
    SQL.Add('from (select A.Qty,');
    sql.Add('GangNo=(select AOrdDefStr1 from JYOrder_Sub_AnPai B where B.APID=A.APID),');
    sql.Add('OrderNo=(select OrderNo from JYOrder_Main B where B.MainID=A.MainID),');
    sql.Add('PRTHX=(select PRTHX from JYOrder_Sub B where B.SubID=A.SubID),');
    sql.Add('SOrddefstr4=(select SOrddefstr4 from JYOrder_Sub B where B.SubID=A.SubID)');
    sql.Add(' from CK_BanCP_CR A');
    sql.Add(' inner join JYOrder_Main DD on A.MainId=DD.MainId');
    sql.Add(' where A.MainId='''+Trim(CDS_Main.fieldbyname('MainId').AsString)+'''');
    {if Trim(CDS_Main.FieldByName('CKOrdNo').AsString)<>'' then
    begin
      sql.Add(' and A.CKOrdNo='''+Trim(CDS_Main.FieldByName('CKOrdNo').AsString)+'''');
    end; }
    sql.Add(' and CRType=''检验入库''');
    SQL.Add(' and CPType=''正品''  )AA ');
    sql.Add('Group by GangNo,OrderNo,SOrddefstr4,PRTHX )AAA');
    //sql.add(' order by SOrddefstr4,PRTHX,Cast(gangno as int)');
    sql.add(' order by SOrddefstr4,PRTHX,gangno ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_HZ);
  SInitCDSData20(ADOQueryTemp,CDS_HZ);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\入库码单.rmf'),'提示',0);
  end;
end;

procedure TfrmCKProductBCPInList.orderNoChange(Sender: TObject);
begin
  if Length(Trim(orderNo.Text))<4 then Exit;
  TBFind.Click;
end;

procedure TfrmCKProductBCPInList.orderNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(orderNo.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        SQL.Add('select A.*,B.OrderNo,B.MPRTCodeName,C.PRTColor,C.PRTHX,B.MPRTMF,B.MPRTKZ,D.MJXH,D.MJQty4');
        sql.Add(',isnull(customerNoName,B.OrderNo) KHName');
        sql.Add(',E.AOrdDefStr1');
        sql.add('from CK_BanCP_CR A ');
        Sql.add(' inner join JYOrder_Main B on A.MainId=B.MainId');
        Sql.add(' inner join JYOrder_Sub C on A.SubId=C.SubId');
        Sql.add(' inner join WFB_MJJY D on A.MJId=D.MJId');
        sql.Add(' inner join JYOrder_Sub_AnPai E on A.APID=E.APID');
        sql.add('where B.OrderNo like :OrderNo');
        SQL.Add(' and CRType=''检验入库'' ');
        Parameters.ParamByName('orderNo').Value:='%'+Trim(orderNo.Text)+'%';

        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

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

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

procedure TfrmCKProductBCPInList.TBRKCXClick(Sender: TObject);
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;
  BegDate.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      while Locate('SSel',True,[]) do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_BanCP_CR where CRID='+Trim(CDS_Main.fieldbyname('CRID').AsString));
          Open;
        end;
        if ADOQueryTemp.RecordCount>1 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('已出库不能撤销入库!','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete CK_BanCP_CR where BCID='''+Trim(CDS_Main.fieldbyname('BCID').AsString)+'''');
          sql.Add('delete CK_BanCP_KC where CRID='+Trim(CDS_Main.fieldbyname('CRID').AsString));
          sql.Add('Update WFB_MJJY Set MJStr2=''未入库'' where MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+'''');
          ExecSQL;
        end;
        CDS_Main.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;

  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销失败!','提示',0);
  end;
end;

procedure TfrmCKProductBCPInList.ToolButton1Click(Sender: TObject);
var
  fPrintFile:String;
begin
  if CDS_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\出库码单最全.rmf' ;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select AA.* from (select A.*,MJ.MJMaoZ,MJJingZ=MJ.MJQty4,');
    sql.Add('GangNo=(select AOrdDefStr1 from JYOrder_Sub_AnPai B where B.APID=A.APID),');
    sql.Add('OrderNo=(select OrderNo from JYOrder_Main B where B.MainID=A.MainID),');
    sql.Add('SOrddefstr4=(select SOrddefstr4 from JYOrder_Sub B where B.SubID=A.SubID),');
    sql.Add('PRTHX=(select PRTHX from JYOrder_Sub B where B.SubID=A.SubID),');
    SQL.Add('TaiTou=(select Top 1 Note from KH_Zdy B where B.ZdyName=DD.OrdDefStr2 and Type=''OrdDefStr2'' ),');
    sql.Add('MJXH=(select MJXH from WFB_MJJY B where B.MJID=A.MJID)');
    sql.Add(' from CK_BanCP_CR A');
    sql.Add(' inner join WFB_MJJY MJ on A.MJId=MJ.MJId');
    //sql.Add(' inner join JYOrder_Sub JYS on A.SubId=JYS.SubId');
    sql.Add(' inner join JYOrder_Main DD on A.MainId=DD.MainId');
    sql.Add(' where A.MainId='''+Trim(CDS_Main.fieldbyname('MainId').AsString)+'''');
    sql.Add(' and A.CPType=''正品''');
    sql.Add(' and CRType=''检验入库'')AA ');
    sql.Add(' order by SOrddefstr4,PRTHX,GangNo,MJXH ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from (');
    sql.Add('select GangNo,OrderNo,SOrddefstr4,PRTHX,Sum(Qty) Qty,Count(*) PS,Sum(MJMaoZ) MJMaoZ,sum(MJJingZ) MJJingZ ');
    SQL.Add('from (select A.Qty,MJMaoZ=A.KgQty,MJJingZ=MJ.MJQty4,');
    sql.Add('GangNo=(select AOrdDefStr1 from JYOrder_Sub_AnPai B where B.APID=A.APID),');
    sql.Add('OrderNo=(select OrderNo from JYOrder_Main B where B.MainID=A.MainID),');
    sql.Add('PRTHX=(select PRTHX from JYOrder_Sub B where B.SubID=A.SubID),');
    sql.Add('SOrddefstr4=(select SOrddefstr4 from JYOrder_Sub B where B.SubID=A.SubID)');
    sql.Add(' from CK_BanCP_CR A');
    sql.Add(' inner join JYOrder_Main DD on A.MainId=DD.MainId');
    sql.Add(' inner join WFB_MJJY MJ on A.MJId=MJ.MJId');
    sql.Add(' where A.MainId='''+Trim(CDS_Main.fieldbyname('MainId').AsString)+'''');
    sql.Add(' and A.CPType=''正品''');
    sql.Add(' and CRType=''检验入库'')AA ');
    sql.Add('Group by GangNo,OrderNo,SOrddefstr4,PRTHX');
    sql.Add(')AAA order by SOrddefstr4,PRTHX,GangNo');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_HZ);
  SInitCDSData20(ADOQueryTemp,CDS_HZ);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\出库码单最全.rmf'),'提示',0);
  end;
end;

end.
