Create account movements and account movements lines

Hi. I am tryng to write new account movements form a script, and the first part of fillling the form been easy, but to write new lines, i’ve seen that is allmost the same as in party , addresses, contact_mechanism, by calling the .new() method. ( acclines = acc.lines.new() ) But when i’m tryng to use .find() over the the field named account to asign the account number from the search rezult i only receive AsssertionError on and on, even if i filter only the instances wich exist. It should receive the instance in accclines.account = rezult( instance), or it should be a string ? thing that this last one i doubt… here is my code…

        for i in range(len(df)):  # Where df is a dataframe
            if not AccMoveLines.find(['account', '=', sanitize(df['CUENTA_AM'][i])], limit=1):
                acc = AccMove()
                acc.journal = AccJournal.find([('code', '=', 'APERTURA')])[0]
                acc.period = AccPeriod.find([('name', '=', '2020-01')])[0]
                rez = Account.find([('code', '=', sanitize(df['CUENTA_AM'][i]))], limit=1)
                acclines = acc.lines.new()
                if rez:
                   print(type(rez[0].code))
                to_save.append(acc)
            # print(acc.account, acc.journal.code, acc.period.name)
        AccMove.save(to_save)

The result is :
100000000
103000001
103000002
103000003
103000004
… Exactly the codes that i have in the excel file.
And i understand that i should can do:
acclines.account = rez # right ? or wrong ? Please help! :slight_smile:
also tryed to pass the : rez[0].code # same error…

    Traceback (most recent call last):
      File "migrate_account_move_lines.py", line 59, in <module>
        acclines.account = rez
      File "/home/user/tryton_server/venv_tryton/lib/python3.6/site-packages/proteus/__init__.py", line 257, in __set__
        assert isinstance(value, (Model, type(None)))
    AssertionError

You have a coma after the variable which contains the instance to a tuple.
Removingt the coma should make it work.

mmm…nope, that was just an error when i’ve edited this post. I did do:

    acclines = acc.lines.new()
    if rez:
       print(type(rez[0].code))
       acclines.account = rez

and what i get is the same error:
100000000

    Traceback (most recent call last):
      File "migrate_account_move_lines.py", line 59, in <module>
        acclines.account = rez
      File "/home/user/tryton_server/venv_tryton/lib/python3.6/site-packages/proteus/__init__.py", line 257, in __set__
        assert isinstance(value, (Model, type(None)))
    AssertionError

This is strange…i’m omitting somthing and i don’t know what exactly.

rez is a list in your code, you can not assign a list of accounts to an account field.

yep, you’re right…but then, i should pass an instance,and tryton should do the magic, right ? but i really don’t know how should i create it. :thinking: ill investigate and return here later…

If i pass the rez[0].code i get the same error.

Use an item of the list. Probably the first one as you always test this one.

1 Like

Yep, that did worked… I was going too far with the instance,but somehow now i see that not all the account codes exists in tryton, and if one of them returns None…

trytond.model.modelstorage.RequiredValidationError: A value is required for field "Account" in "Account Move Line". - 

and i can’t save the changes.

The thing is that i have account moves with acount move lines to save just by inserting
account credit and debit
and others with
account party address( where this is automatically refilled ) credit and debit …

 df = pandas.read_excel('file_name.xlsx', dtype="str", engine="openpyxl")
    for i in range(len(df)):  # Where df is a dataframe
        if Account.find(['code', '=', sanitize(df['CUENTA_AM'][i])], limit=1):
            acc = AccMove()
            acc.journal = AccJournal.find([('code', '=', 'APERTURA')])[0]
            acc.period = AccPeriod.find([('name', '=', '2020-01')])[0]

            acclines = acc.lines.new()

            acc_code = Account.find([('code', '=', sanitize(df['CUENTA_AM'][i]))], limit=1)

            if acc_code:
                acclines.account = acc_code[0]
                acclines.credit = Decimal(0)
                acclines.debit = Decimal(0)


            acc_party_code = Party.find(['code', '=', sanitize(df['TERCERO'][i])], limit=1)
            if acc_party_code:
                acclines.account = acc_code[0]
                acclines.party = acc_party_code[0]
                acclines.credit = Decimal(0)
                acclines.debit = Decimal(0)
            print(acclines.account, acclines.party)

            to_save.append(acc)
    print(len(to_save))
    AccMove.save(to_save)

Didn’t find a solution yet. …

615
Traceback (most recent call last):



File “/home/user/tryton_server/venv_tryton/lib/python3.6/site-packages/trytond-5.2.20-py3.6.egg/trytond/model/modelstorage.py”, line 1169, in required_test
**cls.names(field_name)))
trytond.model.modelstorage.RequiredValidationError: A value is required for field “Party” in “Account Move Line”. -