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.

80 lines
2.5 KiB

4 years ago
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import requests
  4. import json
  5. from odoo import api, fields, models
  6. from datetime import datetime
  7. class PosTransaction(models.Model):
  8. _inherit = 'pos.transaction'
  9. transaction_send = fields.Boolean(
  10. "Transaction envoyée à l'API Mayam",
  11. default=False)
  12. @api.model
  13. def _run_send_mayam(self):
  14. ''' This method is called from a cron job. '''
  15. records = self.search([
  16. ('transaction_send', '=', False)])
  17. records.send_to_mayam()
  18. @api.model
  19. def create(self, values):
  20. transaction = super(PosTransaction, self).create(values)
  21. transaction.send_to_mayam()
  22. return transaction
  23. @api.multi
  24. def send_to_mayam(self):
  25. list_transaction = []
  26. data_transaction = {}
  27. url = "https://api.mayam.fr/transaction"
  28. key_vracoop = "VracoopAPI@2020"
  29. for transaction in self:
  30. seller_id = self.env['product.supplierinfo'].search([
  31. ("product_tmpl_id", '=', transaction.product_id.product_tmpl_id.id)],
  32. limit=1)
  33. data_transaction = {
  34. "date_iso": transaction.date_iso,
  35. "balance_id": transaction.balance_id,
  36. "container_ean13": transaction.container_ean13,
  37. "product_id": transaction.product_id.id,
  38. "product_name": transaction.product_id.name,
  39. "price_net": transaction.price_net,
  40. "weight_net": transaction.weight_net,
  41. "id": transaction.id,
  42. "seller_name": seller_id.name.name,
  43. "seller_code": seller_id.product_code,
  44. "ean13_product": transaction.product_id.barcode,
  45. "ean13_weight_product": transaction.ean13
  46. }
  47. list_transaction.append(data_transaction)
  48. if hasattr(datetime.today(), 'isoformat'):
  49. date = datetime.today().isoformat()
  50. data_json = {
  51. 'date': date,
  52. 'transactions': list_transaction
  53. }
  54. data = {
  55. 'data': json.dumps(data_json),
  56. 'key': key_vracoop
  57. }
  58. response = requests.post(
  59. url=url,
  60. data=data)
  61. if response.text == "Ok":
  62. for transaction in self:
  63. transaction.transaction_send = True
  64. else:
  65. return False