Changeset 9 for trunk/SpotPrice.pas


Ignore:
Timestamp:
Apr 14, 2026, 12:57:08 PM (2 weeks ago)
Author:
chronos
Message:
  • Modified: Loading of spot prices and calculation prices for each EAN.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SpotPrice.pas

    r7 r9  
    44
    55uses
    6   Classes, SysUtils, Generics.Collections, XML, fphttpclient, opensslsockets,
    7   DOM;
     6  Classes, SysUtils, Generics.Collections, Generics.Defaults, XML,
     7  fphttpclient, opensslsockets, DOM, Common, DateUtils, CsvDocument;
    88
    99type
     
    1212  TSpotPrice = record
    1313    Time: TDateTime;
    14     Value: Double;
    15     class function Create(Time: TDateTime; Value: Double): TSpotPrice; static;
     14    Value: Currency;
     15    class function Create(Time: TDateTime; Value: Currency): TSpotPrice; static;
    1616    procedure LoadFromXmlNode(Node: TDOMNode);
    1717    procedure SaveToXmlNode(Node: TDOMNode);
     
    2121
    2222  TSpotPrices = class(TList<TSpotPrice>)
     23  private
     24    function FileNameFilter(FileName: string): Boolean;
     25    function Comparer(constref Left, Right: TSpotPrice): Integer;
     26    procedure LoadSpotReport(FileName: string);
    2327  public
    2428    function GetBlock(var Text: string; StartText, EndText: string): string;
    25     procedure LoadSpotPrices(Date: TDate);
     29    procedure LoadSpotPricesFromWeb(Date: TDate);
     30    procedure Import(Directory: string);
    2631    procedure LoadFromXmlNode(Node: TDOMNode);
    2732    procedure SaveToXmlNode(Node: TDOMNode);
     33    function SearchByTime(Time: TDateTime): TSpotPrice;
    2834  end;
    2935
     
    3137  SpotPriceName = 'SpotPrice';
    3238  SpotPricesName = 'SpotPrices';
     39  DPH = 1.21;
     40
    3341
    3442implementation
     
    3644{ TSpotPrice }
    3745
    38 class function TSpotPrice.Create(Time: TDateTime; Value: Double): TSpotPrice;
     46class function TSpotPrice.Create(Time: TDateTime; Value: Currency): TSpotPrice;
    3947begin
    4048  Result.Time := Time;
     
    5462end;
    5563
    56 procedure TSpotPrices.LoadSpotPrices(Date: TDate);
     64procedure TSpotPrices.LoadSpotPricesFromWeb(Date: TDate);
    5765var
    5866  URL: string;
     
    8290            ValueText := StringReplace(ValueText, 'Kč', '', [rfReplaceAll]).Trim;
    8391            ValueText := StringReplace(ValueText, Chr($c2) + Chr($a0), '', [rfReplaceAll]);
    84             Value := StrToInt(ValueText) / 1000;
     92            Value := StrToInt(ValueText) / 1000 * DPH;
    8593          end;
    8694
     
    94102end;
    95103
     104function TSpotPrices.FileNameFilter(FileName: string): Boolean;
     105begin
     106  Result := ExtractFileExt(FileName) = '.csv';
     107end;
     108
     109function TSpotPrices.Comparer(constref Left, Right: TSpotPrice): Integer;
     110begin
     111  Result := CompareDateTime(Left.Time, Right.Time);
     112end;
     113
     114procedure TSpotPrices.LoadSpotReport(FileName: string);
     115var
     116  CSVDoc: TCSVDocument;
     117  R: Integer;
     118  Date, Time: TDateTime;
     119  Value: Currency;
     120begin
     121  CSVDoc := TCSVDocument.Create;
     122  try
     123    CSVDoc.LoadFromFile(FileName);
     124
     125    for R := 1 to CSVDoc.RowCount - 1 do begin
     126      Date := StrToDate(CSVDoc.Cells[0, R]);
     127      Time := Date + StrToTime(Copy(CSVDoc.Cells[1, R], 1, Pos(' ', CSVDoc.Cells[1, R]) - 1));
     128      Value := StrToCurr(CSVDoc.Cells[3, R]) / 1000;
     129      Add(TSpotPrice.Create(Time, Value));
     130    end;
     131  finally
     132    CSVDoc.Free;
     133  end;
     134end;
     135
     136procedure TSpotPrices.Import(Directory: string);
     137var
     138  Reports: TStringList;
     139  I: Integer;
     140begin
     141  Clear;
     142
     143  Reports := TStringList.Create;
     144  try
     145    SearchFiles(Reports, Directory, FileNameFilter);
     146    for I := 0 to Reports.Count - 1 do
     147      LoadSpotReport(Reports[I]);
     148  finally
     149    Reports.Free;
     150  end;
     151
     152  Sort(TComparer<TSpotPrice>.Construct(Comparer));
     153end;
     154
    96155procedure TSpotPrices.LoadFromXmlNode(Node: TDOMNode);
    97156var
     
    117176    Node.AppendChild(Node2);
    118177  end;
     178end;
     179
     180function TSpotPrices.SearchByTime(Time: TDateTime): TSpotPrice;
     181var
     182  I: Integer;
     183begin
     184  I := 0;
     185  while (I < Count) and (Items[I].Time <> Time) do Inc(I);
     186  if I < Count then Result := Items[I]
     187    else Result := TSpotPrice.Create(0, 0);
    119188end;
    120189
     
    136205end;
    137206
    138 
    139207end.
    140208
Note: See TracChangeset for help on using the changeset viewer.