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

4 years ago
4 years ago
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. try:
  22. transaction.send_to_mayam()
  23. except:
  24. print("Erreur lors de l'envoi à Mayam")
  25. return transaction
  26. @api.multi
  27. def send_to_mayam(self):
  28. list_transaction = []
  29. data_transaction = {}
  30. url = self.env.user.company_id.url_mayam
  31. key_vracoop = "VracoopAPI@2020"
  32. for transaction in self:
  33. seller_id = self.env['product.supplierinfo'].search([
  34. ("product_tmpl_id", '=', transaction.product_id.product_tmpl_id.id)],
  35. limit=1)
  36. data_transaction = {
  37. "date_iso": transaction.date_iso,
  38. "balance_id": transaction.balance_id,
  39. "container_ean13": transaction.container_ean13,
  40. "product_id": transaction.product_id.id,
  41. "product_name": transaction.product_id.name,
  42. "price_net": transaction.price_net,
  43. "weight_net": transaction.weight_net,
  44. "id": transaction.id,
  45. "seller_name": seller_id.name.name,
  46. "seller_code": seller_id.product_code,
  47. "ean13_product": transaction.product_id.barcode,
  48. "ean13_weight_product": transaction.ean13
  49. }
  50. list_transaction.append(data_transaction)
  51. if hasattr(datetime.today(), 'isoformat'):
  52. date = datetime.today().isoformat()
  53. data_json = {
  54. 'date': date,
  55. 'transactions': list_transaction
  56. }
  57. data = {
  58. 'data': json.dumps(data_json),
  59. 'key': key_vracoop
  60. }
  61. response = requests.post(
  62. url=url,
  63. data=data)
  64. if response.text == "Ok":
  65. for transaction in self:
  66. transaction.transaction_send = True
  67. else:
  68. return False