unit U_OrderProgress2;

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,
  cxMemo, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxSkinsCore,
  dxSkinsDefaultPainters, dxSkinscxPCPainter;

type
  TfrmOrderProgress2 = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDBHZ: TRMDBDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    EndDate: TDateTimePicker;
    Label4: TLabel;
    Label5: TLabel;
    Begdate: TDateTimePicker;
    Label2: TLabel;
    OrderNo: TEdit;
    ToolButton3: TToolButton;
    CDSPRTHZ: TClientDataSet;
    CDSPRTMX: TClientDataSet;
    RMDBMX: TRMDBDataSet;
    ADOQueryMain: TADOQuery;
    DataSource1: TDataSource;
    CDS_HZ: TClientDataSet;
    Label18: TLabel;
    CustomerNoName: TComboBox;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBBandedTableView;
    cxGridDBBandedColumn35: TcxGridDBBandedColumn;
    cxGridDBBandedColumn42: TcxGridDBBandedColumn;
    cxGridDBBandedColumn52: TcxGridDBBandedColumn;
    cxGridDBBandedColumn56: TcxGridDBBandedColumn;
    cxGridDBBandedColumn57: TcxGridDBBandedColumn;
    cxGridDBBandedColumn59: TcxGridDBBandedColumn;
    cxGridDBBandedColumn60: TcxGridDBBandedColumn;
    cxGridDBBandedColumn61: TcxGridDBBandedColumn;
    cxGridDBBandedColumn62: TcxGridDBBandedColumn;
    cxGridDBBandedColumn63: TcxGridDBBandedColumn;
    cxGridDBBandedColumn64: TcxGridDBBandedColumn;
    cxGridDBBandedColumn79: TcxGridDBBandedColumn;
    cxGridDBBandedColumn81: TcxGridDBBandedColumn;
    cxGridDBBandedColumn87: TcxGridDBBandedColumn;
    cxGridDBBandedColumn91: TcxGridDBBandedColumn;
    V1Column1: TcxGridDBBandedColumn;
    V1Column2: TcxGridDBBandedColumn;
    V1Column6: TcxGridDBBandedColumn;
    V1Column25: TcxGridDBBandedColumn;
    V1Column26: TcxGridDBBandedColumn;
    V1Column27: TcxGridDBBandedColumn;
    cxGridLevel2: TcxGridLevel;
    V1Column3: TcxGridDBBandedColumn;
    V1Column4: TcxGridDBBandedColumn;
    V1Column5: TcxGridDBBandedColumn;
    V1Column7: TcxGridDBBandedColumn;
    V1Column8: TcxGridDBBandedColumn;
    V1Column9: TcxGridDBBandedColumn;
    V1Column10: TcxGridDBBandedColumn;
    V1Column11: TcxGridDBBandedColumn;
    V1Column12: TcxGridDBBandedColumn;
    V1Column13: TcxGridDBBandedColumn;
    V1Column14: TcxGridDBBandedColumn;
    V1Column15: TcxGridDBBandedColumn;
    V1Column16: TcxGridDBBandedColumn;
    V1Column17: TcxGridDBBandedColumn;
    Panel2: TPanel;
    Panel6: TPanel;
    cxGrid2: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    CDS_SUB: TClientDataSet;
    DataSource2: TDataSource;
    TV1Column1: TcxGridDBBandedColumn;
    TV1Column2: TcxGridDBBandedColumn;
    TV1Column3: TcxGridDBBandedColumn;
    TV1Column4: TcxGridDBBandedColumn;
    TV1Column5: TcxGridDBBandedColumn;
    Label3: TLabel;
    TV1Column6: TcxGridDBBandedColumn;
    TV1Column7: TcxGridDBBandedColumn;
    TV1Column8: TcxGridDBBandedColumn;
    TV1Column9: TcxGridDBBandedColumn;
    Label1: TLabel;
    Label7: TLabel;
    Label6: TLabel;
    MPRTCode: TEdit;
    lidan: TEdit;
    ywy: TEdit;
    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 MPRTCodeNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxGridDBBandedColumn64CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
    procedure V1Column2CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
    procedure V1Column4CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
    procedure V1Column3CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
    procedure cxGridDBBandedColumn59CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
    procedure V1Column11CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
    procedure TV1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure TV1Column1CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
  private

    { Private declarations }

    procedure InitGrid();
    procedure InitsubGrid();
  public
    fkhType: string;
    Fmanage: string;
    CANSHU1: string;
  end;

