aman-satu-flutter/lib/views/work/tabs/buy/buy_edit.dart

274 lines
9.5 KiB
Dart

import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:satu/core/models/but_item/buy_item_response.dart';
import 'package:satu/core/models/dialog_models.dart';
import 'package:satu/core/services/dialog_service.dart';
import 'package:satu/shared/shared_styles.dart';
import 'package:satu/views/work/tabs/buy/component/product_buy_tile.dart';
import '../../../../core/entity/goods_entity.dart';
import '../../../../core/models/buy_invoice/buy_invoice_response.dart';
import '../../../../core/services/buy_service.dart';
import '../../../../core/services/dictionary_service.dart';
import '../../../../core/services/navigator_service.dart';
import '../../../../core/utils/locator.dart';
import '../../../../routes/route_names.dart';
import '../../../../shared/app_colors.dart';
import '../../../../shared/ui_helpers.dart';
import '../../../../widgets/bar/products_app_bar.dart';
import '../../../../widgets/bar/products_header_bar.dart';
class BuyEditView extends StatefulWidget {
const BuyEditView({required this.invoice, Key? key}) : super(key: key);
final BuyInvoiceResponse invoice;
@override
State<BuyEditView> createState() => _BuyEditViewState();
}
class _BuyEditViewState extends State<BuyEditView> {
final BuyService _service = locator<BuyService>();
final DialogService _dialogService = locator<DialogService>();
static const _pageSize = 20;
late double summ;
final PagingController<int, BuyItemResponse> _pagingController =
PagingController(firstPageKey: 1);
bool editable = false;
bool itemsExist = false;
@override
void initState() {
summ = widget.invoice.summ ?? 0;
_pagingController.addPageRequestListener((pageKey) {
_fetchData(pageKey, _pageSize);
});
if (widget.invoice.refBuyInvoiceStatusId == 1) {
editable = true;
}
super.initState();
}
@override
void dispose() {
_pagingController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: ProductsAppBar(
title: 'Покупка',
backgroundColor: backgroundColor,
childHeight: 60,
child: ProductHeaderBar(
count: -1,
sum: summ,
),
),
body: Column(
children: [
Expanded(
child: PagedListView<int, BuyItemResponse>.separated(
physics: const BouncingScrollPhysics(),
separatorBuilder: (BuildContext context, int index) {
return const Divider(
height: 1.0,
color: disableColor,
);
},
pagingController: _pagingController,
builderDelegate: PagedChildBuilderDelegate<BuyItemResponse>(
noItemsFoundIndicatorBuilder: (BuildContext context) {
return const Center(
child: Text(
'Необходимо добавить товар',
style: textGray11Style,
),
);
},
itemBuilder:
(BuildContext context, BuyItemResponse item, int index) {
return ProductBuyTile(
key: ValueKey(item.id),
ean: item.ean13,
name: item.name,
price: item.price,
count: item.cnt,
categoryName: item.category,
editable: editable,
invoiceId: widget.invoice.id,
id: item.id,
editData: (id, price, count) {
_editData(id, price, count);
},
);
},
),
),
)
],
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: editable ? floatingActionButtonRender() : null,
);
}
/// render floating buttons
Widget floatingActionButtonRender() {
return Padding(
padding: EdgeInsets.all(15),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Visibility(
visible: itemsExist,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: FloatingActionButton(
mini: true,
elevation: 2,
backgroundColor: successColor,
onPressed: () async {
DialogResponse confirm =
await _dialogService.showConfirmationDialog(
title: 'Подтверждение',
description: 'Вы действительно хотите завершить покупку?',
confirmationTitle: 'Да',
cancelTitle: 'Нет',
);
if (confirm.confirmed) {
bool result =
await _service.confirmInvoice(widget.invoice.id);
if (result) {
locator<NavigatorService>().pop(result);
}
}
},
child: Icon(
Icons.check,
color: whiteColor,
size: 35,
),
),
)),
Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FloatingActionButton(
elevation: 2,
mini: true,
onPressed: () async {
final Good? good = await locator<NavigatorService>()
.push(addProductViewRoute) as Good?;
if (good != null && good.id != null) {
bool result =
await _service.addItem(widget.invoice.id, good.id!);
if (result) {
_pagingController.refresh();
} else {
_dialogService.showDialog(
description: 'Товара отсутсвует в остатке или ранее не'
' использователся в системе',
);
}
}
},
child: Icon(
Icons.add_rounded,
size: 40,
color: whiteColor,
),
),
verticalSpaceSmall,
FloatingActionButton(
elevation: 2,
mini: true,
onPressed: () async {
final NavigatorService _nav = locator<NavigatorService>();
final dynamic result = await _nav.push(addByBarcodeViewRoute);
if (result != null) {
final List<Good> goods = await locator<DictionaryService>()
.getGoodsByNameOrEan(result as String, onlyEan: true);
if (goods.isNotEmpty) {
final Good good = goods.first;
bool result =
await _service.addItem(widget.invoice.id, good.id!);
if (result) {
_pagingController.refresh();
} else {
_dialogService.showDialog(
description:
'Товара отсутсвует в остатке или ранее не'
' использователся в системе',
);
}
} else {
_dialogService.showDialog(
description: 'Товара отсутсвует в остатке или ранее не'
' использователся в системе',
);
}
}
},
child: Icon(Icons.qr_code_rounded, size: 30, color: whiteColor),
),
],
)
],
),
);
}
Future<void> _fetchData(int pageKey, int perPage) async {
final List<BuyItemResponse> newItems = await _service
.getItemList(widget.invoice.id, page: pageKey, perpage: perPage);
final isLastPage = newItems.length < _pageSize;
if (isLastPage) {
_pagingController.appendLastPage(newItems);
} else {
final nextPageKey = pageKey + 1;
_pagingController.appendPage(newItems, nextPageKey);
}
if ((_pagingController.value.itemList ?? []).isNotEmpty) {
setState(() {
itemsExist = true;
});
}
updateSummary();
}
void _editData(int id, double price, double count) {
final List<BuyItemResponse> oldList =
_pagingController.value.itemList ?? [];
oldList
..firstWhere((element) => element.id == id).price = price
..firstWhere((element) => element.id == id).cnt = count;
setState(() {
_pagingController.itemList = oldList;
});
}
Future<void> updateSummary() async {
final List<BuyInvoiceResponse> newItems =
await _service.getList(page: 1, perpage: 1, filter: [
{'val': widget.invoice.docNumber, 'action': 'equals', 'col': 'doc_number'}
]);
if (newItems.isNotEmpty) {
setState(() {
summ = newItems.first.summ ?? 0;
});
}
}
}