unit U_BpRk;

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
  TfrmbpRk = 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;
    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;
    Label4: TLabel;
    ywy: TBtnEditA;
    v1Column3: TcxGridDBColumn;
    ToolButton3: TToolButton;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    factoryName: TBtnEditA;
    Label9: TLabel;
    ToolButton4: TToolButton;
    v1Column8: TcxGridDBColumn;
    edtScan: TEdit;
    Label5: TLabel;
    Label7: TLabel;
    ZNote: TEdit;
    YPCFD: TComboBox;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    ToolButton5: TToolButton;
    Tv1Column5: TcxGridDBColumn;
    ToolButton6: TToolButton;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column8: 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 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 Tv1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1P_CodePropertiesChange(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
  private
    procedure InitGrid();
    function savedate(): Boolean;
    function UpperCase(const S: string): string;
    procedure SetComboBox();
    function YFData(): Boolean;
    { Private declarations }
  public
    fkeyNO: string;
    FType: integer;
    fCKName: string;
    { Public declarations }
  end;

var
  frmbpRk: TfrmbpRk;

implementation

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

function TfrmbpRk.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 TfrmbpRk.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 TfrmbpRk.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 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(ADOQueryTmp, maxno, 'YR', '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 := 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, '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);
          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(CDS_Main.fieldbyname('P_Code').AsString) <> '') and (CDS_Main.fieldbyname('Price').AsString <> '0') and (CDS_Main.fieldbyname('XZLX').AsString = '公司产品') then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPDATE ZH_CP_Info set YPPrice =' + CDS_Main.fieldbyname('Price').AsString + '');
        sql.Add('where ZIID =''' + CDS_Main.fieldbyname('P_Code').AsString + '''');
//        ShowMessage(sql.text);
        ExecSQL;
      end;
    end;
    if (trim(CDS_Main.fieldbyname('P_Code').AsString) <> '') and (CDS_Main.fieldbyname('Price').AsString <> '0') and ((CDS_Main.fieldbyname('XZLX').AsString = '印花面料') or (CDS_Main.fieldbyname('XZLX').AsString = '烫金面料')) then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPDATE ML_Manage set YPPrice =' + CDS_Main.fieldbyname('Price').AsString + '');
        sql.Add('where TJML =''' + CDS_Main.fieldbyname('P_Code').AsString + '''');
//        ShowMessage(sql.text);
        ExecSQL;
      end;
    end;
    if (trim(CDS_Main.fieldbyname('P_Code').AsString) <> '') and (CDS_Main.fieldbyname('Price').AsString <> '0') and (CDS_Main.fieldbyname('XZLX').AsString = '染色面料') then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPDATE ML_Manage set YPPrice =' + CDS_Main.fieldbyname('Price').AsString + '');
        sql.Add('where MLID =''' + CDS_Main.fieldbyname('CPID').AsString + '''');
//        ShowMessage(sql.text);
        ExecSQL;
      end;
    end;

//    with ADOQueryCmd do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('exec P_YP_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 TfrmbpRk.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*  from YP_InOut2 A');
      sql.Add('where 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;
      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;
    end;
  finally
  end;
end;

procedure TfrmbpRk.FormDestroy(Sender: TObject);
begin
  frmbpRk := nil;
end;

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

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

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

procedure TfrmbpRk.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 cds_main.Locate('YPLX', '', []) then
//  begin
//    application.MessageBox('类型不能为空！', '提示信息', 0);
//    exit;
//  end;
//  if cds_main.Locate('YPLX', 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 TfrmbpRk.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);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmbpRk.ToolButton1Click(Sender: TObject);
begin
  if trim(CRType.text) = '' then
  begin
    Application.MessageBox('需要先选择入库方式！', '提示', 0);
    exit;
  end;
  ToolBar1.SetFocus();
  frmZHCPList_Sel := TfrmZHCPList_Sel.create(self);
  with frmZHCPList_Sel do
  begin
    ZIType.ItemIndex := ZIType.Items.IndexOf(trim('单面料'));
    if showmodal = 1 then
    begin
      self.cds_main.DisableControls;
      Order_Main.DisableControls;
      self.cds_main.Append;
      self.cds_main.FieldByName('XZLX').Value := '公司产品';    // 保存类型记录
      self.cds_main.FieldByName('P_Code').Value := trim(Order_Main.fieldbyname('ZIID').AsString);
      self.cds_main.FieldByName('P_CodeName').Value := trim(Order_Main.fieldbyname('ZIName').AsString);
      self.cds_main.FieldByName('P_cf').Value := trim(Order_Main.fieldbyname('ZICF').AsString);
      self.cds_main.FieldByName('P_SMF').Value := trim(Order_Main.fieldbyname('ZIMF').AsString);
      self.cds_main.FieldByName('P_SKZ').Value := trim(Order_Main.fieldbyname('ZIKZ').AsString);
      self.cds_main.FieldByName('P_SPEC').Value := trim(Order_Main.fieldbyname('ZIGY').AsString);
      self.cds_main.FieldByName('qtyUnit').Value := 'M';
      self.cds_main.FieldByName('YPLX').Value := '样品';
      self.cds_main.FieldByName('RollNum').Value := 0;
      self.cds_main.FieldByName('price').Value := Order_Main.fieldbyname('YPprice').AsFloat;
      if Trim(CRType.text) = '大货剩余' then
      begin
        self.cds_main.FieldByName('YPLX').Value := '大货';
      end;
      self.cds_main.Post;
      Order_Main.EnableControls;
      self.cds_main.EnableControls;
    end;
    free;
  end;

end;

procedure TfrmbpRk.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    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) + ''' ');
      execsql;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_YP_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 TfrmbpRk.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmbpRk.v1P_ColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'YPGSColor';
      flagname := '公司颜色';
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('P_Color').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmbpRk.v1P_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  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 TfrmbpRk.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 TfrmbpRk.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 TfrmbpRk.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 TfrmbpRk.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('KHName').asstring;
    end;
    free;
  end;
end;

procedure TfrmbpRk.ToolButton4Click(Sender: TObject);
begin
  if trim(CRType.text) = '' then
  begin
    Application.MessageBox('需要先选择入库方式！', '提示', 0);
    exit;
  end;
  cds_main.Append;
  self.cds_main.FieldByName('RollNum').Value := 0;
  self.cds_main.FieldByName('price').Value := 0;
  self.cds_main.FieldByName('qtyUnit').Value := 'M';
  self.cds_main.FieldByName('YPLX').Value := '样品';
  if Trim(CRType.text) = '大货剩余' then
  begin
    self.cds_main.FieldByName('YPLX').Value := '大货';
  end;
  cds_main.Post;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmbpRk.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 TfrmbpRk.edtScanKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin

    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('Select AA.* From(');
      sql.Add(' select A.*,Cast(ROW_NUMBER() OVER(order by ZIID) as int) as RowNum ');
      sql.Add(' ,ImageFileTP=(select top 1 ImageFile from Image_Info B inner join Image_File C on B.IMID=C.IMID ');
      sql.Add('             where B.WBID=A.ZIID and B.IMNO=''图片'' and B.Valid=''Y'' and C.Valid=''Y'' and C.IFType=''小'' )');
      sql.Add(' ,SCTP=(select Top 1 WBID from Image_Info B inner join Image_File C on B.IMID=C.IMID ');
      sql.Add('             where B.WBID=A.ZIID and B.IMNO=''图片'' and B.Valid=''Y'' and C.Valid=''Y'' and C.IFType=''小'' )');
      sql.Add(' ,ImageFileNR=(select top 1 ImageFile from Image_Info B inner join Image_File C on B.IMID=C.IMID ');
      sql.Add('             where B.WBID=A.ZIID and B.IMNO=''内容'' and B.Valid=''Y'' and C.Valid=''Y'' and C.IFType=''小'' )');
      sql.Add(',GS=(select Sum(GGQty) from ZH_CP_Info_KC KC where KC.ZIID=A.ZIID )');
      sql.Add(' from ZH_CP_Info A  ');
      sql.Add(' where Valid=''Y''  ');
      sql.Add(')AA');
      sql.Add('where ZIID= ' + Quotedstr(Trim(edtScan.Text)));
//      ShowMessage(sql.text);
      open;
    end;

    if not adoqueryTmp.IsEmpty then
    begin
      with Self.CDS_Main do
      begin
        Append;
        FieldByName('P_Code').Value := Trim(adoqueryTmp.fieldbyname('ZIID').AsString);
        FieldByName('P_CodeName').Value := Trim(adoqueryTmp.fieldbyname('ZIName').AsString);
        FieldByName('P_SMF').Value := Trim(adoqueryTmp.fieldbyname('ZIMF').AsString);
        FieldByName('P_SKZ').Value := Trim(adoqueryTmp.fieldbyname('ZIKZ').AsString);
        FieldByName('P_SPEC').Value := Trim(adoqueryTmp.fieldbyname('ZIType').AsString);
        FieldByName('P_CF').Value := Trim(adoqueryTmp.fieldbyname('ZICF').AsString);
        self.cds_main.FieldByName('RollNum').Value := 0;
        self.cds_main.FieldByName('price').Value := 0;
        self.cds_main.FieldByName('qtyUnit').Value := '个';
        post;
      end;
    end;
    edtScan.Text := '';
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmbpRk.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 TfrmbpRk.Tv1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'KHCPName';
      flagname := '客户产品名称';
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('KHCPName').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmbpRk.v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'YPKuwei';
      flagname := '库位';
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('Kuwei').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmbpRk.Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'sehao';
      flagname := '公司色号';
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('sehao').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmbpRk.Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'KHColor';
      flagname := '客户颜色';
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('KHColor').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmbpRk.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'KHSeHao';
      flagname := '客户色号';
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('KHSeHao').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

function TfrmbpRk.UpperCase(const S: string): string;
var
  Ch: Char;
  L: Integer;
  Source, Dest: PChar;
begin
  L := Length(S);
  SetLength(Result, L);
  Source := Pointer(S);
  Dest := Pointer(Result);
  while L <> 0 do
  begin
    Ch := Source^;
    if (Ch >= 'a') and (Ch <= 'z') then
      Dec(Ch, 32);
    Dest^ := Ch;
    Inc(Source);
    Inc(Dest);
    Dec(L);
  end;
end;

procedure TfrmbpRk.v1P_CodePropertiesChange(Sender: TObject);
var
  mvalue, FFieldName: string;
  str: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;
  str := UpperCase(mvalue);
  with CDS_Main do
  begin
    Edit;
    FieldByName('P_Code').Value := str;
    Post;
  end;

  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmbpRk.ToolButton5Click(Sender: TObject);
begin
//  ToolBar1.SetFocus();
  if trim(CRType.text) = '' then
  begin
    Application.MessageBox('需要先选择入库方式！', '提示', 0);
    exit;
  end;
  frmMLMangeYH := TfrmMLMangeYH.create(self);
  with frmMLMangeYH do
  begin

    if showmodal = 1 then
    begin
      self.cds_main.DisableControls;
      CDS_Main.DisableControls;
      self.cds_main.Append;
      self.cds_main.FieldByName('XZLX').Value := trim(CDS_Main.fieldbyname('LX').AsString);    // 保存类型记录
      self.cds_main.FieldByName('CPID').Value := trim(CDS_Main.fieldbyname('MLID').AsString);    // 保存类型记录
      self.cds_main.FieldByName('P_Code').Value := trim(CDS_Main.fieldbyname('MLXXNO').AsString);
      self.cds_main.FieldByName('P_CodeName').Value := trim(CDS_Main.fieldbyname('MLName').AsString);
      self.cds_main.FieldByName('P_cf').Value := trim(CDS_Main.fieldbyname('MLCF').AsString);
      self.cds_main.FieldByName('P_SMF').Value := trim(CDS_Main.fieldbyname('MLMF').AsString);
      self.cds_main.FieldByName('P_SKZ').Value := trim(CDS_Main.fieldbyname('MLKZ').AsString);
      self.cds_main.FieldByName('P_SPEC').Value := trim(CDS_Main.fieldbyname('GYLXName').AsString);  //工艺
      self.cds_main.FieldByName('qtyUnit').Value := 'M';
      self.cds_main.FieldByName('YPLX').Value := '样品';
      self.cds_main.FieldByName('RollNum').Value := 0;
      self.cds_main.FieldByName('price').Value := CDS_Main.fieldbyname('YPprice').AsFloat;
      if Trim(CRType.text) = '大货剩余' then
      begin
        self.cds_main.FieldByName('YPLX').Value := '大货';
      end;
      self.cds_main.Post;
      CDS_Main.EnableControls;
      self.cds_main.EnableControls;
    end;
    free;
  end;
end;

procedure TfrmbpRk.ToolButton6Click(Sender: TObject);
begin
  frmBpkcsumlist_Sel := TfrmBpkcsumlist_Sel.create(self);
  with frmBpkcsumlist_Sel do
  begin
    fType := 10;
    fckName := self.fckName;
    YPLX.ItemIndex := YPLX.Items.IndexOf(trim('样品'));
    YPLX.Enabled := false;
    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('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('P_SPEC').Value := trim(cds_main.fieldbyname('P_SPEC').AsString);
//          self.cds_main.FieldByName('Qty').Value := 1;
          self.cds_main.FieldByName('SSKC').Value := cds_main.fieldbyname('kcQty').AsFloat;
          self.cds_main.FieldByName('price').Value := 0;
          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 := '大货';
          self.cds_main.Post;
        end;
        cds_main.Next;
      end;
      cds_main.EnableControls;
    end;
    free;
  end;
end;

end.

