Skip to content

Commit

Permalink
feat: added check stocked and consumed products (tickets.json)
Browse files Browse the repository at this point in the history
  • Loading branch information
salvadorbs committed Dec 4, 2023
1 parent 1225f86 commit 841d54a
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 30 deletions.
7 changes: 6 additions & 1 deletion LidlToGrocy.lpi
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<Unit>
<Filename Value="library\lidl.payments.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="lidl.Payments"/>
<UnitName Value="Lidl.Payments"/>
</Unit>
<Unit>
<Filename Value="library\lidl.currency.pas"/>
Expand Down Expand Up @@ -163,6 +163,11 @@
<IsPartOfProject Value="True"/>
<UnitName Value="Grocy.Error"/>
</Unit>
<Unit>
<Filename Value="library\kernel.ticket.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="Kernel.Ticket"/>
</Unit>
</Units>
</ProjectOptions>
<CompilerOptions>
Expand Down
60 changes: 41 additions & 19 deletions library/kernel.application.pas
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ TLidlToGrocy = class(TCustomApplication)
FConfiguration: TConfiguration;
FGrocyService: TGrocyService;

procedure AddGrocyProductInStock(const LidlProduct: TItemsLine; const GrocyProduct: TGrocyProduct;
const LidlTicket: TLidlTicket);
function AddGrocyProductInStock(const LidlProduct: TItemsLine; const GrocyProduct: TGrocyProduct;
const LidlTicket: TLidlTicket): boolean;
function AddNewGrocyProduct(LidlProduct: TItemsLine): TGrocyProduct;
procedure ConsumeGrocyProduct(LidlProduct: TItemsLine);
function ConsumeGrocyProduct(LidlProduct: TItemsLine): boolean;
procedure DoHelp(Sender: TObject);
function InsertOFFImageInGrocy(OFFProductInfo: TOFFProductInfo): boolean;
function GetGrocyProduct(LidlProduct: TItemsLine): TGrocyProduct;
Expand Down Expand Up @@ -82,7 +82,7 @@ TLidlToGrocy = class(TCustomApplication)
implementation

uses
OpenFoodFacts.Service, Grocy.ProductStock, DateUtils, Kernel.Logger;
OpenFoodFacts.Service, Grocy.ProductStock, DateUtils, Kernel.Logger, Kernel.Ticket;

{ TGrocyFastLidlAdder }

Expand Down Expand Up @@ -175,28 +175,33 @@ function TLidlToGrocy.AddNewGrocyProduct(LidlProduct: TItemsLine): TGrocyProduct
TLogger.InfoExit('Product inserted in Grocy. ID = %d', [GrocyProduct.Id]);
end;

procedure TLidlToGrocy.ConsumeGrocyProduct(LidlProduct: TItemsLine);
function TLidlToGrocy.ConsumeGrocyProduct(LidlProduct: TItemsLine): boolean;
begin
Result := False;

if (FConsumeNoW) then
begin
TLogger.Info('Consume product now', [LidlProduct.Quantity]);
FGrocyService.ConsumeByBarcode(LidlProduct.CodeInput, StrToInt(LidlProduct.Quantity));
Result := FGrocyService.ConsumeByBarcode(LidlProduct.CodeInput, StrToInt(LidlProduct.Quantity));
end;
end;

procedure TLidlToGrocy.AddGrocyProductInStock(const LidlProduct: TItemsLine;
const GrocyProduct: TGrocyProduct; const LidlTicket: TLidlTicket);
function TLidlToGrocy.AddGrocyProductInStock(const LidlProduct: TItemsLine; const GrocyProduct: TGrocyProduct;
const LidlTicket: TLidlTicket): boolean;
var
GrocyProductStock: TGrocyProductStock;
begin
Result := False;

if not (FNoStock) then
begin
TLogger.Info('Adding quantity (%s) in Grocy', [LidlProduct.Quantity]);
GrocyProductStock := TGrocyProductStock.Create(LidlProduct,
IncDay(LidlTicket.Date, FConfiguration.GrocyDefaultBestBeforeDays),
'purchase', LidlTicket.Date, IntToStr(FConfiguration.GrocyShoppingLocationId));
GrocyProductStock := TGrocyProductStock.Create(LidlProduct, IncDay(LidlTicket.Date,
FConfiguration.GrocyDefaultBestBeforeDays), 'purchase', LidlTicket.Date,
IntToStr(FConfiguration.GrocyShoppingLocationId));
try
if not FGrocyService.AddProductInStock(GrocyProduct.Id, GrocyProductStock) then
Result := FGrocyService.AddProductInStock(GrocyProduct.Id, GrocyProductStock);
if not Result then
TLogger.Error('Error adding quantity to product in Grocy', []);
finally
GrocyProductStock.Free;
Expand Down Expand Up @@ -255,6 +260,7 @@ procedure TLidlToGrocy.DoRun;
GrocyProduct: TGrocyProduct;
LidlProduct: TItemsLine;
Value: double;
Ticket: TTicket;
begin
if ((FGrocyApiKey = '') or (FLidlToken = '') or (FGrocyApiKey = '')) then
begin
Expand Down Expand Up @@ -283,11 +289,12 @@ procedure TLidlToGrocy.DoRun;

