unit U_TradePlanMulFtyInPut;

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,
  cxCurrencyEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxDropDownEdit,
  RM_Common, RM_Preview, RM_System, RM_Class, RM_GridReport, RM_Dataset, Menus,
  cxSplitter, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, cxCheckBox,
    dxDateRanges, dxBarBuiltInMenu, cxPC,
  System.ImageList, Vcl.ImgList, U_BaseInput, cxImage, cxDBEdit, IdBaseComponent,
  IdComponent, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase,
  IdFTP, ShellAPI,  dxScrollbarAnnotations;

type
  TfrmTradePlanMulFtyInPut = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    GPM_1: TcxGridPopupMenu;
    ADOQueryLabel: TADOQuery;
    Label51: TLabel;
    CDS_Fact: TClientDataSet;
    DSFact: TDataSource;
    cxgrdpmn2: TcxGridPopupMenu;
    Label7: TLabel;
    Qry_ImgHX: TADOQuery;
    DS_ImgHX: TDataSource;
    ToolButton3: TToolButton;
    IdFTP1: TIdFTP;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column11: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    VC_PRTPs: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolBar2: TToolBar;
    btnConSel: TToolButton;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton5: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Panel1: TPanel;
    ScrollBox1: TScrollBox;
    Label3: TLabel;
    Label25: TLabel;
    Label23: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label27: TLabel;
    Label14: TLabel;
    Label20: TLabel;
    OrderNo: TEdit;
    OrdDate: TDateTimePicker;
    DLYDate: TDateTimePicker;
    OrdNote: TMemo;
    cxDBImage1: TcxDBImage;
    BuyConNo: TEdit;
    C_Name: TEdit;
    C_Spec: TEdit;
    C_Composition: TEdit;
    C_GramWeight: TEdit;
    C_Width: TEdit;
    OrdUnit: TComboBox;
    CustName: TBtnEditC;
    OurCoName: TBtnEditC;
    ConNo: TEdit;
    C_Code: TBtnEditC;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    V2Column2: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    TV2Column1: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    V2Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar3: TToolBar;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CustomerNoNameBtnDnClick(Sender: TObject);
    procedure v1OrderUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure YCLFactoryBtnUpClick(Sender: TObject);
    procedure ScrollBox1Click(Sender: TObject);
    procedure v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
    procedure v1Column5PropertiesEditValueChanged(Sender: TObject);
    procedure HYTTBtnUpClick(Sender: TObject);
    procedure KYTTBtnUpClick(Sender: TObject);
    procedure btnConSelClick(Sender: TObject);
    procedure edtGYBtnUpClick(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure v1OrderUnitPropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxDBImage1DblClick(Sender: TObject);
    procedure SYRNameBtnUpClick(Sender: TObject);
    procedure TV2Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TV2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton3Click(Sender: TObject);
    procedure CustNameBtnUpClick(Sender: TObject);
    procedure OurCoNameBtnUpClick(Sender: TObject);
    procedure OurCoNameBtnDnClick(Sender: TObject);
    procedure V2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton5Click(Sender: TObject);
    procedure C_CodeBtnUpClick(Sender: TObject);
    procedure C_CodeBtnDnClick(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
  private
    FSCXDH: string;
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function SaveData(): Boolean;
    function getOrderNo(fconNo: string): string;
    procedure InitImageHX();
    procedure LookImage(FileName: string);
    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FMainId, FFMainId, FOrderNo: string;
    FXS: Integer;
    canshu1: string;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  frmTradePlanMulFtyInPut: TfrmTradePlanMulFtyInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_TradeSalesContractSel, U_CompanySel,
  U_ClothInfoSel, U_UserSel;


{$R *.dfm}

procedure TfrmTradePlanMulFtyInPut.InitImageHX();
begin
  with Qry_ImgHX do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where isnull(WBID,'''')<>'''' ');
    sql.Add('and WBID=(select HXSID from  SalesContract_Sub X where X.SubId=' + quotedstr(Trim(self.CDS_1.fieldbyname('ConSubId').AsString)) + ')');
    open;
  end;
end;

function TfrmTradePlanMulFtyInPut.getOrderNo(fconNo: string): string;
var
  str1: string;
  i: integer;
begin
  str1 := trim(fconNo) + '01';
  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select MainID from Trade_Plan_Main where orderNo=' + quotedstr(trim(str1)));
    open;
  end;
  if not ADOTemp.IsEmpty then
  begin
    for i := 1 to 99 do
    begin
      str1 := trim(fconNo) + rightstr(inttostr(100 + i), 2);
      with ADOCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select MainID from Trade_Plan_Main where orderNo=' + quotedstr(trim(str1)));
        open;
      end;
      if ADOCmd.IsEmpty then
        break;
    end;
  end;
  result := str1;
end;

procedure TfrmTradePlanMulFtyInPut.TBCloseClick(Sender: TObject);
begin

  Close;

end;

procedure TfrmTradePlanMulFtyInPut.InitData();
var
  FDate: TDateTime;
begin
  FDate := SGetServerDateTime(ADOTemp);
  OrdDate.DateTime := FDate;
  DlyDate.DateTime := FDate;

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

  SCSHData(ADOTemp, ScrollBox1, 2);
  SCSHData(ADOTemp, ScrollBox1, 7);
  OurCoName.TxtCode := Trim(ADOTemp.FieldByName('OurCoNo').AsString);
  CustName.TxtCode := Trim(ADOTemp.FieldByName('CustNo').AsString);

  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select *  from Trade_Plan_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<>1');
    end;
    Open;
  end;
  SCreateCDS(ADOTemp, CDS_1);
  SInitCDSData(ADOTemp, CDS_1);

  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from Trade_Plan_Fty  ');
    sql.Add(' where MainId=''' + Trim(FMainId) + '''');
//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS(ADOTemp, CDS_Fact);
  SInitCDSData(ADOTemp, CDS_Fact);

  if PState = 0 then
  begin
    OrdDate.DateTime := SGetServerDateTime(ADOTemp);
    DlyDate.DateTime := OrdDate.DateTime;
    OrderNo.Text := '';
    OrderNo.Enabled := True;
  end
  else
  begin
  end;
  if CopyInt = 99 then
  begin
    PState := 0;
    FMainId := '';
    OrderNo.Text := '';
    OrderNo.Enabled := True;

    OrdDate.DateTime := FDate;

    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value := '';
        FieldByName('SubId').Value := '';
        Post;
        Next;
      end;
    end;

  end;
end;

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

procedure TfrmTradePlanMulFtyInPut.FormShow(Sender: TObject);
var
  i: Integer;
begin
  inherited;
  readCxGrid(TRIM(SELF.Caption), Tv1, '生产指示单管理');
  OrderNo.Text := '';
  fFlileFlag := UserDataFlag + 'HX';
  InitData();
end;

function TfrmTradePlanMulFtyInPut.SaveData(): Boolean;
var
  maxno, Smaxno, maxorderno, MaxFNo: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'JM', 'Trade_Plan_Main', 3, 1) = False then
        raise Exception.Create('取最大号失败!');
      OrderNo.Text := maxno;
    end
    else
    begin
      maxno := Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Trade_Plan_Main where MainId=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId) = '' then
      begin
        Append;
        FieldByName('FillId').Value := Trim(DCode);
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        Edit;
        FieldByName('EditId').Value := Trim(DCode);
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      RTSetsavedata(ADOCmd, 'Trade_Plan_Main', ScrollBox1, 2);
      FieldByName('MainId').Value := Trim(maxno);
      FieldByName('OurCoNo').Value := Trim(OurCoName.TxtCode);
      FieldByName('CustNo').Value := Trim(CustName.TxtCode);
      Post;
    end;

/////////////////////               保存子表            ///////////////////////////
    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_1.fieldbyname('SubId').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, Smaxno, 'JS', 'Trade_Plan_Sub', 4, 1) = False then
            raise Exception.Create('取子流水号失败!');
        end
        else
        begin
          Smaxno := Trim(CDS_1.fieldbyname('SubId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Trade_Plan_Sub where MainId=''' + Trim(maxno) + '''');
          sql.Add(' and SubId=''' + Trim(Smaxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_1.fieldbyname('SubId').AsString) = '' then
            Append
          else
            Edit;
          RTSetSaveDataCDS(ADOCmd, Tv1, CDS_1, 'Trade_Plan_Sub', 0);

          RTSetsavedata(ADOCmd, 'Trade_Plan_Sub', ScrollBox1, 7);
          FieldByName('MainId').Value := Trim(maxno);
          FieldByName('SubId').Value := Trim(Smaxno);
          FieldByName('ConSId').Value := Trim(CDS_1.fieldbyname('ConSId').AsString);
          FieldByName('ConMId').Value := Trim(CDS_1.fieldbyname('ConMId').AsString);
          Post;
          with CDS_1 do
          begin
            Edit;
            fieldbyname('SubId').Value := Trim(Smaxno);
            Post;
          end;

        end;
        Next;
      end;
    end;
/////////////////////               保存子表           ///////////////////////////
/////////////////////               保存加工厂            ///////////////////////////
    with CDS_Fact do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_Fact.fieldbyname('TPFId').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, MaxFNo, 'JF', 'Trade_Plan_Fty', 4, 1) = False then
            raise Exception.Create('取子流水号失败!');
        end
        else
        begin
          MaxFNo := Trim(CDS_Fact.fieldbyname('TPFId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Trade_Plan_Fty where MainId=''' + Trim(maxno) + '''');
          sql.Add(' and TPFId=''' + Trim(MaxFNo) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_Fact.fieldbyname('TPFId').AsString) = '' then
            Append
          else
            Edit;

          if Trim(CDS_Fact.fieldbyname('TPFId').AsString) <> '' then
          begin
            with ADOTemp do
            begin
              Close;
              SQL.Clear;
              sql.Add('select * from Trade_Plan_Fty where MainId=''' + Trim(maxno) + '''');

              Open;
            end;

          end;
          FieldByName('MainId').Value := Trim(maxno);
          FieldByName('TPFId').Value := Trim(MaxFNo);
          RTSetSaveDataCDS(ADOCmd, Tv2, CDS_Fact, 'Trade_Plan_Fty', 0);
          Post;
          with CDS_Fact do
          begin
            Edit;
            fieldbyname('TPFId').Value := Trim(MaxFNo);
            Post;
          end;

        end;
        Next;
      end;
    end;
/////////////////////               保存加工厂           ///////////////////////////


    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Trade_Plan_Main where orderno=''' + Trim(OrderNo.Text) + '''');
      Open;
    end;
    if ADOCmd.RecordCount > 1 then
      raise Exception.Create('订单号重复!');

    FMainId := Trim(maxno);

    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);

    if pos('KEY 约束', Exception(ExceptObject).Message) > 0 then
    begin
      if GetLSNo(ADOCmd, Smaxno, 'JS', 'Trade_Plan_Sub', 4, 1) = False then
      begin
        Application.MessageBox('重新取子流水号失败！', '提示', 0);
        Exit;
      end;
    end;
  end;
end;

procedure TfrmTradePlanMulFtyInPut.TBSaveClick(Sender: TObject);
var
  MAXNO1: string;
begin
  if PState <> 1 then
  begin
    OrdDate.SetFocus;
  end;

  if CDS_1.IsEmpty then
  begin
    Application.MessageBox('明细不能为空!', '提示', 0);
    Exit;
  end;

  if OrdUnit.Text = '' then
  begin
    Application.MessageBox('单位不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_1.Locate('OrdQty', null, []) then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_1.Locate('C_Color', null, []) then
  begin
    Application.MessageBox('颜色不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_Fact.Locate('FtyName', null, []) then
  begin
    Application.MessageBox('工厂名称不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_Fact.Locate('FtyNo', null, []) then
  begin
    Application.MessageBox('工厂编号不能为空!', '提示', 0);
    Exit;
  end;

  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
//    ModalResult := 1;
  end;
end;

procedure TfrmTradePlanMulFtyInPut.ToolButton1Click(Sender: TObject);
var
  i: Integer;
begin

  CopyAddRow(Tv1, CDS_1);
  with CDS_1 do
  begin
    Edit;
    FieldByName('SSel').Value := false;
//    FieldByName('hxFile').Value := '';
//    FieldByName('TPPS').Value := 0;
    Post;
  end;
end;

procedure TfrmTradePlanMulFtyInPut.ToolButton2Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除选择的数据吗？', '提示', 32 + 4) <> IDYES then
    exit;
  CDS_1.DisableControls;
  with CDS_1 do
  begin
    First;
    while Locate('SSel', True, []) do
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from Trade_Cloth_Inspect where isnull(SubId,'''')<>'''' and SubId=''' + Trim(CDS_1.fieldbyname('SubId').AsString) + '''');
        Open;
      end;
      if ADOTemp.IsEmpty = False then
      begin
        Application.MessageBox('已检验不能删除数据!', '提示', 0);
        Exit;
      end;
      if Trim(CDS_1.fieldbyname('SubId').AsString) <> '' then
      begin
        with ADOCmd do
        begin
          Close;
          sql.Clear;

//          sql.Add('insert into Trade_Plan_Sub_Del select * from Trade_Plan_Sub where SubId=''' + Trim(CDS_1.fieldbyname('SubId').AsString) + '''');

          sql.Add('delete Trade_Plan_Sub where SubId=''' + Trim(CDS_1.fieldbyname('SubId').AsString) + '''');

          ExecSQL;
        end;
      end;
      Delete;
    end;
  end;
  CDS_1.EnableControls;

end;

procedure TfrmTradePlanMulFtyInPut.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(TRIM(SELF.Caption), Tv1, '生产指示单管理');

end;

procedure TfrmTradePlanMulFtyInPut.ToolButton5Click(Sender: TObject);
begin
  try
    frmClothInfoSel := TfrmClothInfoSel.Create(Application);
    with frmClothInfoSel do
    begin
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
          if self.CDS_1.locate('ConSubId', trim(CDS_1.Fieldbyname('SubId').AsString), []) then
          begin
            CDS_1.Edit;
            CDS_1.fieldbyname('SSel').value := false;
            continue;
          end;
          with self.CDS_1 do
          begin
            append;
            FieldByName('SSel').Value := false;
            C_Code.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Code').AsString);
            C_Name.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Name').AsString);
            C_Spec.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Spec').AsString);
            C_Composition.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Composition').AsString);
            C_Width.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Width').AsString);
            C_GramWeight.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_GramWeight').AsString);
            OrdUnit.ItemIndex := OrdUnit.Items.IndexOf(trim(frmClothInfoSel.CDS_1.fieldbyname('ConUnit').AsString));

            Fieldbyname('C_ColorNo').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_ColorNo').AsString);
            Fieldbyname('C_Color').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Color').AsString);
            Fieldbyname('C_Pattern').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Pattern').AsString);

            Fieldbyname('OrdQty').value := frmClothInfoSel.CDS_1.Fieldbyname('ConQty').AsFloat;

            Fieldbyname('DDQtyUnit').value := frmClothInfoSel.CDS_1.Fieldbyname('SCSUnit').AsString;
            Fieldbyname('OrderUnit').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('SCSUnit').AsString);

          end;
          CDS_1.Edit;
          CDS_1.fieldbyname('SSel').value := false;
        end;
      end;
    end;
  finally
    frmClothInfoSel.Free;
  end;

end;

procedure TfrmTradePlanMulFtyInPut.ToolButton7Click(Sender: TObject);
var
  i: Integer;
begin
  i := CDS_Fact.RecordCount + 1;
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        CDS_Fact.Append;
        CDS_Fact.FieldByName('SerialNo').Value := i;
        CDS_Fact.FieldByName('FtyNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
        CDS_Fact.FieldByName('FtyName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        CDS_Fact.FieldByName('Follower').Value := Trim(CDS_1.fieldbyname('Follower').AsString);
        CDS_Fact.FieldByName('FollowId').Value := Trim(CDS_1.fieldbyname('FollowId').AsString);
      end;

    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmTradePlanMulFtyInPut.ToolButton8Click(Sender: TObject);
begin
  if CDS_Fact.IsEmpty then
    Exit;

  if Trim(CDS_Fact.fieldbyname('TPFId').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Trade_Plan_Fty where TPFId=''' + Trim(CDS_Fact.fieldbyname('TPFId').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_Fact.Delete;
end;

procedure TfrmTradePlanMulFtyInPut.edtGYBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
  i: integer;
begin
//  fsj := Trim(TBtnEditC(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
//        TBtnEditC(Sender).text := '';
//        with ClientDataSet1 do
//        begin
//          First;
//          while not Eof do
//          begin
//            if FieldByName('SSel').AsBoolean = True then
//            begin
//              i := i + 1;
//              if TBtnEditC(Sender).text = '' then
//                TBtnEditC(Sender).text := FieldByName('ZDYName').AsString
//              else
//                TBtnEditC(Sender).text := TBtnEditC(Sender).text + ',' + FieldByName('ZDYName').AsString;   //inttostr(i)+'.'+
//            end;
//            Next;
//          end;
//        end;
//      end;
//    end;
//  finally
//    frmZDYHelpSel.Free;
//  end;
end;

procedure TfrmTradePlanMulFtyInPut.CustNameBtnUpClick(Sender: TObject);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      if ShowModal = 1 then
      begin
        self.CustName.TxtCode := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.CustName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

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

procedure TfrmTradePlanMulFtyInPut.cxDBImage1DblClick(Sender: TObject);
begin
  if TcxDBImage(Sender).Picture.Height = 0 then
    exit;
  LookImage(Qry_ImgHX.fieldbyname(TcxDBImage(Sender).Hint).asstring);
end;

procedure TfrmTradePlanMulFtyInPut.C_CodeBtnDnClick(Sender: TObject);
begin
  Self.C_Code.Text := '';
  Self.C_Name.Text := '';
  Self.C_Spec.Text := '';
  Self.C_Composition.Text := '';
  Self.C_Width.Text := '';
  Self.C_GramWeight.Text := '';
end;

procedure TfrmTradePlanMulFtyInPut.C_CodeBtnUpClick(Sender: TObject);
begin
  try
    frmClothInfoSel := TfrmClothInfoSel.Create(Application);
    with frmClothInfoSel do
    begin
      if ShowModal = 1 then
      begin
        Self.C_Code.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Code').AsString);
        Self.C_Name.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Name').AsString);
        Self.C_Spec.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Spec').AsString);
        Self.C_Composition.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Composition').AsString);
        Self.C_Width.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Width').AsString);
        Self.C_GramWeight.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_GramWeight').AsString);
      end;
    end;
  finally
    frmClothInfoSel.Free;
  end;

end;

procedure TfrmTradePlanMulFtyInPut.LookImage(FileName: string);
var
  sFieldName: string;
begin

  sFieldName := leftbstr(ExtractFilePath(Application.ExeName), 1) + ':\图片查看';

  if not DirectoryExists(pchar(sFieldName)) then
    CreateDirectory(pchar(sFieldName), nil);

  sFieldName := sFieldName + '\' + trim(FileName);

  try
    IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI', 'SERVER', '服务器地址', '127.0.0.1');
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    ;
  end;

  if IdFTP1.Connected then
  begin
    try
      IdFTP1.Get(fFlileFlag + '\' + Trim(FileName), sFieldName, true, false);
    except
      Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
      IdFTP1.Quit;
      Exit;
    end;
  end
  else
  begin
    Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
    IdFTP1.Quit;
    Exit;
  end;
  if IdFTP1.Connected then
    IdFTP1.Quit;
  ShellExecute(Handle, 'open', PChar(sFieldName), '', '', SW_SHOWNORMAL);
end;

procedure TfrmTradePlanMulFtyInPut.v1OrderUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  mvalues: string;
begin
  mvalues := tv1.Columns[tv1.Controller.FocusedColumn.index].DataBinding.FieldName;
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OrderUnit';
      flagname := '数量单位';
      if ShowModal = 1 then
      begin
        Self.CDS_1.Edit;
        Self.CDS_1.FieldByName(mvalues).Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradePlanMulFtyInPut.v1OrderUnitPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName, YValue: string;
  FDDQty, FCPCDXS, FQty, FDKCQty: Double;
begin
  mvalue := TcxComboBox(Sender).EditingText;                                           //变化后单位
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_1 do
  begin

    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
    YValue := FieldByName('DDQtyUnit').AsString;                                        //原单位=订单单位
    FDDQty := FieldByName('DDQty').AsFloat;                                            //订单数量
    FCPCDXS := FieldByName('CPCDXS').AsFloat;
    FDKCQty := FieldByName('DKCQty').AsFloat;
    FQty := FieldByName('PRTOrderQty').AsFloat;
  end;
  if mvalue = 'Kg' then
  begin
    if YValue = 'Y' then
    begin
      FQty := FDDQty * 0.9144 / FCPCDXS;
    end;
    if YValue = 'M' then
    begin
      FQty := FDDQty / FCPCDXS;
    end;
    if YValue = 'Kg' then
    begin
      FQty := FDDQty;
    end;
  end;

  if mvalue = 'M' then
  begin
    if YValue = 'Y' then
    begin
      FQty := FDDQty * 0.9144;
    end;
    if YValue = 'Kg' then
    begin
      FQty := FDDQty * FCPCDXS;
    end;
    if YValue = 'M' then
    begin
      FQty := FDDQty;
    end;
  end;
  if mvalue = 'Y' then
  begin
    if YValue = 'M' then
    begin
      FQty := FDDQty / 0.9144;
    end;
    if YValue = 'Kg' then
    begin
      FQty := FDDQty * FCPCDXS / 0.9144;
    end;
    if YValue = 'Y' then
    begin
      FQty := FDDQty;
    end;
  end;

  with CDS_1 do
  begin
    Edit;
    FieldByName('PRTOrderQty').Value := FQty - FDKCQty;
    Post;
  end;

  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmTradePlanMulFtyInPut.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.CDS_1.Edit;
        Self.CDS_1.FieldByName('PriceUnit').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradePlanMulFtyInPut.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);
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradePlanMulFtyInPut.btnConSelClick(Sender: TObject);
var
  FConMainId, FMPRTCode: string;
begin
  try
    frmTradeSalesContractSel := TfrmTradeSalesContractSel.Create(Application);
    with frmTradeSalesContractSel do
    begin
      fconNo := self.ConNo.Text;
      fC_Code := self.C_Code.Text;
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
          if self.CDS_1.locate('ConSId', trim(CDS_1.Fieldbyname('ConSId').AsString), []) then
          begin
            CDS_1.Edit;
            CDS_1.fieldbyname('SSel').value := false;
            continue;
          end;

          self.BuyConNo.Text := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('BuyConNo').AsString);
          self.CustName.TxtCode := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('BuyNo').AsString);
          self.CustName.Text := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('BuyName').AsString);

          self.OurCoName.TxtCode := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('Sellno').AsString);
          self.OurCoName.Text := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('Sellname').AsString);

          self.ConNo.Text := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('ConNo').AsString);
          self.C_Code.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_Code').AsString);
          self.C_Name.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_Name').AsString);
          self.C_Spec.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_Spec').AsString);
          self.C_Composition.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_Composition').AsString);
          self.C_Width.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_Width').AsString);
          self.C_GramWeight.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_GramWeight').AsString);
          self.OrdUnit.ItemIndex := OrdUnit.Items.IndexOf(trim(frmTradeSalesContractSel.CDS_1.fieldbyname('ConUnit').AsString));
          with self.CDS_1 do
          begin
            Append;
            FieldByName('SSel').Value := false;
            Fieldbyname('ConNo').value := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('ConNo').AsString);
            Fieldbyname('ConMId').value := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('ConMId').AsString);
            Fieldbyname('ConSId').value := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('ConSId').AsString);
            Fieldbyname('C_ColorNo').value := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_ColorNo').AsString);
            Fieldbyname('C_Color').value := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_Color').AsString);
            Fieldbyname('C_Pattern').value := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_Pattern').AsString);
            Fieldbyname('OrdQty').value := frmTradeSalesContractSel.CDS_1.Fieldbyname('ConQty').AsFloat;
            Post;
          end;
          CDS_1.Edit;
          CDS_1.fieldbyname('SSel').value := false;
        end;

      end;
    end;
  finally
    frmTradeSalesContractSel.Free;
  end;
