unit U_WBSpecInPut2;

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,
  cxMemo, dxScrollbarAnnotations, cxContainer, dxSkinsCore,
  dxSkinsDefaultPainters, cxImageList;

type
  TfrmWBSpecInPut2 = class(TfrmBaseInput)
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1SPName: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DS_1: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    v1Column12: TcxGridDBColumn;
    v1SPSpec: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    ScrollBox1: TScrollBox;
    Label7: TLabel;
    Label3: TLabel;
    WB_Code: TcxTextEdit;
    ToolBar2: TToolBar;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    Label4: TLabel;
    Label8: TLabel;
    WB_YarnQty: TcxTextEdit;
    WB_Move: TcxTextEdit;
    Tv1Column2: TcxGridDBColumn;
    Label1: TLabel;
    Label2: TLabel;
    WB_Name: TcxTextEdit;
    WB_ReedNote: TcxTextEdit;
    Label5: TLabel;
    WB_YarnArr: TcxTextEdit;
    Label6: TLabel;
    WB_YarnStripRemainder: TcxTextEdit;
    Label9: TLabel;
    WB_YarnStripQty: TcxTextEdit;
    Label10: TLabel;
    WB_ReedNo: TcxTextEdit;
    Label11: TLabel;
    WB_Width: TcxTextEdit;
    Label12: TLabel;
    WB_YarnLen: TcxTextEdit;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    WB_Arg_SDWZ: TcxTextEdit;
    WB_Arg_PXDS: TcxTextEdit;
    Label16: TLabel;
    WB_Arg_QJZL: TcxTextEdit;
    Tv1Column3: TcxGridDBColumn;
    WB_YarnArrNote: TcxMemo;
    Note: TcxMemo;
    btn1: TToolButton;
    Tv1Column4: TcxGridDBColumn;
    Label17: TLabel;
    WB_YarnDensity: TcxTextEdit;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBAddClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
    function SaveData(): Boolean;
  public
    { Public declarations }
    FWBSID, FSTKName: string;
  end;

var
  frmWBSpecInPut2: TfrmWBSpecInPut2;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_YarnInfoSel, U_TatClothInfoSel;

{$R *.dfm}

procedure TfrmWBSpecInPut2.btn1Click(Sender: TObject);
begin
  try
    frmTatClothInfoSel := TfrmTatClothInfoSel.Create(Application);
    with frmTatClothInfoSel do
    begin
      if ShowModal = 1 then
      begin

        Self.WB_YarnQty.Text := frmTatClothInfoSel.CDS_1.fieldbyname('C_YarnQty').AsString;
        Self.WB_ReedNo.Text := frmTatClothInfoSel.CDS_1.fieldbyname('C_ReedNo').AsString;

        with Self.ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select A.*  from BS_Cloth_Yarn A inner join Bs_Cloth_Info B on A.BCIID=B.BCIID');
          sql.Add(' where A.YRType=''经丝'' and B.C_Code=''' + Trim(frmTatClothInfoSel.CDS_1.fieldbyname('C_Code').AsString) + '''');
          Open;
          First;
          Self.CDS_Sub.EmptyDataSet;
          while not Eof do
          begin
            with Self.CDS_Sub do
            begin
              Append;
              FieldByName('RowNo').Value := Self.ADOQueryTemp.fieldbyname('RowNo').value;
              FieldByName('Y_Code').Value := Self.ADOQueryTemp.fieldbyname('Y_Code').value;
              FieldByName('Y_Name').Value := Self.ADOQueryTemp.fieldbyname('Y_Name').value;
              FieldByName('Y_Spec').Value := Self.ADOQueryTemp.fieldbyname('Y_Spec').value;
              FieldByName('Y_Color').Value := Self.ADOQueryTemp.fieldbyname('Y_Color').value;
              FieldByName('YarnQty').Value := Self.ADOQueryTemp.fieldbyname('YarnQty').value;
              FieldByName('CycleYarnQty').Value := Self.ADOQueryTemp.fieldbyname('CycleYarnQty').value;
            end;
            Next;
          end;

        end;

      end;
    end;
  finally
    frmTatClothInfoSel.Free;
  end;
end;

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

procedure TfrmWBSpecInPut2.TBAddClick(Sender: TObject);
begin
  try
    frmYarnInfoSel := TfrmYarnInfoSel.Create(Application);
    with frmYarnInfoSel do
    begin
      FSTKName := '纱线';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Append;
          FieldByName('Y_Color').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Color').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_Ratio').Value := 0;
        end;
      end;
    end;
  finally
    frmYarnInfoSel.Free;
  end;
end;

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

    if Trim(FWBSID) = '' then
    begin
      if GetLSNo(ADOQueryCmd, MaxNo, 'QJ', 'Tat_WB_Spec', 5, 0) = False then
        raise Exception.Create('取最大号失败！');

      WB_Code.Text := MaxNo;
    end
    else
    begin
      MaxNo := Trim(FWBSID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from Tat_WB_Spec where WBSID=''' + Trim(MaxNo) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FWBSID) = '' 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('WBSID').Value := Trim(MaxNo);
      RTSetsavedata(ADOQueryCmd, 'Tat_WB_Spec', ScrollBox1, 2);
      Post;
    end;

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

    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Tat_WB_Formula  ');
          sql.Add('where WBSID=''' + Trim(MaxNo) + '''');
          sql.Add('and YRId=''' + Trim(CDS_Sub.fieldbyname('YRId').AsString) + '''');
          open;
        end;
        MaxYRId := Trim(ADOQueryTemp.fieldbyname('YRId').AsString);
        if Trim(MaxYRId) = '' then
        begin
          if GetLSNo(ADOQueryCmd, MaxYRId, 'P', 'Tat_WB_Formula', 4, 0) = False then
            raise Exception.Create('取最大号失败！');
        end
        else
        begin
          MaxYRId := Trim(MaxYRId);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Tat_WB_Formula where YRId=''' + Trim(MaxYRId) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(MaxYRId) = '' 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 := SGetServerDate(ADOQueryTemp);
          end;
          FieldByName('WBSID').Value := Trim(MaxNo);
          FieldByName('YRId').Value := Trim(MaxYRId);
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'Tat_WB_Formula', 0);
          Post;
        end;

        Edit;
        FieldByName('YRId').Value := Trim(MaxYRId);
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update  Tat_WB_Spec set WB_Name=');   // X.RowNo
      sql.Add(' SUBSTRING(CAST((select ''+''+X.Y_Name from Tat_WB_Formula X where  isnull(X.Y_Name,'''')<>'''' and X.WBSID=Tat_WB_Spec.WBSID order by RowNo for xml path('''') ) AS VARCHAR(200)) , 2, 200)');
      sql.Add('where isnull(WB_Name,'''')='''' and WBSID=''' + Trim(MaxNo) + '''');
//      ShowMessage(SQL.Text);
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmWBSpecInPut2.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmWBSpecInPut2.FormShow(Sender: TObject);
var
  fsj: string;
begin
  inherited;
  ReadCxGrid(trim(self.Caption), Tv1, '经规格仓库');

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

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from Tat_WB_Formula ');
    sql.Add(' where WBSID=''' + Trim(FWBSID) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_Sub);
  SInitCDSData(ADOQueryTemp, CDS_Sub);

  if FWBSID = '' then
  begin
    with CDS_Sub do
    begin
      Append;
      FieldByName('RowNo').Value := 'A';
      Post;
      Append;
      FieldByName('RowNo').Value := 'B';
      Post;
      Append;
      FieldByName('RowNo').Value := 'C';
      Post;
      Append;
      FieldByName('RowNo').Value := 'D';
      Post;
      Append;
      FieldByName('RowNo').Value := 'E';
      Post;
      Append;
      FieldByName('RowNo').Value := 'F';
      Post;
      Append;
      FieldByName('RowNo').Value := 'G';
      Post;
    end;
  end;

