unit U_FanYangListHZ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxCalendar, RM_Dataset, RM_System,
  RM_Common, RM_Class, RM_GridReport, DBClient, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, StdCtrls, ComCtrls, ExtCtrls,
  ToolWin, cxPC, cxGridBandedTableView, cxGridDBBandedTableView,
  cxCustomPivotGrid, cxDBPivotGrid, cxContainer, cxTextEdit, cxCurrencyEdit,
  cxCheckBox, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu;

type
  TFrmFanYangListHZ = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    ToolButton1: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    OrderNoM: TEdit;
    CustomerNo: TEdit;
    MPRTCodeName: TEdit;
    ConNo: TEdit;
    MPRTKuanNO: TEdit;
    MPRTCode: TEdit;
    MPRTKZ: TEdit;
    MPRTMF: TEdit;
    cxGrid1: TcxGrid;
    cxGrid1Level1: TcxGridLevel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    TChk: TToolButton;
    TNoChk: TToolButton;
    cxTabControl1: TcxTabControl;
    TV1: TcxGridDBBandedTableView;
    VOrdDate: TcxGridDBBandedColumn;
    V1OrdDefStr1: TcxGridDBBandedColumn;
    V1Mainid: TcxGridDBBandedColumn;
    V1MPRTCodeName: TcxGridDBBandedColumn;
    V1MPRTCode: TcxGridDBBandedColumn;
    V1PRTColor: TcxGridDBBandedColumn;
    V1PRTOrderQty: TcxGridDBBandedColumn;
    V1OrdQty4: TcxGridDBBandedColumn;
    V1SOrdQty1: TcxGridDBBandedColumn;
    V1MLF: TcxGridDBBandedColumn;
    V1OrdQty5: TcxGridDBBandedColumn;
    V1OrdQty2: TcxGridDBBandedColumn;
    V1OrdQty1: TcxGridDBBandedColumn;
    V1ZWF: TcxGridDBBandedColumn;
    V1OrdQty3: TcxGridDBBandedColumn;
    V1SMPYF: TcxGridDBBandedColumn;
    V1ZJ: TcxGridDBBandedColumn;
    V1Note: TcxGridDBBandedColumn;
    V1OrderUnit: TcxGridDBBandedColumn;
    V1OrdQty6: TcxGridDBBandedColumn;
    V1SOrdQty2: TcxGridDBBandedColumn;
    V1OrdDefStr6: TcxGridDBBandedColumn;
    OrdDefStr1: TEdit;
    Label2: TLabel;
    V1CustomerNoName: TcxGridDBBandedColumn;
    V1OrdQty7: TcxGridDBBandedColumn;
    PanelSh: TPanel;
    Label5: TLabel;
    Orddefdate1: TDateTimePicker;
    Label6: TLabel;
    ordQty9: TcxCurrencyEdit;
    Label7: TLabel;
    OrddefNote1: TMemo;
    Button1: TButton;
    Button2: TButton;
    V1Orddefdate1: TcxGridDBBandedColumn;
    V1OrdQty9: TcxGridDBBandedColumn;
    V1OrddefNote1: TcxGridDBBandedColumn;
    ToolButton2: TToolButton;
    V1conNo: TcxGridDBBandedColumn;
    V1orddefstr7: TcxGridDBBandedColumn;
    V1Column1: TcxGridDBBandedColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure OrderNoMChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TChkClick(Sender: TObject);
    procedure TNoChkClick(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TBPrintClick(Sender: TObject);
    procedure VOrdDateCompareRowValuesForCellMerging(Sender: TcxGridColumn;
      ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties;
      const AValue1: Variant; ARow2: TcxGridDataRow;
      AProperties2: TcxCustomEditProperties; const AValue2: Variant;
      var AAreEqual: Boolean);
    procedure V1PRTColorCompareRowValuesForCellMerging(
      Sender: TcxGridColumn; ARow1: TcxGridDataRow;
      AProperties1: TcxCustomEditProperties; const AValue1: Variant;
      ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties;
      const AValue2: Variant; var AAreEqual: Boolean);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  private
    procedure initGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmFanYangListHZ: TFrmFanYangListHZ;

implementation

uses U_DataLink,U_FanYangList_Sub,U_Fun;

{$R *.dfm}

procedure TFrmFanYangListHZ.initGrid();
begin
  with ADOQueryTmp do
  begin
    Filtered:=False;
    close;
    sql.Clear;
    sql.add('exec P_Get_FYList ');
    sql.add('@begdate='''+formatdatetime('yyyy-MM-dd',begdate.DateTime)+''' ');
    sql.Add(',@endDate='''+formatdatetime('yyyy-MM-dd',endDate.DateTime+1)+''' ');
    open;
  end;
  SCreateCDS20(ADOQueryTmp,Order_Main);
  SInitCDSData20(ADOQueryTmp,Order_Main);
  TBFind.Click;
end;

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

procedure TFrmFanYangListHZ.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
end;

procedure TFrmFanYangListHZ.FormDestroy(Sender: TObject);
begin
  FrmFanYangListHZ:=nil;
end;

procedure TFrmFanYangListHZ.FormShow(Sender: TObject);
begin
  ReadCxBandedGrid(self.Caption+Tv1.Name+'5',Tv1,'放样指示单统计');
  BegDate.DateTime:=SGetServerDate10(ADOQueryPrint)-15;
  EndDate.DateTime:=SGetServerDate10(ADOQueryPrint);
  initGrid();
end;

procedure TFrmFanYangListHZ.TBRafreshClick(Sender: TObject);
begin
  initGrid();
end;

procedure TFrmFanYangListHZ.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxBandedGrid(self.Caption+Tv1.Name+'5',Tv1,'放样指示单统计');
end;

procedure TFrmFanYangListHZ.TBFindClick(Sender: TObject);
var strwhere: string;
begin
  if ADOQueryTmp.Active=False then Exit;
  strwhere:=SGetFilters(Panel1,1,2);
  IF strwhere='' then
  begin
    if cxTabControl1.TabIndex<2 then
    strwhere:=' status ='''+inttostr(cxTabControl1.TabIndex)+'''';
  end
  else
  begin
    if cxTabControl1.TabIndex<2 then
    strwhere:=strwhere+' and status ='''+inttostr(cxTabControl1.TabIndex)+'''';
  end;
  SDofilter(ADOQueryTmp,strwhere);
  SCreateCDS20(ADOQueryTmp,Order_Main);
  SInitCDSData20(ADOQueryTmp,Order_Main);
end;

procedure TFrmFanYangListHZ.OrderNoMChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TFrmFanYangListHZ.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    FrmFanYangListSub:=TFrmFanYangListSub.Create(Application);
    with FrmFanYangListSub do
    begin
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      ToolBar2.Visible:=False;
      TBSave.Visible:=False;
      Panel1.Enabled:=False;
      if ShowModal=1 then
      begin
        initGrid();
      end;
    end;
  finally
    FrmFanYangListSub.Free;
  end;
end;

procedure TFrmFanYangListHZ.cxTabControl1Change(Sender: TObject);
begin
  Tchk.Visible:=False;
  TNochk.Visible:=False;
  if CxTabControl1.TabIndex=0 then
  begin
    Tchk.Visible:=True;
  end;
  if CxTabControl1.TabIndex=1 then
  begin
    TNochk.Visible:=true;
  end;
  initGrid();
end;

procedure TFrmFanYangListHZ.TChkClick(Sender: TObject);
begin
  if order_Main.IsEmpty then exit;

  if order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据','提示',0);
    Exit;
  end;
  PanelSh.Left:=(Width-PanelSh.Width) div 2;
  PanelSh.top:=(Height-PanelSh.Height-100) div 2;
  Orddefdate1.DateTime:=date();
  ordQty9.Text:='';
  OrddefNote1.Text:='';
  PanelSh.Visible:=true;

end;

procedure TFrmFanYangListHZ.TNoChkClick(Sender: TObject);
begin
  if order_Main.IsEmpty then exit;
  if order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据','提示',0);
    Exit;
  end;
  try
    with Order_Main do
    begin
      DisableControls;
      first;
      while not eof do
      begin
        IF fieldbyName('Ssel').AsBoolean then
        begin
          with ADOQueryTmp do
          begin
            close;
            sql.Clear;
            sql.add('update jYorderFY_Main set Chker='''',Chktime=Null,status=''0'' ');
            sql.Add(',Orddefdate1=null,ordQty9=0,OrddefNote1='''' ');
            sql.add('where Mainid='''+trim(Order_Main.fieldbyname('Mainid').asstring)+''' ');
            execsql;
          end;
        end;
        next;
      end;

      EnableControls;
    end;


    application.MessageBox('撤销成功！','提示信息');
    initGrid();
  except
   application.MessageBox('撤销失败！','提示信息',0);
  end;
end;

procedure TFrmFanYangListHZ.Tv1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  ToolButton1.Click;
end;

procedure TFrmFanYangListHZ.TBPrintClick(Sender: TObject);
var
  fPrintFile:string;
begin
  if order_Main.IsEmpty then exit;
  {with ADOQueryPrint do
  begin
    close;
    sql.clear;
    sql.Add('select A.*,B.*,');
    //Sql.add('CLRGS=(select count(AA.PRTColor) from (select distinct D.PRTColor from JYOrderFy_Sub D where D.Mainid=A.Mainid ) AA),');
    //sql.add('SumOrdQty1=(select Sum(D.SOrdQty1) from JYOrderFY_Sub D where D.Mainid=A.Mainid ),');
    //sql.add('ZJ=(select C.OrdQty5*Count(D.PRTColor)+C.OrdQty1*C.OrdQty2+C.OrdQty3*Sum(D.SOrdQty1)+C.OrdQty4*Sum(D.SOrdQty1)');
    //sql.add(' from JYOrderFy_Main C inner join JYOrderFy_Sub D on D.Mainid=C.Mainid where C.Mainid=A.Mainid ');
    //sql.add('group by C.OrdQty5,C.OrdQty1,C.OrdQty2,C.OrdQty3,C.OrdQty4 ),');
    sql.add('ZJ=(select (AA.OrdQty5*Count(AA.PRTColor)+AA.OrdQty1*AA.OrdQty2+AA.OrdQty3*Sum(SL)+AA.OrdQty4*Sum(SL)) from ');
    sql.add('(select E.PRTColor,F.OrdQty5,F.OrdQty1,F.OrdQty2,F.OrdQty3,F.OrdQty4,Sum(E.SOrdQty1) SL From JYOrderFy_Sub E inner join ');
    sql.add('JYOrderFy_Main F on F.Mainid=E.Mainid where F.Mainid=A.Mainid group by E.PRTColor,F.OrdQty5,F.OrdQty1,F.OrdQty2,F.OrdQty3,F.OrdQty4 ) AA ');
    sql.add('group by AA.OrdQty5,AA.OrdQty1,AA.OrdQty2,AA.OrdQty3,AA.OrdQty4) ,');

    sql.add('MLF=SOrdQty1*OrdQty4,ZWF=OrdQty2*OrdQty1,SMPYF=OrdQty3*SOrdQty1 ');
    sql.add('from JYOrderFy_Main A ');
    sql.add('inner join JYOrderFy_Sub B on B.Mainid=A.Mainid ');
    sql.Add('where A.Mainid='''+trim(Order_Main.fieldbyname('Mainid').asstring)+''' ');
    open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);}
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\放样指示单统计.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\放样指示单统计.rmf'),'提示',0);
  end;
end;

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

procedure TFrmFanYangListHZ.V1PRTColorCompareRowValuesForCellMerging(
  Sender: TcxGridColumn; ARow1: TcxGridDataRow;
  AProperties1: TcxCustomEditProperties; const AValue1: Variant;
  ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties;
  const AValue2: Variant; var AAreEqual: Boolean);
begin
  if (ARow1.Values[V1Mainid.Index]=ARow2.Values[V1Mainid.Index]) and
     (ARow1.Values[V1PRTColor.Index]=ARow2.Values[V1PRTColor.Index]) then
  begin
    AAreEqual:=true
  end
  else
    AAreEqual:=False;
end;

procedure TFrmFanYangListHZ.Button1Click(Sender: TObject);
begin
  try
    with Order_Main do
    begin
      DisableControls;
      first;
      while not eof do
      begin
        IF fieldbyName('Ssel').AsBoolean then
        begin
          with ADOQueryTmp do
          begin
            close;
            sql.Clear;
            sql.add(' update jYorderFY_Main set Chker='''+Trim(DName)+''',Chktime=getdate(),status=''1'' ');
            IF strTofloatdef(trim(OrdQty9.Text),0)<>0 then
            begin
              sql.Add(',Orddefdate1='''+formatdateTime('yyyy-MM-dd',Orddefdate1.DateTime)+''' ');
              sql.Add(',OrdQty9='''+floattostr(strTofloatdef(trim(OrdQty9.Text),0))+''' ');
              sql.Add(',OrddefNote1='''+trim(OrddefNote1.Text)+''' ');
            end;
            sql.add(' where Mainid='''+trim(Order_Main.fieldbyname('Mainid').asstring)+''' ');
            execsql;
          end;
        end;
        next;
      end;

      EnableControls;
    end;

    PanelSh.Visible:=false;
    application.MessageBox('审核成功！','提示信息');
    initGrid();
  except
   application.MessageBox('审核失败！','提示信息',0);
  end;
end;

procedure TFrmFanYangListHZ.Button2Click(Sender: TObject);
begin
  PanelSh.Visible:=false;
end;

procedure TFrmFanYangListHZ.ToolButton2Click(Sender: TObject);
begin
  IF Order_Main.IsEmpty then exit;
  TcxGridToExcel(self.Caption,CXGrid1);
end;

end.
