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, U_BaseList, cxLookAndFeels,
  cxLookAndFeelPainters,   
  cxNavigator, dxDateRanges, dxScrollbarAnnotations, cxTextEdit,
  dxBarBuiltInMenu, Vcl.Menus, RM_e_Xls, cxPC, cxGridBandedTableView,
  cxGridDBBandedTableView, FrameDateSel, cxButtonEdit, cxDropDownEdit,
  cxContainer, cxProgressBar;

type
  TfrmTradePlanSchedule = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    GPM_1: TcxGridPopupMenu;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    Label5: TLabel;
    ToolButton3: TToolButton;
    RMGridReport1: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    ADOQueryPrint: TADOQuery;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    CustName: TcxTextEdit;
    Label7: TLabel;
    ConNo: TcxTextEdit;
    cxTabControl1: TcxTabControl;
    ToolButton5: TToolButton;
    Label9: TLabel;
    PMGY: TcxTextEdit;
    frmFrameDateSel1: TfrmFrameDateSel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    VC_SCSCode: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1PRTPrice: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column5: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    TV1Column7: TcxGridDBColumn;
    TV1Column8: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    TV1Column9: TcxGridDBColumn;
    TV1Column10: TcxGridDBColumn;
    TV1Column11: TcxGridDBColumn;
    TV1Column12: TcxGridDBColumn;
    TV1Column13: TcxGridDBColumn;
    TV1Column14: TcxGridDBColumn;
    ToolButton4: TToolButton;
    TV1Column3: TcxGridDBColumn;
    Label1: TLabel;
    Label2: TLabel;
    GC_Name: TcxTextEdit;
    CraftCode: TcxTextEdit;
    TV1Column15: TcxGridDBColumn;
    TV1Column16: TcxGridDBColumn;
    Label3: TLabel;
    Saleser: TcxTextEdit;
    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 ConNoChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure TV1DblClick(Sender: TObject);
    procedure ConNoPropertiesEditValueChanged(Sender: TObject);
    procedure TV1Column15PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }

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

var
  frmTradePlanSchedule: TfrmTradePlanSchedule;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_TradePlanScheduleDetail;

{$R *.dfm}

procedure TfrmTradePlanSchedule.InitGrid();
begin

  screen.Cursor := crsqlwait;
  application.ProcessMessages;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      Filtered := False;
      sql.Add(' exec  P_Trade_Plan_Schedule  ');
      sql.Add('  @begdate=' + QuotedStr(Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.begdate.Date))));
      sql.Add(' ,@Enddate=' + QuotedStr(Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.Enddate.Date + 1))));
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' ,@WCFlag=''未完成'' ');
          end;
        1:
          begin
            sql.Add(' ,@WCFlag=''已完成'' ');
          end;
        2:
          begin
            sql.Add(' ,@WCFlag=''全部'' ');
          end;
      end;

      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
    screen.Cursor := crdefault;
    TV1.DataController.Filter.Clear;
  end;
end;

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

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

procedure TfrmTradePlanSchedule.cxTabControl1Change(Sender: TObject);
begin

  InitGrid();
end;

procedure TfrmTradePlanSchedule.ConNoPropertiesEditValueChanged(Sender: TObject);
begin
  ToolButton2.Click;
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;
  readCxGrid(self.Caption, Tv1, '进度统计查询');
  InitGrid();
end;

procedure TfrmTradePlanSchedule.TBRafreshClick(Sender: TObject);
begin
  frmFrameDateSel1.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.FormCreate(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.Enddate.Date := SGetServerDate(ADOQueryTemp);
  frmFrameDateSel1.begdate.Date := frmFrameDateSel1.Enddate.Date - 365;
end;

procedure TfrmTradePlanSchedule.ToolButton3Click(Sender: TObject);
var
  filepath: string;
  mBegdate, mEnddate: string;
begin
  mBegdate := FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.begdate.Date);
  mEnddate := FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.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.ToolButton4Click(Sender: TObject);
begin
  try
    frmTradePlanScheduleDetail := TfrmTradePlanScheduleDetail.Create(Application);
    with frmTradePlanScheduleDetail do
    begin

      FConNo := self.CDS_1.FieldByName('ConNo').AsString;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmTradePlanScheduleDetail.Free;
  end;
end;

procedure TfrmTradePlanSchedule.ToolButton5Click(Sender: TObject);
begin

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

procedure TfrmTradePlanSchedule.TV1Column15PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_1 do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add(' update BS_Contract_Main   ');
    sql.Add(' set  Note2=' + QuotedStr(Trim(mvalue)));
    sql.Add('where ConMid =' + quotedstr((CDS_1.fieldbyname('ConMid').AsString)));
    ExecSQL;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmTradePlanSchedule.TV1DblClick(Sender: TObject);
begin
  ToolButton4.Click;
end;

end.

