unit U_PlanProfitDetails;

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, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu, cxPC, dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges,
  U_BaseHelp, Vcl.Menus, Vcl.Clipbrd, DateUtils, dxSkinWXI,
  dxScrollbarAnnotations, U_BaseList, System.ImageList, Vcl.ImgList;

type
  TfrmPlanProfitDetails = class(TfrmBasehelp)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    OrderNo: TEdit;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    Label1: TLabel;
    ComTaiTou: TEdit;
    ADOQueryPrint: TADOQuery;
    ToolButton1: TToolButton;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v2CRTime: TcxGridDBColumn;
    Tv1Column27: TcxGridDBColumn;
    Tv1Column23: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2FactoryName: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    v2OrderNo: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column24: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    Tv1Column21: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column30: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column26: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    Tv1Column25: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column28: TcxGridDBColumn;
    Tv1Column29: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn42: TcxGridDBColumn;
    cxGridDBColumn46: TcxGridDBColumn;
    cxGridDBColumn47: TcxGridDBColumn;
    cxGridDBColumn50: TcxGridDBColumn;
    cxGridDBColumn52: TcxGridDBColumn;
    cxGridDBColumn53: TcxGridDBColumn;
    cxGridDBColumn55: TcxGridDBColumn;
    cxGridDBColumn56: TcxGridDBColumn;
    cxGridDBColumn57: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    ADOQuery3: TADOQuery;
    ADOQuery2: TADOQuery;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    DS_3: TDataSource;
    CDS_3: TClientDataSet;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    TV2Column6: TcxGridDBColumn;
    TV3Column1: TcxGridDBColumn;
    TV3Column2: TcxGridDBColumn;
    cxSplitter1: TcxSplitter;
    TV2Column7: TcxGridDBColumn;
    TV3Column3: TcxGridDBColumn;
    cxGrid4: TcxGrid;
    TV4: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridDBColumn27: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    cxGridDBColumn29: TcxGridDBColumn;
    cxGridDBColumn30: TcxGridDBColumn;
    cxGridDBColumn31: TcxGridDBColumn;
    cxGridDBColumn32: TcxGridDBColumn;
    cxGridDBColumn33: TcxGridDBColumn;
    cxGridDBColumn34: TcxGridDBColumn;
    cxGridDBColumn35: TcxGridDBColumn;
    cxGridDBColumn36: TcxGridDBColumn;
    cxGridDBColumn37: TcxGridDBColumn;
    cxGridDBColumn38: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    cxSplitter2: TcxSplitter;
    DataSource1: TDataSource;
    TV4Column1: TcxGridDBColumn;
    Tv1Column22: TcxGridDBColumn;
    ClientDataSet1: TClientDataSet;
    DataSource2: TDataSource;
    ADOQuery5: TADOQuery;
    TV4Column2: TcxGridDBColumn;
    TV4Column3: TcxGridDBColumn;
    TV4Column4: TcxGridDBColumn;
    Tv1Column31: TcxGridDBColumn;
    Tv1Column32: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    procedure InitYFFGrid();
    procedure InitZD();
  public
    fmanage, FZKType, FOrderNo, FMainID: string;
    { Public declarations }
    RKFlag, FCYID, FUnit: string;
  end;

var
  frmPlanProfitDetails: TfrmPlanProfitDetails;

implementation

uses
  U_DataLink, U_RtFun, U_ZDYHelp;


{$R *.dfm}

