main.py 20 KB

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