unit U_frmFabList;

interface

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

type
  TfrmFabList = class(TForm)
    ToolBar1: TToolBar;
    Tadd: TToolButton;
    Tupdate: TToolButton;
    Tdel: TToolButton;
    TClose: TToolButton;
    TSel: TToolButton;
    TINIT: TToolButton;
    TV1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    TVSQNo: TcxGridDBColumn;
    TVSQDate: TcxGridDBColumn;
    TVSQName: TcxGridDBColumn;
    TVNote: TcxGridDBColumn;
    TVC_CodeName: TcxGridDBColumn;
    TVC_Spec: TcxGridDBColumn;
    TVMFQty: TcxGridDBColumn;
    TVKZQty: TcxGridDBColumn;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DataSource1: TDataSource;
    ADOQueryTmp: TADOQuery;
    ADOQueryDel: TADOQuery;
    Panel1: TPanel;
    cxTabControl1: TcxTabControl;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Tchk: TToolButton;
    TNochk: TToolButton;
    cds_Main: TClientDataSet;
    Label2: TLabel;
    PONo: TEdit;
    Label3: TLabel;
    ConNo: TEdit;
    Label4: TLabel;
    P_CodeName: TEdit;
    Label5: TLabel;
    P_Spec: TEdit;
    Label6: TLabel;
    Label7: TLabel;
    Tglv: TToolButton;
    TVchktime: TcxGridDBColumn;
    TVchker: TcxGridDBColumn;
    P_MF: TEdit;
    P_KZ: TEdit;
    V1ConNo: TcxGridDBColumn;
    V1PONo: TcxGridDBColumn;
    V1Column1: TcxGridDBColumn;
    TPrint: TToolButton;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    RMDBMain: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    RMDBsub: TRMDBDataSet;
    V1Column2: TcxGridDBColumn;
    V1Column4: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    V1Column6: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    TVC_Note: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DataSource2: TDataSource;
    ADOQuerySub: TADOQuery;
    cxSplitter1: TcxSplitter;
    TBCopy: TToolButton;
    TV1Column1: TcxGridDBColumn;
    procedure FormCreate(Sender: TObject);
    procedure TCloseClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TaddClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TSelClick(Sender: TObject);
    procedure TupdateClick(Sender: TObject);
    procedure TINITClick(Sender: TObject);
    procedure TV1DblClick(Sender: TObject);
    procedure TdelClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TNochkClick(Sender: TObject);
    procedure PONoChange(Sender: TObject);
    procedure TglvClick(Sender: TObject);
    procedure TPrintClick(Sender: TObject);
    procedure TV1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TBCopyClick(Sender: TObject);
  private
    procedure initData();
    procedure setStatus();
    procedure InitSub();
    { Private declarations }
  public
    Ftype: string;
    { Public declarations }
  end;

var
  frmFabList: TfrmFabList;

implementation

uses U_DataLink,U_Fun,U_frmFabSQ;

{$R *.dfm}
procedure TfrmFabList.InitSub();
begin
  ADOQuerySub.Close;
  IF cds_Main.IsEmpty then exit;
  with ADOQuerySub do
  begin
    close;
    sql.Clear;
    sql.Add('select * from Contract_Sub_Sh ');
    sql.Add('where mainID ='+quotedstr((cds_Main.fieldbyname('mainID').AsString)));
    open;
  end;
end;

procedure TfrmFabList.setStatus();
begin
  Tupdate.Enabled:=False;
  Tdel.Enabled:=False;
  Tnochk.Visible:=False;
  Tchk.Visible:=False;
  if trim(Ftype)='高权限' then
  begin
    if cxTabControl1.TabIndex=0 then
    begin
      Tupdate.Enabled:=true;
      Tdel.Enabled:=true;
      Tchk.Visible:=true;
    end;
    if cxTabControl1.TabIndex=1 then
    begin
      Tnochk.Visible:=true;
    end;
  end
  else
  begin
    if cxTabControl1.TabIndex=0 then
    begin
      Tupdate.Enabled:=true;
      Tdel.Enabled:=true;
    end;
  end;
