unit U_CpCk;

interface

uses
  Windows, Messages, SysUtils, StrUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
  cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin, cxButtonEdit, cxDropDownEdit,
  DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, BtnEdit, cxContainer,
  cxTextEdit, 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
  TfrmCpCk = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    CRTime: TDateTimePicker;
    Label2: TLabel;
    CRType: TComboBox;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1P_SPEC: TcxGridDBColumn;
    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;
    ToCustName: TBtnEditA;
    v1MJID: TcxGridDBColumn;
    v1MJXH: TcxGridDBColumn;
    v1CPType: TcxGridDBColumn;
    ckName: TComboBox;
    Label4: TLabel;
    Label5: TLabel;
    orderNO: TBtnEditA;
    Label6: TLabel;
    PRTCodeName: TEdit;
    Label7: TLabel;
    PRTColor: TEdit;
    v1P_Code: TcxGridDBColumn;
    Label8: TLabel;
    InoutNo: TEdit;
    Label9: TLabel;
    custName: TBtnEditA;
    ToolButton3: TToolButton;
    Panel2: TPanel;
    Label10: TLabel;
    Edit1: TEdit;
    Label11: TLabel;
    IsYjx: TCheckBox;
    Label12: TLabel;
    v1Price: TcxGridDBColumn;
    v1Money: TcxGridDBColumn;
    YjxYG: TBtnEditA;
    Label13: TLabel;
    Yjxdate: TDateTimePicker;
    Label14: TLabel;
    PackFee: TcxCurrencyEdit;
    YbFee: TcxCurrencyEdit;
    Label15: TLabel;
    OtherFee: TcxCurrencyEdit;
    Label16: TLabel;
    Panel3: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    v2Column2: TcxGridDBColumn;
    ADO_Sub: TADOQuery;
    DataSource2: TDataSource;
    Label17: TLabel;
    QtyMX: TEdit;
    CDS_Sub: TClientDataSet;
    Label18: TLabel;
    XjNum: TcxCurrencyEdit;
    FHAddress: TBtnEditC;
    Label19: TLabel;
    Label20: TLabel;
    LXFS: TBtnEditC;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    IsTax: TCheckBox;
    Label21: TLabel;
    v2Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    ZsQtyMX: TEdit;
    Label22: TLabel;
    v1Column4: 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;
    TV3Column1: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    TV3Column2: TcxGridDBColumn;
    TV3Column3: TcxGridDBColumn;
    TV3Column4: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    ToolButton6: TToolButton;
    ToolButton7: 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 ToCustNameBtnClick(Sender: TObject);
    procedure orderNOBtnClick(Sender: TObject);
    procedure custNameBtnClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure Panel2DblClick(Sender: TObject);
    procedure v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure QtyMXKeyPress(Sender: TObject; var Key: Char);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure YjxYGBtnClick(Sender: TObject);
    procedure PackFeePropertiesChange(Sender: TObject);
    procedure FHAddressBtnUpClick(Sender: TObject);
    procedure LXFSBtnUpClick(Sender: TObject);
    procedure FHAddressBtnDnClick(Sender: TObject);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure TV2Column2PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    procedure InitGrid();
    procedure Initsub();
    procedure Initsub_1();
    function savedate(): Boolean;
    function savedate1(): Boolean;
    procedure SetComboBox();
    function YSData(): Boolean;
    function SKData(): Boolean;
    function YSData_Other(fYFName: string; fmoney: double): Boolean;
    procedure GetxjNum();
    { Private declarations }
  public
    fkeyNO, FCodeName, FCode, FDRCKNO, FDRCKName: string;
    FType: integer;
    fCKName: string;
    { Public declarations }
  end;

var
  frmCpCk: TfrmCpCk;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_Bpkclist, U_ZdyAttachment, U_ZDYHelpSel,
  U_ProductOrderNewListSEl, U_ContractSelList, U_CpkcSel, U_CPKWListSel,
  U_ZdyCMZ_Sel;
{$R *.dfm}

