unit U_TradePlanProfit;

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, cxDBLookupComboBox, cxGridBandedTableView, cxGridDBBandedTableView,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, 
   dxDateRanges, dxBarBuiltInMenu, U_BaseList,
      
     
     
     
     
    
    
    
    
    
     
     
     
    
    
     dxScrollbarAnnotations,
  cxContainer, FrameDateSel;

type
  TfrmTradePlanProfit = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_1: TRMDBDataSet;
    Label3: TLabel;
    CustName: TcxTextEdit;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    conno: TcxTextEdit;
    CDS_P1: TClientDataSet;
    CDS_P2: TClientDataSet;
    RMDB_2: TRMDBDataSet;
    ADOQueryMain: TADOQuery;
    DataSource1: TDataSource;
    CDS_1: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1CustomerNoName: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1OrderNo: TcxGridDBColumn;
    v1MPRTCodeName: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label7: TLabel;
    Tallyer: TcxTextEdit;
    Label6: TLabel;
    Saleser: TcxTextEdit;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    C_Name: TcxTextEdit;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    CDS_P3: TClientDataSet;
    RMDB_3: TRMDBDataSet;
    CDS_P4: TClientDataSet;
    RMDB_4: TRMDBDataSet;
    CDS_P5: TClientDataSet;
    RMDB_5: TRMDBDataSet;
    RMDB_6: TRMDBDataSet;
    CDS_P6: TClientDataSet;
    CDS_P7: TClientDataSet;
    RMDB_7: TRMDBDataSet;
    frmFrameDateSel1: TfrmFrameDateSel;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    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 CustNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure CustNamePropertiesEditValueChanged(Sender: TObject);
  private

    { Private declarations }

    procedure InitGrid();
  public
    fkhType: string;
    Fmanage: string;
    canshu1: string;
  end;

var
  frmTradePlanProfit: TfrmTradePlanProfit;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_TradePlanProfitDetails;

{$R *.dfm}

procedure TfrmTradePlanProfit.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' exec P_Trade_Profit_Con      ');
      sql.Add('  @PFType=''总利润'' ');
      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))));
      sql.Add(' ,@ConNo='''' ');
      Open;
    end;
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmTradePlanProfit.FormDestroy(Sender: TObject);
begin
  frmTradePlanProfit := nil;
end;

procedure TfrmTradePlanProfit.CustNamePropertiesEditValueChanged(Sender: TObject);
begin
  ToolButton2.Click;
end;

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

procedure TfrmTradePlanProfit.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTradePlanProfit.FormShow(Sender: TObject);
var
  fsj: string;
begin
  ReadCxGrid(trim(self.caption), Tv1, '利润表22');
  InitGrid();
end;

procedure TfrmTradePlanProfit.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmTradePlanProfit.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 TfrmTradePlanProfit.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.caption), Tv1, '利润表22');
end;

procedure TfrmTradePlanProfit.ToolButton4Click(Sender: TObject);
begin
  try
    frmTradePlanProfitDetails := TfrmTradePlanProfitDetails.Create(Application);
    with frmTradePlanProfitDetails do
    begin

      FConNo := Trim(Self.CDS_1.fieldbyname('ConNo').AsString);
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmTradePlanProfitDetails.Free;
  end;
end;

procedure TfrmTradePlanProfit.ToolButton5Click(Sender: TObject);
var
  filepath: string;
  mBegdate, mEnddate: string;
begin
  if CDS_1.IsEmpty then
    exit;

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec  P_Trade_Profit_Con ');
    sql.Add('@PFType=''总利润''  ');
    sql.Add(',@ConNo=  ' + quotedstr(Trim(Trim(Self.CDS_1.fieldbyname('ConNo').AsString))));
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_P1);
  SInitCDSData(ADOQueryTemp, CDS_P1);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec  P_Trade_Profit_Con ');
    sql.Add('@PFType=''计划单''  ');
    sql.Add(',@ConNo=  ' + quotedstr(Trim(Trim(Self.CDS_1.fieldbyname('ConNo').AsString))));
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_P2);
  SInitCDSData(ADOQueryTemp, CDS_P2);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec  P_Trade_Profit_Con ');
    sql.Add('@PFType=''采购''  ');
    sql.Add(',@ConNo=  ' + quotedstr(Trim(Trim(Self.CDS_1.fieldbyname('ConNo').AsString))));
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_P3);
  SInitCDSData(ADOQueryTemp, CDS_P3);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec  P_Trade_Profit_Con ');
    sql.Add('@PFType=''加工''  ');
    sql.Add(',@ConNo=  ' + quotedstr(Trim(Trim(Self.CDS_1.fieldbyname('ConNo').AsString))));
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_P4);
  SInitCDSData(ADOQueryTemp, CDS_P4);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec  P_Trade_Profit_Con ');
    sql.Add('@PFType=''其他''  ');
    sql.Add(',@ConNo=  ' + quotedstr(Trim(Trim(Self.CDS_1.fieldbyname('ConNo').AsString))));
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_P5);
  SInitCDSData(ADOQueryTemp, CDS_P5);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec  P_Trade_Profit_Con ');
    sql.Add('@PFType=''费用''  ');
    sql.Add(',@ConNo=  ' + quotedstr(Trim(Trim(Self.CDS_1.fieldbyname('ConNo').AsString))));
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_P6);
  SInitCDSData(ADOQueryTemp, CDS_P6);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec  P_Trade_Profit_Con ');
    sql.Add('@PFType=''应收''  ');
    sql.Add(',@ConNo=  ' + quotedstr(Trim(Trim(Self.CDS_1.fieldbyname('ConNo').AsString))));
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_P7);
  SInitCDSData(ADOQueryTemp, CDS_P7);

  try
    ExportFtErpFile('订单利润表.rmf', ADOQueryTemp);

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

    RM1.LoadFromFile(filepath);
    RM1.ShowReport;
  finally

  end;

end;

procedure TfrmTradePlanProfit.Tv1DblClick(Sender: TObject);
begin
  ToolButton4.Click;
end;

procedure TfrmTradePlanProfit.CustNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

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

procedure TfrmTradePlanProfit.FormCreate(Sender: TObject);
begin
  canshu1 := Trim(DParameters1);
  frmFrameDateSel1.EndDate.Date := SGetServerDate(ADOQueryTemp);
  frmFrameDateSel1.BegDate.Date := frmFrameDateSel1.EndDate.Date - 90;
end;

end.

