main.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. # -*- coding: utf-8 -*-
  2. from openerp import http
  3. from openerp.http import request
  4. from http_response import make_gzip_response
  5. import logging
  6. LOGGER = logging.getLogger(__name__)
  7. class PosSales(http.Controller):
  8. #################
  9. # ╦ ╔═╗╔═╗ #
  10. # ║ ║ ║║ ╦ #
  11. # ╩═╝╚═╝╚═╝ #
  12. #################
  13. def make_info_log(self, log):
  14. LOGGER.info('\033[1;34m[INFO] --> \033[m{}'.format(log))
  15. #####################################
  16. # ╦╔╗╔╦╔╦╗ ╔═╗╦═╗╔═╗╔═╗╔═╗╔═╗╔═╗ #
  17. # ║║║║║ ║ ╠═╝╠╦╝║ ║║ ║╣ ╚═╗╚═╗ #
  18. # ╩╝╚╝╩ ╩ ╩ ╩╚═╚═╝╚═╝╚═╝╚═╝╚═╝ #
  19. #####################################
  20. @http.route('/eiru_sales/init', auth='user', methods=['GET'], cors='*')
  21. def _init_sale(self, **kw):
  22. self.make_info_log('Sending JSON response')
  23. # Imports
  24. from server_datetime import get_datetime
  25. from res_currency import check_base_currency
  26. from res_config import get_pos_config
  27. from res_users import get_current_user
  28. from account_journal import get_journals, get_currencies_from_journals
  29. from res_partner import get_customers
  30. from product_template import get_products
  31. from account_payment_term import get_payment_terms
  32. from res_bank import get_banks
  33. from res_bank_payment_type import get_bank_payment_types
  34. from res_bank_cheque_type import get_cheque_types
  35. from res_store import get_stores
  36. from res_currency import get_base_currency
  37. from sale_order import get_sale_orders
  38. from stock_picking import get_pickings
  39. from stock_warehouse import get_warehouses
  40. from decimal_precision import check_discount_precision
  41. # Logic
  42. check_base_currency()
  43. check_discount_precision()
  44. config = get_pos_config()
  45. image_type = config.get('imageType')
  46. mode = kw.get('mode', 'unknown')
  47. data = {}
  48. warehouses = get_warehouses()
  49. currencies = get_currencies_from_journals()
  50. if len(currencies) == 0:
  51. currencies = [get_base_currency()]
  52. # Take all data
  53. if mode == 'sale':
  54. data = {
  55. 'settings': config,
  56. 'date': get_datetime(),
  57. 'user': get_current_user(),
  58. 'currencies': currencies,
  59. 'journals': get_journals(),
  60. 'customers': get_customers(image_type),
  61. 'products': get_products(image_type, map(lambda x: x.get('locationStock').get('id'), warehouses)),
  62. 'paymentTerms': get_payment_terms(),
  63. 'banks': get_banks(),
  64. 'bankPaymentTypes': get_bank_payment_types(),
  65. 'chequeTypes': get_cheque_types(),
  66. 'warehouses': warehouses
  67. }
  68. # Take data for picking
  69. if mode == 'product_picking':
  70. data = {
  71. 'settings': config,
  72. 'date': get_datetime(),
  73. 'user': get_current_user(),
  74. 'currencies': currencies,
  75. 'journals': get_journals(),
  76. 'customers': get_customers(image_type=image_type),
  77. 'products': get_products(image_type, map(lambda x: x.get('locationStock').get('id'), warehouses)),
  78. 'paymentTerms': get_payment_terms(),
  79. 'warehouses': warehouses
  80. }
  81. # Take data for payment
  82. if mode == 'payment':
  83. data = {
  84. 'saleOrders': get_sale_orders(),
  85. 'currencies': currencies,
  86. 'customers': get_customers(image_type=image_type),
  87. 'journals': get_journals(),
  88. 'paymentTerms': get_payment_terms(),
  89. 'banks': get_banks(),
  90. 'bankPaymentTypes': get_bank_payment_types(),
  91. 'chequeTypes': get_cheque_types(),
  92. 'stores': get_stores()
  93. }
  94. # Take data for delivery
  95. if mode == 'product_delivery':
  96. data = {
  97. 'stockPickings': get_pickings()
  98. }
  99. return make_gzip_response(data)
  100. #####################################################################
  101. # ╔═╗╔═╗╔╦╗ ╔═╗╦═╗╔═╗╔╦╗╦ ╦╔═╗╔╦╗╔═╗ ╔╦╗╔═╗╔╦╗╔═╗ ╔═╗╔╗╔╦ ╦ ╦ #
  102. # ║ ╦║╣ ║ ╠═╝╠╦╝║ ║ ║║║ ║║ ║ ╚═╗ ║║╠═╣ ║ ╠═╣ ║ ║║║║║ ╚╦╝ #
  103. # ╚═╝╚═╝ ╩ ╩ ╩╚═╚═╝═╩╝╚═╝╚═╝ ╩ ╚═╝ ═╩╝╩ ╩ ╩ ╩ ╩ ╚═╝╝╚╝╩═╝╩ #
  104. #####################################################################
  105. @http.route('/eiru_sales/get_images', auth='user', methods=['GET'], cors='*')
  106. def _get_images_only(self, **kw):
  107. from res_config import get_pos_config
  108. image_type = str(get_pos_config().get('imageType'))
  109. # Imports
  110. from res_partner import get_customers
  111. from product_template import get_products
  112. return make_gzip_response({
  113. 'customers': get_customers(image_type=image_type),
  114. 'products': get_products(image_type=image_type)
  115. })
  116. #################################################################################################
  117. # ╔═╗╦═╗╔═╗╔═╗╔╦╗╔═╗ ╔═╗╦ ╦╔═╗╔╦╗╔═╗╔╦╗╔═╗╦═╗ ╔═╗╔╗╔╔╦╗ ╦═╗╔═╗╔╦╗╦ ╦╦═╗╔╗╔ ╔╦╗╔═╗╔╦╗╔═╗ #
  118. # ║ ╠╦╝║╣ ╠═╣ ║ ║╣ ║ ║ ║╚═╗ ║ ║ ║║║║║╣ ╠╦╝ ╠═╣║║║ ║║ ╠╦╝║╣ ║ ║ ║╠╦╝║║║ ║║╠═╣ ║ ╠═╣ #
  119. # ╚═╝╩╚═╚═╝╩ ╩ ╩ ╚═╝ ╚═╝╚═╝╚═╝ ╩ ╚═╝╩ ╩╚═╝╩╚═ ╩ ╩╝╚╝═╩╝ ╩╚═╚═╝ ╩ ╚═╝╩╚═╝╚╝ ═╩╝╩ ╩ ╩ ╩ ╩ #
  120. #################################################################################################
  121. @http.route('/eiru_sales/create_customer', type='json', auth='user', methods=['POST'], cors='*')
  122. def _create_customer(self, **kw):
  123. from res_partner import create_customer
  124. self.make_info_log('Creating customer')
  125. return create_customer(kw)
  126. #############################################
  127. # ╔═╗╔═╗╦ ╦╔═╗ ╔═╗╔═╗╔╦╗╔╦╗╦╔╗╔╔═╗╔═╗ #
  128. # ╚═╗╠═╣╚╗╔╝║╣ ╚═╗║╣ ║ ║ ║║║║║ ╦╚═╗ #
  129. # ╚═╝╩ ╩ ╚╝ ╚═╝ ╚═╝╚═╝ ╩ ╩ ╩╝╚╝╚═╝╚═╝ #
  130. #############################################
  131. @http.route('/eiru_sales/save_settings', type='json', auth='user', methods=['POST'], cors='*')
  132. def _save_config(self, **kw):
  133. from res_config import save_pos_config
  134. self.make_info_log('save settings')
  135. return save_pos_config(kw)
  136. #############################################################################################
  137. # ╔═╗╦═╗╔═╗╔═╗╔╦╗╔═╗ ╔═╗╦═╗╔═╗╔╦╗╦ ╦╔═╗╔╦╗ ╔═╗╔╗╔╔╦╗ ╦═╗╔═╗╔╦╗╦ ╦╦═╗╔╗╔ ╔╦╗╔═╗╔╦╗╔═╗ #
  138. # ║ ╠╦╝║╣ ╠═╣ ║ ║╣ ╠═╝╠╦╝║ ║ ║║║ ║║ ║ ╠═╣║║║ ║║ ╠╦╝║╣ ║ ║ ║╠╦╝║║║ ║║╠═╣ ║ ╠═╣ #
  139. # ╚═╝╩╚═╚═╝╩ ╩ ╩ ╚═╝ ╩ ╩╚═╚═╝═╩╝╚═╝╚═╝ ╩ ╩ ╩╝╚╝═╩╝ ╩╚═╚═╝ ╩ ╚═╝╩╚═╝╚╝ ═╩╝╩ ╩ ╩ ╩ ╩ #
  140. #############################################################################################
  141. @http.route('/eiru_sales/create_product', type='json', auth='user', methods=['POST'], cors='*')
  142. def _create_product(self, **kw):
  143. from product_template import create_product
  144. return create_product(kw)
  145. #############################################
  146. # ╔═╗╦╔╗╔╦╔═╗╦ ╦ ╔═╗╦═╗╔═╗╔═╗╔═╗╔═╗╔═╗ #
  147. # ╠╣ ║║║║║╚═╗╠═╣ ╠═╝╠╦╝║ ║║ ║╣ ╚═╗╚═╗ #
  148. # ╚ ╩╝╚╝╩╚═╝╩ ╩ ╩ ╩╚═╚═╝╚═╝╚═╝╚═╝╚═╝ #
  149. #############################################
  150. @http.route('/eiru_sales/finish', type='json', auth='user', methods=['POST'], cors='*')
  151. def _finish(self, **kw):
  152. self.make_info_log('Finishing...')
  153. # Imports
  154. from server_datetime import get_date, get_datetime
  155. '''
  156. ╔═╗╦╔╗╔╦╔═╗╦ ╦ ╔╗ ╦ ╦╔╦╗╔═╗╔═╗╔╦╗ ╔═╗╔═╗╔═╗ ╔═╗╦═╗╔═╗╔═╗╔═╗╔╦╗╦ ╦╦═╗╔═╗
  157. ╠╣ ║║║║║╚═╗╠═╣ ╠╩╗║ ║ ║║║ ╦║╣ ║ ╠═╝║ ║╚═╗ ╠═╝╠╦╝║ ║║ ║╣ ║║║ ║╠╦╝║╣
  158. ╚ ╩╝╚╝╩╚═╝╩ ╩ ╚═╝╚═╝═╩╝╚═╝╚═╝ ╩ ╩ ╚═╝╚═╝ ╩ ╩╚═╚═╝╚═╝╚═╝═╩╝╚═╝╩╚═╚═╝
  159. '''
  160. def finish_budget_pos(journal_id, customer_id, cart_items, date_now, payment_term_id, warehouse_id):
  161. # Imports
  162. from account_journal import get_currency
  163. from sale_order import create_sale_from_cart
  164. # Get currency
  165. currency_id = get_currency(journal_id)
  166. if not currency_id:
  167. currency_id = request.env.user.company_id.currency_id.id
  168. self.make_info_log('[OK] Getting journal')
  169. # Create sale order
  170. sale_order = create_sale_from_cart(customer_id, cart_items, date_now, currency_id, payment_term_id, warehouse_id)
  171. self.make_info_log('[OK] Creating sale order')
  172. return (sale_order.id, currency_id)
  173. '''
  174. ╔═╗╦╔╗╔╦╔═╗╦ ╦ ╔═╗╔═╗╦ ╔═╗ ╔═╗╔═╗╔═╗ ╔═╗╦═╗╔═╗╔═╗╔═╗╔╦╗╦ ╦╦═╗╔═╗
  175. ╠╣ ║║║║║╚═╗╠═╣ ╚═╗╠═╣║ ║╣ ╠═╝║ ║╚═╗ ╠═╝╠╦╝║ ║║ ║╣ ║║║ ║╠╦╝║╣
  176. ╚ ╩╝╚╝╩╚═╝╩ ╩ ╚═╝╩ ╩╩═╝╚═╝ ╩ ╚═╝╚═╝ ╩ ╩╚═╚═╝╚═╝╚═╝═╩╝╚═╝╩╚═╚═╝
  177. '''
  178. def finish_sale_pos(journal_id, customer_id, cart_items, date_now, payment_term_id, payment, payment_method, bank_payment_data, warehouse_id):
  179. # Create budget
  180. sale_order_id, currency_id = finish_budget_pos(journal_id, customer_id, cart_items, date_now, payment_term_id, warehouse_id)
  181. # Imports
  182. from sale_order import confirm_sale_order
  183. from account_invoice import (
  184. create_invoice,
  185. create_invoice_move_lines,
  186. number_invoice,
  187. close_invoice
  188. )
  189. from account_move import create_account_move
  190. from account_voucher import create_account_voucher
  191. from account_bank_statement import create_bank_statement
  192. from res_bank_payment import create_bank_payment_statement
  193. # Confirm sale
  194. confirm_sale_order(sale_order_id)
  195. self.make_info_log('[OK] Confirm sale order')
  196. # Create invoice
  197. invoice = create_invoice(sale_order_id, currency_id, date_now)
  198. self.make_info_log('[OK] Creating invoice')
  199. # Create invoice move lines
  200. invoice_move_lines = create_invoice_move_lines(invoice.id, payment, date_now)
  201. self.make_info_log('[OK] Creating invoice move lines')
  202. # Create account move
  203. account_move = create_account_move(invoice.id, invoice_move_lines)
  204. self.make_info_log('[OK] Creating account move')
  205. # Number invoice
  206. number_invoice(invoice.id)
  207. self.make_info_log('[OK] Number invoice')
  208. # Create account voucher
  209. account_voucher = create_account_voucher(account_move.id, journal_id, currency_id, payment)
  210. self.make_info_log('[OK] Creating account voucher')
  211. # Close invoice
  212. close_invoice(invoice.id)
  213. self.make_info_log('[OK] Closing invoice')
  214. # Create bank statement
  215. create_bank_statement(account_voucher.id, date_now)
  216. self.make_info_log('[OK] Creating account bank statement')
  217. # Create bank payment statement
  218. if payment_method == 'Banco':
  219. create_bank_payment_statement(bank_payment_data, invoice.id, account_voucher.id)
  220. self.make_info_log('[OK] Creating bank payment statement')
  221. '''
  222. ╔═╗╦╔╗╔╦╔═╗╦ ╦ ╔═╗╦═╗╔═╗╔╦╗╦ ╦╔═╗╔╦╗ ╔═╗╦╔═╗╦╔═╦╔╗╔╔═╗
  223. ╠╣ ║║║║║╚═╗╠═╣ ╠═╝╠╦╝║ ║ ║║║ ║║ ║ ╠═╝║║ ╠╩╗║║║║║ ╦
  224. ╚ ╩╝╚╝╩╚═╝╩ ╩ ╩ ╩╚═╚═╝═╩╝╚═╝╚═╝ ╩ ╩ ╩╚═╝╩ ╩╩╝╚╝╚═╝
  225. '''
  226. def finish_process_picking(journal_id, customer_id, cart_items, date_now, payment_term_id, warehouse_id):
  227. sale_order_id, _ = finish_budget_pos(journal_id, customer_id, cart_items, date_now, payment_term_id, warehouse_id)
  228. # Imports
  229. from sale_order import confirm_sale_order, force_assign_picking
  230. # Confirm sale
  231. confirm_sale_order(sale_order_id)
  232. self.make_info_log('[OK] Confirm sale order')
  233. # Force assign picking
  234. force_assign_picking(sale_order_id)
  235. self.make_info_log('[OK] Pick assign')
  236. '''
  237. ╔═╗╦╔╗╔╦╔═╗╦ ╦ ╔═╗╔═╗╦ ╦╔╦╗╔═╗╔╗╔╔╦╗
  238. ╠╣ ║║║║║╚═╗╠═╣ ╠═╝╠═╣╚╦╝║║║║╣ ║║║ ║
  239. ╚ ╩╝╚╝╩╚═╝╩ ╩ ╩ ╩ ╩ ╩ ╩ ╩╚═╝╝╚╝ ╩
  240. '''
  241. def finish_payment(sale_order_id, currency_id, date_now, payment_term_id, payment, payment_method, bank_payment_data):
  242. # Imports
  243. from account_invoice import (
  244. create_invoice,
  245. create_invoice_move_lines,
  246. number_invoice,
  247. close_invoice
  248. )
  249. from account_move import create_account_move
  250. from account_voucher import create_account_voucher
  251. from account_bank_statement import create_bank_statement
  252. from res_bank_payment import create_bank_payment_statement
  253. # Payment term
  254. sale_order = request.env['sale.order'].browse(sale_order_id)
  255. sale_order.write({
  256. 'payment_term': payment_term_id
  257. })
  258. # Create invoice
  259. invoice = create_invoice(sale_order_id, currency_id, date_now, False)
  260. self.make_info_log('[OK] Creating invoice')
  261. # Create invoice move lines
  262. invoice_move_lines = create_invoice_move_lines(invoice.id, payment, date_now)
  263. self.make_info_log('[OK] Creating invoice move lines')
  264. # Create account move
  265. account_move = create_account_move(invoice.id, invoice_move_lines)
  266. self.make_info_log('[OK] Creating account move')
  267. # Number invoice
  268. number_invoice(invoice.id)
  269. self.make_info_log('[OK] Number invoice')
  270. # Create account voucher
  271. account_voucher = create_account_voucher(account_move.id, journal_id, currency_id, payment)
  272. self.make_info_log('[OK] Creating account voucher')
  273. # Close invoice
  274. close_invoice(invoice.id)
  275. self.make_info_log('[OK] Closing invoice')
  276. # Create bank statement
  277. create_bank_statement(account_voucher.id, date_now)
  278. self.make_info_log('[OK] Creating account bank statement')
  279. # Create bank payment statement
  280. if payment_method == 'Banco':
  281. create_bank_payment_statement(bank_payment_data, invoice.id, account_voucher.id)
  282. self.make_info_log('[OK] Creating bank payment statement')
  283. '''
  284. ╔═╗╦╔╗╔╦╔═╗╦ ╦ ╔═╗╦═╗╔═╗╔╦╗╦ ╦╔═╗╔╦╗ ╔╦╗╔═╗╦ ╦╦ ╦╔═╗╦═╗╦ ╦
  285. ╠╣ ║║║║║╚═╗╠═╣ ╠═╝╠╦╝║ ║ ║║║ ║║ ║ ║║║╣ ║ ║╚╗╔╝║╣ ╠╦╝╚╦╝
  286. ╚ ╩╝╚╝╩╚═╝╩ ╩ ╩ ╩╚═╚═╝═╩╝╚═╝╚═╝ ╩ ═╩╝╚═╝╩═╝╩ ╚╝ ╚═╝╩╚═ ╩
  287. '''
  288. def finish_product_delivery(stock_picking_id):
  289. from stock_picking import confirm_picking
  290. confirm_picking(stock_picking_id)
  291. '''
  292. ╔═╗╦═╗╔═╗╔═╗╔═╗╔═╗╔═╗ ╔═╗╦╔╗╔╦╔═╗╦ ╦ ╔╦╗╔═╗╔═╗╦╔═╗╦╔═╗╔╗╔
  293. ╠═╝╠╦╝║ ║║ ║╣ ╚═╗╚═╗ ╠╣ ║║║║║╚═╗╠═╣ ║║║╣ ║ ║╚═╗║║ ║║║║
  294. ╩ ╩╚═╚═╝╚═╝╚═╝╚═╝╚═╝ ╚ ╩╝╚╝╩╚═╝╩ ╩ ═╩╝╚═╝╚═╝╩╚═╝╩╚═╝╝╚╝
  295. '''
  296. data = kw.get('data', [])
  297. date_now = get_date()
  298. for row in data:
  299. mode = row.get('mode', 'sale')
  300. journal_id = row.get('journalId', None)
  301. customer_id = row.get('customerId', None)
  302. cart_items = row.get('items', [])
  303. payment_term_id = row.get('paymentTermId', None)
  304. warehouse_id = row.get('warehouseId', None)
  305. payment = float(row.get('payment', 0.0))
  306. payment_method = row.get('paymentMethod', 'Efectivo')
  307. bank_payment_data = row.get('bankPaymentData', {})
  308. if mode == 'budget':
  309. finish_budget_pos(journal_id, customer_id, cart_items, date_now, payment_term_id, warehouse_id)
  310. if mode == 'sale':
  311. finish_sale_pos(journal_id, customer_id, cart_items, date_now, payment_term_id, payment, payment_method, bank_payment_data, warehouse_id)
  312. if mode == 'product_picking':
  313. finish_process_picking(journal_id, customer_id, cart_items, date_now, payment_term_id, warehouse_id)
  314. if mode == 'payment':
  315. sale_order_id = row.get('saleOrderId', None)
  316. currency_id = row.get('currencyId', None)
  317. finish_payment(sale_order_id, currency_id, date_now, payment_term_id, payment, payment_method, bank_payment_data)
  318. if mode == 'product_delivery':
  319. stock_picking_id = row.get('stockPickingId', None)
  320. finish_product_delivery(stock_picking_id)
  321. return {
  322. 'process': True,
  323. 'date': get_datetime()
  324. }