unit U_BpCk;      

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, dxSkinsCore,
      
  dxSkinDarkRoom,  
    
     
    
    
    
    
   
   dxSkinOffice2013White, 
     dxSkinSharpPlus,
   dxSkinSpringTime,  
   dxSkinsDefaultPainters, 
     dxSkinscxPCPainter;

type
  TfrmbpCk = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    CRTime: TDateTimePicker;
    Label2: TLabel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1P_SPEC: TcxGridDBColumn;
    v1P_MF: TcxGridDBColumn;
    v1P_KZ: 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;
    TOYWY: TBtnEditA;
    Label12: TLabel;
    v1Column4: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    defstr1: TBtnEditA;
    v1Column7: TcxGridDBColumn;
    Label13: TLabel;
    KHYWB: TBtnEditA;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    edtScan: TEdit;
    Tv1Column5: TcxGridDBColumn;
    defstr2: TBtnEditA;
    CRType: TBtnEditA;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    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 defstr2BtnClick(Sender: TObject);
    procedure CRTypeBtnClick(Sender: TObject);
    procedure CRTypeDblClick(Sender: TObject);
    procedure Tv1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  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
  frmbpCk: TfrmbpCk;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_Bpkclist, U_ZdyAttachment, U_Bpkcsumlist_Sel,
  U_UserSel, U_Bpkcsumlistsel, U_SCPerson, U_ProductOrderNewList_New;
{$R *.dfm}

