You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.5 KiB
83 lines
2.5 KiB
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
import requests
|
|
import json
|
|
|
|
from odoo import api, fields, models
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
class PosTransaction(models.Model):
|
|
_inherit = 'pos.transaction'
|
|
|
|
transaction_send = fields.Boolean(
|
|
"Transaction envoyée à l'API Mayam",
|
|
default=False)
|
|
|
|
@api.model
|
|
def _run_send_mayam(self):
|
|
''' This method is called from a cron job. '''
|
|
records = self.search([
|
|
('transaction_send', '=', False)])
|
|
records.send_to_mayam()
|
|
|
|
@api.model
|
|
def create(self, values):
|
|
transaction = super(PosTransaction, self).create(values)
|
|
try:
|
|
transaction.send_to_mayam()
|
|
except:
|
|
print("Erreur lors de l'envoi à Mayam")
|
|
return transaction
|
|
|
|
@api.multi
|
|
def send_to_mayam(self):
|
|
list_transaction = []
|
|
data_transaction = {}
|
|
url = self.env.user.company_id.url_mayam
|
|
key_vracoop = "VracoopAPI@2020"
|
|
|
|
for transaction in self:
|
|
seller_id = self.env['product.supplierinfo'].search([
|
|
("product_tmpl_id", '=', transaction.product_id.product_tmpl_id.id)],
|
|
limit=1)
|
|
data_transaction = {
|
|
"date_iso": transaction.date_iso,
|
|
"balance_id": transaction.balance_id,
|
|
"container_ean13": transaction.container_ean13,
|
|
"product_id": transaction.product_id.id,
|
|
"product_name": transaction.product_id.name,
|
|
"price_net": transaction.price_net,
|
|
"weight_net": transaction.weight_net,
|
|
"id": transaction.id,
|
|
"seller_name": seller_id.name.name,
|
|
"seller_code": seller_id.product_code,
|
|
"ean13_product": transaction.product_id.barcode,
|
|
"ean13_weight_product": transaction.ean13
|
|
}
|
|
list_transaction.append(data_transaction)
|
|
|
|
if hasattr(datetime.today(), 'isoformat'):
|
|
date = datetime.today().isoformat()
|
|
|
|
data_json = {
|
|
'date': date,
|
|
'transactions': list_transaction
|
|
}
|
|
|
|
data = {
|
|
'data': json.dumps(data_json),
|
|
'key': key_vracoop
|
|
}
|
|
|
|
response = requests.post(
|
|
url=url,
|
|
data=data)
|
|
|
|
if response.text == "Ok":
|
|
for transaction in self:
|
|
transaction.transaction_send = True
|
|
else:
|
|
return False
|