unit U_BpCk;

interface

uses
  Windows, Messages, SysUtils, StrUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
  cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin, cxButtonEdit, cxDropDownEdit,
  DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, BtnEdit, cxTextEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmbpCk = 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;
    v1P_MF: TcxGridDBColumn;
    v1P_KZ: TcxGridDBColumn;
    v1RollNum: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1MJID: TcxGridDBColumn;
    v1MJXH: TcxGridDBColumn;
    v1CPType: TcxGridDBColumn;
    ckName: TComboBox;
    Label4: TLabel;
    v1P_Color: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    ToolButton3: TToolButton;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    Label3: TLabel;
    Tocust: TBtnEditA;
    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 v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure v1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TocustBtnClick(Sender: TObject);
  private
    fmainID, fsubID: string;
    procedure InitGrid();
    function savedate(): Boolean;
    function saveCPRK(): Boolean;
    procedure SetComboBox();
    function YSData(): Boolean;
    function YFData(): Boolean;
    function YFData10(): Boolean;
    { Private declarations }
  public
    fkeyNO: string;
    FType: integer;
    fCKName: string;
    { Public declarations }
  end;

var
  frmbpCk: TfrmbpCk;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_Bpkclist, U_ProductOrderNewList,
  U_ZdyAttachGYS, U_ProductOrderNewListSEl, U_ZdyAttachment;
{$R *.dfm}

