unit U_TradePlanSchedule;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxDropDownEdit, RM_e_Xls, cxGridBandedTableView,
  cxGridDBBandedTableView, BtnEdit, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges,
  dxBarBuiltInMenu, U_BaseList, cxPC, Vcl.Menus, Clipbrd, dxSkinWXI,
  dxScrollbarAnnotations;

type
  TfrmTradePlanSchedule = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    cxGrid1: TcxGrid;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    GPM_1: TcxGridPopupMenu;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    Label5: TLabel;
    ToolButton3: TToolButton;
    RMGridReport1: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    ADOQueryPrint: TADOQuery;
    Panetime: TPanel;
    TV1: TcxGridDBBandedTableView;
    V1Column1: TcxGridDBBandedColumn;
    VCConNo: TcxGridDBBandedColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    V1Column16: TcxGridDBBandedColumn;
    V1Column17: TcxGridDBBandedColumn;
    V1Column18: TcxGridDBBandedColumn;
    V1Column19: TcxGridDBBandedColumn;
    V1Column20: TcxGridDBBandedColumn;
    CustName: TEdit;
    TV1Column5: TcxGridDBBandedColumn;
    Label7: TLabel;
    OrderNo: TEdit;
    TV1Column13: TcxGridDBBandedColumn;
    TV1Column14: TcxGridDBBandedColumn;
    TV1Column15: TcxGridDBBandedColumn;
    TV1Column12: TcxGridDBBandedColumn;
    TV1Column21: TcxGridDBBandedColumn;
    TV1Column23: TcxGridDBBandedColumn;
    TV1Column26: TcxGridDBBandedColumn;
    cxTabControl1: TcxTabControl;
    Label8: TLabel;
    BuyConNo: TEdit;
    ToolButton5: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    TV1Column30: TcxGridDBBandedColumn;
    TV1Column32: TcxGridDBBandedColumn;
    TV1Column33: TcxGridDBBandedColumn;
    Label9: TLabel;
    C_Name: TEdit;
    TV1Column27: TcxGridDBBandedColumn;
    TV1Column3: TcxGridDBBandedColumn;
    TV1Column2: TcxGridDBBandedColumn;
    TV1Column7: TcxGridDBBandedColumn;
    TV1Column4: TcxGridDBBandedColumn;
    TV1Column8: TcxGridDBBandedColumn;
    TV1Column9: TcxGridDBBandedColumn;
    TV1Column11: TcxGridDBBandedColumn;
    TV1Column16: TcxGridDBBandedColumn;
    TV1Column17: TcxGridDBBandedColumn;
    TV1Column18: TcxGridDBBandedColumn;
    TV1Column19: TcxGridDBBandedColumn;
    TV1Column6: TcxGridDBBandedColumn;
    TV1Column20: TcxGridDBBandedColumn;
    TV1Column22: TcxGridDBBandedColumn;
    TV1Column24: TcxGridDBBandedColumn;
    TV1Column25: TcxGridDBBandedColumn;
    TV1Column29: TcxGridDBBandedColumn;
    TV1Column31: TcxGridDBBandedColumn;
    TV1Column34: TcxGridDBBandedColumn;
    TV1Column35: TcxGridDBBandedColumn;
    TV1Column36: TcxGridDBBandedColumn;
    TV1Column37: TcxGridDBBandedColumn;
    Label1: TLabel;
    TV1Column1: TcxGridDBBandedColumn;
    TV1Column10: TcxGridDBBandedColumn;
    TV1Column28: TcxGridDBBandedColumn;
    TV1Column38: TcxGridDBBandedColumn;
    TV1Column39: TcxGridDBBandedColumn;
    TV1Column40: TcxGridDBBandedColumn;
    TV1Column41: TcxGridDBBandedColumn;
    TV1Column42: TcxGridDBBandedColumn;
    TV1Column43: TcxGridDBBandedColumn;
    Label3: TLabel;
    Panel2: TPanel;
    Label4: TLabel;
    Panel3: TPanel;
    Label10: TLabel;
    Panel4: TPanel;
    Label11: TLabel;
    TV1Column44: TcxGridDBBandedColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure P_OrderNo_YSK(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TV1DblClick(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure TV1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure CustNameKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }

    procedure InitGrid();
  public
    { Public declarations }
    RKFlag, FCYID, FZKType, FBZType: string;
    fType,Canshu2: string;
  end;

var
  frmTradePlanSchedule: TfrmTradePlanSchedule;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_TradePlanScheduleDetail;

{$R *.dfm}

procedure TfrmTradePlanSchedule.InitGrid();
var
  MCG, MDH, MTP, MFH, MLen, MWeight: Double;
begin

  screen.Cursor := crsqlwait;
  Panetime.Visible := true;
  Panetime.Left := (Width - Panetime.Width) div 2;
  Panetime.top := (Height - Panetime.Height - 200) div 2;
  application.ProcessMessages;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      Filtered := False;
      sql.Add(' exec  P_Trade_Plan_Schedule_1  ');
      sql.Add('  @begdate=' + QuotedStr(Trim(FormatDateTime('yyyy-MM-dd', begdate.DateTime))));
      sql.Add(' ,@Enddate=' + QuotedStr(Trim(FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1))));
      if canshu2='业务' then
      sql.Add(' ,@Saleser=' + QuotedStr(Trim(DName)));

      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' ,@WCFlag=''未完成'' ');
          end;
        1:
          begin
            sql.Add(' ,@WCFlag=''暂存区'' ');
          end;
        2:
          begin
            sql.Add(' ,@WCFlag=''生产完成'' ');
          end;
        3:
          begin
            sql.Add(' ,@WCFlag=''已完成'' ');
          end;
        4:
          begin
            sql.Add(' ,@WCFlag=''完成'' ');
          end;
      end;

      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
    Panetime.Visible := false;
    screen.Cursor := crdefault;
    TV1.DataController.Filter.Clear;
  end;
  if ADOQueryMain.IsEmpty = False then
  begin
    MCG := tv1.DataController.Summary.FooterSummaryValues[14];
    MDH := tv1.DataController.Summary.FooterSummaryValues[0];
    MTP := tv1.DataController.Summary.FooterSummaryValues[3];
    MFH := tv1.DataController.Summary.FooterSummaryValues[15];
    Label1.Caption := '未到货：' + FloattoStr(MCG - MDH) + '    待投坯：' + FloattoStr(MDH - MTP) + '    生产中：' + FloattoStr(MTP - MFH);

    MLen:= tv1.DataController.Summary.FooterSummaryValues[17];
    MWeight:=tv1.DataController.Summary.FooterSummaryValues[16];
    Label11.Caption := '下单米数：' + FloattoStr(MLen) + '    下单重量：' + FloattoStr(MWeight);
  end;
