unit U_CP_YDang_MTBJ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, ExtCtrls, StdCtrls, DBClient, DB, ADODB,
  cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, cxDBData, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxPC, cxCalendar, cxButtonEdit,
  RM_System, RM_Common, RM_Class, RM_GridReport, RM_Dataset, RM_e_Xls;

type
  TfrmCP_YDang_MTBJ = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    contract_Main: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    TV1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    ToolBar1: TToolBar;
    TINIT: TToolButton;
    Tglv: TToolButton;
    Tadd: TToolButton;
    Tupdate: TToolButton;
    Tdel: TToolButton;
    TSel: TToolButton;
    Tchk: TToolButton;
    TNochk: TToolButton;
    TClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxTabControl1: TcxTabControl;
    Label2: TLabel;
    CustomerNoName: TEdit;
    Label4: TLabel;
    ConPerson1: TEdit;
    V1PRTCodeName: TcxGridDBColumn;
    V1PRTSpec: TcxGridDBColumn;
    V1MFQty: TcxGridDBColumn;
    V1KZQty: TcxGridDBColumn;
    V1PRTOrderQty: TcxGridDBColumn;
    V1OrderUnit: TcxGridDBColumn;
    V1Note: TcxGridDBColumn;
    TPrint: TToolButton;
    RMReport: TRMGridReport;
    RMDBMAIN: TRMDBDataSet;
    order_print: TClientDataSet;
    Label3: TLabel;
    Label5: TLabel;
    PRTCodeName: TEdit;
    PRTSpec: TEdit;
    V1CustomerNoName: TcxGridDBColumn;
    V1ConPerson1: TcxGridDBColumn;
    V1PRTCF: TcxGridDBColumn;
    V1SOrdQty1: TcxGridDBColumn;
    V1BJtime: TcxGridDBColumn;
    V1SOrdQty5: TcxGridDBColumn;
    V1SOrdQty6: TcxGridDBColumn;
    V1SOrdQty7: TcxGridDBColumn;
    V1SOrdQty8: TcxGridDBColumn;
    V1SOrdQty9: TcxGridDBColumn;
    V1SOrdQty10: TcxGridDBColumn;
    V1SOrdQty11: TcxGridDBColumn;
    V1SOrdQty12: TcxGridDBColumn;
    V1SOrdQty13: TcxGridDBColumn;
    V1SOrdQty2: TcxGridDBColumn;
    V1BJPrice: TcxGridDBColumn;
    V1PriceUnit: TcxGridDBColumn;
    V1CBPrice: TcxGridDBColumn;
    V1PBSPEC: TcxGridDBColumn;
    V1PBMF: TcxGridDBColumn;
    V1PBKZ: TcxGridDBColumn;
    V1PRTCode: TcxGridDBColumn;
    V1BJXS: TcxGridDBColumn;
    V1CustomerNo: TcxGridDBColumn;
    Panel4: TPanel;
    Label14: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    Button1: TButton;
    RadioGroup1: TRadioGroup;
    V1QDLQty: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    Label6: TLabel;
    PRTCode: TEdit;
    procedure TSelClick(Sender: TObject);
    procedure TCloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TaddClick(Sender: TObject);
    procedure TupdateClick(Sender: TObject);
    procedure TdelClick(Sender: TObject);
    procedure TglvClick(Sender: TObject);
    procedure TINITClick(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure TNochkClick(Sender: TObject);
    procedure TV1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TPrintClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure PRTCodeKeyPress(Sender: TObject; var Key: Char);
    procedure CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
  private
    procedure INITDATa();
    procedure SetStatus();
    { Private declarations }
  public
    ftype,fDParameters1:string;
    { Public declarations }
  end;

var
  frmCP_YDang_MTBJ: TfrmCP_YDang_MTBJ;

implementation

uses U_Fun,U_DataLink, U_ZDYHelp, U_CP_YDang_BJ_Sub,U_CP_YDang_MTBJ_Sub;

{$R *.dfm}
procedure TfrmCP_YDang_MTBJ.SetStatus();
begin
  tchk.Visible:=false;
  tnochk.Visible:=false;
  Tupdate.Visible:=false;
  tdel.Visible:=false;
  V1BJPrice.Visible:=False;
  V1SOrdQty1.Visible:=False;
  V1CBPrice.Visible:=False;
  V1SOrdQty2.Visible:=False;
  V1SOrdQty5.Visible:=false;
  V1SOrdQty6.Visible:=false;
  V1SOrdQty7.Visible:=false;
  V1SOrdQty8.Visible:=false;
  V1SOrdQty9.Visible:=false;
  V1SOrdQty10.Visible:=false;
  V1SOrdQty11.Visible:=false;
  V1SOrdQty12.Visible:=false;
  V1SOrdQty13.Visible:=false;
  V1PBSPEC.Visible:=False;
  V1PBMF.Visible:=False;
  V1PBKZ.Visible:=False;
  V1BJXS.visible:=False;
  if Trim(fDParameters1)=''  then
  begin
    case cxTabControl1.TabIndex of
    0:begin
        Tupdate.Visible:=true;
        tdel.Visible:=true;
      end;
    1:begin
        V1BJPrice.Visible:=True;
      end;
    2:begin
        //V1BJPrice.Visible:=True;
      end;
    end;

  end;
  if Trim(fDParameters1)='高权限'  then
  begin
    cxTabControl1.Visible:=True;
    V1SOrdQty1.Visible:=True;
    V1SOrdQty5.Visible:=True;
    V1SOrdQty6.Visible:=True;
    V1SOrdQty7.Visible:=True;
    V1SOrdQty8.Visible:=True;
    V1SOrdQty9.Visible:=True;
    V1BJPrice.Visible:=True;
    V1SOrdQty10.Visible:=True;
    V1SOrdQty11.Visible:=True;
    V1SOrdQty12.Visible:=True;
    V1SOrdQty13.Visible:=True;
    V1CBPrice.Visible:=True;
    V1SOrdQty2.Visible:=True;
    V1PBSPEC.Visible:=True;
    V1PBMF.Visible:=True;
    V1PBKZ.Visible:=True;
    V1BJXS.visible:=True;
    case cxTabControl1.TabIndex of
    0:begin
       tchk.Visible:=true;
       Tupdate.Visible:=true;
       tdel.Visible:=true;

      end;
    1:begin
       tnochk.Visible:=true;

      end;
    2:begin
        
      end;
    end;
  end;
  {if Trim(fDParameters1)='查询'  then
  begin
    Tadd.Visible:=False;
    TPrint.visible:=False;
    V1SOrdQty5.Visible:=False;
    V1SOrdQty6.Visible:=False;
    V1SOrdQty7.Visible:=False;
    V1SOrdQty8.Visible:=False;
    V1SOrdQty9.Visible:=False;
    V1SOrdQty10.Visible:=False;
    V1SOrdQty11.Visible:=False;
    V1SOrdQty12.Visible:=False;
    V1SOrdQty13.Visible:=False;
  end;}
end;

procedure TfrmCP_YDang_MTBJ.INITDATa();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select a.*,b.* from CP_YDang_MTBJ_Main a ');
    sql.Add('inner join CP_YDang_MTBJ_Sub b on b.Mainid=a.Mainid ');
    sql.Add('where a.filltime>='''+formatdatetime('yyyy-MM-dd',begdate.datetime)+'''' );
    sql.Add('and a.filltime<'''+formatdatetime('yyyy-MM-dd',Enddate.datetime+1)+'''' );
    if Trim(fDParameters1)<>'高权限' then
    Begin
      sql.add('and A.ConPerson1='''+trim(DName)+''' ');
    end;
    if cxTabControl1.TabIndex=0 then
    sql.Add('and isnull(a.status,''0'')=''0'' ');
    if cxTabControl1.TabIndex=1 then
    sql.Add('and isnull(a.status,''0'')=''1'' ');
    open;
  end;
  SCreateCDS20(ADOQueryTmp,contract_Main);
  SInitCDSData20(ADOQueryTmp,contract_Main);
end;

procedure TfrmCP_YDang_MTBJ.TSelClick(Sender: TObject);
begin
  if contract_Main.IsEmpty then exit;
  try
    frmCP_YDang_MTBJ_Sub:=TfrmCP_YDang_MTBJ_Sub.create(self);
    with frmCP_YDang_MTBJ_Sub do
    begin
      formid:=2;
      FMainid:=trim(self.contract_Main.fieldbyname('Mainid').asstring);
      Tsave.Visible:=False;
      ToolBar2.Visible:=False;
      Panel1.Enabled:=False;
      TV1.OptionsData.Editing:=False;
      TV1.OptionsData.Deleting:=False;
      if showmodal=1 then
      begin
        INITDATa();
      end;
    end;
  finally
    frmCP_YDang_MTBJ_Sub.Free;
  end;
end;

procedure TfrmCP_YDang_MTBJ.TCloseClick(Sender: TObject);
begin
  close;
  WriteCxGrid(self.Caption+tv1.Name,Tv1);
end;

procedure TfrmCP_YDang_MTBJ.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alclient;
end;

procedure TfrmCP_YDang_MTBJ.FormDestroy(Sender: TObject);
begin
  frmCP_YDang_MTBJ:=nil;
end;

procedure TfrmCP_YDang_MTBJ.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  action:=cafree;
end;

procedure TfrmCP_YDang_MTBJ.TaddClick(Sender: TObject);
begin
  try
    frmCP_YDang_MTBJ_Sub:=TfrmCP_YDang_MTBJ_Sub.create(self);
    with frmCP_YDang_MTBJ_Sub do
    begin
      formid:=0;
      FMainid:='';
      if showmodal=1 then
      begin
        INITDATa();
      end;
    end;
  finally
    frmCP_YDang_MTBJ_Sub.Free;
  end;
end;

procedure TfrmCP_YDang_MTBJ.TupdateClick(Sender: TObject);
begin
  if contract_Main.IsEmpty then exit;
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.add('select Status from CP_YDang_MTBJ_Main ');
    sql.add(' where Mainid='''+trim(contract_Main.fieldbyname('Mainid').asstring)+''' ');
    open;
    if fieldbyname('status').asstring='2' then
    begin
      application.MessageBox('单据已审核','');
      exit;
    end;
  end;
  try
    frmCP_YDang_MTBJ_Sub:=TfrmCP_YDang_MTBJ_Sub.create(self);
    with frmCP_YDang_MTBJ_Sub do
    begin
      formid:=1;
      FMainid:=trim(self.contract_Main.fieldbyname('Mainid').asstring);
      if showmodal=1 then
      begin
        INITDATa();
      end;
    end;
  finally
    frmCP_YDang_MTBJ_Sub.Free;
  end;
end;

procedure TfrmCP_YDang_MTBJ.TdelClick(Sender: TObject);
begin
  if contract_Main.IsEmpty then exit;
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.add('select Status from CP_YDang_MTBJ_Main ');
    sql.add(' where Mainid='''+trim(contract_Main.fieldbyname('Mainid').asstring)+''' ');
    open;
    if fieldbyname('status').asstring='2' then
    begin
      application.MessageBox('单据已审核','');
      exit;
    end;
  end;
  if Application.MessageBox('确定要删除吗','提示',1)=2 then Exit;
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.add('delete from CP_YDang_MTBJ_Main where Mainid='''+trim(contract_Main.fieldbyname('Mainid').AsString)+'''');
    execsql;
  end;
  INITDATa();
end;

procedure TfrmCP_YDang_MTBJ.TglvClick(Sender: TObject);
begin
  SDofilter(ADOQueryTMP,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryTMP,CONTRACT_Main);
  SInitCDSData20(ADOQueryTMP,contract_Main);
end;

procedure TfrmCP_YDang_MTBJ.TINITClick(Sender: TObject);
begin
  INITDATa();
end;

procedure TfrmCP_YDang_MTBJ.TchkClick(Sender: TObject);
begin
  if contract_Main.IsEmpty then exit;
  try
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('update CP_YDang_MTBJ_Main set status=''1'',chktime=getdate(),chker='+quotedstr(trim(Dname)) );
      sql.Add(' where Mainid='''+trim(contract_Main.fieldbyname('Mainid').asstring)+'''');
      execsql;
    end;
    application.MessageBox('审核成功','提示');
    initdata();
  except
    application.messagebox('审核失败','提示');
  end;
end;

procedure TfrmCP_YDang_MTBJ.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption+tv1.Name,Tv1);
  enddate.datetime:=SGetServerDateTime(ADOQueryTmp);
  begdate.datetime:=enddate.datetime-7;
  SetStatus();
  initdata();
end;

procedure TfrmCP_YDang_MTBJ.CustomerNoNameChange(Sender: TObject);
begin
  SDofilter(ADOQueryTMP,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryTMP,CONTRACT_Main);
  SInitCDSData20(ADOQueryTMP,contract_Main);
end;

procedure TfrmCP_YDang_MTBJ.TNochkClick(Sender: TObject);
begin
  if contract_Main.IsEmpty then exit;
  try
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('update CP_YDang_MTBJ_Main set status=''0'',chktime=null,chker=null ');
      sql.Add(' where Mainid='''+trim(contract_Main.fieldbyname('Mainid').asstring)+'''');
      execsql;
    end;
    application.MessageBox('撤销成功','提示');
    initdata();
  except
    application.messagebox('撤销失败','提示');
  end;
end;

procedure TfrmCP_YDang_MTBJ.TV1CellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  Tsel.Click;
end;

procedure TfrmCP_YDang_MTBJ.TPrintClick(Sender: TObject);
begin
  if contract_Main.IsEmpty then exit;
  //Panel4.Visible:=True;
  Button1.Click;
end;

procedure TfrmCP_YDang_MTBJ.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  initdata();
end;

procedure TfrmCP_YDang_MTBJ.Button1Click(Sender: TObject);
begin
  try
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select a.*,b.*,TP.FilesOther from CP_YDang_MTBJ_Main a ');
      sql.Add('inner join CP_YDang_MTBJ_Sub b on b.Mainid=a.Mainid ');
      sql.add('left join TP_File TP on TP.WBID=B.SOrddefstr1 ');
      sql.Add('where a.Mainid='''+trim(contract_Main.fieldbyname('Mainid').AsString)+'''');
      open;
    end;
    SCreateCDS20(ADOQueryTmp,order_print);
    SInitCDSData20(ADOQueryTmp,order_print);
    RMReport.LoadFromFile(ExtractFilePath(Application.ExeName) + 'Report\样品报价1.rmf');
    RMReport.ShowReport;
  except
    application.MessageBox('显示报表错误，打印失败','提示');
    exit;
  end;
end;

procedure TfrmCP_YDang_MTBJ.Image2Click(Sender: TObject);
begin
  Panel4.Visible:=False;
end;

procedure TfrmCP_YDang_MTBJ.PRTCodeKeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13 then
  begin
    with ADOQueryTmp do
    begin
    close;
    sql.Clear;
    sql.Add('select a.*,b.* from CP_YDang_MTBJ_Main a ');
    sql.Add('inner join CP_YDang_MTBJ_Sub b on b.Mainid=a.Mainid ');
    sql.Add('where B.PRTCode like '''+'%'+Trim(PRTCode.Text)+'%'+''' ');
    if Trim(fDParameters1)<>'高权限' then
    Begin
      sql.add('and A.ConPerson1='''+trim(DName)+''' ');
      if cxTabControl1.TabIndex=0 then
      sql.Add('and a.status<>2 ');
    end
    else
    begin
      if cxTabControl1.TabIndex=0 then
      sql.Add('and a.status=1 ');
    end;
    if cxTabControl1.TabIndex=1 then
    sql.Add('and a.status=2 ');
    open;
    end;
  SCreateCDS20(ADOQueryTmp,contract_Main);
  SInitCDSData20(ADOQueryTmp,contract_Main);
  end;
end;

procedure TfrmCP_YDang_MTBJ.CustomerNoNameKeyPress(Sender: TObject;
  var Key: Char);
begin
  if key=#13 then
  begin
    with ADOQueryTmp do
    begin
    close;
    sql.Clear;
    sql.Add('select a.*,b.* from CP_YDang_MTBJ_Main a ');
    sql.Add('inner join CP_YDang_MTBJ_Sub b on b.Mainid=a.Mainid ');
    sql.Add('where A.CustomerNoName like '''+'%'+Trim(CustomerNoName.Text)+'%'+''' ');
    if Trim(fDParameters1)<>'高权限' then
    Begin
      sql.add('and A.ConPerson1='''+trim(DName)+''' ');
      if cxTabControl1.TabIndex=0 then
      sql.Add('and a.status<>2 ');
    end
    else
    begin
      if cxTabControl1.TabIndex=0 then
      sql.Add('and a.status=1 ');
    end;
    if cxTabControl1.TabIndex=1 then
    sql.Add('and a.status=2 ');
    open;
  end;
  SCreateCDS20(ADOQueryTmp,contract_Main);
  SInitCDSData20(ADOQueryTmp,contract_Main);
  end;
end;

end.
