Example #1
0
def accounting_import_step2(request, key):

    from accounting_main.models import AccountingLine, AccountingLineLogging
    from accounting_core.models import AccountingYear, CostCenter, Account

    (session_key, session_data) = _get_import_session_data(request, key)

    if not session_key:
        return session_data  # Not very clean ^^'

    if not session_data['has_data']:
        return redirect('accounting_main-views-accounting_import_step1', key)

    year = get_object_or_404(AccountingYear, pk=session_data['year'])

    # Map line id to have lines (efficiently)
    line_cache = {}
    for line in AccountingLine.objects.filter(accounting_year=year, deleted=False):
        line_cache[line.pk] = line

    diff = session_data['data']

    diff['nop'] = [line_cache[line_pk] for line_pk in diff['nop']]
    diff['to_delete'] = [line_cache[line_pk] for line_pk in diff['to_delete']]
    diff['to_update'] = [(line_cache[line_pk], arg1, arg2) for line_pk, arg1, arg2 in diff['to_update']]

    if request.method == 'POST':

        for wanted_line in diff['to_add']:
            # NB: Si quelqu'un modifie les trucs pendant l'import, ça pétera.
            # C'est ultra peu probable, donc ignoré
            costcenter = CostCenter.objects.get(accounting_year=year, account_number=wanted_line['costcenter'])
            account = Account.objects.get(accounting_year=year, account_number=wanted_line['account'])

            line = AccountingLine(account=account, costcenter=costcenter, date=wanted_line['date'], tva=wanted_line['tva'], text=wanted_line['text'], output=wanted_line['output'], input=wanted_line['input'], document_id=wanted_line['document_id'], deleted=False, accounting_year=year, current_sum=wanted_line['current_sum'], order=wanted_line['order'])
            line.save()
            AccountingLineLogging(object=line, who=request.user, what='created').save()

        for line, wanted_line, diffs in diff['to_update']:

            for field, (old, new) in diffs.items():
                setattr(line, field, new)

            line.save()
            AccountingLineLogging(object=line, who=request.user, what='edited', extra_data=json.dumps({'added': None, 'edited': diffs, 'deleted': None})).save()

        for line in diff['to_delete']:
            for error in line.accountingerror_set.all():
                error.linked_line = None
                error.save()
            line.delete()  # harddelete.

        year.last_accounting_import = now()
        year.save()

        request.session[session_key] = {}
        messages.success(request, _(u"Compta importée ! Si tout est ok, n'oublie pas de notifier les gens."))
        return redirect('accounting_main-views-accounting_import_step0')

    return render(request, "accounting_main/import/step2.html", {'key': key, 'diff': diff})
Example #2
0
    def switch_status_signal(self, request, old_status, dest_status):

        s = super(_AccountingError, self)

        if hasattr(s, 'switch_status_signal'):
            s.switch_status_signal(request, old_status, dest_status)

        if dest_status == '2_fixed':

            if request.POST.get('fix_errors'):

                if self.linked_line and not self.linked_line.deleted and self.linked_line.status == '2_error':

                    from accounting_main.models import AccountingLineLogging

                    old_status = self.linked_line.status
                    self.linked_line.status = '1_validated'
                    self.linked_line.save()

                    AccountingLineLogging(who=request.user, what='state_changed', object=self.linked_line, extra_data=json.dumps({'old': str(self.linked_line.MetaState.states.get(old_status)), 'new': str(self.linked_line.MetaState.states.get('1_validated'))})).save()

                    unotify_people(u'AccountingLine.{}.error'.format(self.costcenter.unit), self.linked_line)
                    notify_people(request, u'AccountingLine.{}.fixed'.format(self.costcenter.unit), 'accounting_line_fixed', self.linked_line, self.linked_line.build_group_members_for_compta_everyone())

            unotify_people(u'AccountingError.{}.created'.format(self.costcenter.unit), self)
            notify_people(request, u'AccountingError.{}.fixed'.format(self.costcenter.unit), 'accounting_error_fixed', self, self.build_group_members_for_compta_everyone_with_messages())
Example #3
0
    def handle(self, *args, **options):

        data = json.loads(sys.stdin.read())

        root_user = TruffeUser.objects.get(username=185952)
        paris_tz = pytz.timezone("Europe/Paris")
        status_mapping = {
            'wai': '0_imported',
            'oky': '1_validated',
            'err': '2_error'
        }

        line_mapping = {}

        for line_data in data['lignes']:

            try:
                ay = AccountingYear.objects.get(name=line_data['year'])
            except:
                print(u"AccountingYear not found !!", line_data['year'])
                ay = None

            if ay:
                try:
                    costcenter = CostCenter.objects.get(
                        account_number=line_data['numero'], accounting_year=ay)
                except:
                    print(u"CostCenter not found !!", line_data['numero'])
                    costcenter = None

                if costcenter:

                    try:
                        account = Account.objects.get(
                            account_number=line_data['compte'],
                            accounting_year=ay)
                    except:
                        print(u"Account not found !!", line_data['compte'])
                        account = None

                    if account:

                        date = paris_tz.localize(
                            datetime.datetime.strptime(line_data['date'],
                                                       '%Y-%m-%d'))
                        line, created = AccountingLine.objects.get_or_create(
                            costcenter=costcenter,
                            accounting_year=ay,
                            status=status_mapping[line_data['status']],
                            account=account,
                            date=date,
                            tva=0,
                            text=line_data['texte'],
                            output=line_data['debit'],
                            input=line_data['credit'],
                            current_sum=line_data['situation'])

                        if created:

                            print("(+/", created, ")", line)
                            AccountingLineLogging(object=line,
                                                  who=root_user,
                                                  what='created').save()

                        line_mapping[line_data['pk']] = line

        for error_data in data['errors']:

            try:
                ay = AccountingYear.objects.get(name=error_data['year'])
            except:
                print(u"AccountingYear not found !!", error_data['year'])
                ay = None

            if ay:
                try:
                    costcenter = CostCenter.objects.get(
                        account_number=error_data['numero'],
                        accounting_year=ay)
                except:
                    print(u"CostCenter not found !!", error_data['numero'])
                    costcenter = None

                if costcenter:
                    date = paris_tz.localize(
                        datetime.datetime.strptime(error_data['date'],
                                                   '%Y-%m-%d %H:%M:%S'))

                    if error_data['ligne']:
                        line = line_mapping[error_data['ligne']]
                    else:
                        line = None

                    error, created = AccountingError.objects.get_or_create(
                        costcenter=costcenter,
                        accounting_year=ay,
                        status='0_drafting',
                        linked_line=line,
                        initial_remark=error_data['texte'])

                    try:
                        user = TruffeUser.objects.get(
                            username=error_data['creator'])
                    except:
                        print("(!) User not found", error_data['creator'])
                        user = root_user

                    if created:
                        print("(+/", created, ")", error)
                        ael = AccountingErrorLogging(object=error,
                                                     who=user,
                                                     when=date,
                                                     what='created')
                        ael.save()
                        # Hack pour forcer la date
                        ael.when = date
                        ael.save()