unit U_ContractCGJGSPList;

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, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP,ShellAPI, cxCheckBox,jpeg,IniFiles;

type
  TfrmContractCGJGSPList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: 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;
    Label3: TLabel;
    ConNo: TEdit;
    Label5: TLabel;
    C_CodeName: TEdit;
    Order_Main: TClientDataSet;
    Label4: TLabel;
    FactoryNoName: TEdit;
    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;
    v1Qty1: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1PRTQty: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column3: TcxGridDBColumn;
    ConType: TComboBox;
    Label14: TLabel;
    ToolButton2: TToolButton;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    TBChkNO: TToolButton;
    TBChkOk: TToolButton;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    ToolButton8: TToolButton;
    cxTabControl1: TcxTabControl;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    ComboBox1: TComboBox;
    Panel2: TPanel;
    Label6: TLabel;
    PChkNote: TEdit;
    ToolButton1: TToolButton;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure conPress(Sender: TObject; var Key: Char);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBChkOkClick(Sender: TObject);
    procedure TBChkNOClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    FInt,PFInt:Integer;
    canshu1:string;
    lstPat: TStringList;
		AJpeg: TJPEGImage;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure ReadINIFile10();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmContractCGJGSPList: TfrmContractCGJGSPList;

implementation
uses
  U_DataLink,U_RTFun,U_ClothContractInPutSXMX,U_ZDYHelp
  {U_ClothContractInPutSX,
  U_ProductOrderList,U_ZDYHelp,
  ,U_ClothContractInPutHZ,U_ClothContractInPutPB}, U_ModuleNote;

{$R *.dfm}

procedure TfrmContractCGJGSPList.FormDestroy(Sender: TObject);
begin
  frmContractCGJGSPList:=nil;
end;

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

procedure TfrmContractCGJGSPList.TBCloseClick(Sender: TObject);
var
  FPath:String;
begin
  Close;
  WriteCxGrid('采购加工合同PChk',Tv1,'生产合同管理');
end;

procedure TfrmContractCGJGSPList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.*,B.*,Money=B.C_Qty*B.Price from Contract_Main A inner join Contract_Sub B on A.MainId=B.MainId ');
      sql.Add(' where isnull(SSPerson,'''')<>'''' and isnull(Chker,'''')<>'''' ');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(PChker,'''')='''' ');
      end else
      begin
        sql.Add(' and A.FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
        sql.Add(' and A.FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''');
        if cxTabControl1.TabIndex<>3 then
        begin
          sql.Add(' and isnull(PChker,'''')<>'''' ');
          if cxTabControl1.TabIndex=1 then
          begin
            sql.Add(' and isnull(PChkStatus,'''')=''审批通过'' ');
          end else
          if cxTabControl1.TabIndex=2 then
          begin
            sql.Add(' and isnull(PChkStatus,'''')=''审批不通过'' ');
          end;
        end;

      end;

      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmContractCGJGSPList.InitForm();
var
  FSJ:string;
begin
  
  ReadCxGrid('采购加工合同PChk',Tv1,'生产合同管理');
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime-30;
  FSJ:='select ZdyName Name,ZdyName Code from KH_Zdy where TYpe=''JGGX'' and ZdyName not like ''%回检%'' ';
  SInitComBoxBySql(ADOQueryTemp,ConType,False,Fsj);
end;

procedure TfrmContractCGJGSPList.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 TfrmContractCGJGSPList.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Contract_Sub where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Contract_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 Contract_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 Contract_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 TfrmContractCGJGSPList.TBPrintClick(Sender: TObject);
var
   fPrintFile,FConNoM,RPTName:string;

begin
  if Order_Main.IsEmpty then Exit;
  if Trim(ComboBox1.Text)='' then
  begin
    Application.MessageBox('报表名称不能为空!','提示',0);
    Exit;
  end;
  RPTName:='Report\'+Trim(ComboBox1.Text)+'.rmf';
 // fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\采购加工合同.rmf' ;
  fPrintFile:= ExtractFilePath(Application.ExeName) +RPTName;
  with ADOQueryTemp do
  begin
    Filtered:=False;
    Close;
    sql.Clear;
    sql.Add('select A.*,B.*,Money=B.C_Qty*B.Price,C.*,D.KHName,D.ZKTelNo,D.ZKFax,D.ZKAddress  ');
    sql.Add(' ,C.ZdyName,C.ZdyStr1,C.ZdyStr2,C.ZdyStr3,C.ZdyStr4');
    sql.Add(' from Contract_Main A inner join Contract_Sub B on A.MainId=B.MainId');
    sql.Add(' left join KH_Zdy C on A.CompanyName=C.ZdyName and C.Type=''GSTT'' ');
    sql.Add(' left join ZH_KH_Info D on A.FactoryNo=D.ZKID');
    sql.Add(' where A.Mainid='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+RPTName),'提示',0);
  end;

