unit U_CYGGCk;

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, BtnEdit, cxTextEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, cxCheckBox, cxCalendar;

type
  TfrmCYGGCk = 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;
    v1P_SPEC: 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;
    ToCustName: TBtnEditA;
    Label6: TLabel;
    Label7: TLabel;
    v1P_Color: TcxGridDBColumn;
    v1P_Code: TcxGridDBColumn;
    Label8: TLabel;
    CRNo: TEdit;
    Label9: TLabel;
    custName: TBtnEditA;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    TOYWY: TBtnEditA;
    Label12: TLabel;
    v1Column4: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    defstr1: TBtnEditA;
    v1Column7: TcxGridDBColumn;
    Label13: TLabel;
    KHYWB: TBtnEditA;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    edtScan: TEdit;
    defstr2: TComboBox;
    Tv1Column5: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    v1Ssel: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    v1kuwei: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    VCfactoryName: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    v1defnote5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    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 ToCustNameBtnClick(Sender: TObject);
    procedure orderNOBtnClick(Sender: TObject);
    procedure custNameBtnClick(Sender: TObject);
    procedure v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure TOYWYBtnClick(Sender: TObject);
    procedure defstr1BtnClick(Sender: TObject);
    procedure KHYWBBtnClick(Sender: TObject);
    procedure edtScanKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
    procedure TV2DblClick(Sender: TObject);
  private
    procedure InitGrid();
    function savedate(): Boolean;
    procedure SetComboBox();
    function YSData(fYFName: string): Boolean;
    { Private declarations }
  public
    CopyInt: integer;
    fkeyNO: string;
    FType: integer;
    fCKName: string;
    { Public declarations }
  end;

var
  frmCYGGCk: TfrmCYGGCk;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_ZdyAttachment, U_Bpkcsumlist, U_UserSel,
  U_CYGGkcsumlist;
{$R *.dfm}