function TfrmCpCk.SKData(): 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, 'FK', 'YF_Money_CR', 4, 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('TOFactoryName').Value := '名豪';
      FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', CRTime.DateTime));
      FieldByName('YFType').Value := '自动生成';
      FieldByName('Qty').Value := 1;
      FieldByName('Price').Value := strtofloatdef(XjNum.Text, 0);
      FieldByName('Money').Value := strtofloatdef(XjNum.Text, 0);
      FieldByName('BBMoney').Value := strtofloatdef(XjNum.Text, 0);
      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('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 TfrmCpCk.GetxjNum();
begin
  if not varisnull(tv1.DataController.Summary.FooterSummaryValues[2]) then
  begin
    XjNum.Value := tv1.DataController.Summary.FooterSummaryValues[2] + strtofloatdef(PackFee.Text, 0) + strtofloatdef(YbFee.Text, 0) + strtofloatdef(OtherFee.Text, 0);
  end
  else
  begin
    XjNum.Value := strtofloatdef(PackFee.Text, 0) + strtofloatdef(YbFee.Text, 0) + strtofloatdef(OtherFee.Text, 0);
  end;
end;

procedure TfrmCpCk.Initsub();
begin
//    if CDS_Main.IsEmpty then
//    Exit;
//  try
//    with Ado_Sub do
//    begin
//      close;
//      sql.Clear;
//      sql.Add('exec P_Get_CP_CK_MX ''' + Cds_main.fieldbyname('P_CodeName').AsString + ''' ,''' + Cds_main.fieldbyname('P_Code').AsString + ''',''' + Cds_main.fieldbyname('CRNO').AsString + ''' ');
//      sql.Add(' ,''' + cds_main.fieldbyname('DRCKNO').AsString + ''' ,''' + cds_main.fieldbyname('DRCKName').AsString + ''' ');
//      open;
//    end;
//    SCreateCDS20(Ado_Sub, CDS_Sub);
//    SInitCDSData20(Ado_Sub, CDS_Sub);
//  finally
//  end;
//  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;

procedure TfrmCpCk.Initsub_1();
begin
  try
    with self.adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_Get_CP_CK_MX ''' + Cds_main.fieldbyname('P_CodeName').AsString + ''' ,''' + Cds_main.fieldbyname('P_Code').AsString + ''',''' + Cds_main.fieldbyname('CRNO').AsString + ''' ');
      sql.Add(' ,''' + cds_main.fieldbyname('DRCKNO').AsString + ''' ,''' + cds_main.fieldbyname('DRCKName').AsString + ''' ');
      open;
    end;
    SCreateCDS20(adoqueryTmp, CDS_Sub);
    SInitCDSData20(adoqueryTmp, CDS_Sub);
  finally
  end;
end;

function TfrmCpCk.YSData(): Boolean;
var
  CRID, YFID, priceUnit: string;
  PrtPrice, huilv: double;
begin
  Result := False;

  priceUnit := 'RMB';
  PrtPrice := 0;
  huilv := 1;
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from JYOrder_sub ');
    sql.Add('where subID=''' + Trim(CDS_Main.fieldbyname('subID').AsString) + '''');
    open;
  end;
  if not ADOQueryTmp.IsEmpty then
  begin
    priceUnit := ADOQueryTmp.fieldbyname('priceUnit').AsString;
    PrtPrice := ADOQueryTmp.fieldbyname('PrtPrice').AsFloat;
    huilv := ADOQueryTmp.fieldbyname('huilv').AsFloat;
  end;
  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('HuiLv').Value := huilv;
      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 := format('%.2f', [FieldByName('Qty').AsFloat * FieldByName('Price').AsFloat]);
      FieldByName('BBMoney').Value := format('%.2f', [FieldByName('Money').AsFloat * FieldByName('HuiLv').AsFloat]);
      FieldByName('ISTAX').Value := IsTax.Checked;
      FieldByName('BZType').Value := '人民币';
      FieldByName('QtyUnit').Value := Trim(CDS_Main.fieldbyname('QtyUnit').AsString);
      FieldByName('ComTaiTou').Value := Trim(custName.Text);
      FieldByName('YFName').Value := '产品销售';
      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;

  Result := True;
end;

function TfrmCpCk.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('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 TfrmCpCk.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=''CPCKTYPE''');
    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 TfrmCpCk.savedate(): Boolean;
var
  fkcID, XHNO, i: integer;
  Filler, FSize: string;
  FillTime: TdateTime;
  maxno: string;
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;

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

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from CP_INout');
      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 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('CKName').Value := trim(CKName.Text);
        ADOQueryCmd.FieldByName('InoutNO').Value := trim(InoutNO.Text);
        ADOQueryCmd.FieldByName('custNo').Value := trim(custName.TxtCode);
        ADOQueryCmd.FieldByName('custName').Value := trim(custName.Text);
       // ADOQueryCmd.FieldByName('TocustNo').Value:=trim(TocustName.TxtCode);
       // ADOQueryCmd.FieldByName('TocustName').Value:=trim(TocustName.Text);
        ADOQueryCmd.FieldByName('FHAddress').Value := trim(FHAddress.Text);
        ADOQueryCmd.FieldByName('LXFS').Value := trim(LXFS.Text);

        if IsYjx.Checked then
        begin
          ADOQueryCmd.FieldByName('IsYjx').Value := '是';
          ADOQueryCmd.FieldByName('xjNum').Value := strtofloatdef(xjNum.text, 0);
          ADOQueryCmd.FieldByName('Yjxdate').Value := formatdateTime('yyyy-MM-dd', Yjxdate.Date);
        end
        else
          ADOQueryCmd.FieldByName('IsYjx').Value := '否';
        ADOQueryCmd.FieldByName('YjxYG').Value := trim(YjxYG.Text);
        ADOQueryCmd.FieldByName('ISTAX').Value := IsTax.Checked;
    //    ADOQueryCmd.FieldByName('Yjxdate').Value:=formatdateTime('yyyy-MM-dd',Yjxdate.Date);
        ADOQueryCmd.FieldByName('PackFee').Value := floattostr(strtofloatdef(PackFee.Text, 0));
        ADOQueryCmd.FieldByName('YbFee').Value := floattostr(strtofloatdef(YbFee.Text, 0));
        ADOQueryCmd.FieldByName('otherFee').Value := floattostr(strtofloatdef(otherFee.Text, 0));
        ADOQueryCmd.FieldByName('mainID').Value := trim(CDS_Main.fieldbyname('mainID').AsString);
        ADOQueryCmd.FieldByName('subID').Value := trim(CDS_Main.fieldbyname('subID').AsString);
        ADOQueryCmd.FieldByName('CRTime').Value := formatdateTime('yyyy-MM-dd', CRTIme.Date);
        ADOQueryCmd.FieldByName('CRType').Value := trim(CRType.Text);
        ADOQueryCmd.FieldByName('CRFlag').Value := '出库';
        ADOQueryCmd.FieldByName('CPTYPE').Value := '正品';
        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('P_ColorNO').Value := trim(fieldbyname('P_ColorNO').AsString);
        ADOQueryCmd.FieldByName('defstr1').Value := trim(fieldbyname('defstr1').AsString);
        ADOQueryCmd.FieldByName('P_MF').Value := fieldbyname('P_MF').AsFloat;
        ADOQueryCmd.FieldByName('P_KZ').Value := fieldbyname('P_KZ').AsFloat;

        ADOQueryCmd.FieldByName('Qty').Value := CDS_Main.fieldbyname('Qty').AsFloat;
        ADOQueryCmd.FieldByName('Price').Value := CDS_Main.fieldbyname('price').AsFloat;
        ADOQueryCmd.FieldByName('money').Value := CDS_Main.fieldbyname('money').AsFloat;
        ADOQueryCmd.FieldByName('rkPrice').Value := CDS_Main.fieldbyname('rkPrice').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);

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