end;

procedure TfrmTradePlanMulFtyInPut.ScrollBox1Click(Sender: TObject);
begin
  OrdDate.SetFocus;
end;

procedure TfrmTradePlanMulFtyInPut.OurCoNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmTradePlanMulFtyInPut.OurCoNameBtnUpClick(Sender: TObject);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '我司';
      if ShowModal = 1 then
      begin
        self.OurCoName.TxtCode := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.OurCoName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;

end;

procedure TfrmTradePlanMulFtyInPut.SYRNameBtnUpClick(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
      fnote := True;
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradePlanMulFtyInPut.Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmClothInfoSel := TfrmClothInfoSel.Create(self);
  with frmClothInfoSel do
  begin
    if showmodal = 1 then
    begin
      CDS_1.Edit;
      CDS_1.fieldbyname('MPRTCode').Value := CDS_1.fieldbyname('CYNO').asstring;
      CDS_1.fieldbyname('MPRTCodeName').Value := CDS_1.fieldbyname('CYName').asstring;
      CDS_1.fieldbyname('MPRTSpec').Value := CDS_1.fieldbyname('CYSpec').asstring;
      CDS_1.fieldbyname('MPRTCF').Value := CDS_1.fieldbyname('CYCF').asstring;
      CDS_1.fieldbyname('PRTColor').Value := CDS_1.fieldbyname('CYColor').asstring;
      CDS_1.fieldbyname('PRTHX').Value := CDS_1.fieldbyname('CYHX').asstring;
      CDS_1.fieldbyname('MPRTMF').Value := CDS_1.fieldbyname('CYMF').asstring;
      CDS_1.fieldbyname('MPRTKZ').Value := CDS_1.fieldbyname('CYKZ').asstring;
      CDS_1.fieldbyname('OrderUnit').Value := CDS_1.fieldbyname('CYPUnit').asstring;
      CDS_1.fieldbyname('SuoLv').Value := CDS_1.fieldbyname('SuoLv').asstring;
      CDS_1.fieldbyname('GcHuaNo').Value := CDS_1.fieldbyname('GcHua').asstring;
      CDS_1.fieldbyname('GsHuaNo').Value := CDS_1.fieldbyname('GsHua').asstring;
      CDS_1.Post;
    end;
    free;
  end;
end;

procedure TfrmTradePlanMulFtyInPut.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin

//  InitImageHX();
end;

procedure TfrmTradePlanMulFtyInPut.TV2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      fnote := True;
      flag := 'Technics';
      flagname := '工艺';
      if ShowModal = 1 then
      begin
        CDS_Fact.Edit;
        CDS_Fact.FieldByName('Technics').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmTradePlanMulFtyInPut.TV2Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  MFollower, MFollowId: string;
begin
  MFollower := '';
  MFollowId := '';

  try
    frmUserSel := TfrmUserSel.Create(Application);
    with frmUserSel do
    begin
      FMultiple := True;
      if ShowModal = 1 then
      begin
        while frmUserSel.CDS_1.Locate('SSel', True, []) do
        begin
          if MFollowId = '' then
          begin
            MFollowId := Trim(CDS_1.FieldByName('UserID').AsString);
            MFollower := Trim(CDS_1.FieldByName('UserName').AsString);
          end
          else
          begin
            MFollowId := MFollowId + ',' + Trim(CDS_1.FieldByName('UserID').AsString);
            MFollower := MFollower + ',' + Trim(CDS_1.FieldByName('UserName').AsString);
          end;

          frmUserSel.CDS_1.Delete;
        end;

        CDS_Fact.Edit;
        CDS_Fact.FieldByName('FollowId').Value := MFollowId;
        CDS_Fact.FieldByName('Follower').Value := MFollower;

      end;
    end;
  finally
    frmUserSel.Free;
  end;
end;

procedure TfrmTradePlanMulFtyInPut.v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FPrice, FHKMoney: Double;
begin

//  mvalue := TcxTextEdit(Sender).EditingText;
//  if Trim(mvalue) = '' then
//  begin
//    mvalue := '0';
//  end;
//  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
//
//  with CDS_1 do
//  begin
//    Edit;
//    FieldByName(FFieldName).Value := Trim(mvalue);
//    Post;
//    FQty := Fieldbyname('PRTOrderQty').AsFloat;
//    FPrice := Fieldbyname('Price').AsFloat;
//  end;
//
//  FHKMoney := RoundFloat(FQty * FPrice, 2);
//  with CDS_1 do
//  begin
//    Edit;
//    FieldByName('MXMoney').Value := FHKMoney;
//    Post;
//  end;
//  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmTradePlanMulFtyInPut.V2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        CDS_Fact.Edit;

        CDS_Fact.FieldByName('FtyNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
        CDS_Fact.FieldByName('FtyName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;

end;

procedure TfrmTradePlanMulFtyInPut.v1Column5PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_1 do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    FieldByName('MGMainId').Value := '';
    Post;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmTradePlanMulFtyInPut.HYTTBtnUpClick(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 ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradePlanMulFtyInPut.KYTTBtnUpClick(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
        V1Name.Caption := '中文名称';
        V1Note.Caption := '英文名称';
        fnote := True;
      end;
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

