Untitled

mail@pastecode.io avatar
unknown
dart
2 years ago
7.0 kB
1
Indexable
Never
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:geocoding/geocoding.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:iketfaa_delivery/App/Common/Services/AuthenticationService/Core/manager.dart';
import 'package:iketfaa_delivery/App/Delivery/Models/Main/OrderAddress.dart';
import 'package:iketfaa_delivery/App/Delivery/Models/Main/PersonalAddress.dart';
import 'package:iketfaa_delivery/App/Delivery/Modules/DeliveryHome/controller/delivery_home_controller.dart';

class DeliveryCartController extends GetxController {
  AuthenticationManager authManager = Get.find();
  final DeliveryHomeController deliveryHomeController = Get.find();
  String min = 'minuts'.tr;
  RxBool cartCheck = false.obs;
  Rx<PersonalAddress>? personalAddress;
  RxString address = 'pickUpMyLocation'.tr.obs;
  Rx<PaymentMethod>? character = PaymentMethod.cash.obs;

  @override
  Future<void> onInit() async {
    deliveryHomeController.deliveryHubController.cart.value.paymentType = 0;
    if (deliveryHomeController
        .deliveryHubController.cart.value.orders!.isNotEmpty) {
      cartCheck.value = true;
    }

    super.onInit();
  }

  @override
  Future<void> onReady() async {
    if (deliveryHomeController
        .deliveryHubController.cart.value.orders!.isNotEmpty) {
      deliveryHomeController.calculateAllData();
    }

    super.onReady();
  }

  Future<bool> sendOrder() async {
    bool result = false;
    bool checkCurrentOrder = false;
    checkCurrentOrder = await deliveryHomeController
        .deliveryHubController.readDatabase
        .checkIfCustomerHasCurrentOrder(authManager.appUser.value.id!);
    if (checkCurrentOrder) {
      Get.back();
      authManager.commonTools.showFailedSnackBar('youAllreadyHaveOrder');
      return result;
    } else {
      OrderAddress customerAddress;
      if (deliveryHomeController
              .deliveryHubController.currentPersonalAddress.value.adID ==
          null) {
        Placemark placemark =
            await authManager.deliveryTools.getAddressPlacemark(LatLng(
          authManager.appUser.value.locationData!.localPosition!.latitude,
          authManager.appUser.value.locationData!.localPosition!.longitude,
        ));
        customerAddress = OrderAddress(
          city: placemark.locality ?? '',
          area: placemark.subLocality ?? '',
          street: placemark.street ?? '',
          buildingNo: '0',
          geoPoint: GeoPoint(
            authManager.appUser.value.locationData!.localPosition!.latitude,
            authManager.appUser.value.locationData!.localPosition!.longitude,
          ),
        );
      } else {
        customerAddress = OrderAddress(
          city: deliveryHomeController
              .deliveryHubController.currentPersonalAddress.value.city,
          area: deliveryHomeController
              .deliveryHubController.currentPersonalAddress.value.area,
          street: deliveryHomeController
              .deliveryHubController.currentPersonalAddress.value.street,
          buildingNo: deliveryHomeController
              .deliveryHubController.currentPersonalAddress.value.buildingNo,
          geoPoint: GeoPoint(
            deliveryHomeController
                .deliveryHubController.currentPersonalAddress.value.lat!,
            deliveryHomeController
                .deliveryHubController.currentPersonalAddress.value.long!,
          ),
        );
      }

      if (deliveryHomeController
              .deliveryHubController.cart.value.orders![0].storeName ==
          'directOrder') {
        deliveryHomeController.deliveryHubController.cart.value.customerID =
            authManager.appUser.value.id;
        deliveryHomeController.deliveryHubController.cart.value.customerName =
            authManager.appUser.value.name;
        deliveryHomeController.deliveryHubController.cart.value
            .customerMobileNumber = authManager.appUser.value.phoneNumber;
        deliveryHomeController.deliveryHubController.cart.value.rate =
            authManager.appUser.value.rating;
        deliveryHomeController.deliveryHubController.cart.value.orderType = 1;
        deliveryHomeController
            .deliveryHubController.cart.value.firstEstimatePrice = 0;
        deliveryHomeController
            .deliveryHubController.cart.value.secondEstimatePrice = 0;
        deliveryHomeController.deliveryHubController.cart.value.date =
            DateTime.now();
        deliveryHomeController
            .deliveryHubController.cart.value.customerAddress = customerAddress;
      } else {
        deliveryHomeController.deliveryHubController.cart.value.customerID =
            authManager.appUser.value.id;
        deliveryHomeController.deliveryHubController.cart.value.customerName =
            authManager.appUser.value.name;
        deliveryHomeController.deliveryHubController.cart.value
            .customerMobileNumber = authManager.appUser.value.phoneNumber;
        deliveryHomeController.deliveryHubController.cart.value.rate =
            authManager.appUser.value.rating;
        deliveryHomeController.deliveryHubController.cart.value.orderType = 0;
        deliveryHomeController
                .deliveryHubController.cart.value.firstEstimatePrice =
            authManager.deliveryTools.getPricePerDistance(
                deliveryHomeController.distance.value,
                deliveryHomeController
                    .deliveryHubController.amoutCalculatorFactor);
        deliveryHomeController
                .deliveryHubController.cart.value.secondEstimatePrice =
            authManager.deliveryTools.getPricePerDistance(
                    deliveryHomeController.distance.value,
                    deliveryHomeController
                        .deliveryHubController.amoutCalculatorFactor) +
                5.0;
        deliveryHomeController.deliveryHubController.cart.value.date =
            DateTime.now();
        deliveryHomeController
            .deliveryHubController.cart.value.customerAddress = customerAddress;
      }
      deliveryHomeController.deliveryHubController.cart.value.totalPrice = 0.0;

      result = await deliveryHomeController.deliveryHubController.addToDatabase
          .sendNewOrder(
              deliveryHomeController.deliveryHubController.cart.value);
      Get.back();

      if (result) {
        await deliveryHomeController.deliveryHubController.globalStorage
            .remove('deliveryCartFromLocal');
        deliveryHomeController.deliveryHubController.cart.value.orders = [];

        Get.back();
      } else
        authManager.commonTools.showFailedSnackBar('failedSendOrder');

      deliveryHomeController.deliveryHubController.cart.refresh();
      return result;
    }
  }

  Future<void> setCurrentAddress(PersonalAddress address) async {
    deliveryHomeController.deliveryHubController.currentPersonalAddress.value =
        address;
  }
}

enum PaymentMethod { cash, Credit_card, wallet }