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,
  cxContainer, cxCurrencyEdit, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinBlueprint,
  dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringTime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinsDefaultPainters,
  dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue,
  dxSkinscxPCPainter;

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;
    v1Qty: 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;
    v1MJXH: TcxGridDBColumn;
    v1MJId: TcxGridDBColumn;
    v1CPType: TcxGridDBColumn;
    v1P_Code: TcxGridDBColumn;
    InoutNo: TEdit;
    Label8: TLabel;
    Label4: TLabel;
    custName: TBtnEditA;
    v1Price: TcxGridDBColumn;
    v1Money: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    QtyMX: TEdit;
    Label17: TLabel;
    ADO_Sub: TADOQuery;
    DataSource2: TDataSource;
    CDS_Sub: TClientDataSet;
    PackFee: TcxCurrencyEdit;
    Label14: TLabel;
    v1OrderNo: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    TV2Column6: TcxGridDBColumn;
    TV2Column7: TcxGridDBColumn;
    TV2Column8: TcxGridDBColumn;
    TV2Column9: TcxGridDBColumn;
    TV2Column10: TcxGridDBColumn;
    TV2Column11: TcxGridDBColumn;
    TV2Column12: TcxGridDBColumn;
    TV2Column13: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolBar3: TToolBar;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    TV3Column1: TcxGridDBColumn;
    TV3Column2: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    TV3Column3: TcxGridDBColumn;
    TV3Column4: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    ToolButton5: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1P_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure v1P_ColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1P_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1P_ColorPropertiesEditValueChanged(Sender: TObject);
    procedure custNameBtnClick(Sender: TObject);
    procedure v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure v1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure QtyMXKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1QtyUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure TV2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure TV2Column2PropertiesEditValueChanged(Sender: TObject);
    procedure v1PricePropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv1Column3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    procedure InitGrid();
    function savedate(): Boolean;
    procedure Initsub();
    procedure InitMX();
    procedure SetComboBox();
    function YSData(): Boolean;
    function YFData(freeName: string): Boolean;
    function YSData_Other(fYFName: string; fmoney: double): Boolean;
    { Private declarations }
  public
    fkeyNO, FCodeName, FCode, FDRCKNO, FDRCKName: string;
    FType: integer;
    fCKName: string;
    { Public declarations }
  end;

var
  frmbpRk: TfrmbpRk;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_BpJylist, U_ZdyAttachment, U_ZdyAttachGYS,
  U_JGCklistSel, U_ProductOrderNewListSEl, U_ZdyAttachCP, U_ZdyCMZ_Sel,U_CPKWListSel,
  U_ZdyYS_Sel;
{$R *.dfm}

