unit U_SMHXList;

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, cxCalendar, cxButtonEdit, cxTextEdit, cxPC;

type
  TfrmSMHXList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    cxTabControl1: TcxTabControl;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Panel1: TPanel;
    Label3: TLabel;
    Label2: TLabel;
    Label5: TLabel;
    Label10: TLabel;
    Label15: TLabel;
    Label22: TLabel;
    Label1: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    MPRTCodeName: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    orderNo: TEdit;
    CkOrdNo: TEdit;
    conNo: TEdit;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    KHName: TEdit;
    Edit1: TEdit;
    ywy: TEdit;
    v1Column11: 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 TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure SPIDKeyPress(Sender: TObject; var Key: Char);
    procedure SPSpecChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure CkOrdNoChange(Sender: TObject);
  private
    canshu1, canshu2: string;
    procedure InitGrid();
    function YFData(): Boolean;
    { Private declarations }
  public
    FCKName: string;
    { Public declarations }
  end;

var
  frmSMHXList: TfrmSMHXList;

implementation

uses
  U_DataLink, U_Fun;

{$R *.dfm}
function TfrmSMHXList.YFData(): Boolean;
var
  Maxno: string;
begin
  Result := False;
  if GetLSNo(ADOQueryCmd, Maxno, 'DC', 'CK_SXPB_CR', 3, 1) = False then
  begin
    Application.MessageBox('取坯布应付最大号失败!', '提示', 0);
    Exit;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_SXPB_CR where 1=2');
    Open;
  end;
  with ADOQueryCmd do
  begin
    Append;
    FieldByName('SPID').Value := Trim(Maxno);
    FieldByName('HXCKOrdNo').Value := Trim(CDS_Main.fieldbyname('CKOrdNo').AsString);
    FieldByName('CKName').Value := '待检布';
    FieldByName('CRType').Value := '扫描回修出库';
    FieldByName('CRTime').Value := CDS_Main.fieldbyname('CKDate').Value;
    FieldByName('ToFactoryName').Value := Trim(CDS_Main.fieldbyname('KHNmae').AsString);
    FieldByName('conNo').Value := Trim(CDS_Main.fieldbyname('conNo').AsString);
    FieldByName('OrderNo').Value := Trim(CDS_Main.fieldbyname('OrderNo').AsString);
    FieldByName('ORDMainIdRK').Value := Trim(CDS_Main.fieldbyname('MainId').AsString);
    FieldByName('SPCode').Value := Trim(CDS_Main.fieldbyname('MPRTCode').AsString);
    FieldByName('SPMF').Value := Trim(CDS_Main.fieldbyname('MPRTMF').AsString);
    FieldByName('SPName').Value := Trim(CDS_Main.fieldbyname('MPRTCodeName').AsString);
    FieldByName('SPKZ').Value := Trim(CDS_Main.fieldbyname('MPRTKZ').AsString);
    FieldByName('CRFlag').Value := '出库';
    FieldByName('CRQtyFlag').Value := -1;
    FieldByName('PiQty').Value := CDS_Main.fieldbyname('ps').Value;

    if CDS_Main.fieldbyname('Qty').ASFLOAT > 0 then
    begin
      FieldByName('Qty').Value := CDS_Main.fieldbyname('Qty').Value;
      FieldByName('QtyUnit').Value := 'M';
    end;
    if CDS_Main.fieldbyname('NETKGQty').ASFLOAT > 0 then
    begin
      FieldByName('Qty').Value := CDS_Main.fieldbyname('NETKGQty').Value;
      FieldByName('QtyUnit').Value := 'Kg';
    end;
    FieldByName('Filler').Value := Trim(DName);
    Post;
  end;

  Result := True;
end;

procedure TfrmSMHXList.FormDestroy(Sender: TObject);
begin
  frmSMHXList := nil;
end;

procedure TfrmSMHXList.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caHide;
end;

procedure TfrmSMHXList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime;
  canshu1 := Trim(DParameters1);
  canshu2 := Trim(DParameters2);
end;

procedure TfrmSMHXList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select AA.*    ');
      sql.Add(' ,B.orderNo,B.MPRTCode,B.MPRTCodeName,B.MPRTMF,B.MPRTKZ,B.conNo,B.KHconNo,B.lidan,B.ywy ');
      sql.Add(' from (  ');
      sql.Add(' select count(A.MJID) PS,SUM(A.NETKGQty) NETKGQty,SUM(A.KGQty) KGQty, SUM(A.Qty) Qty,A.CKOrdNo,A.MainId,A.QtyUnit  ');
      sql.Add(' ,D.KHNmae,D.CKDate,D.CKType');
      sql.Add(' from CK_BanCP_CR A  ');
      sql.Add(' inner join CK_BanCP_CKList D on A.CKOrdNo=D.CKOrdNo  ');
      sql.Add(' where  A.CRFlag=''出库''  and  D.CKType=''回修出库''  ');
      sql.Add(' and   D.CKDate>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)));
      sql.Add(' and  D.CKDate<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)));
      sql.Add(' GROUP BY A.CKOrdNo,A.MainId,A.QtyUnit ,D.KHNmae,D.CKDate,D.CKType  ) AA  ');
      sql.Add('   inner join JYOrder_Main B on AA.MainId=B.MainId ');
      Open;
      if cxTabControl1.TabIndex = 0 then
        SQL.Add(' and not exists(select SPID from CK_SXPB_CR X where X.HXCKOrdNo=AA.CKOrdNo ) ')
      else
        SQL.Add(' and  exists(select SPID from CK_SXPB_CR X where X.HXCKOrdNo=AA.CKOrdNo )');
//      ShowMessage(SQL.Text);
      Open;

    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

procedure TfrmSMHXList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv1, '坯布仓库');
  Close;
end;

procedure TfrmSMHXList.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption, Tv1, '坯布仓库');
 { if Trim(canshu2)='查看' then
  begin
    TBAdd.Visible:=False;
    TBDel.Visible:=False;
    TBEdit.Visible:=False;
  end else
  begin
    TBAdd.Visible:=True;
    TBDel.Visible:=True;
    TBEdit.Visible:=True;
  end;}
  //InitGrid();
end;

procedure TfrmSMHXList.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 TfrmSMHXList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

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

procedure TfrmSMHXList.SPIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    {if Length(Trim(SPID.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add(' select A.* ');
        sql.Add(',ConNo=(select ConNo from Contract_Main CM where CM.MainId=A.CGMainId)');
        //sql.Add(' ,ConNo=(select ConNo from Contract_Main CM where CM.MainId=A.CGMainId )');
        sql.Add(' from CK_SXPB_CR A');
        sql.add(' where SPID like :SXID');
        Parameters.ParamByName('SXID').Value:='%'+Trim(SXID.Text)+'%';
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;  }
  end;
end;

procedure TfrmSMHXList.SPSpecChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmSMHXList.FactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmSMHXList.cxTabControl1Change(Sender: TObject);
begin
  INITGrid();
end;

procedure TfrmSMHXList.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbright then

end;

procedure TfrmSMHXList.ToolButton1Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex <> 0 then
    Exit;
  while CDS_Main.Locate('SSel', True, []) do
  begin
    if YFData() = false then
    begin

      Exit;
    end;
    CDS_Main.Delete;
  end;

end;

procedure TfrmSMHXList.CkOrdNoChange(Sender: TObject);
begin
  TBFind.Click;
end;

end.