for LidlTicket in FLidlTickets do
begin
if (FConfiguration.LidlTickets.IndexOf(LidlTicket.BarCode) <> -1) then
Ticket := FConfiguration.FindTicket(LidlTicket.BarCode);
if (Ticket = nil) then
begin
TLogger.Info('LIDL receipt already inserted in Grocy (barcode %s). I move on to the next one',
[LidlTicket.BarCode]);
continue;
Ticket := TTicket.Create;
Ticket.Id := LidlTicket.BarCode;
FConfiguration.InsertTicket(Ticket);
end;

TLogger.InfoEnter('Started processing LIDL receipt (barcode %s)', [LidlTicket.BarCode]);
Expand All @@ -303,17 +310,31 @@ procedure TLidlToGrocy.DoRun;

if Assigned(GrocyProduct) then
begin
AddGrocyProductInStock(LidlProduct, GrocyProduct, LidlTicket);
ConsumeGrocyProduct(LidlProduct);
if (Ticket.StockedProducts.IndexOf(LidlProduct.CodeInput) = -1) then
begin
if AddGrocyProductInStock(LidlProduct, GrocyProduct, LidlTicket) then
Ticket.StockedProducts.Add(LidlProduct.CodeInput);
end
else
TLogger.Info('Product already added in Grocy Stock with this receipt', []);

if (Ticket.ConsumedProducts.IndexOf(LidlProduct.CodeInput) = -1) then
begin
if ConsumeGrocyProduct(LidlProduct) then
Ticket.ConsumedProducts.Add(LidlProduct.CodeInput);
end
else
TLogger.Info('Product already consumed in Grocy with this receipt', []);
end;

FConfiguration.SaveTickets;
finally
if Assigned(GrocyProduct) then
GrocyProduct.Free;
end;
TLogger.InfoExit('Completed processing', []);
end;

FConfiguration.LidlTickets.Add(LidlTicket.BarCode);
FConfiguration.SaveConfig;

TLogger.InfoExit('Completed processing', []);
Expand All @@ -334,6 +355,7 @@ constructor TLidlToGrocy.Create(TheOwner: TComponent);

FConfiguration := TConfiguration.Create;
FConfiguration.LoadConfig;
FConfiguration.LoadTickets;
end;

destructor TLidlToGrocy.Destroy;
Expand Down
67 changes: 57 additions & 10 deletions library/kernel.configuration.pas
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
interface

uses
Classes, SysUtils, jsonConf;
Classes, SysUtils, jsonConf, Kernel.Ticket, mormot.core.base;

type

Expand All @@ -21,7 +21,7 @@ TConfiguration = class
FGrocyQuIdPurchase: integer;
FGrocyQuIdStock: integer;
FGrocyShoppingLocationId: integer;
FLidlTickets: TStringList;
FLidlTickets: TTicketArray;

procedure RestoreSettings(AJSONConfig: TJSONConfig);
procedure SaveSettings(AJSONConfig: TJSONConfig);
Expand All @@ -39,10 +39,16 @@ TConfiguration = class
property GrocyQuIdPurchase: integer read FGrocyQuIdPurchase write FGrocyQuIdPurchase;
property GrocyQuIdConsume: integer read FGrocyQuIdConsume write FGrocyQuIdConsume;
property GrocyQuIdPrice: integer read FGrocyQuIdPrice write FGrocyQuIdPrice;
property LidlTickets: TStringList read FLidlTickets write FLidlTickets;
property LidlTickets: TTicketArray read FLidlTickets write FLidlTickets;

procedure InsertTicket(Ticket: TTicket);
function FindTicket(Id: string): TTicket;

procedure LoadConfig;
procedure SaveConfig;

procedure LoadTickets;
procedure SaveTickets;
end;

const
Expand All @@ -55,11 +61,13 @@ TConfiguration = class
CONFIG_GROCY_QUIDPURCHASE = 'grocy/qu_id_purchase';
CONFIG_GROCY_QUIDCONSUME = 'grocy/qu_id_consume';
CONFIG_GROCY_QUIDPRICE = 'grocy/qu_id_price';
CONFIG_LIDL_TICKETS = 'lidl/tickets';