function TfrmCYGGCk.YSData(fYFName: string): 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(custName.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(custName.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=''' + trim(fYFName) + ''' ');
    Open;
  end;

  if ADOQueryTmp.IsEmpty then
  begin
    if GetLSNo(ADOQueryTmp, YFID, 'CS', '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(custName.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(custName.Text);
      FieldByName('YFName').Value := fYFName;
      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 TfrmCYGGCk.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=''YPCKTYPE''');
//    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 TfrmCYGGCk.savedate(): Boolean;
var
  fkcID: integer;
  Filler: string;
  FillTime: TdateTime;
  maxno: string;
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(ADOQueryCmd, maxno, 'YC', '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;

          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);
          FieldByName('factoryName').Value := trim(CDS_Main.fieldbyname('factoryName').AsString);
          FieldByName('YWY').Value := trim(CDS_Main.fieldbyname('YWY').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 TfrmCYGGCk.InitGrid();
begin
  try

    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('EXEC P_YP_UP_SSKC  @CRNO=' + quotedstr(trim(fkeyNO)));
//      ShowMessage(SQL.Text);
      ExecSQL;
    end;

    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*  from CYGG_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;
      CRType.ItemIndex := CRType.Items.IndexOf(trim(CDS_Main.fieldbyname('CRType').AsString));
      defstr2.ItemIndex := defstr2.Items.IndexOf(trim(CDS_Main.fieldbyname('defstr2').AsString));
      CRNO.Text := trim(CDS_Main.fieldbyname('CRNO').AsString);
      KHYWB.Text := trim(CDS_Main.FieldByName('KHYWB').AsString);
      custName.TxtCode := trim(CDS_Main.fieldbyname('custNo').AsString);
      custName.Text := trim(CDS_Main.fieldbyname('custName').AsString);
      TOywy.Text := trim(CDS_Main.fieldbyname('TOywy').AsString);
      TocustName.TxtCode := trim(CDS_Main.fieldbyname('TocustNo').AsString);
      TocustName.Text := trim(CDS_Main.fieldbyname('TocustName').AsString);
      defstr1.Text := trim(CDS_Main.fieldbyname('defstr1').AsString);
      defstr2.Text := trim(CDS_Main.fieldbyname('defstr2').AsString);
    end;
  finally
  end;

end;

procedure TfrmCYGGCk.FormDestroy(Sender: TObject);
begin
  frmCYGGCk := nil;
end;

procedure TfrmCYGGCk.FormCreate(Sender: TObject);
begin
  CRTime.Date := DServerDate;
  cxGrid1.Align := alclient;
end;

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

procedure TfrmCYGGCk.FormShow(Sender: TObject);
var
  fsj: string;
begin
  readCxGrid(self.Caption + 'Tv1', Tv1, '成品仓库');
  readCxGrid(self.Caption + 'Tv2', Tv1, '成品仓库');
  fsj := 'select UserName as Name from SY_User where Udept like  ''客服%''   ';
  SInitComBoxBySql(ADOQueryCmd, defstr2, False, fsj);
  ToolBar1.SetFocus;
//  SetComboBox();
  InitGrid();
  if fType = 0 then
  begin
    if GetLSNo(ADOQueryCmd, fkeyNo, 'YC', 'CYGG_InOut', 3, 1) = False then
    begin
      //  ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    CRNO.Text := fkeyNo;
  end;
  if CopyInt = 99 then
  begin
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('CRID').Value := '';
        Post;
        Next;
      end;
    end;
  end;
end;

procedure TfrmCYGGCk.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(defstr2.Text) = '' then
  begin
    application.MessageBox('领用人不能为空！', '提示信息', 0);
    exit;
  end;
  if (trim(CRType.Text) = '船样销售') and (trim(custName.Text) = '') then
  begin
    application.MessageBox('客户信息不能为空！', '提示信息', 0);
    exit;
  end;
  if cds_main.Locate('Qty', 0, []) then
  begin
    application.MessageBox('数量不能为0！', '提示信息', 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 TfrmCYGGCk.v1P_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'Cloth';
      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;
end;

procedure TfrmCYGGCk.ToolButton1Click(Sender: TObject);
begin
  frmCYGGkcsumlist := TfrmCYGGkcsumlist.create(self);
  with frmCYGGkcsumlist do
  begin
    fType := 10;
    fckName := self.fckName;
    if showmodal = 1 then
    begin
      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('FROMOrderno').Value := trim(cds_main.fieldbyname('FROMOrderno').AsString);
          self.cds_main.FieldByName('frommainid').Value := trim(cds_main.fieldbyname('frommainid').AsString);
          self.cds_main.FieldByName('fromsubid').Value := trim(cds_main.fieldbyname('fromsubid').AsString);
          self.cds_main.FieldByName('factoryName').Value := trim(cds_main.fieldbyname('factoryName').AsString);
          self.cds_main.FieldByName('YWY').Value := trim(cds_main.fieldbyname('YWY').AsString);
          self.cds_main.FieldByName('P_Code').Value := trim(cds_main.fieldbyname('P_Code').AsString);
          self.cds_main.FieldByName('P_CodeName').Value := trim(cds_main.fieldbyname('P_CodeName').AsString);
          self.cds_main.FieldByName('P_cf').Value := trim(cds_main.fieldbyname('P_cf').AsString);
          self.cds_main.FieldByName('P_spec').Value := trim(cds_main.fieldbyname('P_spec').AsString);
          self.cds_main.FieldByName('shazhi').Value := trim(cds_main.fieldbyname('shazhi').AsString);
          self.cds_main.FieldByName('P_Color').Value := trim(cds_main.fieldbyname('P_Color').AsString);
          self.cds_main.FieldByName('P_HX').Value := trim(cds_main.fieldbyname('P_HX').AsString);
          self.cds_main.FieldByName('P_SMF').Value := trim(cds_main.fieldbyname('P_SMF').AsString);
          self.cds_main.FieldByName('P_SKZ').Value := trim(cds_main.fieldbyname('P_SKZ').AsString);
          self.cds_main.FieldByName('RollNum').Value := cds_main.fieldbyname('kcRollNum').AsFloat;
          self.cds_main.FieldByName('ckprice').Value := cds_main.fieldbyname('price').AsFloat;
          self.cds_main.FieldByName('price').Value := 0;
          self.cds_main.FieldByName('money').Value := 0;
          self.cds_main.FieldByName('Qty').Value := 1;
          self.cds_main.FieldByName('SSKC').Value := cds_main.fieldbyname('kcQty').AsFloat;
          self.cds_main.FieldByName('QtyUnit').Value := trim(cds_main.fieldbyname('QtyUnit').AsString);
          self.cds_main.FieldByName('kuwei').Value := trim(cds_main.fieldbyname('kuwei').AsString);
          self.cds_main.FieldByName('YPType').Value := trim(cds_main.fieldbyname('YPType').AsString);
          self.cds_main.FieldByName('JuanHao').Value := trim(cds_main.fieldbyname('JuanHao').AsString);
          self.cds_main.FieldByName('ISCGPZ').Value := trim(cds_main.fieldbyname('ISCGPZ').AsString);
          self.cds_main.FieldByName('YPCFD').Value := trim(cds_main.fieldbyname('YPCFD').AsString);
          self.cds_main.Post;
        end;
        cds_main.Next;
      end;
      cds_main.EnableControls;
    end;
    free;
  end;

end;

procedure TfrmCYGGCk.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;

  if CDS_Main.IsEmpty then
    Exit;

  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) + ''' ');
//
//      sql.Add('delete from YF_money_CR  ');
//      SQL.Add('where mainID=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');
//      SQL.Add('and subID=''' + Trim(CDS_Main.fieldbyname('CRID').AsString) + ''' ');

    execsql;
  end;
  CDS_Main.Delete;
end;

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

procedure TfrmCYGGCk.ToCustNameBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'CustomerNoName';
      flagname := '客户名称';
      if ShowModal = 1 then
      begin
        ToCustName.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
        ToCustName.TxtCode := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCYGGCk.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.PRTCodeName.Text:=trim(Order_Main.fieldbyname('PRTCodeName').AsString);
       self.PRTColor.Text:=trim(Order_Main.fieldbyname('PRTColor').AsString);
     end;
     free;
   end;  }
end;

procedure TfrmCYGGCk.custNameBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      fywy := trim(TOywy.Text);
      if ShowModal = 1 then
      begin
        CustName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
        CustName.TxtCode := Trim(CDS_HZ.fieldbyname('CoCode').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;

end;

procedure TfrmCYGGCk.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('ckprice').AsFloat]));
    Post;
  end;
  Tv1.Controller.EditingController.ShowEdit();

