unit U_PSC_Card;

interface

uses
  Windows, Messages, SysUtils, Variants, strutils, 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, cxCalendar, RM_System, RM_Common,
  RM_Class, RM_GridReport, RM_Dataset, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxBarBuiltInMenu, cxPC, dxSkinsCore, dxSkinsDefaultPainters,
  U_BaseList, dxDateRanges, U_BaseHelp, System.ImageList, Vcl.ImgList;

type
  TfrmPSC_Card = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    C_Name: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    MovePanel2: TMovePanel;
    Label10: TLabel;
    RMGridReport1: TRMGridReport;
    PCID: TEdit;
    Label16: TLabel;
    custName: TEdit;
    Label13: TLabel;
    ThreeColorBase: TcxStyleRepository;
    SHuangSe: TcxStyle;
    SkyBlue: TcxStyle;
    Default: TcxStyle;
    QHuangSe: TcxStyle;
    Red: TcxStyle;
    FontBlue: TcxStyle;
    TextSHuangSe: TcxStyle;
    FonePurple: TcxStyle;
    FoneClMaroon: TcxStyle;
    FoneRed: TcxStyle;
    RowColor: TcxStyle;
    handBlack: TcxStyle;
    cxBlue: TcxStyle;
    cxStyle1: TcxStyle;
    RMDBMain: TRMDBDataSet;
    cxPageControl1: TcxPageControl;
    CDS_Sub: TClientDataSet;
    DataSource2: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGrid1: TcxGrid;
    TV2: TcxGridDBBandedTableView;
    cxGridDBBandedColumn1: TcxGridDBBandedColumn;
    cxGridDBBandedColumn2: TcxGridDBBandedColumn;
    cxGridDBBandedColumn3: TcxGridDBBandedColumn;
    cxGridDBBandedColumn4: TcxGridDBBandedColumn;
    cxGridDBBandedColumn5: TcxGridDBBandedColumn;
    cxGridDBBandedColumn6: TcxGridDBBandedColumn;
    cxGridDBBandedColumn7: TcxGridDBBandedColumn;
    cxGridDBBandedColumn8: TcxGridDBBandedColumn;
    cxGridDBBandedColumn9: TcxGridDBBandedColumn;
    cxGridDBBandedColumn10: TcxGridDBBandedColumn;
    cxGridDBBandedColumn11: TcxGridDBBandedColumn;
    cxGridDBBandedColumn12: TcxGridDBBandedColumn;
    cxGridDBBandedColumn13: TcxGridDBBandedColumn;
    cxGridDBBandedColumn14: TcxGridDBBandedColumn;
    cxGridDBBandedColumn15: TcxGridDBBandedColumn;
    cxGridDBBandedColumn16: TcxGridDBBandedColumn;
    cxGridDBBandedColumn17: TcxGridDBBandedColumn;
    cxGridDBBandedColumn18: TcxGridDBBandedColumn;
    cxGridDBBandedColumn19: TcxGridDBBandedColumn;
    cxGridDBBandedColumn20: TcxGridDBBandedColumn;
    cxGridDBBandedColumn21: TcxGridDBBandedColumn;
    cxGridDBBandedColumn22: TcxGridDBBandedColumn;
    cxGridDBBandedColumn23: TcxGridDBBandedColumn;
    cxGridDBBandedColumn24: TcxGridDBBandedColumn;
    cxGridDBBandedColumn25: TcxGridDBBandedColumn;
    cxGridDBBandedColumn26: TcxGridDBBandedColumn;
    cxGridDBBandedColumn27: TcxGridDBBandedColumn;
    cxGridDBBandedColumn28: TcxGridDBBandedColumn;
    cxGridDBBandedColumn29: TcxGridDBBandedColumn;
    cxGridDBBandedColumn30: TcxGridDBBandedColumn;
    cxGridDBBandedColumn31: TcxGridDBBandedColumn;
    cxGridDBBandedColumn32: TcxGridDBBandedColumn;
    cxGridDBBandedColumn33: TcxGridDBBandedColumn;
    cxGridDBBandedColumn34: TcxGridDBBandedColumn;
    cxGridDBBandedColumn35: TcxGridDBBandedColumn;
    cxGridLevel1: TcxGridLevel;
    ADOQueryMain: TADOQuery;
    TV2Column1: TcxGridDBBandedColumn;
    TV2Column2: TcxGridDBBandedColumn;
    TV2Column3: TcxGridDBBandedColumn;
    TV2Column4: TcxGridDBBandedColumn;
    TV2Column5: TcxGridDBBandedColumn;
    TV2Column6: TcxGridDBBandedColumn;
    TV2Column7: TcxGridDBBandedColumn;
    TV2Column8: TcxGridDBBandedColumn;
    TV2Column9: TcxGridDBBandedColumn;
    TV2Column10: TcxGridDBBandedColumn;
    TV2Column11: TcxGridDBBandedColumn;
    Label4: TLabel;
    OrderNo: TEdit;
    ToolButton1: TToolButton;
    TV2Column12: TcxGridDBBandedColumn;
    TV2Column13: TcxGridDBBandedColumn;
    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 C_NameChange(Sender: TObject);
    procedure PRTColorChange(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure TV1DblClick(Sender: TObject);
    procedure cxGridDBBandedColumn6CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure ToolButton1Click(Sender: TObject);
    procedure TV2Column12CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
  private
    FLeft, FTop: Integer;
    procedure InitGrid();
    { Private declarations }
  public
    FKH, FPM: string;
    { Public declarations }
  end;

var
  frmPSC_Card: TfrmPSC_Card;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmPSC_Card.FormDestroy(Sender: TObject);
begin
  inherited;
  frmPSC_Card := nil;
end;

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

procedure TfrmPSC_Card.FormCreate(Sender: TObject);
begin
  inherited;
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  begdate.DateTime := EndDate.DateTime - 180;
end;

procedure TfrmPSC_Card.InitGrid();
var
  WSql: string;
  mpicking_no: string;
begin
  with ADOQueryCmd do
  begin
    close;
    filtered := false;
    sql.Clear;
    sql.Add('EXEC P_Dye_Card_PSC ');
    ExecSQL;
  end;
  WSql := SGetFilters(Panel1, 1, 2);
  if trim(WSql) <> '' then
  begin
    WSql := ' and ' + trim(WSql);
  end;

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      close;
      filtered := false;
      sql.Clear;
      sql.Add('select *,SCDate=0 from Dye_Plan_Card A left join Dye_Plan_Card_PSC B on A.PCID=B.PCID');
    //PCTime
//      sql.Add('@begdate=''' + formatdateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
//      sql.Add(',@enddate=''' + formatdateTime('yyyy-MM-dd', enddate.DateTime + 1) + ''' ');
//      sql.Add(',@Flag=''0'' ');
//      sql.Add(',@customerNoName=''' + Trim(customerNoName.text) + ''' ');
//      sql.Add(',@MPRTCodeName=''' + Trim(MPRTCodeName.text) + ''' ');
//      showmessage(sql.Text);
      open;
      SCreateCDS(ADOQueryMain, CDS_Sub);
      SInitCDSData(ADOQueryMain, CDS_Sub);
    end;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmPSC_Card.TBRafreshClick(Sender: TObject);
begin
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  InitGrid();
  MovePanel2.Visible := False;
end;

procedure TfrmPSC_Card.ToolButton1Click(Sender: TObject);
begin
  WriteCxBandedGrid(trim(self.Caption) + trim(tv2.Name), Tv2, '进度管理');
end;

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

procedure TfrmPSC_Card.TBCloseClick(Sender: TObject);
begin
//  WriteCxBandedGrid(self.Caption+tv1.Name,Tv1,'成品仓库');
  Close;
end;

procedure TfrmPSC_Card.FormShow(Sender: TObject);
begin
  inherited;
//  customerNoName.Text := FKH;
//  MPRTCodeName.Text := FPM;
//  InitGrid();
//  TBFind.Click;
  ReadCxBandedGrid(trim(self.Caption) + trim(tv2.Name), Tv2, '进度管理');
end;

procedure TfrmPSC_Card.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel(self.Caption, cxgrid1);
 // TcxGridToExcel(self.Caption,cxGrid2);
end;

procedure TfrmPSC_Card.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_Sub);
    SInitCDSData(ADOQueryMain, CDS_Sub);
  end;
