unit U_WarpBeamOutEdit;

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, cxSpinEdit,
  StdCtrls, ExtCtrls, cxLookAndFeels, cxLookAndFeelPainters, dxSkinsCore,
  dxSkinsDefaultPainters, cxNavigator, dxDateRanges, dxBarBuiltInMenu,
  U_BaseInput, System.ImageList, Vcl.ImgList;

type
  TfrmWarpBeamOutEdit = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1CRType: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1Column1: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    ToolButton2: TToolButton;
    Tv1Column18: 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 Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton2Click(Sender: TObject);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
  public
    { Public declarations }
    FBCId, FStkName, FKHName: string;
  end;

var
  frmWarpBeamOutEdit: TfrmWarpBeamOutEdit;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_WarpBeamStkSel, U_CompanySel;

{$R *.dfm}

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

procedure TfrmWarpBeamOutEdit.TBAddClick(Sender: TObject);
begin
  try
    frmWarpBeamStkSel := TfrmWarpBeamStkSel.Create(Application);
    with frmWarpBeamStkSel do
    begin
      FstkName := Self.FstkName;
      if ShowModal = 1 then
      begin
        frmWarpBeamStkSel.CDS_Main.DisableControls;
        with frmWarpBeamStkSel.CDS_Main do
        begin
          First;
          while frmWarpBeamStkSel.CDS_Main.Locate('SSel', True, []) do
          begin
            with Self.CDS_Sub do
            begin
              Append;
              FieldByName('IOTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
              FieldByName('STKID').Value := frmWarpBeamStkSel.CDS_Main.fieldbyname('TWIOID').Value;
              FieldByName('IOType').Value := '车间领用';
              FieldByName('FromCoNo').Value := frmWarpBeamStkSel.CDS_Main.fieldbyname('FromCoNo').Value;  //来自单位
              FieldByName('FromCoName').Value := frmWarpBeamStkSel.CDS_Main.fieldbyname('FromCoName').Value;
              FieldByName('StkCoNo').Value := frmWarpBeamStkSel.CDS_Main.fieldbyname('StkCoNo').Value;    //存放单位
              FieldByName('StkCoName').Value := frmWarpBeamStkSel.CDS_Main.fieldbyname('StkCoName').Value;
              FieldByName('ToCoNo').Value := frmWarpBeamStkSel.CDS_Main.fieldbyname('StkCoNo').Value;     //出到单位
              FieldByName('ToCoName').Value := frmWarpBeamStkSel.CDS_Main.fieldbyname('StkCoName').Value;
              FieldByName('WBNo').Value := Trim(frmWarpBeamStkSel.CDS_Main.fieldbyname('WBNo').AsString);
              FieldByName('WBCID').Value := Trim(frmWarpBeamStkSel.CDS_Main.fieldbyname('WBCID').AsString);
              FieldByName('WB_Type').Value := Trim(frmWarpBeamStkSel.CDS_Main.fieldbyname('WB_Type').AsString);
              FieldByName('WB_YarnArr').Value := Trim(frmWarpBeamStkSel.CDS_Main.fieldbyname('WB_YarnArr').AsString);
              FieldByName('WB_MachNo').Value := Trim(frmWarpBeamStkSel.CDS_Main.fieldbyname('WB_MachNo').AsString);
              FieldByName('WB_Len').Value := Trim(frmWarpBeamStkSel.CDS_Main.fieldbyname('WB_Len').AsString);
              FieldByName('WB_Macher').Value := Trim(frmWarpBeamStkSel.CDS_Main.fieldbyname('WB_Macher').AsString);
              FieldByName('WB_YarnQty').Value := frmWarpBeamStkSel.CDS_Main.fieldbyname('WB_YarnQty').Value;
              FieldByName('Qty').Value := frmWarpBeamStkSel.CDS_Main.fieldbyname('Qty').AsFloat;
              FieldByName('StkPosition').Value := frmWarpBeamStkSel.CDS_Main.fieldbyname('StkPosition').Value;
              FieldByName('WB_BatchNo').Value := frmWarpBeamStkSel.CDS_Main.fieldbyname('WB_BatchNo').Value;
              FieldByName('WB_YarnWidth').Value := frmWarpBeamStkSel.CDS_Main.fieldbyname('WB_YarnWidth').Value;
              FieldByName('WB_ClothName').Value := frmWarpBeamStkSel.CDS_Main.fieldbyname('WB_ClothName').Value;
              FieldByName('WB_YarnCode').Value := frmWarpBeamStkSel.CDS_Main.fieldbyname('WB_YarnCode').Value;
              FieldByName('WB_YarnName').Value := frmWarpBeamStkSel.CDS_Main.fieldbyname('WB_YarnName').Value;

              FieldByName('WB_Spec').Value := frmWarpBeamStkSel.CDS_Main.fieldbyname('WB_Spec').Value;

            end;
            frmWarpBeamStkSel.CDS_Main.Delete;
          end;
        end;
        frmWarpBeamStkSel.CDS_Main.EnableControls;
      end;
    end;
  finally
    frmWarpBeamStkSel.Free;
  end;

end;

function TfrmWarpBeamOutEdit.SaveCKData(): Boolean;
var
  MTWIOID, Maxno: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    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_IO where TWIOID=''' + Trim(CDS_Sub.fieldbyname('TWIOID').AsString) + '''');
          Open;
        end;
        MTWIOID := Trim(ADOQueryTemp.fieldbyname('TWIOID').AsString);
        if Trim(MTWIOID) = '' then
        begin
          if not GetLSNo(ADOQueryCmd, Maxno, 'ZC', 'Tat_WB_IO', 4, 1) then
            raise Exception.Create('取经轴出库编号失败！');
        end
        else
        begin
          Maxno := Trim(MTWIOID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Tat_WB_IO where TWIOID=''' + Trim(Maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(MTWIOID) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName)
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
          end;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'Tat_WB_IO', 0);
          FieldByName('STKName').Value := FSTKName;
          FieldByName('TWIOID').Value := Trim(Maxno);
          FieldByName('IOFlag').Value := '出库';
          FieldByName('IOQtyFlag').Value := -1;
          FieldByName('STKID').Value := CDS_Sub.fieldbyname('STKID').Value;
          FieldByName('FromCoNo').Value := CDS_Sub.fieldbyname('FromCoNo').Value;
          FieldByName('FromCoName').Value := CDS_Sub.fieldbyname('FromCoName').Value;
          FieldByName('StkCoNo').Value := CDS_Sub.fieldbyname('StkCoNo').Value;
          FieldByName('ToCoNo').Value := CDS_Sub.fieldbyname('ToCoNo').Value;
          Post;
        end;

        with ADOQueryCmd do
        begin
          Close;
          Sql.Clear;
          Sql.Add('exec P_TatWB_Stk @TWIOID=' + quotedstr(Trim(Maxno)));
          Open;
        end;
        if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
          raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));

        Edit;
        FieldByName('TWIOID').Value := Trim(Maxno);
        Post;

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

end;

procedure TfrmWarpBeamOutEdit.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmWarpBeamOutEdit.FormShow(Sender: TObject);
var
  fsj: string;
begin
  inherited;
  ReadCxGrid(FSTKName + '出库登记', Tv1, '机物料仓库');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from Tat_WB_IO A');
    sql.Add(' where TWIOID=''' + Trim(FBCId) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_Sub);
  SInitCDSData(ADOQueryTemp, CDS_Sub);
//  fsj:='select name=CJName ,Code=null from CheJian';
//  SInitCxGridComboBoxBySql(ADOQueryTemp,v1CheJian,fsj,0,True,'');
end;

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

    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        Sql.Add('exec P_TatWB_Out_Del ');
        Sql.Add(' @TWIOIDS=' + quotedstr(Trim(CDS_Sub.fieldbyname('TWIOID').AsString)));
        Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
        Sql.Add(',@DName=' + quotedstr(Trim(DName)));
        Open;
      end;
      if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
        raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Sub.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    end;
  end
  else
    CDS_Sub.Delete;

end;

procedure TfrmWarpBeamOutEdit.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if CDS_Sub.Locate('IOTime', null, []) = True then
  begin
    Application.MessageBox('出库时间不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('IOType', null, []) = True then
  begin
    Application.MessageBox('出库类型不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_Sub.Locate('Qty', null, []) = True then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmWarpBeamOutEdit.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(fSTKName + '出库登记', Tv1, '机物料仓库');

end;

procedure TfrmWarpBeamOutEdit.ToolButton2Click(Sender: TObject);
var
  Maxno: string;
begin
  if not GetLSNo(ADOQueryCmd, Maxno, 'ZC', 'Tat_WB_IO', 4, 1) then
  begin

  end;
end;

procedure TfrmWarpBeamOutEdit.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('ToCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
          FieldByName('ToCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;

end;

procedure TfrmWarpBeamOutEdit.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  FFSPID: string;
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('TWIOID').AsString) <> '' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Tat_WB_IO where TWIOID=''' + Trim(CDS_Sub.fieldbyname('TWIOID').AsString) + '''');
      Open;
    end;
    FFSPID := Trim(ADOQueryTemp.fieldbyname('TWIOID').AsString);
    if Trim(FFSPID) <> '' then
    begin
      v1CRType.Options.Editing := False;
    end
    else
    begin
      v1CRType.Options.Editing := True;
    end;
  end
  else
  begin
    v1CRType.Options.Editing := True;
  end;
end;

end.

