Collection company_relations (fournisseurs)

Collection company_relations

Les fournisseurs ne possèdent pas leur propre collection. Ils sont représentés comme des CompanyRelation avec to_model: "Supplier". La même collection company_relations sert aussi pour les clients (to_model: "Client") et les sociétés liées (to_model: "Company").

Métadonnées

Service propriétaire

backend

Tenant scope

Oui (champ from)

Soft delete

Oui (champ is_archived)

Index principaux

from + to (unique), from, to, client_number, is_archived, is_favorite

Schéma

interface IContact {
  role: string;
  email: string;
  phone: string;
  first_name: string;
  last_name: string;
  settings: {
    allow_quotation: boolean;
    allow_order_note: boolean;
    allow_delivery_note: boolean;
    allow_shipping_note: boolean;
    allow_invoice: boolean;
    allow_return: boolean;
    allow_marketing: boolean;
    allow_security_alerts: boolean;
  };
}

interface ICutoffTime {
  carrier: string;
  shipping_scheme: string;
  time: string;               // format "HH:MM"
}

interface IContract {
  contract_number?: string;
  title: string;
  description?: string;
  status: "Draft" | "Active" | "Expired" | "Terminated" | "Renewed";
  start_date: Date;
  end_date?: Date;
  renewal_type: "None" | "Automatic" | "Manual";
  renewal_date?: Date;
  document_url?: string;
}

interface CompanyRelation {
  _id: ObjectId;

  from: ObjectId;              // tenant (Company)
  to: ObjectId;                // entité liée (refPath: to_model)
  to_model: "Company" | "Client" | "Supplier";

  client_number: string;
  tier?: ObjectId;             // ref: Tier

  notes?: Map<string, string | boolean | number>;
  contacts?: IContact[];

  // Statut
  is_archived: boolean;        // défaut : false
  is_favorite: boolean;        // défaut : false
  ranking_number?: number;

  // Conditions commerciales
  allow_rebate: boolean;
  apply_tva: boolean;
  allow_order: boolean;
  credit_limit?: number;
  end_of_month_billing: boolean;
  bill_after_delivery_note: boolean;
  allow_quotation: boolean;
  allow_order_note: boolean;
  allow_delivery_note: boolean;
  allow_shipping_note: boolean;
  allow_invoice: boolean;
  allow_return: boolean;
  min_margin_coefficient?: number;
  min_margin_percent?: number;

  // Livraison
  carrier?: string;
  shipping_scheme?: string;
  reference_decote?: string;
  allow_edit_shipping_fees: boolean;
  shipping_fees?: number;
  monthly_free_shipping_threshold?: number;
  daily_free_shipping_threshold?: number;
  cutoff_times?: ICutoffTime[];

  // Paiement
  payment_method?: string;
  payment_terms_days?: number;
  commercial?: ObjectId;       // ref: User
  minimum_order?: number;

  // Restrictions catalogue
  restricted_archetypes?: ObjectId[];
  restricted_brands?: ObjectId[];

  // Règles de prix
  pricing_rules?: {
    markup?: number;
    discount?: number;         // entre 0 et 1
  };

  // API externe
  api_credentials?: {
    client_id: string;
    client_secret: string;
  };

  contracts?: IContract[];     // max 5 contrats

  // Mise à jour prix
  price_updater_type?: "discount" | "coefficient" | "combined" | null;
  price_updater_discount_rate?: number;
  price_updater_coefficient?: number;

  createdAt: Date;
  updatedAt: Date;
}

Règles d’intégrité

  1. from + to est unique — un tenant ne peut avoir qu’une seule relation avec une entité donnée.

  2. is_archived: true désactive la relation sans la supprimer.

  3. Pour les fournisseurs, to pointe vers une société de type "platform".

  4. contracts est limité à 5 contrats par relation.

Features qui consomment cette collection

  • Gestion des fournisseurs (SUP)

  • Tarification (prix d’achat, conditions, ristournes)

  • Règles de marque (BrandRule)

  • Livraison (cutoff times, frais de port)