unit U_BpYPCkEdit;

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
  TfrmBpYPCkEdit = 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;
    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;
    orderNO: TBtnEditA;
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    note: TEdit;
    Label3: TLabel;
    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 orderNOBtnClick(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure v1P_CodeNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo;
      var ADone: Boolean);
  private
        fmainID,fsubID:string;
    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;
    fsyrName:string;
    
    { Public declarations }
  end;

var
  frmBpYPCkEdit: TfrmBpYPCkEdit;

implementation
uses
  U_DataLink,U_Fun10,U_ZDYHelp, U_BpRk, U_ProductOrderNewList,
  U_ZdyAttachCP;
{$R *.dfm}
procedure TfrmBpYPCkEdit.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 TfrmBpYPCkEdit.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=0 ');
        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:=0;
          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('YFName').Value:='样品销售';
          FieldByName('MainId').Value:=Trim(ckNo);
          FieldByName('subID').Value:=Trim(ckID);
          FieldByName('P_CodeName').Value:=trim(Order_Main10.fieldbyname('P_CodeName').AsString);
          FieldByName('P_Color').Value:=trim(Order_Main10.fieldbyname('P_Color').AsString);
          FieldByName('YFDefFlag1').Value:=0;
          FieldByName('status').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 TfrmBpYPCkEdit.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,'CK','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 ADOQuery1 do
    begin
      Close;
        sql.Clear;
        sql.Add('delete  from YF_Money_CR ');
        sql.Add(' where MainID='''+Trim(fkeyNO)+'''');
    //    sql.Add(' and subID='''+Trim(ckID)+'''');
        sql.Add(' and YFName=''样品销售'' ');
        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(fieldbyname('mainID').AsString);
          ADOQueryCmd.FieldByName('subID').Value:=trim(fieldbyname('subID').AsString);
          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('note').Value:=trim(note.Text);
   //     ADOQueryCmd.FieldByName('FirstNo').Value:=trim(FirstName.TxtCode);
   //     ADOQueryCmd.FieldByName('FirstName').Value:=trim(FirstName.Text);
          if FType=1 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 TfrmBpYPCkEdit.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;
  if CRType.Items.Count>0 then CRType.ItemIndex:=0;
end;
procedure TfrmBpYPCkEdit.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*,B.orderNo from BP_InOut A');
      sql.Add('left join  JYOrder_sub B on B.mainID=A.mainID and B.subID=A.subID ');
      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));
      fmainID:=trim(CDS_Main.fieldbyname('MainID').AsString);
      fsubID:=trim(CDS_Main.fieldbyname('subID').AsString);
      orderNo.Text:=trim(CDS_Main.fieldbyname('orderNo').AsString);
      cust.Text:= trim(CDS_Main.fieldbyname('cust').AsString);
       note.Text:=trim(CDS_Main.fieldbyname('note').AsString);
    end;
  finally
  end;
end;

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

procedure TfrmBpYPCkEdit.FormDestroy(Sender: TObject);
begin
  frmBpYPCkEdit:=nil;
end;

procedure TfrmBpYPCkEdit.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 TfrmBpYPCkEdit.FormCreate(Sender: TObject);
begin
  //BegDate.Date:=DServerDate-7;
//  EndDate.Date:=DServerDate;
  CRTime.date:= DServerDate;
  cxGrid1.Align:=alclient;

end;

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

procedure TfrmBpYPCkEdit.TBRafreshClick(Sender: TObject);
begin

  initGrid();
end;

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

  with AdoPrint do
  begin
    close;
    sql.Clear;
    sql.Add('select A.cust,A.P_CodeName,A.P_Color,A.QtyUnit,B.orderNo,C.customerNoname,SUM(A.Qty) as Qty,AVG(A.Price) as Price,SUM(A.money) as Money from BP_INout A ');
    sql.Add('left join JYOrder_sub B on B.mainID=A.mainID and B.subID=A.subID ');
    sql.Add('left join JYOrder_Main C on C.mainID=A.mainID ');
    sql.Add('where A.CRNO='+quotedstr(trim(fkeyNO)));
    sql.Add('group by  A.cust,A.P_CodeName,A.P_Color,A.QtyUnit,B.orderNo,C.customerNoname');
    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 TfrmBpYPCkEdit.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  TcxGridToExcel(self.Caption,cxgrid1);
end;

procedure TfrmBpYPCkEdit.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 TfrmBpYPCkEdit.custBtnClick(Sender: TObject);
begin
{  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='GDCustFlag';
      flagname:='客户名称';
      if ShowModal=1 then
      begin
        cust.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end; }
end;

procedure TfrmBpYPCkEdit.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('%8.2f',[num1]));
    Post;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

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

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

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

procedure TfrmBpYPCkEdit.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;


  with ADOQueryCmd 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 ADOQueryCmd.IsEmpty then
  begin
    application.MessageBox('品名不存在！','提示信息',0);
    with CDS_main do
    begin
      Edit;
      FieldByName('P_CodeName').Value:='';
      Post;
    end;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
  DoKc();

end;

procedure TfrmBpYPCkEdit.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;
  with ADOQueryCmd 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 ADOQueryCmd.IsEmpty then
  begin
    application.MessageBox('颜色不存在！','提示信息',0);
    with CDS_main do
    begin
      Edit;
      FieldByName('P_Color').Value:='';
      Post;
    end;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
  DoKc();
end;

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

procedure TfrmBpYPCkEdit.orderNOBtnClick(Sender: TObject);
begin
   frmProductOrderNewList:=TfrmProductOrderNewList.Create(Application);
   with  frmProductOrderNewList do
   begin
     DParameters1:='高权限';
     if showmodal=1 then
     begin
       self.fmainID:=trim(Order_Main.fieldbyname('MainID').AsString);
       self.fsubID:=trim(Order_Main.fieldbyname('subID').AsString);
       self.orderNo.Text:=trim(Order_Main.fieldbyname('orderNo').AsString);
       self.cust.Text:=trim(Order_Main.fieldbyname('CustomerNoName').AsString);
    //   self.PRTColor.Text:=trim(Order_Main.fieldbyname('PRTColor').AsString);
     end;
     free;
   end;

   with ADOQuery1 do
   begin
     close;
     sql.Clear;
     sql.Add('select * from JYOrder_sub ');
     sql.Add('where mainID='+quotedstr(trim(FmainID)));
     open;
   end;

   CDS_Main.EmptyDataSet;
   CDS_Main.DisableControls;

   IF not ADOQuery1.IsEmpty then
   begin
     while not ADOQuery1.Eof do
     begin
       CDS_Main.Append;
       CDS_Main.Fieldbyname('mainID').Value:=trim(ADOQuery1.Fieldbyname('mainID').AsString);
       CDS_Main.Fieldbyname('subID').Value:=trim(ADOQuery1.Fieldbyname('subID').AsString);
       CDS_Main.Fieldbyname('P_CodeName').Value:=trim(ADOQuery1.Fieldbyname('PrtCodeName').AsString);
       CDS_Main.Fieldbyname('P_Color').Value:=trim(ADOQuery1.Fieldbyname('PrtColor').AsString);
       CDS_Main.Fieldbyname('Qty').Value:=trim(ADOQuery1.Fieldbyname('PrtOrderQty').AsString);
       CDS_Main.Fieldbyname('QtyUnit').Value:='M';
       CDS_Main.Post;
       ADOQuery1.next;
     end;
   end;
   CDS_Main.First;
   CDS_Main.EnableControls;
end;

procedure TfrmBpYPCkEdit.Edit1KeyPress(Sender: TObject; var Key: Char);
var
  num1:double;
begin
  IF key=#13 then
  begin
    with CDS_Main do
    begin
      DisableControls;
      first;
      while not eof do
      begin
        Edit;
        FieldByName('Price').Value:=strtofloatdef(edit1.Text,0);
        num1:=FieldByName('Qty').AsFloat * FieldByName('Price').AsFloat;
        FieldByName('Money').Value:=strtofloat(format('%8.2f',[num1]));
        Post;
        next;
      end;
      first;
      EnableControls;
     end;
     Tv1.Controller.EditingController.ShowEdit();
  end;
end;

procedure TfrmBpYPCkEdit.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 TfrmBpYPCkEdit.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;

end.
