unit U_ContractCGJGList;

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
  TfrmContractCGJGList = 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;
    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;
    ToolButton1: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1ConNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1FactoryNo1Name: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1Qty1: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1PRTQty: TcxGridDBColumn;
    v1C_Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolButton2: TToolButton;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    ComboBox1: TComboBox;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    v1SChker: TcxGridDBColumn;
    v1SChkdate: TcxGridDBColumn;
    TBSSOk: TToolButton;
    TBSSNO: TToolButton;
    v1SSChk: TcxGridDBColumn;
    ToolButton8: TToolButton;
    cxTabControl1: TcxTabControl;
    Label14: TLabel;
    ConTypeOther: TComboBox;
    v1C_Color: TcxGridDBColumn;
    v1C_SeNo: TcxGridDBColumn;
    v1PS: TcxGridDBColumn;
    Label6: TLabel;
    C_Spec: TEdit;
    Label2: TLabel;
    C_Color: TEdit;
    v1Column3: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1KZQty: TcxGridDBColumn;
    v1KZ: TcxGridDBColumn;
    v1C_CF: TcxGridDBColumn;
    v1PiZhong: TcxGridDBColumn;
    v1OrderNo: TcxGridDBColumn;
    Label7: TLabel;
    OrderNo: TEdit;
    ToolButton4: TToolButton;
    v1ConNoHZ: TcxGridDBColumn;
    Label8: TLabel;
    ConNoHZ: TEdit;
    v1KHJC: TcxGridDBColumn;
    Label9: TLabel;
    KHJC: TEdit;
    ToolButton5: TToolButton;
    v1IFCK: TcxGridDBColumn;
    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 TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure conPress(Sender: TObject; var Key: Char);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBSSOkClick(Sender: TObject);
    procedure TBSSNOClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
  private
    FInt,PFInt:Integer;
    canshu1:string;
    lstPat: TStringList;
		AJpeg: TJPEGImage;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure ReadINIFile10();
    procedure setstatus();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmContractCGJGList: TfrmContractCGJGList;

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

{$R *.dfm}
procedure TfrmContractCGJGList.setstatus();
begin
  TBEdit.Visible:=False;
  ToolButton1.Visible:=false;
  TBDel.Visible:=False;
  TBSSOk.Visible:=False;
  TBSSNO.Visible:=false;
  if cxTabControl1.TabIndex=0 then
  begin
    TBEdit.Visible:=true;
    ToolButton1.Visible:=true;
    TBDel.Visible:=true;
    TBSSOk.Visible:=true;
  end;
  if cxTabControl1.TabIndex=1 then
  begin
    TBSSNO.Visible:=true;
  end;
  if cxTabControl1.TabIndex=3 then
  begin
    TBSSOk.Visible:=True;
  end;
  if canshu1='高权限' then
  begin
    TBEdit.Visible:=true;
    TBDel.Visible:=true;
  end;
end;

procedure TfrmContractCGJGList.FormDestroy(Sender: TObject);
begin
  frmContractCGJGList:=nil;
end;

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

procedure TfrmContractCGJGList.TBCloseClick(Sender: TObject);
var
  FPath:String;
