Skip to Content
Menu
This question has been flagged

Hi,

In order to save some EDI X12 records along with Sale Orders, I created some modules, one to define X12 records as new Odoo models, another one to associate X12 records to the sale.order model.

X12_records.py :

class X12Ref(models.Model):
    _name = 'enies.x12_record_ref'
    _description = "X12 REF Record - Reference Information"

    ref01 = fields.Char(
        string='Reference Identification Qualifier',
        size=3,
        required=True
    )

    ref02 = fields.Char(
        string='Reference Identification',
        size=50
    )

    ref03 = fields.Char(
        string='Description',
        size=80
    )

    ref04 = fields.Char(
        string='Reference Identification Qualifier',
        size=3
    )

 

sale.py

class SaleOrder(models.Model):
    _inherit = 'sale.order'

    x12_ref = fields.Many2many(
        'enies.x12_record_ref'
        'sale_order_x12_record_ref_rel',
        'order_id',
        'x12_ref_id',
        string='X12 - Reference Information',
        ondelete='cascade',
        copy=False
    )

There is no view associated to this new field as it's only used for XML-RPC purposes  (REFs must, for example, be provided in outgoing EDI message when answering to an incoming message)

The Many2many relation is used in order not to overload the sale.order model (and it's related table in Postgres) as X12 will only be used sometimes to create orders ; this would lead to blank/unused columns most of the time.

The Many2many relation is also used as enies.x12_record_ref records may be associated to some other models (X12 REF records are also used for invoices, for example)


In order to create Sale Orders with XML-RPC, the following code is used. 

order_data is data structure loaded from a json file.

order_rec = {
                    "origin": False if order_data['order']['origin'] == 0 else True,
                    "company_id": int(order_data['order']['company_id']),
                    "user_id": int(order_data['order']['user_id']),
                    <<< remaining fields as defined the same way >>>
}

if ('ref' in order_data['order']) and (len(order_data['order']['ref']) != 0):
ref_ids = []
for ref in order_data['order']['ref']:
ref_id = odoo_model.execute_kw(
odoo_conf['db'],
odoo_uid,
odoo_conf['password'],
'enies.x12_record_ref',
'create',
[ref]
)

ref_ids.append(ref_id)

order_rec['x12_ref'] = [(6, 0, beg_ref)]

order_id = odoo_model.execute_kw(
odoo_conf['db'],
odoo_uid,
odoo_conf['password'],
'sale.order',
'create',
[order_rec]
)

This code works fine as long as no REF is present in the JSON file, Sale Orders are created the right way. Else, if REF are present in the JSON file, I get the following error from /usr/lib/python2.7/dist-packages/odoo/sql_db.py, line 231 :

ProgrammingError: relation "_unknown" does not exist
LINE 1: DELETE FROM sale_order_x12_record_beg_rel USING "_unknown"

I checked in the database, records are well added in the "sale_order_x12_record_ref_rel" table. The Python code also returns ids when created.

Any idea, please?

Avatar
Discard
Author

Please read :

order_rec['x12_ref'] = [(6, 0, ref_ids)]

Author

Please read :

DELETE FROM sale_order_x12_record_ref_rel

In fact the same exact also issue exists for other X12 records (BEG, for example). I did not mention them here for simplification.

Related Posts Replies Views Activity
0
Nov 22
1595
0
Nov 23
369
1
Mar 22
856
4
Oct 21
21737
1
Apr 20
2419