implementation

{ TConfiguration }
uses
mormot.core.json, mormot.core.os;

{ TConfiguration }

procedure TConfiguration.RestoreSettings(AJSONConfig: TJSONConfig);
begin
Expand All @@ -75,7 +83,6 @@ procedure TConfiguration.RestoreSettings(AJSONConfig: TJSONConfig);
GrocyQuIdPurchase := AJSONConfig.GetValue(CONFIG_GROCY_QUIDPURCHASE, Self.GrocyQuIdPurchase);
GrocyQuIdConsume := AJSONConfig.GetValue(CONFIG_GROCY_QUIDCONSUME, Self.GrocyQuIdConsume);
GrocyQuIdPrice := AJSONConfig.GetValue(CONFIG_GROCY_QUIDPRICE, Self.GrocyQuIdPrice);
AJSONConfig.GetValue(CONFIG_LIDL_TICKETS, Self.LidlTickets, '');
end;

procedure TConfiguration.SaveSettings(AJSONConfig: TJSONConfig);
Expand All @@ -89,7 +96,6 @@ procedure TConfiguration.SaveSettings(AJSONConfig: TJSONConfig);
AJSONConfig.SetValue(CONFIG_GROCY_QUIDPURCHASE, Self.GrocyQuIdPurchase);
AJSONConfig.SetValue(CONFIG_GROCY_QUIDCONSUME, Self.GrocyQuIdConsume);
AJSONConfig.SetValue(CONFIG_GROCY_QUIDPRICE, Self.GrocyQuIdPrice);
AJSONConfig.SetValue(CONFIG_LIDL_TICKETS, Self.LidlTickets);
end;

constructor TConfiguration.Create;
Expand All @@ -103,17 +109,42 @@ constructor TConfiguration.Create;
FGrocyQuIdPurchase := 3;
FGrocyQuIdConsume := 2;
FGrocyQuIdPrice := 3;

FLidlTickets := TStringList.Create;
end;

destructor TConfiguration.Destroy;
var
I: integer;
begin
FLidlTickets.Free;
if Assigned(FLidlTickets) then
begin
for I := 0 to Length(FLidlTickets) - 1 do
FLidlTickets[I].Free;
SetLength(FLidlTickets, 0);
end;

inherited Destroy;
end;

procedure TConfiguration.InsertTicket(Ticket: TTicket);
begin
Insert(Ticket, FLidlTickets, 0);
end;

function TConfiguration.FindTicket(Id: string): TTicket;
var
Ticket: TTicket;
begin
Result := nil;
for Ticket in FLidlTickets do
begin
if (Ticket.Id = Id) then
begin
Result := Ticket;
break;
end;
end;
end;

procedure TConfiguration.LoadConfig;
var
JSONConfig: TJSONConfig;
Expand Down Expand Up @@ -146,4 +177,20 @@ procedure TConfiguration.SaveConfig;
end;
end;

procedure TConfiguration.LoadTickets;
var
json: RawUtf8;
begin
json := StringFromFile(IncludeTrailingPathDelimiter(GetCurrentDir) + 'tickets.json');
DynArrayLoadJson(FLidlTickets, json, TypeInfo(TTicketArray));
end;

procedure TConfiguration.SaveTickets;
var
json: RawUtf8;
begin
json := DynArraySaveJson(FLidlTickets, TypeInfo(TTicketArray));
JsonReformatToFile(json, IncludeTrailingPathDelimiter(GetCurrentDir) + 'tickets.json');
end;

end.
50 changes: 50 additions & 0 deletions library/kernel.ticket.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
unit Kernel.Ticket;

{$mode DelphiUnicode}

interface

uses
Classes, SysUtils, mormot.core.json;

type

{ TTicket }

TTicket = class(TSynAutoCreateFields)
private
FConsumedProducts: TStrings;
FId: string;
FStockedProducts: TStrings;
public
constructor Create; override;
destructor Destroy; override;
published
property Id: string read FId write FId;
property StockedProducts: TStrings read FStockedProducts write FStockedProducts;
property ConsumedProducts: TStrings read FConsumedProducts write FConsumedProducts;
end;

TTicketArray = array of TTicket;

implementation

{ TTicket }

constructor TTicket.Create;
begin
inherited Create;

FStockedProducts := TStringList.Create;
FConsumedProducts := TStringList.Create;
end;

destructor TTicket.Destroy;
begin
FStockedProducts.Free;
FConsumedProducts.Free;

inherited Destroy;
end;

end.

0 comments on commit 841d54a

Please sign in to comment.