forked from hesabix/arc
173 lines
5.7 KiB
Dart
Executable file
173 lines
5.7 KiB
Dart
Executable file
import 'package:uuid/uuid.dart';
|
|
|
|
class InvoiceLineItem {
|
|
/// شناسهٔ پایدار برای UI (جابهجایی ردیف، فوکوس، State ویجتها)
|
|
final String lineKey;
|
|
|
|
final int? productId;
|
|
final String? productCode;
|
|
final String? productName;
|
|
|
|
final String? mainUnit;
|
|
final String? secondaryUnit;
|
|
final num? unitConversionFactor; // 1 main = factor * secondary
|
|
|
|
String? selectedUnit;
|
|
|
|
num quantity;
|
|
|
|
// unit price handling
|
|
String unitPriceSource; // manual | base | priceList
|
|
num unitPrice; // price per selected unit
|
|
|
|
// base prices on main unit (as provided by product)
|
|
final num? baseSalesPriceMainUnit;
|
|
final num? basePurchasePriceMainUnit;
|
|
|
|
/// قیمت فروش ارزی کالا (واحد اصلی) — وقتی [priceFxCurrencyId] با ارز فاکتور یکی باشد استفاده میشود
|
|
final num? salesPriceFxMainUnit;
|
|
/// قیمت خرید ارزی کالا (واحد اصلی)
|
|
final num? purchasePriceFxMainUnit;
|
|
/// ارز قیمتهای ارزی کالا
|
|
final int? priceFxCurrencyId;
|
|
|
|
// discount
|
|
String discountType; // percent | amount
|
|
num discountValue; // either percentage (0-100) or absolute amount
|
|
|
|
// tax
|
|
num taxRate; // percent, editable by user
|
|
|
|
// inventory/constraints
|
|
final int? minOrderQty;
|
|
final bool trackInventory;
|
|
final int? warehouseId; // انبار انتخابی برای ردیف
|
|
|
|
// presentation
|
|
String? description;
|
|
|
|
// unique product instances (for inventory_mode=unique)
|
|
List<int>? selectedInstanceIds;
|
|
|
|
// extra info for API payload
|
|
final Map<String, dynamic>? extraInfo;
|
|
|
|
static final _uuid = Uuid();
|
|
/// مقدار پیشفرض copyWith برای فیلدهای nullable یعنی «فیلد را عوض نکن».
|
|
static const Object _basePurchaseUnset = Object();
|
|
static const Object _salesFxUnset = Object();
|
|
static const Object _purchaseFxUnset = Object();
|
|
static const Object _fxCurrencyUnset = Object();
|
|
|
|
InvoiceLineItem({
|
|
String? lineKey,
|
|
this.productId,
|
|
this.productCode,
|
|
this.productName,
|
|
this.mainUnit,
|
|
this.secondaryUnit,
|
|
this.unitConversionFactor,
|
|
this.selectedUnit,
|
|
this.description,
|
|
this.unitPriceSource = 'base',
|
|
this.unitPrice = 0,
|
|
this.quantity = 1,
|
|
this.discountType = 'percent',
|
|
this.discountValue = 0,
|
|
this.taxRate = 0,
|
|
this.baseSalesPriceMainUnit,
|
|
this.basePurchasePriceMainUnit,
|
|
this.salesPriceFxMainUnit,
|
|
this.purchasePriceFxMainUnit,
|
|
this.priceFxCurrencyId,
|
|
this.minOrderQty,
|
|
this.trackInventory = false,
|
|
this.warehouseId,
|
|
this.selectedInstanceIds,
|
|
this.extraInfo,
|
|
}) : lineKey = lineKey ?? _uuid.v4();
|
|
|
|
InvoiceLineItem copyWith({
|
|
String? lineKey,
|
|
int? productId,
|
|
String? productCode,
|
|
String? productName,
|
|
String? mainUnit,
|
|
String? secondaryUnit,
|
|
num? unitConversionFactor,
|
|
String? selectedUnit,
|
|
num? quantity,
|
|
String? unitPriceSource,
|
|
num? unitPrice,
|
|
String? discountType,
|
|
num? discountValue,
|
|
num? taxRate,
|
|
String? description,
|
|
num? baseSalesPriceMainUnit,
|
|
Object? basePurchasePriceMainUnit = _basePurchaseUnset,
|
|
Object? salesPriceFxMainUnit = _salesFxUnset,
|
|
Object? purchasePriceFxMainUnit = _purchaseFxUnset,
|
|
Object? priceFxCurrencyId = _fxCurrencyUnset,
|
|
int? minOrderQty,
|
|
bool? trackInventory,
|
|
int? warehouseId,
|
|
List<int>? selectedInstanceIds,
|
|
Map<String, dynamic>? extraInfo,
|
|
}) {
|
|
return InvoiceLineItem(
|
|
lineKey: lineKey ?? this.lineKey,
|
|
productId: productId ?? this.productId,
|
|
productCode: productCode ?? this.productCode,
|
|
productName: productName ?? this.productName,
|
|
mainUnit: mainUnit ?? this.mainUnit,
|
|
secondaryUnit: secondaryUnit ?? this.secondaryUnit,
|
|
unitConversionFactor: unitConversionFactor ?? this.unitConversionFactor,
|
|
selectedUnit: selectedUnit ?? this.selectedUnit,
|
|
quantity: quantity ?? this.quantity,
|
|
unitPriceSource: unitPriceSource ?? this.unitPriceSource,
|
|
unitPrice: unitPrice ?? this.unitPrice,
|
|
discountType: discountType ?? this.discountType,
|
|
discountValue: discountValue ?? this.discountValue,
|
|
taxRate: taxRate ?? this.taxRate,
|
|
description: description ?? this.description,
|
|
baseSalesPriceMainUnit: baseSalesPriceMainUnit ?? this.baseSalesPriceMainUnit,
|
|
basePurchasePriceMainUnit: identical(basePurchasePriceMainUnit, _basePurchaseUnset)
|
|
? this.basePurchasePriceMainUnit
|
|
: basePurchasePriceMainUnit as num?,
|
|
salesPriceFxMainUnit: identical(salesPriceFxMainUnit, _salesFxUnset)
|
|
? this.salesPriceFxMainUnit
|
|
: salesPriceFxMainUnit as num?,
|
|
purchasePriceFxMainUnit: identical(purchasePriceFxMainUnit, _purchaseFxUnset)
|
|
? this.purchasePriceFxMainUnit
|
|
: purchasePriceFxMainUnit as num?,
|
|
priceFxCurrencyId: identical(priceFxCurrencyId, _fxCurrencyUnset)
|
|
? this.priceFxCurrencyId
|
|
: priceFxCurrencyId as int?,
|
|
minOrderQty: minOrderQty ?? this.minOrderQty,
|
|
trackInventory: trackInventory ?? this.trackInventory,
|
|
warehouseId: warehouseId ?? this.warehouseId,
|
|
selectedInstanceIds: selectedInstanceIds ?? this.selectedInstanceIds,
|
|
extraInfo: extraInfo ?? this.extraInfo,
|
|
);
|
|
}
|
|
|
|
num get subtotal => quantity * unitPrice;
|
|
|
|
num get discountAmount {
|
|
if (discountType == 'percent') {
|
|
final p = discountValue;
|
|
if (p <= 0) return 0;
|
|
return subtotal * (p / 100);
|
|
}
|
|
return discountValue;
|
|
}
|
|
|
|
num get taxableAmount {
|
|
final base = subtotal - discountAmount;
|
|
return base < 0 ? 0 : base;
|
|
}
|
|
|
|
num get taxAmount => taxableAmount * (taxRate / 100);
|
|
|
|
num get total => taxableAmount + taxAmount;
|
|
}
|