unit U_BpGdCkEdit;

interface

uses
  Windows, Messages, SysUtils, Variants, cxEdit,Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, ExtCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, DB, cxDBData,cxTextEdit,
  cxButtonEdit, cxDropDownEdit, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, DBClient, ADODB, cxGridCustomPopupMenu,
  cxGridPopupMenu, cxCheckBox, BtnEdit, cxCurrencyEdit,strUtils, RM_Common,
  RM_Class, RM_e_Xls, RM_GridReport, RM_System, RM_Dataset;

type
  TfrmBpGdCkEdit = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    TOk: TToolButton;
    v1P_Color: TcxGridDBColumn;
    v1ckqty: TcxGridDBColumn;
    v1price: TcxGridDBColumn;
    v1money: TcxGridDBColumn;
    Panel2: TPanel;
    GroupBox1: TGroupBox;
    CRTime: TDateTimePicker;
    Label8: TLabel;
    CRType: TComboBox;
    Label5: TLabel;
    Label9: TLabel;
    cust: TBtnEditA;
    ADOQuery1: TADOQuery;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    Panel1: TPanel;
    Label23: TLabel;
    ADOPrint: TADOQuery;
    RMDB_MD: TRMDBDataSet;
    RMGridReport1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    Label1: TLabel;
    note: TEdit;
    Label2: TLabel;
    custaddress: TEdit;
    TBKC: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TOkClick(Sender: TObject);
    procedure custBtnClick(Sender: TObject);
    procedure v1ckqtyPropertiesEditValueChanged(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1P_CodeNamePropertiesEditValueChanged(Sender: TObject);
    procedure v1P_ColorPropertiesEditValueChanged(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure v1P_CodeNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo;
      var ADone: Boolean);
    procedure TBKCClick(Sender: TObject);
    procedure CRTypeChange(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetComboBox();
    function  savedate():Boolean;
    function YSData(ckNo,CkID:string;Order_Main10:TClientDataSet):Boolean;
    procedure DoKc();
    
    { Private declarations }
  public
    fType:integer;
    fCKName:string;
    fkeyNO:string;
    
    { Public declarations }
  end;

var
  frmBpGdCkEdit: TfrmBpGdCkEdit;

implementation
uses
  U_DataLink,U_Fun10,U_ZDYHelp, U_BpRk, U_ZdyAttachCP, U_ZdyAttachment,
  U_BpkcCk;
{$R *.dfm}
procedure TfrmBpGdCkEdit.DoKc();
begin
  Label23.Caption:='数量：0';
  IF CDS_Main.IsEmpty then exit;
  IF  trim(CDS_Main.fieldbyname('P_CodeName').AsString)='' then exit;
  IF  trim(CDS_Main.fieldbyname('P_Color').AsString)='' then exit;
  with ADOQuery1 do
  begin
    close;
    sql.Clear;
    sql.Add('select AA.*   ');
    sql.Add('FROM( select A.ckName,P_CodeName,A.P_Color,A.QtyUnit,ckQty=0.00,Price=0.00,money=0.00, ');
    sql.Add('   SUM(case when CRFlag=''入库'' then Qty else -1 * Qty end ) as Qty ');
    sql.Add('from BP_InOut A                                            ');
    sql.Add('where  P_CodeName='+quotedstr(trim(CDS_Main.fieldbyname('P_CodeName').AsString)) );
    sql.Add('and  P_Color='+quotedstr(trim(CDS_Main.fieldbyname('P_Color').AsString)) );
    if trim(FckName)<>'' then
    sql.Add('and ckName='+quotedstr(trim(FckName)) );
    sql.Add('group by A.ckName,P_CodeName,A.P_Color,A.QtyUnit)AA               ');
    open;
  end;
  IF not ADOQuery1.IsEmpty then
  begin
//   Label22.Caption:='匹数：'+trim(ADOQuery1.fieldbyname('kcRollNum').AsString);
   Label23.Caption:='数量：'+trim(ADOQuery1.fieldbyname('Qty').AsString);
  end;
end;
function TfrmBpGdCkEdit.YSData(ckNo,CkID:string;Order_Main10:TClientDataSet):Boolean;
var
  CRID,YFID,Price,PriceUnit,OrderUnit,FComTaiTou:String;
begin
  Result:=False;

  with Order_Main10 do
  begin
    with ADOQuery1 do
    begin
        Close;
        SQL.Clear;
        sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(cust.text)+'''');
        sql.Add('and ZdyStr1=''应收收''');
        sql.Add('and YFDefFlag1=1 ');
        Open;
    end;
    if not ADOQuery1.IsEmpty then
    begin
      CRID:=ADOQuery1.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;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from YF_Money_KC where 1<>1');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('FactoryName').Value:=Trim(cust.text);
          FieldByName('ZdyStr1').Value:='应收收';
          FieldByName('YFDefFlag1').Value:=1;
          Post;
        end;
    end;

    with ADOQuery1 do
    begin
      Close;
        sql.Clear;
        sql.Add('delete  from YF_Money_CR ');
        sql.Add(' where MainID='''+Trim(ckNo)+'''');
        sql.Add(' and subID='''+Trim(ckID)+'''');
        sql.Add(' and YFName=''广东销售'' ');
        execsql;
    end;
    with ADOQuery1 do
    begin
      Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR ');
        sql.Add(' where MainID='''+Trim(ckNo)+'''');
        sql.Add(' and subID='''+Trim(ckID)+'''');
        sql.Add(' and YFName=''广东销售'' ');
        Open;
    end;
      if ADOQuery1.IsEmpty then
      begin
        if GetLSNo(ADOQueryCmd,YFID,'CS','YF_Money_CR',4,1)=False then
        begin
          Application.MessageBox('取坯布应付最大号失败!','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YF_Money_CR where 1<>1');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('YFID').Value:=Trim(YFID);
          FieldByName('YFTypeId').Value:=Trim(ckNo);
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('CRType').Value:='应收款登记';
          FieldByName('CRFlag').Value:='应收收';
          FieldByName('QtyFlag').Value:=1;
          FieldByName('FactoryName').Value:=Trim(cust.Text);
          FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',CRTime.DateTime));
          FieldByName('YFType').Value:='自动生成';
          FieldByName('ps').Value:=0;
          FieldByName('Qty').Value:=Order_Main10.fieldbyname('Qty').AsFloat;
          FieldByName('Price').Value:=Order_Main10.fieldbyname('Price').AsFloat;
          FieldByName('Money').Value:=Order_Main10.fieldbyname('Money').AsFloat;
          FieldByName('BBMoney').Value:=Order_Main10.fieldbyname('Money').AsFloat;
          //FieldByName('HuiLv').Value:=1;
          FieldByName('HuiLv').Value:=1;
          FieldByName('BZType').Value:='人民币';
          FieldByName('QtyUnit').Value:=Trim(Order_Main10.fieldbyname('QtyUnit').AsString);
          FieldByName('ComTaiTou').Value:=Trim(cust.Text);
          FieldByName('P_CodeName').Value:=trim(Order_Main10.fieldbyname('P_CodeName').AsString);
          FieldByName('P_Color').Value:=trim(Order_Main10.fieldbyname('P_Color').AsString);
          FieldByName('YFName').Value:='广东销售';
          FieldByName('MainId').Value:=Trim(ckNo);
          FieldByName('subID').Value:=Trim(CkID);
          FieldByName('YFDefFlag1').Value:=1;
          FieldByName('syrName').Value:='广东门市部';
          Post;
        end;
      end
      else
      begin
        YFID:=Trim(ADOQuery1.fieldbyname('YFID').AsString);
      end;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
        sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
        sql.Add(' where CRID='+CRID);
        ExecSQL;
      end;
   end;
  Result:=True;
end;
function TfrmBpGdCkEdit.savedate():Boolean;
var
  fkcID,i:integer;
  Filler:string;
  FillTime:TdateTime;
  maxno:string;
begin
  result:=false;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if FType=0 then
    begin
      if GetLSNo(ADOQueryCmd,fkeyNO,'GC','BP_InOut',3,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
      Filler:=DName;
      FillTime:=SGetServerDateTime(adoqueryTmp);
    end
    else
    begin
      with adoqueryTmp do
      begin
        close;
        sql.Clear;
        sql.Add('select * from BP_InOut');
        sql.Add('where CRNO='+quotedstr(trim(fkeyNO)));
        open;
        Filler:=adoqueryTmp.fieldbyname('Filler').AsString;
        FillTime:=adoqueryTmp.fieldbyname('FillTime').AsDateTime;
      end;
    end;

    with ADOQueryCmd do
    begin
      Close;
        sql.Clear;
        sql.Add('delete  from YF_Money_CR ');
        sql.Add(' where MainID='''+Trim(fkeyNO)+'''');
        sql.Add(' and YFName=''广东销售'' ');
        sql.Add('and YFDefFlag1=1 ');
        execsql;
    end;

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

    with  CDS_Main do
    begin
      while not eof do
      begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('select * from BP_InOut');
            sql.Add('where 1=2 ');
            open;
          end;   
          ADOQueryCmd.Append;
          ADOQueryCmd.FieldByName('CRNO').Value:=trim(fkeyNO);
          ADOQueryCmd.FieldByName('CRID').Value:=i;
          ADOQueryCmd.FieldByName('CKName').Value:=trim(fckName);
       //   ADOQueryCmd.FieldByName('mainID').Value:=trim(fmainID);
       //   ADOQueryCmd.FieldByName('subID').Value:=trim(fsubID);
          ADOQueryCmd.FieldByName('CRTime').Value:=formatdateTime('yyyy-MM-dd',CRTime.Date);
          ADOQueryCmd.FieldByName('CRType').Value:=trim(crType.Text);
          ADOQueryCmd.FieldByName('CRFlag').Value:='出库';
       //   ADOQueryCmd.FieldByName('MJID').Value:=trim(fieldbyname('MJID').AsString);
       //   ADOQueryCmd.FieldByName('MJXH').Value:=trim(fieldbyname('MJXH').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('P_MF').Value:=fieldbyname('P_MF').AsFloat;
      //    ADOQueryCmd.FieldByName('P_KZ').Value:=fieldbyname('P_KZ').AsFloat;
          ADOQueryCmd.FieldByName('RollNum').Value:=0;
          ADOQueryCmd.FieldByName('Qty').Value:=fieldbyname('Qty').AsFloat;
          ADOQueryCmd.FieldByName('Price').Value:=fieldbyname('Price').AsFloat;
          ADOQueryCmd.FieldByName('money').Value:=fieldbyname('money').AsFloat;
          ADOQueryCmd.FieldByName('QtyUnit').Value:=trim(fieldbyname('QtyUnit').AsString);
      //    ADOQueryCmd.FieldByName('note').Value:=trim(fieldbyname('note').AsString);
          ADOQueryCmd.FieldByName('Filler').Value:=trim(Filler);
          ADOQueryCmd.FieldByName('FillTime').Value:=dateTimetostr(FillTime);
          ADOQueryCmd.FieldByName('cust').Value:=trim(cust.Text);
          ADOQueryCmd.FieldByName('custaddress').Value:=trim(custaddress.Text);
          ADOQueryCmd.FieldByName('note').Value:=trim(note.Text);
   //     ADOQueryCmd.FieldByName('FirstNo').Value:=trim(FirstName.TxtCode);
   //     ADOQueryCmd.FieldByName('FirstName').Value:=trim(FirstName.Text);
          if FType=10 then
          begin
            ADOQueryCmd.FieldByName('editer').Value:=trim(dName);
            ADOQueryCmd.FieldByName('editTime').Value:=SGetServerDateTime(ADOQuery1);
          end;
          ADOQueryCmd.Post;
        IF trim(CRType.Text)='广东零售' then
        begin
          if not YSData(fkeyNo,inttostr(i),CDS_Main) then
          begin
            CDS_Main.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            exit;
            end;
          end;
          i:=i+1;
        next;
       end;
    end;

    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    result:=true;
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;
procedure TfrmBpGdCkEdit.SetComboBox();
begin
 { ckName.Items.Clear;
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''BPCK'' ');
    if trim(fCKName)<>'' then
    sql.Add('and zdyName='+quotedstr(trim(fCKName)));
    open;
    while not eof do
    begin
      ckName.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
  if ckName.Items.Count>0 then ckName.ItemIndex:=0;      }

  CRType.Items.Clear;
 // CRType.Items.Add('');
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''BPCKTYPE''');
    if trim(fCKName)<>'' then
    sql.Add('and note='+quotedstr(trim(fCKName)));
    open;
    while not eof do
    begin
      CRType.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
//  CRType.Items.Add('广东盘点出库');
  if CRType.Items.Count>0 then CRType.ItemIndex:=0;
end;
procedure TfrmBpGdCkEdit.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.* from BP_InOut A');
      sql.Add('where A.CRNO='+quotedstr(trim(fkeyNO)));
      open;
    end;
    SCreateCDS20(adoqueryTmp,CDS_Main);
    SInitCDSData20(adoqueryTmp,CDS_Main);
    if not CDS_Main.IsEmpty then
    begin
      CRTime.Date:= CDS_Main.fieldbyname('CRTime').AsDateTime;
     // CKName.ItemIndex:=CKName.Items.IndexOf(trim(CDS_Main.fieldbyname('CKName').AsString));
      CRType.ItemIndex:=CRType.Items.IndexOf(trim(CDS_Main.fieldbyname('CRType').AsString));
      cust.Text:= trim(CDS_Main.fieldbyname('cust').AsString);
      custaddress.Text:= trim(CDS_Main.fieldbyname('custaddress').AsString);
      note.Text:= trim(CDS_Main.fieldbyname('note').AsString);
    end;
  finally
  end;
end;

procedure TfrmBpGdCkEdit.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  writeCxGrid(self.Caption,Tv1,'布匹仓库');
  if fType=10 then Action:=cahide
  else Action:=cafree;
end;

procedure TfrmBpGdCkEdit.FormDestroy(Sender: TObject);
begin
  frmBpGdCkEdit:=nil;
end;

procedure TfrmBpGdCkEdit.FormShow(Sender: TObject);
begin
{  if fType=10 then
  begin
    TOk.Visible:=true;
  //  v1Ssel.Visible:=true;
    v1ckqty.Visible:=true;
    v1price.Visible:=true;
    v1money.Visible:=true;
    GroupBox1.Visible:=true;
  end
  else
  begin
    TOk.Visible:=false;
  //  v1Ssel.Visible:=false;
    v1ckqty.Visible:=false;
    v1price.Visible:=false;
    v1money.Visible:=false;
    GroupBox1.Visible:=false;
  end;      }
  readCxGrid(self.Caption,Tv1,'布匹仓库');
  SetComboBox();
  InitGrid();
end;

procedure TfrmBpGdCkEdit.FormCreate(Sender: TObject);
begin
  //BegDate.Date:=DServerDate-7;
//  EndDate.Date:=DServerDate;
  CRTime.date:= DServerDate;
  cxGrid1.Align:=alclient;

end;

procedure TfrmBpGdCkEdit.TBCloseClick(Sender: TObject);
begin
    close;
end;

procedure TfrmBpGdCkEdit.TBRafreshClick(Sender: TObject);
begin

  initGrid();
end;

procedure TfrmBpGdCkEdit.TBPrintClick(Sender: TObject);
var
  filepath:string;
begin
  if trim(fkeyNO)='' then exit;

  with AdoPrint do
  begin
    close;
    sql.Clear;
    sql.Add('exec P_Do_PrintMd ');
    sql.Add('@mainID='+quotedstr(trim(fkeyNO)));
    sql.Add(',@flag='+quotedstr(trim('广东出库')));
    open;
  end;

  try
    filepath:=ExtractFilePath(Application.ExeName) + 'report\出库码单建威广州.rmf';
    if not FileExists(Pchar(filepath))  then
    begin
      application.MessageBox(pchar('文件['+filepath+']不存在！'),'提示信息',MB_IConError);
      exit;
    end;
    RMVariables['dtxz'] :=FormatDateTime('yyyy-mm-dd',date());
    RMVariables['zdr'] :=trim(DName);
    RMVariables['Filler'] :=trim(DName);
    RMGridReport1.LoadFromFile(filepath);
    RMGridReport1.ShowReport;
  finally

  end;

end;

procedure TfrmBpGdCkEdit.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  TcxGridToExcel(self.Caption,cxgrid1);
end;

procedure TfrmBpGdCkEdit.TOkClick(Sender: TObject);
begin
  CRTIME.SetFocus;
  IF CDS_Main.IsEmpty then exit;
  if  trim(CRType.Text)='' then
  begin
    application.MessageBox('出库类型不能为空！','提示信息',0);
    exit;
  end;
  if  trim(cust.Text)=''then
  begin
    application.MessageBox('客户不能为空！','提示信息',0);
    exit;
  end;
  if savedate() then
  begin
    application.MessageBox('数据保存成功！','提示信息');
    TOk.Enabled:=false;
    TBPrint.Enabled:=true;
  //  ModalResult:=1;
  end
  else
  begin
    application.MessageBox('数据保存失败！','提示信息',MB_ICONERROR);
  end;
end;

procedure TfrmBpGdCkEdit.custBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachment:=TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      fkhType:='广东客户';
      if ShowModal=1 then
      begin
        cust.Text:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        custaddress.Text:=Trim(CDS_HZ.fieldbyname('defnote1').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmBpGdCkEdit.v1ckqtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue,fieldname,qty1,qty2:String;
  num1:double;

begin
  fieldname:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  with CDS_Main do
  begin
    Edit;
    FieldByName(Trim(fieldname)).Value:=TcxTextEdit(Sender).EditingText;
    num1:=FieldByName('Qty').AsFloat * FieldByName('Price').AsFloat;
    FieldByName('Money').Value:=strtofloat(format('%.2f',[num1]));
    Post;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmBpGdCkEdit.CheckBox1Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmBpGdCkEdit.ToolButton1Click(Sender: TObject);
begin
  CopyAddRow(Tv1,CDS_Main);
  CDS_Main.edit;
  CDS_Main.fieldbyname('QtyUnit').Value:='M';
  CDS_Main.Post;
end;

procedure TfrmBpGdCkEdit.ToolButton2Click(Sender: TObject);
begin
  if  CDS_Main.IsEmpty then exit;
  CDS_Main.Delete;
end;

procedure TfrmBpGdCkEdit.v1P_CodeNamePropertiesEditValueChanged(
  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_Main do
  begin
    Edit;
    FieldByName('P_CodeName').Value:=Trim(mvalue);
    Post;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
  DoKc();

  with ADOQuery1 do
  begin
    close;
    sql.Clear;
    sql.Add('select *  from KH_Zdy_Attachment A ');
    sql.Add('where zdyName='+quotedstr(trim(CDS_Main.fieldbyname('P_CodeName').AsString)));
    open;
  end;
  IF ADOQuery1.IsEmpty then
  begin
    application.MessageBox('品名不存在！','提示信息',0);
    with CDS_Main do
    begin
      Edit;
      FieldByName('P_CodeName').Value:='';
      Post;
    end;
    exit;
  end;

end;

procedure TfrmBpGdCkEdit.v1P_ColorPropertiesEditValueChanged(
  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_Main do
  begin
    Edit;
    FieldByName('P_Color').Value:=Trim(mvalue);
    Post;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
  DoKc();

  with ADOQuery1 do
  begin
    close;
    sql.Clear;
    sql.Add('select *  from KH_Zdy_Attachment A ');
    sql.Add('where zdyName='+quotedstr(trim(CDS_Main.fieldbyname('P_CodeName').AsString)));
    sql.Add('and DEFstr3='+quotedstr(trim(CDS_Main.fieldbyname('P_Color').AsString)));
    open;
  end;
  IF ADOQuery1.IsEmpty then
  begin
    application.MessageBox('颜色不存在！','提示信息',0);
    with CDS_Main do
    begin
      Edit;
      FieldByName('P_Color').Value:='';
      Post;
    end;
  end;
end;

procedure TfrmBpGdCkEdit.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  DoKc();
end;

procedure TfrmBpGdCkEdit.v1P_CodeNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
 try
    frmZdyAttachCP:=TfrmZdyAttachCP.Create(Application);
    with frmZdyAttachCP do
    begin
      if ShowModal=1 then
      begin
        Self.CDS_main.Edit;
        Self.CDS_main.FieldByName('P_Code').Value:=Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
        Self.CDS_main.FieldByName('P_CodeName').Value:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        Self.CDS_main.FieldByName('P_Color').Value:=Trim(CDS_HZ.fieldbyname('DEFstr3').AsString);
    //    Self.Order_Sub.FieldByName('SOrddefstr4').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
        Self.CDS_main.Post;
      end;
    end;
  finally
    frmZdyAttachCP.Free;
  end;
    self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmBpGdCkEdit.Tv1CustomDrawIndicatorCell(
  Sender: TcxGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
      FValue:   string;
      FBounds:   TRect;
  begin
      FBounds   :=   AViewInfo.Bounds;
      if   (AViewInfo   is   TcxGridIndicatorRowItemViewInfo)   then
      begin
          ACanvas.FillRect(FBounds);
          ACanvas.DrawComplexFrame(FBounds,   clBtnHighlight,   clBtnShadow,   [bBottom,   bLeft,   bRight],   1);
          FValue   :=IntToStr(TcxGridIndicatorRowItemViewInfo(AViewInfo).GridRecord.Index+1);
          InflateRect(FBounds,   -1,   -1) ;
          ACanvas.Font.Color   :=   clBlack;
          ACanvas.Brush.Style   :=   bsClear;
          ACanvas.DrawText(FValue,   FBounds,   cxAlignCenter   or   cxAlignTop);
          ADone   :=   True;
      end;
 end;


procedure TfrmBpGdCkEdit.TBKCClick(Sender: TObject);
begin
  frmBpkcCk:=TfrmBpkcCk.create(self);
  with frmBpkcCk do
  begin
    TOk.Visible:=true;
    v1Ssel.Visible:=true;
    fCKName:=self.fCKName;
    if showmodal=1 then
    begin
      CDS_Main.DisableControls;
      self.CDS_Main.DisableControls;
      CDS_Main.First;
      while not CDS_Main.Eof do
      begin
        IF CDS_Main.FieldByName('ssel').AsBoolean then
        begin
        self.CDS_Main.Append;
        self.CDS_Main.fieldbyname('P_CodeName').Value:=CDS_Main.fieldbyname('P_CodeName').Value;
        self.CDS_Main.fieldbyname('P_Color').Value:=CDS_Main.fieldbyname('P_Color').Value;
         self.CDS_Main.fieldbyname('Qty').Value:=CDS_Main.fieldbyname('Qty').AsFloat;
        self.CDS_Main.fieldbyname('QtyUnit').Value:='M';
        self.CDS_Main.fieldbyname('price').Value:=0;
        self.CDS_Main.fieldbyname('money').Value:=0;
        self.CDS_Main.Post;
        end;
        CDS_Main.Next;
      end;
      CDS_Main.EnableControls;
      self.CDS_Main.EnableControls;
    end;
    free;
  end;
end;

procedure TfrmBpGdCkEdit.CRTypeChange(Sender: TObject);
begin
  TBKC.Visible:=false;
  If pos('盘点',trim(CRType.Text))>0 then
  begin
    TBKC.Visible:=true;
  end;
end;

end.
