Skip to content

Commit

Permalink
feat: calculate price including discounts
Browse files Browse the repository at this point in the history
  • Loading branch information
salvadorbs committed Nov 29, 2023
1 parent 9314657 commit a58be6f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
33 changes: 27 additions & 6 deletions library/grocy.productstock.pas
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
interface

uses
Classes, SysUtils, mormot.core.json, mormot.core.rtti;
Classes, SysUtils, mormot.core.json, mormot.core.rtti, Lidl.ItemsLine;

type

Expand All @@ -20,8 +20,8 @@ TGrocyProductStock = class(TSynAutoCreateFields)
FShoppingLocationId: string;
FTransactionType: string;
public
constructor Create(Amount: string; BestBeforeDate: TDateTime; Price: string;
TransactionType: string; PurchasedDate: TDateTime; ShoppingLocationId: string); overload;
constructor Create(LidlProduct: TItemsLine; BestBeforeDate: TDateTime; TransactionType: string;
PurchasedDate: TDateTime; ShoppingLocationId: string); overload;
published
property Amount: string read FAmount write FAmount;
property BestBeforeDate: TDateTime read FBestBeforeDate write FBestBeforeDate;
Expand All @@ -35,16 +35,37 @@ implementation

{ TGrocyProductStock }

constructor TGrocyProductStock.Create(Amount: string; BestBeforeDate: TDateTime; Price: string;
constructor TGrocyProductStock.Create(LidlProduct: TItemsLine; BestBeforeDate: TDateTime;
TransactionType: string; PurchasedDate: TDateTime; ShoppingLocationId: string);
var
I: integer;
TotalDiscounts, CurrentUnitPrice, Total: Real;
fs: TFormatSettings;
begin
inherited Create;
FAmount := Amount;
FAmount := LidlProduct.Quantity;
FBestBeforeDate := Trunc(BestBeforeDate);
FPrice := StringReplace(Price, ',', '.', [rfReplaceAll]);
FShoppingLocationId := ShoppingLocationId;
FTransactionType := TransactionType;
FPurchasedDate := Trunc(PurchasedDate);

fs := DefaultFormatSettings;
fs.ThousandSeparator := '.';
fs.DecimalSeparator := ',';

TotalDiscounts := 0;
for I := 0 to length(LidlProduct.Discounts) - 1 do
TotalDiscounts := TotalDiscounts + StrToFloatDef(LidlProduct.Discounts[I].Amount, 0, fs);

CurrentUnitPrice := StrToFloatDef(LidlProduct.CurrentUnitPrice, 0, fs);

Total := CurrentUnitPrice;
if (CurrentUnitPrice >= TotalDiscounts) then
Total := CurrentUnitPrice - TotalDiscounts;

fs.ThousandSeparator := ',';
fs.DecimalSeparator := '.';
FPrice := FloatToStr(Total, fs);
end;

initialization
Expand Down
4 changes: 2 additions & 2 deletions library/kernel.application.pas
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ procedure TLidlToGrocy.AddGrocyProductInStock(const LidlProduct: TItemsLine;
if not (FNoStock) then
begin
TLogger.Info('Adding quantity (%s) in Grocy', [LidlProduct.Quantity]);
GrocyProductStock := TGrocyProductStock.Create(LidlProduct.Quantity,
IncDay(LidlTicket.Date, FConfiguration.GrocyDefaultBestBeforeDays), LidlProduct.CurrentUnitPrice,
GrocyProductStock := TGrocyProductStock.Create(LidlProduct,
IncDay(LidlTicket.Date, FConfiguration.GrocyDefaultBestBeforeDays),
'purchase', LidlTicket.Date, IntToStr(FConfiguration.GrocyShoppingLocationId));
try
if not FGrocyService.AddProductInStock(GrocyProduct.Id, GrocyProductStock) then
Expand Down

0 comments on commit a58be6f

Please sign in to comment.