# -*- coding: utf-8 -*- from openerp import http from openerp.http import request as r from http_response import make_gzip_response import logging LOGGER = logging.getLogger(__name__) class PosSales(http.Controller): ################# # ╦ ╔═╗╔═╗ # # ║ ║ ║║ ╦ # # ╩═╝╚═╝╚═╝ # ################# def make_info_log(self, log): LOGGER.info('\033[1;34m[INFO] --> \033[m{}'.format(log)) ##################################### # ╦╔╗╔╦╔╦╗ ╔═╗╦═╗╔═╗╔═╗╔═╗╔═╗╔═╗ # # ║║║║║ ║ ╠═╝╠╦╝║ ║║ ║╣ ╚═╗╚═╗ # # ╩╝╚╝╩ ╩ ╩ ╩╚═╚═╝╚═╝╚═╝╚═╝╚═╝ # ##################################### @http.route('/eiru_sales/init', auth='user', methods=['GET'], cors='*') def _init_sale(self, **kw): self.make_info_log('Sending JSON response') # Imports from server_datetime import get_datetime from res_currency import check_base_currency from res_config import get_pos_config from res_users import get_users from account_journal import get_journals, get_currencies_from_journals from res_partner import get_customers from product_template import get_products from product_pricelist import get_pricelists from product_category import get_categories from product_pack import get_product_packs from account_payment_term import get_payment_terms from res_bank import get_banks from res_bank_payment_type import get_bank_payment_types from res_bank_cheque_type import get_cheque_types from res_store import get_stores from res_currency import get_base_currency from sale_order import get_sale_orders from stock_picking import get_pickings from stock_warehouse import get_warehouses from decimal_precision import check_discount_precision # Logic check_base_currency() check_discount_precision() config = get_pos_config() image_type = config.get('imageType') mode = kw.get('mode', 'unknown') data = {} warehouses = get_warehouses() currencies = get_currencies_from_journals() if len(currencies) == 0: currencies = [get_base_currency()] # Take all data if mode == 'sale': data = { 'settings': config, 'date': get_datetime(), 'users': get_users(), 'currencies': currencies, 'journals': get_journals(), 'customers': get_customers(image_type), 'products': get_products(image_type, map(lambda x: x.get('locationStock').get('id'), warehouses)), 'packs': get_product_packs(), 'pricelists': get_pricelists('sale'), 'categories': get_categories(), 'paymentTerms': get_payment_terms(), 'banks': get_banks(), 'bankPaymentTypes': get_bank_payment_types(), 'chequeTypes': get_cheque_types(), 'warehouses': warehouses } # Take data for picking if mode == 'product_picking': data = { 'settings': config, 'date': get_datetime(), 'users': get_users(), 'currencies': currencies, 'journals': get_journals(), 'customers': get_customers(image_type=image_type), 'products': get_products(image_type, map(lambda x: x.get('locationStock').get('id'), warehouses)), 'paymentTerms': get_payment_terms(), 'warehouses': warehouses } # Take data for payment if mode == 'payment': data = { 'saleOrders': get_sale_orders(), 'currencies': currencies, 'customers': get_customers(image_type=image_type), 'journals': get_journals(), 'paymentTerms': get_payment_terms(), 'banks': get_banks(), 'bankPaymentTypes': get_bank_payment_types(), 'chequeTypes': get_cheque_types(), 'stores': get_stores() } # Take data for delivery if mode == 'product_delivery': data = { 'stockPickings': get_pickings() } return make_gzip_response(data) ##################################################################### # ╔═╗╔═╗╔╦╗ ╔═╗╦═╗╔═╗╔╦╗╦ ╦╔═╗╔╦╗╔═╗ ╔╦╗╔═╗╔╦╗╔═╗ ╔═╗╔╗╔╦ ╦ ╦ # # ║ ╦║╣ ║ ╠═╝╠╦╝║ ║ ║║║ ║║ ║ ╚═╗ ║║╠═╣ ║ ╠═╣ ║ ║║║║║ ╚╦╝ # # ╚═╝╚═╝ ╩ ╩ ╩╚═╚═╝═╩╝╚═╝╚═╝ ╩ ╚═╝ ═╩╝╩ ╩ ╩ ╩ ╩ ╚═╝╝╚╝╩═╝╩ # ##################################################################### @http.route('/eiru_sales/get_images', auth='user', methods=['GET'], cors='*') def _get_images_only(self, **kw): from res_config import get_pos_config image_type = str(get_pos_config().get('imageType')) # Imports from res_partner import get_customers from product_template import get_products return make_gzip_response({ 'customers': get_customers(image_type=image_type), 'products': get_products(image_type=image_type) }) ################################################################################################# # ╔═╗╦═╗╔═╗╔═╗╔╦╗╔═╗ ╔═╗╦ ╦╔═╗╔╦╗╔═╗╔╦╗╔═╗╦═╗ ╔═╗╔╗╔╔╦╗ ╦═╗╔═╗╔╦╗╦ ╦╦═╗╔╗╔ ╔╦╗╔═╗╔╦╗╔═╗ # # ║ ╠╦╝║╣ ╠═╣ ║ ║╣ ║ ║ ║╚═╗ ║ ║ ║║║║║╣ ╠╦╝ ╠═╣║║║ ║║ ╠╦╝║╣ ║ ║ ║╠╦╝║║║ ║║╠═╣ ║ ╠═╣ # # ╚═╝╩╚═╚═╝╩ ╩ ╩ ╚═╝ ╚═╝╚═╝╚═╝ ╩ ╚═╝╩ ╩╚═╝╩╚═ ╩ ╩╝╚╝═╩╝ ╩╚═╚═╝ ╩ ╚═╝╩╚═╝╚╝ ═╩╝╩ ╩ ╩ ╩ ╩ # ################################################################################################# @http.route('/eiru_sales/create_customer', type='json', auth='user', methods=['POST'], cors='*') def _create_customer(self, **kw): from res_partner import create_customer self.make_info_log('Creating customer') return create_customer(kw) ############################################# # ╔═╗╔═╗╦ ╦╔═╗ ╔═╗╔═╗╔╦╗╔╦╗╦╔╗╔╔═╗╔═╗ # # ╚═╗╠═╣╚╗╔╝║╣ ╚═╗║╣ ║ ║ ║║║║║ ╦╚═╗ # # ╚═╝╩ ╩ ╚╝ ╚═╝ ╚═╝╚═╝ ╩ ╩ ╩╝╚╝╚═╝╚═╝ # ############################################# @http.route('/eiru_sales/save_settings', type='json', auth='user', methods=['POST'], cors='*') def _save_config(self, **kw): from res_config import save_pos_config self.make_info_log('save settings') return save_pos_config(kw) ############################################################################################# # ╔═╗╦═╗╔═╗╔═╗╔╦╗╔═╗ ╔═╗╦═╗╔═╗╔╦╗╦ ╦╔═╗╔╦╗ ╔═╗╔╗╔╔╦╗ ╦═╗╔═╗╔╦╗╦ ╦╦═╗╔╗╔ ╔╦╗╔═╗╔╦╗╔═╗ # # ║ ╠╦╝║╣ ╠═╣ ║ ║╣ ╠═╝╠╦╝║ ║ ║║║ ║║ ║ ╠═╣║║║ ║║ ╠╦╝║╣ ║ ║ ║╠╦╝║║║ ║║╠═╣ ║ ╠═╣ # # ╚═╝╩╚═╚═╝╩ ╩ ╩ ╚═╝ ╩ ╩╚═╚═╝═╩╝╚═╝╚═╝ ╩ ╩ ╩╝╚╝═╩╝ ╩╚═╚═╝ ╩ ╚═╝╩╚═╝╚╝ ═╩╝╩ ╩ ╩ ╩ ╩ # ############################################################################################# @http.route('/eiru_sales/create_product', type='json', auth='user', methods=['POST'], cors='*') def _create_product(self, **kw): from product_template import create_product return create_product(kw) ############################################# # ╔═╗╦╔╗╔╦╔═╗╦ ╦ ╔═╗╦═╗╔═╗╔═╗╔═╗╔═╗╔═╗ # # ╠╣ ║║║║║╚═╗╠═╣ ╠═╝╠╦╝║ ║║ ║╣ ╚═╗╚═╗ # # ╚ ╩╝╚╝╩╚═╝╩ ╩ ╩ ╩╚═╚═╝╚═╝╚═╝╚═╝╚═╝ # ############################################# @http.route('/eiru_sales/finish', type='json', auth='user', methods=['POST'], cors='*') def _finish(self, **kw): self.make_info_log('Finishing...') # Imports from server_datetime import get_date, get_datetime ''' ╔═╗╦╔╗╔╦╔═╗╦ ╦ ╔╗ ╦ ╦╔╦╗╔═╗╔═╗╔╦╗ ╔═╗╔═╗╔═╗ ╔═╗╦═╗╔═╗╔═╗╔═╗╔╦╗╦ ╦╦═╗╔═╗ ╠╣ ║║║║║╚═╗╠═╣ ╠╩╗║ ║ ║║║ ╦║╣ ║ ╠═╝║ ║╚═╗ ╠═╝╠╦╝║ ║║ ║╣ ║║║ ║╠╦╝║╣ ╚ ╩╝╚╝╩╚═╝╩ ╩ ╚═╝╚═╝═╩╝╚═╝╚═╝ ╩ ╩ ╚═╝╚═╝ ╩ ╩╚═╚═╝╚═╝╚═╝═╩╝╚═╝╩╚═╚═╝ ''' def finish_budget_pos(journal_id, customer_id, cart_items, date_now, payment_term_id, warehouse_id, customer_pricelist_id, user_id): # Imports from account_journal import get_currency from sale_order import create_sale_from_cart # Get currency currency_id = get_currency(journal_id) if not currency_id: currency_id = r.env.user.company_id.currency_id.id self.make_info_log('[OK] Getting journal') # Create sale order sale_order = create_sale_from_cart(customer_id, cart_items, date_now, currency_id, payment_term_id, warehouse_id, customer_pricelist_id, user_id) self.make_info_log('[OK] Creating sale order') return (sale_order.id, currency_id) ''' ╔═╗╦╔╗╔╦╔═╗╦ ╦ ╔═╗╔═╗╦ ╔═╗ ╔═╗╔═╗╔═╗ ╔═╗╦═╗╔═╗╔═╗╔═╗╔╦╗╦ ╦╦═╗╔═╗ ╠╣ ║║║║║╚═╗╠═╣ ╚═╗╠═╣║ ║╣ ╠═╝║ ║╚═╗ ╠═╝╠╦╝║ ║║ ║╣ ║║║ ║╠╦╝║╣ ╚ ╩╝╚╝╩╚═╝╩ ╩ ╚═╝╩ ╩╩═╝╚═╝ ╩ ╚═╝╚═╝ ╩ ╩╚═╚═╝╚═╝╚═╝═╩╝╚═╝╩╚═╚═╝ ''' 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): # Create budget 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) # Imports from sale_order import ( confirm_sale_order, done_sale_order ) from account_invoice import ( create_invoice, create_invoice_move_lines, number_invoice, close_invoice ) from account_move import create_account_move from account_voucher import create_account_voucher from account_bank_statement import create_bank_statement from res_bank_payment import create_bank_payment_statement # Confirm sale confirm_sale_order(sale_order_id) self.make_info_log('[OK] Confirm sale order') # Create invoice invoice = create_invoice(sale_order_id, currency_id, user_id, date_now) self.make_info_log('[OK] Creating invoice') # Create invoice move lines invoice_move_lines = create_invoice_move_lines(invoice.id, payment, date_now) self.make_info_log('[OK] Creating invoice move lines') # Create account move account_move = create_account_move(invoice.id, invoice_move_lines) self.make_info_log('[OK] Creating account move') # Number invoice number_invoice(invoice.id) self.make_info_log('[OK] Number invoice') # Create account voucher account_voucher = create_account_voucher(account_move.id, journal_id, currency_id, payment) self.make_info_log('[OK] Creating account voucher') # Done sale order done_sale_order(sale_order_id) self.make_info_log('[OK] Done sale order') # Close invoice close_invoice(invoice.id) self.make_info_log('[OK] Closing invoice') # Create bank statement invoice.create_bank_statement(date_now, r.context.get('uid'), account_voucher, None) self.make_info_log('[OK] Creating account bank statement') # Create bank payment statement if payment_method == 'Banco': create_bank_payment_statement(bank_payment_data, invoice.id, account_voucher.id) self.make_info_log('[OK] Creating bank payment statement') ''' ╔═╗╦╔╗╔╦╔═╗╦ ╦ ╔═╗╦═╗╔═╗╔╦╗╦ ╦╔═╗╔╦╗ ╔═╗╦╔═╗╦╔═╦╔╗╔╔═╗ ╠╣ ║║║║║╚═╗╠═╣ ╠═╝╠╦╝║ ║ ║║║ ║║ ║ ╠═╝║║ ╠╩╗║║║║║ ╦ ╚ ╩╝╚╝╩╚═╝╩ ╩ ╩ ╩╚═╚═╝═╩╝╚═╝╚═╝ ╩ ╩ ╩╚═╝╩ ╩╩╝╚╝╚═╝ ''' def finish_process_picking(journal_id, customer_id, cart_items, date_now, payment_term_id, warehouse_id, customer_pricelist_id, user_id): sale_order_id, _ = finish_budget_pos(journal_id, customer_id, cart_items, date_now, payment_term_id, warehouse_id, customer_pricelist_id, user_id) # Imports from sale_order import confirm_sale_order, force_assign_picking # Confirm sale confirm_sale_order(sale_order_id) self.make_info_log('[OK] Confirm sale order') # Force assign picking force_assign_picking(sale_order_id) self.make_info_log('[OK] Pick assign') ''' ╔═╗╦╔╗╔╦╔═╗╦ ╦ ╔═╗╔═╗╦ ╦╔╦╗╔═╗╔╗╔╔╦╗ ╠╣ ║║║║║╚═╗╠═╣ ╠═╝╠═╣╚╦╝║║║║╣ ║║║ ║ ╚ ╩╝╚╝╩╚═╝╩ ╩ ╩ ╩ ╩ ╩ ╩ ╩╚═╝╝╚╝ ╩ ''' def finish_payment(sale_order_id, currency_id, date_now, payment_term_id, user_id, payment, payment_method, bank_payment_data): # Imports from sale_order import done_sale_order from account_invoice import ( create_invoice, create_invoice_move_lines, number_invoice, close_invoice ) from account_move import create_account_move from account_voucher import create_account_voucher from account_bank_statement import create_bank_statement from res_bank_payment import create_bank_payment_statement # Payment term sale_order = r.env['sale.order'].browse(sale_order_id) sale_order.write({ 'payment_term': payment_term_id }) # Create invoice invoice = create_invoice(sale_order_id, currency_id, user_id, date_now, False) self.make_info_log('[OK] Creating invoice') # Create invoice move lines invoice_move_lines = create_invoice_move_lines(invoice.id, payment, date_now) self.make_info_log('[OK] Creating invoice move lines') # Create account move account_move = create_account_move(invoice.id, invoice_move_lines) self.make_info_log('[OK] Creating account move') # Number invoice number_invoice(invoice.id) self.make_info_log('[OK] Number invoice') # Create account voucher account_voucher = create_account_voucher(account_move.id, journal_id, currency_id, payment) self.make_info_log('[OK] Creating account voucher') # Done sale order done_sale_order(sale_order_id) self.make_info_log('[OK] Done sale order') # Close invoice close_invoice(invoice.id) self.make_info_log('[OK] Closing invoice') # Create bank statement invoice.create_bank_statement(date_now, r.context.get('uid'), account_voucher, None) self.make_info_log('[OK] Creating account bank statement') # Create bank payment statement if payment_method == 'Banco': create_bank_payment_statement(bank_payment_data, invoice.id, account_voucher.id) self.make_info_log('[OK] Creating bank payment statement') ''' ╔═╗╦╔╗╔╦╔═╗╦ ╦ ╔═╗╦═╗╔═╗╔╦╗╦ ╦╔═╗╔╦╗ ╔╦╗╔═╗╦ ╦╦ ╦╔═╗╦═╗╦ ╦ ╠╣ ║║║║║╚═╗╠═╣ ╠═╝╠╦╝║ ║ ║║║ ║║ ║ ║║║╣ ║ ║╚╗╔╝║╣ ╠╦╝╚╦╝ ╚ ╩╝╚╝╩╚═╝╩ ╩ ╩ ╩╚═╚═╝═╩╝╚═╝╚═╝ ╩ ═╩╝╚═╝╩═╝╩ ╚╝ ╚═╝╩╚═ ╩ ''' def finish_product_delivery(stock_picking_id, user_id): from stock_picking import confirm_picking confirm_picking(stock_picking_id, user_id) ''' ╔═╗╦═╗╔═╗╔═╗╔═╗╔═╗╔═╗ ╔═╗╦╔╗╔╦╔═╗╦ ╦ ╔╦╗╔═╗╔═╗╦╔═╗╦╔═╗╔╗╔ ╠═╝╠╦╝║ ║║ ║╣ ╚═╗╚═╗ ╠╣ ║║║║║╚═╗╠═╣ ║║║╣ ║ ║╚═╗║║ ║║║║ ╩ ╩╚═╚═╝╚═╝╚═╝╚═╝╚═╝ ╚ ╩╝╚╝╩╚═╝╩ ╩ ═╩╝╚═╝╚═╝╩╚═╝╩╚═╝╝╚╝ ''' data = kw.get('data', []) date_now = get_date() for row in data: mode = row.get('mode', 'sale') journal_id = row.get('journalId', None) customer_id = row.get('customerId', None) customer_pricelist_id = row.get('customerPricelistId', None) cart_items = row.get('items', []) payment_term_id = row.get('paymentTermId', None) warehouse_id = row.get('warehouseId', None) user_id = row.get('userId', None) payment = float(row.get('payment', 0.0)) payment_method = row.get('paymentMethod', 'Efectivo') bank_payment_data = row.get('bankPaymentData', {}) if warehouse_id is None: warehouse_id = r.env['sale.order']._get_default_warehouse() if mode == 'budget': finish_budget_pos(journal_id, customer_id, cart_items, date_now, payment_term_id, warehouse_id, customer_pricelist_id, user_id) if mode == 'sale': 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) if mode == 'product_picking': finish_process_picking(journal_id, customer_id, cart_items, date_now, payment_term_id, warehouse_id, customer_pricelist_id, user_id) if mode == 'payment': sale_order_id = row.get('saleOrderId', None) currency_id = row.get('currencyId', None) finish_payment(sale_order_id, currency_id, date_now, payment_term_id, user_id, payment, payment_method, bank_payment_data) if mode == 'product_delivery': stock_picking_id = row.get('stockPickingId', None) finish_product_delivery(stock_picking_id, user_id) return { 'process': True, 'date': get_datetime() }