unit U_WBSpecInPut;

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, dxSkinsCore,
  dxSkinsDefaultPainters, dxDateRanges, dxBarBuiltInMenu, U_BaseInput,
  U_BaseList, System.ImageList, Vcl.ImgList;

type
  TfrmYarnInfoInPut = 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;
    Note: TMemo;
    Label3: TLabel;
    WB_Code: TEdit;
    ToolBar2: TToolBar;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    Label4: TLabel;
    Label8: TLabel;
    WB_YarnQty: TEdit;
    WB_Spec: TEdit;
    Tv1Column2: 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);
  private
    { Private declarations }
    function SaveData(): Boolean;
  public
    { Public declarations }
    FWBSID, FSTKName: string;

  end;

var
  frmYarnInfoInPut: TfrmYarnInfoInPut;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_YarnInfoSel;

{$R *.dfm}

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

procedure TfrmYarnInfoInPut.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 TfrmYarnInfoInPut.SaveData(): Boolean;
var
  MaxNo, MaxYRId: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;

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

    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;
    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.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 A.* ');
    sql.Add(' from Tat_WB_Formula A');
    sql.Add(' where WBSID=''' + Trim(FWBSID) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_Sub);
  SInitCDSData(ADOQueryTemp, CDS_Sub);
end;

procedure TfrmYarnInfoInPut.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 TfrmYarnInfoInPut.TBSaveClick(Sender: TObject);
begin
  if Trim(WB_Code.Text) = '' then
  begin
    Application.MessageBox('编号不能为空!', '提示', 0);
    Exit;
  end;
  if Trim(WB_Spec.Text) = '' then
  begin
    Application.MessageBox('规格不能为空!', '提示', 0);
    Exit;
  end;

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

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

end.