function TfrmbpRk.YSData_Other(fYFName: string; fmoney: double): 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) + ''' and YFDefFlag1=0 ');
    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 ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    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) + '''');
    sql.Add(' and YFName=''' + trim(fYFName) + ''' ');
    execsql;
  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(ADOQueryCmd, 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('Qty').Value := 1;
      FieldByName('Price').Value := fmoney;
      FieldByName('Money').Value := fmoney;
      FieldByName('BBMoney').Value := fmoney;
      FieldByName('HuiLv').Value := 1;
      FieldByName('BZType').Value := '人民币';
      FieldByName('QtyUnit').Value := '个';
      FieldByName('ComTaiTou').Value := Trim(cust);
      FieldByName('YFName').Value := fYFName;
        //  FieldByName('ISTAX').Value:=IsTax.Checked;
      FieldByName('MainId').Value := Trim(CDS_Main.fieldbyname('CRNO').AsString);
      FieldByName('subID').Value := Trim(CDS_Main.fieldbyname('CRID').AsString);
      FieldByName('note').Value := '退货';
      FieldByName('status').Value := '0';
      Post;
    end;
  end
  else
  begin
    YFID := Trim(ADOQueryTmp.fieldbyname('YFID').AsString);
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID=' + CRID);
    ExecSQL;
  end;
  Result := True;
end;

procedure TfrmbpRk.Initsub();
begin
//  if CDS_Main.IsEmpty then
//    exit;
//
//  with Ado_Sub do
//  begin
//    close;
//    sql.Clear;
//    sql.Add('select * from CP_InOut_MX A');
//    sql.Add('where A.MJID=' + quotedstr(trim(CDS_Main.fieldbyname('MJID').AsString)));
//    open;
//  end;
//  SCreateCDS20(Ado_Sub, CDS_sub);
//  SInitCDSData20(Ado_Sub, CDS_sub);
end;

function TfrmbpRk.YFData(freeName: 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) + ''' and YFDefFlag1=0 ');
    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 ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    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) + '''');
    sql.Add(' and YFName=''' + Trim(freeName) + '''');
    execsql;
  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(freeName) + '''');
    Open;
  end;

  if ADOQueryTmp.IsEmpty then
  begin
    if GetLSNo(ADOQueryCmd, YFID, 'CF', '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 := '人民币';
      FieldByName('QtyUnit').Value := Trim(CDS_Main.fieldbyname('QtyUnit').AsString);
      FieldByName('ComTaiTou').Value := Trim(cust);
      FieldByName('YFName').Value := freeName;
      FieldByName('MainId').Value := Trim(CDS_Main.fieldbyname('CRNO').AsString);
      FieldByName('subID').Value := Trim(CDS_Main.fieldbyname('CRID').AsString);
      FieldByName('status').Value := '0';
      Post;
    end;
  end
  else
  begin
    YFID := Trim(ADOQueryTmp.fieldbyname('YFID').AsString);
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID=' + CRID);
    ExecSQL;
  end;

  Result := True;
end;

function TfrmbpRk.YSData(): 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) + ''' and YFDefFlag1=0 ');
    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 ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    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) + '''');
    sql.Add(' and YFName=''客户退货'' ');
    execsql;
  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, '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 := -1 * CDS_Main.fieldbyname('RollNum').AsFloat;
      FieldByName('Qty').Value := -1 * CDS_Main.fieldbyname('Qty').AsFloat;
      FieldByName('Price').Value := CDS_Main.fieldbyname('Price').AsFloat;
      FieldByName('Money').Value := -1 * CDS_Main.fieldbyname('Money').AsFloat;
      FieldByName('BBMoney').Value := -1 * CDS_Main.fieldbyname('Money').AsFloat;
      FieldByName('HuiLv').Value := 1;
      FieldByName('BZType').Value := '人民币';
      FieldByName('QtyUnit').Value := Trim(CDS_Main.fieldbyname('QtyUnit').AsString);
      FieldByName('ComTaiTou').Value := Trim(cust);
      FieldByName('YFName').Value := '客户退货';
      FieldByName('MainId').Value := Trim(CDS_Main.fieldbyname('CRNO').AsString);
      FieldByName('subID').Value := Trim(CDS_Main.fieldbyname('CRID').AsString);
      FieldByName('note').Value := '退货';
      FieldByName('status').Value := '0';
      Post;
    end;
  end
  else
  begin
    YFID := Trim(ADOQueryTmp.fieldbyname('YFID').AsString);
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID=' + CRID);
    ExecSQL;
  end;

  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=''CPCK'' ');
    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=''CPRKTYPE''');
    if trim(fCKName) <> '' then
      sql.Add('and note=' + quotedstr(trim(fCKName)));
  //  sql.Add('and zdyName<>'+quotedstr(trim('加工入库')));
    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, XHNO, i: integer;
  Filler, FSize: string;
  FillTime: TdateTime;
begin
  result := false;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if FType = 0 then
    begin
      Filler := DName;
      FillTime := SGetServerDateTime(adoqueryTmp);
    end
    else
    begin
      with adoqueryTmp do
      begin
        close;
        sql.Clear;
        sql.Add('select * from CP_InOut');
        sql.Add('where CRNO=' + quotedstr(trim(fkeyNO)));
        open;
        Filler := adoqueryTmp.fieldbyname('Filler').AsString;
        FillTime := adoqueryTmp.fieldbyname('FillTime').AsDateTime;
      end;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete  from YF_Money_CR ');
      sql.Add(' where MainID=''' + Trim(fkeyNO) + '''');
      execsql;
    end;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from CP_InOut');
      sql.Add('where CRNO=' + quotedstr(trim(fkeyNO)));
      execsql;
    end;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from CP_kc');
      sql.Add('where CRNO=' + quotedstr(trim(fkeyNO)));
      execsql;
    end;

    CDS_Main.DisableControls;
    CDS_Main.First;
    XHNO := 1;
    with CDS_Main do
    begin
       First;

      while not eof do
      begin
        Edit;
        FieldByName('CRNO').Value := trim(fkeyNO);
//        FieldByName('CRID').Value := recNo;
        post;

//        with CDS_Sub do
//        begin
//          First;
//          while not eof do
//          begin
//            i := 1;
//            while i < 16 do
//            begin
//              FSize := 'Size' + IntToStr(i);
//              if StrToFloatDef(CDS_Sub.fieldbyname(FSize).AsString, 0) > 0 then
//              begin
                with ADOQueryCmd do
                begin
                  Close;
                  sql.Clear;
                  SQL.add('Update CP_Kcid Set kcid=kcid+1');
                  sql.Add('select kcid  from CP_Kcid');
                  Open;
                end;

                fkcID := ADOQueryCmd.fieldbyname('kcid').AsInteger;

                with ADOQueryCmd do
                begin
                  close;
                  sql.Clear;
                  sql.Add('select * from CP_InOut');
                  sql.Add('where 1=2 ');
                  open;
                end;

                ADOQueryCmd.Append;
                ADOQueryCmd.FieldByName('CRNO').Value := trim(fkeyNO);
                ADOQueryCmd.FieldByName('CRID').Value := XHNO;
                ADOQueryCmd.FieldByName('custNo').Value := trim(custName.TxtCode);
                ADOQueryCmd.FieldByName('custName').Value := trim(custName.Text);

                ADOQueryCmd.FieldByName('CRTime').Value := formatdateTime('yyyy-MM-dd', CRTIme.Date);
                ADOQueryCmd.FieldByName('CKName').Value := trim(CKName.Text);
                ADOQueryCmd.FieldByName('CRType').Value := trim(CRType.Text);
                ADOQueryCmd.FieldByName('CRFlag').Value := '入库';
                ADOQueryCmd.FieldByName('mainID').Value := trim(CDS_Main.fieldbyname('mainID').AsString);
                ADOQueryCmd.FieldByName('subID').Value := trim(CDS_Main.fieldbyname('subID').AsString);
//                ADOQueryCmd.FieldByName('MJID').Value := trim(CDS_Main.fieldbyname('MJID').AsString);
//                ADOQueryCmd.FieldByName('batchNo').Value := trim(CDS_Main.fieldbyname('batchNo').AsString);
//                ADOQueryCmd.FieldByName('MJXH').Value := trim(CDS_Main.fieldbyname('MJXH').AsString);
                ADOQueryCmd.FieldByName('CPTYPE').Value := trim(CDS_Main.fieldbyname('CPTYPE').AsString);
                ADOQueryCmd.FieldByName('P_Code').Value := trim(CDS_Main.fieldbyname('P_Code').AsString);
                ADOQueryCmd.FieldByName('P_CodeName').Value := trim(CDS_Main.fieldbyname('P_CodeName').AsString);
                ADOQueryCmd.FieldByName('P_spec').Value := trim(CDS_Main.fieldbyname('P_spec').AsString);
                ADOQueryCmd.FieldByName('P_Color').Value := trim(CDS_Main.fieldbyname('P_Color').AsString);
//                ADOQueryCmd.FieldByName('P_ColorNo').Value := trim(CDS_Sub.fieldbyname('P_ColorNo').AsString);
//                ADOQueryCmd.FieldByName('P_MF').Value := CDS_Main.fieldbyname('P_MF').AsFloat;
//                ADOQueryCmd.FieldByName('P_KZ').Value := CDS_Main.fieldbyname('P_KZ').AsFloat;
                ADOQueryCmd.FieldByName('RollNum').Value := CDS_Main.fieldbyname('RollNum').AsFloat;
//                ADOQueryCmd.FieldByName('Qty').Value := CDS_Sub.fieldbyname(FSize).AsFloat;
                   ADOQueryCmd.FieldByName('Qty').Value := CDS_Main.fieldbyname('Qty').AsFloat;
//                ADOQueryCmd.FieldByName('QtyUnit').Value := trim(CDS_Main.fieldbyname('QtyUnit').AsString);
                ADOQueryCmd.FieldByName('Price').Value := CDS_Main.fieldbyname('price').AsFloat;
                ADOQueryCmd.FieldByName('rkPrice').Value := CDS_Main.fieldbyname('price').AsFloat;
                ADOQueryCmd.FieldByName('money').Value := CDS_Main.fieldbyname('money').AsFloat;
                ADOQueryCmd.FieldByName('note').Value := trim(CDS_Main.fieldbyname('note').AsString);
                ADOQueryCmd.FieldByName('Filler').Value := trim(Filler);
                ADOQueryCmd.FieldByName('FillTime').Value := dateTimetostr(FillTime);
                ADOQueryCmd.FieldByName('InOutNo').Value := trim(InOutNo.Text);

                ADOQueryCmd.FieldByName('DRCKNO').Value := trim(CDS_Main.fieldbyname('DRCKNO').AsString);
                ADOQueryCmd.FieldByName('DRCKName').Value := trim(CRType.Text);
//                trim(CDS_Main.fieldbyname('DRCKName').AsString);
//                ADOQueryCmd.FieldByName('fromMainID').Value := trim(CDS_Main.fieldbyname('fromMainID').AsString);
//                ADOQueryCmd.FieldByName('fromsubID').Value := trim(CDS_Main.fieldbyname('fromsubID').AsString);
                ADOQueryCmd.FieldByName('defstr1').Value := trim(CDS_Main.fieldbyname('defstr1').AsString);
//                ADOQueryCmd.FieldByName('defstr2').Value := trim(CDS_Main.fieldbyname('defstr2').AsString);
//                ADOQueryCmd.FieldByName('defstr3').Value := trim(CDS_Main.fieldbyname('defstr3').AsString);
//                ADOQueryCmd.FieldByName('defstr4').Value := trim(CDS_Main.fieldbyname('defstr4').AsString);
//                ADOQueryCmd.FieldByName('defstr5').Value := trim(CDS_Main.fieldbyname('defstr5').AsString);
//                if CheckBox1.Checked then
//                begin
//                  ADOQueryCmd.FieldByName('Caijian').Value := '裁剪';
//                end;
//                if CheckBox2.Checked then
//                begin
//                  ADOQueryCmd.FieldByName('Fengren').Value := '缝纫';
//                end;
//                if i = 1 then
//                begin
//                  ADOQueryCmd.FieldByName('defstr1').Value := 'CXXS';
//                end
//                else if i = 2 then
//                begin
//                  ADOQueryCmd.FieldByName('defstr1').Value := 'CXS';
//                end
//                else if i = 3 then
//                begin
//                  ADOQueryCmd.FieldByName('defstr1').Value := 'CS';
//                end
//                else if i = 4 then
//                begin
//                  ADOQueryCmd.FieldByName('defstr1').Value := 'CM';
//                end
//                else if i = 5 then
//                begin
//                  ADOQueryCmd.FieldByName('defstr1').Value := 'CL';
//                end
//                else if i = 6 then
//                begin
//                  ADOQueryCmd.FieldByName('defstr1').Value := 'CXL';
//                end
//                else if i = 7 then
//                begin
//                  ADOQueryCmd.FieldByName('defstr1').Value := 'AXS';
//                end
//                else if i = 8 then
//                begin
//                  ADOQueryCmd.FieldByName('defstr1').Value := 'AS';
//                end
//                else if i = 9 then
//                begin
//                  ADOQueryCmd.FieldByName('defstr1').Value := 'AM';
//                end
//                else if i = 10 then
//                begin
//                  ADOQueryCmd.FieldByName('defstr1').Value := 'AL';
//                end
//                else if i = 11 then
//                begin
//                  ADOQueryCmd.FieldByName('defstr1').Value := 'AXL';
//                end
//                else if i = 12 then
//                begin
//                  ADOQueryCmd.FieldByName('defstr1').Value := 'AXXL';
//                end
//                else if i = 13 then
//                begin
//                  ADOQueryCmd.FieldByName('defstr1').Value := 'CXXL';
//                end
//                else if i = 14 then
//                begin
//                  ADOQueryCmd.FieldByName('defstr1').Value := 'AXXXL';
//                end
//                else if i = 15 then
//                begin
//                  ADOQueryCmd.FieldByName('defstr1').Value := 'CXXXS';
//                end;

                if FType = 1 then
                begin
                  ADOQueryCmd.FieldByName('editer').Value := trim(dName);
                  ADOQueryCmd.FieldByName('editTime').Value := SGetServerDateTime(adoqueryTmp);
                end;
                ADOQueryCmd.FieldByName('kcID').Value := fkcID;
                ADOQueryCmd.Post;

                with ADOQueryCmd do
                begin
                  close;
                  sql.Clear;
                  sql.Add('select * from CP_KC');
                  sql.Add('where 1=2 ');
                  open;
                end;
                ADOQueryCmd.Append;
                ADOQueryCmd.FieldByName('kcID').Value := fkcID;
                ADOQueryCmd.FieldByName('CKName').Value := trim(CKName.Text);
                ADOQueryCmd.FieldByName('CRNO').Value := trim(fkeyNO);
                ADOQueryCmd.FieldByName('CRID').Value := XHNO;
                ADOQueryCmd.FieldByName('CRTime').Value := formatdateTime('yyyy-MM-dd', CRTIme.Date);
                ADOQueryCmd.FieldByName('CRType').Value := trim(CRType.Text);
                ADOQueryCmd.FieldByName('CRFlag').Value := '入库';
                ADOQueryCmd.FieldByName('mainID').Value := trim(cds_main.fieldbyname('mainID').AsString);
                ADOQueryCmd.FieldByName('subID').Value := trim(cds_main.fieldbyname('subID').AsString);
//                ADOQueryCmd.FieldByName('MJXH').Value := trim(fieldbyname('MJXH').AsString);
//                ADOQueryCmd.FieldByName('MJID').Value := trim(fieldbyname('MJID').AsString);
                ADOQueryCmd.FieldByName('CPType').Value := trim(cds_main.fieldbyname('CPType').AsString);
                ADOQueryCmd.FieldByName('P_Code').Value := trim(cds_main.fieldbyname('P_Code').AsString);
                ADOQueryCmd.FieldByName('P_CodeName').Value := trim(cds_main.fieldbyname('P_CodeName').AsString);
                ADOQueryCmd.FieldByName('P_spec').Value := trim(cds_main.fieldbyname('P_spec').AsString);
                ADOQueryCmd.FieldByName('P_Color').Value := trim(CDS_Main.fieldbyname('P_Color').AsString);
                ADOQueryCmd.FieldByName('DRCKNO').Value := trim(CDS_Main.fieldbyname('DRCKNO').AsString);
                ADOQueryCmd.FieldByName('DRCKName').Value := trim(CDS_Main.fieldbyname('DRCKName').AsString);
                ADOQueryCmd.FieldByName('Qty').Value := CDS_Main.fieldbyname('QTY').AsFloat;
                ADOQueryCmd.FieldByName('kcID').Value := fkcID;
                ADOQueryCmd.Post;

                XHNO := XHNO + 1;
//              end;
//
//              i := i + 1;
//            end;

            Next;
          end;
        end;




//        if trim(CRType.Text) = '加工入库' then
//        begin
//          if not YFData('加工费') then
//          begin
//            ADOQueryCmd.Connection.RollbackTrans;
//            exit;
//          end;
//        end;
//        if trim(CRType.Text) = '退货入库' then
//        begin
//          if not YSData() then
//          begin
//            ADOQueryCmd.Connection.RollbackTrans;
//            exit;
//          end;
//        end;
        next;
//      end;
  {    if trim(CRType.Text)='退货入库' then
      begin
        IF strtofloatdef(PackFee.Text,0)<>0 then
        begin
         if not YSData_other('打包费',strtofloatdef(PackFee.Text,0)) then
         begin
           ADOQueryCmd.Connection.RollbackTrans;
           exit;
         end;
        end;
      end;  }
//    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 isnull(mainID,'''') as Mainid,isnull(subID,'''') as Subid,isnull(P_Code,'''') as P_Code,isnull(P_CodeName,'''') as P_CodeName,isnull(P_spec,'''') as P_spec ');
      sql.Add(',isnull(P_MF,'''') as P_MF,isnull(P_KZ,'''') as P_KZ,RKprice,isnull(P_Color,'''') as P_Color,isnull(defstr1,'''') as defstr1,');
      sql.Add('SUM(RollNum ) as RollNum,');
      sql.Add('SUM((Qty+zsQty)) as Qty,');
      sql.Add('CAST( P_KZ as varchar(10)) as P_KZ_s,');
      sql.Add('SUM((Qty+zsQty))*RKprice as money');
      sql.Add(',ISNULL(OrderNO,'''')  as OrderNO');
      sql.Add(',isnull(DRCKNO ,'''') as DRCKNO	,isnull(DRCKName,'''') as DRCKName,CRNO,InOutNo,CRTime,isnull(custName,'''') as custName,CRType,CKName,isnull(custNo,'''') as custNo,CPType ');
      SQL.Add(',isnull(Note,'''') as Note,isnull(Price,0) as Price');
      sql.Add('from (select A.*,');
      sql.Add('orderNo=(select Top 1 orderNo from JYOrder_Main X where X.MainID=A.MainID )');
      sql.Add('from CP_InOut A');
      sql.Add('where CRNO=' + quotedstr(trim(fkeyNO)));
      SQL.Add('and P_Code=' + quotedstr(trim(FCode)));
      SQL.Add('and P_CodeName=' + quotedstr(trim(FCodeName)));
      SQL.Add('and DRCKNO=' + quotedstr(trim(FDRCKNO)));
      SQL.Add('and DRCKName=' + quotedstr(trim(FDRCKName)));
      sql.Add(')AA');
      sql.Add('group by ISNULL(mainID,''''),ISNULL(subID,''''),isnull(P_Code,''''),isnull(P_CodeName,''''),ISNULL(P_spec,'''')');
      sql.Add(',isnull(P_MF,''''),isnull(P_KZ,''''),RKprice,ISNULL(OrderNO,'''') ,isnull(P_Color,''''),isnull(defstr1,''''),CAST( P_KZ as varchar(10))');
      sql.Add(',isnull(DRCKNO ,'''')	,isnull(DRCKName,''''),CRNO,InOutNo,CRTime,isnull(custName,''''),CRType,CKName,isnull(custNo,''''),CPType');
      sql.Add(',isnull(Note,''''),isnull(Price,0)');
      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));
      inoutNo.Text := CDS_Main.fieldbyname('inoutNo').asstring;
      custName.TxtCode := trim(CDS_Main.fieldbyname('custNo').AsString);
      custName.Text := trim(CDS_Main.fieldbyname('custName').AsString);
   //   FirstName.TxtCode:=trim(CDS_Main.fieldbyname('FirstNo').AsString);
   //   FirstName.Text:=trim(CDS_Main.fieldbyname('FirstName').AsString);
    end
    else
      ToolButton1.Click;
  finally
  end;
  Initsub();
  InitMX();
end;

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

procedure TfrmbpRk.FormCreate(Sender: TObject);
begin
  CRTime.Date := DServerDate;
  Panel2.Align := alclient;

end;

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

procedure TfrmbpRk.FormShow(Sender: TObject);
begin
  readCxGrid(self.Caption + tv1.Name + '4', Tv1, '成品仓库');
  SetComboBox();
  InitGrid();
  if fType = 0 then
  begin
    if GetLSNo(ADOQueryCmd, fkeyNo, 'RK', 'CP_InOut', 3, 1) = False then
    begin
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    inoutNo.Text := 'R' + Trim(RightBStr(fkeyNo, Length(fkeyNo) - 2));
  end;
end;

procedure TfrmbpRk.TBSaveClick(Sender: TObject);
begin
  if cds_main.IsEmpty then
    exit;
  CRTime.SetFocus;
  if trim(CRType.Text) = '' then
  begin
    application.MessageBox('入库类型不能为空！', '提示信息', 0);
    exit;
  end;

  if trim(custName.Text) = '' then
  begin
    application.MessageBox('加工厂不能为空！', '提示信息', MB_ICONERROR);
    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  trim(CRType.Text)='加工入库' then
  begin
    if cds_main.Locate('fromMainID',NULL,[])  then
    begin
      application.MessageBox('加工单号不能为空！','提示信息',0);
      exit;
    end;
    if cds_main.Locate('fromMainID','',[])  then
    begin
      application.MessageBox('加工单号不能为空！','提示信息',0);
      exit;
    end;
  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);
        Self.CDS_Main.FieldByName('P_Code').Value := Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmbpRk.ToolButton1Click(Sender: TObject);
//var
//  FMJID: string;
begin
//  if GetLSNo(self.ADOQueryCmd, FMJID, 'MR', 'CP_InOut_MX', 3, 1) = False then
//  begin
//      //  ADOQueryCmd.Connection.RollbackTrans;
//    Application.MessageBox('取最大号失败!', '提示', 0);
//    Exit;
//  end;
  with CDS_Main do
  begin
    append;
//    FieldByName('MJID').Value := trim(FMJID);
    FieldbyName('CPType').Value := '正品';
 //   FieldbyName('QtyUnit').Value:='KG';
    post;
  end;
{ frmBpJylist:=TfrmBpJylist.create(self);
  with frmBpJylist do
  begin
    fType:=10;
 //   fckName:=self.fckName;
    if showmodal=1 then
    begin
      self.cds_main.DisableControls;
      cds_main.DisableControls;
      cds_main.First;
      while not cds_main.Eof  do
      begin
        if (cds_main.fieldbyname('ssel').AsBoolean)  then  //and (not self.cds_main.Locate('MJID',trim(cds_main.fieldbyname('MJID').AsString),[]))
        begin
        self.cds_main.Append;
      //  self.cds_main.FieldByName('kcid').Value:=trim(cds_main.fieldbyname('kcid').AsString);
        self.cds_main.FieldByName('mainID').Value:=trim(cds_main.fieldbyname('mainID').AsString);
        self.cds_main.FieldByName('subID').Value:=trim(cds_main.fieldbyname('subID').AsString);
        self.cds_main.FieldByName('MJID').Value:=trim(cds_main.fieldbyname('MJID').AsString);
        self.cds_main.FieldByName('MJXH').Value:=trim(cds_main.fieldbyname('MJXH').AsString);
        self.cds_main.FieldByName('CPTYPE').Value:=trim(cds_main.fieldbyname('MJType').AsString);
        self.cds_main.FieldByName('P_Code').Value:=trim(cds_main.fieldbyname('MprtCode').AsString);
        self.cds_main.FieldByName('P_CodeName').Value:=trim(cds_main.fieldbyname('MprtCodeName').AsString);
        self.cds_main.FieldByName('P_spec').Value:=trim(cds_main.fieldbyname('Mprtspec').AsString);
    //    self.cds_main.FieldByName('P_Color').Value:=trim(cds_main.fieldbyname('P_Color').AsString);
    //    self.cds_main.FieldByName('P_MF').Value:=cds_main.fieldbyname('P_MF').AsFloat;
    //    self.cds_main.FieldByName('P_KZ').Value:=cds_main.fieldbyname('P_KZ').AsFloat;
        self.cds_main.FieldByName('RollNum').Value:=1;
        self.cds_main.FieldByName('Qty').Value:=cds_main.fieldbyname('MJLen').AsFloat;
        self.cds_main.FieldByName('QtyUnit').Value:=trim(cds_main.fieldbyname('MJTypeother').AsString);
        self.cds_main.Post;
        end;
        cds_main.Next;
      end;
      cds_main.EnableControls;
      self.cds_main.EnableControls;
    end;
    free;
  end;}
end;

procedure TfrmbpRk.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  CDS_Main.Delete;
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 := 'OrdColor';
//      flagname := '颜色';
//      V1Name.Caption := '中文名称';
//      V1Note.Caption := '英文名称';
//      MainType := Trim(DName);
//      fnote := True;
//      if ShowModal = 1 then
//      begin
//        Self.CDS_Main.Edit;
//        Self.CDS_Main.FieldByName('P_Color').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
//        Self.CDS_Main.Post;
//      end;
//    end;
//  finally
//    frmZDYHelp.Free;
//  end;
//  self.tv1.Controller.EditingController.ShowEdit();

  frmZdyYS_sel := TfrmZdyYS_sel.create(self);
  with frmZdyYS_sel do
  begin
    if showmodal = 1 then
    begin
      with CDS_Main do
      begin
        Edit;
        FieldByName('P_Color').value := CDS_HZ.fieldbyname('CodeName').AsString;
        Post;
      end;
    end;
    free;
  end;
end;

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

      if ShowModal = 1 then
      begin
        with CDS_Main do
        begin
          CDS_Main.Edit;
          CDS_Main.fieldbyname('P_Code').Value := Trim(CDS_HZ.fieldbyname('ZDYCode').AsString);
          CDS_Main.fieldbyname('P_CodeName').Value := Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
//        CDS_Main.fieldbyname('defstr1').Value := Trim(CDS_HZ.fieldbyname('CYCM').AsString);
          CDS_Main.Post;
        end;

        with CDS_Sub do
        begin
          append;
          FieldByName('P_Color').Value := Trim(CDS_HZ.fieldbyname('CYYS').AsString);
          post;
        end;
      end;

    end;
  finally
    frmZdyAttachCP.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmbpRk.v1P_ColorPropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
{  mvalue:=TcxTextEdit(Sender).EditingText;

  IF trim(mvalue)='' then exit;

  IF rightBstr(trim(mvalue),1)<>'#'THEN
  BEGIn
    mvalue:=trim(mvalue)+'#';
  END;



  with CDS_Main do
  begin
    Edit;
    FieldByName('P_Color').Value:=Trim(mvalue);
    Post;
  end;
  self.tv1.Controller.EditingController.ShowEdit();}
end;

procedure TfrmbpRk.custNameBtnClick(Sender: TObject);
begin
  if CRType.Text = '退货入库' then
  begin
    try
      frmZdyAttachment := TfrmZdyAttachment.Create(Application);
      with frmZdyAttachment do
      begin
        if ShowModal = 1 then
        begin
          custName.Text := Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
          custName.TxtCode := Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
        end;
      end;
    finally
      frmZdyAttachment.Free;
    end;
  end
  else
  begin
    try
      frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
      with frmZdyAttachGYS do
      begin
        if ShowModal = 1 then
        begin
          custName.Text := Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
          custName.TxtCode := Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
        end;
      end;
    finally
      frmZdyAttachGYS.Free;
    end;
  end;
end;

procedure TfrmbpRk.v1QtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    FieldbyName('Money').Value := format('%.2f', [FieldByName('Qty').AsFloat * FieldByName('Price').AsFloat]);
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmbpRk.v1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmJGCklistSel := TfrmJGCklistSel.create(self);
  with frmJGCklistSel do
  begin
    cust.text := self.custName.Text;
    if showmodal = 1 then
    begin
      self.CDS_Main.edit;
      self.CDS_Main.FieldByName('frommainid').Value := CDS_Main.FieldByName('crno').AsString;
      self.CDS_Main.FieldByName('fromsubid').Value := CDS_Main.FieldByName('crid').AsString;
      self.CDS_Main.post;

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

procedure TfrmbpRk.QtyMXKeyPress(Sender: TObject; var Key: Char);
var
  fMJXH: string;
  fRollNum: integer;
  fQty: double;
begin
  if Key = #13 then
  begin
    if strtofloatdef(QtyMX.Text, 0) = 0 then
      exit;

    if GetLSNo(self.ADOQueryCmd, fMJXH, 'MX', 'CP_InOut_MX', 4, 1) = False then
    begin
      //  ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    try
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select *  from CP_InOut_MX A');
        sql.Add('where 1=2');
        open;
        append;
        Fieldbyname('MJID').Value := trim(CDS_Main.fieldbyname('MJID').AsString);
        Fieldbyname('MJXH').Value := fMJXH;
        Fieldbyname('RollNUm').Value := 1;
        Fieldbyname('Qty').Value := strtofloatdef(QtyMX.Text, 0);
        Fieldbyname('Qtyunit').Value := trim(CDS_Main.fieldbyname('Qtyunit').AsString);
        post;
      end;

      with CDS_sub do
      begin
        append;
        Fieldbyname('Qty').Value := strtofloatdef(QtyMX.Text, 0);
        post;
      end;
      if varisnull(tv2.DataController.Summary.FooterSummaryValues[0]) then
      begin
        fRollNum := 0;
        fQty := 0;
      end
      else
      begin
        fRollNum := CDS_Sub.RecordCount;
        fQty := tv2.DataController.Summary.FooterSummaryValues[0];
      end;
      with CDS_Main do
      begin
        EDIT;
        FieldByName('RollNum').Value := fRollNum;
        Fieldbyname('Qty').Value := fQty;
        FieldbyName('Money').Value := format('%.2f', [FieldByName('Qty').AsFloat * FieldByName('Price').AsFloat]);
        post;
      end;
      QtyMX.Text := '';
    except
    end;
  end;
end;

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

procedure TfrmbpRk.Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
var
  FROLLnUM: INTEGER;
  FQTY: double;
begin
  if CDS_Sub.IsEmpty then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete   from CP_InOut_MX ');
      sql.Add('where MJXH=' + quotedstr(trim(CDS_Sub.fieldbyname('MJXH').AsString)));
      execsql;
    end;
    Initsub();
    if varisnull(tv2.DataController.Summary.FooterSummaryValues[0]) then
    begin
      FROLLnUM := 0;
      FQTY := 0;
    end
    else
    begin
      FROLLnUM := CDS_Sub.RecordCount;
      FQTY := tv2.DataController.Summary.FooterSummaryValues[0];
    end;
    with CDS_Main do
    begin
      EDIT;
      FieldByName('RollNum').Value := FROLLnUM;
      Fieldbyname('Qty').Value := FQTY;
      FieldbyName('Money').Value := format('%.2f', [FieldByName('Qty').AsFloat * FieldByName('Price').AsFloat]);
      post;
    end;
  except
    application.MessageBox('删除失败', '提示信息', 0)
  end;
end;

procedure TfrmbpRk.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmProductOrderNewListSEl := TfrmProductOrderNewListSEl.create(self);
  with frmProductOrderNewListSEl do
  begin
    FCloth := 1;
    if showmodal = 1 then
    begin
      Order_Main.First;
      while not Order_Main.Eof do
      begin
        if Order_Main.fieldbyname('ssel').AsBoolean then
        begin
          CDS_Main.edit;       
          CDS_Main.FieldByName('orderNo').Value := trim(order_main.fieldbyname('orderNo').asstring);
          CDS_Main.FieldByName('mainID').Value := trim(order_main.fieldbyname('mainID').asstring);
          CDS_Main.FieldByName('subID').Value := trim(order_main.fieldbyname('subID').asstring);
          CDS_Main.FieldByName('P_Code').Value := trim(order_main.fieldbyname('prtcode').asstring);
          CDS_Main.FieldByName('P_CodeName').Value := trim(order_main.fieldbyname('prtCodeName').asstring);
          CDS_Main.FieldByName('P_Spec').Value := trim(order_main.fieldbyname('prtspec').asstring);
          CDS_Main.FieldByName('P_Color').Value := trim(order_main.fieldbyname('prtcolor').asstring);
//          CDS_Main.FieldByName('P_EColor').Value := trim(order_main.fieldbyname('SOrdDefStr4').asstring);  //颜色英文
          CDS_Main.FieldByName('defstr1').Value := trim(order_main.fieldbyname('Sorddefstr5').asstring);
          CDS_Main.FieldByName('qty').Value := trim(order_main.fieldbyname('PRTOrderQty').asstring);

          CDS_Main.post;
        end;
        Order_Main.Next;
      end;

      free;

//      CDS_Main.Edit;
//      CDS_Main.FieldByName('orderNo').Value := trim(order_main.fieldbyname('orderNo').asstring);
//      CDS_Main.FieldByName('mainID').Value := trim(order_main.fieldbyname('mainID').asstring);
//      CDS_Main.FieldByName('subID').Value := trim(order_main.fieldbyname('subID').asstring);
//      CDS_Main.FieldByName('P_Code').Value := trim(order_main.fieldbyname('prtcode').asstring);
//      CDS_Main.FieldByName('P_CodeName').Value := trim(order_main.fieldbyname('prtCodeName').asstring);
//      CDS_Main.FieldByName('P_Spec').Value := trim(order_main.fieldbyname('prtspec').asstring);
////      CDS_Main.FieldByName('P_Color').Value := trim(order_main.fieldbyname('prtcolor').asstring);
////      CDS_Main.FieldByName('defstr1').Value := trim(order_main.fieldbyname('Sorddefstr5').asstring);
////      CDS_Main.FieldByName('P_MF').Value := trim(order_main.fieldbyname('PRTMF').asstring);
////      CDS_Main.FieldByName('P_KZ').Value := trim(order_main.fieldbyname('PRTKZ').asstring);
//      CDS_Main.post;
    end;
//    free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmbpRk.v1QtyUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'QtyUnitType';
      flagname := '数量单位';
      V1Name.Caption := '数量单位';
      fnote := false;
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('QtyUnit').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_Main.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmbpRk.v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmZdyCMZ_sel := TfrmZdyCMZ_sel.create(self);
  with frmZdyCMZ_sel do
  begin
    if showmodal = 1 then
    begin
      with CDS_Main do
      begin
        Edit;
        FieldByName('defstr1').value := cdssub.fieldbyname('SizeSpec').AsString;
        Post;
      end;
    end;
    free;
  end;
end;

procedure TfrmbpRk.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  initsub();
end;

procedure TfrmbpRk.InitMX();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_Get_CP_RK_MX ''' + Cds_main.fieldbyname('P_CodeName').AsString + ''' ,''' + Cds_main.fieldbyname('P_Code').AsString + ''',''' + Cds_main.fieldbyname('CRNO').AsString + ''',''' + Cds_main.fieldbyname('DRCKNO').AsString + ''',''' + Cds_main.fieldbyname('DRCKName').AsString + ''' ');
//      ShowMessage(SQL.text);
      open;
    end;
    SCreateCDS20(adoqueryTmp, CDS_Sub);
    SInitCDSData20(adoqueryTmp, CDS_Sub);
  finally
  end;
end;

procedure TfrmbpRk.TV2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmZdyYS_sel := TfrmZdyYS_sel.create(self);
  with frmZdyYS_sel do
  begin
    if showmodal = 1 then
    begin
      with CDS_Sub do
      begin
        Edit;
        FieldByName('P_Color').value := CDS_HZ.fieldbyname('CodeName').AsString;
        Post;
      end;
    end;
    free;
  end;
end;

procedure TfrmbpRk.ToolButton3Click(Sender: TObject);
begin
  with CDS_Sub do
  begin
    append;
    post;
  end;
end;

procedure TfrmbpRk.ToolButton4Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    exit;
  CDS_Sub.Delete;
end;

procedure TfrmbpRk.TV2Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  Qty1, Qty2, Qty3, Qty4, Qty5, Qty6, Qty7, Qty8, Qty9, Qty10, Qty11, Qty12, Qty13, Qty14, Qty15, ZQty, a: Integer;
  Fprice, Fmoney: Double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv3.Controller.FocusedColumn.DataBinding.FilterFieldName);

  with CDS_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;

  Qty1 := StrToIntDef(CDS_Sub.fieldbyname('Size1').asstring, 0);
  Qty2 := StrToIntDef(CDS_Sub.fieldbyname('Size2').asstring, 0);
  Qty3 := StrToIntDef(CDS_Sub.fieldbyname('Size3').asstring, 0);
  Qty4 := StrToIntDef(CDS_Sub.fieldbyname('Size4').asstring, 0);
  Qty5 := StrToIntDef(CDS_Sub.fieldbyname('Size5').asstring, 0);
  Qty6 := StrToIntDef(CDS_Sub.fieldbyname('Size6').asstring, 0);
  Qty7 := StrToIntDef(CDS_Sub.fieldbyname('Size7').asstring, 0);
  Qty8 := StrToIntDef(CDS_Sub.fieldbyname('Size8').asstring, 0);
  Qty9 := StrToIntDef(CDS_Sub.fieldbyname('Size9').asstring, 0);
  Qty10 := StrToIntDef(CDS_Sub.fieldbyname('Size10').asstring, 0);
  Qty11 := StrToIntDef(CDS_Sub.fieldbyname('Size11').asstring, 0);
  Qty12 := StrToIntDef(CDS_Sub.fieldbyname('Size12').asstring, 0);
  Qty13 := StrToIntDef(CDS_Sub.fieldbyname('Size13').asstring, 0);
  Qty14 := StrToIntDef(CDS_Sub.fieldbyname('Size14').asstring, 0);
  Qty15 := StrToIntDef(CDS_Sub.fieldbyname('Size15').asstring, 0);
  ZQty := Qty1 + Qty2 + Qty3 + Qty4 + Qty5 + Qty6 + Qty7 + Qty8 + Qty9 + Qty10 + Qty11 + Qty12 + Qty13 + Qty14 + Qty15;

  with CDS_Sub do
  begin
    Edit;
    FieldByName('ZQty').Value := ZQty;
    Post;
  end;

  tv3.Controller.EditingController.ShowEdit();
//   CDS_Main.FieldByName('Qty').Value
  a := TV3.DataController.Summary.FooterSummaryValues[15];

  CDS_Main.Edit;
  CDS_Main.FieldByName('Qty').Value := a;
  CDS_Main.post;

  Fprice := StrToIntDef(CDS_Main.fieldbyname('Price').asstring, 0);

  Fmoney := Fprice * a;

  with CDS_Main do
  begin
    Edit;
    FieldByName('Money').Value := Fmoney;
    Post;
  end;
end;

procedure TfrmbpRk.v1PricePropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    FieldbyName('Money').Value := format('%.2f', [FieldByName('Qty').AsFloat * FieldByName('Price').AsFloat]);
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmbpRk.ToolButton5Click(Sender: TObject);
begin
  frmProductOrderNewListSEl := TfrmProductOrderNewListSEl.create(self);
  with frmProductOrderNewListSEl do
  begin
    FCloth := 1;
    if showmodal = 1 then
    begin
      Order_Main.First;
      while not Order_Main.Eof do
      begin
        if Order_Main.fieldbyname('ssel').AsBoolean then
        begin
          CDS_Main.Append;
          CDS_Main.FieldByName('orderNo').Value := trim(order_main.fieldbyname('orderNo').asstring);
          CDS_Main.FieldByName('mainID').Value := trim(order_main.fieldbyname('mainID').asstring);
          CDS_Main.FieldByName('subID').Value := trim(order_main.fieldbyname('subID').asstring);
          CDS_Main.FieldByName('P_Code').Value := trim(order_main.fieldbyname('prtcode').asstring);
          CDS_Main.FieldByName('P_CodeName').Value := trim(order_main.fieldbyname('prtCodeName').asstring);
          CDS_Main.FieldByName('P_Spec').Value := trim(order_main.fieldbyname('prtspec').asstring);
          CDS_Main.FieldByName('P_Color').Value := trim(order_main.fieldbyname('prtcolor').asstring);
//          CDS_Main.FieldByName('P_EColor').Value := trim(order_main.fieldbyname('SOrdDefStr4').asstring);  //颜色英文
          CDS_Main.FieldByName('defstr1').Value := trim(order_main.fieldbyname('Sorddefstr5').asstring);
          CDS_Main.FieldByName('qty').Value := trim(order_main.fieldbyname('PRTOrderQty').asstring);

          CDS_Main.post;
        end;
        Order_Main.Next;
      end;
      free;
    end;

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

procedure TfrmbpRk.Tv1Column3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmCPKWListSel := TfrmCPKWListSel.Create(Application);
    with frmCPKWListSel do
    begin
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('DRCKName').Value := Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
      end;
    end;
  finally
    frmCPKWListSel.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

end.

