unit U_ClothSCGYChkInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid, cxMemo,
  cxRichEdit, ComCtrls, cxContainer, cxTextEdit, cxMaskEdit, cxButtonEdit,
  StdCtrls, ToolWin, DBClient, ADODB, ExtCtrls, BtnEdit, cxDropDownEdit,
  StrUtils, cxPC, ShellAPI, cxCalendar, cxLookAndFeels,
  cxLookAndFeelPainters, dxBarBuiltInMenu, cxNavigator;

type
  TfrmClothSCGYChkInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    ADOQuery1: TADOQuery;
    ADOQueryTemp: TADOQuery;
    Panel2: TPanel;
    ToolBar3: TToolBar;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    DataSource2: TDataSource;
    CDS_YuanLiao: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxTabSheet4: TcxTabSheet;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridLevel2: TcxGridLevel;
    v3Column18: TcxGridDBColumn;
    cxGrid5: TcxGrid;
    Tv5: TcxGridDBTableView;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridDBColumn27: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    Panel1: TPanel;
    Panel3: TPanel;
    cxGrid6: TcxGrid;
    Tv6: TcxGridDBTableView;
    cxGridDBColumn41: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGridDBColumn42: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    ClientDataSet6: TClientDataSet;
    DataSource6: TDataSource;
    ClientDataSet5: TClientDataSet;
    DataSource3: TDataSource;
    ClientDataSet3: TClientDataSet;
    DataSource5: TDataSource;
    Panel4: TPanel;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    cxTabSheet3: TcxTabSheet;
    cxGrid7: TcxGrid;
    Tv7: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn29: TcxGridDBColumn;
    cxGridDBColumn30: TcxGridDBColumn;
    cxGridDBColumn31: TcxGridDBColumn;
    cxGridLevel5: TcxGridLevel;
    Tv7Column1: TcxGridDBColumn;
    ClientDataSet7: TClientDataSet;
    DataSource7: TDataSource;
    v7Column1: TcxGridDBColumn;
    v7Column2: TcxGridDBColumn;
    v7Column3: TcxGridDBColumn;
    v7Column4: TcxGridDBColumn;
    v7Column5: TcxGridDBColumn;
    v7Column6: TcxGridDBColumn;
    v7Column7: TcxGridDBColumn;
    v7Column8: TcxGridDBColumn;
    v7Column9: TcxGridDBColumn;
    v7Column10: TcxGridDBColumn;
    v5Column1: TcxGridDBColumn;
    v5Column2: TcxGridDBColumn;
    v5Column3: TcxGridDBColumn;
    v5Column4: TcxGridDBColumn;
    v5Column5: TcxGridDBColumn;
    v5Column6: TcxGridDBColumn;
    v5Column7: TcxGridDBColumn;
    v5Column8: TcxGridDBColumn;
    v5Column9: TcxGridDBColumn;
    v5Column10: TcxGridDBColumn;
    v5Column11: TcxGridDBColumn;
    v5Column12: TcxGridDBColumn;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label11: TLabel;
    Label9: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label22: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label8: TLabel;
    Label12: TLabel;
    Label10: TLabel;
    Label13: TLabel;
    Label16: TLabel;
    Label26: TLabel;
    Label31: TLabel;
    Label36: TLabel;
    Label37: TLabel;
    Label42: TLabel;
    Label47: TLabel;
    Label33: TLabel;
    Label54: TLabel;
    Label58: TLabel;
    Label43: TLabel;
    Label59: TLabel;
    Label60: TLabel;
    Label61: TLabel;
    Label62: TLabel;
    ConNo: TEdit;
    QDTime: TDateTimePicker;
    CarType: TcxButtonEdit;
    DeliveryDate: TDateTimePicker;
    PiKgQty: TEdit;
    PiQty: TEdit;
    C_Qty: TEdit;
    PiKgQtyFD: TEdit;
    C_Note: TRichEdit;
    LabName: TComboBox;
    BZNote: TcxButtonEdit;
    gongyi: TcxButtonEdit;
    C_Spec: TRichEdit;
    SuoShuiNote: TcxButtonEdit;
    KZ: TEdit;
    MF: TEdit;
    C_Code: TcxButtonEdit;
    C_CodeName: TEdit;
    KHName: TcxButtonEdit;
    YWY: TcxButtonEdit;
    lidan: TcxButtonEdit;
    FactoryNoName: TcxButtonEdit;
    GDY: TcxButtonEdit;
    ConType: TcxButtonEdit;
    Label7: TLabel;
    SJNote: TRichEdit;
    PSNote: TEdit;
    ISKF: TCheckBox;
    ISYT: TCheckBox;
    ISCZ: TCheckBox;
    Label14: TLabel;
    JiTaiType: TcxButtonEdit;
    Label15: TLabel;
    YGPS: TEdit;
    Label17: TLabel;
    YGQty: TEdit;
    Label20: TLabel;
    Label21: TLabel;
    DayQty: TEdit;
    JTZB: TcxButtonEdit;
    Label24: TLabel;
    Label25: TLabel;
    GongYiCode: TcxButtonEdit;
    Label27: TLabel;
    ZhenCun: TEdit;
    TJGongYiCode: TEdit;
    Label23: TLabel;
    Label28: TLabel;
    Label29: TLabel;
    MinWet: TEdit;
    MaxWet: TEdit;
    Label30: TLabel;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v2Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv6DblClick(Sender: TObject);
    procedure Tv3MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Tv5MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure GongYiCodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column11PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    FXS, Fint: Integer;
    procedure InitData();
    function SaveData(): Boolean;
    procedure InitGY();
    { Private declarations }
  public
    PState, FCopyInt: Integer;
    FMainId, FConNo, PType, FSubId,ChkStatus: string;
    { Public declarations }
  end;

