Skip to Content
Menu
This question has been flagged
2 Replies
3671 Views

Hi, 

We are developing a module for Odoo 10 that sends data to a fiscal device using POST method. The fiscal device has an api and for the invoice, the following is the json.dump;

payload_tuples = json.dumps({
"invoiceType": 0,
"transactionType": 0,
"cashier": "Benjamin",
"TraderSystemInvoiceNumber": "test0123",
"buyer": {
"buyerName": "Buyer Name",
"pinOfBuyer": "Buyers Pin",
"buyerAddress": "Buyers Address",
"buyerPhone": "Buyers Phone"
},
"items": [
{
"name": "test",
"quantity": 1.0,
"unitPrice": 1.0
 
}
],
"payment": [
{
"amount": 1,
"paymentType": "Cash"
}
],
"lines": [
{
"lineType": "Text",
"alignment": "bold center",
"format": "Bold",
"value": "Thank you!!"
}
]
})


For this case, we wish to match data from the linvoice lines as follows;

In the items part i.e. 

"items": [
{
"name": "test",
"quantity": 1.0,
"unitPrice": 1.0
 
}
],


I wish that my module gets figures for  the following "name":  from  line.name           ; "quantity": from  line.quantity                AND    "unitPrice":  from   line.price_unit .   fields that are in the invoice lines.


I am using Odoo 10 CE hosted on a ubuntu 16 server.


Kindly assist.

Regards.
 

Avatar
Discard
Best Answer

In order to match data from the invoice lines to the payload for the fiscal device, you can use the Odoo ORM (Object-Relational Mapping) to query the invoice lines and retrieve the desired fields. Here's an example of how you can do this in your module:

invoice_lines = self.env['account.invoice.line'].search([('invoice_id', '=', invoice.id)])
items = []
for line in invoice_lines:
    item = {
        'name': line.name,
        'quantity': line.quantity,
        'unitPrice': line.price_unit
    }
    items.append(item)

payload = {
    'invoiceType': 0,
    'transactionType': 0,
    'cashier': "Benjamin",
    'TraderSystemInvoiceNumber': "test0123",
    'buyer': {
        'buyerName': "Buyer Name",
        'pinOfBuyer': "Buyers Pin",
        'buyerAddress': "Buyers Address",
        'buyerPhone': "Buyers Phone"
    },
    'items': items,
    'payment': [
        {
            'amount': 1,
            'paymentType': "Cash"
        }
    ],
    'lines': [
        {
            'lineType': "Text",
            'alignment': "bold center",
            'format': "Bold",
            'value': "Thank you!!"
        }
    ]
}

In this example, we first query the invoice lines using the search method of the account.invoice.line model, passing the invoice_id as a filter. Then, we iterate over the invoice lines, creating an item dictionary for each line that contains the name, quantity and unitPrice fields, and append it to the items list. Finally, we include the items list in the payload dictionary.

Please keep in mind that this is just an example and you may need to adapt it to your specific use case. Also you will need to import the json library at the beginning of the file.

Also, it's important to test the changes before applying them in production as it may have side effects on other parts of the system.

Avatar
Discard
Author

Hi Mahmoud,
Thank you for your reply. I used your suggestions and wish to share my code / module with you.

Regards,

Author Best Answer

Hi, 

Did,



for rec in self:
#Get Token Method
if get_url:
if get_url and pin :
items = []
for line in rec.invoice_line_ids:
invoice_line = {'ID':line\.id,'DESC':line\.name,'TAXCODE':tax\.name,'QTY':line\.quantity,'AMT':line\.price_unit,'line_total':line\.price_subtotal\}
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ items\.append\(invoice_line\)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ items\ =\ json\.dumps\(items\)


 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ cashier\ =\ self\.user_id\.name
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ tsin\ =\ self\.number
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ buyername\ =\ self\.partner_id\.name
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ pinofbuyer\ =\ self\.partner_id\.vat
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ buyeradd\ =\ self\.partner_id\.city
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ buyerphone\ =\ self\.partner_id\.phone 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ invoice_lines\ =\ self\.env\['account\.invoice\.line'\]\.search\(\[\('invoice_id',\ '=',\ invoice.id)])
items = []
for line in invoice_lines:
item = {
'name': line.name,
'quantity': line.quantity,
'unitPrice': line.price_unit
}
items.append(item)
payload_tuples = json.dumps({
"invoiceType": 0,
"transactionType": 0,
"cashier": cashier,
"TraderSystemInvoiceNumber": tsin,
"buyer": {
"buyerName": buyername,
"pinOfBuyer": pinofbuyer,
"buyerAddress": buyeradd,
"buyerPhone": buyerphone
},
"items": items,
"payment": [
{
"amount": 1,
"paymentType": "Cash"
}
],
"lines": [
{
"lineType": "Text",
"alignment": "bold center",
"format": "Bold",
"value": "Thank you!!"
}
]
})




Still cannot get it to work.  Kindly check and see where i went wrong.


Regards,


Avatar
Discard