unit U_skHx;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, StdCtrls, ComCtrls, ToolWin, DBClient, ADODB,
  cxCurrencyEdit, cxCheckBox;

type
  TfrmskHx = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    factoryName: TEdit;
    Label2: TLabel;
    Money: TEdit;
    Label3: TLabel;
    curHsNum: TEdit;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    hxNum: TEdit;
    Label4: TLabel;
    NhxNum: TEdit;
    Label5: TLabel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DS_ysk: TDataSource;
    cdsYsk: TClientDataSet;
    ADOQueryYsk: TADOQuery;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    v1OrderNO: TcxGridDBColumn;
    v1PBNote: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1FPNo: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);

    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure v1Column7PropertiesEditValueChanged(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);

  private
    procedure InitYsk();
    procedure Initsk();
    procedure SellRec(mFlag:integer);
    function savedata(mtype:integer):Boolean;
    { Private declarations }
  public
    fKeyNo:string;
    ffactoryName:string;
    { Public declarations }
  end;

var
  frmskHx: TfrmskHx;

implementation
 uses
  U_DataLink,U_Fun10,U_ZDYHelp;
{$R *.dfm}
function TfrmskHx.savedata(mtype:integer):Boolean;
var
  PZNo: String;
begin
  result:=false;
  if GetLSNo(ADOQueryCmd,PZNo,'HX','YF_log',4,1)=False then
  begin
    Application.MessageBox('取最大号失败！','提示',0);
    Exit;
  end;
  with AdoqueryCmd do
  begin
    Close;
    SQL.Clear;
    SQL.Add('EXEC P_Do_Yshx');
    SQL.Add(' @hxFlag = ' + IntToStr(mType));
    SQL.Add(',@ysAutoID = ' + QuotedStr(Trim(cdsYsk.FieldByName('YFID').AsString)));
    SQL.Add(',@skAutoID = ' + QuotedStr(Trim(fkeyNO)));
    SQL.Add(',@hxMoney = ' + FloatToStr(cdsYsk.FieldByName('curHxNum').AsFloat));
    SQL.Add(',@UserID = ' + QuotedStr(Trim(DName)));
    SQL.Add(',@PZNo = ' + QuotedStr(Trim(PZNo)));
    Open;
    if FieldByName('Result').AsInteger <> 0 then
    begin
      Application.MessageBox(PChar(FieldByName('ShowMsg').AsString), '提示', MB_ICONSTOP);
    end
    else
    begin
      Result := True;
    end;
  end;
end;

procedure TfrmskHx.SellRec(mFlag:integer);
var
  tmpSum:double;
begin
  tmpSum:=0.0;
  try
    cdsYsk.DisableControls;
    with cdsYsk do
    begin
      first;
      while not eof do
      begin
        tmpSum:=tmpSum+FieldByName('NHxNum').asFloat;
        edit;
        if mFlag=0 then
        begin
          FieldByName('ssel').value:=False;
          FieldByName('curHxNum').value:=0;
        end
        else
        begin
          FieldByName('ssel').value:=True;
          if (tmpSum-StrToFloatDef(curHsNum.Text,0))>0 then
            FieldByName('curHxNum').value:=FieldByName('NHxNum').asFloat-(tmpSum-StrToFloatDef(curHsNum.Text,0))
          else
            FieldByName('curHxNum').value:=FieldByName('NHxNum').asFloat;
        end;
        post;
        //////////////
        if (mFlag=1) and ((tmpSum-StrToFloatDef(curHsNum.Text,0))>0) then
          break;
        /////////////////////
        next;
      end;
    end;
    cdsYsk.EnableControls ;
  except
    cdsYsk.EnableControls ;
  end;
end;

procedure TfrmskHx.InitYsk();
begin
  try
    ADOQueryTmp.DisableControls;
    with ADOQueryTmp do
    begin
      Close;
      SQL.Clear;
      Filtered:=False;
      sql.Add('select *,NhxNum=Money-isnull(hxNum,0),curHxNum=0.00,inoutNo=(select Top 1 InoutNo from CP_INout X where X.CRNO=A.MainID) ');
      sql.Add('from YF_Money_CR A ');
      sql.Add('where factoryName='+quotedstr(trim(ffactoryName)));
      sql.Add(' and CRType='+quotedstr(trim('应收款登记')));
      sql.Add(' and Money-isnull(hxNum,0)<>0');
      sql.Add(' order by CRTime,YFID ');
      Open;
    end;
    SCreateCDS20(ADOQueryTmp,cdsYsk);
    SInitCDSData20(ADOQueryTmp,cdsYsk);
  finally;
    ADOQueryTmp.EnableControls;
  end;
