unit U_ZHCPDJList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter, RM_Dataset, RM_System,
  RM_Common, RM_Class, RM_GridReport, RM_e_Xls, Menus, cxButtonEdit,
  cxDropDownEdit, cxPC, Buttons, cxTextEdit, cxImage, StrUtils, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxSkinsCore, dxSkinDarkRoom,
  dxSkinOffice2013White, dxSkinSharpPlus, dxSkinSpringTime,
  dxSkinsDefaultPainters, dxSkinscxPCPainter, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSilver, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinValentine, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue;

type
  TfrmZHCPDJList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    ToolButton1: TToolButton;
    Panel11: TPanel;
    v1Column1: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1ZICost: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    TBEdit: TMenuItem;
    TBDel: TMenuItem;
    Panel2: TPanel;
    v1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    BJ: TMenuItem;
    Panel5: TPanel;
    Label3: TLabel;
    Label4: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Panel1: TPanel;
    Label2: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label5: TLabel;
    KHName: TEdit;
    GuoJia: TEdit;
    YWY: TEdit;
    Note: TEdit;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Label1: TLabel;
    LiDan: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure cxTabControl2Change(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure BJClick(Sender: TObject);
    procedure KHNameChange(Sender: TObject);
  private
    canshu1, canshu2: string;
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    function StrFenHang(FStr: string; var FHStr: string): Boolean;
    { Private declarations }
  public
    FFInt, FCloth: Integer;

    { Public declarations }
  end;

//var
  //frmZHCPList: TfrmZHCPList;

implementation

uses
  U_DataLink, U_RTFun, U_ZHDJInPut, U_KWView;

{$R *.dfm}

procedure TfrmZHCPDJList.FormDestroy(Sender: TObject);
begin
  //frmZHCPList:=nil;
end;

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

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

procedure TfrmZHCPDJList.TBCloseClick(Sender: TObject);
begin
  if Trim(Self.Caption) = '展会登记*' then
  begin
    WriteCxGrid('展会登记YX', Tv1, '展会产品管理');
  end
  else if Trim(Self.Caption) = '展会报价*' then
  begin
    WriteCxGrid('展会报价YX', Tv1, '展会产品管理');
  end
  else if Trim(Self.Caption) = '展会登记(高权限)*' then
  begin
    WriteCxGrid('展会登记(高权限)YX', Tv1, '展会产品管理');
  end;
  Close;
end;

procedure TfrmZHCPDJList.InitGrid();
begin
  Panel2.Visible := True;
  Panel2.Refresh;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      sql.Add(' from ZH_KH_DJ A  ');
      sql.Add(' where Valid=''Y''  ');
      sql.Add(' and ZDDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
      sql.Add(' and ZDDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
      if Trim(canshu2) <> '高权限' then
      begin
        SQL.Add(' and (isnull(YWY,'''')=''' + Trim(DName) + ''' or isnull(LiDan,'''')=''' + Trim(DName) + ''' or isnull(Filler,'''')=''' + Trim(DName) + ''')');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  Panel2.Visible := False;
end;

procedure TfrmZHCPDJList.InitForm();
begin
  BegDate.Date := SGetServerDateMBeg(ADOQueryTemp);
  EndDate.Date := SGetServerDate(ADOQueryTemp);
  if Trim(Self.Caption) = '报价登记*' then
  begin
    ReadCxGrid('展会登记YX', Tv1, '展会产品管理');
    TBAdd.Visible := True;
    TBEdit.Visible := True;
   // TBDel.Visible:=True;
  end
  else if Trim(Self.Caption) = '报价管理*' then
  begin
    ReadCxGrid('展会报价YX', Tv1, '展会产品管理');
    TBAdd.Visible := True;
    TBEdit.Visible := True;
    TBDel.Visible := True;
    BJ.Visible := True;
    canshu2 := '高权限';
  end
  else if Trim(Self.Caption) = '报价登记(高权限)*' then
  begin
    ReadCxGrid('展会登记(高权限)YX', Tv1, '展会产品管理');
    TBAdd.Visible := True;
    TBEdit.Visible := True;
    TBDel.Visible := True;
    canshu2 := '高权限';
  end;
  //InitGrid();
end;

procedure TfrmZHCPDJList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

function TfrmZHCPDJList.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      //First;
     // while Order_Main.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' UPdate ZH_KH_DJ Set DelTime=getdate(),DelerCode=''' + Trim(DCode) + ''',Deler=''' + Trim(DName) + ''', Valid=''N'' ');
          sql.Add(' where ZDID=''' + Trim(Order_Main.fieldbyname('ZDID').AsString) + '''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;

    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

function TfrmZHCPDJList.StrFenHang(FStr: string; var FHStr: string): Boolean;
var
  i, j: Integer;
  LSStr: string;
begin
  Result := False;
  FStr := Trim(FStr);
  FHStr := '';
  j := 1;
  i := Pos(' ', FStr);
  while i > 0 do
  begin
    if j = 1 then
    begin
      LSStr := Copy(FStr, 1, i - 1);  //'1234  5678 456'
      FHStr := '1:' + LSStr;
    end
    else
    begin
      LSStr := Copy(FStr, 1, i - 1);
      if Trim(LSStr) = '' then
      begin
        FStr := Copy(FStr, i + 1, Length(FStr) - i);
      end
      else
        FHStr := FHStr + #13 + #10 + inttostr(j) + ':' + LSStr;
    end;
    if Trim(LSStr) <> '' then
    begin
      j := j + 1;
      FStr := Copy(FStr, i + 1, Length(FStr) - i);
    end;
    i := Pos(' ', FStr);
  end;
  if j = 1 then
  begin
    FHStr := FStr;
  end
  else
  begin
    if Trim(FStr) <> '' then
    begin
      FHStr := FHStr + #13 + #10 + inttostr(j) + ':' + FStr;
    end;
  end;
  Result := True;
end;

procedure TfrmZHCPDJList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(Self.Caption, cxGrid1);
end;

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

procedure TfrmZHCPDJList.TBAddClick(Sender: TObject);
begin
  try
    frmZHDJInPut := TfrmZHDJInPut.Create(Application);
    with frmZHDJInPut do
    begin
      PState := 0;
      FMainId := '';
      frmZHDJInPut.canshu2 := Self.canshu2;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmZHDJInPut.Free;
  end;
end;

procedure TfrmZHCPDJList.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmZHCPDJList.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmZHCPDJList.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmZHCPDJList.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmZHDJInPut := TfrmZHDJInPut.Create(Application);
    with frmZHDJInPut do
    begin
      PState := 1;
      FMainId := Trim(Order_Main.fieldbyname('ZDID').AsString);
      TBSave.Visible := False;
      if Trim(self.Caption) = '展会报价*' then
      begin
        frmZHDJInPut.canshu1 := '报价';
      end;
      if ShowModal = 1 then
      begin
        //InitGrid();
      end;
    end;
  finally
    frmZHDJInPut.Free;
  end;
end;

procedure TfrmZHCPDJList.CustomerNoNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

procedure TfrmZHCPDJList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if DelData() then
  begin
    //Order_Main.Delete;
  end;
end;

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

procedure TfrmZHCPDJList.ZJStatusChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmZHCPDJList.Tv1DblClick(Sender: TObject);
begin
  ToolButton1.Click;
end;

procedure TfrmZHCPDJList.cxTabControl2Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmZHCPDJList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmZHDJInPut := TfrmZHDJInPut.Create(Application);
    with frmZHDJInPut do
    begin
      PState := 1;
      FMainId := Trim(Order_Main.fieldbyname('ZDID').AsString);
      frmZHDJInPut.canshu2 := Self.canshu2;
      if ShowModal = 1 then
      begin
        InitGrid();
        Self.Order_Main.Locate('ZDID', FMainId, []);
      end;
    end;
  finally
    frmZHDJInPut.Free;
  end;
end;

procedure TfrmZHCPDJList.BJClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmZHDJInPut := TfrmZHDJInPut.Create(Application);
    with frmZHDJInPut do
    begin
      PState := 1;
      FMainId := Trim(Order_Main.fieldbyname('ZDID').AsString);
      frmZHDJInPut.canshu1 := '报价';
      if ShowModal = 1 then
      begin
        InitGrid();
        Self.Order_Main.Locate('ZDID', FMainId, []);
      end;
    end;
  finally
    frmZHDJInPut.Free;
  end;
end;

procedure TfrmZHCPDJList.KHNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

end.

