unit U_MachineInPut;

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,
  StrUtils, U_BaseInput, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxDateRanges, dxScrollbarAnnotations, cxContainer, dxBarBuiltInMenu,
  System.ImageList, Vcl.ImgList, cxImageList, dxSkinsCore,
  dxSkinsDefaultPainters;

type
  TfrmMachineInPut = class(TfrmBaseInput)
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ToolButton1: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
  public
    { Public declarations }
    FBCId, FSTKName: string;
  end;

var
  frmMachineInPut: TfrmMachineInPut;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_EmployeeSel;

{$R *.dfm}

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

function TfrmMachineInPut.SaveCKData(): Boolean;
var
  MaxNo, MWBID: 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 Bs_Vehicle where VID=''' + Trim(CDS_Sub.fieldbyname('VID').AsString) + '''');
          open;
        end;
        MWBID := Trim(ADOQueryTemp.fieldbyname('VID').AsString);
        if Trim(MWBID) = '' then
        begin
          if GetLSNo(ADOQueryCmd, MaxNo, 'M', 'Bs_Vehicle', 4, 1) = False then
            raise Exception.Create('取最大号失败！');
        end
        else
        begin
          MaxNo := Trim(MWBID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Bs_Vehicle where VID=''' + Trim(MaxNo) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(MWBID) = '' 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('VID').Value := Trim(MaxNo);
//          FieldByName('STKName').Value := Trim(FSTKName);
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'Bs_Vehicle', 0);
          Post;
        end;

        Edit;
        FieldByName('VID').Value := Trim(MaxNo);
        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 TfrmMachineInPut.TBAddClick(Sender: TObject);
begin
  with CDS_Sub do
  begin
    Append;

    Post;
  end;
end;

procedure TfrmMachineInPut.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmMachineInPut.FormShow(Sender: TObject);
var
  fsj: string;
begin
  inherited;
  ReadCxGrid(trim(self.Caption), Tv1, '染色坯布仓库');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from Bs_Vehicle A');
    sql.Add(' where VID=''' + Trim(FBCId) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_Sub);
  SInitCDSData(ADOQueryTemp, CDS_Sub);
end;

procedure TfrmMachineInPut.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('VID').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 Bs_Vehicle where VID=''' + Trim(CDS_Sub.fieldbyname('VID').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 TfrmMachineInPut.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;

  if CDS_Sub.Locate('VNO', null, []) = True then
  begin
    Application.MessageBox('编号不能为空!', '提示', 0);
    Exit;
  end;

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

procedure TfrmMachineInPut.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '染色坯布仓库');
end;

procedure TfrmMachineInPut.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  inherited;

  try
    frmEmployeeSel := TfrmEmployeeSel.Create(Application);
    with frmEmployeeSel do
    begin
      FPost := '司机';
      if ShowModal = 1 then
      begin

//        FRCode := Trim(TSpeedButton(Sender).Hint);
//  FRName := Trim(TSpeedButton(Sender).Caption);
        with self.CDS_Sub do
        begin
          Edit;
          FieldByName('Driver').Value := FRName;
          Post;
        end;
      end;
    end;
  finally
    frmEmployeeSel.Free;
  end;

end;

end.

