Skip to content

Commit

Permalink
feat: can add and see billings #298
Browse files Browse the repository at this point in the history
  • Loading branch information
bvlourenco committed Dec 28, 2022
1 parent e08e865 commit 41860c8
Show file tree
Hide file tree
Showing 12 changed files with 459 additions and 88 deletions.
10 changes: 3 additions & 7 deletions backend/src/mongodb/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ func (cbd *CreateBillingData) ParseBody(body io.Reader) error {
return errors.New("invalid emission time")
}

if cbd.Notes == nil {
return errors.New("invalid notes")
}

if cbd.Event == nil {
return errors.New("invalid event")
}
Expand Down Expand Up @@ -271,16 +267,16 @@ func (b *BillingsType) DeleteBilling(id primitive.ObjectID) (*models.Billing, er

billing, err := b.GetBilling(id)
if err != nil {
return nil, err
return billing, err
}

deleteResult, err := b.Collection.DeleteOne(ctx, bson.M{"_id": id})
if err != nil {
return nil, err
return billing, err
}

if deleteResult.DeletedCount != 1 {
return nil, fmt.Errorf("should have deleted 1 billing, deleted %v", deleteResult.DeletedCount)
return billing, fmt.Errorf("should have deleted 1 billing, deleted %v", deleteResult.DeletedCount)
}

return billing, nil
Expand Down
9 changes: 2 additions & 7 deletions backend/src/mongodb/company.go
Original file line number Diff line number Diff line change
Expand Up @@ -1156,14 +1156,9 @@ func (c *CompaniesType) UpdatePackage(companyID primitive.ObjectID, packageID pr

// UpdateBilling updates the billing of a company's participation related to the current event.
// Uses a models.Billing ID to store this information.
func (c *CompaniesType) UpdateBilling(companyID primitive.ObjectID, billingID primitive.ObjectID) (*models.Company, error) {
func (c *CompaniesType) UpdateBilling(companyID primitive.ObjectID, billingID primitive.ObjectID, event int) (*models.Company, error) {

ctx := context.Background()
currentEvent, err := Events.GetCurrentEvent()

if err != nil {
return nil, err
}

var updatedCompany models.Company

Expand All @@ -1173,7 +1168,7 @@ func (c *CompaniesType) UpdateBilling(companyID primitive.ObjectID, billingID pr
},
}

var filterQuery = bson.M{"_id": companyID, "participations.event": currentEvent.ID}
var filterQuery = bson.M{"_id": companyID, "participations.event": event}

var optionsQuery = options.FindOneAndUpdate()
optionsQuery.SetReturnDocument(options.After)
Expand Down
6 changes: 3 additions & 3 deletions backend/src/router/company.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ func addCompanyParticipationBilling(w http.ResponseWriter, r *http.Request) {
return
}

updatedCompany, err := mongodb.Companies.UpdateBilling(companyID, newBilling.ID)
updatedCompany, err := mongodb.Companies.UpdateBilling(companyID, newBilling.ID, newBilling.Event)
if err != nil {
http.Error(w, "Could not update company's billing", http.StatusExpectationFailed)

Expand Down Expand Up @@ -604,7 +604,7 @@ func deleteCompanyParticipationBilling(w http.ResponseWriter, r *http.Request) {
return
}

_, err = mongodb.Billings.DeleteBilling(billingID)
bill, err := mongodb.Billings.DeleteBilling(billingID)
if err != nil {
http.Error(w, "Billing not found"+err.Error(), http.StatusNotFound)
return
Expand All @@ -618,7 +618,7 @@ func deleteCompanyParticipationBilling(w http.ResponseWriter, r *http.Request) {
}

// create deleted billing
if _, err := mongodb.Companies.UpdateBilling(companyID, billingID); err != nil {
if _, err := mongodb.Companies.UpdateBilling(companyID, billingID, bill.Event); err != nil {
log.Printf("error adding billing to company participation: %s\n", err.Error())
}

Expand Down
2 changes: 1 addition & 1 deletion backend/src/router/company_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ func TestAddCompanyPackage(t *testing.T) {

var packageID = updatedCompany.Participations[0].Package

createdPackage, err := mongodb.Packages.GetPackage(packageID)
createdPackage, err := mongodb.Packages.GetPackage(*packageID)
assert.NilError(t, err)

assert.Equal(t, createdPackage.Name, name)
Expand Down
17 changes: 15 additions & 2 deletions frontend/lib/models/billing.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:convert';

import 'package:intl/intl.dart';

class BillingStatus {
final bool proForma;
final bool invoice;
Expand Down Expand Up @@ -64,7 +66,7 @@ class Billing {
company: json['company'],
value: json['value'],
invoiceNumber: json['invoiceNumber'],
emission: DateTime(json['emission']),
emission: DateTime.parse(json['emission']),
notes: json['notes'],
visible: json['visible'],
);
Expand All @@ -84,6 +86,17 @@ class Billing {

@override
String toString() {
return json.encode(this.toJson());
String repr = "";
Map<String, dynamic> bill = this.toJson();
bill.forEach((key, value) {
repr += key + ' ';
if (value != DateTime) {
repr += value.toString();
} else {
repr += DateFormat('yyyy-MM-dd HH:mm').format(value);
}
repr += '\n';
});
return repr;
}
}
17 changes: 16 additions & 1 deletion frontend/lib/routes/company/CompanyScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:frontend/components/appbar.dart';
import 'package:frontend/components/eventNotifier.dart';
import 'package:frontend/components/threads/participations/communicationsList.dart';
import 'package:frontend/models/company.dart';
import 'package:frontend/routes/company/billing/AddBillingForm.dart';
import 'package:frontend/routes/company/billing/BillingScreen.dart';
import 'package:frontend/routes/company/CompanyTableNotifier.dart';
import 'package:frontend/routes/company/DetailsScreen.dart';
Expand Down Expand Up @@ -98,8 +99,22 @@ class _CompanyScreenState extends State<CompanyScreen>
int index = _tabController.index;
switch (index) {
case 0:
case 1:
return null;
case 1:
return FloatingActionButton.extended(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddBillingForm(
id: widget.company.id,
onEditCompany: (context, _company) {
companyChangedCallback(context, company: _company);
})));
},
label: const Text('Add new Billing'),
icon: const Icon(Icons.add),
);
case 2:
{
if (widget.company.lastParticipation != latestEvent) {
Expand Down
Loading

0 comments on commit 41860c8

Please sign in to comment.