unit DblDeclineUnit; {$mode objfpc}{$H+} interface uses Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls, Math, contexthelpunit; type { TDblDeclineFrm } TDblDeclineFrm = class(TForm) HelpBtn: TButton; ResetBtn: TButton; ComputeBtn: TButton; ReturnBtn: TButton; CostEdit: TEdit; LifeEdit: TEdit; EndEdit: TEdit; PeriodEdit: TEdit; DeprecEdit: TEdit; Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; Label5: TLabel; procedure ComputeBtnClick(Sender: TObject); procedure FormShow(Sender: TObject); procedure HelpBtnClick(Sender: TObject); procedure ResetBtnClick(Sender: TObject); function DoubleDecliningBalance(Cost, Salvage: Extended; Life, Period: Integer): Extended; private { private declarations } public { public declarations } end; var DblDeclineFrm: TDblDeclineFrm; implementation { TDblDeclineFrm } procedure TDblDeclineFrm.ResetBtnClick(Sender: TObject); begin CostEdit.Text := ''; LifeEdit.Text := ''; EndEdit.Text := ''; PeriodEdit.Text := ''; DeprecEdit.Text := ''; end; procedure TDblDeclineFrm.FormShow(Sender: TObject); begin ResetBtnClick(self); end; procedure TDblDeclineFrm.HelpBtnClick(Sender: TObject); begin ContextHelpForm.HelpMessage((Sender as TButton).tag); end; procedure TDblDeclineFrm.ComputeBtnClick(Sender: TObject); VAR Depreciation, Cost, Salvage : Extended; Life, Period : integer; begin Cost := StrToFloat(CostEdit.Text); Salvage := StrToFloat(EndEdit.Text); Life := StrToInt(LifeEdit.Text); Period := StrToInt(PeriodEdit.Text); Depreciation := DoubleDecliningBalance(Cost, Salvage, Life, Period); DeprecEdit.Text := FloatToStr(Depreciation); end; function TDblDeclineFrm.DoubleDecliningBalance(Cost, Salvage: Extended; Life, Period: Integer): Extended; { dv := cost * (1 - 2/life)**(period - 1) DDB = (2/life) * dv if DDB > dv - salvage then DDB := dv - salvage if DDB < 0 then DDB := 0 } var DepreciatedVal, Factor: Extended; begin Result := 0; if (Period < 1) or (Life < Period) or (Life < 1) or (Cost <= Salvage) then Exit; {depreciate everything in period 1 if life is only one or two periods} if ( Life <= 2 ) then begin if ( Period = 1 ) then DoubleDecliningBalance:=Cost-Salvage else DoubleDecliningBalance:=0; {all depreciation occurred in first period} exit; end; Factor := 2.0 / Life; DepreciatedVal := Cost * IntPower((1.0 - Factor), Period - 1); {DepreciatedVal is Cost-(sum of previous depreciation results)} Result := Factor * DepreciatedVal; {Nominal computed depreciation for this period. The rest of the function applies limits to this nominal value. } {Only depreciate until total depreciation equals cost-salvage.} if Result > DepreciatedVal - Salvage then Result := DepreciatedVal - Salvage; {No more depreciation after salvage value is reached. This is mostly a nit. If Result is negative at this point, it's very close to zero.} if Result < 0.0 then Result := 0.0; end; initialization {$I dbldeclineunit.lrs} end.