procedure TfrmPlanProfitDetails.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add('Select Amount=(Case when ISNULL(A.RegType,'''')=''收款'' then -1*Amount else Amount end)');
//      sql.Add(',SSAmount=(Case when ISNULL(A.RegType,'''')=''收款'' then -1*Amount else Amount end)-ISNULL(A.SXFAmount,0)');
//      sql.Add(',SSCNYAmount=((Case when ISNULL(A.RegType,'''')=''收款'' then -1*Amount else Amount end) ');
//      sql.Add('-ISNULL(A.SXFAmount,0))*ISNULL((Case when Currency=''CNY'' then 1 else A.JHExchangeRate end),1)');

      sql.Add(',AAA = ISNULL((select CWBSY from Costing_Table where COrderNo = A.OrderNo), 0),');
      sql.Add('BBB = ISNULL((select CRMBSSJE from Costing_Table where COrderNo = A.OrderNo), 0),');
      sql.Add('CCC = ISNULL((select CTSSY from Costing_Table where COrderNo = A.OrderNo), 0)');



      SQL.Add(',BuyConNO=ISNULL(A.BuyConNo,(select TOP 1 BuyConNo from Trade_Plan_Sub X where X.MainID=A.OrdMainId ))');
      SQL.Add(',A.*,Saleser=(select Top 1 Saleser from BS_Company X where X.CoAbbrName=A.OppCoName)');
      sql.Add(' from Finance_Flow A where A.OrderNo= ' + QuotedStr(Trim(FOrderNo)));
      sql.Add(' and FFFlag =  ''应收收'' ');
//      sql.Add(' and ISNULL(A.FFTime,A.SQDate)<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
      //ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_HZ);
    SInitCDSData(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    TV4.DataController.Filter.Clear;
  end;
//  ToolButton2.Click;
end;

procedure TfrmPlanProfitDetails.InitYFFGrid();
begin
  try
    ADOQuery5.DisableControls;
    with ADOQuery5 do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add('Select Amount=(Case when ISNULL(A.RegType,'''')=''付款'' then -1*Amount else Amount end)');
//      sql.Add(',SSAmount=(Case when ISNULL(A.RegType,'''')=''收款'' then -1*Amount else Amount end)-ISNULL(A.SXFAmount,0)');
//      sql.Add(',SSCNYAmount=((Case when ISNULL(A.RegType,'''')=''收款'' then -1*Amount else Amount end) ');
//      sql.Add('-ISNULL(A.SXFAmount,0))*ISNULL((Case when Currency=''CNY'' then 1 else A.JHExchangeRate end),1)');

      sql.Add(',DDD = ISNULL((select GDS from Costing_Table where COrderNo = A.OrderNo), 0),');
      sql.Add('EEE = ISNULL((select COrderSF from Costing_Table where COrderNo = A.OrderNo), 0),');


      SQL.Add('BuyConNO=ISNULL(A.BuyConNo,(select TOP 1 BuyConNo from Trade_Plan_Sub X where X.MainID=A.OrdMainId ))');
      SQL.Add(',A.*,Saleser=(select Top 1 Saleser from BS_Company X where X.CoAbbrName=A.OppCoName)');
      sql.Add(' from Finance_Flow A where A.OrderNo= ' + QuotedStr(Trim(FOrderNo)));
      sql.Add(' and FFFlag =  ''应付付'' ');
//      sql.Add(' and ISNULL(A.FFTime,A.SQDate)>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
//      sql.Add(' and ISNULL(A.FFTime,A.SQDate)<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
      //ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS(ADOQuery5, ClientDataSet1);
    SInitCDSData(ADOQuery5, ClientDataSet1);
  finally
    ADOQuery5.EnableControls;
    TV1.DataController.Filter.Clear;
  end;
//  ToolButton2.Click;
end;

procedure TfrmPlanProfitDetails.InitZD();
begin

  with ADOQuery2 do
  begin
    Filtered := False;
    Close;
    SQL.Clear;
    sql.Add('select A.OrderNo,C.OrderNo TOOrdNO,A.C_Pattern,A.C_Color,A.C_ColorNo,ISNULL(A.ZDPrice,0) ZDPrice');
    if FUnit = 'M' then
    begin
      sql.Add(',ZDAmount=(ISNULL(A.ZDPrice,0)*SUM(StkMeter))')
    end
    else if FUnit = 'Y' then
    begin
      sql.Add(',ZDAmount=(ISNULL(A.ZDPrice,0)*SUM(StkYardage))')
    end
    else if FUnit = 'Kg' then
    begin
      sql.Add(',ZDAmount=(ISNULL(A.ZDPrice,0)*SUM(StkGrossWeight))')
    end;
    SQL.Add(',COUNT(CIID) Piece,SUM(StkGrossWeight) GrossWeight');
    sql.Add(',SUM(StkNetWeight) NetWeight,SUM(StkMeter) Meter,SUM(StkYardage) Yardage');
    SQL.Add('from Trade_Cloth_Stock_ZD  A left join Trade_Plan_Sub B on A.ZDSubId=B.SubID');
    sql.Add('left join Trade_Plan_Main C on C.MainId=B.MainId');
    sql.Add(' where A.MainID= ' + QuotedStr(Trim(FMainID)));
    Sql.Add('GROUP BY A.OrderNo,C.OrderNo,A.C_Pattern,A.C_Color,A.C_ColorNo,A.ZDPrice');
//      ShowMessage(sql.text);
    Open;
  end;
  SCreateCDS(ADOQuery2, CDS_2);
  SInitCDSData(ADOQuery2, CDS_2);

  with ADOQuery3 do
  begin
    Filtered := False;
    Close;
    SQL.Clear;
    sql.Add('select A.OrderNo FromOrdNo,C.OrderNo,B.C_Pattern,B.C_Color,B.C_ColorNo,COUNT(CIID) Piece,ISNULL(A.ZDPrice,0) ZDPrice');
      if FUnit = 'M' then
    begin
      sql.Add(',ZDAmount=(ISNULL(A.ZDPrice,0)*SUM(StkMeter))')
    end
    else if FUnit = 'Y' then
    begin
      sql.Add(',ZDAmount=(ISNULL(A.ZDPrice,0)*SUM(StkYardage))')
    end
    else if FUnit = 'Kg' then
    begin
      sql.Add(',ZDAmount=(ISNULL(A.ZDPrice,0)*SUM(StkGrossWeight))')
    end;
    SQL.Add(',SUM(StkGrossWeight) GrossWeight,SUM(StkNetWeight) NetWeight,SUM(StkMeter) Meter,SUM(StkYardage) Yardage');
    SQL.Add('from Trade_Cloth_Stock_ZD  A left join Trade_Plan_Sub B on A.ZDSubId=B.SubID');
    sql.Add('left join Trade_Plan_Main C on C.MainId=B.MainId where A.ZDMainID= ' + QuotedStr(Trim(FMainID)));
    Sql.Add('GROUP BY A.OrderNO,C.OrderNo,B.C_Pattern,B.C_Color,B.C_ColorNo,A.ZDPrice');
//      ShowMessage(sql.text);
    Open;
  end;
  SCreateCDS(ADOQuery3, CDS_3);
  SInitCDSData(ADOQuery3, CDS_3);

//  ToolButton2.Click;
end;

procedure TfrmPlanProfitDetails.N1Click(Sender: TObject);
begin

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

end;

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

procedure TfrmPlanProfitDetails.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmPlanProfitDetails.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(self.Caption), Tv1, '利润明细');
//  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
////  begdate.DateTime := StartOfTheMonth(Enddate.DateTime);
//  begdate.DateTime := EndDate.DateTime - 365;
  InitGrid();
  InitYFFGrid();
  InItZD();
