Files
fcs/lib/pages/invoice/invoice_info.dart

215 lines
5.8 KiB
Dart
Raw Permalink Normal View History

2024-09-22 16:49:59 +06:30
import 'package:fcs/constants.dart';
2020-10-18 02:38:46 +06:30
import 'package:fcs/domain/entities/carton.dart';
2020-10-14 20:56:46 +06:30
import 'package:fcs/domain/entities/invoice.dart';
import 'package:fcs/helpers/theme.dart';
2020-10-26 04:41:24 +06:30
import 'package:fcs/pages/carton/model/carton_model.dart';
import 'package:fcs/pages/invoice/editor/invoice_carton_table.dart';
import 'package:fcs/pages/invoice/invoice_table.dart';
import 'package:fcs/pages/invoice/model/invoice_model.dart';
import 'package:fcs/pages/invoice/widgets.dart';
2020-10-14 20:56:46 +06:30
import 'package:fcs/pages/main/util.dart';
2020-10-15 03:06:13 +06:30
import 'package:fcs/pages/rates/model/shipment_rate_model.dart';
2020-10-14 20:56:46 +06:30
import 'package:fcs/pages/widgets/display_text.dart';
2020-10-26 04:41:24 +06:30
import 'package:fcs/pages/widgets/fcs_icons.dart';
import 'package:fcs/pages/widgets/local_app_bar.dart';
2020-10-26 04:41:24 +06:30
import 'package:fcs/pages/widgets/local_button.dart';
2020-10-14 20:56:46 +06:30
import 'package:fcs/pages/widgets/progress.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
2020-10-26 04:41:24 +06:30
class InvoiceInfo extends StatefulWidget {
2021-09-10 12:00:08 +06:30
final Invoice? invoice;
final bool? forCustomer;
2020-10-28 06:35:47 +06:30
InvoiceInfo({this.invoice, this.forCustomer});
2020-10-14 20:56:46 +06:30
@override
2020-10-26 04:41:24 +06:30
_InvoiceInfoState createState() => _InvoiceInfoState();
2020-10-14 20:56:46 +06:30
}
2020-10-26 04:41:24 +06:30
class _InvoiceInfoState extends State<InvoiceInfo> {
2020-10-14 20:56:46 +06:30
var dateFormatter = new DateFormat('dd MMM yyyy');
2021-09-10 12:00:08 +06:30
Invoice? _invoice;
2020-10-14 20:56:46 +06:30
bool _isLoading = false;
2020-10-26 04:41:24 +06:30
bool _showCartons = false;
2020-10-14 20:56:46 +06:30
@override
void initState() {
super.initState();
2021-09-10 12:00:08 +06:30
_invoice = widget.invoice!;
2021-09-10 17:14:59 +06:30
_invoice!.shipments!.forEach((s) {
s!.isSelected = true;
2020-10-26 04:41:24 +06:30
});
_loadCartons();
}
2020-10-14 20:56:46 +06:30
2020-10-26 04:41:24 +06:30
_loadCartons() async {
CartonModel cartonModel = Provider.of<CartonModel>(context, listen: false);
List<Carton> cartons = [];
for (var c in _invoice?.cartons ?? []) {
var _carton = await cartonModel.getCarton(c.id);
2024-02-09 13:35:32 +06:30
if (_carton != null) {
_carton.isChecked = true;
cartons.add(_carton);
}
2020-10-14 20:56:46 +06:30
}
2020-10-26 04:41:24 +06:30
setState(() {
2021-09-10 12:00:08 +06:30
_invoice!.cartons = cartons;
2020-10-26 04:41:24 +06:30
});
2020-10-14 20:56:46 +06:30
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
2021-09-10 12:00:08 +06:30
bool isCanceled = _invoice!.status == invoice_cancel_status;
bool isPaid = _invoice!.status == invoice_paid_status;
2020-10-26 04:41:24 +06:30
var rateModel = Provider.of<ShipmentRateModel>(context);
var rate = rateModel.rate;
final cartonTable = InvoiceCartonTable(
2021-09-10 12:00:08 +06:30
cartons: _invoice!.cartons,
2020-10-26 04:41:24 +06:30
rate: rate,
);
final invoiceTableBox = InvoiceTable(
2021-09-10 12:00:08 +06:30
invoice: _invoice!,
2020-10-26 04:41:24 +06:30
rate: rate,
deliveryFeeSelected: (selected) {
setState(() {
if (selected) {
2021-09-10 12:00:08 +06:30
_invoice!.deliveryFee = rate.deliveryFee;
2020-10-26 04:41:24 +06:30
} else {
2021-09-10 12:00:08 +06:30
_invoice!.deliveryFee = 0;
2020-10-26 04:41:24 +06:30
}
});
},
discountSelected: (discount) {
setState(() {
2021-09-10 12:00:08 +06:30
_invoice!.discount = discount;
2020-10-26 04:41:24 +06:30
});
},
);
final toggleButtonsBox = ToggleButtons(
color: Colors.black45,
selectedColor: Colors.black45,
disabledColor: Colors.grey,
selectedBorderColor: primaryColor,
borderColor: Colors.transparent,
fillColor: Colors.transparent,
highlightColor: Colors.black45,
children: <Widget>[
Icon(cartonIconData),
],
onPressed: (int index) {
setState(() {
_showCartons = !_showCartons;
});
},
isSelected: [_showCartons],
);
2020-10-14 20:56:46 +06:30
2020-10-26 04:41:24 +06:30
final headerBox = Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2021-09-10 15:22:11 +06:30
Text(dateFormatter.format(_invoice!.invoiceDate!)),
2020-10-26 04:41:24 +06:30
SizedBox(
height: 5,
),
Text(_invoice?.userName ?? ""),
Text(
_invoice?.fcsID ?? "",
style: TextStyle(fontSize: 12),
)
],
),
Spacer(),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
toggleButtonsBox,
],
),
],
);
final paymentMethodBox = DisplayText(
labelTextKey: "invoice.payment_method",
2021-09-10 15:22:11 +06:30
text: _invoice!.paymentMethod!.name,
2020-10-14 20:56:46 +06:30
);
2020-10-26 04:41:24 +06:30
final cancelBtn = LocalButton(
textKey: "invoice.cancel.btn",
callBack: _cancel,
);
2020-10-14 20:56:46 +06:30
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: LocalAppBar(
labelKey: 'invoice.form.title',
backgroundColor: Colors.white,
labelColor: primaryColor,
arrowColor: primaryColor),
2020-10-14 20:56:46 +06:30
body: Padding(
padding: const EdgeInsets.all(8.0),
child: ListView(
children: <Widget>[
2021-09-10 12:00:08 +06:30
getInvoiceStatus(context, _invoice!),
2020-10-26 04:41:24 +06:30
headerBox,
_showCartons ? cartonTable : Container(),
_showCartons
? Divider(
color: primaryColor,
thickness: 2,
)
2020-10-14 20:56:46 +06:30
: Container(),
2020-10-26 04:41:24 +06:30
invoiceTableBox,
SizedBox(
height: 10,
),
paymentMethodBox,
SizedBox(
height: 10,
),
2021-09-10 12:00:08 +06:30
isCanceled || isPaid || widget.forCustomer!
2020-10-28 06:58:45 +06:30
? Container()
: cancelBtn,
2020-10-14 20:56:46 +06:30
],
),
),
),
);
}
2020-10-26 04:41:24 +06:30
_cancel() {
showConfirmDialog(context, "invoice.cancel.confirm", _cancelInvoice);
2020-10-14 20:56:46 +06:30
}
2020-10-26 04:41:24 +06:30
_cancelInvoice() async {
setState(() {
_isLoading = true;
});
2020-10-14 20:56:46 +06:30
2020-10-26 04:41:24 +06:30
try {
InvoiceModel invoiceModel =
Provider.of<InvoiceModel>(context, listen: false);
2020-10-14 20:56:46 +06:30
2021-09-10 12:00:08 +06:30
await invoiceModel.cancelInvoice(_invoice!);
2020-10-26 04:41:24 +06:30
Navigator.pop(context, true);
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
2020-10-14 20:56:46 +06:30
});
2020-10-26 04:41:24 +06:30
}
2020-10-14 20:56:46 +06:30
}
}