function TfrmbpCk.YSData(): 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(Tocust.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(Tocust.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, 'CF', 'YF_Money_CR', 3, 1) = False then
    begin
      Application.MessageBox('取应收最大号失败!', '提示', 0);
      Exit;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR where 1<>1');
      Open;
    end;

    with ADOQueryCmd do
    begin
      Append;
      FieldByName('YFID').Value := Trim(YFID);
      FieldByName('YFTypeId').Value := Trim(CDS_Main.fieldbyname('CRNO').AsString);
      FieldByName('CRID').Value := StrToInt(CRID);
      FieldByName('Filler').Value := Trim(DName);
      FieldByName('CRType').Value := '应收款登记';
      FieldByName('CRFlag').Value := '应收收';
      FieldByName('QtyFlag').Value := 1;
      FieldByName('FactoryName').Value := Tocust.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 := Tocust.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;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_CR Set ps=(select isnull(Sum(RollNum),0) from BP_Inout A where A.CRNO=YF_Money_CR.MainId)');
    sql.Add(',Qty=(select isnull(Sum(Qty),0) from BP_Inout A where A.CRNO=YF_Money_CR.MainId)');
    sql.Add(',money=(select isnull(Sum(Qty),0) from BP_Inout A where A.CRNO=YF_Money_CR.MainId)*price');
    sql.Add(',BBmoney=(select isnull(Sum(Qty),0) from BP_Inout A where A.CRNO=YF_Money_CR.MainId)*price');
    sql.Add(' where CRID=' + CRID);
    ExecSQL;
  end;
  Result := True;
end;

function TfrmbpCk.YFData10(): 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(CDS_Main.fieldbyname('factoryName').AsString) + ''' 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(CDS_Main.fieldbyname('factoryName').AsString);
      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, 'CF', 'YF_Money_CR', 3, 1) = False then
    begin
      Application.MessageBox('取应收最大号失败!', '提示', 0);
      Exit;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR where 1<>1');
      Open;
    end;

    with ADOQueryCmd do
    begin
      Append;
      FieldByName('YFID').Value := Trim(YFID);
      FieldByName('YFTypeId').Value := Trim(CDS_Main.fieldbyname('CRNO').AsString);
      FieldByName('CRID').Value := StrToInt(CRID);
      FieldByName('Filler').Value := Trim(DName);
      FieldByName('CRType').Value := '应付款登记';
      FieldByName('CRFlag').Value := '应付付';
      FieldByName('QtyFlag').Value := 1;
      FieldByName('FactoryName').Value := CDS_Main.fieldbyname('factoryName').AsString;
      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 := CDS_Main.fieldbyname('factoryName').AsString;
      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;

function TfrmbpCk.saveCPRK(): Boolean;
var
  fkcID: integer;
  fcprkNo: string;
  finoutNo: string;
begin
  result := false;
  try
    if GetLSNo(ADOQueryCmd, fcprkNo, 'RK', 'CP_InOut', 3, 1) = False then
    begin
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    finoutNo := 'R' + Trim(RightBStr(fcprkNo, Length(fcprkNo) - 2));

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from CP_InOut');
      sql.Add('where fromMainID=' + quotedstr(trim(CDS_Main.fieldbyname('CRNO').AsString)));
      sql.Add('and fromsubID=' + quotedstr(trim(CDS_Main.fieldbyname('CRID').AsString)));
      execsql;
    end;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from CP_kc');
      sql.Add('where fromMainID=' + quotedstr(trim(CDS_Main.fieldbyname('CRNO').AsString)));
      sql.Add('and fromsubID=' + quotedstr(trim(CDS_Main.fieldbyname('CRID').AsString)));
      execsql;
    end;
    with CDS_Main do
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.add('Update CP_Kcid Set kcid=kcid+1');
        sql.Add('select kcid  from CP_Kcid');
        Open;
      end;
      fkcID := ADOQueryCmd.fieldbyname('kcid').AsInteger;
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from CP_InOut');
        sql.Add('where 1=2 ');
        open;
      end;

      ADOQueryCmd.Append;
      ADOQueryCmd.FieldByName('CRNO').Value := trim(fcprkNo);
      ADOQueryCmd.FieldByName('CRID').Value := 1;
      ADOQueryCmd.FieldByName('custName').Value := trim(fieldbyname('factoryName').AsString);

      ADOQueryCmd.FieldByName('CRTime').Value := formatdateTime('yyyy-MM-dd', CRTIme.Date);
      ADOQueryCmd.FieldByName('CKName').Value := '成品仓库';
      ADOQueryCmd.FieldByName('CRType').Value := '加工入库';
      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('batchNo').Value := '';
      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('P_MF').Value := fieldbyname('P_MF').AsFloat;
      ADOQueryCmd.FieldByName('P_KZ').Value := fieldbyname('P_KZ').AsFloat;
      ADOQueryCmd.FieldByName('RollNum').Value := self.CDS_Main.fieldbyname('RollNum').AsFloat;
      ADOQueryCmd.FieldByName('Qty').Value := self.CDS_Main.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('note').Value := trim(fieldbyname('note').AsString);
      ADOQueryCmd.FieldByName('Filler').Value := trim(DName);
       // ADOQueryCmd.FieldByName('FillTime').Value:=date();
      ADOQueryCmd.FieldByName('InOutNo').Value := trim(finoutNo);
      ADOQueryCmd.FieldByName('fromMainID').Value := trim(fieldbyname('CRNO').AsString);
      ADOQueryCmd.FieldByName('fromsubID').Value := trim(fieldbyname('CRID').AsString);
      ADOQueryCmd.FieldByName('kcID').Value := fkcID;
      ADOQueryCmd.Post;

      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from CP_KC');
        sql.Add('where 1=2 ');
        open;
      end;
      ADOQueryCmd.Append;
      ADOQueryCmd.FieldByName('kcID').Value := fkcID;
      ADOQueryCmd.FieldByName('CKName').Value := '成品仓库';
      ADOQueryCmd.FieldByName('CRNO').Value := trim(fcprkNo);
      ADOQueryCmd.FieldByName('CRID').Value := 1;
      ADOQueryCmd.FieldByName('CRTime').Value := formatdateTime('yyyy-MM-dd', CRTIme.Date);
      ADOQueryCmd.FieldByName('CRType').Value := '加工入库';
      ADOQueryCmd.FieldByName('CRFlag').Value := '入库';
      ADOQueryCmd.FieldByName('mainID').Value := trim(fieldbyname('mainID').AsString);
      ADOQueryCmd.FieldByName('subID').Value := trim(fieldbyname('subID').AsString);
      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('P_MF').Value := fieldbyname('P_MF').AsFloat;
      ADOQueryCmd.FieldByName('P_KZ').Value := fieldbyname('P_KZ').AsFloat;
      ADOQueryCmd.FieldByName('RollNum').Value := self.CDS_Main.fieldbyname('RollNum').AsFloat;
      ADOQueryCmd.FieldByName('Qty').Value := self.CDS_Main.fieldbyname('Qty').AsFloat;
      ADOQueryCmd.FieldByName('QtyUnit').Value := trim(fieldbyname('QtyUnit').AsString);
      ADOQueryCmd.FieldByName('fromMainID').Value := trim(fieldbyname('CRNO').AsString);
      ADOQueryCmd.FieldByName('fromsubID').Value := trim(fieldbyname('CRID').AsString);
      ADOQueryCmd.FieldByName('kcID').Value := fkcID;
      ADOQueryCmd.Post;
    end;
    result := true;
  except

  end;
end;

function TfrmbpCk.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(CDS_Main.fieldbyname('cust').AsString) + ''' 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(CDS_Main.fieldbyname('cust').AsString);
      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, 'CF', 'YF_Money_CR', 3, 1) = False then
    begin
      Application.MessageBox('取应收最大号失败!', '提示', 0);
      Exit;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR where 1<>1');
      Open;
    end;

    with ADOQueryCmd do
    begin
      Append;
      FieldByName('YFID').Value := Trim(YFID);
      FieldByName('YFTypeId').Value := Trim(CDS_Main.fieldbyname('CRNO').AsString);
      FieldByName('CRID').Value := StrToInt(CRID);
      FieldByName('Filler').Value := Trim(DName);
      FieldByName('CRType').Value := '应付款登记';
      FieldByName('CRFlag').Value := '应付付';
      FieldByName('QtyFlag').Value := 1;
      FieldByName('FactoryName').Value := CDS_Main.fieldbyname('cust').AsString;
      FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', CRTime.DateTime));
      FieldByName('YFType').Value := '自动生成';
      FieldByName('ps').Value := -1 * CDS_Main.fieldbyname('RollNum').AsFloat;
      FieldByName('Qty').Value := -1 * CDS_Main.fieldbyname('Qty').AsFloat;
      FieldByName('Price').Value := CDS_Main.fieldbyname('Price').AsFloat;
      FieldByName('Money').Value := -1 * CDS_Main.fieldbyname('Money').AsFloat;
      FieldByName('BBMoney').Value := -1 * CDS_Main.fieldbyname('Money').AsFloat;
      FieldByName('HuiLv').Value := 1;
      FieldByName('BZType').Value := '人民币';
      FieldByName('QtyUnit').Value := Trim(CDS_Main.fieldbyname('QtyUnit').AsString);
      FieldByName('ComTaiTou').Value := CDS_Main.fieldbyname('cust').AsString;
      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_CR Set ps=(select isnull(Sum(RollNum),0) from BP_Inout A where A.CRNO=YF_Money_CR.MainId)');
    sql.Add(',Qty=(select isnull(Sum(Qty),0) from BP_Inout A where A.CRNO=YF_Money_CR.MainId)');
    sql.Add(',money=(select isnull(Sum(Qty),0) from BP_Inout A where A.CRNO=YF_Money_CR.MainId)*price');
    sql.Add(',BBmoney=(select isnull(Sum(Qty),0) from BP_Inout A where A.CRNO=YF_Money_CR.MainId)*price');
    sql.Add(' where CRID=' + CRID);
    ExecSQL;
  end;

  Result := True;