end;

procedure TfrmPlanProfitDetails.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
  InitYFFGrid();
  InitZD();
end;

procedure TfrmPlanProfitDetails.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '利润明细');
end;

procedure TfrmPlanProfitDetails.ToolButton2Click(Sender: TObject);
var
  fsj: string;
begin
  if ADOQueryMain.Active = False then
    Exit;

  SDofilter(ADOQueryMain, Trim(SGetFilters(Panel1, 1, 2)));
  SCreateCDS(ADOQueryMain, CDS_HZ);
  SInitCDSData(ADOQueryMain, CDS_HZ);
end;

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

procedure TfrmPlanProfitDetails.ToolButton4Click(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_HZ.IsEmpty then
    exit;

  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    with ADOQueryPrint do
    begin
      Close;
      SQL.Clear;
      Filtered := False;
      sql.Add(' exec P_YSSK_List @begdate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', begdate.DateTime))) + ',@Enddate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1))) + ',@flag=''已审核''');
      Open;
    end;
    Open;
  end;

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

    RmVariables['begDate'] := FormatDateTime('yyyy-mm-dd', begDate.Date);
    RmVariables['enddate'] := FormatDateTime('yyyy-mm-dd', enddate.Date);
    RMVariables['ZDR'] := trim(DName);
    RM1.LoadFromFile(filepath);
    RM1.ShowReport;
  finally

  end;

end;

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

end.

