unit U_CKProductBCPKCHZList;

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, MovePanel, cxButtonEdit, cxPC, RM_Common, RM_Class, RM_e_Xls,
  RM_Dataset, RM_System, RM_GridReport, cxCheckBox, cxTextEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu;

type
  TfrmCKProductBCPKCHZList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    Label4: TLabel;
    MPRTCodeName: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    cxgrdPRTColor: TcxGridDBColumn;
    VC_KGQty: TcxGridDBColumn;
    VC_Qty: TcxGridDBColumn;
    v1orderNo: TcxGridDBColumn;
    Label6: TLabel;
    MJID: TEdit;
    VC_JQty: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    v1MPRTKZ: TcxGridDBColumn;
    Label8: TLabel;
    MPRTKZ: TEdit;
    Label9: TLabel;
    MPRTMF: TEdit;
    Label7: TLabel;
    CPType: TComboBox;
    v1Column3: TcxGridDBColumn;
    VC_NetKGQty: TcxGridDBColumn;
    Label10: TLabel;
    ConNo: TEdit;
    v1ConNo: TcxGridDBColumn;
    v1lidan: TcxGridDBColumn;
    v1ywy: TcxGridDBColumn;
    Label11: TLabel;
    Label12: TLabel;
    lidan: TEdit;
    ywy: TEdit;
    VC_ganghao: TcxGridDBColumn;
    Label14: TLabel;
    ganghao: TEdit;
    Panel2: TPanel;
    Panel3: TPanel;
    cxTabControl1: TcxTabControl;
    Panel6: TPanel;
    Label16: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    PRTColor: TEdit;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDBPRT: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    VC_PJKuLin: TcxGridDBColumn;
    VC_TJDate: TcxGridDBColumn;
    ToolButton2: TToolButton;
    VC_ISHj: TcxGridDBColumn;
    chkwhj: TCheckBox;
    chkyhj: TCheckBox;
    CheckBox1: TCheckBox;
    VC_JYF: TcxGridDBColumn;
    MovePanel2: TPanel;
    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 v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure PRTColorChange(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1Column6PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure chkwhjClick(Sender: TObject);
    procedure chkyhjClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetStatus();
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

//var
//  frmCKProductBCPKCHZList: TfrmCKProductBCPKCHZList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp,U_RTFun;

{$R *.dfm}
procedure TfrmCKProductBCPKCHZList.SetStatus();
var
  i: Integer;
begin
  case cxTabControl1.TabIndex of
    0:
      begin
        ClearOrHideControls(Panel1, '0', True, True);
        ClearOrHideControls(Panel1, '1', True, False);
        ClearOrHideControls(Panel1, '2', True, False);
        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := false;
        end;
        IsVisibleTV(Panel1, TV1, '0', True);

      end;

    1:
      begin

        ClearOrHideControls(Panel1, '0', False, True);
        ClearOrHideControls(Panel1, '1', True, True);
        ClearOrHideControls(Panel1, '2', True, False);

        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := True;
        end;

        ClearOrHideControls(Panel1, '0', False, True);
        ClearOrHideControls(Panel1, '1', False, True);
        ClearOrHideControls(Panel1, '2', True, True);

      end;

    2:
      begin

        ClearOrHideControls(Panel1, '0', False, True);
        ClearOrHideControls(Panel1, '1', False, True);
        ClearOrHideControls(Panel1, '2', False, True);

        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := True;
        end;
      end;
    3:
      begin
        ClearOrHideControls(Panel1, '0', True, True);
        ClearOrHideControls(Panel1, '1', True, False);
        ClearOrHideControls(Panel1, '2', True, False);
        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := false;
        end;
        IsVisibleTV(Panel1, TV1, '0', True);
        v1ConNo.Visible:=False;
        v1orderNo.Visible:=False;
        v1MPRTMF.Visible:=False;
        v1MPRTKZ.Visible:=False;
        v1lidan.Visible:=False;
        v1ywy.Visible:=False;
      end;
  end;

  VC_JQty.Visible := True;
  VC_KGQty.Visible := True;
  VC_NetKGQty.Visible := True;
  VC_Qty.Visible := True;

  VC_ISHj.Visible := True;
  if cxTabControl1.TabIndex<>3 then
  begin
    VC_JYF.Visible:=True;
    VC_TJDate.Visible := True;
    VC_PJKuLin.Visible := True;
  end;
end;

procedure TfrmCKProductBCPKCHZList.FormDestroy(Sender: TObject);
begin
//  frmCKProductBCPKCHZList := nil;
end;

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

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

procedure TfrmCKProductBCPKCHZList.InitGrid();
var
  Pwhere, fwhere: string;
begin
  {with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select top 1 * from CK_BanCP_KCKL where DATEDIFF(mi,TJDate,getdate ())>60 ');
    Open;
  end;
  if not ADOQueryTemp.IsEmpty then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Update_FinshHZKL ');
      ExecSQL;
    end;
  end; }

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      case cxTabControl1.TabIndex of
        0:   /////////////////////////订单汇总//////////////////////////////
          begin
            Pwhere := SGetFilters(Panel1, 1, 2);
            if trim(Pwhere) <> '' then
              fwhere := fwhere + ' and ' + trim(Pwhere);
            if trim(Pwhere) <> '' then
            begin
              if chkyhj.Checked then
              begin
                fwhere := fwhere + ' and ISHj=1 ';
              end;
              if chkwhj.Checked then
              begin
                fwhere := fwhere + ' and ISHj=0 ';
              end;
            end;

            sql.Add('exec P_View_FinshHZKC  @HZType=''订单'' ');
            sql.Add(', @criteria=' + quotedstr(Trim(fwhere)));
          end;
        1:      /////////////////////////颜色汇总//////////////////////////////
          begin
            Pwhere := SGetFilters(Panel1, 1, 2);
            if trim(Pwhere) <> '' then
              fwhere := fwhere + ' and ' + trim(Pwhere);
            if trim(Pwhere) <> '' then
            begin
              if chkyhj.Checked then
              begin
                fwhere := fwhere + ' and ISHj=1 ';
              end;
              if chkwhj.Checked then
              begin
                fwhere := fwhere + ' and ISHj=0 ';
              end;
            end;
            sql.Add('exec P_View_FinshHZKC  @HZType=''颜色'' ');
            sql.Add(', @criteria=' + quotedstr(Trim(fwhere)));
          end;

        2:      /////////////////////////缸号汇总//////////////////////////////
          begin
            Pwhere := SGetFilters(Panel1, 1, 2);
            if trim(Pwhere) <> '' then
              fwhere := fwhere + ' and ' + trim(Pwhere);
            if trim(Pwhere) <> '' then
            begin
              if chkyhj.Checked then
              begin
                fwhere := fwhere + ' and ISHj=1 ';
              end;
              if chkwhj.Checked then
              begin
                fwhere := fwhere + ' and ISHj=0 ';
              end;
            end;
            sql.Add('exec P_View_FinshHZKC  @HZType=''缸号'' ');
            sql.Add(', @criteria =' + quotedstr(Trim(fwhere)));
          end;
        3:   /////////////////////////产品汇总//////////////////////////////
          begin
            Pwhere := SGetFilters(Panel1, 1, 2);
            if trim(Pwhere) <> '' then
              fwhere := fwhere + ' and ' + trim(Pwhere);
            if trim(Pwhere) <> '' then
            begin
              if chkyhj.Checked then
              begin
                fwhere := fwhere + ' and ISHj=1 ';
              end;
              if chkwhj.Checked then
              begin
                fwhere := fwhere + ' and ISHj=0 ';
              end;
            end;

            sql.Add('exec P_View_FinshHZKC  @HZType=''产品'' ');
            sql.Add(', @criteria=' + quotedstr(Trim(fwhere)));
          end;

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

procedure TfrmCKProductBCPKCHZList.TBRafreshClick(Sender: TObject);
begin
  SetStatus();
  //BegDate.SetFocus;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  InitGrid();
  MovePanel2.Visible := False;
end;

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

procedure TfrmCKProductBCPKCHZList.TBCloseClick(Sender: TObject);
begin
  if Trim(DName)='ADMIN' then
  begin
    WriteCxGridAll(trim(self.Caption), Tv1,ADOQueryCmd,ADOQueryTemp);
  end;


  Close;
end;

procedure TfrmCKProductBCPKCHZList.FormShow(Sender: TObject);
begin
  ReadCxGridAll(trim(self.Caption), Tv1);
end;

procedure TfrmCKProductBCPKCHZList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('库存汇总列表', cxGrid2);
end;

procedure TfrmCKProductBCPKCHZList.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 TfrmCKProductBCPKCHZList.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPKCHZList.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SOrdDefStr10';
      flagname := '库存存放地点';
      if ShowModal = 1 then
      begin
        with CDS_Main do
        begin
          Edit;
          FieldByName('SOrdDefStr10').Value := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Sub Set SOrdDefStr10=''' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString) + '''');
          sql.Add(' where SubId=''' + Trim(Self.CDS_Main.fieldbyname('SubId').AsString) + '''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCKProductBCPKCHZList.PRTColorChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPKCHZList.Tv1DblClick(Sender: TObject);
var
  Tabint: integer;
begin
  Tabint := cxTabControl1.TabIndex;
  case Tabint of
    0:
      begin
        AssignmentControls(Panel1, CDS_Main, '0');
        cxTabControl1.TabIndex := 1;
      end;
    1:
      begin
        AssignmentControls(Panel1, CDS_Main, '1');
        cxTabControl1.TabIndex := 2;
      end;
  end;

end;

procedure TfrmCKProductBCPKCHZList.cxTabControl1Change(Sender: TObject);
begin

  SetStatus();
  InitGrid();
end;

procedure TfrmCKProductBCPKCHZList.ToolButton1Click(Sender: TObject);
var
  fPrintFile: string;
begin
  if CDS_Main.IsEmpty then
    EXIT;

  case cxTabControl1.TabIndex of
    0:
      begin
        ExportFtErpFile('成品汇总订单码单.rmf', ADOQueryTemp);
        fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\成品汇总订单码单.rmf';
      end;

    1:
      begin
        ExportFtErpFile('成品汇总颜色码单.rmf', ADOQueryTemp);
        fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\成品汇总颜色码单.rmf';
      end;

    2:
      begin
        ExportFtErpFile('成品汇总缸号码单.rmf', ADOQueryTemp);
        fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\成品汇总缸号码单.rmf';
      end;
  end;

  if FileExists(fPrintFile) then
  begin
//    RM1.DefaultCopies := 2;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmCKProductBCPKCHZList.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  FKuLin: double;
begin
     // 库龄 白15天 绿15到30天 蓝30到45天  黄45到60天  红60天以上  是

  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('PJKuLin').Index] = null then
    Exit;
  FKuLin := AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('PJKuLin').Index];

  if ((FKuLin < 30) and (FKuLin >= 15)) then
  begin
    ACanvas.Brush.Color := $00FF00;
  end;
  if ((FKuLin < 45) and (FKuLin >= 30)) then
  begin
    ACanvas.Brush.Color := $ED9564;
  end;
  if ((FKuLin < 60) and (FKuLin >= 45)) then
  begin
    ACanvas.Brush.Color := $00FFFF;
  end;
  if FKuLin >= 60 then
  begin
    ACanvas.Brush.Color := $4763FF;
  end;

end;

procedure TfrmCKProductBCPKCHZList.ToolButton2Click(Sender: TObject);
begin

  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Update_FinshHZKL ');
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;

  except
    ADOQueryCmd.Connection.RollbackTrans;

    Application.MessageBox('更新失败!', '提示', 0);
  end;
end;

procedure TfrmCKProductBCPKCHZList.v1Column6PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxCheckBox(Sender).EditValue;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    if mvalue = 'False' then
      sql.Add('update JYOrder_Main set ISHj=0 where MAINID=' + quotedstr(Trim(CDS_Main.fieldbyname('MAINID').AsString)))
    else
      sql.Add('update JYOrder_Main set ISHj=1 where MAINID=' + quotedstr(Trim(CDS_Main.fieldbyname('MAINID').AsString)));

    sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
    sql.Add(' ' + quotedstr(trim(DName)));
    sql.Add(',getdate() ');
    sql.Add(',' + quotedstr(trim(self.Caption)));
    sql.Add(',' + quotedstr(trim('修改上架子')));
    sql.Add(',' + quotedstr(Trim(CDS_Main.fieldbyname('MAINID').AsString)));
    sql.Add(',' + quotedstr(trim('成功')));
    sql.Add(')');
    execsql;
  end;
end;

procedure TfrmCKProductBCPKCHZList.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Trim(canshu1) <> '管理' then
  begin
    VC_ISHj.Editing := False;
  end
  else
  begin
    VC_ISHj.Editing := true;
  end;

end;

procedure TfrmCKProductBCPKCHZList.chkwhjClick(Sender: TObject);
begin

  if chkwhj.Checked then
  begin
    chkyhj.Checked := False;
  end;
  InitGrid();
end;

procedure TfrmCKProductBCPKCHZList.chkyhjClick(Sender: TObject);
begin
  if chkyhj.Checked then
  begin
    chkwhj.Checked := False;
  end;
  InitGrid();
end;

procedure TfrmCKProductBCPKCHZList.CheckBox1Click(Sender: TObject);
var
  i:Integer;
begin
  with Panel1 do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if CheckBox1.Checked=True then
        Controls[i].Tag:=1
      else
        Controls[i].Tag:=2;
    end;
  end;
  TBFind.Click;
end;


end.

