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, ExtCtrls;

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;
    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;
    Panel1: TPanel;
    Label7: TLabel;
    OrderNo: TEdit;
    Label8: TLabel;
    Label9: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    v1Column8: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    ADOQueryCmd: TADOQuery;
    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);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure EnddateChange(Sender: TObject);
    procedure begdateChange(Sender: TObject);
    
  private
    procedure InitYsk();
    procedure Initsk();
    procedure SellRec(mFlag:integer);
    function savedata():Boolean;
    { Private declarations }
  public
    fKeyNo:string;
    ffactoryName,FKPNO:string;
    { Public declarations }
  end;

var
  frmskHx: TfrmskHx;

implementation
 uses
  U_DataLink,U_Fun10,U_ZDYHelp;
{$R *.dfm}
function TfrmskHx.savedata():Boolean;
var
  PZNo: String;
begin
  result:=false;
  try
     if GetLSNo(ADOQueryTmp,PZNo,'HX','YF_log',4,1)=False then
    begin
      Application.MessageBox('取最大号失败！','提示',0);
      Exit;
    end;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add(' insert into YF_log(AutoID,FromNo,ToNo,Num,Type,Filler)');
      sql.Add('values('+quotedstr(trim(PZNo))
      +','+quotedstr(trim(fkeyNO))
      +','+quotedstr(trim(cdsYsk.FieldByName('YFSID').AsString))
      +','+trim(floattostr(cdsYsk.FieldByName('curHxNum').AsFloat))
      +','+quotedstr(trim('FK'))
      +','+quotedstr(trim(DName))+')');
      execsql;
    end;


    with AdoqueryCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add('exec P_Do_Yshx   @PZNo= ' + QuotedStr(PZNo));
      SQL.Add(',@CZType= ''新增''');
      execsql;
    end;
    
  except
    result:=false;
  end;
   result:=true;
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=MXMoney-isnull(hxNum,0),curHxNum=0.00 ');
      sql.Add('from YF_Money_CR A  inner join YF_Money_CR_Sub B on A.YFID=B.YFID');
      sql.Add('where A.factoryName='+quotedstr(trim(ffactoryName)));
      sql.Add(' and A.CRType='+quotedstr(trim('应付款登记')));
        sql.Add(' and A.status=''1''');
      sql.Add(' and MXMoney-isnull(hxNum,0)<>0');
      if trim(OrderNo.Text)<>'' then
        sql.Add(' and OrderNo like ''%'+trim(OrderNo.Text)+'%'' ');
      sql.Add('and A.CRTime>='''+FormatDateTime('yyyy-MM-dd',begdate.DateTime)+''' ');
      sql.Add(' and CRTime<'''+FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1)+''' ');
      sql.Add(' order by CRTime ');
      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=MXMoney-isnull(hxNum,0) from YF_Money_CR_Sub A ');
      sql.Add('where YFSID='+quotedstr(trim(fkeyNO)));
      Open;
    end;
    IF not ADOQueryTmp.IsEmpty then
    begin
      factoryName.Text:=trim(ffactoryName);
      money.Text:=trim(ADOQueryTmp.fieldbyname('MXMoney').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
  Enddate.DateTime:=SGetServerDate(ADOQueryTmp);
  begdate.DateTime:=Enddate.DateTime-30;
  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
  if cdsYsk.IsEmpty then
  begin
    Application.MessageBox('没有对应的应收款记录!','提示信息',0);
    Exit;
  end;
  if Abs((tv1.DataController.Summary.FooterSummaryValues[3])) <= 0.000001 then
  begin
    Application.MessageBox('未选择当前需核销的应收款记录!','警告信息',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() then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Exit;
          end;
      end;
      cdsYsk.Next ;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  finally
    Initysk();
    initsk();
  end;

end;

procedure TfrmskHx.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13 then
  begin
        InitYsk();
  end;
end;

procedure TfrmskHx.EnddateChange(Sender: TObject);
begin
InitYsk();
end;

procedure TfrmskHx.begdateChange(Sender: TObject);
begin
InitYsk();
end;

end.
