unit U_YarnInfoInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxCalendar, cxDropDownEdit, ComCtrls, ToolWin,
  cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxClasses, cxControls, cxGridCustomView, cxGrid, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxButtonEdit, cxTextEdit, StdCtrls, ExtCtrls,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxDateRanges,
  dxBarBuiltInMenu, U_BaseInput, U_BaseList, System.ImageList, Vcl.ImgList,
  cxContainer, cxMaskEdit, cxMemo, cxImageList, dxSkinsCore,
  dxSkinsDefaultPainters, dxCore, cxDateUtils, dxSkinBasic, dxSkinBlack,
  dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkroom,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinOffice2016Colorful, dxSkinOffice2016Dark,
  dxSkinOffice2019Black, dxSkinOffice2019Colorful, dxSkinOffice2019DarkGray,
  dxSkinOffice2019White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringtime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinTheBezier, dxSkinValentine,
  dxSkinVisualStudio2013Blue, dxSkinVisualStudio2013Dark,
  dxSkinVisualStudio2013Light, dxSkinVS2010, dxSkinWhiteprint, dxSkinWXI,
  dxSkinXmas2008Blue;

type
  TfrmYarnInfoInPut = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ToolButton1: TToolButton;
    ScrollBox1: TScrollBox;
    Label2: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label1: TLabel;
    Brand: TcxTextEdit;
    Y_Name: TcxTextEdit;
    Label3: TLabel;
    Y_Code: TcxTextEdit;
    Label4: TLabel;
    Label8: TLabel;
    Denier: TcxTextEdit;
    Label9: TLabel;
    Note: TcxMemo;
    Label5: TLabel;
    ADOConnection1: TADOConnection;
    Label10: TLabel;
    FF: TcxTextEdit;
    Label11: TLabel;
    ND: TcxTextEdit;
    Label12: TLabel;
    ZS: TcxTextEdit;
    Label13: TLabel;
    BYDate: TcxDateEdit;
    Y_Color: TcxButtonEdit;
    Y_Type: TcxButtonEdit;
    Y_Composition: TcxButtonEdit;
    Y_Spec: TcxButtonEdit;
    Label14: TLabel;
    CJ: TcxButtonEdit;
    Label15: TLabel;
    LLSH: TcxTextEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Y_NameClick(Sender: TObject);
    procedure Y_NameExit(Sender: TObject);
    procedure Y_SpecPropertiesChange(Sender: TObject);
    procedure DenierClick(Sender: TObject);
    procedure DenierExit(Sender: TObject);
    procedure ZSClick(Sender: TObject);
    procedure ZSExit(Sender: TObject);
    procedure DenierPropertiesEditValueChanged(Sender: TObject);
    procedure ZSPropertiesEditValueChanged(Sender: TObject);
    procedure ParentCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Y_ColorDblClick(Sender: TObject);
    procedure CJPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Y_CompositionPropertiesChange(Sender: TObject);
    procedure ZSPropertiesChange(Sender: TObject);
    procedure Y_ColorPropertiesChange(Sender: TObject);
    procedure FFPropertiesChange(Sender: TObject);
    procedure CJPropertiesChange(Sender: TObject);
  private
    { Private declarations }
    function SaveData(): Boolean;
    procedure UpdateYarnName;
  public
    { Public declarations }
    FBYIID, FSTKName: string;
  end;

var
  frmYarnInfoInPut: TfrmYarnInfoInPut;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CompanySel;    //, U_ProductInfoSel

{$R *.dfm}

procedure TfrmYarnInfoInPut.CJPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';

      if ShowModal = 1 then
      begin
        self.CJ.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmYarnInfoInPut.CJPropertiesChange(Sender: TObject);
begin
  UpdateYarnName;

end;

procedure TfrmYarnInfoInPut.DenierClick(Sender: TObject);
begin
  ZS.Style.Color := clMoneyGreen;
end;

procedure TfrmYarnInfoInPut.DenierExit(Sender: TObject);
begin
  ZS.Style.Color := clWhite;
end;

procedure TfrmYarnInfoInPut.DenierPropertiesEditValueChanged(Sender: TObject);
begin
  ZS.text := floattostr(roundfloat(5315.5 / strtofloatdef(Denier.text, 0), 0));
end;

procedure TfrmYarnInfoInPut.FFPropertiesChange(Sender: TObject); //纺法
begin
  UpdateYarnName;
end;

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

