def _create_wallet_to_wallet_transaction(payload, customer):

    _, destination = get_source_and_destination_of_transaction(payload.copy())
    payload.update({
        'source_country': customer.country.iso,
        'destination_country': destination.country.iso
    })
    operation_amount = currency_change(customer.country.currency.iso,
                                       destination.country.currency.iso,
                                       Decimal(payload.get('amount')))

    transaction = Transaction()
    transaction.transaction_type = TransactionType.WALLET_TO_WALLET.value
    transaction.number = random_code(10)
    transaction.code = random_code(9)
    transaction.agent = AgentRepository.fetch_by_username('AGENT_WALLET')
    transaction.amount = payload.get('amount')
    transaction.paid_amount = payload.get('paid_amount')
    transaction.source_content_object = customer
    transaction.destination_content_object = destination
    transaction.grille = get_grille_tarifaire(payload)
    transaction.source_country = customer.country
    transaction.destination_country = destination.country
    transaction.paid_amount_in_destination_currency = operation_amount

    transaction.save()

    debit_customer_account(customer, get_customer_balance(customer),
                           payload.get('paid_amount'))
    credit_customer_account(destination, get_customer_balance(destination),
                            operation_amount)
    create_relation_between(customer, destination)
    return transaction
def _create_recouvrement_transaction(payload, agent):
    source, destination = get_source_and_destination_of_transaction(
        payload.copy())
    _can_agent_execute_transaction(agent.entity, destination)
    payload.update({
        'source_country': agent.entity.country.iso,
        'destination_country': 'GW'
    })

    operation_amount = currency_change(agent.entity.country.currency.iso,
                                       destination.country.currency.iso,
                                       Decimal(payload.get('amount')))
    print('XXCXCCCCXXXXXX')  # Il y a des prints de test à enlever
    print(source)
    print('OKKOKKOKOKOKOKOK')

    transaction = Transaction()
    transaction.transaction_type = TransactionType.RECOUVREMENT.value
    transaction.agent = agent
    transaction.number = random_code(10)
    transaction.code = random_code(9)
    transaction.amount = payload.get('amount')
    transaction.paid_amount = payload.get('paid_amount')
    transaction.source_content_object = source
    transaction.destination_content_object = destination
    transaction.grille = get_grille_tarifaire(payload)
    transaction.source_country = agent.entity.country
    transaction.destination_country = destination.country
    transaction.status = TransactionStatus.SUCCESS.value
    transaction.paid_amount_in_destination_currency = operation_amount
    transaction.save()

    credit_entity(destination, get_entity_balance(destination),
                  operation_amount)
    return transaction
def _create_debit_compte_entite_transaction(payload, agent):
    _, destination = get_source_and_destination_of_transaction(payload.copy())
    _can_agent_execute_transaction(agent.entity, destination)
    payload.update({
        'source_country': agent.entity.country.iso,
        'destination_country': destination.country.iso
    })

    operation_amount = currency_change(agent.entity.country.currency.iso,
                                       destination.country.currency.iso,
                                       Decimal(payload.get('amount')))

    transaction = Transaction()
    transaction.transaction_type = TransactionType.DEBIT_COMPTE_ENTITE.value
    transaction.agent = agent
    transaction.number = random_code(10)
    transaction.code = random_code(9)
    transaction.amount = payload.get('amount')
    transaction.paid_amount = payload.get('paid_amount')
    transaction.source_content_object = agent.entity
    transaction.destination_content_object = destination
    transaction.grille = get_grille_tarifaire(payload)
    transaction.source_country = agent.entity.country
    transaction.destination_country = destination.country
    transaction.status = TransactionStatus.SUCCESS.value
    transaction.paid_amount_in_destination_currency = operation_amount
    transaction.save()

    debit_entity(destination, get_entity_balance(destination),
                 payload.get('amount'))
    return transaction
