unit U_WarpBeamInEdit;

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, System.ImageList, Vcl.ImgList,
  dxScrollbarAnnotations, dxSkinsCore, dxSkinsDefaultPainters, cxImageList;

type
  TfrmWarpBeamInEdit = class(TfrmBaseInput)
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v1SPName: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    v1Column4: TcxGridDBColumn;
    DS_1: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    v1Column12: TcxGridDBColumn;
    v1FactoryName: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1SPSpec: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: 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 v1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FormDestroy(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1SPNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1SPSpecPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
  public
    { Public declarations }
    FBCId, canshu3, FSTKName, FKHName: string;
  end;

var
  frmWarpBeamInEdit: TfrmWarpBeamInEdit;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CompanySel, U_TatWBPlanCardSel,
  U_WarpBeamInfoSel;

{$R *.dfm}

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

procedure TfrmWarpBeamInEdit.TBAddClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty = False then
  begin
    CopyAddRowCDS(CDS_Sub);
    with CDS_Sub do
    begin
      Edit;
      FieldByName('TWIOID').Value := null;
      FieldByName('StkCoNo').Value := '本厂仓库';
      FieldByName('StkCoName').Value := '本厂仓库';
      FieldByName('Qty').Value := 0;
      post;
    end;
  end
  else
  begin
    with CDS_Sub do
    begin
      Append;
      FieldByName('IOTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
      FieldByName('StkCoNo').Value := '本厂仓库';
      FieldByName('StkCoName').Value := '本厂仓库';
      FieldByName('Qty').Value := 0;
      Post;
    end;
  end;

end;

function TfrmWarpBeamInEdit.SaveCKData(): Boolean;
var
  Maxno, MTWIOID: 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, 'ZR', '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('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;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'Tat_WB_IO', 0);
          FieldByName('FromCoNo').Value := CDS_Sub.fieldbyname('FromCoNo').Value;
          FieldByName('StkCoNo').Value := CDS_Sub.fieldbyname('StkCoNo').Value;
          FieldByName('FromOrdNo').Value := CDS_Sub.fieldbyname('FromOrdNo').value;
          FieldByName('FromMainId').Value := CDS_Sub.fieldbyname('FromMainId').value;
          FieldByName('FromSubId').Value := CDS_Sub.fieldbyname('FromSubId').value;
          FieldByName('TWIOID').Value := Trim(Maxno);
          FieldByName('StkID').Value := Trim(Maxno);
          FieldByName('STKName').Value := FSTKName;
          FieldByName('IOFlag').Value := '入库';
          FieldByName('IOQtyFlag').Value := 1;
          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;
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_Sub.EnableControls;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

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

procedure TfrmWarpBeamInEdit.FormDestroy(Sender: TObject);
begin
  inherited;
  frmWarpBeamInEdit := nil;
end;

procedure TfrmWarpBeamInEdit.FormShow(Sender: TObject);
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);
end;

procedure TfrmWarpBeamInEdit.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_In_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
  begin
    CDS_Sub.Delete;
  end;

end;

procedure TfrmWarpBeamInEdit.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 CDS_Sub.Locate('WBNo', null, []) = True then
  begin
    Application.MessageBox('轴号不能为空!', '提示', 0);
    Exit;
  end;
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    ModalResult := 1;
    Exit;
  end;
end;

procedure TfrmWarpBeamInEdit.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(FSTKName + '入库登记', Tv1, '机物料仓库');

end;

procedure TfrmWarpBeamInEdit.ToolButton2Click(Sender: TObject);
begin
  try
    frmTatWBPlanCardSel := TfrmTatWBPlanCardSel.Create(Application);
    with frmTatWBPlanCardSel do
    begin
      if ShowModal = 1 then
      begin
        with frmTatWBPlanCardSel.CDS_1 do
        begin
          First;

          while not eof do
          begin
            if FieldByName('ssel').AsBoolean then
            begin
              with Self.CDS_Sub do
              begin
                Append;
                FieldByName('IOTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
                FieldByName('StkCoNo').Value := '本厂仓库';
                FieldByName('StkCoName').Value := '本厂仓库';
                FieldByName('WBCID').Value := Trim(frmTatWBPlanCardSel.CDS_1.fieldbyname('WBCID').AsString);
                FieldByName('WB_Code').Value := Trim(frmTatWBPlanCardSel.CDS_1.fieldbyname('WB_Code').AsString);
                FieldByName('WB_Spec').Value := Trim(frmTatWBPlanCardSel.CDS_1.fieldbyname('WB_Spec').AsString);
                FieldByName('WB_Type').Value := Trim(frmTatWBPlanCardSel.CDS_1.fieldbyname('WB_Type').AsString);
                FieldByName('WB_YarnArr').Value := Trim(frmTatWBPlanCardSel.CDS_1.fieldbyname('WB_YarnArr').AsString);
                FieldByName('WB_MachNo').Value := Trim(frmTatWBPlanCardSel.CDS_1.fieldbyname('WB_MachNo').AsString);
                FieldByName('WB_Len').Value := Trim(frmTatWBPlanCardSel.CDS_1.fieldbyname('WB_Len').AsString);
                FieldByName('WB_Macher').Value := Trim(frmTatWBPlanCardSel.CDS_1.fieldbyname('WB_Macher').AsString);
                FieldByName('WB_YarnQty').Value := Trim(frmTatWBPlanCardSel.CDS_1.fieldbyname('WB_YarnQty').AsString);
                FieldByName('Qty').Value := Trim(frmTatWBPlanCardSel.CDS_1.fieldbyname('WB_YarnLen').AsString);
              end;
            end;
            Next;
          end;

        end;

      end;
    end;
  finally
    frmTatWBPlanCardSel.Free;
  end;
end;

procedure TfrmWarpBeamInEdit.Tv1Column1PropertiesButtonClick(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('StkCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
          FieldByName('StkCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;

end;

procedure TfrmWarpBeamInEdit.v1Column14PropertiesButtonClick(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('FromCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
          FieldByName('FromCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;

end;

procedure TfrmWarpBeamInEdit.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'StkPosition';
      flagname := '库位';
      MainType := FSTKName;
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('StkPosition').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmWarpBeamInEdit.v1SPNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  try
    frmTatWBPlanCardSel := TfrmTatWBPlanCardSel.Create(Application);
    with frmTatWBPlanCardSel do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('WBCID').Value := Trim(CDS_1.fieldbyname('WBCID').AsString);
        end;
      end;
    end;
  finally
    frmTatWBPlanCardSel.Free;
  end;
end;

procedure TfrmWarpBeamInEdit.v1SPSpecPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmWarpBeamInfoSel := TfrmWarpBeamInfoSel.Create(Application);
    with frmWarpBeamInfoSel do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('WBNo').Value := Trim(frmWarpBeamInfoSel.CDS_Main.fieldbyname('WBNo').AsString);
        end;
      end;
    end;
  finally
    frmWarpBeamInfoSel.Free;
  end;
end;

end.