//                ADOQueryCmd.FieldByName('kcID').Value := FieldByName('kcID').AsInteger;
        ADOQueryCmd.Post;
        XHNO := XHNO + 1;
        Next;
      end;

    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    result := true;
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

function TfrmCpCk.savedate1(): Boolean;
var
  fkcID, XHNO, i: integer;
  Filler, FSize: string;
  FillTime: TdateTime;
  maxno: string;
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;

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


{    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update CP_kc set RollNum=A.RollNum + B.RollNum, Qty=A.Qty + B.Qty ');
      sql.Add('from  CP_kc  A');
      sql.Add('inner join CP_INout B on B.kcid=A.kcid');
      sql.Add('where B.CRNO='+quotedstr(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;
    CDS_Main.DisableControls;
    CDS_Main.First;
    XHNO := 1;
    with CDS_Main do
    begin
      while not eof do
      begin
        Edit;
        FieldByName('CRNO').Value := trim(fkeyNO);
//        FieldByName('CRID').Value := recNo;
        post;

     {   IF trim(CRType.Text)='销售出库'then
        begin
          with ADOQueryTmp do
          begin
            close;
            sql.Clear;
            sql.Add('select sum(RollNum) as RollNum,sum(Qty) as Qty from CP_InOut_MX');
            sql.Add('where MJID='+quotedstr(trim(CDS_main.fieldbyname('MJID').AsString)));
            open;
          end;
          IF ADOQueryTmp.IsEmpty then
          begin
            CDS_Main.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            application.MessageBox('销售出库请输入明细码单数据！','提示信息',0);
            exit;
          end
          else
          begin
            IF ADOQueryTmp.fieldbyname('RollNum').AsFloat<> CDS_main.fieldbyname('RollNum').AsFloat then
            begin
              CDS_Main.EnableControls;
              ADOQueryCmd.Connection.RollbackTrans;
              application.MessageBox('输入明细码单的匹数和单挑数据的匹数不一致！','提示信息',0);
              exit;
            end;
            IF ADOQueryTmp.fieldbyname('Qty').AsFloat<> CDS_main.fieldbyname('Qty').AsFloat then
            begin
              CDS_Main.EnableControls;
              ADOQueryCmd.Connection.RollbackTrans;
              application.MessageBox('输入明细码单的数量和单挑数据的数量不一致！','提示信息',0);
              exit;
            end;
          end;
        end;
          }

        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('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('CKName').Value := trim(CKName.Text);
                ADOQueryCmd.FieldByName('InoutNO').Value := trim(InoutNO.Text);
                ADOQueryCmd.FieldByName('custNo').Value := trim(custName.TxtCode);
                ADOQueryCmd.FieldByName('custName').Value := trim(custName.Text);
       // ADOQueryCmd.FieldByName('TocustNo').Value:=trim(TocustName.TxtCode);
       // ADOQueryCmd.FieldByName('TocustName').Value:=trim(TocustName.Text);
                ADOQueryCmd.FieldByName('FHAddress').Value := trim(FHAddress.Text);
                ADOQueryCmd.FieldByName('LXFS').Value := trim(LXFS.Text);

                if IsYjx.Checked then
                begin
                  ADOQueryCmd.FieldByName('IsYjx').Value := '是';
                  ADOQueryCmd.FieldByName('xjNum').Value := strtofloatdef(xjNum.text, 0);
                  ADOQueryCmd.FieldByName('Yjxdate').Value := formatdateTime('yyyy-MM-dd', Yjxdate.Date);
                end
                else
                  ADOQueryCmd.FieldByName('IsYjx').Value := '否';
                ADOQueryCmd.FieldByName('YjxYG').Value := trim(YjxYG.Text);
                ADOQueryCmd.FieldByName('ISTAX').Value := IsTax.Checked;
    //    ADOQueryCmd.FieldByName('Yjxdate').Value:=formatdateTime('yyyy-MM-dd',Yjxdate.Date);
                ADOQueryCmd.FieldByName('PackFee').Value := floattostr(strtofloatdef(PackFee.Text, 0));
                ADOQueryCmd.FieldByName('YbFee').Value := floattostr(strtofloatdef(YbFee.Text, 0));
                ADOQueryCmd.FieldByName('otherFee').Value := floattostr(strtofloatdef(otherFee.Text, 0));
                ADOQueryCmd.FieldByName('mainID').Value := trim(CDS_Main.fieldbyname('mainID').AsString);
                ADOQueryCmd.FieldByName('subID').Value := trim(CDS_Main.fieldbyname('subID').AsString);
                ADOQueryCmd.FieldByName('CRTime').Value := formatdateTime('yyyy-MM-dd', CRTIme.Date);
//                ADOQueryCmd.FieldByName('TocustName').Value := trim(CDS_Main.fieldbyname('TocustName').AsString);
//                ADOQueryCmd.FieldByName('batchNo').Value := trim(CDS_Main.fieldbyname('batchNo').AsString);
                ADOQueryCmd.FieldByName('CRType').Value := trim(CRType.Text);
                ADOQueryCmd.FieldByName('CRFlag').Value := '出库';
//                ADOQueryCmd.FieldByName('MJID').Value := trim(fieldbyname('MJID').AsString);
//                ADOQueryCmd.FieldByName('MJXH').Value := trim(fieldbyname('MJXH').AsString);
                ADOQueryCmd.FieldByName('CPTYPE').Value := '正品';
                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_Sub.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('P_ColorNO').Value := trim(fieldbyname('P_ColorNO').AsString);
//                ADOQueryCmd.FieldByName('defstr1').Value := trim(fieldbyname('defstr1').AsString);
//                ADOQueryCmd.FieldByName('P_MF').Value := fieldbyname('P_MF').AsFloat;
//                ADOQueryCmd.FieldByName('P_KZ').Value := fieldbyname('P_KZ').AsFloat;
//                ADOQueryCmd.FieldByName('RollNum').Value := fieldbyname('RollNum').AsFloat;
                ADOQueryCmd.FieldByName('Qty').Value := CDS_sub.fieldbyname(FSize).AsFloat;
//                ADOQueryCmd.FieldByName('zsQty').Value := fieldbyname('zsQty').AsFloat;
//                ADOQueryCmd.FieldByName('QtyUnit').Value := trim(fieldbyname('QtyUnit').AsString);
                ADOQueryCmd.FieldByName('Price').Value := CDS_Main.fieldbyname('price').AsFloat;
                ADOQueryCmd.FieldByName('money').Value := CDS_Main.fieldbyname('money').AsFloat;
                ADOQueryCmd.FieldByName('rkPrice').Value := CDS_Main.fieldbyname('rkPrice').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('FirstNo').Value:=trim(FirstName.TxtCode);
   //     ADOQueryCmd.FieldByName('FirstName').Value:=trim(FirstName.Text);

                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 := FieldByName('kcID').AsInteger;
                ADOQueryCmd.Post;

                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,'''') ');
                  sql.Add(',isnull(P_Color,'''') as P_Color,defstr1,isnull(P_MF,'''') as P_MF,isnull(P_KZ,'''') as P_KZ,RKprice, ');
                  sql.Add('SUM(CRFlagRate * RollNum ) as RollNum,');
                  sql.Add('SUM(CRFlagRate * (Qty+zsQty)) as Qty,');
                  sql.Add('CAST( P_KZ as varchar(10)) as P_KZ_s,');
                  sql.Add(' SUM(CRFlagRate * (Qty+zsQty))*RKprice as money ');
                  sql.Add(',ISNULL(OrderNO,'''')  as OrderNO');
                  sql.Add(',isnull(DRCKNO ,'''') as DRCKNO	,isnull(DRCKName,'''') as DRCKName');
                  sql.Add('from( ');
                  sql.Add('select  RKprice=(select top 1  RKprice from CP_inout D  where  D.P_code=A.P_code order by D.RKprice desc),  ');
                  SQL.Add('A.mainid,Subid,P_Code,P_CodeName,P_spec,P_MF,P_KZ,DRCKNO,DRCKName,RollNum,Qty,zsQty,P_color,DEFstr1, ');
                  sql.Add('case when CRFlag=''入库'' then 1 else -1 end as CRFlagRate ');
                  sql.Add(',Orderno  ');
                  sql.Add('from CP_inout A');
                  sql.Add('left join JYOrder_main B on A.maiNID=B.Mainid  ');
                  sql.Add('where a.Valid=''Y'' ');
                  sql.Add('and isnull(A.p_Codename,'''')=''' + CDS_Main.fieldbyname('p_Codename').AsString + ''' ');
                  sql.Add('and isnull(A.P_Code,'''')=''' + CDS_Main.fieldbyname('P_Code').AsString + ''' ');
                  sql.Add('and isnull(A.DRCKNO,'''')=''' + CDS_Main.fieldbyname('DRCKNO').AsString + ''' ');
                  sql.Add('and isnull(A.DRCKName,'''')=''' + CDS_Main.fieldbyname('DRCKName').AsString + ''' ');
                  sql.Add('and  isnull(A.P_color,'''')=''' + CDS_Sub.fieldbyname('P_Color').AsString + ''' ');
                  sql.Add('and isnull(defstr1,'''')=(select defstr1 from CP_inout X where X.CRNO=''' + trim(fkeyNO) + ''' and X.CRID=''' + IntToStr(XHNO) + ''')');
                  sql.Add(' )AA');
                  sql.Add(' group by ISNULL(mainID,''''),ISNULL(subID,''''),isnull(P_Code,''''),isnull(P_CodeName,''''),ISNULL(P_spec,''''),isnull(P_MF,''''),isnull(P_KZ,''''),RKprice,ISNULL(OrderNO,'''') ,CAST( P_KZ as varchar(10)) ');
                  sql.Add(',isnull(DRCKNO ,'''')	,isnull(DRCKName,''''),isnull(P_Color,'''') ,defstr1 ');
//                  showmessage(sql.Text);
                  open;
                end;

                if ((ADOQueryTmp.FieldByName('Qty').AsFloat) < 0) or (ADOQueryTmp.IsEmpty = True) then
                begin
//                  application.MessageBox('出库数量不能大于库存数量！', '提示信息', 0);
                  Application.MessageBox(PChar(CDS_Sub.fieldbyname('P_Color').AsString + '出库数量不能大于库存数量'), '提示', 0);
                  CDS_Main.EnableControls;
                  ADOQueryCmd.Connection.RollbackTrans;
                  exit;
                end;

                XHNO := XHNO + 1;
              end;

              i := i + 1;
            end;

            Next;
          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;
//        if strtofloatdef(YbFee.Text, 0) <> 0 then
//        begin
//          if not YSData_other('样板费', strtofloatdef(YbFee.Text, 0)) then
//          begin
//            ADOQueryCmd.Connection.RollbackTrans;
//            exit;
//          end;
//        end;
//        if strtofloatdef(OtherFee.Text, 0) <> 0 then
//        begin
//          if not YSData_other('其它费', strtofloatdef(otherFee.Text, 0)) then
//          begin
//            ADOQueryCmd.Connection.RollbackTrans;
//            exit;
//          end;
//        end;
//        if IsYjx.Checked then
//        begin
//          if not SKData() 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 TfrmCpCk.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,');
      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,FHAddress,LXFS,TocustName');
      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,'''') ,CAST( P_KZ as varchar(10)),isnull(P_Color,'''')');
      sql.Add(',isnull(DRCKNO ,'''')	,isnull(DRCKName,''''),CRNO,InOutNo,CRTime,isnull(custName,''''),CRType,CKName,isnull(custNo,''''),CPType');
      sql.Add(',isnull(Note,''''),isnull(Price,0),FHAddress,LXFS,TocustName');
//      ShowMessage(SQL.text);
      open;
    end;
    SCreateCDS20(adoqueryTmp, CDS_Main);
    SInitCDSData20(adoqueryTmp, CDS_Main);    }
    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,FHAddress,LXFS');
      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),FHAddress,LXFS');
      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 := trim(CDS_Main.fieldbyname('InoutNO').AsString);
      custName.TxtCode := trim(CDS_Main.fieldbyname('custNo').AsString);
      custName.Text := trim(CDS_Main.fieldbyname('custName').AsString);
      FHAddress.Text := trim(CDS_Main.fieldbyname('FHAddress').AsString);
      LXFS.Text := trim(CDS_Main.fieldbyname('LXFS').AsString);
    //  orderNo.Text:=trim(CDS_Main.fieldbyname('orderNo').AsString);
    //  PRTCodeName.Text:=trim(CDS_Main.fieldbyname('PRTCodeName').AsString);
     // PRTColor.Text:=trim(CDS_Main.fieldbyname('PRTColor').AsString);
//      TocustName.TxtCode := trim(CDS_Main.fieldbyname('TocustNo').AsString);
//      TocustName.Text := trim(CDS_Main.fieldbyname('TocustName').AsString);
//      IStax.Checked := CDS_Main.FieldByName('IsTax').AsBoolean;
//      if CDS_Main.fieldbyname('IsYjx').AsString = '是' then
//      begin
//        IsYjx.Checked := true;
//        Yjxdate.Date := CDS_Main.FieldByName('Yjxdate').AsDateTime;
//      end
//      else
//        IsYjx.Checked := false;
//      YjxYG.Text := trim(CDS_Main.FieldByName('YjxYG').AsString);
//      PackFee.Value := CDS_Main.FieldByName('PackFee').AsFloat;
//      YbFee.Value := CDS_Main.FieldByName('YbFee').AsFloat;
//      otherFee.Value := CDS_Main.FieldByName('otherFee').AsFloat;
//      XjNum.Value := CDS_Main.FieldByName('XjNum').AsFloat;
//      ToolButton1.Click;
      Initsub_1();
    end
    else
//      ToolButton1.Click;




  finally
  end;
  Initsub();
end;

procedure TfrmCpCk.FormDestroy(Sender: TObject);
begin
  frmCpCk := nil;
end;

procedure TfrmCpCk.FormCreate(Sender: TObject);
begin
  CRTime.Date := DServerDate;
  panel3.Align := alclient;
  Yjxdate.Date := DServerDate;
end;

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

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

procedure TfrmCpCk.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(CRType.Text) = '销售出库' then
  begin
    if trim(custName.Text) = '' then
    begin
      application.MessageBox('客户不能为空！', '提示信息', 0);
      exit;
    end;
  end;

  if IsYjx.Checked then
  begin
    if trim(YjxYG.Text) = '' then
    begin
      application.MessageBox('收款人不能为空！', '提示信息', 0);
      exit;
    end;
  end;
  if (trim(CRType.Text) = '样品零售') and (trim(orderNO.Text) = '') then
  begin
    application.MessageBox('订单信息不能为空！', '提示信息', 0);
    exit;
  end;

//  if cds_main.Locate('QtyUnit','',[])  then
//  begin
//    application.MessageBox('数量单位不能为空！','提示信息',0);
//    exit;
//  end;
//  if cds_main.Locate('QtyUnit',NULL,[])  then
//  begin
//    application.MessageBox('数量单位不能为空！','提示信息',0);
//    exit;
//  end;

  if savedate() then
  begin
    application.MessageBox('数据保存成功！', '提示信息');
    ModalResult := 1;
  end
  else
  begin
    application.MessageBox('数据保存失败！', '提示信息', MB_ICONERROR);
  end;
end;

procedure TfrmCpCk.v1P_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'Cloth';
      flagname := '坯布名称';
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('P_CodeName').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_Main.FieldByName('P_Code').Value := Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCpCk.ToolButton1Click(Sender: TObject);
var
  FMJID: string;
begin
  frmCpkcSel := TfrmCpkcSel.create(self);
  with frmCpkcSel do
  begin
    fType := 10;
    fckName := self.fckName;
    if showmodal = 1 then
    begin
      cds_main.DisableControls;
      cds_main.First;
      while not cds_main.Eof do
      begin
        if (cds_main.fieldbyname('ssel').AsBoolean) then
        begin
//          if GetLSNo(self.ADOQueryCmd, FMJID, 'MJ', 'CP_InOut_MX', 3, 1) = False then
//          begin
//      //  ADOQueryCmd.Connection.RollbackTrans;
//            Application.MessageBox('取最大号失败!', '提示', 0);
//            Exit;
//          end;
          self.cds_main.Append;
          self.cds_main.FieldByName('orderNo').Value := trim(cds_main.fieldbyname('orderNo').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('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('Qty').Value := cds_main.fieldbyname('Qty').AsFloat;
          self.cds_main.FieldByName('P_Color').Value := trim(cds_main.fieldbyname('P_Color').AsString);
          self.cds_main.FieldByName('defstr1').Value := cds_main.fieldbyname('defstr1').AsString;
          self.cds_main.FieldByName('price').Value := cds_main.fieldbyname('rkprice').AsFloat;
          self.cds_main.FieldByName('rkprice').Value := cds_main.fieldbyname('rkprice').AsFloat;
          self.cds_main.FieldByName('money').Value := cds_main.fieldbyname('money').AsFloat;
          self.cds_main.FieldByName('DRCKNO').Value := trim(cds_main.fieldbyname('DRCKNO').AsString);
          self.cds_main.FieldByName('DRCKName').Value := trim(cds_main.fieldbyname('DRCKName').AsString);
          self.cds_main.Post;

        end;
        cds_main.Next;
      end;
      cds_main.EnableControls;
    end;
    free;
  end;
end;

procedure TfrmCpCk.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  CDS_Main.Delete;
  getxjNum();
end;

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

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

procedure TfrmCpCk.orderNOBtnClick(Sender: TObject);
begin
 {  frmProductOrderNewList:=TfrmProductOrderNewList.Create(Application);
   with  frmProductOrderNewList do
   begin
     DParameters1:='高权限';
     if showmodal=1 then
     begin
       self.fmainID:=trim(Order_Main.fieldbyname('MainID').AsString);
       self.fsubID:=trim(Order_Main.fieldbyname('subID').AsString);
       self.orderNo.Text:=trim(Order_Main.fieldbyname('orderNo').AsString);
       self.PRTCodeName.Text:=trim(Order_Main.fieldbyname('PRTCodeName').AsString);
       self.PRTColor.Text:=trim(Order_Main.fieldbyname('PRTColor').AsString);
     end;
     free;
   end;  }
end;

procedure TfrmCpCk.custNameBtnClick(Sender: TObject);
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);
        FHAddress.Text := Trim(CDS_HZ.fieldbyname('DEFNote2').AsString);
        LXFS.Text := Trim(CDS_HZ.fieldbyname('DEFstr1').AsString) + ' ' + Trim(CDS_HZ.fieldbyname('DEFstr2').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmCpCk.ToolButton3Click(Sender: TObject);
begin
  Panel2.Visible := true;
end;

procedure TfrmCpCk.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if CDS_Main.IsEmpty then
      exit;
    if trim(edit1.Text) = '' then
      exit;
    if CDS_Main.Locate('MJID', trim(edit1.Text), []) then
    begin
      CDS_Main.Delete;
      Label11.Caption := '条码:' + trim(edit1.Text) + '删除成功！';
    end
    else
    begin
      Label11.Caption := '条码:' + trim(edit1.Text) + '删除失败！';
    end;

    edit1.Text := '';
  end;
end;

procedure TfrmCpCk.Panel2DblClick(Sender: TObject);
begin
  Panel2.Visible := false;
end;

procedure TfrmCpCk.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();
  GetxjNum();
end;

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

procedure TfrmCpCk.QtyMXKeyPress(Sender: TObject; var Key: Char);
var
  fMJXH: string;
  fRollNum: integer;
  fQty, fzsQty: 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) - strtofloatdef(zsQtyMX.Text, 0);
        Fieldbyname('zsQty').Value := strtofloatdef(zsQtyMX.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) - strtofloatdef(zsQtyMX.Text, 0);
        Fieldbyname('zsQty').Value := strtofloatdef(zsQtyMX.Text, 0);
        post;
      end;
      if varisnull(tv2.DataController.Summary.FooterSummaryValues[0]) then
      begin
        fRollNum := 0;
        fQty := 0;
        fzsQty := 0;
      end
      else
      begin
        fRollNum := CDS_Sub.RecordCount;
        fQty := tv2.DataController.Summary.FooterSummaryValues[0];
        fzsQty := tv2.DataController.Summary.FooterSummaryValues[1];
      end;
      with CDS_Main do
      begin
        EDIT;
        FieldByName('RollNum').Value := fRollNum;
        Fieldbyname('Qty').Value := fQty;
        Fieldbyname('zsQty').Value := fzsQty;
        FieldbyName('Money').Value := format('%.2f', [FieldByName('Qty').AsFloat * FieldByName('Price').AsFloat]);
        post;
      end;
      QtyMX.Text := '';
   //   zsQtyMX.Text:='';
      QtyMX.SetFocus;
    except
    end;
  end;
end;

procedure TfrmCpCk.Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
var
  FROLLnUM: INTEGER;
  FQTY: double;
  fzsQty: double;
begin
  if CDS_Sub.IsEmpty then
    exit;
  if Application.MessageBox('确定要删除这匹数据吗？', '提示', 32 + 4) <> IDYES 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)));

      sql.Add('update CP_InOut SET RollNum=(select count(MJXH) from CP_InOut_MX X where X.MJID=CP_InOut.MJID) ');
      sql.Add(',Qty=(select sum(Qty) from CP_InOut_MX X where X.MJID=CP_InOut.MJID) ');
      sql.Add(',ZsQty=(select sum(ZsQty) from CP_InOut_MX X where X.MJID=CP_InOut.MJID) ');
      sql.Add('where MJID=' + quotedstr(trim(CDS_Sub.fieldbyname('MJID').AsString)));
      sql.Add('and CRFLag=''出库'' ');
      execsql;
    end;
    Initsub();
    if varisnull(tv2.DataController.Summary.FooterSummaryValues[0]) then
    begin
      FROLLnUM := 0;
      FQTY := 0;
      fzsQty := 0;
    end
    else
    begin
      FROLLnUM := CDS_Sub.RecordCount;
      FQTY := tv2.DataController.Summary.FooterSummaryValues[0];
      fzsQty := tv2.DataController.Summary.FooterSummaryValues[1];
    end;
    with CDS_Main do
    begin
      EDIT;
      FieldByName('RollNum').Value := FROLLnUM;
      Fieldbyname('Qty').Value := FQTY;
      Fieldbyname('ZsQty').Value := fzsQty;
      FieldbyName('Money').Value := format('%.2f', [FieldByName('Qty').AsFloat * FieldByName('Price').AsFloat]);
      post;
    end;
  except
    application.MessageBox('删除失败', '提示信息', 0)
  end;
end;

procedure TfrmCpCk.YjxYGBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'YjxYGFlag';
      flagname := '收款人名称';
      if ShowModal = 1 then
      begin
        YjxYG.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCpCk.PackFeePropertiesChange(Sender: TObject);
begin
  GetxjNum();
end;

procedure TfrmCpCk.FHAddressBtnUpClick(Sender: TObject);
begin
  if Trim(custName.Text) = '' then
  begin
    Application.MessageBox('客户不能为空!', '提示', 0);
    Exit;
  end;
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := 'FHAddress';
      flagname := '发货地址';
      MainType := Trim(custName.Text);
      if ShowModal = 1 then
      begin
        FHAddress.Text := Trim(ReturnStr);
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmCpCk.LXFSBtnUpClick(Sender: TObject);
begin
  if Trim(custName.Text) = '' then
  begin
    Application.MessageBox('客户不能为空!', '提示', 0);
    Exit;
  end;
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := 'LXFS';
      flagname := '联系方式';
      MainType := Trim(custName.Text);
      if ShowModal = 1 then
      begin
        LXFS.Text := Trim(ReturnStr);
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmCpCk.FHAddressBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmCpCk.Tv2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  FROLLnUM: INTEGER;
  FQTY: double;
  fzsQty: double;
begin
  if Button = mbRight then
  begin
    if CDS_Sub.IsEmpty then
      exit;
    if Application.MessageBox('确定要删除这匹数据吗？', '提示', 32 + 4) <> IDYES 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)));

        sql.Add('update CP_InOut SET RollNum=(select count(MJXH) from CP_InOut_MX X where X.MJID=CP_InOut.MJID) ');
        sql.Add(',Qty=(select sum(Qty) from CP_InOut_MX X where X.MJID=CP_InOut.MJID) ');
        sql.Add(',zsQty=(select sum(zsQty) from CP_InOut_MX X where X.MJID=CP_InOut.MJID) ');
        sql.Add('where MJID=' + quotedstr(trim(CDS_Sub.fieldbyname('MJID').AsString)));
        sql.Add('and CRFlag=''出库''');

        execsql;
      end;
      Initsub();
      if varisnull(tv2.DataController.Summary.FooterSummaryValues[0]) then
      begin
        FROLLnUM := 0;
        FQTY := 0;
        fzsQty := 0;
      end
      else
      begin
        FROLLnUM := CDS_Sub.RecordCount;
        FQTY := tv2.DataController.Summary.FooterSummaryValues[0];
        fzsQty := tv2.DataController.Summary.FooterSummaryValues[1];
      end;
      with CDS_Main do
      begin
        EDIT;
        FieldByName('RollNum').Value := FROLLnUM;
        Fieldbyname('Qty').Value := FQTY;
        Fieldbyname('zsQty').Value := fzsQty;
        FieldbyName('Money').Value := format('%.3f', [FieldByName('Qty').AsFloat * FieldByName('Price').AsFloat]);
        post;
      end;
    except
      application.MessageBox('删除失败', '提示信息', 0)
    end;
  end;
end;

procedure TfrmCpCk.ToolButton4Click(Sender: TObject);
begin
  with CDS_Sub do
  begin
    append;
    post;
  end;
end;

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

procedure TfrmCpCk.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 TfrmCpCk.ToolButton6Click(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 TfrmCpCk.ToolButton7Click(Sender: TObject);
begin
  frmContractSelList := TfrmContractSelList.create(self);
  with frmContractSelList 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('ConNo').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.FieldByName('Price').Value := trim(order_main.fieldbyname('PRTPrice').asstring);
          CDS_Main.FieldByName('Money').Value := (order_main.fieldbyname('PRTOrderQty').asfloat) * (order_main.fieldbyname('PRTPrice').asfloat);

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

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

procedure TfrmCpCk.Tv1Column1PropertiesButtonClick(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('DRCKNo').Value := Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
      end;
    end;
  finally
    frmCPKWListSel.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCpCk.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try                                                       
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'CPRKTYPE';
      flagname := '仓库名称';
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('DRCKName').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
//        Self.CDS_Main.FieldByName('P_Code').Value := Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCpCk.v1Column1PropertiesButtonClick(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;

end.

