# -*- coding: utf-8 -*- from openerp import http from openerp.http import request from res_config import get_pos_config, save_pos_config from server_datetime import get_datetime, get_date from res_users import get_current_user from account_journal import get_journals, get_currencies_from_journals, get_currency from res_partner import get_customers, create_customer from product_template import get_products, create_product 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 sale_order import create_sale_from_cart, confirm_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 from res_store import get_stores 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)) ''' New purchase resource route ''' @http.route('/eiru_sales/init', auth='user', methods=['GET'], cors='*') def _init_sale(self, **kw): self.make_info_log('Sending JSON response') config = get_pos_config() data = { 'settings': config, 'date': get_datetime(), 'user': get_current_user(), 'currencies': get_currencies_from_journals(), 'journals': get_journals(), 'customers': get_customers(image_type=config.get('imageType')), 'products': get_products(image_type=config.get('imageType')), 'paymentTerms': get_payment_terms(), 'banks': get_banks(), 'bankPaymentTypes': get_bank_payment_types(), 'chequeTypes': get_cheque_types(), 'stores': get_stores() } return make_gzip_response(data) ''' Get products data only ''' @http.route('/eiru_sales/get_images', auth='user', methods=['GET'], cors='*') def _get_images_only(self, **kw): image_type = str(get_pos_config().get('imageType')) return make_gzip_response({ 'customers': get_customers(image_type=image_type), 'products': get_products(image_type=image_type) }) ''' Create customer and return data ''' @http.route('/eiru_sales/create_customer', type='json', auth='user', methods=['POST'], cors='*') def _create_customer(self, **kw): self.make_info_log('Creating customer') return create_customer(kw) ''' Save settings ''' @http.route('/eiru_sales/save_settings', type='json', auth='user', methods=['POST'], cors='*') def _save_config(self, **kw): self.make_info_log('save settings') return save_pos_config(kw) ''' Create product and return data ''' @http.route('/eiru_sales/create_product', type='json', auth='user', methods=['POST'], cors='*') def _create_product(self, **kw): return create_product(kw) ''' Process sale ''' @http.route('/eiru_sales/process_sale', type='json', auth='user', methods=['POST'], cors='*') def _process_sale(self, **kw): self.make_info_log('Processing sale...') date_now = get_date() journal_id = kw.get('journalId', None) customer_id = kw.get('customerId', None) cart_items = kw.get('items', []) payment_term_id = kw.get('paymentTermId', None) payment = float(kw.get('payment')) bank_payment_data = kw.get('bankPaymentData', {}) # Get currency currency_id = get_currency(journal_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) self.make_info_log('[OK] Creating sale order') # Check if budget if kw.get('mode', 'sale') != 'sale': return { 'process': True } # 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, 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') # Close invoice close_invoice(invoice.id) self.make_info_log('[OK] Closing invoice') # Create bank statement create_bank_statement(account_voucher.id, date_now) self.make_info_log('[OK] Creating account bank statement') # Create bank payment statement if kw.get('paymentMethod', 'Efectivo') == 'Banco': create_bank_payment_statement(bank_payment_data, currency_id, invoice.date_invoice, journal_id, customer_id) self.make_info_log('[OK] Creating bank payment statement') return { 'process': True, 'name': sale_order.display_name, 'date': get_datetime() }