unit U_YGYPInPut_BGYPKCDJ;

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_BGYPKCDJ = 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;
    v1KCName: TcxGridDBColumn;
    v1KCQty: TcxGridDBColumn;
    v1KCUnit: TcxGridDBColumn;
    v1KCNote: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    TADD: TToolButton;
    TDel: TToolButton;
    v1KCPlace: TcxGridDBColumn;
    v1RKDate: TcxGridDBColumn;
    v1RKPeoPle: TcxGridDBColumn;
    ToolButton1: TToolButton;
    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 TADDClick(Sender: TObject);
  private
    TPInt:Integer;
    procedure InitData();
    function  SaveData():Boolean;
    { Private declarations }
  public
    PState,CopyInt:Integer;
    FBGMainID,FKCID:String;
    { Public declarations }
  end;

var
  frmYGYPInPut_BGYPKCDJ: TfrmYGYPInPut_BGYPKCDJ;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel,U_YGYPZZChkList_BGYPLYSQ;

{$R *.dfm}

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

procedure TfrmYGYPInPut_BGYPKCDJ.InitData();
begin
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* from OA_YG_DangAN_BGYPKC A ');
    sql.add('where A.KCID='''+trim(FKCID)+'''');
    Open;
  end;
  SCreateCDS20(ADOCmd,Order_Sub);
  SInitCDSData20(ADOCmd,Order_Sub);
end;

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

function TfrmYGYPInPut_BGYPKCDJ.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('KCID').AsString='' then
        begin
          if GetLSNo(ADOCmd,maxno,'BC','OA_YG_DangAN_BGYPKC',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败!','提示',0);
            Exit;
          end;
        end
        else
        begin
          maxno:=trim(Order_Sub.fieldbyname('KCID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from OA_YG_DangAN_BGYPKC where KCID='''+Trim(maxno)+'''');
          Open;
          if isempty then
          begin
            append;
          end
          else
          edit;
          fieldbyname('KCID').Value:=trim(Maxno);
          fieldbyname('BGMainid').Value:=trim(Order_Sub.fieldbyname('BGMainid').asstring);
          fieldbyname('DeptType').Value:='总经办';
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'OA_YG_DangAN_BGYPKC',0);
          post;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.add('update OA_YG_DangAN_BGYP_Sub set RKStatus=''已入库'' ');
          sql.add('where BGMainid='''+Trim(Order_Sub.fieldbyname('BGMainid').asstring)+'''');
          execsql;
        end;
        next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmYGYPInPut_BGYPKCDJ.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_BGYPKCDJ.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmYGYPInPut_BGYPKCDJ.FormDestroy(Sender: TObject);
begin
  frmYGYPInPut_BGYPKCDJ:=nil;
end;

procedure TfrmYGYPInPut_BGYPKCDJ.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_BGYPKCDJ.v1BGNamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='BGName';
    flagname:='办公用品';
    if showModal=1 then
    begin
      with ADOCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select Top 1 KCUnit from OA_YG_DangAN_BGYPKC ');
        sql.add('where KCName='''+trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+''' ');
        sql.add('and DeptType=''总经办''');
        open;
      end;
      with Order_Sub do
      begin
        edit;
        fieldbyname('KCName').Value:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        fieldbyname('KCUnit').Value:=trim(ADOCmd.fieldbyname('KCUnit').AsString);
      end;
    end;
    free;
  end;
end;

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

procedure TfrmYGYPInPut_BGYPKCDJ.ToolButton1Click(Sender: TObject);
begin
  try
    frmYGYPZZChkList_BGYPLYSQ:=TfrmYGYPZZChkList_BGYPLYSQ.Create(self);
    with frmYGYPZZChkList_BGYPLYSQ do 
    begin
      FSQType:='采购申请';
      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('KCName').Value:=order_Main.fieldbyname('BGName').AsString;
                fieldbyname('KCQty').Value:=Order_Main.fieldbyname('BGQty').asfloat;
                fieldbyname('KCUnit').value:=Order_Main.fieldbyname('BGUnit').AsString;
                fieldbyname('BGMainid').value:=Order_Main.fieldbyname('BGMainid').asstring;
                fieldbyname('RKPeoPle').AsString:=trim(DName);
                fieldbyname('RKDate').AsDateTime:=SGetServerDate(ADOTemp);
                post;
              end;
            end;
            next;
          end;
        end;
      end;
    end;
  finally;
    frmYGYPZZChkList_BGYPLYSQ.Free;
  end;
end;

procedure TfrmYGYPInPut_BGYPKCDJ.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_BGYPKCDJ.TADDClick(Sender: TObject);
begin
  with Order_Sub do
  begin
    append;
    fieldbyname('RKPeoPle').AsString:=trim(DName);
    fieldbyname('RKDate').AsDateTime:=SGetServerDate(ADOTemp);
    post;
  end;
end;

end.
