unit U_YGCL_BZInput;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxDropDownEdit, BtnEdit;

type
  TfrmYGCL_BZInput = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2GXName: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2BZName: TcxGridDBColumn;
    v2SYRName: TcxGridDBColumn;
    v2DJDate: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure v2PSNotePropertiesEditValueChanged(Sender: TObject);
    procedure vPricePropertiesEditValueChanged(Sender: TObject);
    procedure v2SYRNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2PRTColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure v2BZNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function SaveData(): Boolean;
  public
    { Public declarations }
    RKFlag, FCYID: string;
    fDEFstr5: string;
  end;

var
  frmYGCL_BZInput: TfrmYGCL_BZInput;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_GXGL_List_DXCX;

{$R *.dfm}

procedure TfrmYGCL_BZInput.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add('select * from OA_YG_BZ where BZID=''' + Trim(FCYID) + '''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYGCL_BZInput.FormDestroy(Sender: TObject);
begin
  frmYGCL_BZInput := nil;
end;

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

procedure TfrmYGCL_BZInput.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('班组录入', Tv2, '客户自定义');
  Close;
end;

procedure TfrmYGCL_BZInput.FormShow(Sender: TObject);
begin
  ReadCxGrid('班组录入', Tv2, '客户自定义');
  InitGrid();
end;

procedure TfrmYGCL_BZInput.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

function TfrmYGCL_BZInput.SaveData(): Boolean;
var
  maxId, CRID, fzdyCode, FFFlag, FStr: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      first;
      while not Eof do
      begin
        if Trim(fieldbyname('BZID').AsString) = '' then
        begin
          if GetLSNo(ADOQueryCmd, maxId, 'BZ', 'OA_YG_BZ', 4, 1) = False then
          begin
            Result := False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxId := Trim(FCYID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from OA_YG_BZ where BZID=''' + Trim(FCYID) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FCYID) = '' then
          begin
            Append;
            FieldByName('DJER').Value := Trim(DName);
          end
          else
          begin
            Edit;
          end;
          FieldByName('BZID').Value := Trim(maxId);
          RTSetSaveDataCDS(ADOQueryCmd, Tv2, CDS_HZ, 'OA_YG_BZ', 0);
          FieldByName('PSID').Value := CDS_HZ.fieldbyname('PSID').AsString;
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from OA_YG_BZ where BZName=''' + Trim(CDS_HZ.fieldbyname('BZName').AsString) + '''');
          Open;
        end;
        if ADOQueryCmd.RecordCount > 1 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          application.MessageBox(PChar('班组名称' + trim(ADOQueryCmd.fieldbyname('BZName').AsString) + '已存在'), '提示');
          Exit;
        end;
        with CDS_HZ do
        begin
          Edit;
          FieldByName('BZID').Value := Trim(maxId);
        end;
        next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ;
    Result := True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;
end;

procedure TfrmYGCL_BZInput.ToolButton3Click(Sender: TObject);
begin
  cxGrid2.SetFocus;
  if CDS_HZ.Locate('BZName', '', []) = true then
  begin
    Application.MessageBox('班组名称不能为空', '提示');
    exit;
  end;
  if CDS_HZ.Locate('BZName', null, []) = true then
  begin
    Application.MessageBox('班组名称不能为空', '提示');
    exit;
  end;
  if Application.MessageBox('确定要保存数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

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

procedure TfrmYGCL_BZInput.v2PSNotePropertiesEditValueChanged(Sender: TObject);
var
  mavlue: string;
begin
  mavlue := Trim(TcxTextEdit(Sender).EditingText);
  if mavlue = '' then
    mavlue := '0';
  with CDS_HZ do
  begin
    Edit;
    FieldByName('Qty').Value := Trim(mavlue);
    FieldByName('Money').Value := FieldByName('price').AsFloat * strtofloat(mavlue);
  end;
end;

procedure TfrmYGCL_BZInput.vPricePropertiesEditValueChanged(Sender: TObject);
var
  mavlue: string;
begin
  mavlue := Trim(TcxTextEdit(Sender).EditingText);
  if mavlue = '' then
    mavlue := '0';
  with CDS_HZ do
  begin
    Edit;
    FieldByName('price').Value := Trim(mavlue);
    FieldByName('Money').Value := FieldByName('price').AsFloat * strtofloat(mavlue);
  end;
end;

procedure TfrmYGCL_BZInput.v2SYRNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'CJName';
      flagname := '车间名称';
      if ShowModal = 1 then
      begin
        with CDS_HZ do
        begin
          Edit;
          FieldByName('SYRName').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    ;
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYGCL_BZInput.v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTCodeName';
      flagname := '品名';
      if ShowModal = 1 then
      begin
        with CDS_HZ do
        begin
          Edit;
          FieldByName('MPRTCodeName').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    ;
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYGCL_BZInput.v2PRTColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PRTColor';
      flagname := '颜色';
      if ShowModal = 1 then
      begin
        with CDS_HZ do
        begin
          Edit;
          FieldByName('PRTColor').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    ;
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYGCL_BZInput.ToolButton2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  if application.MessageBox('确定删除吗', '提示', 1) = 2 then
    exit;
  with ADOQueryCmd do
  begin
    close;
    SQL.Clear;
    sql.Add('delete from OA_YG_BZ where BZID=''' + trim(CDS_HZ.fieldbyname('BZID').AsString) + '''');
    ExecSQL;
  end;
  CDS_HZ.Delete;
end;

procedure TfrmYGCL_BZInput.v2BZNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'BZName';
      flagname := '班组名称';
      if ShowModal = 1 then
      begin
        with CDS_HZ do
        begin
          Edit;
          FieldByName('BZName').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYGCL_BZInput.ToolButton1Click(Sender: TObject);
begin
  frmGXGL_List_DXCX := TfrmGXGL_List_DXCX.Create(self);
  with frmGXGL_List_DXCX do
  begin
    if ShowModal = 1 then
    begin
      with Order_FZ do
      begin
        first;
        while not eof do
        begin
          with self.CDS_HZ do
          begin
            Append;
            FieldByName('GXName').AsString := Trim(Order_FZ.fieldbyname('PSName').AsString);
            FieldByName('SYRName').AsString := Trim(Order_FZ.fieldbyname('SYRName').AsString);
            FieldByName('PSID').AsString := Trim(Order_FZ.fieldbyname('PSID').AsString);
          end;
          next;
        end;
      end;
    end;
    free;
  end;
end;

end.