var
  frmClothSCGYChkInPut: TfrmClothSCGYChkInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_CompressionFun,U_ZdyAttachGYS, U_FjList_RZ, U_ClothGongYiListSel;

{$R *.dfm}

procedure TfrmClothSCGYChkInPut.TBCloseClick(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmClothSCGYChkInPut.InitData();
var
  PBNO, PBFlag: string;
begin
  if PState = 1 then
  begin
    ConNo.ReadOnly := True;
  end;
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('  select *   from Cloth_Main OM   Left join Cloth_Sub OS on OM.MainId=OS.MainId ');
    if PState = 1 then
    begin
      sql.Add(' where OM.MainId= ' + quotedstr(Trim(FMainId)));
    end;

    Open;
  end;
  SCSHData(ADOQuery1, ScrollBox1, 0);
  SCSHData(ADOQuery1, ScrollBox1, 2);
  ISYT.Checked := ADOQuery1.FieldByName('ISYT').Value;
  ISKF.Checked := ADOQuery1.FieldByName('ISKF').Value;
  ISCZ.Checked := ADOQuery1.FieldByName('ISCZ').Value;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(',YLQty=Cast(C_Qty*PBQty*0.01*(1+isnull(ZPSH,0)*0.01) as decimal(18,1))');
    sql.Add('from Cloth_YCL_PB A ');
    sql.Add('inner join Cloth_Sub B on A.SubId=B.SubId');
    SQL.Add(' inner join Cloth_Main C on B.Mainid=C.MainId');
    sql.Add(' where A.MainId=''' + Trim(FMainId) + '''');
    Open;
  end;

  SCreateCDS20(ADOTemp, CDS_YuanLiao);
  SInitCDSData20(ADOTemp, CDS_YuanLiao);

  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Cloth_PaiZhen where MainId=''' + Trim(FMainId) + '''');
    sql.Add(' and PZType=''排针'' ');
    Open;
  end;

  SCreateCDS20(ADOTemp, ClientDataSet3);
  SInitCDSData20(ADOTemp, ClientDataSet3);

  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Cloth_PaiZhen where MainId=''' + Trim(FMainId) + '''');
    sql.Add(' and PZType=''三角'' ');
    Open;
  end;

  SCreateCDS20(ADOTemp, ClientDataSet5);
  SInitCDSData20(ADOTemp, ClientDataSet5);

  if PState = 0 then
  begin
    with ADOTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add('select Top 1* from Cloth_Main order by FillTime desc ');
      Open;
    end;
    QDTime.DateTime := SGetServerDate(ADOTemp);
    QDTime.Checked := True;
    DeliveryDate.Date := QDTime.DateTime;
//    PiZhong.Text := '0.1';
  end;

  if FCopyInt = 1 then
  begin
    FMainId := '';
    ConNo.Text := '';
    ConNo.ReadOnly := False;
    CDS_YuanLiao.DisableControls;
    with CDS_YuanLiao do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value := '';
        FieldByName('SubId').Value := '';
        FieldByName('PBId').Value := '';
        FieldByName('BegPBQty').Value := Null;
        Post;
        Next;
      end;
    end;
    CDS_YuanLiao.EnableControls;
    ClientDataSet3.DisableControls;
    with ClientDataSet3 do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value := '';
        FieldByName('SubId').Value := '';
        FieldByName('PZId').Value := '';
        Post;
        Next;
      end;
    end;
    ClientDataSet3.EnableControls;
    ClientDataSet5.DisableControls;
    with ClientDataSet5 do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value := '';
        FieldByName('SubId').Value := '';
        FieldByName('PZId').Value := '';
        Post;
        Next;
      end;
    end;
    ClientDataSet5.EnableControls;
  end;

  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select TFID,FileName,TFdate,Filler,FillTime from TP_File ');
    sql.Add('where WBID=' + quotedstr(FMainId));
    sql.Add('and TFType=''PB附件'' ');
    open;
  end;
  SCreateCDS20(ADOTemp, ClientDataSet6);
  SInitCDSData20(ADOTemp, ClientDataSet6);
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Cloth_WJG where MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCreateCDS20(ADOTemp, ClientDataSet7);
  SInitCDSData20(ADOTemp, ClientDataSet7);
end;

procedure TfrmClothSCGYChkInPut.FormShow(Sender: TObject);
begin
  InitData();
  if ChkStatus='审核' then
  begin
    if Trim(GongYiCode.Text)='' then
    begin
      if Trim(TJGongYiCode.Text)<>'' then
      begin
        GongYiCode.Text:=Trim(TJGongYiCode.Text);
      end;
      InitGY();
    end;
  end;

end;
procedure TfrmClothSCGYChkInPut.InitGY();
begin
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CLoth_GY where GongYiCode='''+Trim(GongYiCode.Text)+'''');
    sql.Add(' and QYFlag=1 and Valid=''Y'' ');
    Open;
  end;
  SJNote.Text:=Trim(ADOTemp.fieldbyname('SJNote').AsString);
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* from Cloth_YCL_PB_GY A ');
    sql.Add(' inner join Cloth_GY B on A.GYID=B.GYID');
    sql.Add(' where B.GongYiCode='''+Trim(GongYiCode.Text)+''' and B.Valid=''Y'' and B.QYFlag=1 ');
    Open;
  end;
  SCreateCDS20(ADOTemp,CDS_YuanLiao);
  SInitCDSData20(ADOTemp,CDS_YuanLiao);
  with CDS_YuanLiao do
  begin
    first;
    while not eof do
    begin
      edit;
      FieldByName('MainId').Value := '';
      FieldByName('SubId').Value := '';
      FieldByName('PBId').Value := '';
      next;
    end;
  end;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* from Cloth_PaiZhen_GY A ');
    sql.Add(' inner join Cloth_GY B on A.GYID=B.GYID');
    sql.Add(' where B.GongYiCode='''+Trim(GongYiCode.Text)+''' and B.Valid=''Y'' and B.QYFlag=1 ');
    sql.Add(' and A.PZType=''排针'' ');
    Open;
  end;
  SCreateCDS20(ADOTemp,ClientDataSet3);
  SInitCDSData20(ADOTemp,ClientDataSet3);
  with ClientDataSet3 do
  begin
    first;
    while not eof do
    begin
      edit;
      FieldByName('MainId').Value := '';
      FieldByName('SubId').Value := '';
      FieldByName('PZId').Value := '';
      next;
    end;
  end;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* from Cloth_PaiZhen_GY A ');
    sql.Add(' inner join Cloth_GY B on A.GYID=B.GYID');
    sql.Add(' where B.GongYiCode='''+Trim(GongYiCode.Text)+''' and B.Valid=''Y'' and B.QYFlag=1 ');
    sql.Add(' and A.PZType=''三角'' ');
    Open;
  end;
  SCreateCDS20(ADOTemp,ClientDataSet5);
  SInitCDSData20(ADOTemp,ClientDataSet5);
  with ClientDataSet5 do
  begin
    first;
    while not eof do
    begin
      edit;
      FieldByName('MainId').Value := '';
      FieldByName('SubId').Value := '';
      FieldByName('PZId').Value := '';
      next;
    end;
  end;
end;
function TfrmClothSCGYChkInPut.SaveData(): Boolean;
var
  maxno, maxsubno, maxpbno, maxpzno, maxsjno: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    maxno:=Trim(FMainId);
    maxsubno:=Trim(FSubId);
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Cloth_Main SET status=''2'',chker1=''' + trim(DName) + ''',Chktime1=getdate() ');
      sql.Add(',SJNOte='''+Trim(SJNote.Text)+'''');
      sql.Add('where mainID=' + quotedstr(trim(FMainId)));

      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('坯布生产工艺单审核')));
      sql.Add(',' + quotedstr(trim('订单号：' + trim(ConNo.Text))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    with ADOCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from Cloth_Sub where ');
      sql.Add('  SubId=''' + Trim(FSubId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      Edit;
      RTSetsavedata(ADOCmd, 'Cloth_Sub', ScrollBox1, 2);
      Post;
    end;        
    ///保存子表
    ///保存配比表
    with CDS_YuanLiao do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_YuanLiao.fieldbyname('PBId').AsString) = '' then
        begin
          if GetLSNo(ADOTemp, maxpbno, 'PB', 'Cloth_YCL_PB', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取配比子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxpbno := Trim(CDS_YuanLiao.fieldbyname('PBId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Cloth_YCL_PB where ');
          sql.Add('  PBId=''' + Trim(maxpbno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_YuanLiao.fieldbyname('PBId').AsString) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FillTime').Value := SGetServerDateTime(ADOTemp);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
          end;
          FieldByName('MainId').Value := Trim(FMainId);
          FieldByName('SubId').Value := Trim(FSubId);
          FieldByName('PBId').Value := Trim(maxpbno);
          FieldByName('SPId').Value := Trim(CDS_YuanLiao.fieldbyname('SPId').AsString);
          RTSetSaveDataCDS(ADOCmd, Tv2, CDS_YuanLiao, 'Cloth_YCL_PB', 0);
          Post;
        end;
        CDS_YuanLiao.Edit;
        CDS_YuanLiao.FieldByName('SubId').Value := Trim(FSubId);
        CDS_YuanLiao.FieldByName('MainId').Value := Trim(FMainId);
        CDS_YuanLiao.FieldByName('PBId').Value := Trim(maxpbno);
        //Order_Sub.Post;
        Next;
      end;
    end;
    ///保存配比表
    ///保存排针表
    ClientDataSet3.DisableControls;
    with ClientDataSet3 do
    begin
      First;
      while not Eof do
      begin
        if Trim(ClientDataSet3.fieldbyname('PZId').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, maxpzno, 'PZ', 'Cloth_PaiZhen', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取排针流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxpzno := Trim(ClientDataSet3.fieldbyname('PZId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Cloth_PaiZhen where ');
          sql.Add('  PZId=''' + Trim(maxpzno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(ClientDataSet3.fieldbyname('PZId').AsString) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FillTime').Value := SGetServerDateTime(ADOTemp);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
          end;
          FieldByName('MainId').Value := Trim(FMainId);
          FieldByName('SubId').Value := Trim(FSubId);
          FieldByName('PZId').Value := Trim(maxpzno);
          RTSetSaveDataCDS(ADOCmd, Tv3, ClientDataSet3, 'Cloth_PaiZhen', 0);
          FieldByName('PZType').Value := '排针';
          FieldByName('PZName').Value := ClientDataSet3.fieldbyname('PZName').AsString;
          Post;
        end;
        ClientDataSet3.Edit;
        ClientDataSet3.FieldByName('PZId').Value := Trim(maxpzno);
        //Order_Sub.Post;
        Next;
      end;
    end;
    ClientDataSet3.EnableControls;
    ///保存排针表
    ///保存三角表
    ClientDataSet5.DisableControls;
    with ClientDataSet5 do
    begin
      First;
      while not Eof do
      begin
        if Trim(ClientDataSet5.fieldbyname('PZId').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, maxsjno, 'PZ', 'Cloth_PaiZhen', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取三角流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxsjno := Trim(ClientDataSet5.fieldbyname('PZId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Cloth_PaiZhen where ');
          sql.Add('  PZId=''' + Trim(maxsjno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(ClientDataSet5.fieldbyname('PZId').AsString) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FillTime').Value := SGetServerDateTime(ADOTemp);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
          end;
          FieldByName('MainId').Value := Trim(FMainId);
          FieldByName('SubId').Value := Trim(FSubId);
          FieldByName('PZId').Value := Trim(maxsjno);
          RTSetSaveDataCDS(ADOCmd, Tv5, ClientDataSet5, 'Cloth_PaiZhen', 0);
          FieldByName('PZType').Value := '三角';
          Post;
        end;
        ClientDataSet5.Edit;
        ClientDataSet5.FieldByName('PZId').Value := Trim(maxsjno);
        //Order_Sub.Post;
        Next;
      end;
    end;
    ClientDataSet5.EnableControls;
    ///保存三角表
    ///保存外加工表
    ClientDataSet7.DisableControls;
    with ClientDataSet7 do
    begin
      First;
      while not Eof do
      begin
        if Trim(ClientDataSet7.fieldbyname('JGId').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, maxsjno, 'JG', 'Cloth_WJG', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取外发加工流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxsjno := Trim(ClientDataSet7.fieldbyname('JGId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Cloth_WJG where ');
          sql.Add('  JGId=''' + Trim(maxsjno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(ClientDataSet7.fieldbyname('JGId').AsString) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FillTime').Value := SGetServerDateTime(ADOTemp);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
          end;
          FieldByName('MainId').Value := Trim(FMainId);
          FieldByName('SubId').Value := Trim(FSubId);
          FieldByName('JGId').Value := Trim(maxsjno);
          FieldByName('WFGYSNo').Value := Trim(ClientDataSet7.fieldbyname('WFGYSNo').AsString);
          FieldByName('WFGYSNameJC').Value := Trim(ClientDataSet7.fieldbyname('WFGYSNameJC').AsString);
          RTSetSaveDataCDS(ADOCmd, Tv7, ClientDataSet7, 'Cloth_WJG', 0);
          Post;
        end;
        ClientDataSet7.Edit;
        ClientDataSet7.FieldByName('JGId').Value := Trim(maxsjno);
        //Order_Sub.Post;
        Next;
      end;
    end;
    ClientDataSet7.EnableControls;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select WFGYSName from Cloth_WJG where MainId=''' + Trim(maxno) + '''');
      sql.Add(' group by WFGYSName having count(*)>1');
      Open;
    end;
    if ADOCmd.RecordCount > 0 then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('外发加工单位在一张订单中只能存在一次!', '提示', 0);
      Exit;
    end;
    ////////////////更新外发表数据
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update Cloth_WJG Set JGMoney=isnull(JGPrice,0)*isnull(WFWCQtyZP,0)');
      sql.Add(',JSMoney=isnull(JGPrice,0)*isnull(WFWCQtyZP,0)-isnull(KKMoney,0)');
      sql.Add(' where MainId=''' + Trim(maxno) + '''');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Update_Cloth_Sub_WJG_Qty :MainId');
      Parameters.ParamByName('MainId').Value := Trim(maxno);
      ExecSQL;
    end;
    ///////////////更新外发表数据
    ///保存外加工表
    ///更新附件表主键
    ClientDataSet6.DisableControls;
    with ClientDataSet6 do
    begin
      First;
      while not Eof do
      begin
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update TP_File Set WBID=''' + Trim(maxno) + '''');
          sql.Add(' where TFID=''' + Trim(ClientDataSet6.fieldbyname('TFID').AsString) + '''');
          ExecSQL;
        end;
        Next;
      end;
    end;
    ClientDataSet6.EnableControls;
    ///更新附件表主键
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ClientDataSet3.EnableControls;
    ClientDataSet5.EnableControls;
    ClientDataSet6.EnableControls;
    CDS_YuanLiao.EnableControls;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmClothSCGYChkInPut.TBSaveClick(Sender: TObject);
begin
  if Trim(GongYiCode.Text)='' then
  begin
    Application.MessageBox('工艺编号不能为空!','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('审核成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmClothSCGYChkInPut.ToolButton3Click(Sender: TObject);
var
  OpenDiaLog: TOpenDialog;
  fFileName: string;
  fFilePath: string;
  maxNo: string;
  FJStream: TMemoryStream;
  mfileSize: integer;
  mCreationTime: TdateTime;
  mWriteTime: TdateTime;
begin
  if cxPageControl1.ActivePageIndex = 2 then
  begin
    try
      adoqueryCmd.Connection.BeginTrans;
      OpenDiaLog := TOpenDialog.Create(Self);
      if OpenDiaLog.Execute then
      begin
        fFilePath := OpenDiaLog.FileName;
        fFileName := ExtractFileName(OpenDiaLog.FileName);
        Panel4.Caption := '正在加载文件，请稍等...';
        Panel4.Visible := true;
        application.ProcessMessages;
        if GetLSNo(ADOQueryCmd, maxNo, 'FJ', 'TP_File', 4, 1) = False then
        begin
          adoqueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败！', '提示', 0);
          Exit;
        end;
        //获取文件信息
        GetFileInfo(fFilePath, mfileSize, mCreationTime, mWriteTime);
        with adoqueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('delete from TP_File ');
          sql.Add('where TFID=' + quotedstr(trim(maxNo)));
          execsql;
        end;
        try
          FJStream := TMemoryStream.Create;
          with adoqueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('select * from TP_File ');
            sql.Add('where TFID=' + quotedstr(trim(maxNo)));
            open;
            append;
            fieldbyname('TFID').Value := trim(maxNo);
            fieldbyname('TFType').Value := 'PB附件';
            fieldbyname('Filler').Value := trim(DName);
            fieldbyname('FileName').Value := trim(fFileName);
            fieldbyname('TFDate').Value := mWriteTime;
            FJStream.LoadFromFile(fFilePath);
            CompressionStream(FJStream);
            tblobfield(FieldByName('Filesother')).LoadFromStream(FJStream);
            post;
          end;
          with ClientDataSet6 do
          begin
            Append;
            FieldByName('TFID').Value := Trim(maxNo);
            FieldByName('FillTime').Value := SGetServerDateTime(ADOTemp);
            FieldByName('TFdate').Value := mWriteTime;
            FieldByName('Filler').Value := Trim(DName);
            fieldbyname('FileName').Value := trim(fFileName);
            Post;
          end;
        finally
          FJStream.Free;
        end;
      end;
      adoqueryCmd.Connection.CommitTrans;
      Panel4.Visible := False;
    except
      Panel4.Visible := False;
      adoqueryCmd.Connection.RollbackTrans;
      application.MessageBox('附件保存失败！', '提示信息', 0);
    end;
  end
  else if cxPageControl1.ActivePageIndex = 3 then
  begin
    with ClientDataSet7 do
    begin
      Append;
      Post;
    end;
  end;

end;

procedure TfrmClothSCGYChkInPut.ToolButton4Click(Sender: TObject);
begin

  if cxPageControl1.ActivePageIndex = 2 then
  begin
    if ClientDataSet6.IsEmpty then
      Exit;
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete TP_File where TFID=''' + Trim(ClientDataSet6.fieldbyname('TFID').AsString) + '''');
      ExecSQL;
    end;
    ClientDataSet6.Delete;
  end
  else if cxPageControl1.ActivePageIndex = 3 then
  begin
    if ClientDataSet7.IsEmpty then
      Exit;
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Cloth_WJG where JGID=''' + Trim(ClientDataSet7.fieldbyname('JGID').AsString) + '''');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Update_Cloth_Sub_WJG_Qty :Mainid');
      Parameters.ParamByName('Mainid').Value := Trim(FMainId);
      ExecSQL;
    end;
    ClientDataSet7.Delete;
  end;

end;

procedure TfrmClothSCGYChkInPut.v2Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PinPai';
      flagname := '品牌';
      if ShowModal = 1 then
      begin
        Self.CDS_YuanLiao.Edit;
        Self.CDS_YuanLiao.FieldByName('PinPai').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmClothSCGYChkInPut.Tv6DblClick(Sender: TObject);
var
  sFieldName: string;
  fileName: string;
  ff: TADOBlobStream;
  FJStream: TMemoryStream;
begin
  if ClientDataSet6.IsEmpty then
    exit;
  Panel4.Caption := '正在下载文件，请稍等...';
  Panel4.Visible := true;
  application.ProcessMessages;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from TP_File where TFID=''' + Trim(ClientDataSet6.fieldbyname('TFID').AsString) + '''');
    Open;
  end;
  sFieldName := 'D:\图片查看';
  if not DirectoryExists(pchar(sFieldName)) then
    CreateDirectory(pchar(sFieldName), nil);
  fileName := ClientDataSet6.fieldbyname('FileName').AsString;
  sFieldName := sFieldName + '\' + trim(fileName);
  try
    ff := TADOBlobstream.Create(ADOQueryTemp.fieldByName('FilesOther') as TblobField, bmRead);
    FJStream := TMemoryStream.Create;
    ff.SaveToStream(FJStream);
    UnCompressionStream(FJStream);
    FJStream.SaveToFile(sFieldName);
    Panel4.Visible := False;
    ShellExecute(Handle, 'open', PChar(sFieldName), '', '', SW_SHOWNORMAL);
  finally
    Panel4.Visible := False;
    FJStream.free;
    ff.Free;
  end;
end;

procedure TfrmClothSCGYChkInPut.Tv3MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  Fint := 1;
end;

procedure TfrmClothSCGYChkInPut.Tv5MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  Fint := 2;
end;

procedure TfrmClothSCGYChkInPut.GongYiCodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmClothGongYiListSel := TfrmClothGongYiListSel.Create(Application);
    with frmClothGongYiListSel do
    begin
      if ShowModal = 1 then
      begin
        Self.GongYiCode.Text := frmClothGongYiListSel.Order_Main.fieldbyname('GongYiCode').AsString;
        Self.SJNote.Text := frmClothGongYiListSel.Order_Main.fieldbyname('SJNote').AsString;
        Self.C_Spec.Text := frmClothGongYiListSel.Order_Main.fieldbyname('C_Spec').AsString;
        Self.MF.Text := frmClothGongYiListSel.Order_Main.fieldbyname('MF').AsString;
        Self.KZ.Text := frmClothGongYiListSel.Order_Main.fieldbyname('KZ').AsString;
        Self.CarType.Text := frmClothGongYiListSel.Order_Main.fieldbyname('CarType').AsString;
        Self.JTZB.Text := frmClothGongYiListSel.Order_Main.fieldbyname('JTZB').AsString;
        Self.ZhenCun.Text := frmClothGongYiListSel.Order_Main.fieldbyname('ZhenCun').AsString;
      end;
    end;
  finally
    frmClothGongYiListSel.Free;
  end;
  InitGY();

end;

procedure TfrmClothSCGYChkInPut.v2Column11PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin

      if ShowModal = 1 then
      begin
        with Self.CDS_YuanLiao do
        begin
          Edit;
          FieldByName('GYSName').Value := Trim(CDS_HZ.fieldbyname('CoName').AsString);
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

end.