var
  frmOrderProgress2: TfrmOrderProgress2;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmOrderProgress2.InitGrid();
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('SELECT KG=(SELECT SUM(PRTOrderQty) FROM JYOrder_Sub b inner join jyorder_main a on a.mainid=b.mainid WHERE OrderUnit=''KG''  ');
    sql.Add(' and FILLTIME>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
    sql.Add(' and FILLTIME<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');

    SQL.Add('),M=(SELECT SUM(PRTOrderQty) FROM JYOrder_Sub b inner join jyorder_main a on a.mainid=b.mainid WHERE OrderUnit=''M'' ');
    sql.Add(' and FILLTIME>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
    sql.Add(' and FILLTIME<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
    SQL.Add('),Y=(SELECT SUM(PRTOrderQty) FROM JYOrder_Sub b inner join jyorder_main a on a.mainid=b.mainid WHERE OrderUnit=''Y'' ');
    sql.Add(' and FILLTIME>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
    sql.Add(' and FILLTIME<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
    SQL.Add(')');
    Open;
  end;
  Label3.Caption := ADOQueryTemp.fieldbyname('kg').AsString + '公斤  ' + ADOQueryTemp.fieldbyname('M').AsString + '米  ' + ADOQueryTemp.fieldbyname('Y').AsString + '码';
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' exec P_View_OrderProgress2 @Begdate=' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', Begdate.DateTime))));
      sql.Add(' ,@enddate=' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1))));
      if canshu1 = '查询' then
      begin
        sql.Add(',@canshu=' + QuotedStr(Trim(DName)));
      end;

      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
//    CDS_HZ.Last;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmOrderProgress2.InitsubGrid();
begin
  try
    ADOQueryTemp.DisableControls;
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add(' SELECT SUM(qty)PBQTY,SUM(isnull(PiQty,0))PBPS,ORDMainIdRK,FactoryName AS PBC');
      SQL.Add('FROM CK_SXPB_CR WHERE CKName=''坯布'' AND CRFlag=''入库''  ');
      sql.Add('and ORDMainIdRK=' + quotedstr(Trim(CDS_HZ.fieldbyname('MainId').AsString)));
      SQL.Add('GROUP BY ORDMainIdRK,FactoryName');
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_SUB);
    SInitCDSData20(ADOQueryTemp, CDS_SUB);
//    CDS_HZ.Last;
  finally
    ADOQueryTemp.EnableControls;
  end;
end;

procedure TfrmOrderProgress2.FormDestroy(Sender: TObject);
begin
  frmOrderProgress2 := nil;
end;

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

procedure TfrmOrderProgress2.TBCloseClick(Sender: TObject);
begin
  WriteCxBandedGrid(trim(self.caption), Tv1, '进度管理10');
  Close;
end;

procedure TfrmOrderProgress2.FormShow(Sender: TObject);
var
  fsj: string;
begin
  ReadCxBandedGrid(trim(self.caption), Tv1, '进度管理10');
  fsj := 'select distinct(CustomerNoName) Name from JYORDER_MAIN  order by CustomerNoName ';
  SInitComBoxBySql(ADOQueryCmd, CustomerNoName, False, fsj);
//  InitGrid();
end;

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

procedure TfrmOrderProgress2.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  end;
end;

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

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

procedure TfrmOrderProgress2.FormCreate(Sender: TObject);
begin
  canshu1 := Trim(DParameters1);
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 90;
end;

procedure TfrmOrderProgress2.ToolButton3Click(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID, FKHName, FFPNo: string;
  i: Integer;
begin
  ExportFtErpFile('跟进表.rmf', ADOQueryTemp);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CRJD_HZ ');
    sql.add('@MainId=' + quotedstr(Trim(CDS_HZ.fieldbyname('MainId').AsString)));
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDSPRTHZ);
  SInitCDSData20(ADOQueryTemp, CDSPRTHZ);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CRJD_CD ');
    sql.add('@MainId=' + quotedstr(Trim(CDS_HZ.fieldbyname('MainId').AsString)));
    Open;
  end;
  RMVariables['HZCD'] := ADOQueryTemp.fieldbyname('HZCD').Value;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CRJD ');
    sql.add('@MainId=' + quotedstr(Trim(CDS_HZ.fieldbyname('MainId').AsString)));
    sql.add(',@Falg=''明细''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDSPRTMX);
  SInitCDSData20(ADOQueryTemp, CDSPRTMX);

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\跟进表.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找到' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmOrderProgress2.cxGridDBBandedColumn64CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
begin
  if (ARow1.Values[V1Column8.Index] = ARow2.Values[V1Column8.Index]) then
    AAreEqual := True
  else
    AAreEqual := False;
end;

procedure TfrmOrderProgress2.V1Column2CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
begin
  if (ARow1.Values[V1Column9.Index] = ARow2.Values[V1Column9.Index]) then
    AAreEqual := True
  else
    AAreEqual := False;
end;

procedure TfrmOrderProgress2.V1Column4CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
begin
  if (ARow1.Values[V1Column9.Index] = ARow2.Values[V1Column9.Index]) and (ARow1.Values[V1Column4.Index] = ARow2.Values[V1Column4.Index]) then
    AAreEqual := True
  else
    AAreEqual := False;
end;

procedure TfrmOrderProgress2.V1Column3CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
begin
  if (ARow1.Values[V1Column8.Index] = ARow2.Values[V1Column8.Index]) and (ARow1.Values[V1Column3.Index] = ARow2.Values[V1Column3.Index]) then
    AAreEqual := True
  else
    AAreEqual := False;
end;

procedure TfrmOrderProgress2.cxGridDBBandedColumn59CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
begin
  if (ARow1.Values[V1Column9.Index] = ARow2.Values[V1Column9.Index]) and (ARow1.Values[cxGridDBBandedColumn57.Index] = ARow2.Values[cxGridDBBandedColumn57.Index]) then
    AAreEqual := True
  else
    AAreEqual := False;
end;

procedure TfrmOrderProgress2.V1Column11CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
begin
  if (ARow1.Values[V1Column9.Index] = ARow2.Values[V1Column9.Index]) and (ARow1.Values[V1Column11.Index] = ARow2.Values[V1Column11.Index]) and (ARow1.Values[TV1Column4.Index] = ARow2.Values[TV1Column4.Index]) then
    AAreEqual := True
  else
    AAreEqual := False;
end;

procedure TfrmOrderProgress2.TV1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if (AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('keyNO').Index]) mod 2 = 0 then
  begin
    ACanvas.Brush.Color := clSkyBlue;
  end;
//  else
//  begin
//    ACanvas.Brush.Color := $EEF5FF;
//  end;
end;

procedure TfrmOrderProgress2.TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitsubGrid();
end;

procedure TfrmOrderProgress2.TV1Column1CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
begin
  if (ARow1.Values[cxGridDBBandedColumn62.Index] = ARow2.Values[cxGridDBBandedColumn62.Index]) and (ARow1.Values[V1Column2.Index] = ARow2.Values[V1Column2.Index]) then
    AAreEqual := True
  else
    AAreEqual := False;
end;

end.