function TfrmYarnInfoInPut.SaveData(): Boolean;
var
  MaxNo, MaxPRID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;

    if Trim(FBYIID) = '' then
    begin
      if GetLSNo(ADOQueryCmd, MaxNo, 'P', 'BS_Yarn_Info', 4, 0) = False then
        raise Exception.Create('取最大号失败！');
      Y_Code.Text := MaxNo;
    end
    else
    begin
      MaxNo := Trim(FBYIID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from BS_Yarn_Info where BYIID=''' + Trim(MaxNo) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FBYIID) = '' 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(ADOQueryTemp);
      end;
      FieldByName('BYIID').Value := Trim(MaxNo);
      RTSetsavedata(ADOQueryCmd, 'BS_Yarn_Info', ScrollBox1, 2);
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BS_Yarn_Info where Y_Code=' + quotedstr(trim(Y_Code.Text)));
      Open;
    end;
    if ADOQueryCmd.RecordCount > 1 then
      raise Exception.Create('编号重复!');
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BS_Yarn_Info where Y_Name=' + quotedstr(trim(Y_Name.Text)));
      Open;
    end;
    if ADOQueryCmd.RecordCount > 1 then
      raise Exception.Create('品名重复!');

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

end;

procedure TfrmYarnInfoInPut.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmYarnInfoInPut.FormCreate(Sender: TObject);
begin
  inherited;
  try
    with ADOConnection1 do
    begin
      Connected := false;
      ConnectionString := DConString;
      Connected := true;
    end;
    ADOQueryBaseCmd.Connection := ADOConnection1;
    ADOQueryBaseTemp.Connection := ADOConnection1;
  except
    application.MessageBox('网络连接失败！', '提示信息');
  end;
end;

procedure TfrmYarnInfoInPut.FormShow(Sender: TObject);
var
  fsj: string;
begin
  inherited;

//  fsj := ' select Code=ZDYNo,Name=ZDYName from KH_ZDY where Type=''YType'' and MainType=''纱线档案'' ';
//  SInitTcxComBoxBySql(ADOQueryTemp, Y_Type, false, fsj);
//
//  fsj := ' select Code=ZDYNo,Name=ZDYName from KH_ZDY where Type=''Y_Spec'' and MainType=''纱线档案'' ';
//  SInitTcxComBoxBySql(ADOQueryTemp, Y_Spec, false, fsj);
//
//  fsj := ' select Code=ZDYNo,Name=ZDYName from KH_ZDY where Type=''Y_Composition'' and MainType=''纱线档案'' ';
//  SInitTcxComBoxBySql(ADOQueryTemp, Y_Composition, false, fsj);
//
//  fsj := ' select Code=ZDYNo,Name=ZDYName from KH_ZDY where Type=''Y_Color'' and MainType=''纱线档案'' ';
//  SInitTcxComBoxBySql(ADOQueryTemp, Y_Color, false, fsj);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from BS_Yarn_Info ');
    sql.Add(' where BYIID=''' + Trim(FBYIID) + '''');
    Open;
  end;
  SCSHData(ADOQueryTemp, ScrollBox1, 2);

  if FBYIID = '' then
  begin
    ByDate.Date := SGetServerDateTime(ADOQueryTemp);
  end;

end;

procedure TfrmYarnInfoInPut.Y_ColorDblClick(Sender: TObject);
begin
  TcxButtonEdit(Sender).Text := '';
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';
end;

procedure TfrmYarnInfoInPut.Y_ColorPropertiesChange(Sender: TObject); //颜色
begin
  UpdateYarnName;

end;

procedure TfrmYarnInfoInPut.Y_CompositionPropertiesChange(Sender: TObject); //成分
begin
  UpdateYarnName;

end;

procedure TfrmYarnInfoInPut.ParentCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxButtonEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      MainType := '纱线档案';
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TcxButtonEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYarnInfoInPut.TBSaveClick(Sender: TObject);
begin
//  if Trim(Y_Code.Text) = '' then
//  begin
//    Application.MessageBox('编号不能为空!', '提示', 0);
//    Exit;
//  end;
  ToolBar1.SetFocus;

  Y_Name.text := Trim(Y_Composition.text) + Trim(ZS.text) + 'S' + Trim(Y_Color.text) + Trim(FF.text); //+ Trim(CJ.text)
  if Trim(Y_Name.Text) = '' then
  begin
    Application.MessageBox('名称不能为空!', '提示', 0);
    Exit;
  end;

  if Trim(Denier.Text) <> '' then
  begin
    if StrToFloatdef(Denier.Text, 0) = 0 then
    begin
      Application.MessageBox('理论D数请输入数字!', '提示', 0);
      Exit;
    end;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    ModalResult := 1;
    Exit;
  end;
end;

procedure TfrmYarnInfoInPut.Y_NameClick(Sender: TObject);
begin
  Y_Composition.Style.Color := clMoneyGreen;
  ZS.Style.Color := clMoneyGreen;
  Y_Color.Style.Color := clMoneyGreen;
  FF.Style.Color := clMoneyGreen;
//  CJ.Style.Color := clMoneyGreen;
end;

procedure TfrmYarnInfoInPut.Y_NameExit(Sender: TObject);
begin
  Y_Composition.Style.Color := clWhite;
  ZS.Style.Color := clWhite;
  Y_Color.Style.Color := clWhite;
  FF.Style.Color := clWhite;
//  CJ.Style.Color := clWhite;
end;

procedure TfrmYarnInfoInPut.Y_SpecPropertiesChange(Sender: TObject);
begin
  Y_Name.text := Trim(Y_Composition.text) + Trim(ZS.text) + 'S' + Trim(Y_Color.text) + Trim(FF.text);   //+ Trim(CJ.text)
end;

procedure TfrmYarnInfoInPut.ZSClick(Sender: TObject);
begin
  Denier.Style.Color := clMoneyGreen;
end;

procedure TfrmYarnInfoInPut.ZSExit(Sender: TObject);
begin
  Denier.Style.Color := clWhite;
end;

procedure TfrmYarnInfoInPut.ZSPropertiesChange(Sender: TObject);
begin
  UpdateYarnName;
end;

procedure TfrmYarnInfoInPut.ZSPropertiesEditValueChanged(Sender: TObject);
begin
  Denier.text := floattostr(roundfloat(5315.5 / strtofloatdef(ZS.text, 0), 0));
end;

procedure TfrmYarnInfoInPut.UpdateYarnName;
begin
  Y_Name.Text := Trim(Y_Composition.Text) + Trim(ZS.Text) + 'S' + Trim(Y_Color.Text) + Trim(FF.Text);  // + Trim(CJ.Text)
end;

end.

