unit U_KkAndDjList;

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, BtnEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmKkAndDjList = 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;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ADOQueryPrint: TADOQuery;
    ToolButton1: TToolButton;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column21: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column26: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    v1Column29: TcxGridDBColumn;
    v1Column30: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Panel1: TPanel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label12: TLabel;
    Label11: TLabel;
    Label16: TLabel;
    Label1: TLabel;
    Label10: TLabel;
    Label13: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    SPName: TEdit;
    FactoryName: TEdit;
    SPID: TEdit;
    SPSpec: TEdit;
    SPCF: TEdit;
    ORDERNOM: TEdit;
    CRType: TComboBox;
    RCGangNo: TEdit;
    PBFactoryName: TEdit;
    CheckBox1: TCheckBox;
    chkdate: TCheckBox;
    lidan: TEdit;
    YWY: TEdit;
    DJType: TEdit;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    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 N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure SPIDKeyPress(Sender: TObject; var Key: Char);
    procedure SPIDChange(Sender: TObject);
    procedure SPSpecChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure CheckBox1Click(Sender: TObject);
    procedure ORDERNOMKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton1Click(Sender: TObject);
    procedure Panel2Click(Sender: TObject);
    procedure Panel3Click(Sender: TObject);
    procedure Panel4Click(Sender: TObject);
  private
    procedure SetStatus();
    procedure InitGrid();
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

var
  frmKkAndDjList: TfrmKkAndDjList;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmKkAndDjList.SetStatus();
begin

end;

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

procedure TfrmKkAndDjList.FormCreate(Sender: TObject);
begin
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime;
end;

procedure TfrmKkAndDjList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.ORDERNO AS ORDERNOM   ');
      sql.Add(',lidan=(select lidan from JYOrder_Main X where X.orderNo=A.QCorderNo) ');
      sql.Add(',YWY=(select YWY from JYOrder_Main X where X.orderNo=A.QCorderNo) ');
      sql.Add(',DJType=(select DJType from JYOrder_Main X where X.orderNo=A.QCorderNo) ');
      

      sql.Add(' from CK_SXPB_CR A inner join  JYOrder_Main B ON A.ORDMainIdRK=B.MainId');
      sql.Add(' where isnull(CKName,'''')=''待检布'' and CRFlag=''出库'' ');
      if CheckBox1.Checked then
      begin
        sql.Add(' and isnull(DJJS,0)=0 ');

      end;
      if chkdate.Checked then
      begin
        sql.Add(' and A.DJDate>=' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', Begdate.DateTime))));
        sql.Add(' and A.DJDate<' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1))));
      end
      else
      begin
        sql.Add(' and A.CRTime>=' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', Begdate.DateTime))));
        sql.Add(' and A.CRTime<' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1))));
      end;

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

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

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

procedure TfrmKkAndDjList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '开卡管理');
  Close;
end;

procedure TfrmKkAndDjList.FormShow(Sender: TObject);
begin
  SetStatus();

//  ReadCxGrid(trim(self.Caption), Tv1, '开卡管理');
  InitGrid();
end;

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

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

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

procedure TfrmKkAndDjList.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 TfrmKkAndDjList.SPIDChange(Sender: TObject);
begin
  if Length(Trim(SPID.Text)) < 4 then
  begin
    if Trim(SPID.Text) <> '' then
      Exit;
  end;
  TBFind.Click;
end;

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

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

procedure TfrmKkAndDjList.FormDestroy(Sender: TObject);
begin
  frmKkAndDjList := nil;
end;

procedure TfrmKkAndDjList.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  Fsunhao: double;
begin

  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('sunhao').Index] = null then
    Exit;
  Fsunhao := AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('sunhao').Index];
  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('ZPPS').Index] = null then
    Exit;
  if (AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('ZPPS').Index]) > 0 then
  begin
      //损耗±2% 绿色、 损耗±2% ~ ±5% 黄色、 损耗大于±5% 红色
    if Fsunhao < 0 then
      Fsunhao := -Fsunhao;
    if Fsunhao <= 2 then
      ACanvas.Brush.Color := $00FF00;
    if (Fsunhao > 2) and (Fsunhao <= 5) then
      ACanvas.Brush.Color := $00FFFF;
    if Fsunhao > 5 then
      ACanvas.Brush.Color := $4763FF;
  end;
end;

procedure TfrmKkAndDjList.CheckBox1Click(Sender: TObject);
begin
  if chkdate.Checked then
    CheckBox1.Checked := False;
  InitGrid();
end;

procedure TfrmKkAndDjList.ORDERNOMKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Close;
        sql.Clear;
        sql.Add('select A.*,B.ORDERNO AS ORDERNOM   ');
        sql.Add(',lidan=(select lidan from JYOrder_Main X where X.orderNo=A.QCorderNo) ');
        sql.Add(',YWY=(select YWY from JYOrder_Main X where X.orderNo=A.QCorderNo) ');
        sql.Add(',DJType=(select DJType from JYOrder_Main X where X.orderNo=A.QCorderNo) ');
        sql.Add(' from CK_SXPB_CR A inner join  JYOrder_Main B ON A.ORDMainIdRK=B.MainId ');
        sql.Add(' where isnull(CKName,'''')=''待检布'' and CRFlag=''入库'' ');
        sql.Add(' and B.ORDERNO like ' + quotedstr('%' + trim(ORDERNOM.Text) + '%'));

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

