main.py 21 KB

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