unit U_CYGGRk;

interface

uses
  Windows, Messages, SysUtils, StrUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
  cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin, cxButtonEdit, cxDropDownEdit,
  DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, cxTextEdit, BtnEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmCYGGRk = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    CRTime: TDateTimePicker;
    Label2: TLabel;
    CRType: TComboBox;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1P_MF: TcxGridDBColumn;
    v1P_KZ: TcxGridDBColumn;
    v1RollNum: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    ckName: TComboBox;
    v1P_Color: TcxGridDBColumn;
    v1P_Code: TcxGridDBColumn;
    CRNo: TEdit;
    Label8: TLabel;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Label4: TLabel;
    ywy: TBtnEditA;
    v1Column3: TcxGridDBColumn;
    ToolButton3: TToolButton;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    factoryName: TBtnEditA;
    Label9: TLabel;
    ToolButton4: TToolButton;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    edtScan: TEdit;
    Label5: TLabel;
    Label7: TLabel;
    ZNote: TEdit;
    YPCFD: TComboBox;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    ToolButton5: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1P_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure v1P_ColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1P_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ywyBtnClick(Sender: TObject);
    procedure v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure factoryNameBtnClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure edtScanKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
    procedure ToolButton5Click(Sender: TObject);
  private
    procedure InitGrid();
    function savedate(): Boolean;
    procedure SetComboBox();
    function YFData(): Boolean;
    { Private declarations }
  public
    fkeyNO: string;
    FType: integer;
    fCKName: string;
    { Public declarations }
  end;

var
  frmCYGGRk: TfrmCYGGRk;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_CPManageSel, U_ZdyAttachGYS,
  U_ProductOrderListSel;
{$R *.dfm}

function TfrmCYGGRk.YFData(): Boolean;
var
  CRID, YFID, Price, PriceUnit, OrderUnit, FComTaiTou, cust: string;
begin
  Result := False;
  with ADOQueryTmp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName=''' + Trim(factoryName.Text) + ''' ');
    Open;
  end;

  if not ADOQueryTmp.IsEmpty then
  begin
    CRID := ADOQueryTmp.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(factoryName.Text);
      FieldByName('ZdyStr1').Value := '应收收';
      Post;
    end;
  end;

  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR ');
    sql.Add(' where MainID=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + '''');
    sql.Add(' and subID=''' + Trim(CDS_Main.fieldbyname('CRID').AsString) + '''');
    sql.Add(' and YFName=''样品生产'' ');
    Open;
  end;

  if ADOQueryTmp.IsEmpty then
  begin
    if GetLSNo(ADOQueryCmd, YFID, 'YF', 'YF_Money_CR', 3, 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(CDS_Main.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(factoryName.Text);
      FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', CRTime.DateTime));
      FieldByName('YFType').Value := '自动生成';
      FieldByName('ps').Value := CDS_Main.fieldbyname('RollNum').AsFloat;
      FieldByName('Qty').Value := CDS_Main.fieldbyname('Qty').AsFloat;
      FieldByName('Price').Value := CDS_Main.fieldbyname('Price').AsFloat;
      FieldByName('Money').Value := CDS_Main.fieldbyname('Money').AsFloat;
      FieldByName('BBMoney').Value := CDS_Main.fieldbyname('Money').AsFloat;
      FieldByName('HuiLv').Value := 1;
      FieldByName('BZType').Value := 'RMB';
      FieldByName('QtyUnit').Value := Trim(CDS_Main.fieldbyname('QtyUnit').AsString);
      FieldByName('ComTaiTou').Value := Trim(factoryName.Text);
      FieldByName('YFName').Value := '样品生产';
      FieldByName('MainId').Value := Trim(CDS_Main.fieldbyname('CRNO').AsString);
      FieldByName('subID').Value := Trim(CDS_Main.fieldbyname('CRID').AsString);
      FieldByName('orderNo').Value := Trim(CDS_Main.fieldbyname('P_Code').AsString);
      FieldByName('P_CodeName').Value := Trim(CDS_Main.fieldbyname('P_CodeName').AsString);
      FieldByName('P_Spec').Value := Trim(CDS_Main.fieldbyname('P_Spec').AsString);
      FieldByName('P_Color').Value := Trim(CDS_Main.fieldbyname('P_Color').AsString);
      FieldByName('status').Value := '0';
      Post;
    end;
  end
  else
  begin
    YFID := Trim(ADOQueryTmp.fieldbyname('YFID').AsString);
  end;

  Result := True;
end;

procedure TfrmCYGGRk.SetComboBox();
begin
  ckName.Items.Clear;
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''YPCK'' ');
    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;
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''YPRKTYPE''');
    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;

