unit U_BPRKDR;

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, strutils, cxCurrencyEdit, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxSkinsCore, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringTime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinsDefaultPainters,
  dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue,
  dxSkinscxPCPainter, ComObj;

type
  TfrmBPRKDR = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v2Column5: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    ToolButton4: TToolButton;
    OpenDialog1: TOpenDialog;
    CDS_LM: TClientDataSet;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    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 v2Column6PropertiesEditValueChanged(Sender: TObject);
    procedure v2defstr9PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column5PropertiesEditValueChanged(Sender: TObject);
    procedure Tv2Column1PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function SaveData(): Boolean;
    function YFData(): Boolean;
  public
    { Public declarations }
    RKFlag, FMLID: string;
    fkhType: string;
    fkeyNO: string;
  end;

var
  frmBPRKDR: TfrmBPRKDR;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

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

procedure TfrmBPRKDR.FormDestroy(Sender: TObject);
begin
  frmBPRKDR := nil;
end;

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

procedure TfrmBPRKDR.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('产品资料录入1', Tv1, '客户自定义');
  Close;
  ModalResult := 1;
end;

procedure TfrmBPRKDR.FormShow(Sender: TObject);
begin
  ReadCxGrid('产品资料录入1', Tv1, '客户自定义');
  InitGrid();
//  if Trim(FMLID) = '' then
//  begin
//    with CDS_HZ do
//    begin
//      Edit;
//      FieldByName('type').Value := '面料';
//      Post;
//    end;
//
//  end;
end;

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

function TfrmBPRKDR.SaveData(): Boolean;
var
  fkcID: integer;
  Filler: string;
  FillTime: TdateTime;
begin
  result := false;
  try
    ADOQueryCmd.Connection.BeginTrans;

    if GetLSNo(ADOQueryCmd, fkeyNO, 'BK', 'BP_InOut', 3, 1) = False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    Filler := DName;
    FillTime := SGetServerDateTime(adoqueryTemp);

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from BP_InOut');
      sql.Add('where CRNO=' + quotedstr(trim(fkeyNO)));
      execsql;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' delete  from YF_Money_CR ');
      sql.Add(' where MainID=' + quotedstr(trim(fkeyNO)));
      execsql;
    end;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from BP_kc');
      sql.Add('where CRNO=' + quotedstr(trim(fkeyNO)));
      execsql;
    end;

    CDS_HZ.DisableControls;
    CDS_HZ.First;
    with CDS_HZ do
    begin
      while not eof do
      begin

        Edit;
        FieldByName('CRNO').Value := trim(fkeyNO);
        FieldByName('CRNOS').Value := trim(fkeyNO + inttostr(recNo));
        FieldByName('CRID').Value := recNo;
        post;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          SQL.add('Update BP_Kcid Set kcid=kcid+1');
          sql.Add('select kcid  from BP_Kcid');
          Open;
        end;
        fkcID := ADOQueryCmd.fieldbyname('kcid').AsInteger;
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from BP_InOut');
          sql.Add('where 1=2 ');
          open;
        end;
   //     fkcID:=strtoint(RightBStr(trim(fkeyNO),length(fkeyNO)-3)+inttostr(recNo));
        ADOQueryCmd.Append;
        RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_HZ, 'BP_InOut', 0);
        ADOQueryCmd.FieldByName('CRNO').Value := trim(fkeyNO);
        ADOQueryCmd.FieldByName('CRNOS').Value := trim(fkeyNO + inttostr(recNo));
        ADOQueryCmd.FieldByName('CRID').Value := recNo;
        ADOQueryCmd.FieldByName('CRTime').Value := trim(fieldbyname('CRTime').AsString);
        ADOQueryCmd.FieldByName('CKName').Value := '布匹仓库';
        ADOQueryCmd.FieldByName('CRType').Value := '导入入库';
        ADOQueryCmd.FieldByName('CRFlag').Value := '入库';
        ADOQueryCmd.FieldByName('Filler').Value := trim(Filler);
        ADOQueryCmd.FieldByName('FillTime').Value := dateTimetostr(FillTime);
        ADOQueryCmd.FieldByName('kcID').Value := fkcID;
        ADOQueryCmd.Post;

        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from BP_KC');
          sql.Add('where 1=2 ');
          open;
        end;
        ADOQueryCmd.Append;
        ADOQueryCmd.FieldByName('kcID').Value := fkcID;
        ADOQueryCmd.FieldByName('CKName').Value := '布匹仓库';
        ADOQueryCmd.FieldByName('CRNO').Value := trim(fkeyNO);
        ADOQueryCmd.FieldByName('CRNOS').Value := trim(fkeyNO + inttostr(recNo));
        ADOQueryCmd.FieldByName('CRID').Value := recNo;
        ADOQueryCmd.FieldByName('CRTime').Value := trim(fieldbyname('CRTime').AsString);
        ADOQueryCmd.FieldByName('CRType').Value := '导入入库';
        ADOQueryCmd.FieldByName('CRFlag').Value := '入库';
        ADOQueryCmd.FieldByName('MJXH').Value := trim(fieldbyname('MJXH').AsString);
        ADOQueryCmd.FieldByName('MJID').Value := trim(fieldbyname('MJID').AsString);
        ADOQueryCmd.FieldByName('CPType').Value := '正品';
        ADOQueryCmd.FieldByName('P_Code').Value := trim(fieldbyname('P_Code').AsString);
        ADOQueryCmd.FieldByName('P_CodeName').Value := trim(fieldbyname('P_CodeName').AsString);
        ADOQueryCmd.FieldByName('P_spec').Value := trim(fieldbyname('P_spec').AsString);
        ADOQueryCmd.FieldByName('P_Color').Value := trim(fieldbyname('P_Color').AsString);
        ADOQueryCmd.FieldByName('gangNo').Value := trim(fieldbyname('gangNo').AsString);
        ADOQueryCmd.FieldByName('P_MF').Value := fieldbyname('P_MF').AsFloat;
        ADOQueryCmd.FieldByName('P_KZ').Value := fieldbyname('P_KZ').AsFloat;
        ADOQueryCmd.FieldByName('RollNum').Value := fieldbyname('RollNum').AsFloat;
        ADOQueryCmd.FieldByName('Qty').Value := fieldbyname('Qty').AsFloat;
        ADOQueryCmd.FieldByName('QtyUnit').Value := trim(fieldbyname('QtyUnit').AsString);
        ADOQueryCmd.FieldByName('kcID').Value := fkcID;
        ADOQueryCmd.Post;

