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,
  RM_Common, RM_Class, RM_e_Xls, RM_GridReport, RM_System, RM_Dataset,
  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;
    v1P_MF: TcxGridDBColumn;
    v1P_KZ: TcxGridDBColumn;
    v1RollNum: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    ckName: TComboBox;
    v1CPType: TcxGridDBColumn;
    v1P_Color: TcxGridDBColumn;
    note: TEdit;
    Label4: TLabel;
    cust: TBtnEditA;
    Label9: TLabel;
    ADOQuery1: TADOQuery;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Edit1: TEdit;
    Label5: TLabel;
    Label6: TLabel;
    Edit2: TEdit;
    TBPrint: TToolButton;
    ADOPrint: TADOQuery;
    RMDB_MD: TRMDBDataSet;
    RMGridReport1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    custaddress: TEdit;
    Label7: TLabel;
    factoryName: TBtnEditA;
    Label8: TLabel;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    ADOQuery2: TADOQuery;
    Tv1Column3: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1P_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure v1P_ColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure custBtnClick(Sender: TObject);
    procedure v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure Edit2KeyPress(Sender: TObject; var Key: Char);
    procedure TBPrintClick(Sender: TObject);
    procedure factoryNameBtnClick(Sender: TObject);
    procedure v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1QtyUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    procedure InitGrid();
    function savedate(): Boolean;
    procedure SetComboBox();
    function YSData_gd(ckNo, CkID: string; Order_Main10: TClientDataSet): Boolean;
    function YSData_YP(ckNo, CkID: string; Order_Main10: TClientDataSet): Boolean;
    function YFData(): Boolean;
    { Private declarations }
  public
    fkeyNO: string;
    FType: integer;
    fCKName: string;
    { Public declarations }
  end;

var
  frmbpRk: TfrmbpRk;

implementation

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

function TfrmbpRk.YFData(): Boolean;
var
  CRID, YFID, Price, PriceUnit, OrderUnit, FComTaiTou: string;
