unit U_CollectWriteOff;

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,
  cxTextEdit, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, 
   dxDateRanges, dxBarBuiltInMenu, cxGridCustomPopupMenu,
  cxGridPopupMenu, U_BaseInput, System.ImageList, Vcl.ImgList, Vcl.Menus;

type
  TfrmCollectWriteOff = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DS_Ysk: TDataSource;
    CDS_Ysk: TClientDataSet;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    VC_ORDERNO: TcxGridDBColumn;
    v1PBNote: TcxGridDBColumn;
    Panel1: TPanel;
    v1Column8: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    Label6: TLabel;
    F_Name: TEdit;
    GPM_1: TcxGridPopupMenu;
    Button1: TButton;
    ToolButton1: TToolButton;
    Label8: TLabel;
    Label9: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    ADO_YSK: TADOQuery;
    ADO_SK: TADOQuery;
    Label3: TLabel;
    curHsNum: TEdit;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    OppCoName: TEdit;
    Amount: TEdit;
    FFWoAmount: TEdit;
    NWoAmount: TEdit;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure v1Column7PropertiesEditValueChanged(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure KHConNoKeyPress(Sender: TObject; var Key: Char);
    procedure EnddateChange(Sender: TObject);
    procedure begdateChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
  private
    procedure InitYsk();
    procedure InitSk();
    procedure SellRec(mFlag: integer);
    { Private declarations }
  public
    FFFID: string;
    fOppCoNo, FKPNO, FOurCoName: string;
    { Public declarations }
  end;

var
  frmCollectWriteOff: TfrmCollectWriteOff;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp;
{$R *.dfm}

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

procedure TfrmCollectWriteOff.InitYsk();
begin
  try
    ADO_YSK.DisableControls;
    with ADO_YSK do
    begin
      Close;
      SQL.Clear;
      Filtered := False;
      sql.Add('select *,NWoAmount=Amount-isnull(FFWoAmount,0),CurWoAmount=0.00 ');
      sql.Add('from Finance_Flow A ');
      sql.Add(' where Amount-isnull(FFWoAmount,0)<>0');
      sql.Add(' and FFType=' + quotedstr(trim('应收款登记')));
      sql.Add(' and A.status=''9'' ');
      sql.Add(' and OppCoNo=' + quotedstr(trim(fOppCoNo)));
      sql.Add(' and OurCoName=' + quotedstr(trim(FOurCoName)));
      sql.Add(' order by FFTime,FFID ');
      Open;
    end;
    SDofilter(ADO_YSK, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADO_YSK, CDS_Ysk);
    SInitCDSData(ADO_YSK, CDS_Ysk);
  finally
    ADO_YSK.EnableControls;
  end;
end;

procedure TfrmCollectWriteOff.InitSk();
begin
  try
    ADO_SK.DisableControls;
    with ADO_SK do
    begin
      Close;
      SQL.Clear;
      Filtered := False;
      sql.Add('select *,NWoAmount=Amount-isnull(FFWoAmount,0) from Finance_Flow A ');
      sql.Add('where FFID=' + quotedstr(trim(FFFID)));
      sql.Add(' and A.status=''9'' ');
      Open;
    end;

    SCSHData(ADO_SK, ScrollBox1, 0);
    curHsNum.Text := ADO_SK.FieldByName('NWoAmount').AsString;
  finally
    ADO_SK.EnableControls;
  end;
end;

procedure TfrmCollectWriteOff.FormDestroy(Sender: TObject);
begin
  inherited;
  frmCollectWriteOff := nil;
end;

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

procedure TfrmCollectWriteOff.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(self.Caption, Tv1, '财务管理');
  Enddate.DateTime := SGetServerDate(ADOQueryTmp);
  begdate.DateTime := Enddate.DateTime - 30;
  InitSk();
  InitYsk();
end;

procedure TfrmCollectWriteOff.TBCloseClick(Sender: TObject);
begin

  close;
end;

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

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

procedure TfrmCollectWriteOff.Button1Click(Sender: TObject);
var
  tmpSum: double;
begin
  tmpSum := 0.0;
  try
    CDS_Ysk.DisableControls;
    with CDS_Ysk do
    begin
      first;
      while not eof do
      begin
        if FieldByName('ssel').AsBoolean then
        begin
          tmpSum := tmpSum + FieldByName('NWoAmount').asFloat;
          edit;
          if (tmpSum - StrToFloatDef(curHsNum.Text, 0)) > 0 then
          begin
            FieldByName('CurWoAmount').value := FieldByName('NWoAmount').asFloat - (tmpSum - StrToFloatDef(curHsNum.Text, 0))
          end
          else
          begin
            FieldByName('CurWoAmount').value := FieldByName('NWoAmount').asFloat;
          end;
        end
        else
        begin
          edit;
          FieldByName('CurWoAmount').value := 0;
        end;
        post;
        if FieldByName('ssel').AsBoolean and ((tmpSum - StrToFloatDef(curHsNum.Text, 0)) > 0) then
          break;
        next;
      end;
    end;
    CDS_Ysk.Locate('SSel', True, []);
    CDS_Ysk.EnableControls;
  except
    CDS_Ysk.EnableControls;
  end;
end;

procedure TfrmCollectWriteOff.TBSaveClick(Sender: TObject);
begin
  if CDS_Ysk.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;

  try
    CDS_Ysk.First;
    while not CDS_Ysk.Eof do
    begin
      if CDS_Ysk.FieldByName('ssel').AsBoolean and (Abs(CDS_Ysk.FieldByName('CurWoAmount').AsFloat) > 0.001) then
      begin

        with AdoqueryCmd do
        begin
          Close;
          SQL.Clear;
          SQL.Add(' exec P_Fin_Flow_WriteOff');
          SQL.Add(' @ToID = ' + QuotedStr(Trim(CDS_Ysk.FieldByName('FFID').AsString)));
          SQL.Add(',@FromID = ' + QuotedStr(Trim(FFFID)));
          SQL.Add(',@CurWoAmount = ' + FloatToStr(CDS_Ysk.FieldByName('CurWoAmount').AsFloat));
          SQL.Add(',@DName = ' + QuotedStr(Trim(DName)));
          Open;

        end;
        if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
          raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
      end;
      CDS_Ysk.Next;
    end;
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
  InitYsk();
  InitSk();
end;

procedure TfrmCollectWriteOff.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv1, '财务管理');
end;

procedure TfrmCollectWriteOff.KHConNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitYsk();
  end;
end;

procedure TfrmCollectWriteOff.N1Click(Sender: TObject);
begin
  SellRec(1);

end;

procedure TfrmCollectWriteOff.N2Click(Sender: TObject);
begin
  SellRec(0);
end;

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

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

end.