end;

procedure TfrmbpCk.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=''BPCKTYPE''');
    if trim(fCKName) <> '' then
      sql.Add('and note=' + quotedstr(trim(fCKName)));
    open;
    while not eof do
    begin
      CRType.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
  if CRType.Items.Count > 0 then
    CRType.ItemIndex := 0;
end;

function TfrmbpCk.savedate(): Boolean;
var
  fkcID: integer;
  Filler: string;
  FillTime: TdateTime;
  maxno: string;
begin
  result := false;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if FType = 0 then
    begin
      if GetLSNo(ADOQueryCmd, fkeyNO, 'BC', '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('update BP_kc set RollNum=A.RollNum + B.RollNum, Qty=A.Qty + B.Qty ');
      sql.Add('from  BP_kc  A');
      sql.Add('inner join BP_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 YF_Money_CR ');
      sql.Add(' where MainID=' + quotedstr(trim(fkeyNO)));
     //   sql.Add(' and subID='''+Trim(CDS_Main.fieldbyname('CRID').AsString)+'''');
     //   sql.Add(' and YFName=''加工费'' ');
      execsql;
    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from BP_InOut');
      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
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from BP_InOut');
          sql.Add('where 1=2 ');
          open;
        end;
        ADOQueryCmd.Append;
        ADOQueryCmd.FieldByName('CRNO').Value := trim(fkeyNO);
        ADOQueryCmd.FieldByName('CRID').Value := recNo;
        ADOQueryCmd.FieldByName('CKName').Value := trim(CKName.Text);
        ADOQueryCmd.FieldByName('cust').Value := trim(fieldbyname('cust').AsString);
        ADOQueryCmd.FieldByName('Tocust').Value := trim(Tocust.Text);
        ADOQueryCmd.FieldByName('factoryName').Value := trim(fieldbyname('factoryName').AsString);
        ADOQueryCmd.FieldByName('mainID').Value := trim(fieldbyname('mainID').AsString);
        ADOQueryCmd.FieldByName('subID').Value := trim(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('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('JHRollNum').Value := fieldbyname('JHRollNum').AsFloat;
        ADOQueryCmd.FieldByName('JHQty').Value := fieldbyname('JHQty').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('note').Value := trim(fieldbyname('note').AsString);
        ADOQueryCmd.FieldByName('Filler').Value := trim(Filler);
        ADOQueryCmd.FieldByName('FillTime').Value := dateTimetostr(FillTime);
   //     ADOQueryCmd.FieldByName('FirstNo').Value:=trim(FirstName.TxtCode);
   //     ADOQueryCmd.FieldByName('FirstName').Value:=trim(FirstName.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;

        edit;
        FieldByName('CRNO').Value := trim(fkeyNO);
        FieldByName('CRID').Value := recNo;
        FieldByName('mainID').Value := trim(fmainID);
        FieldByName('subID').Value := trim(fsubID);
        post;

     {   IF trim(CRType.Text)='加工出库' then
        begin
          if not saveCPRK() then
          begin
            CDS_Main.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            exit;
          end;
          if not YFData10() then
          begin
            CDS_Main.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            exit;
          end;
        end;

        IF trim(CRType.Text)='退货出库' then
        begin
          if not YFData() then
          begin
            CDS_Main.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            exit;
          end;
        end;  }

        next;
      end;
      if trim(CRType.Text) = '退货出库' then
      begin
        if not YFData() then
        begin
          CDS_Main.EnableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          exit;
        end;
      end;
      if trim(CRType.Text) = '销售出库' then
      begin
        if not YSData() then
        begin
          CDS_Main.EnableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          exit;
        end;
      end;

      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add(' update BP_kc SET RollNum=RollNum-(select SUM(RollNUM) from BP_INout X where X.KCID=BP_kc.KCID and X.CRNo=''' + trim(fkeyNO) + '''  ), ');
        sql.Add('              Qty=Qty-(select SUM(Qty) from BP_INout X where X.KCID=BP_kc.KCID and X.CRNo=''' + trim(fkeyNO) + ''' ) ');
        sql.Add(' where exists(select KCID from BP_INout X where X.KCID=BP_kc.KCID and X.CRNO=''' + trim(fkeyNO) + ''' )');
       //  sql.Add('update BP_kc set RollNum=A.RollNum - B.RollNum, Qty=A.Qty - B.Qty ');
       //  sql.Add('from  BP_kc  A');
       //  sql.Add('inner join BP_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('update BP_kc set RollNum=1');
        sql.Add('from  BP_kc  A');
        sql.Add('where A.RollNum=0 and A.Qty<>0 ');
        execsql;
      end;

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

procedure TfrmbpCk.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 A.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));
      Tocust.text := CDS_Main.fieldbyname('Tocust').AsString;
   //   FirstName.TxtCode:=trim(CDS_Main.fieldbyname('FirstNo').AsString);
   //   FirstName.Text:=trim(CDS_Main.fieldbyname('FirstName').AsString);
    end;
  finally
  end;
end;

procedure TfrmbpCk.FormDestroy(Sender: TObject);
begin
  frmbpCk := nil;
end;

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

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

procedure TfrmbpCk.FormShow(Sender: TObject);
begin
  SetComboBox();
  InitGrid();
end;

procedure TfrmbpCk.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) = '销售出库') and (trim(Tocust.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 TfrmbpCk.v1P_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'Cloth';
      flagname := '坯布名称';
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('P_CodeName').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_Main.FieldByName('P_Code').Value := Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmbpCk.ToolButton1Click(Sender: TObject);
begin
  frmBpkclist := TfrmBpkclist.create(self);
  with frmBpkclist 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) and (not self.cds_main.Locate('kcid', cds_main.fieldbyname('kcid').AsInteger, [])) then
        begin
          self.cds_main.Append;
          self.cds_main.FieldByName('kcid').Value := trim(cds_main.fieldbyname('kcid').AsString);
          self.cds_main.FieldByName('cust').Value := trim(cds_main.fieldbyname('cust').AsString);
          self.cds_main.FieldByName('factoryName').Value := trim(cds_main.fieldbyname('factoryName').AsString);
          self.cds_main.FieldByName('MJID').Value := trim(cds_main.fieldbyname('MJID').AsString);
          self.cds_main.FieldByName('MJXH').Value := trim(cds_main.fieldbyname('MJXH').AsString);
          self.cds_main.FieldByName('CPTYPE').Value := trim(cds_main.fieldbyname('CPTYPE').AsString);
          self.cds_main.FieldByName('P_Code').Value := trim(cds_main.fieldbyname('P_Code').AsString);
          self.cds_main.FieldByName('P_CodeName').Value := trim(cds_main.fieldbyname('P_CodeName').AsString);
          self.cds_main.FieldByName('P_spec').Value := trim(cds_main.fieldbyname('P_spec').AsString);
          self.cds_main.FieldByName('P_Color').Value := trim(cds_main.fieldbyname('P_Color').AsString);
          self.cds_main.FieldByName('gangNo').Value := trim(cds_main.fieldbyname('gangNo').AsString);
          self.cds_main.FieldByName('P_MF').Value := cds_main.fieldbyname('P_MF').AsFloat;
          self.cds_main.FieldByName('P_KZ').Value := cds_main.fieldbyname('P_KZ').AsFloat;
          self.cds_main.FieldByName('JHRollNum').Value := cds_main.fieldbyname('RollNum').AsFloat;
          self.cds_main.FieldByName('JHQty').Value := cds_main.fieldbyname('Qty').AsFloat;
          self.cds_main.FieldByName('QtyUnit').Value := trim(cds_main.fieldbyname('QtyUnit').AsString);
          self.cds_main.Post;
        end;
        cds_main.Next;
      end;
      cds_main.EnableControls;
    end;
    free;
  end;

end;

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

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

procedure TfrmbpCk.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 TfrmbpCk.ToolButton3Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  CopyAddRow(Tv1, CDS_Main);
end;

procedure TfrmbpCk.v1Column6PropertiesButtonClick(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.post;
    end;
    free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmbpCk.v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmZdyAttachGYS := TfrmZdyAttachGYS.create(self);
  with frmZdyAttachGYS do
  begin
    if showmodal = 1 then
    begin
      CDS_Main.Edit;
      CDS_Main.FieldByName('factoryName').Value := trim(CDS_HZ.fieldbyname('ZDYName').asstring);
      CDS_Main.post;
    end;
    free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmbpCk.TocustBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if ShowModal = 1 then
      begin
        Tocust.Text := Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
    //   custaddress.Text:=Trim(CDS_HZ.fieldbyname('defnote1').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

end.