begin
  Result := False;
  with ADOQueryTmp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName=''' + Trim(cust.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;
  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, 'CF', 'YF_Money_CR', 3, 1) = False then
    begin
      Application.MessageBox('取应收最大号失败!', '提示', 0);
      Exit;
    end;
  end
  else
  begin
    YFID := Trim(ADOQueryTmp.fieldbyname('YFID').AsString);
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where YFID=''' + trim(YFID) + '''');
    Open;
  end;

  with ADOQueryCmd do
  begin
    if IsEmpty then
      Append
    else
      edit;
    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(cust.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.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;

  Result := True;
end;

function TfrmbpRk.YSData_gd(ckNo, CkID: string; Order_Main10: TClientDataSet): Boolean;
var
  CRID, YFID, Price, PriceUnit, OrderUnit, FComTaiTou: string;
begin
  Result := False;
  with Order_Main10 do
  begin
    with ADOQuery1 do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where FactoryName=''' + Trim(cust.text) + '''');
      sql.Add('and YFDefFlag1=1 ');
      Open;
    end;
    if not ADOQuery1.IsEmpty then
    begin
      CRID := ADOQuery1.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(cust.text);
        FieldByName('ZdyStr1').Value := '应收收';
        FieldByName('YFDefFlag1').Value := 1;
        Post;
      end;
    end;

    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('delete  from YF_Money_CR ');
      sql.Add(' where MainID=''' + Trim(ckNo) + '''');
      sql.Add(' and subID=''' + Trim(CkID) + '''');
      sql.Add(' and YFName=''广东销售退货'' ');
      execsql;
    end;
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR ');
      sql.Add(' where MainID=''' + Trim(ckNo) + '''');
      sql.Add(' and subID=''' + Trim(CkID) + '''');
      sql.Add(' and YFName=''广东销售退货'' ');
      Open;
    end;
    if ADOQuery1.IsEmpty then
    begin
      if GetLSNo(ADOQueryCmd, YFID, 'TH', '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(ckNo);
        FieldByName('CRID').Value := StrToInt(CRID);
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('CRType').Value := '应收款登记';
        FieldByName('CRFlag').Value := '应收收';
        FieldByName('QtyFlag').Value := 1;
        FieldByName('FactoryName').Value := Trim(cust.Text);
        FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', CRTime.DateTime));
        FieldByName('YFType').Value := '自动生成';
        FieldByName('ps').Value := -1 * Order_Main10.fieldbyname('RollNum').AsFloat;
        FieldByName('Qty').Value := -1 * Order_Main10.fieldbyname('Qty').AsFloat;
        FieldByName('Price').Value := Order_Main10.fieldbyname('Price').AsFloat;
        FieldByName('Money').Value := -1 * Order_Main10.fieldbyname('Money').AsFloat;
        FieldByName('BBMoney').Value := -1 * Order_Main10.fieldbyname('Money').AsFloat;
          //FieldByName('HuiLv').Value:=1;
        FieldByName('HuiLv').Value := 1;
        FieldByName('BZType').Value := '人民币';
        FieldByName('QtyUnit').Value := Trim(Order_Main10.fieldbyname('QtyUnit').AsString);
        FieldByName('ComTaiTou').Value := Trim(cust.Text);
        FieldByName('P_CodeName').Value := trim(Order_Main10.fieldbyname('P_CodeName').AsString);
        FieldByName('P_Color').Value := trim(Order_Main10.fieldbyname('P_Color').AsString);
        FieldByName('YFName').Value := '广东销售退货';
        FieldByName('MainId').Value := Trim(ckNo);
        FieldByName('subID').Value := Trim(CkID);
        FieldByName('YFDefFlag1').Value := 1;
        FieldByName('syrName').Value := '广东门市部';
        Post;
      end;
    end
    else
    begin
      YFID := Trim(ADOQuery1.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;
  end;
  Result := True;
end;

function TfrmbpRk.YSData_YP(ckNo, CkID: string; Order_Main10: TClientDataSet): Boolean;
var
  CRID, YFID, Price, PriceUnit, OrderUnit, FComTaiTou: string;
begin
  Result := False;
  with Order_Main10 do
  begin
    with ADOQuery1 do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where FactoryName=''' + Trim(cust.text) + '''');
      sql.Add('and YFDefFlag1=0 ');
      Open;
    end;
    if not ADOQuery1.IsEmpty then
    begin
      CRID := ADOQuery1.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(cust.text);
        FieldByName('ZdyStr1').Value := '应收收';
        FieldByName('YFDefFlag1').Value := 0;
        Post;
      end;
    end;

    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('delete  from YF_Money_CR ');
      sql.Add(' where MainID=''' + Trim(ckNo) + '''');
      sql.Add(' and subID=''' + Trim(CkID) + '''');
      sql.Add(' and YFName=''样品退货'' ');
      execsql;
    end;
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR ');
      sql.Add(' where MainID=''' + Trim(ckNo) + '''');
      sql.Add(' and subID=''' + Trim(CkID) + '''');
      sql.Add(' and YFName=''样品退货'' ');
      Open;
    end;
    if ADOQuery1.IsEmpty then
    begin
      if GetLSNo(ADOQueryCmd, YFID, 'TH', '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(ckNo);
        FieldByName('CRID').Value := StrToInt(CRID);
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('CRType').Value := '应收款登记';
        FieldByName('CRFlag').Value := '应收收';
        FieldByName('QtyFlag').Value := 1;
        FieldByName('FactoryName').Value := Trim(cust.Text);
        FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', CRTime.DateTime));
        FieldByName('YFType').Value := '自动生成';
        FieldByName('ps').Value := -1 * Order_Main10.fieldbyname('RollNum').AsFloat;
        FieldByName('Qty').Value := -1 * Order_Main10.fieldbyname('Qty').AsFloat;
        FieldByName('Price').Value := Order_Main10.fieldbyname('Price').AsFloat;
        FieldByName('Money').Value := -1 * Order_Main10.fieldbyname('Money').AsFloat;
        FieldByName('BBMoney').Value := -1 * Order_Main10.fieldbyname('Money').AsFloat;
          //FieldByName('HuiLv').Value:=1;
        FieldByName('HuiLv').Value := 1;
        FieldByName('BZType').Value := '人民币';
        FieldByName('QtyUnit').Value := Trim(Order_Main10.fieldbyname('QtyUnit').AsString);
        FieldByName('ComTaiTou').Value := Trim(cust.Text);
        FieldByName('P_CodeName').Value := trim(Order_Main10.fieldbyname('P_CodeName').AsString);
        FieldByName('P_Color').Value := trim(Order_Main10.fieldbyname('P_Color').AsString);
        FieldByName('YFName').Value := '样品退货';
        FieldByName('MainId').Value := Trim(ckNo);
        FieldByName('subID').Value := Trim(CkID);
        FieldByName('YFDefFlag1').Value := 0;
        FieldByName('syrName').Value := '建威纺织';
        Post;
      end;
    end
    else
    begin
      YFID := Trim(ADOQuery1.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;
  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=''BPCK'' ');
    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=''BPRKTYPE''');
    if trim(fCKName) <> '' then
      sql.Add('and note=' + quotedstr(trim(fCKName)));
    sql.Add('and zdyName not like ''%调拨%'' ');
    open;
    while not eof do
    begin
      CRType.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
  if CRType.Items.Count > 0 then
    CRType.ItemIndex := 0;