def _create_cash_to_wallet_transaction(payload, agent):

    source, destination = get_source_and_destination_of_transaction(
        payload.copy())
    payload.update({
        'source_country': agent.entity.country.iso,
        'destination_country': destination.country.iso
    })

    operation_amount = currency_change(agent.entity.country.currency.iso,
                                       destination.country.currency.iso,
                                       Decimal(payload.get('amount')))

    transaction = Transaction()
    transaction.transaction_type = TransactionType.CASH_TO_WALLET.value
    transaction.agent = agent
    transaction.number = random_code(10)
    transaction.code = random_code(9)
    transaction.amount = payload.get('amount')
    transaction.paid_amount = payload.get('paid_amount')
    transaction.source_content_object = source
    transaction.destination_content_object = destination
    transaction.grille = get_grille_tarifaire(payload)
    transaction.source_country = agent.entity.country
    transaction.destination_country = destination.country
    transaction.status = TransactionStatus.SUCCESS.value
    transaction.paid_amount_in_destination_currency = operation_amount
    transaction.save()

    credit_customer_account(destination, get_customer_balance(destination),
                            operation_amount)
    return transaction
def _create_cash_to_bank_account_transaction(payload, agent):
    source, destination = get_source_and_destination_of_transaction(
        payload.copy()
    )  # Remettre cette ligne avec la ligne d'avant pour avoir une bonne visibilité de la méthode
    transaction = Transaction()
    transaction.transaction_type = TransactionType.CASH_TO_CASH.value
    transaction.agent = agent
    transaction.number = random_code(10)
    transaction.code = random_code(9)
    transaction.amount = payload.get('amount')
    transaction.paid_amount = payload.get('paid_amount')
    transaction.source_content_object = source
    transaction.destination_content_object = destination
    transaction.grille = get_grille_tarifaire(payload)
    transaction.source_country = SharedRepository.fetch_country_by_iso(
        payload.get('source_country'))  # pareil pour cette ligne
    transaction.destination_country = SharedRepository.fetch_country_by_iso(
        payload.get('destination_country'))  # pareil pour cette ligne
    transaction.other_informations = 'BANQUE : {}, RIB : {}'.format(
        payload.get('bank_name'), payload.get('rib'))
    transaction.source_revenu = SharedRepository.fetch_source_revenu_libelle_by_code(
        payload.get('source_revenu'))
    transaction.motif_envoi = SharedRepository.fetch_motif_envoi_libelle_by_code(
        payload.get('motif_envoi'))
    transaction.save()
    create_relation_between(source, destination)
    return transaction
def create_batch_transaction(entity, commission):

    batch = Transaction()
    batch.transaction_type = TransactionType.BATCH.value
    batch.number = random_code(10)
    batch.code = random_code(9)
    batch.amount = commission
    batch.paid_amount = commission
    batch.destination_content_object = entity
    batch.agent = AgentRepository.get_batch_agent()
    batch.source_content_object = entity
    batch.status = TransactionStatus.SUCCESS.value
    batch.paid_amount_in_destination_currency = commission
    batch.save()
    return batch
def pay_transaction(payload, agent):
    transaction = search_transaction(payload)
    _can_agent_pay_transaction(transaction, agent)

    parent = transaction
    parent.status = TransactionStatus.SUCCESS.value
    parent.save()

    transaction.pk = None
    transaction.number = random_code(10)
    transaction.code = random_code(9)
    transaction.amount = currency_change(
        parent.agent.entity.country.currency.iso,
        agent.entity.country.currency.iso, parent.amount)
    transaction.paid_amount = currency_change(
        parent.agent.entity.country.currency.iso,
        agent.entity.country.currency.iso, parent.paid_amount)
    transaction.status = TransactionStatus.SUCCESS.value
    transaction.parent_transaction_number = parent.number
    transaction.transaction_type = TransactionType.RETRAIT_CASH.value
    transaction.agent = agent
    transaction.save()
    return transaction