begin
  Close;
  WriteCxGrid('采购加工合同',Tv1,'生产合同管理');
  FPath:='D:\Right1209\';
  if  DirectoryExists(ExtractFileDir(FPath)) then
    winexec('cmd /c rd /s /q D:\Right1209\',sw_hide);
end;

procedure TfrmContractCGJGList.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 ');
      sql.Add(',KHJC=(select Top 1 C.zdyNameZ from KH_Zdy_Attachment C inner join JYOrder_Main D on C.ZdyName=D.CustomerNoName where D.OrderNo=B.OrderNO)');
      sql.Add(',ConNoHZ=(select C.ConNo from JYOrder_Main C where C.OrderNo=B.OrderNo)');
      sql.Add(',SChk=(case when isnull(SChkstatus,'''')='''' then ''待送审'' when isnull(SChkstatus,'''')=''1'' then ''已送审''');
      sql.Add(' when isnull(SChkstatus,'''')=''2'' then ''审核通过'' end)');
      SQL.Add('from Contract_Main A inner join Contract_Sub B on A.MainId=B.MainId ');
      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='''+Trim(DName)+'''');
      end;
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(B.SChkstatus,'''')='''' ');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and isnull(B.SChkstatus,'''')=''1'' ');
      end else
      if cxTabControl1.TabIndex=2 then
      begin
        sql.Add(' and isnull(B.SChkstatus,'''')=''2'' ');
      end else
      if cxTabControl1.TabIndex=3 then
      begin
        sql.Add(' and isnull(B.SChkStatus,'''')=''审核不通过'' ');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmContractCGJGList.InitForm();
begin
  ReadCxGrid('采购加工合同',Tv1,'生产合同管理');
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime-30;
end;

procedure TfrmContractCGJGList.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 TfrmContractCGJGList.TBEditClick(Sender: TObject);
var
  FInt:Integer;
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(canshu1)<>'高权限' then
  begin
    if (cxTabControl1.TabIndex<>0) and (cxTabControl1.TabIndex<>3) then Exit;
    if Trim(Order_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
    begin
      Application.MessageBox('不能操作他人数据!','提示',0);
      Exit;
    end;
  end;
  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);
      EditInt:=FInt;
      frmClothContractInPutSXMX.canshu1:=Trim(self.canshu1);
      if ShowModal=1 then
      begin

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

procedure TfrmContractCGJGList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(canshu1)<>'高权限' then
  begin
    if cxTabControl1.TabIndex<>0 then Exit;
    if Trim(Order_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
    begin
      Application.MessageBox('不能操作他人数据!','提示',0);
      Exit;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_SXPB_CR where CGMainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=false then
  begin
    Application.MessageBox('已经录入码单不能删除数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    Order_Main.Delete;
  end;
end;

function TfrmContractCGJGList.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 TfrmContractCGJGList.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 TfrmContractCGJGList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmContractCGJGList.TBAddClick(Sender: TObject);
begin
  try
    frmClothContractInPutSXMX:=TfrmClothContractInPutSXMX.Create(Application);
    with frmClothContractInPutSXMX do
    begin
      PState:=0;
      FMainId:='';
      if ShowModal=1 then
      begin

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

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

procedure TfrmContractCGJGList.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)+'%'+'''');
        if Trim(canshu1)<>'高权限' then
        begin
          sql.Add(' and A.Filler='''+Trim(DName)+'''');
        end;
        if cxTabControl1.TabIndex=0 then
        begin
          sql.Add(' and isnull(SSPerson,'''')='''' ');
        end else
        if cxTabControl1.TabIndex=1 then
        begin
          sql.Add(' and isnull(SSPerson,'''')<>'''' ');
        end;
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmContractCGJGList.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  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);
      PCopyInt:=1;
      if ShowModal=1 then
      begin

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

procedure TfrmContractCGJGList.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 TfrmContractCGJGList.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 TfrmContractCGJGList.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
  cxTabControl1.TabIndex:=0;
end;

procedure TfrmContractCGJGList.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 TfrmContractCGJGList.TBSSOkClick(Sender: TObject);
begin
  if (cxTabControl1.TabIndex<>0) and (cxTabControl1.TabIndex<>3) 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_Sub Set SChker='''+Trim(DName)+''',SChkstatus=''1''');
    sql.Add(',SChkdate=getdate() where Subid='''+Trim(Order_Main.fieldbyname('Subid').AsString)+'''');
    ExecSQL;
  end;
  Order_Main.Delete;
end;

procedure TfrmContractCGJGList.TBSSNOClick(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>1 then Exit;
  if Order_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Sub where SubID='''+Trim(Order_Main.fieldbyname('SubID').AsString)+'''');
    sql.Add(' and isnull(SChkStatus,'''')=''2'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False 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_Sub Set SChker='''',SChkstatus=''''');
    sql.Add(',SChkdate=null where Subid='''+Trim(Order_Main.fieldbyname('Subid').AsString)+'''');
    ExecSQL;
  end;
  Order_Main.Delete;
end;

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

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

procedure TfrmContractCGJGList.ToolButton4Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmFjList_RZ:=TfrmFjList_RZ.Create(Application);
    with frmFjList_RZ do
    begin
      fkeyNO:=Trim(Self.Order_Main.fieldbyname('ConNoHZ').AsString);
      fType:='合同';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmFjList_RZ.Free;
  end;
end;

procedure TfrmContractCGJGList.ToolButton5Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>2 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_Sub Set IFCK=''是''');
    sql.Add(' where Subid='''+Trim(Order_Main.fieldbyname('Subid').AsString)+'''');
    ExecSQL;
  end;
end;

end.
