Files
lazarus-ccr/applications/lazstats/source_orig/sumyrsdepunit.pas
wp_xxyyzz 819af1d403 LazStats: Adding original source, part 7.
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7886 8e941d3f-bd1b-0410-a28a-d453659cc2b4
2020-11-16 11:21:34 +00:00

103 lines
2.5 KiB
ObjectPascal

unit SumYrsDepUnit;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
StdCtrls, Math, contexthelpunit;
type
{ TSumYrsDepFrm }
TSumYrsDepFrm = class(TForm)
HelpBtn: TButton;
ResetBtn: TButton;
ComputeBtn: TButton;
ReturnBtn: TButton;
CostEdit: TEdit;
SalvageEdit: TEdit;
LifeEdit: TEdit;
PeriodEdit: TEdit;
DepreciationEdit: 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);
private
{ private declarations }
function SYDDepreciation(Cost, Salvage: Extended; Life, Period: Integer): Extended;
public
{ public declarations }
end;
var
SumYrsDepFrm: TSumYrsDepFrm;
implementation
{ TSumYrsDepFrm }
procedure TSumYrsDepFrm.ResetBtnClick(Sender: TObject);
begin
CostEdit.Text := '';
SalvageEdit.Text := '';
LifeEdit.Text := '';
DepreciationEdit.Text := '';
PeriodEdit.Text := '';
end;
procedure TSumYrsDepFrm.ComputeBtnClick(Sender: TObject);
VAR
Cost, Depreciation, Salvage : Extended;
Life, Period : integer;
begin
Cost := StrToFloat(CostEdit.Text);
Salvage := StrToFloat(SalvageEdit.Text);
Life := StrToInt(LifeEdit.Text);
Period := StrToInt(PeriodEdit.Text);
Depreciation := SYDDepreciation(Cost, Salvage, Life, Period);
DepreciationEdit.Text := FloatToStr(Depreciation);
end;
procedure TSumYrsDepFrm.FormShow(Sender: TObject);
begin
ResetBtnClick(self);
end;
procedure TSumYrsDepFrm.HelpBtnClick(Sender: TObject);
begin
ContextHelpForm.HelpMessage((Sender as TButton).tag);
end;
function TSumYrsDepFrm.SYDDepreciation(Cost, Salvage: Extended; Life, Period: Integer): Extended;
{ SYD = (cost - salvage) * (life - period + 1) / (life*(life + 1)/2) }
{ Note: life*(life+1)/2 = 1+2+3+...+life "sum of years"
The depreciation factor varies from life/sum_of_years in first period = 1
downto 1/sum_of_years in last period = life.
Total depreciation over life is cost-salvage.}
var
X1, X2: Extended;
begin
Result := 0;
if (Period < 1) or (Life < Period) or (Cost <= Salvage) then Exit;
X1 := 2 * (Life - Period + 1);
X2 := Life * (Life + 1);
Result := (Cost - Salvage) * X1 / X2
end;
initialization
{$I sumyrsdepunit.lrs}
end.