unit U_OrderInPut;

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, cxCalendar, StrUtils,
  cxDropDownEdit, cxGridBandedTableView, cxGridDBBandedTableView, cxSplitter,
  RM_Common, RM_Class, RM_e_Xls, RM_System, RM_GridReport, RM_Dataset,
  cxLookAndFeels, cxLookAndFeelPainters, dxSkinsCore, dxSkinsDefaultPainters,
  dxSkinscxPCPainter, cxNavigator;

type
  TfrmOrderInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    OrderNo: TEdit;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    Label10: TLabel;
    Label3: TLabel;
    Label14: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    OrdDate: TDateTimePicker;
    MPRTMF: TBtnEditC;
    MPRTKZ: TBtnEditC;
    Label2: TLabel;
    MPRTCodeName: TBtnEditC;
    Label6: TLabel;
    MPRTSpec: TBtnEditC;
    Label25: TLabel;
    DLYDate: TDateTimePicker;
    Label41: TLabel;
    LiDanPerson: TBtnEditC;
    Label43: TLabel;
    YWY: TBtnEditC;
    CustomerNoName: TEdit;
    Label29: TLabel;
    note: TBtnEditC;
    cxGrid4: TcxGrid;
    TV4: TcxGridDBBandedTableView;
    cxGridDBBandedColumn1: TcxGridDBBandedColumn;
    cxGridDBBandedColumn2: TcxGridDBBandedColumn;
    cxGridDBBandedColumn3: TcxGridDBBandedColumn;
    V4FPType: TcxGridDBBandedColumn;
    V4OtherMoney: TcxGridDBBandedColumn;
    V4Note: TcxGridDBBandedColumn;
    cxGridLevel1: TcxGridLevel;
    Button5: TButton;
    DataSource4: TDataSource;
    Order_RLDJ: TClientDataSet;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    V4SKDate: TcxGridDBBandedColumn;
    V4SKMoney: TcxGridDBBandedColumn;
    V4BanKFee: TcxGridDBBandedColumn;
    V4BZType: TcxGridDBBandedColumn;
    V4WRLMoney: TcxGridDBBandedColumn;
    V4WYHMoney: TcxGridDBBandedColumn;
    cxStyle3: TcxStyle;
    ADOQueryCmd: TADOQuery;
    ConNO: TBtnEditC;
    Label4: TLabel;
    Label5: TLabel;
    JGFactoryName: TBtnEditC;
    Panel1: TPanel;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    TYPTP: TToolButton;
    ToolButton3: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    v1StyleNo: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1PRTColorEng: TcxGridDBColumn;
    v1PRTHX: TcxGridDBColumn;
    v1HXTuGao: TcxGridDBColumn;
    v1PRTColorNo: TcxGridDBColumn;
    v1OrdColQty: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1OrdNote: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel2: TPanel;
    ToolBar3: TToolBar;
    TAdd: TToolButton;
    TDel: TToolButton;
    DataSource2: TDataSource;
    Order_GX: TClientDataSet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    TV2PSXH: TcxGridDBColumn;
    TV2PSName: TcxGridDBColumn;
    v2PSSH: TcxGridDBColumn;
    TV2PSNote: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolBar4: TToolBar;
    ToolButton8: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    v3Column8: TcxGridDBColumn;
    Tv3Column1: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    v3Column7: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    DataSource3: TDataSource;
    CDS_JGC: TClientDataSet;
    cxSplitter1: TcxSplitter;
    CDS_Print: TClientDataSet;
    ADOQueryTemp: TADOQuery;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    v3TOGYSName: TcxGridDBColumn;
    RMDBMain: TRMDBDataSet;
    TKCTX: TToolButton;
    ToolButton6: TToolButton;
    v3PSCodeName: TcxGridDBColumn;
    Label9: TLabel;
    BPPiZhong: TBtnEditC;
    Label16: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    MPRTYaoFeng: TBtnEditC;
    MPRTTangJin: TBtnEditC;
    Label22: TLabel;
    GanKou: TBtnEditC;
    Label23: TLabel;
    KHConNO: TEdit;
    MPRTBZNote: TMemo;
    MPRTTeBieNote: TMemo;
    MPRTZuRanXing: TMemo;
    MPRTCode: TEdit;
    Label11: TLabel;
    Label15: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    MPRTMFNOte: TBtnEditC;
    MPRTKZNOte: TBtnEditC;
    MPRTChuanY: TBtnEditC;
    MPRTKaiJian: TBtnEditC;
    Label7: TLabel;
    OrdDefNote2: TMemo;
    v1PBFK: TcxGridDBColumn;
    v1PBSpec: TcxGridDBColumn;
    v1PBKZ: TcxGridDBColumn;
    v1PRTGY: TcxGridDBColumn;
    v1ZXChiCun: TcxGridDBColumn;
    v1PBQty: TcxGridDBColumn;
    v1CJNote: TcxGridDBColumn;
    v1PBQtyUnit: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1PRTColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CustomerNoNameBtnDnClick(Sender: TObject);
    procedure MPRTCodeNameBtnUpClick(Sender: TObject);
    procedure NoteDblClick(Sender: TObject);
    procedure v1OrderUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure OrdDefStr2BtnUpClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure YCLFactoryBtnUpClick(Sender: TObject);
    procedure MPRTSCTeBieNoteBtnUpClick(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure v1HXTuGaoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1SKTuGaoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ConNOBtnUpClick(Sender: TObject);
    procedure v1PRTHXPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure JGFactoryNameBtnUpClick(Sender: TObject);
    procedure v1OrdColQtyPropertiesEditValueChanged(Sender: TObject);
    procedure TYPTPClick(Sender: TObject);
    procedure v1OrdMSQtyPropertiesEditValueChanged(Sender: TObject);
    procedure TAddClick(Sender: TObject);
    procedure Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure TDelClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure TV2PSNotePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v3Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v3TOGYSNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TKCTXClick(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure v3PSCodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure MPRTBZNoteDblClick(Sender: TObject);
    procedure MPRTTeBieNoteDblClick(Sender: TObject);
    procedure MPRTZuRanXingDblClick(Sender: TObject);
    procedure v1PBFKPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1PBSpecPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1PBKZPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1PRTGYPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1PBQtyUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function SaveData(): Boolean;
    function SaveDataSubOne(): Boolean;
    function SaveDataMain(): Boolean;
    function SaveDataSubMore(): Boolean;
    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FMainId, FFMainId, FOrderNo, ConSubId: string;
    FXS: Integer;
    canshu1: string;
    { Public declarations }
  end;

var
  frmOrderInPut: TfrmOrderInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_Fun, U_ZDYHelpSel, U_ContractListSel, U_FileUp_TuGao,
  U_ZdyAttachment, U_ZdyAttachKH, U_CPManageCX, U_Order_KCTX, U_FjList_RZ;

{$R *.dfm}

procedure TfrmOrderInPut.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('指示单录入AA', Tv1, '生产指示单管理');
end;

procedure TfrmOrderInPut.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from JYOrder_Sub where 1=1 ');
    if PState = 1 then
    begin
      sql.Add(' and MainId=''' + Trim(FMainId) + '''');
    end;
    if PState = 0 then
    begin
      sql.Add(' and 1=2');
    end;
    Open;
  end;
  SCreateCDS20(ADOQuery1, Order_Sub);
  SInitCDSData20(ADOQuery1, Order_Sub);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main where MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCSHDataNew(ADOQuery1, ScrollBox1, 2);
  ConSubId := Trim(ADOQuery1.fieldbyname('ConSubId').AsString);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Process where MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryCmd, Order_GX);
  SInitCDSData20(ADOQueryCmd, Order_GX);
  if PState = 0 then
  begin
    OrdDate.DateTime := SGetServerDateTime(ADOTemp);
    DlyDate.DateTime := OrdDate.DateTime;
  end
  else
  begin
  end;
  if CopyInt = 99 then
  begin
    PState := 0;
    FMainId := '';
    OrderNo.Text := '';
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value := '';
        FieldByName('SubId').Value := '';
        Post;
        Next;
      end;
    end;
    with Order_GX do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value := '';
        FieldByName('PSId').Value := '';
        Post;
        Next;
      end;
    end;
  end;
end;

procedure TfrmOrderInPut.ZDYHelp(FButn: TcxButtonEdit; LType: string);
var
  FType, ZDYName, FText: string;
begin
end;

procedure TfrmOrderInPut.FormShow(Sender: TObject);
begin
  readCxGrid('指示单录入AA', Tv1, '生产指示单管理');

  YWY.Text := Trim(DName);
  InitData();
end;

function TfrmOrderInPut.SaveData(): Boolean;
var
  maxno, maxsubno: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'JM', 'JYOrder_Main', 3, 1) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
    end
    else
    begin
      maxno := Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from JYOrder_Main where MainId=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId) = '' then
      begin
        Append;
        FieldByName('OrdFlag1').Value := 0;
        FieldByName('ChkStatus').Value := '';
      end
      else
      begin
        Edit;
      end;
      FieldByName('MainId').Value := Trim(maxno);
      fieldbyname('fillCode').Value := trim(DCode);
      SSetsaveSqlNew(ADOCmd, 'JYOrder_Main', ScrollBox1, 2);
      if Trim(FMainId) = '' then
      begin
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      FieldByName('ConSubId').Value := Trim(ConSubId);
      Post;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main where orderno=''' + Trim(OrderNo.Text) + '''');
      Open;
    end;
    if ADOCmd.RecordCount > 1 then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('订单号重复!', '提示', 0);
      Exit;
    end;
    FMainId := Trim(maxno);
    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('SubId').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, maxno, 'JS', 'JYOrder_Sub', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(Order_Sub.fieldbyname('SubId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_Sub where MainId=''' + Trim(FMainId) + '''');
          sql.Add(' and SubId=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('SubId').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('MainId').Value := Trim(FMainId);
          FieldByName('SubId').Value := Trim(maxno);
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'JYOrder_Sub', 0);
          fieldbyname('OrdColQty').Value := Order_Sub.fieldbyname('OrdColQty').AsFloat;
          fieldbyname('PBQty').Value := Order_Sub.fieldbyname('PBQty').AsFloat;
          if Trim(fieldbyname('PRTOrderQty').AsString) = '' then
          begin
            fieldbyname('PRTOrderQty').Value := 0
          end;
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SubId').Value := Trim(maxno);
        Order_Sub.FieldByName('MainId').Value := Trim(FMainId);
        Next;
      end;
    end;
    ///保存流程表
    with Order_GX do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_GX.fieldbyname('PSId').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, maxsubno, 'PS', 'JYOrder_Process', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取流程流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxsubno := Trim(Order_GX.fieldbyname('PSId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_Process where PSId=''' + Trim(maxsubno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_GX.fieldbyname('PSId').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('MainId').Value := Trim(FMainId);
          FieldByName('PSId').Value := Trim(maxsubno);
          RTSetSaveDataCDS(ADOCmd, Tv2, Order_GX, 'JYOrder_Process', 0);
          Post;
        end;
        Order_GX.Edit;
        Order_GX.FieldByName('PSId').Value := Trim(maxsubno);
        Order_GX.FieldByName('MainId').Value := Trim(FMainId);
        //Order_Sub.Post;
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    ;
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

function TfrmOrderInPut.SaveDataMain(): Boolean;
var
  maxno: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from JYOrder_Main where MainId=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId) = '' then
      begin
        Append;
        maxno := Trim(FFMainId);
      end
      else
      begin
        maxno := Trim(FMainId);
        Edit;
      end;
      FieldByName('MainId').Value := Trim(maxno);
      SSetsaveSqlNew(ADOCmd, 'JYOrder_Main', ScrollBox1, 2);
      if PState = 1 then
      begin
        FieldByName('OrdUpDate').Value := SGetServerDateTime(ADOTemp);
      end;
      if Trim(FMainId) = '' then
      begin
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      Post;
    end;
    FMainId := Trim(maxno);
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

function TfrmOrderInPut.SaveDataSubOne(): Boolean;
var
  maxno: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存子表
    with Order_Sub do
    begin
      //First;
      //while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('SubId').AsString) = '' then
        begin
          if GetLSNo(ADOTemp, maxno, 'JS', 'JYOrder_Sub', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(Order_Sub.fieldbyname('SubId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_Sub where MainId=''' + Trim(FMainId) + '''');
          sql.Add(' and SubId=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('SubId').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('MainId').Value := Trim(FMainId);
          FieldByName('SubId').Value := Trim(maxno);
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'JYOrder_Sub', 0);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SubId').Value := Trim(maxno);
        //Order_Sub.Post;
        //Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！', '提示', 0);
  end;
end;

function TfrmOrderInPut.SaveDataSubMore(): Boolean;
var
  maxno: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('SubId').AsString) = '' then
        begin
          if GetLSNo(ADOTemp, maxno, 'JS', 'JYOrder_Sub', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(Order_Sub.fieldbyname('SubId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_Sub where MainId=''' + Trim(FMainId) + '''');
          sql.Add(' and SubId=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('SubId').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('MainId').Value := Trim(FMainId);
          FieldByName('SubId').Value := Trim(maxno);
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'JYOrder_Sub', 0);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SubId').Value := Trim(maxno);
        //Order_Sub.Post;
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！', '提示', 0);
  end;
end;

procedure TfrmOrderInPut.TBSaveClick(Sender: TObject);
begin
  OrdDate.SetFocus;
  if Trim(OrderNo.Text) = '' then
  begin
    Application.MessageBox('订单号不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(CustomerNOName.Text) = '' then
  begin
    Application.MessageBox('客户不能为空！', '提示', 0);
    Exit;
  end;
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空!', '提示', 0);
    Exit;
  end;

  if Order_Sub.Locate('OrderUnit', Null, []) = true then
  begin
    application.MessageBox('数量单位不能为空', '提示', 0);
    exit;
  end;

  if Trim(ConSubId) = '' then
  begin
    Application.MessageBox('没有选择合同!', '提示', 0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmOrderInPut.v1PRTColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OrdColor';
      flagname := '颜色';
      V1Name.Caption := '中文名称';
      V1Note.Caption := '英文名称';
      MainType := Trim(DName);
      fnote := True;
      if ShowModal = 1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PRTColor').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.Order_Sub.FieldByName('PRTColorEng').Value := Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.ToolButton1Click(Sender: TObject);
var
  i: Integer;
begin
  if Trim(OrderNo.Text) = '' then
  begin
    Application.MessageBox('订单号不能为空!', '提示', 0);
    Exit;
  end;
  i := Order_Sub.RecordCount;
  i := i + 1;
  CopyAddRow(Tv1, Order_Sub);
  with Order_Sub do
  begin
    Edit;
    FieldByName('XHNO').Value := IntToStr(i);
    if i < 9 then
      FieldByName('PRTColorNo').Value := '0' + Trim(IntToStr(i))
    else
      FieldByName('PRTColorNo').Value := Trim(IntToStr(i));
    FieldByName('PRTColor').Value := '';
    FieldByName('PRTColorEng').Value := '';
    FieldByName('PRTOrderQty').Value := null;
    fieldbyname('HXTuGao').value := NULL;
    Post;
  end;
end;

procedure TfrmOrderInPut.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  if Trim(Order_Sub.fieldbyname('SubId').AsString) <> '' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_SXPB_CR where OrdSubidRK=''' + Trim(Order_Sub.fieldbyname('SubId').AsString) + '''');
      Open;
    end;
    if ADOTemp.IsEmpty = False then
    begin
      Application.MessageBox('仓库已选择此明细,不能删除数据!', '提示', 0);
      Exit;
    end;
  end;
  if Trim(Order_Sub.fieldbyname('SubId').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Sub where SubId=''' + Trim(Order_Sub.fieldbyname('SubId').AsString) + '''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
 { if Order_Sub.IsEmpty then
  begin
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Main where MainId='''+Trim(FMainId)+'''');
      ExecSQL;
    end;
  end;  }
end;

procedure TfrmOrderInPut.CustomerNoNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmOrderInPut.MPRTCodeNameBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTCodeName';
      flagname := '产品名称';
      fnote := true;
      V1Note.Caption := '编号';
      if ShowModal = 1 then
      begin
        MPRTCodeName.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        MPRTCode.Text := Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.NoteDblClick(Sender: TObject);
begin
  {try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='OrdNote';
      flagname:='备注及要求';
      if ShowModal=1 then
      begin
        Note.Text:=Note.Text+frmZDYHelpSel.ReturnStr;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;}
end;

procedure TfrmOrderInPut.v1OrderUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OrderUnit';
      flagname := '数量单位';
      if ShowModal = 1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('OrderUnit').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PriceUnit';
      flagname := '价格单位';
      if Trim(DParameters1) <> '高权限' then
      begin
        TBAdd.Visible := False;
        TBEdit.Visible := False;
        TBDel.Visible := False;
      end;

      if ShowModal = 1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PriceUnit').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.OrdDefStr2BtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if Trim(flag) = 'OrdDefStr2' then
      begin
        //flag:='OrdDefStr2';
        V1Name.Caption := '中文名称';
        V1Note.Caption := '英文名称';
        fnote := True;
      end;
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        if Trim(flag) = 'MPRTCode' then
        begin
          with ADOTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select Top 1 * from JYOrder_Main where MPRTCode=''' + Trim(ClientDataSet1.fieldbyname('ZDYName').AsString) + '''');
            sql.Add(' order by FillTime desc');
            Open;
          end;
          MPRTCodeName.Text := ADOTemp.fieldbyname('MPRTCodeName').Value;
          MPRTMF.Text := ADOTemp.fieldbyname('MPRTMF').Value;
          MPRTKZ.Text := ADOTemp.fieldbyname('MPRTKZ').Value;
          MPRTSpec.Text := ADOTemp.fieldbyname('MPRTSpec').Value;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.ToolButton3Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  OneKeyPost(Tv1, Order_Sub);
end;

procedure TfrmOrderInPut.YCLFactoryBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      MainType := TEdit(Sender).Name;
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.MPRTSCTeBieNoteBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := ReturnStr;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPut.Button5Click(Sender: TObject);
var
  maxsubno: string;
  FRLMoney, FSXMoney, FOtherMoney: double;
begin
  with Order_RLDJ do
  begin
    First;
    while not Eof do
    begin
      if Trim(Order_RLDJ.fieldbyname('FPId').AsString) = '' then
      begin
        if GetLSNo(ADOTemp, maxsubno, 'FP', 'SK_Money_FP', 3, 1) = False then
        begin
          Application.MessageBox('取子流水号失败！', '提示', 0);
          Exit;
        end;
      end
      else
      begin
        maxsubno := Trim(Order_RLDJ.fieldbyname('FPId').AsString);
      end;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from SK_Money_FP where FPId=''' + Trim(maxsubno) + '''');
        Open;
      end;
      with ADOQueryCmd do
      begin
        if Trim(Order_RLDJ.fieldbyname('FPId').AsString) = '' then
        begin
          Append;
          FieldByName('Filler').Value := Trim(DName);
        end
        else
        begin
          Edit;
          FieldByName('Editer').Value := Trim(DName);
          FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
        end;
        FieldByName('FPId').Value := Trim(maxsubno);
        FieldByName('YFID').Value := Trim(Order_RLDJ.fieldbyname('YFID').AsString);
        FieldByName('FPTYpe').Value := Order_RLDJ.fieldbyname('FPTYpe').Value;
        FieldByName('FPDate').Value := Order_RLDJ.fieldbyname('FPDate').Value;
        FieldByName('WBID').Value := Trim(Order_RLDJ.fieldbyname('WBID').AsString);  //指示单关联字段
        FieldByName('TTID').Value := Trim(Order_RLDJ.fieldbyname('TTID').AsString);
        fieldbyname('FPMoney').Value := Order_RLDJ.fieldbyname('FPMoney').AsFloat;
        fieldbyname('SXMoney').Value := Order_RLDJ.fieldbyname('SXMoney').AsFloat;
        fieldbyname('OtherMoney').Value := Order_RLDJ.fieldbyname('OtherMoney').AsFloat;
        FieldByName('Note').Value := Trim(Order_RLDJ.fieldbyname('Note').AsString);
        Post;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPdate JYOrder_Main Set DJMoney=(select Sum(FPMoney) from SK_Money_FP A ');
        sql.Add(' where A.WBID=JYOrder_Main.MainID and A.FPType=''定金'') ');
        sql.Add(',HKMoney=(select Sum(FPMoney) from SK_Money_FP A ');
        sql.Add(' where A.WBID=JYOrder_Main.MainID and A.FPType=''货款'') ');
        sql.Add(',SXMoney=(select Sum(SXMoney) from SK_Money_FP A ');
        sql.Add(' where A.WBID=JYOrder_Main.MainID) ');
        sql.add(',JYOtherMoney=(select Sum(OtherMoney) from SK_Money_FP A ');
        sql.Add(' where A.WBID=JYOrder_Main.MainID)');
        sql.add(',SCHuiLv=(Case when (select Top 1 C.HuiLv from YF_Money_CR C inner join SK_Money_FP A on C.YFID=A.YFID ');
        sql.add(' where A.WBID=JYOrder_Main.MainID and C.CRType=''收款登记'' and C.BZType=''$'' Order by A.FPDate desc)<>0 then ');
        sql.add(' (select Top 1 C.HuiLv from YF_Money_CR C inner join SK_Money_FP A on C.YFID=A.YFID ');
        sql.add(' where A.WBID=JYOrder_Main.MainID and C.CRType=''收款登记'' and C.BZType=''$'' Order by A.FPDate desc)*1.17-0.35 else NULL end)');
        sql.Add(' where MainID=''' + Trim(Order_RLDJ.fieldbyname('WBID').AsString) + '''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPdate YF_Money_CR Set FPDJMoney=(select Sum(FPMoney) from SK_Money_FP A ');
        sql.Add(' where A.YFID=YF_Money_CR.YFID and A.FPType=''定金''),');
        sql.Add('YSXMoney=(select Sum(SXMoney) from SK_Money_FP A ');
        sql.Add(' where A.YFID=YF_Money_CR.YFID), ');
        sql.Add('TKMoney=(select Sum(OtherMoney) from SK_Money_FP A ');
        sql.Add(' where A.YFID=YF_Money_CR.YFID) ');
        sql.Add('where YFID=''' + Trim(Order_RLDJ.fieldbyname('YFID').AsString) + '''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPdate YF_Money_CR Set FPHKMoney=(select Sum(FPMoney) from SK_Money_FP A ');
        sql.Add(' where A.YFID=YF_Money_CR.YFID and A.FPType=''货款'') ');
        sql.Add('where YFID=''' + Trim(Order_RLDJ.fieldbyname('YFID').AsString) + '''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YF_Money_CR Set WFPMoney=isnull(RZMoney,0)-isnull(FPDJMoney,0)-isnull(FPHKMoney,0)-isnull(TKMoney,0) ');
        sql.Add('where YFID=''' + Trim(Order_RLDJ.fieldbyname('YFID').AsString) + '''');
        ExecSQL;
      end;
      Order_RLDJ.Edit;
      Order_RLDJ.FieldByName('FPId').Value := Trim(maxsubno);
      Next;
    end;
  end;
end;

procedure TfrmOrderInPut.v1HXTuGaoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmFileUp_TuGao := TfrmFileUp_TuGao.Create(Application);
    with frmFileUp_TuGao do
    begin
      Code.Text := Trim(Self.OrderNo.Text) + Trim(Self.Order_Sub.fieldbyname('StyleNo').AsString) + Trim(Self.Order_Sub.fieldbyname('PRTHX').AsString);
      CYID := Trim(Self.Order_Sub.fieldbyname('HXTuGao').AsString);
      if ShowModal = 1 then
      begin
        with Order_Sub do
        begin
          edit;
          fieldbyname('HXTuGao').AsString := Trim(ADOQueryFile.fieldbyname('CYID').asstring);
        end;
      end;
    end;
  finally
    frmFileUp_TuGao.Free;
  end;
end;

procedure TfrmOrderInPut.v1SKTuGaoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmFileUp_TuGao := TfrmFileUp_TuGao.Create(Application);
    with frmFileUp_TuGao do
    begin
      Code.Text := Trim(Self.Order_Sub.fieldbyname('SOrddefstr2').AsString);
      CYID := Trim(Self.Order_Sub.fieldbyname('SKTuGao').AsString);
      if ShowModal = 1 then
      begin
        with Order_Sub do
        begin
          edit;
          fieldbyname('SKTuGao').AsString := Trim(ADOQueryFile.fieldbyname('CYID').asstring);
        end;
      end;
    end;
  finally
    frmFileUp_TuGao.Free;
  end;
end;

procedure TfrmOrderInPut.ConNOBtnUpClick(Sender: TObject);
begin
  try
    frmContractListSel := TfrmContractListSel.Create(Application);
    with frmContractListSel do
    begin
      canshu1 := Self.canshu1;
      if ShowModal = 1 then
      begin

        ConSubId := Trim(CDS_Sub.fieldbyname('SubId').AsString);
        Self.ConNo.Text := Trim(Order_Main.fieldbyname('ConNo').AsString);
        Self.OrderNo.Text := Trim(Order_Main.fieldbyname('ConNo').AsString) + '-1';
        Self.CustomerNoName.Text := Trim(Order_Main.fieldbyname('CustomerNoName').AsString);
        Self.YWY.Text := Trim(Order_Main.fieldbyname('ConPerson1').AsString);
        MPRTCodeName.Text := Trim(CDS_Sub.fieldbyname('PRTCodeName').AsString);
        MPRTMF.Text := Trim(CDS_Sub.fieldbyname('PRTMF').AsString);
        MPRTKZ.Text := Trim(CDS_Sub.fieldbyname('PRTKZ').AsString);
        MPRTSpec.Text := Trim(CDS_Sub.fieldbyname('PRTspec').AsString);
        self.KHConNO.Text := Trim(Order_Main.fieldbyname('KHConNO').AsString);
        self.OrdDefNote2.Text := Trim(Order_Main.fieldbyname('condefNote2').AsString);
        self.note.Text := Trim(Order_Main.fieldbyname('condefNote4').AsString);
        with CDS_Sub do
        begin
          First;
          while not eof do
          begin
            if FieldByName('Ssel').AsBoolean = true then
            begin
              with Self.Order_Sub do
              begin
                append;
                FieldByName('PRTColor').Value := Trim(CDS_Sub.fieldbyname('PRTColor').AsString);
                FieldByName('PRTColorEng').Value := Trim(CDS_Sub.fieldbyname('SOrdDefStr4').AsString);
                FieldByName('PRTOrderQty').Value := CDS_Sub.fieldbyname('PRTOrderQty').AsFloat;
                FieldByName('OrderUnit').Value := Trim(CDS_Sub.fieldbyname('OrderUnit').AsString);
                FieldByName('PRTColorNo').Value := Trim(CDS_Sub.fieldbyname('PRTColorNo').AsString);
                FieldByName('StyleNo').Value := Trim(CDS_Sub.fieldbyname('StyleNo').AsString);
                FieldByName('PRTHX').Value := Trim(CDS_Sub.fieldbyname('PRTHX').AsString);
                FieldByName('ZXChiCun').Value := CDS_Sub.fieldbyname('ZXChiCun').AsString;
                FieldByName('OrdColQty').Value := CDS_Sub.fieldbyname('MXTQty').AsFloat;
                post;
              end;
            end;
            next;
          end;
        end;
      end;
    end;
  finally
    frmContractListSel.Free;
  end;
end;

procedure TfrmOrderInPut.v1PRTHXPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PRTHX';
      flagname := '花型';
      if ShowModal = 1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PRTHX').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.JGFactoryNameBtnUpClick(Sender: TObject);
begin
  try
    frmZdyAttachKH := TfrmZdyAttachKH.Create(Application);
    with frmZdyAttachKH do
    begin
      if ShowModal = 1 then
      begin
        JGFactoryName.Text := Trim(CDS_HZ.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZdyAttachKH.Free;
  end;
end;

procedure TfrmOrderInPut.v1OrdColQtyPropertiesEditValueChanged(Sender: TObject);
var
  Fieldname: string;
  mavlue: string;
begin
  Fieldname := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  mavlue := TcxTextEdit(Sender).EditingText;
  if mavlue = '' then
    mavlue := '0';
  with Order_Sub do
  begin
    edit;
    fieldbyname('OrdColQty').AsString := trim(mavlue);
  end;
  with Order_Sub do
  begin
    edit;
    FieldByName('PRTOrderQty').Value := FieldByName('OrdColQty').AsFloat * FieldByName('OrdMSQty').AsFloat;
  end;
end;

procedure TfrmOrderInPut.TYPTPClick(Sender: TObject);
begin
  if Trim(OrderNo.Text) = '' then
  begin
    Application.MessageBox('订单号不能为空!', '提示', 0);
    Exit;
  end;
  frmCPManage_CX := TfrmCPManage_CX.create(self);
  with frmCPManage_CX do
  begin
    if showmodal = 1 then
    begin
      CDS_Main.DisableControls;
      with CDS_Main do
      begin
        first;
        while not Eof do
        begin
          if FieldByName('Ssel').AsBoolean = true then
          begin
            with Self.Order_Sub do
            begin
              append;
              FieldByName('PRTColor').Value := Trim(CDS_Main.fieldbyname('CYColor').AsString);
              FieldByName('PRTColorEng').Value := Trim(CDS_Main.fieldbyname('CYColorEng').AsString);
              FieldByName('PRTHX').Value := Trim(CDS_Main.fieldbyname('CYHX').AsString);
              FieldByName('HXTuGao').Value := Trim(CDS_Main.fieldbyname('CYID').AsString);
              post;
            end;
          end;
          next;
        end;
      end;
      CDS_Main.DisableControls;
    end;
    free;
  end;
end;

procedure TfrmOrderInPut.v1OrdMSQtyPropertiesEditValueChanged(Sender: TObject);
var
  Fieldname: string;
  mavlue: string;
begin
  Fieldname := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  mavlue := TcxTextEdit(Sender).EditingText;
  if mavlue = '' then
    mavlue := '0';
  with Order_Sub do
  begin
    edit;
    fieldbyname('OrdMSQty').AsString := trim(mavlue);
  end;
  with Order_Sub do
  begin
    edit;
    FieldByName('PRTOrderQty').Value := FieldByName('OrdColQty').AsFloat * FieldByName('OrdMSQty').AsFloat;
  end;
end;

procedure TfrmOrderInPut.TAddClick(Sender: TObject);
var
  Fint: Integer;
  FReal, FMF, FKZ: Double;
begin
  Fint := Order_GX.RecordCount;
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := 'FromGX';
      flagname := '加工工序';
      if ShowModal = 1 then
      begin
        with ClientDataSet1 do
        begin
          First;
          while not eof do
          begin
            if ClientDataSet1.FieldByName('SSel').AsBoolean = True then
            begin
              with Order_GX do
              begin
                Append;
                FieldByName('PSXH').Value := Fint + 1;
                FieldByName('PSName').Value := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
                Fint := Fint + 1;
                Post;
              end;
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPut.Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.PRTHX,PRTColor=isnull(B.PRTColorNo,'''')+'' ''+isnull(B.PRTColorEng,'''')+'' ''+isnull(B.PRTColor,''''), ');
    sql.Add(' B.PRTOrderQty,B.OrderUnit from JYOrder_PCS_Sub A');
    sql.Add(' left join JYOrder_Sub B on A.SubId=B.SubId');
    sql.Add('where A.PSID=''' + Trim(Order_GX.fieldbyname('PSID').AsString) + '''');
    Open;
  end;
  SCreateCDS20(ADOCmd, CDS_JGC);
  SInitCDSData20(ADOCmd, CDS_JGC);
end;

procedure TfrmOrderInPut.TDelClick(Sender: TObject);
begin
  if Order_GX.IsEmpty then
    exit;
  if CDS_JGC.IsEmpty = False then
  begin
    Application.MessageBox('已安排不能删除数据!', '提示', 0);
    Exit;
  end;
  if Trim(Order_GX.fieldbyname('PSId').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Process where PSId=''' + Trim(Order_GX.fieldbyname('PSId').AsString) + '''');
      ExecSQL;
    end;
  end;
  Order_GX.Delete;
end;

procedure TfrmOrderInPut.ToolButton4Click(Sender: TObject);
begin
  if Order_GX.IsEmpty then
  begin
    Application.MessageBox('加工流程不能为空!', '提示', 0);
    Exit;
  end;
  if Trim(Order_GX.fieldbyname('PSID').AsString) = '' then
  begin
    Application.MessageBox('加工流程信息未保存!', '提示', 0);
    Exit;
  end;
  try
    frmZdyAttachKH := TfrmZdyAttachKH.Create(Application);
    with frmZdyAttachKH do
    begin
      if ShowModal = 1 then
      begin
        with CDS_JGC do
        begin
          Append;
          FieldByName('FactoryNo').Value := Trim(CDS_HZ.fieldbyname('ATID').AsString);
          FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('ZdyName').AsString);
          FieldByName('PFDate').Value := Now;
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachKH.Free;
  end;
end;

procedure TfrmOrderInPut.ToolButton5Click(Sender: TObject);
begin
  if CDS_JGC.IsEmpty then
    Exit;
  if Trim(CDS_JGC.fieldbyname('PFId').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_PCS_Sub where PFId=''' + Trim(CDS_JGC.fieldbyname('PFId').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_JGC.Delete;
end;

procedure TfrmOrderInPut.ToolButton8Click(Sender: TObject);
var
  maxno: string;
begin
  if CDS_JGC.IsEmpty then
    Exit;
  ToolBar1.SetFocus;
  try
    ADOCmd.Connection.BeginTrans;
    CDS_JGC.DisableControls;
    with CDS_JGC do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_JGC.fieldbyname('PFID').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, maxno, 'PF', 'JYOrder_PCS_Sub', 3, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            CDS_JGC.EnableControls;
            Application.MessageBox('取流程加工厂流水号失败!', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(CDS_JGC.fieldbyname('PFID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_PCS_Sub where PFID=''' + Trim(CDS_JGC.fieldbyname('PFID').AsString) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_JGC.fieldbyname('PFID').AsString) = '' then
          begin
            Append;
          end
          else
          begin
            Edit;
          end;
          FieldByName('MainID').Value := Trim(FMainId);
          FieldByName('PSID').Value := Trim(Order_GX.fieldbyname('PSID').AsString);
          FieldByName('SubID').Value := Trim(CDS_JGC.fieldbyname('SubID').AsString);
          FieldByName('PFID').Value := Trim(maxno);
          FieldByName('FactoryNo').Value := Trim(CDS_JGC.fieldbyname('FactoryNo').AsString);
          FieldByName('FactoryName').Value := Trim(CDS_JGC.fieldbyname('FactoryName').AsString);
          FieldByName('PFDlyDate').Value := CDS_JGC.fieldbyname('PFDlyDate').Value;
          RTSetSaveDataCDS(ADOCmd, Tv3, CDS_JGC, 'JYOrder_PCS_Sub', 0);
          FieldByName('PFTPPS').Value := CDS_JGC.fieldbyname('PFTPPS').AsFloat;
          FieldByName('PFTPQty').Value := CDS_JGC.fieldbyname('PFTPQty').AsFloat;
          Post;
        end;
        with CDS_JGC do
        begin
          edit;
          fieldbyname('PFID').AsString := trim(maxno);
        end;
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    CDS_JGC.EnableControls;
    Application.MessageBox('操作成功!', '提示', 0);
    Exit;
  except
    ADOCmd.Connection.RollbackTrans;
    CDS_JGC.EnableControls;
    Application.MessageBox('操作异常!', '提示', 0);
    Exit;
  end;
end;

procedure TfrmOrderInPut.TV2PSNotePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  fsj: string;
  i: Integer;
begin
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := 'PSNote';
      flagname := '注意品质说明';
      MainType := Trim(Order_GX.fieldbyname('PSName').AsString);
      if ShowModal = 1 then
      begin
        i := 0;
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if ClientDataSet1.FieldByName('SSel').AsBoolean = True then
            begin
              i := i + 1;
              if Trim(fsj) = '' then
              begin
                fsj := fsj + Trim(IntToStr(i)) + ') ' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
              end
              else
              begin
                fsj := fsj + #10 + Trim(IntToStr(i)) + ') ' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
              end;

            end;
            Next;
          end;
        end;
        with Order_GX do
        begin
          Edit;
          FieldByName('PSNote').Value := Trim(fsj);
          //Post;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPut.v3Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PFGenDanPerson';
      flagname := '跟单员';
      fnote := True;
      V1Note.Caption := '电话';
      if ShowModal = 1 then
      begin
        with CDS_JGC do
        begin
          Edit;
          FieldByName('PFGenDanPerson').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.v3TOGYSNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachKH := TfrmZdyAttachKH.Create(Application);
    with frmZdyAttachKH do
    begin
      if ShowModal = 1 then
      begin
        with CDS_JGC do
        begin
          edit;
          FieldByName('TOGYSName').Value := Trim(CDS_HZ.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZdyAttachKH.Free;
  end;
end;

procedure TfrmOrderInPut.TKCTXClick(Sender: TObject);
begin
  if MPRTCodeName.Text = '' then
  begin
    Application.MessageBox('产品名称不能为空', '提示');
    exit;
  end;
  FrmKCTiXing := TFrmKCTiXing.create(self);
  with FrmKCTiXing do
  begin
    SPName.Text := trim(MPRTCodeName.text);
    if ShowModal = 1 then
    begin

    end;
  end;
end;

procedure TfrmOrderInPut.ToolButton6Click(Sender: TObject);
begin
  if ConNo.Text = '' then
  begin
    application.MessageBox('合同号不能为空', '提示');
    exit;
  end;
  try
    frmFjList_RZ := TfrmFjList_RZ.Create(Application);
    with frmFjList_RZ do
    begin
      fkeyNO := Trim(ConNo.Text);
      FType := '合同';
      if ShowModal = 1 then
      begin

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

procedure TfrmOrderInPut.v3PSCodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PSCodeName';
      flagname := '产品名称';
      if ShowModal = 1 then
      begin
        with CDS_JGC do
        begin
          Edit;
          FieldByName('PSCodeName').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.MPRTBZNoteDblClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
  i: integer;
begin
  fsj := Trim(TMemo(Sender).Hint);
  FWZ := Pos('/', fsj);
  i := 0;
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        MPRTBZNote.Lines.Clear;
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSel').AsBoolean = True then
            begin
              i := i + 1;
              MPRTBZNote.Lines.Add(inttostr(i) + '.' + FieldByName('ZDYName').AsString)
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPut.MPRTTeBieNoteDblClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
  i: integer;
begin
  fsj := Trim(TMemo(Sender).Hint);
  FWZ := Pos('/', fsj);
  i := 0;
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        MPRTTeBieNote.Lines.Clear;
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSel').AsBoolean = True then
            begin
              i := i + 1;
              MPRTTeBieNote.Lines.Add(FieldByName('ZDYName').AsString);
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPut.MPRTZuRanXingDblClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
  i: integer;
begin
  fsj := Trim(TMemo(Sender).Hint);
  FWZ := Pos('/', fsj);
  i := 0;
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        MPRTZuRanXing.Lines.Clear;
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSel').AsBoolean = True then
            begin
              MPRTZuRanXing.Lines.Add(FieldByName('ZDYName').AsString);
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPut.v1PBFKPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PBFK';
      flagname := '坯布幅宽';
      if ShowModal = 1 then
      begin
        with CDS_JGC do
        begin
          Edit;
          FieldByName('PBFK').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.v1PBSpecPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PBSpec';
      flagname := '坯布规格';
      if ShowModal = 1 then
      begin
        with CDS_JGC do
        begin
          Edit;
          FieldByName('PBSpec').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.v1PBKZPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PBKZ';
      flagname := '坯布克重';
      if ShowModal = 1 then
      begin
        with CDS_JGC do
        begin
          Edit;
          FieldByName('PBKZ').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    ;
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.v1PRTGYPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PRTGY';
      flagname := '工艺';
      if ShowModal = 1 then
      begin
        with Order_Sub do
        begin
          Edit;
          FieldByName('PRTGY').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    ;
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.v1PBQtyUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PBQtyUnit';
      flagname := '坯布数量单位';
      if ShowModal = 1 then
      begin
        with Order_Sub do
        begin
          Edit;
          FieldByName('PBQtyUnit').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    ;
    frmZDYHelp.Free;
  end;
end;

end.