end;

procedure TfrmTradePlanSchedule.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTradePlanSchedule := nil;
end;

procedure TfrmTradePlanSchedule.OrderNoChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmTradePlanSchedule.CustNameKeyPress(Sender: TObject;
  var Key: Char);
begin
  inherited;
  if Key = #13 then
  begin
    Key := #0;
    ToolButton2.Click;
  end;
end;

procedure TfrmTradePlanSchedule.cxTabControl1Change(Sender: TObject);
begin

  InitGrid();
end;


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

procedure TfrmTradePlanSchedule.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTradePlanSchedule.FormShow(Sender: TObject);
begin
  inherited;
  readCxBandedGrid(self.Caption + '3', Tv1, '进度统计查询');
  canshu2 := self.fParameters2;
  InitGrid();
end;

procedure TfrmTradePlanSchedule.TBRafreshClick(Sender: TObject);
begin
  begdate.SetFocus;
  InitGrid();
end;

procedure TfrmTradePlanSchedule.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  end;
end;

procedure TfrmTradePlanSchedule.P_OrderNo_YSK(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmTradePlanSchedule.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

procedure TfrmTradePlanSchedule.MPRTCodeNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmTradePlanSchedule.N1Click(Sender: TObject);
begin

  Clipboard.SetTextBuf(PChar(Trim(CDS_1.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmTradePlanSchedule.FormCreate(Sender: TObject);
begin
  inherited;
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := Enddate.DateTime - 60;
end;

procedure TfrmTradePlanSchedule.ToolButton3Click(Sender: TObject);
var
  filepath: string;
  mBegdate, mEnddate: string;
begin
  mBegdate := FormatDateTime('yyyy-MM-dd', begdate.Date);
  mEnddate := FormatDateTime('yyyy-MM-dd', enddate.Date);
  if CDS_1.IsEmpty then
    exit;

  try

    filepath := ExtractFilePath(Application.ExeName) + 'report\订单利润表.rmf';
    if not FileExists(Pchar(filepath)) then
    begin
      application.MessageBox(pchar('文件[' + filepath + ']不存在！'), '提示信息', MB_IConError);
      exit;
    end;

    RMGridReport1.LoadFromFile(filepath);

    RMGridReport1.ShowReport;
  finally

  end;

end;

procedure TfrmTradePlanSchedule.ToolButton5Click(Sender: TObject);
begin

  WriteCxBandedGrid(self.Caption + '3', Tv1, '进度统计查询');
end;

procedure TfrmTradePlanSchedule.TV1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if cxTabControl1.TabIndex = 0 then
  begin
    if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('DQFlag').Index] = null then
      Exit;
    if (AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('DQFlag').Index]) = 1 then
      ACanvas.Brush.Color := $4763FF;
    if (AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('DQFlag').Index]) = 2 then
      ACanvas.Brush.Color := $008CFF;
    if (AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('DQFlag').Index]) = 3 then
      ACanvas.Brush.Color := $00FFFF;
  end;

end;

procedure TfrmTradePlanSchedule.TV1DblClick(Sender: TObject);
begin

  try
    frmTradePlanScheduleDetail := TfrmTradePlanScheduleDetail.Create(Application);
    with frmTradePlanScheduleDetail do
    begin
      FMainId := self.CDS_1.FieldByName('MainId').AsString;
      FOrderNo := self.CDS_1.FieldByName('OrderNo').AsString;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmTradePlanScheduleDetail.Free;
  end;
end;

end.

