main.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # -*- coding: utf-8 -*-
  2. from openerp import http
  3. from openerp.http import request
  4. from res_config import get_pos_config, save_pos_config
  5. from server_datetime import get_datetime, get_date
  6. from res_users import get_current_user
  7. from account_journal import get_journals, get_currencies_from_journals, get_currency
  8. from res_partner import get_customers, create_customer
  9. from product_template import get_products, create_product
  10. from account_payment_term import get_payment_terms
  11. from res_bank import get_banks
  12. from res_bank_payment_type import get_bank_payment_types
  13. from res_bank_cheque_type import get_cheque_types
  14. from sale_order import create_sale_from_cart, confirm_sale_order
  15. from account_invoice import create_invoice, create_invoice_move_lines, number_invoice, close_invoice
  16. from account_move import create_account_move
  17. from account_voucher import create_account_voucher
  18. from account_bank_statement import create_bank_statement
  19. from res_bank_payment import create_bank_payment_statement
  20. from res_store import get_stores
  21. from http_response import make_gzip_response
  22. import logging
  23. LOGGER = logging.getLogger(__name__)
  24. class PosSales(http.Controller):
  25. '''
  26. '''
  27. def make_info_log(self, log):
  28. LOGGER.info('\033[1;34m[INFO] --> \033[m{}'.format(log))
  29. '''
  30. New purchase resource route
  31. '''
  32. @http.route('/eiru_sales/init', auth='user', methods=['GET'], cors='*')
  33. def _init_sale(self, **kw):
  34. self.make_info_log('Sending JSON response')
  35. config = get_pos_config()
  36. data = {
  37. 'settings': config,
  38. 'date': get_datetime(),
  39. 'user': get_current_user(),
  40. 'currencies': get_currencies_from_journals(),
  41. 'journals': get_journals(),
  42. 'customers': get_customers(image_type=config.get('imageType')),
  43. 'products': get_products(image_type=config.get('imageType')),
  44. 'paymentTerms': get_payment_terms(),
  45. 'banks': get_banks(),
  46. 'bankPaymentTypes': get_bank_payment_types(),
  47. 'chequeTypes': get_cheque_types(),
  48. 'stores': get_stores()
  49. }
  50. return make_gzip_response(data)
  51. '''
  52. Get products data only
  53. '''
  54. @http.route('/eiru_sales/get_images', auth='user', methods=['GET'], cors='*')
  55. def _get_images_only(self, **kw):
  56. image_type = str(get_pos_config().get('imageType'))
  57. return make_gzip_response({
  58. 'customers': get_customers(image_type=image_type),
  59. 'products': get_products(image_type=image_type)
  60. })
  61. '''
  62. Create customer and return data
  63. '''
  64. @http.route('/eiru_sales/create_customer', type='json', auth='user', methods=['POST'], cors='*')
  65. def _create_customer(self, **kw):
  66. self.make_info_log('Creating customer')
  67. return create_customer(kw)
  68. '''
  69. Save settings
  70. '''
  71. @http.route('/eiru_sales/save_settings', type='json', auth='user', methods=['POST'], cors='*')
  72. def _save_config(self, **kw):
  73. self.make_info_log('save settings')
  74. return save_pos_config(kw)
  75. '''
  76. Create product and return data
  77. '''
  78. @http.route('/eiru_sales/create_product', type='json', auth='user', methods=['POST'], cors='*')
  79. def _create_product(self, **kw):
  80. return create_product(kw)
  81. '''
  82. Process sale
  83. '''
  84. @http.route('/eiru_sales/process_sale', type='json', auth='user', methods=['POST'], cors='*')
  85. def _process_sale(self, **kw):
  86. self.make_info_log('Processing sale...')
  87. date_now = get_date()
  88. journal_id = kw.get('journalId', None)
  89. customer_id = kw.get('customerId', None)
  90. cart_items = kw.get('items', [])
  91. payment_term_id = kw.get('paymentTermId', None)
  92. payment = float(kw.get('payment'))
  93. bank_payment_data = kw.get('bankPaymentData', {})
  94. # Get currency
  95. currency_id = get_currency(journal_id)
  96. self.make_info_log('[OK] Getting journal')
  97. # Create sale order
  98. sale_order = create_sale_from_cart(customer_id, cart_items, date_now, currency_id, payment_term_id)
  99. self.make_info_log('[OK] Creating sale order')
  100. # Check if budget
  101. if kw.get('mode', 'sale') != 'sale':
  102. return {
  103. 'process': True
  104. }
  105. # Confirm sale
  106. confirm_sale_order(sale_order.id)
  107. self.make_info_log('[OK] Confirm sale order')
  108. # Create invoice
  109. invoice = create_invoice(sale_order.id, currency_id, date_now)
  110. self.make_info_log('[OK] Creating invoice')
  111. # Create invoice move lines
  112. invoice_move_lines = create_invoice_move_lines(invoice.id, payment, date_now)
  113. self.make_info_log('[OK] Creating invoice move lines')
  114. # Create account move
  115. account_move = create_account_move(invoice.id, invoice_move_lines)
  116. self.make_info_log('[OK] Creating account move')
  117. # Number invoice
  118. number_invoice(invoice.id)
  119. self.make_info_log('[OK] Number invoice')
  120. # Create account voucher
  121. account_voucher = create_account_voucher(account_move.id, journal_id, currency_id, payment)
  122. self.make_info_log('[OK] Creating account voucher')
  123. # Close invoice
  124. close_invoice(invoice.id)
  125. self.make_info_log('[OK] Closing invoice')
  126. # Create bank statement
  127. create_bank_statement(account_voucher.id, date_now)
  128. self.make_info_log('[OK] Creating account bank statement')
  129. # Create bank payment statement
  130. if kw.get('paymentMethod', 'Efectivo') == 'Banco':
  131. create_bank_payment_statement(bank_payment_data, currency_id, invoice.date_invoice, journal_id, customer_id)
  132. self.make_info_log('[OK] Creating bank payment statement')
  133. return {
  134. 'process': True,
  135. 'name': sale_order.display_name,
  136. 'date': get_datetime()
  137. }