0

Estoy intentando crear de manera automática la factura de una orden de venta a través de API (XML-RPC) en Odoo 15, el código que tengo hasta ahora ya crea una factura, sin embargo no la relaciona con la orden de venta ni tampoco la agrega las líneas de la orden de venta (order_line), por lo que al final queda una factura en borrador completamente vacía.

Aclaro que me estopy basando en las funciones que se encuentran en el modelo sale_order llamadas _prepare_invoices y _create_invoices, a las cuales no puedo acceder desde API ya que son funciones privadas.

dir_path = os.path.dirname(os.path.realpath(__file__))

server_url  ='http://localhost:8090'
db_name = 'yuju'
username = 'admin'
password = 'odoo'

print('Conectando API Odoo')
common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(server_url))
uid = common.authenticate(db_name, username, password, {})
models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(server_url))

print('Se consulta el número de orden de venta en el archivo drive')
# Define the path to the Excel file containing the orders and Read Excel file
excel_file_path = dir_path + '/files/SO_data.xlsx'
sale_file = pd.read_excel(excel_file_path, usecols=['id','XML name'])
sale_id_file = sale_file['id'].tolist()
xml_id_file = sale_file['XML name'].tolist()

so_domain = ['id', 'in', sale_id_file]
sale_ids = models.execute_kw(db_name, uid, password,'sale.order', 'search_read', [[so_domain]])

if sale_ids:
    sale_id = int(sale_ids[0]['id'])
    # Create invoice for sales order
    for sale_order in sale_ids:
        #Define the invoice values
        sale_id = int(sale_order['id'])
        currency_id = str(sale_order['currency_id'][0])
        narration = sale_order['note']
        campaign_id = False
        medium_id = sale_order['medium_id']
        source_id = sale_order['source_id']
        user_id = int(sale_order['user_id'][0])
        invoice_user_id = int(sale_order['user_id'][0])
        team_id = int(sale_order['team_id'][0])
        partner_id = int(sale_order['partner_id'][0])
        partner_shipping_id = int(sale_order['partner_shipping_id'][0])
        fiscal_position_id = sale_order['fiscal_position_id']
        partner_bank_id = 1
        journal_id = 1
        invoice_origin = sale_order['name']
        invoice_payment_term_id = sale_order['payment_term_id']
        payment_reference = sale_order['reference']
        transaction_ids = sale_order['transaction_ids']
        company_id = 1
        sale_order_line_id = sale_order['order_line']
        sale_order_line_change =  int(sale_order['order_line'][0])
        # Call to sale order line to get order line data
        print('Obtiene el order_line  de la venta: ',invoice_origin,' ID:',sale_id)
        # for order_line in sale_order_line_id:
        sol_domain = ['id', '=', sale_order_line_id]
        sale_order_line = models.execute_kw(db_name, uid, password, 'sale.order.line', 'search_read', [[sol_domain]])
        invoice = {
            'ref': '',
            'move_type': 'out_invoice',
            'currency_id': currency_id,
            'narration': narration,
            'campaign_id': campaign_id,
            'medium_id': medium_id,
            'source_id': source_id,
            'user_id': user_id,
            'invoice_user_id': invoice_user_id,
            'team_id': team_id,
            'partner_id': partner_id,
            'partner_shipping_id': partner_shipping_id,
            'fiscal_position_id': fiscal_position_id,
            'partner_bank_id': partner_bank_id,
            'journal_id': journal_id,  # company comes from the journal
            'invoice_origin': invoice_origin,
            'invoice_payment_term_id': invoice_payment_term_id,
            'payment_reference': payment_reference,
            'transaction_ids': [(6, 0, transaction_ids)],
            'invoice_line_ids': [(0,0,{
                'display_type': sale_order_line[0]['display_type'],
                'sequence': sale_order_line[0]['sequence'],
                'name': sale_order_line[0]['name'],
                'product_id': sale_order_line[0]['product_id'][0],
                'product_uom_id': sale_order_line[0]['product_uom'][0],
                'quantity': sale_order_line[0]['product_qty'],
                'discount': sale_order_line[0]['discount'],
                'price_unit': sale_order_line[0]['price_unit'],
                'tax_ids': [(6, 0, [sale_order_line[0]['tax_id'][0]])],
                'analytic_tag_ids': [(6, 0, sale_order_line[0]['analytic_tag_ids'])],
                'sale_line_ids': [(4, sale_order_line_change)],
                })],
            'company_id': company_id,
        }
        print('Valores de la factura:', invoice)
        create_inv = models.execute_kw(db_name, uid, password, 'account.move', 'create', [invoice])
        print('La factura de la orden: ', sale_id, 'fue creada con ID: ', create_inv)
2
  • Seguro que lo que extraes del xls no está vacío? El archivo está físicamente disponible?
    – Alfabravo
    Commented el 13 abr. 2023 a las 18:20
  • @Alfabravo si, es un archivo de prueba está en la ubicación que declaré, hice un debug en pycharm y si extrae los datos del xls
    – AnibalAP
    Commented el 13 abr. 2023 a las 22:29

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.