<commit_before>from django.db import models

from accounting.fields import CurrencyField
from accounting.models import AccountType, Account
from accounting.models import AccountingProxy, economic_subject
from accounting import types

## People
@economic_subject
class Person(models.Model):
    name = models.CharField(max_length=128)
    surname = models.CharField(max_length=128)
    
    @property
    def full_name(self):
        return self.name + self.surname
    
    def setup_accounting(self):
        self.subject.init_accounting_system()
        system = self.accounting_system
        # create a generic asset-type account (a sort of "virtual wallet")
        system.add_account(parent=system.root, name='wallet', kind=types.asset)  
    
    def save(self, *args, **kwargs):
        # run only at instance creation-time 
        if not self.pk:
            self.setup_accounting() 
        super(Person, self).save(*args, **kwargs)

    
## GASs
@economic_subject
class GAS(models.Model):
    name = models.CharField(max_length=128, unique=True)
    membership_fee = CurrencyField(null=True, blank=True)

    def setup_accounting(self):
        self.subject.init_accounting_system()
        system = self.accounting_system
        ## setup a base account hierarchy
        # GAS's cash       
        system.add_account(parent=system.root, name='cash', kind=types.asset) 
        # root for GAS members' accounts 
        system.add_account(parent=system.root, name='members', kind=types.asset, is_placeholder=True)
        # a placeholder for organizing transactions representing payments to suppliers
        system.add_account(parent_path='/expenses', name='suppliers', kind=types.expense, is_placeholder=True)
        # recharges made by GAS members to their own account
        system.add_account(parent_path='/incomes', name='recharges', kind=types.income)
        # membership fees
        system.add_account(parent_path='/incomes', name='fees', kind=types.income)
        
    def save(self, *args, **kwargs):
        # run only at instance creation-time 
        if not self.pk:
            self.setup_accounting() 
        super(GAS, self).save(*args, **kwargs)


class GASMember(models.Model):
    person = models.ForeignKey(Person)
    gas = models.ForeignKey(GAS)
    
    def setup_accounting(self):
        person_system = self.person.subject.accounting_system
        gas_system = self.gas.subject.accounting_system
        
        ## account creation
        ## Person-side
        try:
            base_account = person_system['/expenses/gas'] 
        except Account.DoesNotExist:
            person_system.add_account(parent_path='/expenses', name='gas', kind=types.expense, is_placeholder=True)
        # placeholder for payments made to the GAS
        person_system.add_account(parent=base_account, name=str(self.gas.name), kind=types.expense, is_placeholder=True)
        # recharges
        person_system.add_account(parent_path='/expenses/' + str(self.gas.name), name='recharges', kind=types.expense)
        # membership fees
        person_system.add_account(parent_path='/expenses/' + str(self.gas.name), name='fees', kind=types.expense)
        ## GAS-side   <commit_msg>Fixed account-setup logic for ``GASMember`` model.<commit_after>from django.db import models

from accounting.fields import CurrencyField	    
from accounting.models import Account
from accounting.models import AccountingProxy, economic_subject
from accounting import types

## People
@economic_subject
class Person(models.Model):
    name = models.CharField(max_length=128)
    surname = models.CharField(max_length=128)
    
    @property
    def full_name(self):
        return self.name + self.surname
    
    def setup_accounting(self):
        self.subject.init_accounting_system()
        system = self.accounting_system
        # create a generic asset-type account (a sort of "virtual wallet")
        system.add_account(parent=system.root, name='wallet', kind=types.asset)  
    
    def save(self, *args, **kwargs):
        # run only at instance creation-time 
        if not self.pk:
            self.setup_accounting() 
        super(Person, self).save(*args, **kwargs)

    
## GASs
@economic_subject
class GAS(models.Model):
    name = models.CharField(max_length=128, unique=True)
    membership_fee = CurrencyField(null=True, blank=True)

    def setup_accounting(self):
        self.subject.init_accounting_system()
        system = self.accounting_system
        ## setup a base account hierarchy
        # GAS's cash       
        system.add_account(parent=system.root, name='cash', kind=types.asset) 
        # root for GAS members' accounts 
        system.add_account(parent=system.root, name='members', kind=types.asset, is_placeholder=True)
        # a placeholder for organizing transactions representing payments to suppliers
        system.add_account(parent_path='/expenses', name='suppliers', kind=types.expense, is_placeholder=True)
        # recharges made by GAS members to their own account
        system.add_account(parent_path='/incomes', name='recharges', kind=types.income)
        # membership fees
        system.add_account(parent_path='/incomes', name='fees', kind=types.income)
        
    def save(self, *args, **kwargs):
        # run only at instance creation-time 
        if not self.pk:
            self.setup_accounting() 
        super(GAS, self).save(*args, **kwargs)


class GASMember(models.Model):
    person = models.ForeignKey(Person)
    gas = models.ForeignKey(GAS)
    
    def setup_accounting(self):
        person_system = self.person.subject.accounting_system
        gas_system = self.gas.subject.accounting_system
        
        ## account creation
        ## Person-side
        # placeholder for payments made by this person to GASs (s)he belongs to
        try:
            person_system['/expenses/gas'] 
        except Account.DoesNotExist:
            person_system.add_account(parent_path='/expenses', name='gas', kind=types.expense, is_placeholder=True)
        # base account for expenses related to this GAS membership
        person_system.add_account(parent_path='/expenses/', name=str(self.gas.name), kind=types.expense, is_placeholder=True)
        # recharges
        person_system.add_account(parent_path='/expenses/' + str(self.gas.name), name='recharges', kind=types.expense)
        # membership fees
        person_system.add_account(parent_path='/expenses/' + str(self.gas.name), name='fees', kind=types.expense)
        ## GAS-side   
{'fraction_non_alphanumeric': 0.10346935656426716, 'fraction_numerical': 0.002751031636863824, 'mean_word_length': 3.165499681731381, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1329377', 'n_tokens_mistral': 1910, 'n_tokens_neox': 1865, 'n_words': 411}