unit U_YpBaoJiaList;

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, cxCalendar, cxButtonEdit, cxSplitter,
  RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport,
  cxTextEdit, cxPC;

type
  TfrmYpBaoJiaList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Label2: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label5: TLabel;
    BjName: TEdit;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1FactoryNo1Name: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1PRTQty: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column3: TcxGridDBColumn;
    CustomerName: TEdit;
    Label6: TLabel;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    ToolButton2: TToolButton;
    cxTabControl1: TcxTabControl;
    ToolButton1: TToolButton;
    ToolButton3: TToolButton;
    v1Column4: TcxGridDBColumn;
    ToolButton4: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    FInt,PFInt:Integer;
    procedure InitGrid();
    procedure InitForm();
    procedure SetStatus();
    function  DelData():Boolean;
    { Private declarations }
  public
    CanShu1:string;
    { Public declarations }
  end;

var
  frmYpBaoJiaList: TfrmYpBaoJiaList;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp
  ,U_YpBaoJiaInput,U_CPManageFMSel;

{$R *.dfm}
procedure TfrmYpBaoJiaList.SetStatus();
begin
  ToolButton1.Visible:=False;
  ToolButton3.Visible:=False;
  TBEdit.Visible:=False;
  TBDel.Visible:=False;
  case cxTabControl1.TabIndex of
  0:begin
      if Trim(CanShu1)='高权限' then
      begin
          TBEdit.Visible:=True;
          TBDel.Visible:=True;
          ToolButton1.Visible:=True;
      end
      else
      begin
         TBEdit.Visible:=True;
         TBDel.Visible:=True;
      end;
    end;
  1:begin
      ToolButton3.Visible:=True;
    end;
  2:begin

    end;
  end;
end;  

procedure TfrmYpBaoJiaList.FormDestroy(Sender: TObject);
begin
  frmYpBaoJiaList:=nil;
end;

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

procedure TfrmYpBaoJiaList.TBCloseClick(Sender: TObject);
begin

  ReadCxGrid(self.Caption,Tv1,'样品报价管理');
  Close;
end;

procedure TfrmYpBaoJiaList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.* ');
      sql.Add('from BaoJiaMain A ');
      sql.Add('where  A.FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
      sql.Add('and A.FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''');
      if Trim(CanShu1)<>'高权限' then
      begin
        sql.Add('and A.Filler='+quotedstr(DName));
      end;
      case cxTabControl1.TabIndex of
      0:begin
          sql.Add('and isnull(A.status,0)=''0'' ');
        end;
      1:begin
          sql.Add('and isnull(A.status,0)=''1'' ');
        end;
      2:begin
         
        end;
      end;
//      showmessage(sql.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYpBaoJiaList.InitForm();
begin
  ReadCxGrid(self.Caption,Tv1,'样品报价管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  InitGrid();
  SetStatus();
end;

procedure TfrmYpBaoJiaList.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;

procedure TfrmYpBaoJiaList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.fieldbyname('filler').AsString)<>Trim(DName) then Exit;
  try
    frmYpBaoJiaInput:=TfrmYpBaoJiaInput.Create(Application);
    with frmYpBaoJiaInput do
    begin
      WindowState:=wsnormal;
      PState:=1;
      BjCode.Enabled:=false;
      FMainId:=Trim(Self.Order_Main.fieldbyname('BJMainId').AsString);
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmYpBaoJiaInput.Free;
  end;

end;

procedure TfrmYpBaoJiaList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.fieldbyname('filler').AsString)<>Trim(DName) then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete BaoJiaSub where BJMainId='''+Trim(Order_Main.fieldbyname('BJMainId').AsString)+'''');
      sql.Add('delete BaoJiaMain where BJMainId='''+Trim(Order_Main.fieldbyname('BJMainId').AsString)+'''');
      ExecSQL;
    end;
   InitGrid();
end;

function TfrmYpBaoJiaList.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete ContractSX_Sub where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from ContractSX_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete ContractSX_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;
    if Trim(Order_Main.fieldbyname('SubId').AsString)='' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete ContractSX_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmYpBaoJiaList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel('坯布合同订单列表',cxGrid1);
end;

procedure TfrmYpBaoJiaList.TBPrintClick(Sender: TObject);
var
   fPrintFile,FConNoM:string;

begin
   if Order_Main.IsEmpty then Exit;

    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\样品报价单.rmf' ;


  with ADOQueryTemp do
  begin
    Filtered:=False;
    Close;
    sql.Clear;
    sql.Add('select A.*,B.*  ');
    sql.Add('from BaoJiaSub A');
    sql.Add('inner join BaoJiaMain B on B.BJMainId=A.BJMainId');
    sql.Add('where  B.BJMainId='''+Trim(Order_Main.fieldbyname('BJMainId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);

  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找'+trim(fPrintFile)),'提示',0);
  end;

end;

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

procedure TfrmYpBaoJiaList.TBAddClick(Sender: TObject);
begin
    try
      frmYpBaoJiaInput:=TfrmYpBaoJiaInput.Create(Application);
      with frmYpBaoJiaInput do
      begin
        WindowState:=wsnormal;
        PState:=0;
        FMainId:='';
        if ShowModal=1 then
        begin
          InitGrid();
        end;
      end;
    finally
      frmYpBaoJiaInput.Free;
    end;
end;

procedure TfrmYpBaoJiaList.ConNoMChange(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 TfrmYpBaoJiaList.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmYpBaoJiaList.cxPageControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmYpBaoJiaList.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmYpBaoJiaInput:=TfrmYpBaoJiaInput.Create(Application);
    with frmYpBaoJiaInput do
    begin
      WindowState:=wsnormal;
      PState:=2;
      ToolBar1.Visible:=false;
      FMainId:=Trim(Self.Order_Main.fieldbyname('BJMainId').AsString);
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmYpBaoJiaInput.Free;
  end;
end;

procedure TfrmYpBaoJiaList.ToolButton1Click(Sender: TObject);
begin
  IF Order_Main.IsEmpty then exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update BaoJiaMain SET status=''1'' ');
      sql.Add(' ,Chker='''+Trim(DName)+'''');
      sql.Add(' ,Chktime=GETDATE()');
      sql.Add(' where BJMainId ='+quotedstr(trim(Order_Main.fieldbyname('BJMainId').AsString)));
      execsql;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！','提示信息',0);
  end;
end;

procedure TfrmYpBaoJiaList.ToolButton3Click(Sender: TObject);
begin
  IF Order_Main.IsEmpty then exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update BaoJiaMain SET status=''0'' ,Chker='''' ,Chktime=null');
      sql.Add(' where BJMainId ='+quotedstr(trim(Order_Main.fieldbyname('BJMainId').AsString)));
      execsql;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！','提示信息',0);
  end;
end;

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

procedure TfrmYpBaoJiaList.Tv1DblClick(Sender: TObject);
begin
ToolButton2.Click;
end;

procedure TfrmYpBaoJiaList.ToolButton4Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.fieldbyname('filler').AsString)<>Trim(DName) then Exit;
  try
    frmYpBaoJiaInput:=TfrmYpBaoJiaInput.Create(Application);
    with frmYpBaoJiaInput do
    begin
      WindowState:=wsnormal;
      PState:=1;
      PCopyInt:=1;
      BjCode.Enabled:=false;
      FMainId:=Trim(Self.Order_Main.fieldbyname('BJMainId').AsString);
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmYpBaoJiaInput.Free;
  end;

end;

end.