end;

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

procedure TfrmCYGGCk.defstr1BtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'YPYSType';
      flagname := '样品运输方式';
      if ShowModal = 1 then
      begin
        defstr1.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCYGGCk.KHYWBBtnClick(Sender: TObject);
begin

  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'KHYWB';
      flagname := '客户部门';

      if ShowModal = 1 then
      begin
        KHYWB.Text := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCYGGCk.edtScanKeyPress(Sender: TObject; var Key: Char);
var
  PWhere: string;
begin
  if Key = #13 then
  begin
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_CYGG_CK_SCAN  @SCAN  =' + Quotedstr(Trim(edtScan.Text)));
//      ShowMessage(sql.Text);
      open;
    end;
    if not adoqueryTmp.IsEmpty then
    begin

      SCreateCDS20(adoqueryTmp, CDS_2);
      SInitCDSData20(adoqueryTmp, CDS_2);
      cxGrid2.Visible := True;
    end;
    edtScan.Text := '';
  end;
end;

procedure TfrmCYGGCk.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 TfrmCYGGCk.TV2DblClick(Sender: TObject);
begin
  self.cds_main.Append;
  self.cds_main.FieldByName('FROMOrderno').Value := trim(CDS_2.fieldbyname('FROMOrderno').AsString);
  self.cds_main.FieldByName('frommainid').Value := trim(CDS_2.fieldbyname('frommainid').AsString);
  self.cds_main.FieldByName('fromsubid').Value := trim(CDS_2.fieldbyname('fromsubid').AsString);
  self.cds_main.FieldByName('factoryName').Value := trim(CDS_2.fieldbyname('factoryName').AsString);
  self.cds_main.FieldByName('YWY').Value := trim(CDS_2.fieldbyname('YWY').AsString);
  self.cds_main.FieldByName('P_Code').Value := trim(CDS_2.fieldbyname('P_Code').AsString);
  self.cds_main.FieldByName('P_CodeName').Value := trim(CDS_2.fieldbyname('P_CodeName').AsString);
  self.cds_main.FieldByName('P_cf').Value := trim(CDS_2.fieldbyname('P_cf').AsString);
  self.cds_main.FieldByName('P_spec').Value := trim(CDS_2.fieldbyname('P_spec').AsString);
  self.cds_main.FieldByName('shazhi').Value := trim(CDS_2.fieldbyname('shazhi').AsString);
  self.cds_main.FieldByName('P_Color').Value := trim(CDS_2.fieldbyname('P_Color').AsString);
  self.cds_main.FieldByName('P_HX').Value := trim(CDS_2.fieldbyname('P_HX').AsString);
  self.cds_main.FieldByName('P_SMF').Value := trim(CDS_2.fieldbyname('P_SMF').AsString);
  self.cds_main.FieldByName('P_SKZ').Value := trim(CDS_2.fieldbyname('P_SKZ').AsString);
  self.cds_main.FieldByName('RollNum').Value := CDS_2.fieldbyname('kcRollNum').AsFloat;
  self.cds_main.FieldByName('ckprice').Value := CDS_2.fieldbyname('price').AsFloat;
  self.cds_main.FieldByName('price').Value := 0;
  self.cds_main.FieldByName('money').Value := 0;
  self.cds_main.FieldByName('Qty').Value := 1;
  self.cds_main.FieldByName('SSKC').Value := CDS_2.fieldbyname('kcQty').AsFloat;
  self.cds_main.FieldByName('QtyUnit').Value := trim(CDS_2.fieldbyname('QtyUnit').AsString);
  self.cds_main.FieldByName('kuwei').Value := trim(CDS_2.fieldbyname('kuwei').AsString);
  self.cds_main.FieldByName('YPType').Value := trim(CDS_2.fieldbyname('YPType').AsString);
  self.cds_main.FieldByName('JuanHao').Value := trim(CDS_2.fieldbyname('JuanHao').AsString);
  self.cds_main.FieldByName('ISCGPZ').Value := trim(CDS_2.fieldbyname('ISCGPZ').AsString);
  self.cds_main.FieldByName('YPCFD').Value := trim(CDS_2.fieldbyname('YPCFD').AsString);
  self.cds_main.Post;
  cxGrid2.Visible := False;
end;

end.

