main.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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_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 = request.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 confirm_sale_order
  189. from account_invoice import (
  190. create_invoice,
  191. create_invoice_move_lines,
  192. number_invoice,
  193. close_invoice
  194. )
  195. from account_move import create_account_move
  196. from account_voucher import create_account_voucher
  197. from account_bank_statement import create_bank_statement
  198. from res_bank_payment import create_bank_payment_statement
  199. # Confirm sale
  200. confirm_sale_order(sale_order_id)
  201. self.make_info_log('[OK] Confirm sale order')
  202. # Create invoice
  203. invoice = create_invoice(sale_order_id, currency_id, user_id, date_now)
  204. self.make_info_log('[OK] Creating invoice')
  205. # Create invoice move lines
  206. invoice_move_lines = create_invoice_move_lines(invoice.id, payment, date_now)
  207. self.make_info_log('[OK] Creating invoice move lines')
  208. # Create account move
  209. account_move = create_account_move(invoice.id, invoice_move_lines)
  210. self.make_info_log('[OK] Creating account move')
  211. # Number invoice
  212. number_invoice(invoice.id)
  213. self.make_info_log('[OK] Number invoice')
  214. # Create account voucher
  215. account_voucher = create_account_voucher(account_move.id, journal_id, currency_id, payment)
  216. self.make_info_log('[OK] Creating account voucher')
  217. # Close invoice
  218. close_invoice(invoice.id)
  219. self.make_info_log('[OK] Closing invoice')
  220. # Create bank statement
  221. create_bank_statement(account_voucher.id, date_now)
  222. self.make_info_log('[OK] Creating account bank statement')
  223. # Create bank payment statement
  224. if payment_method == 'Banco':
  225. create_bank_payment_statement(bank_payment_data, invoice.id, account_voucher.id)
  226. self.make_info_log('[OK] Creating bank payment statement')
  227. '''
  228. ╔═╗╦╔╗╔╦╔═╗╦ ╦ ╔═╗╦═╗╔═╗╔╦╗╦ ╦╔═╗╔╦╗ ╔═╗╦╔═╗╦╔═╦╔╗╔╔═╗
  229. ╠╣ ║║║║║╚═╗╠═╣ ╠═╝╠╦╝║ ║ ║║║ ║║ ║ ╠═╝║║ ╠╩╗║║║║║ ╦
  230. ╚ ╩╝╚╝╩╚═╝╩ ╩ ╩ ╩╚═╚═╝═╩╝╚═╝╚═╝ ╩ ╩ ╩╚═╝╩ ╩╩╝╚╝╚═╝
  231. '''
  232. def finish_process_picking(journal_id, customer_id, cart_items, date_now, payment_term_id, warehouse_id, user_id):
  233. sale_order_id, _ = finish_budget_pos(journal_id, customer_id, cart_items, date_now, payment_term_id, warehouse_id, user_id)
  234. # Imports
  235. from sale_order import confirm_sale_order, force_assign_picking
  236. # Confirm sale
  237. confirm_sale_order(sale_order_id)
  238. self.make_info_log('[OK] Confirm sale order')
  239. # Force assign picking
  240. force_assign_picking(sale_order_id)
  241. self.make_info_log('[OK] Pick assign')
  242. '''
  243. ╔═╗╦╔╗╔╦╔═╗╦ ╦ ╔═╗╔═╗╦ ╦╔╦╗╔═╗╔╗╔╔╦╗
  244. ╠╣ ║║║║║╚═╗╠═╣ ╠═╝╠═╣╚╦╝║║║║╣ ║║║ ║
  245. ╚ ╩╝╚╝╩╚═╝╩ ╩ ╩ ╩ ╩ ╩ ╩ ╩╚═╝╝╚╝ ╩
  246. '''
  247. def finish_payment(sale_order_id, currency_id, date_now, payment_term_id, user_id, payment, payment_method, bank_payment_data):
  248. # Imports
  249. from account_invoice import (
  250. create_invoice,
  251. create_invoice_move_lines,
  252. number_invoice,
  253. close_invoice
  254. )
  255. from account_move import create_account_move
  256. from account_voucher import create_account_voucher
  257. from account_bank_statement import create_bank_statement
  258. from res_bank_payment import create_bank_payment_statement
  259. # Payment term
  260. sale_order = request.env['sale.order'].browse(sale_order_id)
  261. sale_order.write({
  262. 'payment_term': payment_term_id
  263. })
  264. # Create invoice
  265. invoice = create_invoice(sale_order_id, currency_id, user_id, date_now, False)
  266. self.make_info_log('[OK] Creating invoice')
  267. # Create invoice move lines
  268. invoice_move_lines = create_invoice_move_lines(invoice.id, payment, date_now)
  269. self.make_info_log('[OK] Creating invoice move lines')
  270. # Create account move
  271. account_move = create_account_move(invoice.id, invoice_move_lines)
  272. self.make_info_log('[OK] Creating account move')
  273. # Number invoice
  274. number_invoice(invoice.id)
  275. self.make_info_log('[OK] Number invoice')
  276. # Create account voucher
  277. account_voucher = create_account_voucher(account_move.id, journal_id, currency_id, payment)
  278. self.make_info_log('[OK] Creating account voucher')
  279. # Close invoice
  280. close_invoice(invoice.id)
  281. self.make_info_log('[OK] Closing invoice')
  282. # Create bank statement
  283. create_bank_statement(account_voucher.id, date_now)
  284. self.make_info_log('[OK] Creating account bank statement')
  285. # Create bank payment statement
  286. if payment_method == 'Banco':
  287. create_bank_payment_statement(bank_payment_data, invoice.id, account_voucher.id)
  288. self.make_info_log('[OK] Creating bank payment statement')
  289. '''
  290. ╔═╗╦╔╗╔╦╔═╗╦ ╦ ╔═╗╦═╗╔═╗╔╦╗╦ ╦╔═╗╔╦╗ ╔╦╗╔═╗╦ ╦╦ ╦╔═╗╦═╗╦ ╦
  291. ╠╣ ║║║║║╚═╗╠═╣ ╠═╝╠╦╝║ ║ ║║║ ║║ ║ ║║║╣ ║ ║╚╗╔╝║╣ ╠╦╝╚╦╝
  292. ╚ ╩╝╚╝╩╚═╝╩ ╩ ╩ ╩╚═╚═╝═╩╝╚═╝╚═╝ ╩ ═╩╝╚═╝╩═╝╩ ╚╝ ╚═╝╩╚═ ╩
  293. '''
  294. def finish_product_delivery(stock_picking_id, user_id):
  295. from stock_picking import confirm_picking
  296. confirm_picking(stock_picking_id, user_id)
  297. '''
  298. ╔═╗╦═╗╔═╗╔═╗╔═╗╔═╗╔═╗ ╔═╗╦╔╗╔╦╔═╗╦ ╦ ╔╦╗╔═╗╔═╗╦╔═╗╦╔═╗╔╗╔
  299. ╠═╝╠╦╝║ ║║ ║╣ ╚═╗╚═╗ ╠╣ ║║║║║╚═╗╠═╣ ║║║╣ ║ ║╚═╗║║ ║║║║
  300. ╩ ╩╚═╚═╝╚═╝╚═╝╚═╝╚═╝ ╚ ╩╝╚╝╩╚═╝╩ ╩ ═╩╝╚═╝╚═╝╩╚═╝╩╚═╝╝╚╝
  301. '''
  302. data = kw.get('data', [])
  303. date_now = get_date()
  304. for row in data:
  305. mode = row.get('mode', 'sale')
  306. journal_id = row.get('journalId', None)
  307. customer_id = row.get('customerId', None)
  308. customer_pricelist_id = row.get('customerPricelistId', None)
  309. cart_items = row.get('items', [])
  310. payment_term_id = row.get('paymentTermId', None)
  311. warehouse_id = row.get('warehouseId', None)
  312. user_id = row.get('userId', None)
  313. payment = float(row.get('payment', 0.0))
  314. payment_method = row.get('paymentMethod', 'Efectivo')
  315. bank_payment_data = row.get('bankPaymentData', {})
  316. if mode == 'budget':
  317. finish_budget_pos(journal_id, customer_id, cart_items, date_now, payment_term_id, warehouse_id, customer_pricelist_id, user_id)
  318. if mode == 'sale':
  319. 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)
  320. if mode == 'product_picking':
  321. finish_process_picking(journal_id, customer_id, cart_items, date_now, payment_term_id, warehouse_id, user_id)
  322. if mode == 'payment':
  323. sale_order_id = row.get('saleOrderId', None)
  324. currency_id = row.get('currencyId', None)
  325. finish_payment(sale_order_id, currency_id, date_now, payment_term_id, user_id, payment, payment_method, bank_payment_data)
  326. if mode == 'product_delivery':
  327. stock_picking_id = row.get('stockPickingId', None)
  328. finish_product_delivery(stock_picking_id, user_id)
  329. return {
  330. 'process': True,
  331. 'date': get_datetime()
  332. }