unit U_TatClothInfoInput;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, ADODB, DBClient, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, ComCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, cxTextEdit,
  cxButtonEdit, StdCtrls, ExtCtrls, cxCurrencyEdit, BtnEdit, U_BaseList,
  cxLookAndFeels, cxLookAndFeelPainters, dxSkinsCore, dxSkinsDefaultPainters,
  cxNavigator, dxDateRanges, dxBarBuiltInMenu, cxPC;

type
  TfrmTatClothInfoInput = class(TForm)
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label5: TLabel;
    Label15: TLabel;
    Label22: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    Label3: TLabel;
    C_Code: TEdit;
    C_Name: TEdit;
    C_YarnQty: TEdit;
    C_FinishLatitudeDensity: TEdit;
    C_Width: TBtnEditC;
    C_GramWeight: TBtnEditC;
    Label12: TLabel;
    C_FinishLongitudeDensity: TEdit;
    ToolButton6: TToolButton;
    BCIID: TEdit;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    C_LatitudeSpec: TEdit;
    C_LongitudeSpec: TEdit;
    C_ReedNo: TEdit;
    Label9: TLabel;
    C_Spec: TEdit;
    Panel1: TPanel;
    cxPageControl2: TcxPageControl;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Panel3: TPanel;
    Panel4: TPanel;
    Label20: TLabel;
    ToolBar2: TToolBar;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton5: TToolButton;
    Panel2: TPanel;
    cxPageControl1: TcxPageControl;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column10: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel5: TPanel;
    Panel6: TPanel;
    Label21: TLabel;
    ToolBar3: TToolBar;
    ToolButton4: TToolButton;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    CDS_1: TClientDataSet;
    DS_1: TDataSource;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    Label10: TLabel;
    C_MachLatitudeDensity: TEdit;
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CYColorKeyPress(Sender: TObject; var Key: Char);
    procedure CYKZExit(Sender: TObject);
    procedure CYMFExit(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CYJGGYBtnDnClick(Sender: TObject);
    procedure C_WidthBtnDnClick(Sender: TObject);
    procedure C_WidthBtnUpClick(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
  private
    canshu1: string;
    Fint: Integer;
    procedure InitGrid();
    function SaveData(): Boolean;
    procedure CALCYarnQty();
    { Private declarations }
  public
    FBCIID, FCTID: string;
    CopyInt: Integer;

    { Public declarations }
  end;

var
  frmTatClothInfoInput: TfrmTatClothInfoInput;

implementation

uses
  U_DataLink, U_RTFun, U_iniParam, U_ZDYHelp, U_AttachmentUpload, U_YarnInfoSel;

{$R *.dfm}
procedure TfrmTatClothInfoInput.CALCYarnQty();
var
  MYarnQty, MOrdQty, MWidth, MMachLatitudeDensity: Double;
begin
  MYarnQty := STRTOFLOATDEF(C_YarnQty.Text, 0);
  MOrdQty := 10000;
  MWidth := STRTOFLOATDEF(StringReplace(C_Width.Text, 'CM', '', [rfReplaceAll, rfIgnoreCase]), 0);
  MMachLatitudeDensity := STRTOFLOATDEF(C_MachLatitudeDensity.Text, 0);
  with CDS_1 do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('Y_Qty').Value := MYarnQty * MOrdQty * FieldByName('Y_Ratio').AsFloat / 100 / 9000 * FieldByName('Denier').AsFloat / 1000;
      Next;
    end;
    EnableControls;
  end;

  with CDS_2 do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('Y_Qty').Value := MMachLatitudeDensity * MOrdQty * MWidth * FieldByName('Y_Ratio').AsFloat / 100 / 9000 * FieldByName('Denier').AsFloat / 1000;
      Next;
    end;
    EnableControls;
  end;

end;

procedure TfrmTatClothInfoInput.InitGrid();
begin

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from BS_Cloth_Info where BCIID=''' + Trim(FBCIID) + '''');
    Open;
  end;

  SCSHData(ADOQueryTemp, ScrollBox1, 2);
  SCSHData(ADOQueryTemp, ScrollBox1, 0);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*  from BS_Cloth_Yarn A ');
    sql.Add(' where YRType=''经丝'' and A.BCIID=''' + Trim(FBCIID) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_1);
  SInitCDSData(ADOQueryTemp, CDS_1);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*  from BS_Cloth_Yarn A ');
    sql.Add(' where YRType=''纬丝'' and A.BCIID=''' + Trim(FBCIID) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_2);
  SInitCDSData(ADOQueryTemp, CDS_2);
end;

procedure TfrmTatClothInfoInput.FormShow(Sender: TObject);
begin
  InitGrid();
  if CopyInt = 1 then
  begin
    FBCIID := '';
    BCIID.text := '';
    C_Code.text := '';

  end;
//  if C_Code.text = '' then
//  begin
//    with ADOQueryTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add(' exec P_YP_Get_CPBH  ');
//      sql.Add('@CTID=' + quotedstr(Trim(FCTID)));
//
//      Open;
//    end;
//  end;
//  if trim(C_Code.Text) = '' then
//  begin
//    with ADOQueryTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add(' exec P_YP_Get_No @Str=''CP'' ');
//      Open;
//    end;
//    C_Code.Text := trim(ADOQueryTemp.FieldByName('NewC_Code').asstring);
//  end;

//  if FCPName = '针织' then
//  begin
//    CYPUnit.text := 'KG';
//  end;
//  if FCPName = '梭织' then
//  begin
//    CYPUnit.text := 'M';
//  end;

end;

procedure TfrmTatClothInfoInput.TBCloseClick(Sender: TObject);
begin
  Close;
end;

function TfrmTatClothInfoInput.SaveData(): Boolean;
var
  maxId, maxpbno: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FBCIID) = '' then
    begin
      if GetLSNo(ADOQueryCmd, maxId, 'Y', 'BS_Cloth_Info', 4, 1) = False then
      begin
        Result := False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;

      BCIID.Text := trim(maxId);
    end
    else
    begin
      maxId := Trim(FBCIID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from BS_Cloth_Info where BCIID=''' + Trim(FBCIID) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FBCIID) = '' then
      begin
        Append;
        FieldByName('CTID').Value := Trim(FCTID);
        FieldByName('FILLID').Value := Trim(DCode);
        FieldByName('FILLER').Value := Trim(DName);
        FieldByName('FILLTIME').Value := SGetServerDateTime(ADOQueryTemp);
      end
      else
      begin
        Edit;
        FieldByName('EDITER').Value := Trim(DName);
        FieldByName('EDITTIME').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('BCIID').Value := Trim(maxId);
      if trim(C_Code.Text) = '' then
        C_Code.Text := Trim(maxId);
      RTSetsavedata(ADOQueryCmd, 'BS_Cloth_Info', ScrollBox1, 2);
      Post;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) as AA from BS_Cloth_Info where C_Code=''' + Trim(C_Code.Text) + '''');
      Open;
      if FieldByName('AA').AsInteger > 1 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Result := False;
        Application.MessageBox('编号重复！', '提示', 0);
        Exit;
      end;
    end;
    //////////////////////////            保存经丝配比表              //////////////////////////
    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_1.fieldbyname('YRId').AsString) = '' then
        begin
          if GetLSNo(ADOQueryTemp, maxpbno, 'RJ', 'BS_Cloth_Yarn', 4, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取配比子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxpbno := Trim(CDS_1.fieldbyname('YRId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from BS_Cloth_Yarn where ');
          sql.Add('  YRId=''' + Trim(maxpbno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_1.fieldbyname('YRId').AsString) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FillTime').Value := SGetServerDateTime(ADOQueryTemp);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('YRType').Value := '经丝';
          FieldByName('BCIID').Value := Trim(maxId);
          FieldByName('YRId').Value := Trim(maxpbno);
          RTSetSaveDataCDS(ADOQueryCmd, Tv2, CDS_1, 'BS_Cloth_Yarn', 0);
          Post;
        end;
        CDS_1.Edit;
        CDS_1.FieldByName('BCIID').Value := Trim(maxId);
        CDS_1.FieldByName('YRId').Value := Trim(maxpbno);

        Next;
      end;
    end;
//////////////////////////            保存经丝配比表              //////////////////////////

//////////////////////////            保存纬丝配比表              //////////////////////////
    with CDS_2 do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_2.fieldbyname('YRId').AsString) = '' then
        begin
          if GetLSNo(ADOQueryTemp, maxpbno, 'RJ', 'BS_Cloth_Yarn', 4, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取配比子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxpbno := Trim(CDS_2.fieldbyname('YRId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from BS_Cloth_Yarn where ');
          sql.Add('  YRId=''' + Trim(maxpbno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_2.fieldbyname('YRId').AsString) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FillTime').Value := SGetServerDateTime(ADOQueryTemp);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('YRType').Value := '纬丝';
          FieldByName('BCIID').Value := Trim(maxId);
          FieldByName('YRId').Value := Trim(maxpbno);
          RTSetSaveDataCDS(ADOQueryCmd, Tv2, CDS_2, 'BS_Cloth_Yarn', 0);
          Post;
        end;
        CDS_2.Edit;
        CDS_2.FieldByName('BCIID').Value := Trim(maxId);
        CDS_2.FieldByName('YRId').Value := Trim(maxpbno);

        Next;
      end;
    end;
//////////////////////////            保存纬丝配比表              //////////////////////////
    ADOQueryCmd.Connection.CommitTrans;
    FBCIID := maxId;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!', '提示', 0);
  end;
end;

procedure TfrmTatClothInfoInput.CYJGGYBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
end;

procedure TfrmTatClothInfoInput.ToolButton1Click(Sender: TObject);
var
  MC_Code: string;
begin
  C_Name.SetFocus;
  if C_Name.Text = '' then
  begin
    Application.MessageBox('产品名称不能为空！', '提示', 0);
    Exit;
  end;
  if trim(C_Code.Text) = '' then
  begin

    if GetLSNo(ADOQueryCmd, MC_Code, 'C', 'BS_Cloth_Info', 4, 0) = False then
    begin
      Application.MessageBox('取最成品编号失败!', '提示', 0);
      Exit;
    end;
    C_Code.Text := MC_Code;

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

procedure TfrmTatClothInfoInput.ToolButton2Click(Sender: TObject);
begin
  try
    frmYarnInfoSel := TfrmYarnInfoSel.Create(Application);
    with frmYarnInfoSel do
    begin
      if ShowModal = 1 then
      begin
        with self.CDS_1 do
        begin
          Append;
          FieldByName('Brand').Value := frmYarnInfoSel.CDS_1.fieldbyname('Brand').Value;
          FieldByName('Y_Code').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Code').Value;
          FieldByName('Y_Name').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Name').Value;
          FieldByName('Y_Spec').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Spec').Value;
          FieldByName('Y_Color').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Color').Value;
          FieldByName('Denier').Value := frmYarnInfoSel.CDS_1.fieldbyname('Denier').Value;
          FieldByName('Y_Ratio').Value := 0;
          Post;
        end;

      end;
    end;
  finally
    frmYarnInfoSel.Free;
  end;
end;

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

procedure TfrmTatClothInfoInput.ToolButton4Click(Sender: TObject);
begin
  try
    frmYarnInfoSel := TfrmYarnInfoSel.Create(Application);
    with frmYarnInfoSel do
    begin
      if ShowModal = 1 then
      begin
        with self.CDS_2 do
        begin
          Append;
          FieldByName('Brand').Value := frmYarnInfoSel.CDS_1.fieldbyname('Brand').Value;
          FieldByName('Y_Code').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Code').Value;
          FieldByName('Y_Name').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Name').Value;
          FieldByName('Y_Spec').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Spec').Value;
          FieldByName('Y_Color').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Color').Value;
          FieldByName('Denier').Value := frmYarnInfoSel.CDS_1.fieldbyname('Denier').Value;
          FieldByName('Y_Ratio').Value := 0;
          Post;
        end;
      end;
    end;
  finally
    frmYarnInfoSel.Free;
  end;
end;

procedure TfrmTatClothInfoInput.ToolButton5Click(Sender: TObject);
begin
  C_Name.SetFocus;
  CALCYarnQty();
end;

procedure TfrmTatClothInfoInput.CYColorKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    Key := #0;
    SelectNext(ActiveControl as TWinControl, True, True);
  end;
end;

procedure TfrmTatClothInfoInput.CYKZExit(Sender: TObject);
begin
//  if pos('G/M2', trim(CYKZ.Text)) = 0 then
//  begin
//    if pos('G/M', trim(CYKZ.Text)) = 0 then
//    begin
//      CYKZ.Text := trim(CYKZ.Text) + 'G/M2';
//    end;
//  end;
end;

procedure TfrmTatClothInfoInput.C_WidthBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmTatClothInfoInput.C_WidthBtnUpClick(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 TfrmTatClothInfoInput.CYMFExit(Sender: TObject);
begin
//  if pos('"', trim(CYMF.Text)) = 0 then
//  begin
//    if pos('CM', trim(CYMF.Text)) = 0 then
//      CYMF.Text := trim(CYMF.Text) + 'CM';
//  end;

end;

procedure TfrmTatClothInfoInput.FormCreate(Sender: TObject);
begin

//  LaYangDate.DateTime := SGetServerDateTime(ADOQueryTemp);
end;

procedure TfrmTatClothInfoInput.ToolButton6Click(Sender: TObject);
begin
  if trim(C_Code.Text) = '' then
    Exit;
  try
    frmAttachmentUpload := TfrmAttachmentUpload.Create(Application);
    with frmAttachmentUpload do
    begin
      FEditAuthority := True;
      fkeyNO := trim(C_Code.Text);
      fType := '成品档案';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmAttachmentUpload.Free;
  end;
end;

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

procedure TfrmTatClothInfoInput.ToolButton8Click(Sender: TObject);
begin
  C_Name.SetFocus;
  CALCYarnQty();
end;

end.

