unit U_ContractCGJGViewList_CX;

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
  TfrmContractCGJGViewList_CX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    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;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    Label6: TLabel;
    CGNo: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1CGNo: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1FactoryNo1Name: TcxGridDBColumn;
    v1Qty1: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1PBMoney: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1JGGX: TcxGridDBColumn;
    v1IFHS: TcxGridDBColumn;
    v1ChkStatus: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure conPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TSselClick(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
  private
    FInt,PFInt:Integer;
    canshu1:string;
    lstPat: TStringList;
		AJpeg: TJPEGImage;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure ReadINIFile10();
    { Private declarations }
  public
    formid,canshu2,FConNO:string;
    { Public declarations }
  end;

var
  frmContractCGJGViewList_CX: TfrmContractCGJGViewList_CX;

implementation
uses
  U_DataLink,U_RTFun,U_ZDYHelp;

{$R *.dfm}

procedure TfrmContractCGJGViewList_CX.FormDestroy(Sender: TObject);
begin
  frmContractCGJGViewList_CX:=nil;
end;

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

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

procedure TfrmContractCGJGViewList_CX.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(' from Contract_Main A inner join Contract_Sub B on A.MainId=B.MainId ');
      sql.Add(' where exists(select C.ConNO from JYOrder_Main C where C.OrderNo=A.ConNO and C.ConNO='''+trim(FConNO)+''')');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally;
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmContractCGJGViewList_CX.InitForm();
begin
  InitGrid();
  ReadCxGrid('采购加工合同View1',Tv1,'生产合同管理');
end;

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

procedure TfrmContractCGJGViewList_CX.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 TfrmContractCGJGViewList_CX.FormShow(Sender: TObject);
begin
  if canshu2='辅料' then
  begin
    Label6.Caption:='辅料合同号';
    v1CGNo.Caption:='辅料合同号';
  end;
  InitForm();
end;

procedure TfrmContractCGJGViewList_CX.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,'''')<>'''' ');
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmContractCGJGViewList_CX.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
  if formid<>'1' then
  canshu2:=Trim(DParameters2);
end;

procedure TfrmContractCGJGViewList_CX.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 TfrmContractCGJGViewList_CX.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmContractCGJGViewList_CX.TSselClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  Modalresult:=1;
end;

procedure TfrmContractCGJGViewList_CX.RadioGroup1Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

end.