end;

procedure TfrmskHx.Initsk();
begin
  try
    ADOQueryTmp.DisableControls;
    with ADOQueryTmp do
    begin
      Close;
      SQL.Clear;
      Filtered:=False;
      sql.Add('select *,NhxNum=KHDKMoney-isnull(hxNum,0) from YF_Money_CR A');
      sql.Add('where YFID='+quotedstr(trim(fkeyNO)));
      Open;
    end;
    IF not ADOQueryTmp.IsEmpty then
    begin
      factoryName.Text:=trim(ADOQueryTmp.fieldbyname('factoryName').AsString);
      money.Text:=trim(ADOQueryTmp.fieldbyname('KHDKMoney').AsString);
      hxNUm.Text:=trim(ADOQueryTmp.fieldbyname('hxNUm').AsString);
      NhxNUm.Text:=trim(ADOQueryTmp.fieldbyname('NhxNUm').AsString);
      curHsNum.Text:=NhxNUm.Text;
    end;
  finally;
    ADOQueryTmp.EnableControls;
  end;
end;

procedure TfrmskHx.FormDestroy(Sender: TObject);
begin
  frmskHx:=nil;
end;

procedure TfrmskHx.FormCreate(Sender: TObject);
begin
  cxGrid1.Align:=alclient;
end;

procedure TfrmskHx.FormShow(Sender: TObject);
begin
  initsk();
  initysk();
end;

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

procedure TfrmskHx.v1Column7PropertiesEditValueChanged(Sender: TObject);
begin
  try
    cdsYsk.DisableControls;
    if TcxCurrencyEdit(sender).EditValue >cdsYsk.FieldByName('NHxNum').AsFloat then
    begin
      Application.MessageBox('本次核销数不能不大于未核销金额!','提示信息',0);
      TcxCurrencyEdit(sender).EditValue:=null;
      with cdsYsk do
      begin
        edit;
        FieldByName('ssel').value:=False;
        FieldByName('curHxNum').value:=0;
        Post;
     end;
     Exit;
    end;

    with cdsYsk do
    begin
      edit;
      if TcxCurrencyEdit(sender).EditValue=0 then
        FieldByName('ssel').value:=False
      else
        FieldByName('ssel').value:=True;
      FieldByName('curHxNum').value:=TcxCurrencyEdit(sender).EditValue;
      Post;
    end;
  finally
    cdsYsk.EnableControls ;
  end;
end;

procedure TfrmskHx.CheckBox1Click(Sender: TObject);
begin
  if checkBox1.Checked then SellRec(1)
  else SellRec(0);
end;

procedure TfrmskHx.TBSaveClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if cdsYsk.IsEmpty then
  begin
    Application.MessageBox('没有对应的应收款记录!','提示信息',0);
    Exit;
  end;
  if Abs((tv1.DataController.Summary.FooterSummaryValues[3])) <= 0.000001 then
  begin
    Application.MessageBox('本次核销金额不能为0','警告信息',0);
    Exit;
  end;
  if (tv1.DataController.Summary.FooterSummaryValues[3]-StrToFloatDef(curHsNum.Text,0))>0.0 then
  begin
    Application.MessageBox('本次核销金额大于当前可核销收款总金额!','警告信息',0);
    Exit;
  end;

  ADOQueryCmd.Connection.BeginTrans;
  try
    cdsYsk.First ;
    while not cdsYsk.Eof do
    begin
      if cdsYsk.FieldByName('ssel').AsBoolean and (Abs(cdsYsk.FieldByName('curHxNum').AsFloat) > 0.001) then
      begin
        if not savedata(1) then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Exit;
        end;
      end;
      cdsYsk.Next ;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  finally
    Initysk();
    initsk();
  end;
end;

end.