end;

procedure TfrmPSC_Card.C_NameChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

procedure TfrmPSC_Card.TBPrintClick(Sender: TObject);
var
  fPrintFile: string;
begin
  if CDS_Main.IsEmpty then
    Exit;

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\流程卡进度表.rmf';
  if FileExists(fPrintFile) then
  begin
    RMGridReport1.LoadFromFile(fPrintFile);
   //MVariables['SYRName']:=Trim(SYRName);
    RMVariables['begdate'] := formatdateTime('yyyy-MM-dd', begdate.DateTime);
    RMVariables['enddate'] := formatdateTime('yyyy-MM-dd', enddate.DateTime);
    RMGridReport1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + trim(fPrintFile)), '提示', 0);
  end;
end;

procedure TfrmPSC_Card.TV1DblClick(Sender: TObject);
begin
  AssignmentControls(Panel1, CDS_main, '0');
  cxPageControl1.ActivePageIndex := 1;
  InitGrid();
end;

procedure TfrmPSC_Card.TV2Column12CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  FFieldName: string;
begin
  FFieldName := AViewInfo.Item.DataBinding.FilterFieldName;
  if (AViewInfo.GridRecord.Values[tv2.GetColumnByFieldName(FFieldName).Index] >= 0) and (AViewInfo.GridRecord.Values[tv2.GetColumnByFieldName(FFieldName).Index] <= 3) then
  begin
    ACanvas.Brush.Color := clSkyBlue;
  end
  else if (AViewInfo.GridRecord.Values[tv2.GetColumnByFieldName(FFieldName).Index] > 3) and (AViewInfo.GridRecord.Values[tv2.GetColumnByFieldName(FFieldName).Index] <= 7) then
  begin
    ACanvas.Brush.Color := clFuchsia;
  end
  else if (AViewInfo.GridRecord.Values[tv2.GetColumnByFieldName(FFieldName).Index] > 7) then
  begin
    ACanvas.Brush.Color := clRed;
  end;
end;

procedure TfrmPSC_Card.cxGridDBBandedColumn6CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  FFieldName, FFieldName1: string;
begin
  FFieldName := AViewInfo.Item.DataBinding.FilterFieldName;
  FFieldName1 := 'f' + rightstr(FFieldName, length(FFieldName) - 1);

  if (not varisnull(AViewInfo.GridRecord.Values[tv2.GetColumnByFieldName(FFieldName).Index])) and (not varisnull(AViewInfo.GridRecord.Values[tv2.GetColumnByFieldName(FFieldName1).Index])) and (AViewInfo.GridRecord.Values[tv2.GetColumnByFieldName(FFieldName1).Index] = 1) then
  begin
    ACanvas.Brush.Color := clLime;
  end;
end;

procedure TfrmPSC_Card.cxPageControl1Change(Sender: TObject);
var
  i: Integer;
begin
  if cxPageControl1.ActivePageIndex = 0 then
  begin
    for i := 0 to Panel1.ControlCount - 1 do
    begin
      if Panel1.Controls[i] is TEdit then
        TEdit(Panel1.Controls[i]).Clear;
    end;
  end;
  InitGrid();
end;

end.

