unit U_YGYPInPut_BGYPCKDJ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxCalendar,StrUtils, cxDropDownEdit,jpeg,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP, cxPC,
  cxGridCustomPopupMenu, cxGridPopupMenu, Menus, ExtDlgs;

type
  TfrmYGYPInPut_BGYPCKDJ = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    TBSave: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1CKName: TcxGridDBColumn;
    v1CKQty: TcxGridDBColumn;
    v1CKUnit: TcxGridDBColumn;
    v1CKNote: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    TDel: TToolButton;
    v1CKPlace: TcxGridDBColumn;
    v1CKDate: TcxGridDBColumn;
    v1CKPeoPle: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1LYName: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure v1BGUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1BGNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TDelClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure v1KCPlacePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1LYNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    TPInt:Integer;
    procedure InitData();
    function  SaveData():Boolean;
    { Private declarations }
  public
    PState,CopyInt:Integer;
    FBGMainID,FCKID:String;
    { Public declarations }
  end;

var
  frmYGYPInPut_BGYPCKDJ: TfrmYGYPInPut_BGYPCKDJ;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel,U_YGYPZZChkList_BGYPLYSQ,U_YGYPZZChkList_BGYPKC;

{$R *.dfm}

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

procedure TfrmYGYPInPut_BGYPCKDJ.InitData();
begin
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* from OA_YG_DangAN_BGYPCK A ');
    sql.add('where A.CKID='''+trim(FCKID)+'''');
    Open;
  end;
  SCreateCDS20(ADOCmd,Order_Sub);
  SInitCDSData20(ADOCmd,Order_Sub);
end;

procedure TfrmYGYPInPut_BGYPCKDJ.FormShow(Sender: TObject);
begin
  InitData();
end;

function TfrmYGYPInPut_BGYPCKDJ.SaveData():Boolean;
var
  maxno:String;
begin
  Result:=False;
  try
    ADOCmd.Connection.BeginTrans;
    with Order_Sub do
    begin
      first;
      while not eof do
      begin
        if fieldbyname('CKID').AsString='' then
        begin
          if GetLSNo(ADOCmd,maxno,'CK','OA_YG_DangAN_BGYPCK',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败!','提示',0);
            Exit;
          end;
        end
        else
        begin
          maxno:=trim(Order_Sub.fieldbyname('CKID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from OA_YG_DangAN_BGYPCK where CKID='''+Trim(maxno)+'''');
          Open;
          if isempty then
          begin
            append;
          end
          else
          edit;
          fieldbyname('CKID').Value:=trim(Maxno);
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'OA_YG_DangAN_BGYPKC',0);
          fieldbyname('CKPlace').Value:=trim(Order_Sub.fieldbyname('CKPlace').AsString);
          fieldbyname('DeptType').Value:=trim(Order_Sub.fieldbyname('DeptType').AsString);
          post;
        end;
        next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmYGYPInPut_BGYPCKDJ.TBSaveClick(Sender: TObject);
var
  FSFNO:String;
begin
  ToolBar1.SetFocus;
  if order_Sub.isempty then
  begin
    Application.MessageBox('明细不能为空','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    Modalresult:=1;
  end;
end;

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

procedure TfrmYGYPInPut_BGYPCKDJ.FormDestroy(Sender: TObject);
begin
  frmYGYPInPut_BGYPCKDJ:=nil;
end;

procedure TfrmYGYPInPut_BGYPCKDJ.v1BGUnitPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='BGUnit';
    flagname:='数量单位';
    if showModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('KCUnit').Value:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmYGYPInPut_BGYPCKDJ.v1BGNamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='BGName';
    flagname:='办公用品';
    if showModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('KCName').Value:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmYGYPInPut_BGYPCKDJ.TDelClick(Sender: TObject);
begin
  if Order_Sub.IsEmpty then exit;
  Order_Sub.Delete;
end;

procedure TfrmYGYPInPut_BGYPCKDJ.ToolButton1Click(Sender: TObject);
begin
  try
    frmYGYPZZChkList_BGYPKC:=TfrmYGYPZZChkList_BGYPKC.Create(self);
    with frmYGYPZZChkList_BGYPKC do
    begin
      Fformid:='1';
      if showmodal=1 then
      begin
        with Order_Main do
        begin
          first;
          while not eof do
          begin
            if fieldbyname('Ssel').AsBoolean=true then
            begin
              with Order_Sub do
              begin
                append;
                fieldbyname('CKName').Value:=order_Main.fieldbyname('KCName').AsString;
                fieldbyname('CKQty').Value:=Order_Main.fieldbyname('SumKCQty').asfloat;
                fieldbyname('CKUnit').value:=Order_Main.fieldbyname('KCUnit').AsString;
                fieldbyname('CKPlace').value:=Order_Main.fieldbyname('KCPlace').AsString;
                fieldbyname('DeptType').value:=Order_Main.fieldbyname('DeptType').AsString;
                fieldbyname('CKPeoPle').AsString:=trim(DName);
                fieldbyname('CKDate').AsDateTime:=SGetServerDate(ADOTemp);
                post;
              end;
            end;
            next;
          end;
        end;
      end;
    end;
  finally;
    frmYGYPZZChkList_BGYPKC.Free;
  end;
end;

procedure TfrmYGYPInPut_BGYPCKDJ.v1KCPlacePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='KCPlace';
    flagname:='存放地点';
    if showModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('KCPlace').Value:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmYGYPInPut_BGYPCKDJ.v1LYNamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='LYName';
    flagname:='领用人';
    if showModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('LYName').Value:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

end.
