/**
 * 采购订单业务规则验证器
 * 将业务判断逻辑集中管理
 */
export class OrderRules {
  constructor(public order: BusinessAPI.OrderVO) {}

  /**
   * 是否包含运费成本
   */
  shouldIncludeFreightCost(): boolean {
    return !!this.order.orderDealer?.freightCostFlag;
  }

  /**
   * 是否包含草帘费成本
   */
  shouldIncludeStrawCurtainCost(): boolean {
    if (!this.order.orderDealer?.strawMatCostFlag) {
      return false;
    }

    return !!(
      this.order.orderVehicle?.openStrawCurtain &&
      this.order.orderVehicle?.strawCurtainPrice
    );
  }

  /**
   * 是否启用计提税金
   */
  shouldEnableAccrualTax(): boolean {
    return !!this.order.orderDealer?.enableAccrualTax;
  }

  /**
   * 是否启用公司返利（税费补贴）
   */
  shouldEnableCompanyRebate(): boolean {
    return !!this.order.orderDealer?.enableCompanyRebate;
  }

  /**
   * 是否启用诚信志远分成
   */
  shouldEnableShareProfit(): boolean {
    return (
      !!this.order.orderDealer?.enableShare &&
      this.order.type === "PRODUCTION_PURCHASE"
    );
  }

  /**
   * 是否启用亏损处理
   */
  shouldEnableLoss(): boolean {
    return !!this.order.orderDealer?.enableLoss;
  }

  /**
   * 市场报价是否包含包装费
   */
  shouldIncludePackingInMarketPrice(): boolean {
    return !!this.order.orderDealer?.includePackingFlag;
  }

  /**
   * 获取计提税率（百分比）
   */
  getAccrualTaxRatio(): number {
    return this.order.orderDealer?.accrualTaxRatio ?? 0;
  }

  /**
   * 获取公司返利率（百分比）
   */
  getCompanyRebateRatio(): number {
    return this.order.orderDealer?.companyRebateRatio ?? 0;
  }

  /**
   * 获取诚信志远分成比例（百分比）
   */
  getShareRatio(): number {
    return this.order.orderDealer?.shareRatio ?? 0;
  }

  /**
   * 获取定价方式
   */
  getPricingMethod(): "BY_GROSS_WEIGHT" | "BY_NET_WEIGHT" {
    return this.order.pricingMethod!;
  }

  /**
   * 获取定价方式
   */
  getSupplierPricingMethod(
    supplier: BusinessAPI.OrderSupplier,
  ): "BY_GROSS_WEIGHT" | "BY_NET_WEIGHT" {
    return supplier.pricingMethod!;
  }

  /**
   * 根据供应商定价方式获取重量值
   */
  getWeightBySupplierPricingMethod(
    supplier: BusinessAPI.OrderSupplier,
  ): number {
    return this.getSupplierPricingMethod(supplier) === "BY_GROSS_WEIGHT"
      ? supplier.grossWeight || 0
      : supplier.netWeight || 0;
  }

  /**
   * 根据采购单定价方式获取重量值
   */
  getWeightByOrderPricingMethod(supplier: BusinessAPI.OrderSupplier): number {
    return this.getPricingMethod() === "BY_GROSS_WEIGHT"
      ? supplier.grossWeight || 0
      : supplier.netWeight || 0;
  }
}