function TfrmCYGGRk.savedate(): Boolean;
var
  fkcID: integer;
  Filler, maxno: string;
  FillTime: TdateTime;
begin
  result := false;

  if FType = 0 then
  begin
    Filler := DName;
    FillTime := SGetServerDateTime(adoqueryTmp);
  end
  else
  begin
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CYGG_InOut');
      sql.Add('where CRNO=' + quotedstr(trim(fkeyNO)));
      open;
      Filler := adoqueryTmp.fieldbyname('Filler').AsString;
      FillTime := adoqueryTmp.fieldbyname('FillTime').AsDateTime;
    end;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    CDS_Main.First;
    with CDS_Main do
    begin
      while not eof do
      begin
        if Trim(CDS_Main.fieldbyname('CRID').AsString) = '' then
        begin
          if GetLSNo(ADOQueryTmp, maxno, 'YR', 'CYGG_InOut', 4, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(CDS_Main.fieldbyname('CRID').AsString);
        end;

        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from CYGG_InOut');
          sql.Add('where CRID= ' + quotedstr(Trim(maxno)));
          open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_Main.fieldbyname('CRID').AsString) = '' then
          begin
            Append;
            FieldByName('Filler').Value := trim(Filler);
            FieldByName('FillTime').Value := dateTimetostr(FillTime);
          end
          else
          begin
            Edit;
            FieldByName('editer').Value := trim(dName);
            FieldByName('editTime').Value := SGetServerDateTime(adoqueryTmp);
          end;
          if trim(CDS_Main.fieldbyname('FromOrderNo').AsString) = '' then
            FieldByName('YPType').Value := '开发样'
          else
            FieldByName('YPType').Value := '船样';
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Main, 'CYGG_InOut', 0);
          SSetsaveSqlNew(ADOQueryCmd, 'CYGG_InOut', Panel1, 0);
          FieldByName('CRNO').Value := trim(fkeyNO);
          FieldByName('CRID').Value := Trim(maxno);
          FieldByName('CRQtyFlag').Value := 1;
          FieldByName('CRFlag').Value := '入库';
          FieldByName('fROMmainID').Value := trim(CDS_Main.fieldbyname('fROMmainID').AsString);
          FieldByName('fROMsubID').Value := trim(CDS_Main.fieldbyname('fROMsubID').AsString);
          Post;
        end;

        CDS_Main.edit;
        CDS_Main.FieldByName('CRNO').Value := trim(fkeyNO);
        CDS_Main.FieldByName('CRID').Value := maxno;

        next;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_CYGG_Judge_KC ');
      Open;
    end;
    if Trim(ADOQueryCmd.FieldByName('msg').AsString) = '异常' then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      CDS_Main.EnableControls;
      Application.MessageBox('产生负数库存操作异常！', '提示', 0);
      exit;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    result := true;
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmCYGGRk.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*  from CYGG_InOut A');

      sql.Add('where 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));
      CRNo.Text := CDS_Main.fieldbyname('CRNo').asstring;
      ywy.Text := CDS_Main.fieldbyname('ywy').asstring;
      factoryName.Text := CDS_Main.fieldbyname('factoryName').asstring;
   //   FirstName.TxtCode:=trim(CDS_Main.fieldbyname('FirstNo').AsString);
   //   FirstName.Text:=trim(CDS_Main.fieldbyname('FirstName').AsString);
    end;
  finally
  end;
end;

procedure TfrmCYGGRk.FormDestroy(Sender: TObject);
begin
  frmCYGGRk := nil;
end;

procedure TfrmCYGGRk.FormCreate(Sender: TObject);
begin
  CRTime.Date := SGetServerDateTime(ADOQueryTmp);
  cxGrid1.Align := alclient;
end;

procedure TfrmCYGGRk.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  writeCxGrid(self.Caption, Tv1, '成品仓库');
  Action := cahide;
end;

procedure TfrmCYGGRk.FormShow(Sender: TObject);
begin
  readCxGrid(self.Caption, Tv1, '成品仓库');
//  SetComboBox();
  edtScan.SetFocus;
  InitGrid();
  if fType = 0 then
  begin
    if GetLSNo(ADOQueryCmd, fkeyNo, 'YR', 'CP_InOut', 3, 1) = False then
    begin
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    CRNo.Text := fkeyNo;
  end;
end;

procedure TfrmCYGGRk.TBSaveClick(Sender: TObject);
begin
  if cds_main.IsEmpty then
    exit;
  ToolBar1.SetFocus;
  if trim(CRType.Text) = '' then
  begin
    application.MessageBox('入库类型不能为空！', '提示信息', 0);
    exit;
  end;
  if (trim(CRType.Text) = '样品生产') and (trim(factoryName.Text) = '') then
  begin
    application.MessageBox('供应商信息不能为空！', '提示信息', 0);
    exit;
  end;
  if cds_main.Locate('qty', 0, []) then
  begin
    application.MessageBox('请填写数量！', '提示信息', 0);
    exit;
  end;
  if cds_main.Locate('qty', NULL, []) then
  begin
    application.MessageBox('请填写数量！', '提示信息', 0);
    exit;
  end;
  if cds_main.Locate('QtyUnit', '', []) then
  begin
    application.MessageBox('数量单位不能为空！', '提示信息', 0);
    exit;
  end;
  if cds_main.Locate('QtyUnit', NULL, []) then
  begin
    application.MessageBox('数量单位不能为空！', '提示信息', 0);
    exit;
  end;
  if savedate() then
  begin
    application.MessageBox('数据保存成功！', '提示信息');
    ModalResult := 1;
  end
  else
  begin
    application.MessageBox('数据保存失败！', '提示信息', MB_ICONERROR);
  end;
end;

procedure TfrmCYGGRk.v1P_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTCodeName';
      flagname := '产品名称';
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('P_CodeName').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      //  Self.CDS_Main.FieldByName('P_Code').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCYGGRk.ToolButton1Click(Sender: TObject);
begin
  frmCPManageSel := TfrmCPManageSel.create(self);
  with frmCPManageSel do
  begin
    Fstatus := 1;
    if showmodal = 1 then
    begin
      self.cds_main.DisableControls;
      CDS_Main.DisableControls;
      CDS_Main.First;
      while not CDS_Main.Eof do
      begin
        if (cds_main.fieldbyname('ssel').AsBoolean) and (not self.cds_main.Locate('P_Code', trim(cds_main.fieldbyname('CYNo').AsString), [])) then
        begin
          self.cds_main.Append;
          self.cds_main.FieldByName('P_Code').Value := trim(cds_main.fieldbyname('CYNo').AsString);
          self.cds_main.FieldByName('P_CodeName').Value := trim(cds_main.fieldbyname('CYName').AsString);
          self.cds_main.FieldByName('P_spec').Value := trim(cds_main.fieldbyname('CYSpec').AsString);
          self.cds_main.FieldByName('P_cf').Value := trim(cds_main.fieldbyname('CYECF').AsString);
          self.cds_main.FieldByName('P_SMF').Value := trim(cds_main.fieldbyname('CYMF').AsString);
          self.cds_main.FieldByName('P_SKZ').Value := trim(cds_main.fieldbyname('CYKZ').AsString);
          self.cds_main.FieldByName('ShaZhi').Value := trim(cds_main.fieldbyname('ShaZhi').AsString);
          self.cds_main.FieldByName('P_CF').Value := trim(cds_main.fieldbyname('CYCF').AsString);
          self.cds_main.FieldByName('qtyUnit').Value := '个';
          self.cds_main.FieldByName('RollNum').Value := 0;
          self.cds_main.FieldByName('price').Value := 0;
          self.cds_main.Post;
        end;
        cds_main.Next;
      end;
      cds_main.EnableControls;
      self.cds_main.EnableControls;
    end;
    free;
  end;

end;

procedure TfrmCYGGRk.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update  CYGG_InOut SET valid=''N'' ');
      sql.Add(',editer=' + quotedstr(trim(DName)));
      sql.Add(',edittime=getdate()');
      SQL.Add('where CRID=''' + Trim(CDS_Main.fieldbyname('CRID').AsString) + ''' ');
      execsql;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_CYGG_Judge_KC ');
      Open;
    end;
    if Trim(ADOQueryCmd.FieldByName('msg').AsString) = '异常' then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('产生负数库存操作异常！', '提示', 0);
      exit;
    end;
    CDS_Main.Delete;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;

    Application.MessageBox('数据删除失败！', '提示信息', 0);
  end;
end;

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

procedure TfrmCYGGRk.v1P_ColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OrdColor';
      flagname := '颜色';
      V1Name.Caption := '中文名称';
      V1Note.Caption := '英文名称';
      MainType := Trim(DName);
      fnote := True;
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('P_Color').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_Main.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCYGGRk.v1P_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmCPManageSel := TfrmCPManageSel.Create(self);
  with frmCPManageSel do
  begin
    Fstatus := 3;
    if showmodal = 1 then
    begin

      Self.CDS_Main.Edit;
      Self.CDS_Main.FieldByName('P_Code').Value := Trim(CDS_Main.fieldbyname('CYNo').AsString);
      Self.CDS_Main.FieldByName('P_CodeName').Value := Trim(CDS_Main.fieldbyname('CYName').AsString);
      Self.CDS_Main.FieldByName('P_CF').Value := Trim(CDS_Main.fieldbyname('CYCF').AsString);
      Self.CDS_Main.FieldByName('P_SMF').Value := Trim(CDS_Main.fieldbyname('CYMF').AsString);
      Self.CDS_Main.FieldByName('P_SKZ').Value := Trim(CDS_Main.fieldbyname('CYKZ').AsString);
      Self.CDS_Main.FieldByName('P_Color').Value := Trim(CDS_Main.fieldbyname('CYColor').AsString);
//      self.cds_main.FieldByName('P_HX').Value := trim(cds_main.fieldbyname('defnote4').AsString);
      self.cds_main.FieldByName('RollNum').Value := 0;
    end;
    free;
  end;
//  try
//    frmZDYHelp:=TfrmZDYHelp.Create(Application);
//    with frmZDYHelp do
//    begin
//      flag:='YPCode';
//      flagname:='样品编号';
//      if ShowModal=1 then
//      begin
//        Self.CDS_Main.Edit;
//        Self.CDS_Main.FieldByName('P_Code').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
//      end;
//    end;
//  finally
//    frmZDYHelp.Free;
//  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCYGGRk.ywyBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'ywy';
      flagname := '业务员名称';
      if ShowModal = 1 then
      begin
        ywy.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

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

end;

procedure TfrmCYGGRk.ToolButton3Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  CopyAddRowCDS(CDS_Main);
  with CDS_Main do
  begin
    Edit;
    FieldByName('CRID').Value := '';
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCYGGRk.factoryNameBtnClick(Sender: TObject);
begin
  frmZdyAttachGYS := TfrmZdyAttachGYS.create(self);
  with frmZdyAttachGYS do
  begin
  //  DEFstr5.ItemIndex:=DEFstr5.Items.IndexOf('加工厂');
   // DEFstr5.Enabled:=false;
    if showmodal = 1 then
    begin
      factoryName.Text := CDS_Hz.fieldbyname('CoName').asstring;
    end;
    free;
  end;
end;

procedure TfrmCYGGRk.ToolButton4Click(Sender: TObject);
begin
  cds_main.Append;
  self.cds_main.FieldByName('RollNum').Value := 0;
  self.cds_main.FieldByName('price').Value := 0;
  self.cds_main.FieldByName('qtyUnit').Value := '个';
  cds_main.Post;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCYGGRk.v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmProductOrderListSel := TfrmProductOrderListSel.Create(Application);
    with frmProductOrderListSel do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Main do
        begin
          Edit;
          FieldByName('FROMMainId').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MainId').AsString);
          FieldByName('FROMSubId').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('SubId').AsString);
          FieldByName('FromOrderNo').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderNo').AsString);
          if FieldByName('P_Code').AsString = '' then
          begin
            FieldByName('P_Code').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCode').AsString);
            FieldByName('P_CodeName').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCodeName').AsString);
//          FieldByName('P_Color').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTColor').AsString);
            FieldByName('P_HX').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTHX').AsString);
            FieldByName('P_SMF').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTMF').AsString);
            FieldByName('P_SKZ').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTKZ').AsString);
            FieldByName('P_SPEC').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTSpec').AsString);
            FieldByName('P_CF').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCF').AsString);
            self.cds_main.FieldByName('RollNum').Value := 0;
            self.cds_main.FieldByName('price').Value := 0;
          end;
        end;
      end;
    end;
  finally
    frmProductOrderListSel.Free;
  end;
end;

procedure TfrmCYGGRk.edtScanKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin

    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CP_YDang   ');
      sql.Add('where CYID= ' + Quotedstr(Trim(edtScan.Text)));
      open;
    end;

    if not adoqueryTmp.IsEmpty then
    begin
      with Self.CDS_Main do
      begin
        Append;
        FieldByName('P_Code').Value := Trim(adoqueryTmp.fieldbyname('CYNo').AsString);
        FieldByName('P_CodeName').Value := Trim(adoqueryTmp.fieldbyname('CYName').AsString);
        FieldByName('P_SMF').Value := Trim(adoqueryTmp.fieldbyname('CYMF').AsString);
        FieldByName('P_SKZ').Value := Trim(adoqueryTmp.fieldbyname('CYKZ').AsString);
        FieldByName('P_SPEC').Value := Trim(adoqueryTmp.fieldbyname('CYSpec').AsString);
        FieldByName('P_CF').Value := Trim(adoqueryTmp.fieldbyname('CYCF').AsString);
        self.cds_main.FieldByName('RollNum').Value := 0;
        self.cds_main.FieldByName('price').Value := 0;
        self.cds_main.FieldByName('qtyUnit').Value := '个';
        post;
      end;
    end
    else
    begin
      with adoqueryTmp do
      begin
        close;
        sql.Clear;
        sql.Add('select * from JYOrder_Main A     ');
        sql.Add('inner join JYOrder_Sub B on A.MainId=B.MainId    ');
        sql.Add('where B.SubId= ' + Quotedstr(Trim(edtScan.Text)));
        open;
      end;
      if not adoqueryTmp.IsEmpty then
      begin
        with Self.CDS_Main do
        begin
          Append;
          FieldByName('FROMMainId').Value := Trim(adoqueryTmp.fieldbyname('MainId').AsString);
          FieldByName('FROMSubId').Value := Trim(adoqueryTmp.fieldbyname('SubId').AsString);
          FieldByName('FromOrderNo').Value := Trim(adoqueryTmp.fieldbyname('OrderNo').AsString);
          FieldByName('P_Code').Value := Trim(adoqueryTmp.fieldbyname('MPRTCode').AsString);
          FieldByName('P_CodeName').Value := Trim(adoqueryTmp.fieldbyname('MPRTCodeName').AsString);
          FieldByName('P_HX').Value := Trim(adoqueryTmp.fieldbyname('PRTHX').AsString);
          FieldByName('P_SMF').Value := Trim(adoqueryTmp.fieldbyname('MPRTMF').AsString);
          FieldByName('P_SKZ').Value := Trim(adoqueryTmp.fieldbyname('MPRTKZ').AsString);
          FieldByName('P_SPEC').Value := Trim(adoqueryTmp.fieldbyname('MPRTSpec').AsString);
          FieldByName('P_CF').Value := Trim(adoqueryTmp.fieldbyname('MPRTCF').AsString);
          self.cds_main.FieldByName('RollNum').Value := 0;
          self.cds_main.FieldByName('price').Value := 0;
          self.cds_main.FieldByName('qtyUnit').Value := '个';
          Post;
        end;

      end;
    end;

    edtScan.Text := '';
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCYGGRk.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);    //Platform   specific.   May   not   work   on   Linux.
    ACanvas.Font.Color := clBlack;
    ACanvas.Brush.Style := bsClear;
    ACanvas.DrawText(FValue, FBounds, cxAlignCenter or cxAlignTop);
    ADone := True;
  end;
end;

procedure TfrmCYGGRk.ToolButton5Click(Sender: TObject);
begin
  try
    frmProductOrderListSel := TfrmProductOrderListSel.Create(Application);
    with frmProductOrderListSel do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Main do
        begin
          while not Order_Main.Eof do
          begin
            if Order_Main.FieldByName('SSel').AsBoolean = True then
            begin
              append;
              CDS_Main.FieldByName('FROMMainId').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MainId').AsString);
              CDS_Main.FieldByName('FROMSubId').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('SubId').AsString);
              CDS_Main.FieldByName('FromOrderNo').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderNo').AsString);

              CDS_Main.FieldByName('P_Code').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCode').AsString);
              CDS_Main.FieldByName('P_CodeName').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCodeName').AsString);
//          FieldByName('P_Color').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTColor').AsString);
              CDS_Main.FieldByName('P_HX').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTHX').AsString);
              CDS_Main.FieldByName('P_SMF').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTMF').AsString);
              CDS_Main.FieldByName('P_SKZ').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTKZ').AsString);
              CDS_Main.FieldByName('P_SPEC').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTSpec').AsString);
              CDS_Main.FieldByName('P_CF').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCF').AsString);
              self.cds_main.FieldByName('RollNum').Value := 0;
              self.cds_main.FieldByName('price').Value := 0;
              self.cds_main.FieldByName('qtyUnit').Value := '个';

            end;
            Order_Main.Delete;
          end

        end;
      end;

    end;
  finally
    frmProductOrderListSel.Free;
  end;
end;

end.