end;

procedure TfrmFabList.initData();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.add('from contract_main_Sh A ');
    sql.Add('where A.SQDate>='''+formatdatetime('yyyy-MM-dd',begdate.datetime)+''' ' );
    sql.Add('and A.SQDate<'''+formatdatetime('yyyy-MM-dd',Enddate.datetime+1)+''' ' );
    if cxtabControl1.TabIndex=0 then
    sql.Add('and A.ChkStatus=''0'' ');
    if CxtabControl1.TabIndex=1 then
    sql.Add('and A.ChkStatus=''1'' ');
    if trim(Ftype)<>'高权限' then
    begin
      sql.Add('and A.SQName='''+trim(DName)+'''');
    end;
    open;
  end;
  SCreateCDS20(ADOQueryTmp,cds_Main);
  SInitCDSData20(ADOQueryTmp,cds_Main);
end;

procedure TfrmFabList.FormCreate(Sender: TObject);
begin
  cxGrid1.Align:=alclient;
end;

procedure TfrmFabList.TCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmFabList.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  writeCxGrid(self.Caption+tv1.Name,Tv1);
  action:=cafree;
end;

procedure TfrmFabList.TaddClick(Sender: TObject);
begin
  try
    frmFabSQ:=TfrmFabSQ.create(self);
    with frmFabSQ do
    begin
      formid:=0;
      FMainId:='';
      if showmodal=1 then
      begin
        initData();
      end;
    end;
  finally
    frmFabSQ.free;
  end;
end;

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

procedure TfrmFabList.TSelClick(Sender: TObject);
begin
  if cds_Main.IsEmpty then exit;
  frmFabSQ:=TfrmFabSQ.create(self);
  with frmFabSQ do
  begin
    formid:=2;
    FMainId:=trim(FrmFabList.cds_Main.fieldbyname('Mainid').AsString);
    showmodal;
    free;
  end;
end;

procedure TfrmFabList.TupdateClick(Sender: TObject);
begin
  if cds_Main.IsEmpty then exit;
  try
    frmFabSQ:=TfrmFabSQ.create(self);
    with frmFabSQ do
    begin
      formid:=1;
      FMainId:=trim(FrmFabList.cds_Main.fieldbyname('Mainid').AsString);
      if showmodal=1 then
      begin
        initData();
      end;
    end;
  finally
    frmFabSQ.free;
  end;
end;

procedure TfrmFabList.TINITClick(Sender: TObject);
begin
  initData();
end;

procedure TfrmFabList.TV1DblClick(Sender: TObject);
begin
  Tsel.Click;
end;

procedure TfrmFabList.TdelClick(Sender: TObject);
begin
  if cds_Main.IsEmpty then exit;
  if application.MessageBox('确定要删除吗？','提示',1)=2 then exit;
  try
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from Contract_Main ');
      sql.add('where SQNo='''+trim(cds_Main.fieldbyname('SQNo').asstring)+'''');
      open;
      if not isempty then
      begin
        application.MessageBox('已采购不能删除！','提示');
        exit;
      end;
    end;
    with ADOQueryDel do
    begin
      close;
      sql.Clear;
      sql.Add('delete from contract_Main_Sh ');
      sql.Add('where mainid='''+trim(cds_Main.fieldbyname('Mainid').AsString)+'''');
      sql.Add('delete from contract_Sub_Sh ');
      sql.Add('where mainid='''+trim(cds_Main.fieldbyname('Mainid').AsString)+'''');
      execsql;
    end;
  except
    application.messagebox('数据删除失败','提示',0);
  end;
  initdata();
end;

procedure TfrmFabList.FormDestroy(Sender: TObject);
begin
  frmFablist:=nil;
end;

procedure TfrmFabList.cxTabControl1Change(Sender: TObject);
begin
  setStatus();
  initData();
end;

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

procedure TfrmFabList.TNochkClick(Sender: TObject);
begin
  if cds_Main.IsEmpty then exit;
  try
  {  with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from Contract_Main ');
      sql.add('where SQNo='''+trim(cds_Main.fieldbyname('SQNo').asstring)+'''');
      open;
      if not isempty then
      begin
        application.MessageBox('已采购不能撤销','提示');
        exit;
      end;
    end; }
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('update contract_Main_Sh set chkstatus=''0'',chktime=null,chker=null');
      sql.add(' where Mainid='''+trim(cds_Main.fieldbyname('Mainid').asstring)+'''');
      execsql;
    end;
    application.MessageBox('撤销成功','提示');
    initdata();
  except
    application.MessageBox('撤销失败','提示');
  end;
end;

procedure TfrmFabList.PONoChange(Sender: TObject);
begin
  SDofilter(AdoQueryTmp,SGetFilters(Panel1,1,0));
  SCreateCDS20(AdoQueryTmp,cds_Main);
  SInitCDSData20(AdoQueryTmp,cds_Main);
end;

procedure TfrmFabList.TglvClick(Sender: TObject);
begin
  SDofilter(AdoQueryTmp,SGetFilters(Panel1,1,0));
  SCreateCDS20(AdoQueryTmp,cds_Main);
  SInitCDSData20(AdoQueryTmp,cds_Main);
end;

procedure TfrmFabList.TPrintClick(Sender: TObject);
var
  fPrintFile:string;
begin
  if cds_Main.IsEmpty then exit;
  with ADOQueryPrint do
  begin
    close;
    sql.clear;
    sql.Add('select a.*,b.* ');
    sql.Add(',FYName=(select top 1 MPrtCodeName from JYOrderFy_Other X where X.orderNo=A.fyNO)');
    sql.Add(',FYSpec=(select top 1 Mprtspec from JYOrderFy_Other X where X.orderNo=A.fyNO)');
    sql.Add(',FYCF=(select top 1 MprtCF from JYOrderFy_Other X where X.orderNo=A.fyNO)');
    sql.Add(',FYMF=(select top 1 MPRTMF from JYOrderFy_Other X where X.orderNo=A.fyNO)');
    sql.Add(',FYKZ=(select top 1 MPRTKZ from JYOrderFy_Other X where X.orderNo=A.fyNO)');
    sql.Add(',PBfactory=(select top 1 PBfactory from JYOrderFy_Other X where X.orderNo=A.fyNO)');
    sql.Add(',FYNote=(select top 1 Note from JYOrderFy_Other X where X.orderNo=A.fyNO)');
  //  sql.Add('C.MPrtCodeName as FYName,C.Mprtspec as FYSpec,C.MprtCF as FYCF,C.MPRTMF as FYMF,C.MPRTKZ as FYKZ,C.PBfactory,C.Note as FYNote, ');
    sql.Add(',customerNo=(select top 1 zdyNameZ from KH_Zdy_Attachment X where X.zdyName=A.customerNoName)');
    sql.add('from contract_main_Sh A ');
    sql.Add('inner join contract_Sub_Sh B on B.Mainid=A.Mainid ');
  //  sql.Add('left join JYOrderFy_Other C on C.OrderNo=A.FYNO ');
    sql.Add('where A.Mainid='''+trim(cds_Main.fieldbyname('Mainid').asstring)+''' ');
    open;
  end;
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\坯布申请合同.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+trim(fPrintFile)),'提示',0);
  end;
end;

procedure TfrmFabList.TV1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  initsub();
end;

procedure TfrmFabList.TBCopyClick(Sender: TObject);
begin
  if cds_Main.IsEmpty then exit;
  frmFabSQ:=TfrmFabSQ.create(self);
  with frmFabSQ do
  begin
    formid:=3;
    FMainId:=trim(FrmFabList.cds_Main.fieldbyname('Mainid').AsString);
    showmodal;
    free;
  end;
end;

end.
