unit U_TatPlanMachList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, ExtCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxButtonEdit, cxDropDownEdit, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, cxContainer,
  cxTextEdit, cxCurrencyEdit, cxCheckBox, RM_System, RM_Dataset, RM_Common,
  RM_Class, RM_GridReport, cxPC, Menus, RM_e_Xls, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxSkinsCore, dxSkinsDefaultPainters,
  dxDateRanges, dxBarBuiltInMenu, U_BaseList;

type
  TfrmTatPlanMachList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    GPM_1: TcxGridPopupMenu;
    ADOQueryMain: TADOQuery;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    VC_MCNo: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label15: TLabel;
    MCNo: TEdit;
    Label1: TLabel;
    OrderNo: TEdit;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Label5: TLabel;
    C_Code: TEdit;
    ToolButton2: TToolButton;
    cxTabControl1: TcxTabControl;
    Label2: TLabel;
    Label3: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label4: TLabel;
    C_Name: TEdit;
    Tv1Column2: TcxGridDBColumn;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure P_CodeNameChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure GSBHChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure N1Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetStatus();
    procedure SetSequence(MOperate: string);
    { Private declarations }
  public
    fCKName, canshu1, FAPID: string;
    fCRType: string;
    { Public declarations }
  end;

var
  frmTatPlanMachList: TfrmTatPlanMachList;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_TatPlanMachInput;
{$R *.dfm}

procedure TfrmTatPlanMachList.SetStatus();
begin

end;

procedure TfrmTatPlanMachList.SetSequence(MOperate: string);
begin
  if cxTabControl1.TabIndex <> 0 then
  begin
    application.MessageBox('请在未生产状态下', '提示信息', 0);
    exit;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      sql.Add('EXEC P_Tat_Plan_Mach_SetSequence  @PMID=''' + Trim(Self.CDS_1.fieldbyname('PMID').AsString) + '''');
      sql.Add(',@Operate=' + QuotedStr(MOperate));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmTatPlanMachList.InitGrid();
var
  MPmid: string;
begin
  if not CDS_1.IsEmpty then
    MPmid := Trim(Self.CDS_1.fieldbyname('PMID').AsString);

  with adoqueryMain do
  begin
    close;
    sql.Clear;
    sql.Add(' exec P_Tat_Plan_Mach ');
    sql.add(' @BegDate=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
    sql.Add(' ,@EndDate=''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');

    case cxTabControl1.TabIndex of
      0:
        begin
          sql.Add(',@Status=''未完成'' ');
        end;
      1:
        begin
          sql.Add(',@Status=''生产中'' ');
        end;
      2:
        begin
          sql.Add(',@Status=''已完成'' ');
        end;
      3:
        begin
          sql.Add(',@Status=''全部'' ');
        end;
    end;

    open;
  end;
  SCreateCDS(adoqueryMain, CDS_1);
  SInitCDSData(adoqueryMain, CDS_1);
  CDS_1.Locate('Pmid', MPmid, []);
end;

procedure TfrmTatPlanMachList.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmTatPlanMachList.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := cafree;
end;

procedure TfrmTatPlanMachList.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTatPlanMachList := nil;
end;

procedure TfrmTatPlanMachList.P_CodeNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmTatPlanMachList.FormShow(Sender: TObject);
begin
  inherited;
  BegDate.DateTime := SGetServerDate(ADOQueryTmp) - 30;
  EndDate.DateTime := SGetServerDate(ADOQueryTmp);
  readCxGrid(self.Caption, Tv1, '仓储管理');
  SetStatus();
  InitGrid();
end;

procedure TfrmTatPlanMachList.TBCloseClick(Sender: TObject);
begin

  close;
end;

procedure TfrmTatPlanMachList.TBRafreshClick(Sender: TObject);
begin
  initGrid();
end;

procedure TfrmTatPlanMachList.TBExportClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

procedure TfrmTatPlanMachList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmTatPlanMachList.N1Click(Sender: TObject);
begin
  SetSequence('上移');
end;

procedure TfrmTatPlanMachList.N2Click(Sender: TObject);
begin
  SetSequence('下移');
end;

procedure TfrmTatPlanMachList.GSBHChange(Sender: TObject);
begin
  if ADOQuerymain.Active = False then
    Exit;
  SDofilter(ADOQuerymain, SGetFilters(Panel1, 1, 3));
  SCreateCDS(ADOQuerymain, CDS_1);
  SInitCDSData(ADOQuerymain, CDS_1);
end;

procedure TfrmTatPlanMachList.ToolButton1Click(Sender: TObject);
begin
  try
    frmTatPlanMachInput := TfrmTatPlanMachInput.Create(Application);
    with frmTatPlanMachInput do
    begin
      FMCNo := Trim(Self.CDS_1.fieldbyname('MCNo').AsString);
//      FPMID := Trim(Self.CDS_1.fieldbyname('PMID').AsString);
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmTatPlanMachInput.Free;
  end;
end;

procedure TfrmTatPlanMachList.ToolButton2Click(Sender: TObject);
begin
  writeCxGrid(self.Caption, Tv1, '仓储管理');
end;

end.