function TfrmbpCk.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 TfrmbpCk.SetComboBox();
begin
//  ckName.Items.Clear;
//  with adoQueryTmp do     exec P_Print_cpCkMd
//  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 TfrmbpCk.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 YP_InOut2');
      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', 'YP_InOut2', 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 YP_INout2');
          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 := SGetServerDateTime(adoqueryTmp);
            FieldByName('CRTIME').Value := SGetServerDateTime(adoqueryTmp)
          end
          else
          begin
            Edit;
            FieldByName('editer').Value := trim(dName);
            FieldByName('editTime').Value := SGetServerDateTime(adoqueryTmp);
          end;

          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Main, 'YP_InOut2', 0);
          SSetsaveSqlNew(ADOQueryCmd, 'YP_InOut2', 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('ToMainID').Value := trim(CDS_Main.fieldbyname('ToMainID').AsString);
          FieldByName('ToSubID').Value := trim(CDS_Main.fieldbyname('ToSubID').AsString);
          FieldByName('factoryName').Value := trim(CDS_Main.fieldbyname('factoryName').AsString);
          FieldByName('YWY').Value := trim(CDS_Main.fieldbyname('YWY').AsString);
          FieldByName('CustNo').Value := trim(CustName.TxtCode);
          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('select sum(A.Qty*A.CRQtyFlag)SL FROM  YP_InOut2 A ');
      sql.Add('where Valid=''Y''  AND isnull(kuwei,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('kuwei').AsString)));  //库位
      sql.Add('and isnull(factoryName,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('factoryName').AsString)));    //来样工厂
      sql.Add('and isnull(P_Code,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_Code').AsString)));    //编号
      sql.Add('and isnull(P_codename,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_codename').AsString))); //公司产品名称
      sql.Add('and isnull(KHCPName,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('KHCPName').AsString))); //客户产品名称
      sql.Add('and isnull(P_spec,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_spec').AsString))); //工艺
      sql.Add('and isnull(P_cf,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_cf').AsString)));   //成分
      sql.Add('and isnull(shazhi,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('shazhi').AsString))); //纱支
      sql.Add('and isnull(P_SMF,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_SMF').AsString)));  //门幅
      sql.Add('and isnull(P_SKZ,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_SKZ').AsString)));  //克重
      sql.Add('and isnull(P_Color,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_Color').AsString))); //公司颜色
      sql.Add('and isnull(sehao,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('sehao').AsString))); //公司色号
      sql.Add('and isnull(KHColor,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('KHColor').AsString))); //客户颜色
      sql.Add('and isnull(KHSeHao,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('KHSeHao').AsString))); //客户色号
      sql.Add('and isnull(QtyUnit,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('QtyUnit').AsString))); //数量单位
      SQL.Add('   group by  kuwei,factoryName,P_Code,P_codename,KHCPName,P_spec,P_cf,shazhi,P_SMF,P_SKZ,P_Color,sehao,KHColor,KHSeHao,QtyUnit');
      Open;
    end;
    if ADOQueryCmd.IsEmpty = false then
    begin
      if ADOQueryCmd.FieldByName('SL').VALUE < 0 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        CDS_Main.EnableControls;
        Application.MessageBox('产生负数库存操作异常！', '提示', 0);
        exit;
      end;
    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;
    result := False;
  end;
end;

procedure TfrmbpCk.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 YP_InOut2 A');
      sql.Add('where A.CRNO=' + quotedstr(trim(fkeyNO)));
      sql.Add('and valid=''Y''');
      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.text := trim(CDS_Main.fieldbyname('CRType').AsString);
      defstr2.Text := trim(CDS_Main.fieldbyname('defstr2').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 TfrmbpCk.FormDestroy(Sender: TObject);
begin
  frmbpCk := nil;
end;

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

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

procedure TfrmbpCk.FormShow(Sender: TObject);
var
  fsj: string;
begin
  readCxGrid(self.Caption + '3', 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', 'YP_InOut2', 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 TfrmbpCk.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(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('Price', NULL, []) then
  begin
    application.MessageBox('单价不能为空！', '提示信息', 0);
    exit;
  end;
  if cds_main.Locate('money', 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 TfrmbpCk.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 TfrmbpCk.ToolButton1Click(Sender: TObject);
begin
  frmBpkcsumlist_Sel := TfrmBpkcsumlist_Sel.create(self);
  with frmBpkcsumlist_Sel 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('factoryName').Value := trim(cds_main.fieldbyname('factoryName').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('KHCPName').Value := trim(cds_main.fieldbyname('KHCPName').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('sehao').Value := trim(cds_main.fieldbyname('sehao').AsString);
          self.cds_main.FieldByName('KHColor').Value := trim(cds_main.fieldbyname('KHColor').AsString);
          self.cds_main.FieldByName('KHSeHao').Value := trim(cds_main.fieldbyname('KHSeHao').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('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('YPLX').Value := trim(cds_main.fieldbyname('YPLX').AsString);
          self.cds_main.Post;
        end;
        cds_main.Next;
      end;
      cds_main.EnableControls;
    end;
    free;
  end;

end;

procedure TfrmbpCk.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  YP_InOut2 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 TfrmbpCk.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmbpCk.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 TfrmbpCk.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 TfrmbpCk.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('KHNameJC').AsString);
        CustName.TxtCode := Trim(CDS_HZ.fieldbyname('KHNo').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;

end;

procedure TfrmbpCk.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 TfrmbpCk.TOYWYBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'YPywy';
      flagname := '业务员名称';
      if ShowModal = 1 then
      begin
        TOYWY.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

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

procedure TfrmbpCk.KHYWBBtnClick(Sender: TObject);
begin

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

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

procedure TfrmbpCk.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_YP_CK_SCAN  @SCAN  =' + Quotedstr(Trim(edtScan.Text)));
//      ShowMessage(sql.Text);
      open;
    end;
    if (not adoqueryTmp.IsEmpty) and (ADOQueryTmp.RecordCount = 1) then
    begin

      self.cds_main.Append;
      self.cds_main.FieldByName('FROMOrderno').Value := trim(adoqueryTmp.fieldbyname('FROMOrderno').AsString);
      self.cds_main.FieldByName('frommainid').Value := trim(adoqueryTmp.fieldbyname('frommainid').AsString);
      self.cds_main.FieldByName('fromsubid').Value := trim(adoqueryTmp.fieldbyname('fromsubid').AsString);
      self.cds_main.FieldByName('factoryName').Value := trim(adoqueryTmp.fieldbyname('factoryName').AsString);
      self.cds_main.FieldByName('YWY').Value := trim(adoqueryTmp.fieldbyname('YWY').AsString);
      self.cds_main.FieldByName('P_Code').Value := trim(adoqueryTmp.fieldbyname('P_Code').AsString);
      self.cds_main.FieldByName('P_CodeName').Value := trim(adoqueryTmp.fieldbyname('P_CodeName').AsString);
      self.cds_main.FieldByName('P_cf').Value := trim(adoqueryTmp.fieldbyname('P_cf').AsString);
      self.cds_main.FieldByName('P_spec').Value := trim(adoqueryTmp.fieldbyname('P_spec').AsString);
      self.cds_main.FieldByName('shazhi').Value := trim(adoqueryTmp.fieldbyname('shazhi').AsString);
      self.cds_main.FieldByName('P_Color').Value := trim(adoqueryTmp.fieldbyname('P_Color').AsString);
      self.cds_main.FieldByName('P_HX').Value := trim(adoqueryTmp.fieldbyname('P_HX').AsString);
      self.cds_main.FieldByName('P_SMF').Value := trim(adoqueryTmp.fieldbyname('P_SMF').AsString);
      self.cds_main.FieldByName('P_SKZ').Value := trim(adoqueryTmp.fieldbyname('P_SKZ').AsString);
      self.cds_main.FieldByName('RollNum').Value := adoqueryTmp.fieldbyname('kcRollNum').AsFloat;
      self.cds_main.FieldByName('ckprice').Value := adoqueryTmp.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 := adoqueryTmp.fieldbyname('kcQty').AsFloat;
      self.cds_main.FieldByName('QtyUnit').Value := trim(adoqueryTmp.fieldbyname('QtyUnit').AsString);
      self.cds_main.FieldByName('kuwei').Value := trim(adoqueryTmp.fieldbyname('kuwei').AsString);
      self.cds_main.FieldByName('YPType').Value := trim(adoqueryTmp.fieldbyname('YPType').AsString);
      self.cds_main.FieldByName('JuanHao').Value := trim(adoqueryTmp.fieldbyname('JuanHao').AsString);
      self.cds_main.FieldByName('ISCGPZ').Value := trim(adoqueryTmp.fieldbyname('ISCGPZ').AsString);
      self.cds_main.FieldByName('YPCFD').Value := trim(adoqueryTmp.fieldbyname('YPCFD').AsString);
      self.cds_main.Post;

    end
    else if (not adoqueryTmp.IsEmpty) then
    begin
      frmBpkcsumlistSEL := TfrmBpkcsumlistSEL.create(self);
      with frmBpkcsumlistSEL do
      begin
        fType := 10;
        CYID.Text := edtScan.Text;
        fckName := self.fckName;
        CANSHU8 := '1';
        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;
    edtScan.Text := '';
  end;
end;

procedure TfrmbpCk.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 TfrmbpCk.defstr2BtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'LinYongRen';
      flagname := '领用人';
      if ShowModal = 1 then
      begin
        defstr2.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmbpCk.CRTypeBtnClick(Sender: TObject);
begin
  frmSCPerson := TfrmSCPerson.Create(Application);
  with frmSCPerson do
  begin
    FlagStr := 'YPCRType';
    if ShowModal = 1 then
    begin
      CRType.Text := Trim(FSDPerson);
//      TJCPID.Text := 'CP1910290001';
    end;
    free;
  end;
end;

procedure TfrmbpCk.CRTypeDblClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'YPCRType';
      flagname := '出库类型';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmbpCk.Tv1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmProductOrderNewList_New := TfrmProductOrderNewList_New.Create(Application);
    with frmProductOrderNewList_New do
    begin
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('ToOrderNo').Value := Trim(Order_Main.fieldbyname('MLOrderNo').AsString);
        Self.CDS_Main.FieldByName('ToConNo').Value := Trim(Order_Main.fieldbyname('MLConNo').AsString);
        Self.CDS_Main.FieldByName('ToMainID').Value := Trim(Order_Main.fieldbyname('NewMLID').AsString);
        Self.CDS_Main.FieldByName('ToSubID').Value := Trim(Order_Main.fieldbyname('NewSubid').AsString);
        Self.CDS_Main.FieldByName('Price').Value := Trim(Order_Main.fieldbyname('MLPrice').AsString);
        if (Self.CDS_Main.FieldByName('Qty').Value <> null) and (Self.CDS_Main.FieldByName('Qty').Value <> 0) then
        begin
          if (Self.CDS_Main.FieldByName('Price').Value <> null) and (Self.CDS_Main.FieldByName('Price').Value <> 0) then
          begin
            Self.CDS_Main.FieldByName('money').Value := (Self.CDS_Main.FieldByName('Price').Value) * (Self.CDS_Main.FieldByName('Qty').Value);
          end;
        end;
      end;
    end;
  finally
    frmProductOrderNewList_New.Free;
  end;

end;

end.

