using Khaemhat.Core.Entities;
namespace Khaemhat.Core.Parties;
/// What a party is to the company (SPEC §11.2): the archive and the code mask follow from it.
public enum PartyKind
{
/// A customer (it may also be a supplier).
Customer = 0,
/// A supplier that is not a customer.
Supplier = 1,
/// A qualified potential customer, without fiscal documents.
Prospect = 2,
/// A commercial contact still to qualify (CRM).
Lead = 3,
}
/// The kind of subject: it decides the checks and the values proposed (SDI code, split payment, VAT number).
public enum SubjectType
{
/// A domestic business.
Company = 0,
/// A private person (consumer).
Person = 1,
/// A public administration (split payment, office code of 6 characters).
PublicAdministration = 2,
/// A business in another EU member state (VIES).
EuCompany = 3,
/// A business outside the EU.
NonEuCompany = 4,
/// A domestic business under the flat-rate scheme (no VAT charged, stamp duty).
FlatRate = 5,
}
/// The outcome of the last check of the VAT number on VIES, saved on the party.
public enum VatCheckStatus
{
/// Not checked.
None = 0,
/// Registered for intra-EU operations.
Valid = 1,
/// Not registered.
Invalid = 2,
/// The service could not answer; to repeat.
Unavailable = 3,
}
/// How the documents are sent to a customer.
public enum DocumentDelivery
{
/// Not sent: printed or downloaded.
None = 0,
/// By e-mail.
Email = 1,
/// By certified e-mail (PEC).
Pec = 2,
}
/// What an address of a party is used for.
public enum PartyAddressKind
{
/// Delivery address (destination of delivery notes).
Delivery = 0,
/// Address for invoices, when different from the registered office.
Billing = 1,
/// A building site or a branch.
Site = 2,
/// Another address.
Other = 3,
}
///
/// A party (SPEC §11.2, wiki Anagrafiche): customer, supplier, prospect or lead, registered once. The code follows
/// the mask of its kind (ADR 0027); converting a lead or prospect into a customer gives it a customer code and keeps the
/// old one in .
///
public sealed class Party : Entity, IDemoMarkable
{
/// The code, unique among the parties.
public string Code { get; set; } = string.Empty;
/// The code it had before a conversion.
public string? PreviousCode { get; set; }
/// What the party is.
public PartyKind Kind { get; set; }
/// A customer that also sells to the company.
public bool IsAlsoSupplier { get; set; }
/// The kind of subject.
public SubjectType SubjectType { get; set; }
/// The business name, or "last name first name" for a person.
public string Name { get; set; } = string.Empty;
/// The first name of a person.
public string? FirstName { get; set; }
/// The last name of a person.
public string? LastName { get; set; }
/// The name on fiscal documents, when different.
public string? FiscalName { get; set; }
/// The VAT number, compact, without the country prefix.
public string? VatNumber { get; set; }
/// The tax code.
public string? TaxCode { get; set; }
/// The registration in the companies register (REA).
public string? Rea { get; set; }
/// The category (party_categories).
public long? CategoryId { get; set; }
/// Street of the registered office.
public string? Address { get; set; }
/// Street number.
public string? StreetNumber { get; set; }
/// Postal code.
public string? PostalCode { get; set; }
/// Municipality or city.
public string? City { get; set; }
/// Province abbreviation (or region abroad).
public string? Province { get; set; }
/// Country (ISO 3166-1 alpha-2).
public string CountryCode { get; set; } = "IT";
/// Phone.
public string? Phone { get; set; }
/// E-mail.
public string? Email { get; set; }
/// Certified e-mail (PEC).
public string? Pec { get; set; }
/// Web site.
public string? Website { get; set; }
/// The salesperson (salespeople).
public long? SalespersonId { get; set; }
/// Language of the documents (ISO 639-1).
public string Language { get; set; } = "it";
/// Currency of the documents (ISO 4217).
public string CurrencyCode { get; set; } = "EUR";
/// Start of the relationship.
public DateOnly? Since { get; set; }
/// Privacy notice accepted.
public bool PrivacyAccepted { get; set; }
/// Consent to marketing.
public bool Marketing { get; set; }
/// Notes.
public string? Notes { get; set; }
/// Last VIES outcome.
public VatCheckStatus VatCheck { get; set; }
/// When VIES answered (or failed), UTC.
public DateTime? VatCheckedAt { get; set; }
/// The customer sub-account of the ledger, created at the first posting (M7).
public string? CustomerAccountCode { get; set; }
/// The supplier sub-account of the ledger, created at the first posting (M7).
public string? SupplierAccountCode { get; set; }
/// Hidden from new documents and default searches; kept with its documents.
public bool Disabled { get; set; }
///
public bool IsDemo { get; set; }
/// Whether the party is a customer (also a supplier that is a customer).
public bool IsCustomer => Kind == PartyKind.Customer;
/// Whether the party is a supplier, alone or also a customer.
public bool IsSupplier => Kind == PartyKind.Supplier || (Kind == PartyKind.Customer && IsAlsoSupplier);
}
/// An address of a party besides the registered office.
public sealed class PartyAddress : Entity
{
/// The party.
public long PartyId { get; set; }
/// What it is used for.
public PartyAddressKind Kind { get; set; }
/// The name shown on documents (site, branch).
public string Name { get; set; } = string.Empty;
/// Street.
public string? Address { get; set; }
/// Street number.
public string? StreetNumber { get; set; }
/// Postal code.
public string? PostalCode { get; set; }
/// City.
public string? City { get; set; }
/// Province.
public string? Province { get; set; }
/// Country (ISO 3166-1 alpha-2).
public string CountryCode { get; set; } = "IT";
/// The address proposed first for its kind.
public bool IsDefault { get; set; }
/// Notes (opening hours, how to reach it).
public string? Notes { get; set; }
}
/// A contact person of a party.
public sealed class PartyContact : Entity
{
/// The party.
public long PartyId { get; set; }
/// Name and surname.
public string Name { get; set; } = string.Empty;
/// Role (purchasing, administration...).
public string? Role { get; set; }
/// Phone.
public string? Phone { get; set; }
/// Mobile phone.
public string? Mobile { get; set; }
/// E-mail.
public string? Email { get; set; }
/// The main contact.
public bool IsDefault { get; set; }
/// Notes.
public string? Notes { get; set; }
}
/// A bank account of a party: the bank of its RiBa, or where the company pays it.
public sealed class PartyBankAccount : Entity
{
/// The party.
public long PartyId { get; set; }
/// The IBAN, compact.
public string Iban { get; set; } = string.Empty;
/// The BIC.
public string? Bic { get; set; }
/// The bank.
public string? BankName { get; set; }
/// The national bank code (ABI), read from the IBAN.
public string? BankCode { get; set; }
/// The branch code (CAB), read from the IBAN.
public string? BranchCode { get; set; }
/// The account proposed on documents.
public bool IsDefault { get; set; }
}
/// The sales terms of a customer, proposed on its documents (M5).
public sealed class PartySalesTerms : Entity
{
/// The party (one row per party).
public long PartyId { get; set; }
/// The price list (price_lists).
public long? PriceListId { get; set; }
/// First discount, percent.
public decimal? Discount1 { get; set; }
/// Second discount, percent.
public decimal? Discount2 { get; set; }
/// The payment terms (payment_terms).
public long? PaymentTermId { get; set; }
/// The account of the company where the customer pays (bank_accounts).
public long? CompanyBankAccountId { get; set; }
/// The carrier (carriers).
public long? CarrierId { get; set; }
/// The shipping terms (shipping_terms).
public long? ShippingTermId { get; set; }
/// The warehouse goods leave from (warehouses).
public long? WarehouseId { get; set; }
/// The credit limit: the most the customer may owe.
public decimal? CreditLimit { get; set; }
/// Orders and deliveries are blocked.
public bool Blocked { get; set; }
/// Delivery notes are grouped into one deferred invoice.
public bool GroupDeliveryNotes { get; set; } = true;
/// The day of the month the deferred invoices are issued, if fixed.
public int? InvoiceDay { get; set; }
/// How the documents are sent.
public DocumentDelivery SendDocuments { get; set; }
/// Where the documents are sent, when not the main e-mail or PEC.
public string? DocumentsEmail { get; set; }
}
/// The purchase terms of a supplier, proposed on its orders (M6).
public sealed class PartyPurchaseTerms : Entity
{
/// The party (one row per party).
public long PartyId { get; set; }
/// The payment terms (payment_terms).
public long? PaymentTermId { get; set; }
/// Days between order and delivery.
public int? LeadTimeDays { get; set; }
/// The smallest order accepted.
public decimal? MinimumOrder { get; set; }
/// Discount, percent.
public decimal? Discount { get; set; }
/// The shipping terms (shipping_terms).
public long? ShippingTermId { get; set; }
/// The VAT register of the purchases (M7).
public string? VatRegisterCode { get; set; }
/// The cost account proposed (M7).
public string? CostAccountCode { get; set; }
/// The e-mail for the orders.
public string? OrdersEmail { get; set; }
}
///
/// The fiscal data of a party (mockup 015, 016): e-invoicing, VAT regime, withholding and contributions, the declaration
/// of intent of a habitual exporter and the identifiers.
///
public sealed class PartyFiscal : Entity
{
/// The party (one row per party).
public long PartyId { get; set; }
/// The SDI recipient code (7 characters, 6 for a public administration).
public string? RecipientCode { get; set; }
/// The PEC the e-invoices go to, without a recipient code.
public string? RecipientPec { get; set; }
/// The reference of the administration (public administrations).
public string? AdministrationReference { get; set; }
/// A courtesy PDF goes with the e-invoice.
public bool AttachPdf { get; set; } = true;
/// A copy is sent by e-mail when the invoice is issued.
public bool SendCopy { get; set; }
/// Split payment (art. 17-ter DPR 633/72).
public bool SplitPayment { get; set; }
/// Reverse charge by default.
public bool ReverseCharge { get; set; }
/// The VAT code proposed (tax_codes).
public long? TaxCodeId { get; set; }
/// VAT exigibility: I immediate, D deferred, S split payment.
public string? Exigibility { get; set; }
/// The stamp duty is charged to the customer.
public bool StampDutyCharged { get; set; } = true;
/// Subject to withholding tax (professionals).
public bool Withholding { get; set; }
/// Withholding rate, percent.
public decimal? WithholdingRate { get; set; }
/// Withholding reason of the e-invoice (A, M, Q...).
public string? WithholdingReason { get; set; }
/// Pension fund of the e-invoice (TC01-TC22).
public string? PensionFund { get; set; }
/// Pension fund rate, percent.
public decimal? PensionFundRate { get; set; }
/// Agent subject to ENASARCO.
public bool Enasarco { get; set; }
/// Declaration of intent active.
public bool IntentActive { get; set; }
/// Number of the declaration.
public string? IntentNumber { get; set; }
/// Date of the declaration.
public DateOnly? IntentDate { get; set; }
/// Protocol of the revenue agency.
public string? IntentProtocol { get; set; }
/// The non-taxable VAT code used under the declaration (tax_codes, nature N3.5).
public long? IntentTaxCodeId { get; set; }
/// The ceiling of the declaration.
public decimal? IntentCeiling { get; set; }
/// The amount already invoiced under the declaration (updated by the invoices, M5).
public decimal IntentUsed { get; set; }
/// The declaration is valid from.
public DateOnly? IntentFrom { get; set; }
/// The declaration is valid until.
public DateOnly? IntentTo { get; set; }
/// Global location number.
public string? Gln { get; set; }
/// Economic operators registration and identification number (customs).
public string? Eori { get; set; }
/// The code of the company at the party.
public string? OurCode { get; set; }
/// What remains of the ceiling of the declaration.
public decimal? IntentRemaining => IntentCeiling is { } ceiling ? ceiling - IntentUsed : null;
}