procedure TfrmKkAndDjList.ToolButton1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while not eof do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' exec P_UPdate_LCKJY ''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
//          ShowMessage(SQL.Text);
          execsql;
        end;
        CDS_Main.Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_Main.EnableControls;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_Main.EnableControls;
    Application.MessageBox('更新异常!', '提示', 0);
  end;
  InitGrid();
end;

procedure TfrmKkAndDjList.Panel2Click(Sender: TObject);
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*   ');
      sql.Add(',lidan=(select lidan from JYOrder_Main X where X.orderNo=A.QCorderNo) ');
      sql.Add(',YWY=(select YWY from JYOrder_Main X where X.orderNo=A.QCorderNo) ');
      sql.Add(',DJType=(select DJType from JYOrder_Main X where X.orderNo=A.QCorderNo) ');
      sql.Add(' from CK_SXPB_CR A');
      sql.Add(' where isnull(CKName,'''')=''待检布'' and CRFlag=''入库'' ');
      if CheckBox1.Checked then
      begin
        sql.Add(' and isnull(DJJS,0)=0 ');

      end;
      if chkdate.Checked then
      begin
        sql.Add(' and A.DJDate>=' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', Begdate.DateTime))));
        sql.Add(' and A.DJDate<' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1))));
      end
      else
      begin
        sql.Add(' and A.CRTime>=' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', Begdate.DateTime))));
        sql.Add(' and A.CRTime<' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1))));
      end;
      sql.Add(' and ABS(A.sunhao)<=2');

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

procedure TfrmKkAndDjList.Panel3Click(Sender: TObject);
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*   ');
      sql.Add(',lidan=(select lidan from JYOrder_Main X where X.orderNo=A.QCorderNo) ');
      sql.Add(',YWY=(select YWY from JYOrder_Main X where X.orderNo=A.QCorderNo) ');
      sql.Add(',DJType=(select DJType from JYOrder_Main X where X.orderNo=A.QCorderNo) ');
      sql.Add(' from CK_SXPB_CR A');
      sql.Add(' where isnull(CKName,'''')=''待检布'' and CRFlag=''入库'' ');
      if CheckBox1.Checked then
      begin
        sql.Add(' and isnull(DJJS,0)=0 ');

      end;
      if chkdate.Checked then
      begin
        sql.Add(' and A.DJDate>=' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', Begdate.DateTime))));
        sql.Add(' and A.DJDate<' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1))));
      end
      else
      begin
        sql.Add(' and A.CRTime>=' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', Begdate.DateTime))));
        sql.Add(' and A.CRTime<' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1))));
      end;
      sql.Add(' and ABS(A.sunhao)>2 and  ABS(A.sunhao)<=5 ');

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

procedure TfrmKkAndDjList.Panel4Click(Sender: TObject);
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*   ');
      sql.Add(',lidan=(select lidan from JYOrder_Main X where X.orderNo=A.QCorderNo) ');
      sql.Add(',YWY=(select YWY from JYOrder_Main X where X.orderNo=A.QCorderNo) ');
      sql.Add(',DJType=(select DJType from JYOrder_Main X where X.orderNo=A.QCorderNo) ');
      sql.Add(' from CK_SXPB_CR A');
      sql.Add(' where isnull(CKName,'''')=''待检布'' and CRFlag=''入库'' ');
      if CheckBox1.Checked then
      begin
        sql.Add(' and isnull(DJJS,0)=0 ');

      end;
      if chkdate.Checked then
      begin
        sql.Add(' and A.DJDate>=' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', Begdate.DateTime))));
        sql.Add(' and A.DJDate<' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1))));
      end
      else
      begin
        sql.Add(' and A.CRTime>=' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', Begdate.DateTime))));
        sql.Add(' and A.CRTime<' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1))));
      end;
      sql.Add(' and ABS(A.sunhao)>5 ');

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

end.

