422 lines
12 KiB
Dart
422 lines
12 KiB
Dart
import 'package:shamsi_date/shamsi_date.dart';
|
||
enum BusinessType {
|
||
company('شرکت'),
|
||
shop('مغازه'),
|
||
store('فروشگاه'),
|
||
union('اتحادیه'),
|
||
club('باشگاه'),
|
||
institute('موسسه'),
|
||
individual('شخصی');
|
||
|
||
const BusinessType(this.displayName);
|
||
final String displayName;
|
||
}
|
||
|
||
enum BusinessField {
|
||
manufacturing('تولیدی'),
|
||
commercial('بازرگانی'),
|
||
service('خدماتی'),
|
||
other('سایر');
|
||
|
||
const BusinessField(this.displayName);
|
||
final String displayName;
|
||
}
|
||
|
||
class BusinessData {
|
||
// مرحله 1: اطلاعات پایه
|
||
String name;
|
||
BusinessType? businessType;
|
||
BusinessField? businessField;
|
||
|
||
// مرحله 2: اطلاعات تماس
|
||
String? address;
|
||
String? phone;
|
||
String? mobile;
|
||
String? postalCode;
|
||
|
||
// مرحله 3: اطلاعات قانونی
|
||
String? nationalId;
|
||
String? registrationNumber;
|
||
String? economicId;
|
||
|
||
// مرحله 4: اطلاعات جغرافیایی
|
||
String? country;
|
||
String? province;
|
||
String? city;
|
||
|
||
// مرحله 5: سال(های) مالی
|
||
List<FiscalYearData> fiscalYears;
|
||
|
||
// ارزها
|
||
int? defaultCurrencyId;
|
||
List<int> currencyIds;
|
||
|
||
BusinessData({
|
||
this.name = '',
|
||
this.businessType,
|
||
this.businessField,
|
||
this.address,
|
||
this.phone,
|
||
this.mobile,
|
||
this.postalCode,
|
||
this.nationalId,
|
||
this.registrationNumber,
|
||
this.economicId,
|
||
this.country,
|
||
this.province,
|
||
this.city,
|
||
List<FiscalYearData>? fiscalYears,
|
||
this.defaultCurrencyId,
|
||
List<int>? currencyIds,
|
||
}) : fiscalYears = fiscalYears ?? <FiscalYearData>[],
|
||
currencyIds = currencyIds ?? <int>[];
|
||
|
||
// تبدیل به Map برای ارسال به API
|
||
Map<String, dynamic> toJson() {
|
||
return {
|
||
'name': name,
|
||
// بکاند انتظار مقادیر فارسی enum را دارد
|
||
'business_type': businessType?.displayName,
|
||
'business_field': businessField?.displayName,
|
||
'address': address,
|
||
'phone': phone,
|
||
'mobile': mobile,
|
||
'postal_code': postalCode,
|
||
'national_id': nationalId,
|
||
'registration_number': registrationNumber,
|
||
'economic_id': economicId,
|
||
'country': country,
|
||
'province': province,
|
||
'city': city,
|
||
'fiscal_years': fiscalYears.map((e) => e.toJson()).toList(),
|
||
'default_currency_id': defaultCurrencyId,
|
||
'currency_ids': _buildCurrencyIdsPayload(),
|
||
};
|
||
}
|
||
|
||
List<int>? _buildCurrencyIdsPayload() {
|
||
if (defaultCurrencyId == null && currencyIds.isEmpty) return null;
|
||
final setIds = <int>{...currencyIds};
|
||
if (defaultCurrencyId != null) setIds.add(defaultCurrencyId!);
|
||
return setIds.toList();
|
||
}
|
||
|
||
// کپی کردن با تغییرات
|
||
BusinessData copyWith({
|
||
String? name,
|
||
BusinessType? businessType,
|
||
BusinessField? businessField,
|
||
String? address,
|
||
String? phone,
|
||
String? mobile,
|
||
String? postalCode,
|
||
String? nationalId,
|
||
String? registrationNumber,
|
||
String? economicId,
|
||
String? country,
|
||
String? province,
|
||
String? city,
|
||
List<FiscalYearData>? fiscalYears,
|
||
int? defaultCurrencyId,
|
||
List<int>? currencyIds,
|
||
}) {
|
||
return BusinessData(
|
||
name: name ?? this.name,
|
||
businessType: businessType ?? this.businessType,
|
||
businessField: businessField ?? this.businessField,
|
||
address: address ?? this.address,
|
||
phone: phone ?? this.phone,
|
||
mobile: mobile ?? this.mobile,
|
||
postalCode: postalCode ?? this.postalCode,
|
||
nationalId: nationalId ?? this.nationalId,
|
||
registrationNumber: registrationNumber ?? this.registrationNumber,
|
||
economicId: economicId ?? this.economicId,
|
||
country: country ?? this.country,
|
||
province: province ?? this.province,
|
||
city: city ?? this.city,
|
||
fiscalYears: fiscalYears ?? this.fiscalYears,
|
||
defaultCurrencyId: defaultCurrencyId ?? this.defaultCurrencyId,
|
||
currencyIds: currencyIds ?? this.currencyIds,
|
||
);
|
||
}
|
||
|
||
// بررسی اعتبار مرحله 1
|
||
bool isStep1Valid() {
|
||
return name.isNotEmpty && businessType != null && businessField != null;
|
||
}
|
||
|
||
// بررسی اعتبار مرحله 2 (اختیاری)
|
||
bool isStep2Valid() {
|
||
// اعتبارسنجی موبایل اگر وارد شده باشد
|
||
if (mobile != null && mobile!.isNotEmpty) {
|
||
if (!_isValidMobile(mobile!)) {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// اعتبارسنجی تلفن ثابت اگر وارد شده باشد
|
||
if (phone != null && phone!.isNotEmpty) {
|
||
if (!_isValidPhone(phone!)) {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
// بررسی اعتبار مرحله 3 (اختیاری)
|
||
bool isStep3Valid() {
|
||
// اعتبارسنجی کد ملی اگر وارد شده باشد
|
||
if (nationalId != null && nationalId!.isNotEmpty) {
|
||
if (!_isValidNationalId(nationalId!)) {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
// بررسی اعتبار مرحله 4 (اطلاعات جغرافیایی - اختیاری)
|
||
bool isStep4Valid() {
|
||
return true;
|
||
}
|
||
|
||
// بررسی اعتبار مرحله 5 (سال مالی - اجباری)
|
||
bool isFiscalStepValid() {
|
||
if (fiscalYears.isEmpty) return false;
|
||
final fy = fiscalYears.first;
|
||
if (fy.title.trim().isEmpty || fy.startDate == null || fy.endDate == null) return false;
|
||
if (fy.startDate!.isAfter(fy.endDate!)) return false;
|
||
return true;
|
||
}
|
||
|
||
// بررسی اعتبار کل فرم
|
||
bool isFormValid() {
|
||
return isStep1Valid() && isStep2Valid() && isStep3Valid() && isStep4Valid() && isFiscalStepValid();
|
||
}
|
||
|
||
// اعتبارسنجی شماره موبایل ایرانی
|
||
bool _isValidMobile(String mobile) {
|
||
// حذف فاصلهها و کاراکترهای اضافی
|
||
String cleanMobile = mobile.replaceAll(RegExp(r'[\s\-\(\)]'), '');
|
||
|
||
// بررسی فرمتهای مختلف موبایل ایرانی
|
||
RegExp mobileRegex = RegExp(r'^(\+98|0)?9\d{9} |