Skip to content

Commit

Permalink
feat: can add and edit billing info #298
Browse files Browse the repository at this point in the history
  • Loading branch information
bvlourenco committed Dec 2, 2022
1 parent 454c005 commit f4f58aa
Show file tree
Hide file tree
Showing 8 changed files with 492 additions and 90 deletions.
39 changes: 23 additions & 16 deletions backend/src/mongodb/company.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,10 +801,10 @@ func (c *CompaniesType) UpdateCompanyParticipationStatus(companyID primitive.Obj

// UpdateCompanyData is the data used to update a company, using the method UpdateCompany.
type UpdateCompanyData struct {
Name string
Description string
Site string
BillingInfo models.CompanyBillingInfo
Name *string
Description *string
Site *string
BillingInfo *models.CompanyBillingInfo
}

// ParseBody fills the UpdateCompanyData from a body
Expand All @@ -814,10 +814,6 @@ func (ucd *UpdateCompanyData) ParseBody(body io.Reader) error {
return err
}

if len(ucd.Name) == 0 {
return errors.New("Invalid name")
}

return nil
}

Expand All @@ -827,15 +823,26 @@ func (c *CompaniesType) UpdateCompany(companyID primitive.ObjectID, data UpdateC
ctx := context.Background()
var updatedCompany models.Company

updateFields := bson.M{}

if data.Name != nil {
updateFields["name"] = *data.Name
}
if data.Description != nil {
updateFields["description"] = *data.Description
}
if data.Site != nil {
updateFields["site"] = *data.Site
}
if data.BillingInfo != nil {
billingInfo := *data.BillingInfo
updateFields["billingInfo.name"] = billingInfo.Name
updateFields["billingInfo.address"] = billingInfo.Address
updateFields["billingInfo.tin"] = billingInfo.TIN
}

var updateQuery = bson.M{
"$set": bson.M{
"name": data.Name,
"description": data.Description,
"site": data.Site,
"billingInfo.name": data.BillingInfo.Name,
"billingInfo.address": data.BillingInfo.Address,
"billingInfo.tin": data.BillingInfo.TIN,
},
"$set": updateFields,
}

var filterQuery = bson.M{"_id": companyID}
Expand Down
33 changes: 22 additions & 11 deletions backend/src/router/company_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1110,11 +1110,15 @@ func TestUpdateCompany(t *testing.T) {
newCompany, err := mongodb.Companies.CreateCompany(createCompanyData)
assert.NilError(t, err)

name := "some other name"
description := "some other description"
site := "some other site"

ucd := &mongodb.UpdateCompanyData{
Name: "some other name",
Description: "some other description",
Site: "some site",
BillingInfo: models.CompanyBillingInfo{
Name: &name,
Description: &description,
Site: &site,
BillingInfo: &models.CompanyBillingInfo{
Name: "some billing name",
Address: "some billing address",
TIN: "some billing tin",
Expand Down Expand Up @@ -1161,10 +1165,13 @@ func TestUpdateCompanyInvalidPayload(t *testing.T) {
newCompany, err := mongodb.Companies.CreateCompany(createCompanyData)
assert.NilError(t, err)

description := "some other description"
site := "some other site"

ucd := &mongodb.UpdateCompanyData{
Description: "some other description",
Site: "some site",
BillingInfo: models.CompanyBillingInfo{
Description: &description,
Site: &site,
BillingInfo: &models.CompanyBillingInfo{
Name: "some billing name",
Address: "some billing address",
TIN: "some billing tin",
Expand All @@ -1190,11 +1197,15 @@ func TestUpdateCompanyNotFound(t *testing.T) {
log.Fatal(err)
}

name := "some other name"
description := "some other description"
site := "some other site"

ucd := &mongodb.UpdateCompanyData{
Name: "some other name",
Description: "some other description",
Site: "some site",
BillingInfo: models.CompanyBillingInfo{
Name: &name,
Description: &description,
Site: &site,
BillingInfo: &models.CompanyBillingInfo{
Name: "some billing name",
Address: "some billing address",
TIN: "some billing tin",
Expand Down
47 changes: 0 additions & 47 deletions frontend/lib/routes/company/BillingInfoScreen.dart

This file was deleted.

25 changes: 10 additions & 15 deletions frontend/lib/routes/company/CompanyScreen.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'package:flutter/material.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:frontend/components/threads/addThreadForm.dart';
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/BillingInfoScreen.dart';
import 'package:frontend/routes/company/billing/AddBillingInfoForm.dart';
import 'package:frontend/routes/company/billing/BillingScreen.dart';
import 'package:frontend/routes/company/CompanyTableNotifier.dart';
import 'package:frontend/routes/company/DetailsScreen.dart';
import 'package:frontend/routes/company/ParticipationList.dart';
Expand Down Expand Up @@ -99,19 +101,8 @@ class _CompanyScreenState extends State<CompanyScreen>
int index = _tabController.index;
switch (index) {
case 0:
return null;
case 1:
return FloatingActionButton.extended(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
Container(child: Text("In Progress..."))));
},
label: const Text('Add Billing Info'),
icon: const Icon(Icons.add),
);
return null;
case 2:
{
if (widget.company.lastParticipation != latestEvent) {
Expand Down Expand Up @@ -168,7 +159,7 @@ class _CompanyScreenState extends State<CompanyScreen>
controller: _tabController,
tabs: [
Tab(text: 'Details'),
Tab(text: 'BillingInfo'),
Tab(text: 'Billing'),
Tab(text: 'Participations'),
Tab(text: 'Communications'),
],
Expand All @@ -178,7 +169,11 @@ class _CompanyScreenState extends State<CompanyScreen>
DetailsScreen(
company: widget.company,
),
BillingInfoScreen(),
BillingScreen(
participations: widget.company.participations,
billingInfo: widget.company.billingInfo,
id: widget.company.id,
),
ParticipationList(
company: widget.company,
onParticipationChanged:
Expand Down
154 changes: 154 additions & 0 deletions frontend/lib/routes/company/billing/AddBillingInfoForm.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:frontend/components/appbar.dart';
import 'package:frontend/models/company.dart';
import 'package:frontend/routes/company/CompanyTableNotifier.dart';
import 'package:frontend/services/companyService.dart';
import 'package:provider/provider.dart';

class AddBillingInfoForm extends StatefulWidget {
final String id;
final void Function(CompanyBillingInfo) onBillingInfoEdit;

AddBillingInfoForm({Key? key, required this.id, required this.onBillingInfoEdit})
: super(key: key);

@override
_AddBillingInfoFormState createState() => _AddBillingInfoFormState();
}

class _AddBillingInfoFormState extends State<AddBillingInfoForm> {
final _formKey = GlobalKey<FormState>();

final _nameController = TextEditingController();
final _addressController = TextEditingController();
final _tinController = TextEditingController();

final _companyService = CompanyService();

void _submit() async {
if (_formKey.currentState!.validate()) {
var name = _nameController.text;
var address = _addressController.text;
var tin = _tinController.text;

ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Creating Billing Info...')),
);

CompanyBillingInfo bi =
new CompanyBillingInfo(name: name, address: address, tin: tin);

Company? c =
await _companyService.updateCompany(id: widget.id, billingInfo: bi);

if (c != null) {
CompanyTableNotifier notifier =
Provider.of<CompanyTableNotifier>(context, listen: false);
notifier.edit(c);

widget.onBillingInfoEdit(bi);

ScaffoldMessenger.of(context).hideCurrentSnackBar();

ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Done'),
duration: Duration(seconds: 2),
),
);
} else {
ScaffoldMessenger.of(context).hideCurrentSnackBar();

ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('An error occured.')),
);
}
Navigator.pop(context);
}
}

Widget _buildForm() {
return Form(
key: _formKey,
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: _nameController,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter the name of billing info';
}
return null;
},
decoration: const InputDecoration(
icon: const Icon(Icons.title),
labelText: "Billing Info name *",
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: _addressController,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter the address of billing info';
}
return null;
},
decoration: const InputDecoration(
icon: const Icon(Icons.title),
labelText: "Billing Info address *",
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: _tinController,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter the tin (NIF/Contribuinte) of billing info';
}
return null;
},
decoration: const InputDecoration(
icon: const Icon(Icons.title),
labelText: "Billing Info tin (NIF/Contribuinte) *",
),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () => _submit(),
child: const Text('Submit'),
),
),
],
),
);
}

@override
Widget build(BuildContext context) {
CustomAppBar appBar = CustomAppBar(
disableEventChange: true,
);
return Scaffold(
body: Stack(children: [
Container(
margin: EdgeInsets.fromLTRB(0, appBar.preferredSize.height, 0, 0),
child: _buildForm()),
appBar,
]),
);
}
}
Loading

0 comments on commit f4f58aa

Please sign in to comment.