//        if trim(CRType.Text) = '采购入库' then
//        begin
//          if not YFData() then
//          begin
//            CDS_Main.EnableControls;
//            ADOQueryCmd.Connection.RollbackTrans;
//            exit;
//          end;
//        end;
//        with ADOQuery1 do
//        begin
//          close;
//          sql.Clear;                        
//          sql.Add('select * from CK_SXPB_CRMX A ');
//          sql.Add(' right JOIN BP_InOut B on B.CRID=A.CRID  and B.crno=A.crno');
//          sql.Add(' where A.CRNO=' + quotedstr(trim(CDS_Main.fieldbyname('CRNO').AsString)));
//          sql.Add(' and A.CRID=' + quotedstr(trim(CDS_Main.fieldbyname('CRID').AsString)));
//          SQL.Add(' and crzt=''入库''');
//          sql.Add(' order by MXID');
////      ShowMessage(SQL.text);
//          open;
//        end;
//        if ADOQuery1.RecordCount > 0 then
//        begin
//          with ADOQuery2 do
//          begin
//            Close;
//            sql.Clear;
//            sql.Add('Update CK_SXPB_CRMX Set CKNO=(select kuwei from BP_InOut A where A.crno=CK_SXPB_CRMX.CRNO  and A.CRID=CK_SXPB_CRMX.CRID)');
//            sql.Add(' where CRNO=' + quotedstr(trim(CDS_Main.fieldbyname('CRNO').AsString)));
//            sql.Add(' and CRID=' + quotedstr(trim(CDS_Main.fieldbyname('CRID').AsString)));
////            ShowMessage(sql.Text);
//            ExecSQL;
//          end;
//        end;

        next;
      end;

    end;
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    result := true;
  except
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;
{function TfrmBPRKDR.SaveData(): Boolean;
var
  fkeyNO, CRID: string;
    fkcID: integer;
begin
  if CDS_HZ.IsEmpty then
    exit;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with CDS_HZ do
    begin
      first;
      while not eof do
      begin
        if (CDS_HZ.FieldByName('P_CodeName').asstring = '') then
        begin
          Next;
        end
        else
        begin
          if Trim(CDS_HZ.fieldbyname('CRNO').AsString) = '' then
          begin
            if GetLSNo(ADOQueryCmd, fkeyNO, 'BK', 'BP_InOut', 3, 1) = False then
            begin
              Result := False;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取最大号失败！', '提示', 0);
              Exit;
            end;

          end
          else
          begin
            fkeyNO := Trim(FMLID);
          end;

          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('select * from BP_InOut where CRNO=''' + Trim(CDS_HZ.fieldbyname('CRno').AsString) + '''');
            Open;
          end;
          with ADOQueryCmd do
          begin
            if Trim(CDS_HZ.fieldbyname('CRNO').AsString) = '' then
            begin
              Append;
              FieldByName('Filler').Value := Trim(DName);
              FieldByName('Filltime').Value := SGetServerDateTime(ADOQueryTemp);
            end
            else
            begin
              Edit;
              FieldByName('Editer').Value := Trim(DName);
              FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTemp);
            end;
            FieldByName('CRNO').Value := trim(fkeyNO);
            FieldByName('CRNOS').Value := trim(fkeyNO);
            FieldByName('CRID').Value := 1;
            FieldByName('CRFlag').Value := '入库';
            FieldByName('CRType').Value := '导入入库';
            FieldByName('CKName').Value := '布匹仓库';

            RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_HZ, 'BP_InOut', 0);
            Post;
             with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.add('Update BP_Kcid Set kcid=kcid+1');
              sql.Add('select kcid  from BP_Kcid');
              Open;
            end;
            fkcID := ADOQueryCmd.fieldbyname('kcid').AsInteger;
            with ADOQueryCmd do
            begin
              close;
              sql.Clear;
              sql.Add('select * from BP_KC');
              sql.Add('where 1=2 ');
              open;
            end;
            ADOQueryCmd.Append;
            ADOQueryCmd.FieldByName('kcID').Value := fkcID;
            ADOQueryCmd.FieldByName('CKName').Value := '布匹仓库';
            ADOQueryCmd.FieldByName('CRNO').Value := trim(fkeyNO);
            ADOQueryCmd.FieldByName('CRNOS').Value := trim(fkeyNO);
            ADOQueryCmd.FieldByName('CRID').Value := 1;
            ADOQueryCmd.FieldByName('CRTime').Value := Trim(CDS_HZ.fieldbyname('CRTime').AsString);
            ADOQueryCmd.FieldByName('CRType').Value := '导入入库';
            ADOQueryCmd.FieldByName('CRFlag').Value := '入库';
            ADOQueryCmd.FieldByName('MJXH').Value := trim(fieldbyname('MJXH').AsString);
            ADOQueryCmd.FieldByName('MJID').Value := trim(fieldbyname('MJID').AsString);
            ADOQueryCmd.FieldByName('CPType').Value := trim(fieldbyname('CPType').AsString);
            ADOQueryCmd.FieldByName('P_Code').Value := trim(fieldbyname('P_Code').AsString);
            ADOQueryCmd.FieldByName('P_CodeName').Value := trim(fieldbyname('P_CodeName').AsString);
            ADOQueryCmd.FieldByName('P_spec').Value := trim(fieldbyname('P_spec').AsString);
            ADOQueryCmd.FieldByName('P_Color').Value := trim(fieldbyname('P_Color').AsString);
            ADOQueryCmd.FieldByName('gangNo').Value := trim(fieldbyname('gangNo').AsString);
            ADOQueryCmd.FieldByName('P_MF').Value := fieldbyname('P_MF').AsFloat;
            ADOQueryCmd.FieldByName('P_KZ').Value := fieldbyname('P_KZ').AsFloat;
            ADOQueryCmd.FieldByName('RollNum').Value := fieldbyname('RollNum').AsFloat;
            ADOQueryCmd.FieldByName('Qty').Value := fieldbyname('Qty').AsFloat;
            ADOQueryCmd.FieldByName('QtyUnit').Value := trim(fieldbyname('QtyUnit').AsString);
            ADOQueryCmd.FieldByName('kcID').Value := fkcID;
            ADOQueryCmd.Post;  
//            if not YFData() then
//            begin
//              CDS_HZ.EnableControls;
//              ADOQueryCmd.Connection.RollbackTrans;
//              exit;
//            end;
          end;
          next;
        end;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;

    application.MessageBox('保存成功！', '提示');
    Modalresult := 1;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end; }

function TfrmBPRKDR.YFData(): Boolean;
var
  CRID, YFID, Price, PriceUnit, OrderUnit, FComTaiTou: string;
begin
  Result := False;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName=''' + Trim(CDS_HZ.fieldbyname('cust').AsString) + ''' and YFDefFlag1=0 ');
    Open;
  end;

  if not ADOQueryTemp.IsEmpty then
  begin
    CRID := ADOQueryTemp.fieldbyname('CRID').AsString;
  end
  else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CRID set CRID=CRID+1');
      sql.Add('select * from YF_Money_CRID ');
      Open;
    end;
    CRID := ADOQueryCmd.fieldbyname('CRID').AsString;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete  from YF_Money_CR ');
    sql.Add(' where MainID=''' + Trim(CDS_HZ.fieldbyname('CRNO').AsString) + '''');
    sql.Add(' and subID=''' + Trim(CDS_HZ.fieldbyname('CRID').AsString) + '''');
    sql.Add(' and YFName=''采购费'' ');
    execsql;
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR ');
    sql.Add(' where MainID=''' + Trim(CDS_HZ.fieldbyname('CRNO').AsString) + '''');
    sql.Add(' and subID=''' + Trim(CDS_HZ.fieldbyname('CRID').AsString) + '''');
    sql.Add(' and YFName=''采购费'' ');
    Open;
  end;

  if ADOQueryTemp.IsEmpty then
  begin
    if GetLSNo(ADOQueryCmd, YFID, 'CF', 'YF_Money_CR', 3, 1) = False then
    begin
      Application.MessageBox('取应收最大号失败!', '提示', 0);
      Exit;
    end;
  end
  else
  begin
    YFID := Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where YFID=''' + trim(YFID) + '''');
    Open;
  end;

  with ADOQueryCmd do
  begin
    if IsEmpty then
      Append
    else
      edit;
    FieldByName('YFID').Value := Trim(YFID);
    FieldByName('YFTypeId').Value := Trim(CDS_HZ.fieldbyname('CRNO').AsString);
    FieldByName('CRID').Value := StrToInt(CRID);
    FieldByName('Filler').Value := Trim(DName);
    FieldByName('CRType').Value := '应付款登记';
    FieldByName('CRFlag').Value := '应付付';
    FieldByName('QtyFlag').Value := 1;
    FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('cust').AsString);
    FieldByName('CRTime').Value := Trim(CDS_HZ.fieldbyname('CRTime').AsString);
    FieldByName('YFType').Value := '自动生成';
    FieldByName('ps').Value := CDS_HZ.fieldbyname('RollNum').AsFloat;
    FieldByName('Qty').Value := CDS_HZ.fieldbyname('Qty').AsFloat;
    FieldByName('Price').Value := CDS_HZ.fieldbyname('Price').AsFloat;
    FieldByName('Money').Value := CDS_HZ.fieldbyname('Money').AsFloat;
    FieldByName('BBMoney').Value := CDS_HZ.fieldbyname('Money').AsFloat;
    FieldByName('HuiLv').Value := 1;
    FieldByName('BZType').Value := '人民币';
    FieldByName('QtyUnit').Value := Trim(CDS_HZ.fieldbyname('QtyUnit').AsString);
    FieldByName('ComTaiTou').Value := Trim(CDS_HZ.fieldbyname('cust').AsString);
    FieldByName('YFName').Value := '采购费';
    FieldByName('MainId').Value := Trim(CDS_HZ.fieldbyname('CRNO').AsString);
    FieldByName('subID').Value := Trim(CDS_HZ.fieldbyname('CRID').AsString);
    FieldByName('status').Value := '0';
    Post;
  end;

  Result := True;
end;

procedure TfrmBPRKDR.ToolButton3Click(Sender: TObject);
begin
  cxGrid1.SetFocus;

  if Application.MessageBox('确定要保存数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

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

  end;

 { try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from KH_Zdy_MLXX where MLID='' '' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
  if Trim(FMLID) = '' then
  begin
    with CDS_HZ do
    begin
      Edit;
      FieldByName('Type').Value := '面料';
      Post;
    end;
  end;  }
end;

procedure TfrmBPRKDR.v2Column6PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;

  if trim(mvalue) = '' then
    exit;

  if rightBstr(trim(mvalue), 1) <> '#' then
  begin
    mvalue := trim(mvalue) + '#';
  end;

  with CDS_HZ do
  begin
    Edit;
    FieldByName('ZdyName').Value := Trim(mvalue);
    Post;
  end;
  self.tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmBPRKDR.v2defstr9PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;

  if trim(mvalue) = '' then
    exit;

  if rightBstr(trim(mvalue), 1) <> '#' then
  begin
    mvalue := trim(mvalue) + '#';
  end;

  with CDS_HZ do
  begin
    Edit;
    FieldByName('ZdyName').Value := Trim(mvalue);
    Post;
  end;
  self.tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmBPRKDR.v2Column5PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
//  mvalue := TcxTextEdit(Sender).EditingText;
//
//  if trim(mvalue) = '' then
//    exit;
//
//  if rightBstr(trim(mvalue), 1) <> '#' then
//  begin
//    mvalue := trim(mvalue) + '#';
//  end;
//
//  with CDS_HZ do
//  begin
//    Edit;
//    FieldByName('DEFstr3').Value := Trim(mvalue);
//    Post;
//  end;
//  self.tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmBPRKDR.Tv2Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;

  if trim(mvalue) = '' then
    exit;

  if rightBstr(trim(mvalue), 1) <> '#' then
  begin
    mvalue := trim(mvalue) + '#';
  end;

  with CDS_HZ do
  begin
    Edit;
    FieldByName('CPHH').Value := Trim(mvalue);
    Post;
  end;
  self.tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmBPRKDR.ToolButton1Click(Sender: TObject);
begin
  CopyAddRow(Tv1, CDS_HZ);
  with CDS_HZ do
  begin
    Edit;
    FieldByName('CRNO').Value := '';
    FieldByName('note').Value := '';
    Post;
  end;
end;

procedure TfrmBPRKDR.ToolButton2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;

  if Trim(CDS_HZ.fieldbyname('SubId').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete KH_Zdy_MLXX where MLID=''' + Trim(CDS_HZ.fieldbyname('MLID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
end;

procedure TfrmBPRKDR.ToolButton4Click(Sender: TObject);
var
  excelApp, WorkBook: Variant;
  i, j, k, LX, ExcelRowCount: integer;
  maxId, FCPID, FCPName, t1, t2, t3, FFID: string;
begin
  with CDS_LM do
  begin
    FieldDefs.Clear;
    FieldDefs.Add('LXH', ftInteger, 0);
    FieldDefs.Add('lCode', ftString, 40);
    FieldDefs.Add('LName', ftString, 40);
    close;
    CreateDataSet;
  end;
  try
    excelApp := CreateOleObject('Excel.Application');
    openDialog1.Filter := '*.CSV;*.xls';
    if opendialog1.Execute then
    begin
      WorkBook := excelApp.WorkBooks.Open(OpenDialog1.FileName);
    end
    else
      exit;
    excelApp.Visible := false;
    ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
  except
    application.MessageBox('加载EXCEL错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  CDS_LM.EmptyDataSet;

  for j := 0 to Tv1.ColumnCount - 1 do
  begin
    with CDS_LM do
    begin
      Append;
      FieldByName('LCode').Value := trim(Tv1.Columns[j].DataBinding.FieldName);
      FieldByName('LName').Value := trim(Tv1.Columns[j].Caption);
      Post;
    end;
  end;

  try
    for i := 1 to 50 do
    begin
      if trim(WorkBook.WorkSheets[1].Cells[1, i].value) = '' then
        continue;
      if CDS_LM.Locate('LName', trim(WorkBook.WorkSheets[1].Cells[1, i].value), []) then
      begin
        with CDS_LM do
        begin
          Edit;
          FieldByName('LXH').Value := i;
          Post;
        end;
      end;
    end;
  except
    application.MessageBox('加载数组错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  try
    for i := 2 to ExcelRowCount do
    begin
      with CDS_HZ do
      begin
        Append;
        CDS_LM.First;
        while not CDS_LM.Eof do
        begin
          if (CDS_LM.FieldByName('LXH').AsInteger > 0) then
            CDS_HZ.fieldbyname(CDS_LM.FieldByName('LCode').AsString).Value := WorkBook.WorkSheets[1].Cells[i, CDS_LM.FieldByName('LXH').AsInteger].Value;
          CDS_LM.Next;
        end;
        Post;
      end;
      with CDS_HZ do
      begin
        First; // 移动到第一条记录
        while not Eof do
        begin
          if Trim(FieldByName('P_CodeName').AsString) = '' then
          begin
            Edit;
            Delete;
          end
          else
          begin
            Next; // 移动到下一条记录
          end;
        end;
      end;
    end;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    exit;
  end;

end;

end.

