Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Pilling: models
- """
- from datetime import datetime
- from sqlalchemy import Column, Integer, BigInteger, Boolean, SmallInteger, \
- ForeignKey, String, UniqueConstraint, DateTime
- from sqlalchemy.ext.declarative import declarative_base
- from sqlalchemy.orm import relationship
- from pilling.db.mixins import TableMixin, SmallAutoIdMixin, DictMixin, \
- BigAutoIdMixin
- BASE = declarative_base()
- # Dict models
- class PaymentType(BASE, TableMixin, SmallAutoIdMixin, DictMixin):
- """ Payment type data mapper """
- class BillType(BASE, TableMixin, SmallAutoIdMixin, DictMixin):
- """ Bill type data mapper """
- class IdleProcessing(BASE, TableMixin, SmallAutoIdMixin, DictMixin):
- """ Idle processing methods data mapper """
- class StatusProcessing(BASE, TableMixin, SmallAutoIdMixin, DictMixin):
- """ Service status processing methods data mapper """
- class AccountingInterval(BASE, TableMixin, SmallAutoIdMixin, DictMixin):
- """ Accounting intervals data mapper """
- # Data directory models
- class Organization(BASE, TableMixin, SmallAutoIdMixin, DictMixin):
- """ Organization data mapper """
- class Provider(BASE, TableMixin, SmallAutoIdMixin, DictMixin):
- """ Providers data mapper """
- class Currency(BASE, TableMixin, SmallAutoIdMixin, DictMixin):
- """ Currency data mapper """
- class PaymentSystem(BASE, TableMixin, SmallAutoIdMixin, DictMixin):
- """ Payment system data mapper """
- # Client/account models
- class Client(BASE, TableMixin, BigAutoIdMixin):
- """ Client data mapper """
- # If this item is locked for editing
- locked = Column(Boolean, nullable=False, default=False)
- # Actuality time range
- t_from = Column(DateTime, nullable=False, index=True)
- t_to = Column(DateTime, nullable=True, index=True)
- # Remote system's ID
- remote_id = Column(String(length=255), nullable=True, unique=True,
- index=True)
- class Contract(BASE, TableMixin, BigAutoIdMixin):
- """ Client's contract data mapper """
- # If this item is locked for editing
- locked = Column(Boolean, nullable=False, default=False)
- # Actuality time range
- t_from = Column(DateTime, nullable=False, index=True)
- t_to = Column(DateTime, nullable=True, index=True)
- # Remote system's ID
- remote_id = Column(String(length=255), nullable=True, unique=True,
- index=True)
- # Time since when all changes require complex logic and cannot be done
- # explicitly
- activation_time = Column(DateTime, nullable=True, index=True)
- # Client
- client_id = Column(BigInteger, ForeignKey("client.id"), nullable=False,
- index=True)
- client = relationship("Client")
- # Organization
- organization_id = Column(SmallInteger, ForeignKey("organization.id"),
- nullable=False, index=True)
- organization = relationship("Organization")
- # Provider
- provider_id = Column(SmallInteger, ForeignKey("provider.id"),
- nullable=False, index=True)
- provider = relationship("Provider")
- # Payment type
- payment_type_id = Column(SmallInteger, ForeignKey("payment_type.id"),
- nullable=False, index=True)
- payment_type = relationship("PaymentType")
- # Bill type
- bill_type_id = Column(SmallInteger, ForeignKey("bill_type.id"),
- nullable=True, index=True)
- bill_type = relationship("BillType")
- class Tariff(BASE, TableMixin, BigAutoIdMixin):
- """ Tariff data mapper """
- # Actuality time range
- t_from = Column(DateTime, nullable=False, index=True)
- t_to = Column(DateTime, nullable=True, index=True)
- # Name
- name = Column(String(length=255), nullable=False)
- # Accounting interval: interval of service accounting in accordance with
- # contract terms
- accounting_interval_id = Column(
- SmallInteger, ForeignKey("accounting_interval.id"), nullable=False)
- accounting_interval = relationship("AccountingInterval")
- # Charge interval: interval of produced consumes
- charge_interval_id = Column(
- SmallInteger, ForeignKey("accounting_interval.id"), nullable=False)
- charge_interval = relationship("AccountingInterval")
- # Virtual money unconditional sum
- vm_unconditional = Column(BigInteger, nullable=False)
- # Virtual money conditional sum
- vm_conditional = Column(BigInteger, nullable=False)
- # Idle processing
- idle_processing_id = Column(
- SmallInteger, ForeignKey("idle_processing.id"), nullable=False)
- idle_processing = relationship("IdleProcessing")
- # Service status processing
- status_processing_id = Column(
- SmallInteger, ForeignKey("status_processing.id"), nullable=False)
- status_processing = relationship("StatusProcessing")
- # TODO: maybe some new tariff properties
- class ServicePackBundle(BASE, TableMixin, BigAutoIdMixin):
- """ Service pack bundle data mapper """
- # If this item is locked for editing
- locked = Column(Boolean, nullable=False, default=False)
- # Actuality time range
- t_from = Column(DateTime, nullable=False, index=True)
- t_to = Column(DateTime, nullable=True, index=True)
- # Remote system's ID
- remote_id = Column(String(length=255), nullable=True, unique=True,
- index=True)
- # Client
- client_id = Column(BigInteger, ForeignKey("client.id"), nullable=False,
- index=True)
- client = relationship("Client")
- class ServicePack(BASE, TableMixin, BigAutoIdMixin):
- """ Client contract's service pack data mapper """
- # If this item is locked for editing
- locked = Column(Boolean, nullable=False, default=False)
- # Actuality time range
- t_from = Column(DateTime, nullable=False, index=True)
- t_to = Column(DateTime, nullable=True, index=True)
- # Remote system's ID
- remote_id = Column(String(length=255), nullable=True, unique=True,
- index=True)
- # Contract
- contract_id = Column(BigInteger, ForeignKey("contract.id"), nullable=False,
- index=True)
- contract = relationship("Contract")
- # Service pack bundle
- service_pack_bundle_id = Column(
- BigInteger, ForeignKey("service_pack_bundle.id"), nullable=False,
- index=True)
- service_pack_bundle = relationship("ServicePackBundle")
- class ServicePackVar(BASE, TableMixin, BigAutoIdMixin):
- """ Service pack options, time-log table """
- # Actuality time range
- t_from = Column(DateTime, nullable=False, index=True)
- t_to = Column(DateTime, nullable=True, index=True)
- # Service pack
- service_pack_id = Column(BigInteger, ForeignKey("service_pack.id"),
- nullable=False, index=True)
- service_pack = relationship("ServicePack")
- # Tariff
- tariff_id = Column(BigInteger, ForeignKey("tariff.id"),
- nullable=False, index=True)
- tariff = relationship("Tariff")
- # Virtual money unconditional sum
- vm_unconditional = Column(BigInteger, nullable=False)
- # Virtual money conditional sum
- vm_conditional = Column(BigInteger, nullable=False)
- # Discount percents
- discount_percents = Column(Integer, nullable=False, default=0)
- # Discount virtual money sum
- discount_vm = Column(Integer, nullable=False, default=0)
- # Invoice issuing group
- invoice_group = Column(SmallInteger, nullable=True)
- # State: financial suspension
- st_fin_suspension = Column(Boolean, nullable=False, default=False)
- # State: self (voluntary) suspension of services
- st_self_suspension = Column(Boolean, nullable=False, default=False)
- # State: forced (moderated) suspension of services
- st_forced_suspension = Column(Boolean, nullable=False, default=False)
- # State: free period due to money refund
- st_refund = Column(Boolean, nullable=False, default=False)
- # State: service pack freeze: no consumes will be produced.
- # Needed for total account suspension when cancellation is undesirable
- st_freeze = Column(Boolean, nullable=False, default=False)
- # Money models
- class Payment(BASE, TableMixin, BigAutoIdMixin):
- """ Payment data mapper """
- # Outcome: negative payment, moneyback
- outcome = Column(Boolean, nullable=False)
- # Outcome finished: money fully returned
- outcome_finished = Column(Boolean, nullable=False)
- # Payment system
- payment_system_id = Column(SmallInteger, ForeignKey("payment_system.id"),
- nullable=False, index=True)
- payment_system = relationship("PaymentSystem")
- payment_system_remote_id = Column(String(length=255), nullable=True)
- # Organization
- organization_id = Column(SmallInteger, ForeignKey("organization.id"),
- nullable=True, index=True)
- organization = relationship("Organization")
- # Client
- client_id = Column(BigInteger, ForeignKey("client.id"), nullable=False,
- index=True)
- client = relationship("Client")
- # Currency
- currency_id = Column(BigInteger, ForeignKey("currency.id"), nullable=False)
- currency = relationship("Currency")
- # Payment sum. Cents
- curr_sum = Column(BigInteger, nullable=False)
- # Currency rate in virtual money cents
- curr_rate = Column(Integer, nullable=False, default=100)
- # Virtual money sum in cents
- vm_sum = Column(BigInteger, nullable=False)
- # Payment time (money accounting time)
- time = Column(DateTime, nullable=False, default=datetime.utcnow)
- # Record creation time
- created_at = Column(DateTime, nullable=False, default=datetime.utcnow)
- # Payment cancellation time
- cancelled_at = Column(DateTime, nullable=False)
- # Creation operator
- create_operator = Column(String(length=255), nullable=False, index=True)
- # Cancellation operator
- cancel_operator = Column(String(length=255), nullable=True, index=True)
- # Comment
- comment = Column(String(length=255), nullable=True)
- # Payment order, invoice, bill
- pay_order_time = Column(DateTime, nullable=True)
- pay_order_num = Column(String(length=255), nullable=True)
- invoice_num = Column(String(length=255), nullable=True)
- bill_num = Column(String(length=255), nullable=True)
- # Table Args
- __table_args__ = (
- # Unique payment system remote id inside each payment system
- UniqueConstraint("payment_system_id", "payment_system_remote_id"),
- )
- class MoneyTag(BASE, TableMixin, BigAutoIdMixin):
- """ Money tags (payment - consume pre-association) data mapper """
- # Record creation time
- created_at = Column(DateTime, nullable=False, default=datetime.utcnow)
- # Record cancellation time
- cancelled_at = Column(DateTime, nullable=False)
- # Creation operator
- create_operator = Column(String(length=255), nullable=False, index=True)
- # Cancellation operator
- cancel_operator = Column(String(length=255), nullable=True, index=True)
- class Income(BASE, TableMixin, BigAutoIdMixin):
- """ Income (money transfer from payment to account) data mapper """
- # If this income is made from outcome
- from_outcome = Column(Boolean, nullable=False, default=False)
- # Payment
- payment_id = Column(BigInteger, ForeignKey("payment.id"), nullable=False,
- index=True)
- payment = relationship("Payment")
- # Contract
- contract_id = Column(BigInteger, ForeignKey("contract.id"),
- nullable=False, index=True)
- contract = relationship("Contract")
- # Service pack
- service_pack_id = Column(BigInteger, ForeignKey("service_pack.id"),
- nullable=False, index=True)
- service_pack = relationship("ServicePack")
- # Payment system
- payment_system_id = Column(SmallInteger, ForeignKey("payment_system.id"),
- nullable=False, index=True)
- payment_system = relationship("PaymentSystem")
- # Money tag
- money_tag_id = Column(BigInteger, ForeignKey("money_tag.id"),
- nullable=True, index=True)
- money_tag = relationship("MoneyTag")
- # Virtual money produced
- vm_produced = Column(Integer, nullable=False)
- # Virtual money consumed
- vm_consumed = Column(Integer, nullable=False)
- # Payment time (money accounting time)
- time = Column(DateTime, nullable=False, default=datetime.utcnow)
- # Record creation time
- created_at = Column(DateTime, nullable=False, default=datetime.utcnow)
- # Payment cancellation time
- cancelled_at = Column(DateTime, nullable=False)
- # Creation operator
- create_operator = Column(String(length=255), nullable=False, index=True)
- # Cancellation operator
- cancel_operator = Column(String(length=255), nullable=True, index=True)
- class Outcome(BASE, TableMixin, BigAutoIdMixin):
- """ Outcome (moneyback or account-to-account transfer withdrawal).
- Outcome is a negative income on a contract to be covered from income
- by an expense """
- # Payment system (from source)
- payment_system_id = Column(SmallInteger, ForeignKey("payment_system.id"),
- nullable=False, index=True)
- payment_system = relationship("PaymentSystem")
- # Contract. Outcome is per contract
- contract_id = Column(BigInteger, ForeignKey("contract.id"),
- nullable=False, index=True)
- contract = relationship("Contract")
- # Virtual money produced
- vm_produced = Column(Integer, nullable=False)
- # Virtual money consumed
- vm_consumed = Column(Integer, nullable=False)
- # Payment time (money accounting time)
- time = Column(DateTime, nullable=False, default=datetime.utcnow)
- # Record creation time
- created_at = Column(DateTime, nullable=False, default=datetime.utcnow)
- # Payment cancellation time
- cancelled_at = Column(DateTime, nullable=False)
- # Creation operator
- create_operator = Column(String(length=255), nullable=False, index=True)
- # Cancellation operator
- cancel_operator = Column(String(length=255), nullable=True, index=True)
- class Expense(BASE, TableMixin, BigAutoIdMixin):
- """ Expense (money withdrawal from income to consume or outcome) """
- # Income, source
- income_id = Column(BigInteger, ForeignKey("income.id"),
- nullable=False, index=True)
- income = relationship("Income")
- # Consume, optional destination
- consume_id = Column(BigInteger, ForeignKey("consume.id"),
- nullable=True, index=True)
- consume = relationship("Consume")
- # Outcome, optional destination
- outcome_id = Column(BigInteger, ForeignKey("outcome.id"),
- nullable=True, index=True)
- outcome = relationship("Outcome")
- # Virtual money sum
- vm_sum = Column(Integer, nullable=False)
- # Record creation time
- created_at = Column(DateTime, nullable=False, default=datetime.utcnow)
- # Payment cancellation time
- cancelled_at = Column(DateTime, nullable=False)
- # Creation operator
- create_operator = Column(String(length=255), nullable=False, index=True)
- # Cancellation operator
- cancel_operator = Column(String(length=255), nullable=True, index=True)
- class Outpay(BASE, TableMixin, BigAutoIdMixin):
- """ Outpay (moneyback/account-to-account transfer action).
- Many outpays may point to a single negative payment """
- # Outcome, source
- outcome_id = Column(BigInteger, ForeignKey("outcome.id"),
- nullable=False, index=True)
- outcome = relationship("Outcome")
- # Payment, optional destination
- payment_id = Column(BigInteger, ForeignKey("payment.id"), nullable=True,
- index=True)
- payment = relationship("Payment")
- # Income, optional destination
- income_id = Column(BigInteger, ForeignKey("income.id"), nullable=True,
- index=True)
- income = relationship("Income")
- # Virtual money sum
- vm_sum = Column(Integer, nullable=False)
- # Record creation time
- created_at = Column(DateTime, nullable=False, default=datetime.utcnow)
- # Payment cancellation time
- cancelled_at = Column(DateTime, nullable=False)
- # Creation operator
- create_operator = Column(String(length=255), nullable=False, index=True)
- # Cancellation operator
- cancel_operator = Column(String(length=255), nullable=True, index=True)
- class Consume(BASE, TableMixin, BigAutoIdMixin):
- """ Money consume data mapper """
- # Service pack
- service_pack_id = Column(SmallInteger, ForeignKey("service_pack.id"),
- nullable=False, index=True)
- service_pack = relationship("ServicePack")
- # Corresponding accounting period range
- accounting_interval_from = Column(DateTime, nullable=True, index=True)
- accounting_interval_to = Column(DateTime, nullable=True, index=True)
- # Corresponding charge period range
- charge_interval_to = Column(DateTime, nullable=True, index=True)
- charge_interval_from = Column(DateTime, nullable=True, index=True)
- # Virtual money produced
- vm_produced = Column(Integer, nullable=False)
- vm_produced_v = Column(Integer, nullable=False) # TODO: produced_v doc
- # Virtual money consumed
- vm_consumed = Column(Integer, nullable=False)
- # Money tag (payment - consume pre-association)
- money_tag_id = Column(BigInteger, ForeignKey("money_tag.id"),
- nullable=True, index=True)
- money_tag = relationship("MoneyTag")
- # Money tag type todo: money tag type doc
- money_tag_type = Column(Integer, nullable=False, default=0)
- # Denormalization: service pack special statuses total time
- # Financial suspension total time
- st_fin_suspension_time = Column(Integer, nullable=False, default=0)
- # Self (voluntary) suspension total time
- st_self_suspension_time = Column(Integer, nullable=False, default=0)
- # Forced (moderated) suspension total time
- st_forced_suspension_time = Column(Integer, nullable=False, default=0)
- # Free period due to money refund total time
- st_refund_time = Column(Integer, nullable=False, default=0)
- # Service pack freeze total time
- st_freeze_time = Column(Integer, nullable=False, default=0)
Advertisement
Add Comment
Please, Sign In to add comment