end;

function TfrmbpRk.savedate(): Boolean;
var
  fkcID: integer;
  Filler: string;
  FillTime: TdateTime;
begin
  result := false;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if FType = 0 then
    begin
      if GetLSNo(ADOQueryCmd, fkeyNO, 'BK', 'BP_InOut', 3, 1) = False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
      Filler := DName;
      FillTime := SGetServerDateTime(adoqueryTmp);
    end
    else
    begin
      with adoqueryTmp do
      begin
        close;
        sql.Clear;
        sql.Add('select * from BP_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 BP_InOut');
      sql.Add('where CRNO=' + quotedstr(trim(fkeyNO)));
      execsql;
    end;

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

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

    CDS_Main.DisableControls;
    CDS_Main.First;
    with CDS_Main do
    begin
      while not eof do
      begin

        Edit;
        FieldByName('CRNO').Value := trim(fkeyNO);
        FieldByName('CRNOS').Value := trim(fkeyNO + inttostr(recNo));
        FieldByName('CRID').Value := recNo;
        post;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          SQL.add('Update BP_Kcid Set kcid=kcid+1');
          sql.Add('select kcid  from BP_Kcid');
          Open;
        end;
        fkcID := ADOQueryCmd.fieldbyname('kcid').AsInteger;
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from BP_InOut');
          sql.Add('where 1=2 ');
          open;
        end;
   //     fkcID:=strtoint(RightBStr(trim(fkeyNO),length(fkeyNO)-3)+inttostr(recNo));
        ADOQueryCmd.Append;
        ADOQueryCmd.FieldByName('CRNO').Value := trim(fkeyNO);
        ADOQueryCmd.FieldByName('CRNOS').Value := trim(fkeyNO + inttostr(recNo));
        ADOQueryCmd.FieldByName('CRID').Value := recNo;
        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(fieldbyname('mainID').AsString);
        ADOQueryCmd.FieldByName('subID').Value := trim(fieldbyname('subID').AsString);
        ADOQueryCmd.FieldByName('MJID').Value := trim(fieldbyname('MJID').AsString);
        ADOQueryCmd.FieldByName('MJXH').Value := trim(fieldbyname('MJXH').AsString);
        ADOQueryCmd.FieldByName('CPTYPE').Value := trim(fieldbyname('CPTYPE').AsString);
        ADOQueryCmd.FieldByName('P_Code').Value := trim(fieldbyname('P_Code').AsString);
        ADOQueryCmd.FieldByName('P_CodeName').Value := trim(fieldbyname('P_CodeName').AsString);
        ADOQueryCmd.FieldByName('P_spec').Value := trim(fieldbyname('P_spec').AsString);
        ADOQueryCmd.FieldByName('P_Color').Value := trim(fieldbyname('P_Color').AsString);
        ADOQueryCmd.FieldByName('gangNo').Value := trim(fieldbyname('gangNo').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 := fieldbyname('Qty').AsFloat;
        ADOQueryCmd.FieldByName('QtyUnit').Value := trim(fieldbyname('QtyUnit').AsString);
        ADOQueryCmd.FieldByName('price').Value := fieldbyname('price').AsFloat;
        ADOQueryCmd.FieldByName('money').Value := fieldbyname('money').AsFloat;
        ADOQueryCmd.FieldByName('KuWei').Value := fieldbyname('KuWei').AsString;
        ADOQueryCmd.FieldByName('P_Colorno').Value := trim(fieldbyname('P_Colorno').AsString);
        ADOQueryCmd.FieldByName('note').Value := trim(fieldbyname('note').AsString);
        ADOQueryCmd.FieldByName('note').Value := trim(note.Text);
        ADOQueryCmd.FieldByName('cust').Value := trim(cust.Text);
        ADOQueryCmd.FieldByName('factoryName').Value := trim(factoryName.Text);
        ADOQueryCmd.FieldByName('custaddress').Value := trim(custaddress.Text);
        ADOQueryCmd.FieldByName('Filler').Value := trim(Filler);
        ADOQueryCmd.FieldByName('FillTime').Value := dateTimetostr(FillTime);
        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 BP_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('CRNOS').Value := trim(fkeyNO + inttostr(recNo));
        ADOQueryCmd.FieldByName('CRID').Value := recNo;
        ADOQueryCmd.FieldByName('CRTime').Value := formatdateTime('yyyy-MM-dd', CRTIme.Date);
        ADOQueryCmd.FieldByName('CRType').Value := trim(CRType.Text);
        ADOQueryCmd.FieldByName('CRFlag').Value := '入库';
        ADOQueryCmd.FieldByName('MJXH').Value := trim(fieldbyname('MJXH').AsString);
        ADOQueryCmd.FieldByName('MJID').Value := trim(fieldbyname('MJID').AsString);
        ADOQueryCmd.FieldByName('CPType').Value := trim(fieldbyname('CPType').AsString);
        ADOQueryCmd.FieldByName('P_Code').Value := trim(fieldbyname('P_Code').AsString);
        ADOQueryCmd.FieldByName('P_CodeName').Value := trim(fieldbyname('P_CodeName').AsString);
        ADOQueryCmd.FieldByName('P_spec').Value := trim(fieldbyname('P_spec').AsString);
        ADOQueryCmd.FieldByName('P_Color').Value := trim(fieldbyname('P_Color').AsString);
        ADOQueryCmd.FieldByName('gangNo').Value := trim(fieldbyname('gangNo').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 := fieldbyname('Qty').AsFloat;
        ADOQueryCmd.FieldByName('QtyUnit').Value := trim(fieldbyname('QtyUnit').AsString);
        ADOQueryCmd.FieldByName('kcID').Value := fkcID;
        ADOQueryCmd.Post;

        if trim(CRType.Text) = '采购入库' then
        begin
          if not YFData() then
          begin
            CDS_Main.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            exit;
          end;
        end;
        with ADOQuery1 do
        begin
          close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_CRMX A ');
          sql.Add(' right JOIN BP_InOut B on B.CRID=A.CRID  and B.crno=A.crno');
          sql.Add(' where A.CRNO=' + quotedstr(trim(CDS_Main.fieldbyname('CRNO').AsString)));
          sql.Add(' and A.CRID=' + quotedstr(trim(CDS_Main.fieldbyname('CRID').AsString)));
          SQL.Add(' and crzt=''入库''');
          sql.Add(' order by MXID');
//      ShowMessage(SQL.text);
          open;
        end;
        if ADOQuery1.RecordCount > 0 then
        begin
          with ADOQuery2 do
          begin
            Close;
            sql.Clear;
            sql.Add('Update CK_SXPB_CRMX Set CKNO=(select kuwei from BP_InOut A where A.crno=CK_SXPB_CRMX.CRNO  and A.CRID=CK_SXPB_CRMX.CRID)');
            sql.Add(' where CRNO=' + quotedstr(trim(CDS_Main.fieldbyname('CRNO').AsString)));
            sql.Add(' and CRID=' + quotedstr(trim(CDS_Main.fieldbyname('CRID').AsString)));
//            ShowMessage(sql.Text);
            ExecSQL;
          end;
        end;

        next;
      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 A.*,');
      sql.Add('orderNo=(select Top 1 orderNo from JYOrder_Main X where X.MainID=A.MainID )');
      sql.Add('from BP_InOut A ');
      sql.Add('where CRNO=' + quotedstr(trim(fkeyNO)));
      open;
    end;
    SCreateCDS20(adoqueryTmp, CDS_Main);
    SInitCDSData20(adoqueryTmp, CDS_Main);
    if not CDS_Main.IsEmpty then
    begin
      CRTime.Date := CDS_Main.fieldbyname('CRTime').AsDateTime;
      CKName.ItemIndex := CKName.Items.IndexOf(trim(CDS_Main.fieldbyname('CKName').AsString));
      CRType.ItemIndex := CRType.Items.IndexOf(trim(CDS_Main.fieldbyname('CRType').AsString));
   //   FirstName.TxtCode:=trim(CDS_Main.fieldbyname('FirstNo').AsString);
   //   FirstName.Text:=trim(CDS_Main.fieldbyname('FirstName').AsString);
      note.Text := trim(CDS_Main.fieldbyname('note').AsString);
      cust.Text := trim(CDS_Main.fieldbyname('cust').AsString);
      factoryName.Text := trim(CDS_Main.fieldbyname('factoryName').AsString);
      custaddress.Text := trim(CDS_Main.fieldbyname('custaddress').AsString);
    end;
  finally
  end;
end;

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

procedure TfrmbpRk.FormCreate(Sender: TObject);
begin
  CRTime.Date := DServerDate;
  cxGrid1.Align := alclient;
  readCxGrid(self.Caption, Tv1, '布匹仓库');
end;

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

procedure TfrmbpRk.FormShow(Sender: TObject);
begin
  SetComboBox();
  InitGrid();
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 formatdateTime('yyyy-MM-dd', CRTime.Date) > formatdateTime('yyyy-MM-dd', Date) then
  begin
    application.MessageBox('入库日期不能大于今日！', '提示信息', 0);
    exit;
  end;

  if trim(cust.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 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);
begin
  CDS_Main.Append;
  CDS_Main.fieldbyname('CPType').Value := '正品';
  CDS_Main.fieldbyname('RollNum').Value := 1;
  CDS_Main.fieldbyname('QtyUnit').Value := 'M';
  CDS_Main.Post;
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
//  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;
  frmZdyYS_sel := TfrmZdyYS_sel.create(self);
  with frmZdyYS_sel do
  begin
    if showmodal = 1 then
    begin
      CDS_HZ.First;
      while not CDS_HZ.Eof do
      begin
        if CDS_HZ.fieldbyname('ssel').AsBoolean then
        begin
          with cds_main do
          begin
            Edit;
            FieldByName('P_Color').value := CDS_HZ.fieldbyname('CodeName').AsString;
            FieldByName('P_ColorNo').value := CDS_HZ.fieldbyname('Colorno').AsString;
            Post;
          end;
        end;
        CDS_HZ.Next;
      end;

    end;
    free;
  end;
end;

procedure TfrmbpRk.custBtnClick(Sender: TObject);
begin

  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        cust.Text := Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
    //   custaddress.Text:=Trim(CDS_HZ.fieldbyname('defnote1').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;

end;

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

procedure TfrmbpRk.Edit1KeyPress(Sender: TObject; var Key: Char);
var
  FReal: Double;
  maxno: string;
begin
  if Key = #13 then
  begin
    if Trim(Edit1.Text) = '' then
      Exit;
    if TryStrToFloat(Edit1.Text, FReal) = False then
    begin
      Application.MessageBox('数量非法数字!', '提示', 0);
      Exit;
    end;
    if CDS_Main.IsEmpty then
    begin
      CDS_Main.Append;
      CDS_Main.fieldbyname('CPType').Value := '正品';
      CDS_main.fieldbyname('RollNum').Value := 1;
      CDS_main.fieldbyname('Qty').Value := Edit1.Text;
      CDS_Main.fieldbyname('QtyUnit').Value := 'M';
      CDS_Main.Post;
    end
    else
    begin
      CopyAddRowCDS(CDS_main);
      tv1.Controller.EditingController.ShowEdit();
      with CDS_main do
      begin
        Edit;
        CDS_main.fieldbyname('RollNum').Value := 1;
        CDS_main.fieldbyname('Qty').Value := Edit1.Text;
        Post;
      end;
    end;
    Edit1.Text := '';
  end;

  tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmbpRk.Edit2KeyPress(Sender: TObject; var Key: Char);
var
  FReal: Double;
  maxno: string;
  I: integer;
begin
  if Key = #13 then
  begin
    if Trim(Edit2.Text) = '' then
      Exit;
    if TryStrToFlOAT(Edit2.Text, FReal) = False then
    begin
      Application.MessageBox('数量非法数字!', '提示', 0);
      Exit;
    end;
    if CDS_main.IsEmpty then
      exit;
    I := STRTOintdef(Edit2.Text, 0);
    for I := 1 to STRTOintdef(Edit2.Text, 0) do
    begin
      CopyAddRowCDS(CDS_main);
    end;
    Edit1.Text := '';
  end;
  tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmbpRk.TBPrintClick(Sender: TObject);
var
  filepath: string;
begin
  if CDS_Main.IsEmpty then
    exit;

  with AdoPrint do
  begin
    close;
    sql.Clear;
    sql.Add('exec P_Do_PrintMd ');
    sql.Add('@mainID=' + quotedstr(trim(fkeyNO)));
    sql.Add(',@flag=' + quotedstr(trim('广东出库')));
    open;
  end;

  try
    filepath := ExtractFilePath(Application.ExeName) + 'report\退货码单建威广州.rmf';
    if not FileExists(Pchar(filepath)) then
    begin
      application.MessageBox(pchar('文件[' + filepath + ']不存在！'), '提示信息', MB_IConError);
      exit;
    end;
    RMVariables['dtxz'] := FormatDateTime('yyyy-mm-dd', date());
    RMVariables['zdr'] := trim(DName);
    RMVariables['Filler'] := trim(CDS_Main.fieldbyname('Filler').AsString);
    RMGridReport1.LoadFromFile(filepath);
    RMGridReport1.ShowReport;
  finally

  end;
end;

procedure TfrmbpRk.factoryNameBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        factoryName.Text := Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
    //   custaddress.Text:=Trim(CDS_HZ.fieldbyname('defnote1').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmbpRk.v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmProductOrderNewListSEl := TfrmProductOrderNewListSEl.create(self);
  with frmProductOrderNewListSEl do
  begin
    if showmodal = 1 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_CodeName').Value := trim(order_main.fieldbyname('prtCodeName').asstring);
      CDS_Main.FieldByName('P_Color').Value := trim(order_main.fieldbyname('prtcolor').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.FieldByName('note').Value := trim(order_main.fieldbyname('SOrdDefNote1').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.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('KuWei').Value := Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
      end;
    end;
  finally
    frmCPKWListSel.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmbpRk.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OrdColorNO';
      flagname := '色号';
//      V1Name.Caption := '中文名称';
//      V1Note.Caption := '英文名称';
//      MainType := Trim(DName);
//      fnote := True;
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('P_ColorNO').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_Main.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

end.