def _create_activation_carte_transaction(payload, agent):

    payload.update({
        'source_country': payload.get('customer').get('country'),
        'destination_country': agent.entity.country.iso
    })
    source, destination = get_source_and_destination_of_transaction(
        payload.copy())
    transaction = Transaction()
    transaction.transaction_type = TransactionType.ACTIVATION_CARTE.value
    transaction.agent = agent
    transaction.number = random_code(10)
    transaction.code = random_code(9)
    transaction.amount = payload.get('amount')
    transaction.paid_amount = payload.get('paid_amount')
    transaction.source_content_object = source
    transaction.destination_content_object = destination
    transaction.grille = get_grille_tarifaire(payload)
    transaction.status = TransactionStatus.SUCCESS.value
    transaction.source_country = agent.entity.country
    transaction.destination_country = SharedRepository.fetch_country_by_iso(
        payload.get('customer').get('country'))
    transaction.save()
    return transaction
class Agent(models.Model):
    habilitation = models.CharField(max_length=30,
                                    choices=[(tag.value, tag.value)
                                             for tag in AgentHabilitation],
                                    default=AgentHabilitation.AGENT.value)

    code = models.CharField(
        unique=True,
        max_length=10,
        null=False,
        blank=False,
        default=random_code(6),
    )
    informations = models.OneToOneField(User, on_delete=models.CASCADE)
    entity = models.ForeignKey(Entity,
                               on_delete=models.DO_NOTHING,
                               null=True,
                               blank=True)
    phone_number = models.CharField(max_length=10, null=False, blank=False)
    address = models.CharField(max_length=100, null=False, blank=False)
    authentication_type = models.CharField(max_length=30,
                                           choices=[
                                               (tag.value, tag.value)
                                               for tag in AuthenticationType
                                           ],
                                           default='DEFAULT')
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.informations.username

    def save(self, *args, **kwargs):
        if self.pk is None:
            self.code = random_code(6)
        super(Agent, self).save(*args, **kwargs)

    class Meta:
        verbose_name = _("Agent")
        verbose_name_plural = _("Agents")
        app_label = 'entity'
예제 #10
0
class Transaction(models.Model):

    number = models.CharField(max_length=11,
                              default=random_code(10),
                              unique=True,
                              blank=False,
                              null=False)
    code = models.CharField(max_length=11,
                            default=random_code(9),
                            unique=True,
                            blank=False,
                            null=False)

    agent = models.ForeignKey(Agent, on_delete=models.DO_NOTHING)
    amount = models.DecimalField(max_digits=20, decimal_places=2)
    operation_amount = models.DecimalField(max_digits=20,
                                           decimal_places=2,
                                           default=0)
    paid_amount = models.DecimalField(max_digits=20,
                                      decimal_places=2,
                                      default=0)
    limit = models.Q(app_label='entity', model='entity') \
        | models.Q(app_label='kyc', model='customer')
    paid_amount_in_destination_currency = models.DecimalField(max_digits=20,
                                                              decimal_places=2,
                                                              default=0)
    source_content_type = models.ForeignKey(ContentType,
                                            related_name="source_content_type",
                                            null=False,
                                            blank=False,
                                            limit_choices_to=limit,
                                            on_delete=models.DO_NOTHING)
    source_object_id = models.PositiveIntegerField()
    source_content_object = GenericForeignKey('source_content_type',
                                              'source_object_id')
    destination_content_type = models.ForeignKey(
        ContentType,
        related_name="destination_content_type",
        null=False,
        blank=False,
        limit_choices_to=limit,
        on_delete=models.DO_NOTHING)
    destination_object_id = models.PositiveIntegerField()
    destination_content_object = GenericForeignKey('destination_content_type',
                                                   'destination_object_id')
    grille = models.ForeignKey(Grille,
                               null=True,
                               blank=True,
                               on_delete=models.DO_NOTHING)
    source_country = models.ForeignKey(
        Country,
        null=True,
        blank=True,
        on_delete=models.DO_NOTHING,
        related_name='transaction_source_country')
    destination_country = models.ForeignKey(
        Country,
        null=True,
        blank=True,
        on_delete=models.DO_NOTHING,
        related_name='transaction_destination_country')
    status = models.CharField(max_length=20,
                              choices=[(tag.value, tag.value)
                                       for tag in TransactionStatus],
                              default=TransactionStatus.PENDING.value)
    transaction_type = models.CharField(
        max_length=30,
        choices=[(tag.value, tag.value) for tag in TransactionType],
        default=TransactionType.CASH_TO_CASH.value)
    parent_transaction_number = models.CharField(max_length=11,
                                                 default=random_code(10),
                                                 blank=True,
                                                 null=True)
    other_informations = models.TextField(blank=True, null=True)
    source_revenu = models.ForeignKey(SourceRevenu,
                                      null=True,
                                      blank=True,
                                      on_delete=models.DO_NOTHING,
                                      related_name='transaction_source_revenu')
    motif_envoi = models.ForeignKey(MotifEnvoi,
                                    null=True,
                                    blank=True,
                                    on_delete=models.DO_NOTHING,
                                    related_name='transaction_motif_envoi')
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    payload = JSONField(null=True)

    class Meta:
        verbose_name = _('Transaction')
        verbose_name_plural = _('Transactions')
        app_label = 'transaction'

    def __str__(self):
        return str(self.number)

    @property
    def paid_amount_with_currency_of_agent_operation(self):
        return '{0} {1}'.format(self.paid_amount,
                                self.agent.entity.country.currency.iso)
 def save(self, *args, **kwargs):
     if self.pk is None:
         self.code = random_code(6)
     super(Agent, self).save(*args, **kwargs)
 def save(self, *args, **kwargs):
     if self.pk is None:
         self.code = random_code(6)
         self.account_number = '{}{}'.format(self.country.iso.code, random_code(9))
     super(Entity, self).save(*args, **kwargs)
     self.initialize_accounts()
