sistemato assets, finito creazione company, inizio lavoro store
This commit is contained in:
@@ -1,42 +1,32 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flux/features/company/data/company_repository.dart';
|
||||
import 'package:flux/features/company/models/company_model.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
|
||||
part 'company_events.dart';
|
||||
part 'company_state.dart';
|
||||
|
||||
class CompanyBloc extends Bloc<CompanyEvent, CompanyState> {
|
||||
final _supabase = GetIt.instance<SupabaseClient>();
|
||||
|
||||
final CompanyRepository _repository = GetIt.I<CompanyRepository>();
|
||||
CompanyBloc() : super(const CompanyState(status: CompanyStatus.initial)) {
|
||||
on<SaveCompanyRequested>((event, emit) async {
|
||||
on<CreateCompanyRequested>((event, emit) async {
|
||||
emit(const CompanyState(status: CompanyStatus.loading));
|
||||
|
||||
try {
|
||||
// Recuperiamo l'ID utente corrente da Supabase Auth
|
||||
final userId = _supabase.auth.currentUser!.id;
|
||||
|
||||
await _supabase.from('company').insert({
|
||||
'user_id': userId,
|
||||
'ragione_sociale': event.ragioneSociale,
|
||||
'partita_iva': event.partitaIva,
|
||||
// Se il CF è vuoto, usa la P.IVA (logica salva-tempo per ditte individuali)
|
||||
'codice_fiscale': event.codiceFiscale.isEmpty
|
||||
? event.partitaIva
|
||||
: event.codiceFiscale,
|
||||
'codice_univoco': event.codiceUnivoco,
|
||||
'indirizzo': event.indirizzo,
|
||||
'cap': event.cap,
|
||||
'citta': event.citta,
|
||||
'provincia': event.provincia,
|
||||
'company_logo': event.companyLogo,
|
||||
'is_paid': false, // Di default partono con trial/non pagato
|
||||
});
|
||||
|
||||
emit(const CompanyState(status: CompanyStatus.success));
|
||||
final createdCompany = await _repository.createCompany(event.company);
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CompanyStatus.success,
|
||||
company: createdCompany,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(CompanyState(status: CompanyStatus.failure, error: e.toString()));
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CompanyStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -9,39 +9,11 @@ abstract class CompanyEvent extends Equatable {
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class SaveCompanyRequested extends CompanyEvent {
|
||||
final String ragioneSociale;
|
||||
final String partitaIva;
|
||||
final String codiceFiscale;
|
||||
final String codiceUnivoco;
|
||||
final String indirizzo;
|
||||
final String cap;
|
||||
final String citta;
|
||||
final String provincia;
|
||||
final String companyLogo;
|
||||
class CreateCompanyRequested extends CompanyEvent {
|
||||
final CompanyModel company;
|
||||
|
||||
const SaveCompanyRequested({
|
||||
required this.ragioneSociale,
|
||||
required this.partitaIva,
|
||||
required this.codiceFiscale,
|
||||
required this.codiceUnivoco,
|
||||
required this.indirizzo,
|
||||
required this.cap,
|
||||
required this.citta,
|
||||
required this.provincia,
|
||||
this.companyLogo = '', // Default vuoto come da schema SQL
|
||||
});
|
||||
const CreateCompanyRequested({required this.company});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
ragioneSociale,
|
||||
partitaIva,
|
||||
codiceFiscale,
|
||||
codiceUnivoco,
|
||||
indirizzo,
|
||||
cap,
|
||||
citta,
|
||||
provincia,
|
||||
companyLogo,
|
||||
];
|
||||
List<Object?> get props => [company];
|
||||
}
|
||||
|
||||
@@ -4,10 +4,23 @@ enum CompanyStatus { initial, loading, success, failure }
|
||||
|
||||
class CompanyState extends Equatable {
|
||||
final CompanyStatus status;
|
||||
final String? error;
|
||||
final String? errorMessage;
|
||||
final CompanyModel? company;
|
||||
|
||||
const CompanyState({required this.status, this.error});
|
||||
const CompanyState({required this.status, this.errorMessage, this.company});
|
||||
|
||||
CompanyState copyWith({
|
||||
CompanyStatus? status,
|
||||
String? errorMessage,
|
||||
CompanyModel? company,
|
||||
}) {
|
||||
return CompanyState(
|
||||
status: status ?? this.status,
|
||||
errorMessage: errorMessage ?? this.errorMessage,
|
||||
company: company ?? this.company,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [status, error];
|
||||
List<Object?> get props => [status, errorMessage];
|
||||
}
|
||||
|
||||
38
lib/features/company/data/company_repository.dart
Normal file
38
lib/features/company/data/company_repository.dart
Normal file
@@ -0,0 +1,38 @@
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
import '../models/company_model.dart';
|
||||
|
||||
class CompanyRepository {
|
||||
final SupabaseClient _supabase = Supabase.instance.client;
|
||||
|
||||
Future<CompanyModel> createCompany(CompanyModel company) async {
|
||||
try {
|
||||
// .select().single() trasforma la risposta nell'oggetto appena inserito
|
||||
final response = await _supabase
|
||||
.from('company')
|
||||
.insert(company.toJson())
|
||||
.select()
|
||||
.single();
|
||||
|
||||
return CompanyModel.fromJson(response);
|
||||
} on PostgrestException catch (e) {
|
||||
throw e.message;
|
||||
} catch (e) {
|
||||
throw 'Errore imprevisto durante la creazione dell\'azienda';
|
||||
}
|
||||
}
|
||||
|
||||
Future<CompanyModel?> getCompany() async {
|
||||
try {
|
||||
final userId = _supabase.auth.currentUser?.id;
|
||||
final response = await _supabase
|
||||
.from('company')
|
||||
.select()
|
||||
.eq('user_id', userId as Object)
|
||||
.maybeSingle();
|
||||
|
||||
return response != null ? CompanyModel.fromJson(response) : null;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import 'package:equatable/equatable.dart';
|
||||
|
||||
class CompanyModel extends Equatable {
|
||||
final String id;
|
||||
final DateTime createdAt;
|
||||
final DateTime? createdAt;
|
||||
final String userId;
|
||||
final String ragioneSociale;
|
||||
final String indirizzo;
|
||||
@@ -17,8 +17,8 @@ class CompanyModel extends Equatable {
|
||||
final String companyLogo;
|
||||
|
||||
const CompanyModel({
|
||||
required this.id,
|
||||
required this.createdAt,
|
||||
this.id = '',
|
||||
this.createdAt,
|
||||
required this.userId,
|
||||
required this.ragioneSociale,
|
||||
required this.indirizzo,
|
||||
@@ -28,7 +28,7 @@ class CompanyModel extends Equatable {
|
||||
required this.partitaIva,
|
||||
required this.codiceFiscale,
|
||||
required this.codiceUnivoco,
|
||||
required this.isPaid,
|
||||
this.isPaid = false,
|
||||
this.paymentExpiration,
|
||||
this.companyLogo = '',
|
||||
});
|
||||
|
||||
@@ -5,6 +5,9 @@ import 'package:flux/features/company/bloc/company_bloc.dart';
|
||||
import 'package:flux/core/blocs/session/session_bloc.dart';
|
||||
import 'package:flux/core/theme/theme.dart';
|
||||
import 'package:flux/core/widgets/flux_text_field.dart';
|
||||
import 'package:flux/features/company/models/company_model.dart';
|
||||
import 'package:flux/features/settings/settings.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
|
||||
class CreateCompanyScreen extends StatefulWidget {
|
||||
const CreateCompanyScreen({super.key});
|
||||
@@ -18,148 +21,197 @@ class CreateCompanyScreen extends StatefulWidget {
|
||||
class _CreateCompanyScreenState extends State<CreateCompanyScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
// Controller per ogni campo dello schema SQL
|
||||
// Controller per i campi obbligatori
|
||||
final _ragioneSocialeController = TextEditingController();
|
||||
final _pIvaController = TextEditingController();
|
||||
final _cfController = TextEditingController();
|
||||
final _sdiController = TextEditingController();
|
||||
final _indirizzoController = TextEditingController();
|
||||
final _capController = TextEditingController();
|
||||
final _cittaController = TextEditingController();
|
||||
final _provinciaController = TextEditingController();
|
||||
final _pIvaController = TextEditingController();
|
||||
final _cfController = TextEditingController();
|
||||
final _univocoController = TextEditingController();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
// Ricordati sempre di chiuderli!
|
||||
_ragioneSocialeController.dispose();
|
||||
_indirizzoController.dispose();
|
||||
_capController.dispose();
|
||||
_cittaController.dispose();
|
||||
_provinciaController.dispose();
|
||||
_pIvaController.dispose();
|
||||
_cfController.dispose();
|
||||
_univocoController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _onSave() {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
// Recuperiamo l'ID utente attuale da Supabase o dal SessionBloc
|
||||
final userId = GetIt.I.get<AppSettings>().currentUserId!;
|
||||
|
||||
final company = CompanyModel(
|
||||
userId: userId,
|
||||
ragioneSociale: _ragioneSocialeController.text.trim(),
|
||||
indirizzo: _indirizzoController.text.trim(),
|
||||
cap: _capController.text.trim(),
|
||||
citta: _cittaController.text.trim(),
|
||||
provincia: _provinciaController.text.trim(),
|
||||
partitaIva: _pIvaController.text.trim(),
|
||||
codiceFiscale: _cfController.text.trim(),
|
||||
codiceUnivoco: _univocoController.text.trim().toUpperCase(),
|
||||
// Gli altri campi hanno i default nel modello
|
||||
);
|
||||
|
||||
// Spariamo l'evento al Bloc
|
||||
context.read<CompanyBloc>().add(CreateCompanyRequested(company: company));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (context) => CompanyBloc(),
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Configurazione Azienda'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.logout_rounded),
|
||||
onPressed: () {
|
||||
// Qui chiami il tuo Bloc dell'autenticazione per fare logout
|
||||
// Esempio se hai un AuthBloc o SessionBloc:
|
||||
context.read<AuthBloc>().add(LogoutRequested());
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Configurazione Azienda'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.logout_rounded),
|
||||
onPressed: () {
|
||||
// Qui chiami il tuo Bloc dell'autenticazione per fare logout
|
||||
// Esempio se hai un AuthBloc o SessionBloc:
|
||||
context.read<AuthBloc>().add(LogoutRequested());
|
||||
|
||||
// Se vuoi solo tornare brutalmente alla login per testare il logo:
|
||||
// Navigator.of(context).pushReplacementNamed('/login');
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: BlocConsumer<CompanyBloc, CompanyState>(
|
||||
listener: (context, state) {
|
||||
if (state.status == CompanyStatus.success) {
|
||||
context.read<SessionBloc>().add(AppStarted());
|
||||
}
|
||||
},
|
||||
builder: (context, state) {
|
||||
return SafeArea(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(24.0),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildHeader(context),
|
||||
const SizedBox(height: 32),
|
||||
// Se vuoi solo tornare brutalmente alla login per testare il logo:
|
||||
// Navigator.of(context).pushReplacementNamed('/login');
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: BlocConsumer<CompanyBloc, CompanyState>(
|
||||
listener: (context, state) {
|
||||
if (state.status == CompanyStatus.success && state.company != null) {
|
||||
// 1. Aggiorniamo la singleton con i dati reali (ID incluso)
|
||||
GetIt.I.get<AppSettings>().setCurrentCompany(state.company);
|
||||
|
||||
// --- SEZIONE 1: IDENTITÀ FISCALE ---
|
||||
_SectionTitle(title: 'DATI FISCALI'),
|
||||
const SizedBox(height: 16),
|
||||
FluxTextField(
|
||||
label: 'Ragione Sociale',
|
||||
icon: Icons.business,
|
||||
controller: _ragioneSocialeController,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: FluxTextField(
|
||||
label: 'Partita IVA',
|
||||
icon: Icons.numbers,
|
||||
controller: _pIvaController,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: FluxTextField(
|
||||
label: 'Codice Fiscale',
|
||||
icon: Icons.badge_outlined,
|
||||
controller: _cfController,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FluxTextField(
|
||||
label: 'Codice Univoco (SDI) / PEC',
|
||||
icon: Icons.send_and_archive_outlined,
|
||||
controller: _sdiController,
|
||||
),
|
||||
// 2. Notifichiamo il SessionBloc per cambiare pagina
|
||||
context.read<SessionBloc>().add(AppStarted());
|
||||
}
|
||||
|
||||
const SizedBox(height: 32),
|
||||
|
||||
// --- SEZIONE 2: SEDE LEGALE ---
|
||||
_SectionTitle(title: 'SEDE LEGALE'),
|
||||
const SizedBox(height: 16),
|
||||
FluxTextField(
|
||||
label: 'Indirizzo e n. civico',
|
||||
icon: Icons.home_work_outlined,
|
||||
controller: _indirizzoController,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
flex: 2,
|
||||
child: FluxTextField(
|
||||
label: 'Città',
|
||||
icon: Icons.location_city,
|
||||
controller: _cittaController,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: FluxTextField(
|
||||
label: 'CAP',
|
||||
icon: Icons.map_outlined,
|
||||
controller: _capController,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: FluxTextField(
|
||||
label: 'Prov',
|
||||
icon: Icons.explore_outlined,
|
||||
controller: _provinciaController,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 32),
|
||||
|
||||
// --- SEZIONE 3: LOGO AZIENDALE ---
|
||||
_SectionTitle(title: 'BRANDING'),
|
||||
const SizedBox(height: 16),
|
||||
_buildLogoPicker(context),
|
||||
|
||||
const SizedBox(height: 48),
|
||||
|
||||
// --- BOTTONE INVIO ---
|
||||
_buildSubmitButton(context, state),
|
||||
],
|
||||
),
|
||||
if (state.status == CompanyStatus.failure) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
state.errorMessage ?? 'Errore durante il salvataggio',
|
||||
),
|
||||
backgroundColor: Colors.redAccent,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
}
|
||||
},
|
||||
builder: (context, state) {
|
||||
return SafeArea(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(24.0),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildHeader(context),
|
||||
const SizedBox(height: 32),
|
||||
|
||||
// --- SEZIONE 1: IDENTITÀ FISCALE ---
|
||||
_SectionTitle(title: 'DATI FISCALI'),
|
||||
const SizedBox(height: 16),
|
||||
FluxTextField(
|
||||
label: 'Ragione Sociale',
|
||||
icon: Icons.business,
|
||||
controller: _ragioneSocialeController,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: FluxTextField(
|
||||
label: 'Partita IVA',
|
||||
icon: Icons.numbers,
|
||||
controller: _pIvaController,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: FluxTextField(
|
||||
label: 'Codice Fiscale',
|
||||
icon: Icons.badge_outlined,
|
||||
controller: _cfController,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FluxTextField(
|
||||
label: 'Codice Univoco (SDI) / PEC',
|
||||
icon: Icons.send_and_archive_outlined,
|
||||
controller: _univocoController,
|
||||
),
|
||||
|
||||
const SizedBox(height: 32),
|
||||
|
||||
// --- SEZIONE 2: SEDE LEGALE ---
|
||||
_SectionTitle(title: 'SEDE LEGALE'),
|
||||
const SizedBox(height: 16),
|
||||
FluxTextField(
|
||||
label: 'Indirizzo e n. civico',
|
||||
icon: Icons.home_work_outlined,
|
||||
controller: _indirizzoController,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
flex: 2,
|
||||
child: FluxTextField(
|
||||
label: 'Città',
|
||||
icon: Icons.location_city,
|
||||
controller: _cittaController,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: FluxTextField(
|
||||
label: 'CAP',
|
||||
icon: Icons.map_outlined,
|
||||
controller: _capController,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: FluxTextField(
|
||||
label: 'Prov',
|
||||
icon: Icons.explore_outlined,
|
||||
controller: _provinciaController,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 32),
|
||||
|
||||
// --- SEZIONE 3: LOGO AZIENDALE ---
|
||||
_SectionTitle(title: 'BRANDING'),
|
||||
const SizedBox(height: 16),
|
||||
_buildLogoPicker(context),
|
||||
|
||||
const SizedBox(height: 48),
|
||||
|
||||
// --- BOTTONE INVIO ---
|
||||
_buildSubmitButton(context, state),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -207,7 +259,7 @@ class _CreateCompanyScreenState extends State<CreateCompanyScreen> {
|
||||
child: ElevatedButton(
|
||||
onPressed: state.status == CompanyStatus.loading
|
||||
? null
|
||||
: () => _submit(context),
|
||||
: () => _onSave(),
|
||||
child: state.status == CompanyStatus.loading
|
||||
? const CircularProgressIndicator()
|
||||
: const Text('SALVA AZIENDA'),
|
||||
@@ -215,23 +267,6 @@ class _CreateCompanyScreenState extends State<CreateCompanyScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
void _submit(BuildContext context) {
|
||||
// Qui chiameremo il Bloc passando tutti i dati raccolti dai controller
|
||||
context.read<CompanyBloc>().add(
|
||||
SaveCompanyRequested(
|
||||
ragioneSociale: _ragioneSocialeController.text,
|
||||
partitaIva: _pIvaController.text,
|
||||
codiceFiscale: _cfController.text,
|
||||
codiceUnivoco: _sdiController.text,
|
||||
indirizzo: _indirizzoController.text,
|
||||
cap: _capController.text,
|
||||
citta: _cittaController.text,
|
||||
provincia: _provinciaController.text,
|
||||
companyLogo: '', // Per ora vuoto come da accordi
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildHeader(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
|
||||
Reference in New Issue
Block a user