end;

procedure TfrmWBSpecInPut2.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('YRId').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;

    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete Tat_WB_Formula where YRId=''' + Trim(CDS_Sub.fieldbyname('YRId').AsString) + '''');
        ExecSQL;
      end;

      ADOQueryCmd.Connection.CommitTrans;
      CDS_Sub.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!', '提示', 0);
    end;

  end
  else
  begin
    CDS_Sub.Delete;
  end;

end;

procedure TfrmWBSpecInPut2.TBSaveClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if StrToFloatDef(WB_YarnDensity.Text, 0) = 0 then
  begin
    Application.MessageBox('请填写正确的经密!', '提示', 0);
    Exit;
  end;
  if StrToFloatDef(WB_Width.Text, 0) = 0 then
  begin
    Application.MessageBox('请填写正确的经轴门幅!', '提示', 0);
    Exit;
  end;

  with CDS_Sub do
  begin
    CDS_Sub.First;
    while not Eof do
    begin
      if trim(CDS_Sub.fieldbyname('Y_Code').asstring) <> '' then
      begin
        if StrToFloatDef(Trim(CDS_Sub.fieldbyname('YarnQty').AsString), 0) = 0 then
        begin
          Application.MessageBox('根数不能为空!', '提示', 0);
          Exit;
        end;
      end;

      if trim(CDS_Sub.fieldbyname('Y_Code').asstring) <> '' then
      begin
        if StrToFloatDef(Trim(CDS_Sub.fieldbyname('CycleYarnQty').AsString), 0) = 0 then
        begin
          Application.MessageBox('循环根数不能为空!', '提示', 0);
          Exit;
        end;
      end;

      CDS_Sub.Next;
    end;
  end;

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

procedure TfrmWBSpecInPut2.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '经规格仓库');
end;

procedure TfrmWBSpecInPut2.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmYarnInfoSel := TfrmYarnInfoSel.Create(Application);
    with frmYarnInfoSel do
    begin
      FSTKName := '纱线';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('Y_Color').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Color').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('FF').Value := frmYarnInfoSel.CDS_1.fieldbyname('FF').value;
          FieldByName('ND').Value := frmYarnInfoSel.CDS_1.fieldbyname('ND').value;
          FieldByName('Y_Ratio').Value := 0;
        end;
      end;
    end;
  finally
    frmYarnInfoSel.Free;
  end;
end;

end.