end;

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

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

procedure TfrmContractCGJGSPList.conPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(ConNo.Text))<3 then Exit;
    try
      ADOQueryMain.DisableControls;

      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('select A.*,B.*,Money=B.C_Qty*B.Price from Contract_Main A inner join Contract_Sub B on A.MainId=B.MainId ');
        sql.Add(' where A.ConNo like '''+'%'+Trim(ConNo.text)+'%'+'''');
        sql.Add(' and isnull(SSPerson,'''')<>'''' and isnull(Chker,'''')<>'''' ');
        if cxTabControl1.TabIndex=0 then
        begin
          sql.Add(' and isnull(PChker,'''')='''' ');
        end else
        begin
          if cxTabControl1.TabIndex<>3 then
          begin
            sql.Add(' and isnull(PChker,'''')<>'''' ');
            if cxTabControl1.TabIndex=1 then
            begin
              sql.Add(' and isnull(PChkStatus,'''')=''审批通过'' ');
            end else
            if cxTabControl1.TabIndex=2 then
            begin
              sql.Add(' and isnull(PChkStatus,'''')=''审批不通过'' ');
            end;
          end;

        end;
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmContractCGJGSPList.ToolButton2Click(Sender: TObject);
begin
  try
    frmClothContractInPutSXMX:=TfrmClothContractInPutSXMX.Create(Application);
    with frmClothContractInPutSXMX do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FConNo:=Trim(Self.Order_Main.fieldbyname('ConNo').AsString);
      TBSave.Visible:=False;
      ToolBar2.Visible:=False;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmClothContractInPutSXMX.Free;
  end;
end;

procedure TfrmContractCGJGSPList.ToolButton3Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='采购加工合同';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmModuleNote.Free;
  end;
end;

procedure TfrmContractCGJGSPList.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
  cxTabControl1.TabIndex:=0;
end;

procedure TfrmContractCGJGSPList.ReadINIFile10();
var
  programIni:Tinifile;  //配置文件名
  FileName:string;
begin
  FileName:=ExtractFilePath(Paramstr(0))+'SYSTEMSET.INI';
  programIni:=Tinifile.create(FileName);
  server:=programIni.ReadString('SERVER','服务器地址','127.0.0.1');
  programIni.Free;
end;

procedure TfrmContractCGJGSPList.TBChkOkClick(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>0 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('Update Contract_Main Set PChkStatus=''审批通过'',PChker='''+Trim(DName)+'''');
    sql.Add(',PChkTIme=getdate() where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    ExecSQL;
  end;
  Order_Main.Delete;
end;

procedure TfrmContractCGJGSPList.TBChkNOClick(Sender: TObject);
begin
  if cxTabControl1.TabIndex=0 then Exit;
  if cxTabControl1.TabIndex=3 then Exit;
  if Order_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  if Application.MessageBox('确定要执行审批撤销操作吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('Update Contract_Main Set PChkStatus=NUll,PChker=NUll,PChkNote=NULL ');
    sql.Add(',PChkTIme=Null where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    ExecSQL;
  end;
  Order_Main.Delete;
end;

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

procedure TfrmContractCGJGSPList.ToolButton8Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel('采购加工合同',cxGrid1);
end;

procedure TfrmContractCGJGSPList.ToolButton1Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex>1 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Trim(PChkNote.Text)='' then
  begin
    Application.MessageBox('审批备注不能为空!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要审批不通过吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add(' Update Contract_Main Set PChkStatus=''审批不通过'',PChker='''+Trim(DName)+'''');
    sql.Add(',PChkTime=getdate(),PChkNote='''+Trim(PChkNote.Text)+'''');
    sql.add(' where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    ExecSQL;
  end;
  //ChkNote.Text:='';
  Order_Main.Delete;
end;

end.