class Entity(MPTTModel):

    code = models.CharField(
        unique=True,
        max_length=10,
        null=False,
        blank=False,
        default=random_code(6),
    )
    account_number = models.CharField(
        unique=True,
        max_length=9,
        null=False,
        blank=False,
        default=random_code(9),
    )
    category = models.CharField(max_length=30, choices=[
                                (tag.value, tag.value) for tag in EntityType], default='PROVIDER')
    brand_name = models.CharField(max_length=30, null=False, blank=False)
    country = models.ForeignKey(Country, on_delete=models.DO_NOTHING)
    phone_number = models.CharField(max_length=10, null=False, blank=False)
    email = models.CharField(max_length=30, null=False, blank=False)
    address = models.CharField(max_length=100, null=False, blank=False)
    parent = TreeForeignKey("self", on_delete=models.CASCADE,
                            null=True, blank=True, related_name="children")
    accounts = GenericRelation(Account)
    status = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    is_payer = models.BooleanField(default=False)
    avatar = models.ImageField(upload_to=entity_logo_directory_path, blank=True)

    def __str__(self):
        return self.brand_name

    def to_dict(self):
        return model_to_dict(self, fields=[field.name for field in self._meta.fields if field.name != 'secret_key' and field.name != 'avatar'])

    def save(self, *args, **kwargs):
        if self.pk is None:
            self.code = random_code(6)
            self.account_number = '{}{}'.format(self.country.iso.code, random_code(9))
        super(Entity, self).save(*args, **kwargs)
        self.initialize_accounts()

    def initialize_accounts(self):
        if self.accounts.count() == 0:
            Account.objects.create(content_object=self,
                                   category='PRINCIPAL', balance=0)
            Account.objects.create(content_object=self,
                                   category='COMMISSION', balance=0)

    @property
    def logo(self):
        if self.avatar:
            return f'{settings.BACKEND_BASE_URL}self.avatar.url'
        else:
            return None

    class MPTTMeta:
        order_insertion_by = ["brand_name"]

    class Meta:
        verbose_name = _('Entity')
        verbose_name_plural = _('Entities')
        app_label = 'entity'