123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- # -*- coding: utf-8 -*-
- from openerp import http
- from openerp.http import request as r
- from helpers import (
- get_date,
- get_current_user,
- get_currencies_from_journal,
- get_journals,
- get_suppliers,
- get_products,
- get_picking_types,
- get_payment_terms,
- get_banks,
- get_bank_payment_types,
- get_cheque_types,
- get_pickings,
- make_gzip_response
- )
- import logging
- LOGGER = logging.getLogger(__name__)
- class Purchases(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_purchases/init', auth='user', methods=['GET'], cors='*')
- def init_purchase(self, **kw):
- self.make_info_log('Preparing data to {}'.format(kw.get('mode')))
- mode = kw.get('mode', 'unknown')
- data = None
- if mode == 'purchase' or mode == 'expense':
- data = {
- 'date': get_date(),
- 'user': get_current_user(),
- 'currencies': get_currencies_from_journal(),
- 'journals': get_journals(),
- 'suppliers': get_suppliers(),
- 'products': get_products(kw.get('mode')),
- 'pickingTypes': get_picking_types(),
- 'paymentTerms': get_payment_terms(),
- 'banks': get_banks(),
- 'bankPaymentTypes': get_bank_payment_types(),
- 'chequeTypes': get_cheque_types()
- }
- if mode == 'product_picking':
- data = {
- 'date': get_date(),
- 'user': get_current_user(),
- 'currencies': get_currencies_from_journal(),
- 'suppliers': get_suppliers(),
- 'products': get_products()
- }
- if mode == 'payment':
- data = {
- 'currencies': get_currencies_from_journal(),
- 'suppliers': get_suppliers(),
- 'journals': get_journals(),
- 'paymentTerms': get_payment_terms(),
- 'banks': get_banks(),
- 'bankPaymentTypes': get_bank_payment_types(),
- 'chequeTypes': get_cheque_types()
- }
-
- if mode == 'product_taking':
- data = {
- 'stockPickings': get_pickings()
- }
- return make_gzip_response(data)
- # '''
- # Create supplier and return data
- # '''
- # @http.route('/eiru_purchases/create_supplier', type='json', auth='user', methods=['POST'], cors='*')
- # def create_supplier(self, **kw):
- # self.make_info_log('Creating supplier')
- # '''
- # Create product and return data
- # '''
- # @http.route('/eiru_purchases/create_product', type='json', auth='user', methods=['POST'], cors='*')
- # def create_product(self, **kw):
- # self.make_info_log('Creating product')
- # '''
- # Number to invoice
- # '''
- # def validate_invoice(self, invoice_ids, type=None):
- # assert len(invoice_ids) == 1
- # invoice = request.env['account.invoice'].browse(invoice_ids)
- # invoice.action_number()
- # invoice.invoice_validate()
- # if type != 'purchase':
- # name = 'GASTO' + invoice.name[invoice.name.index('/'):]
- # invoice.write({
- # 'number': name,
- # 'internal_number': name
- # })
- # '''
- # Create voucher
- # '''
- # def create_account_voucher(self, account_move_id, journal_id, currency_id, paid_amount):
- # account_move = request.env['account.move'].browse(account_move_id)
- # account_journal = request.env['account.journal'].browse(journal_id)
- # account_voucher = request.env['account.voucher'].create({
- # 'reference': account_move.name,
- # 'type': 'payment',
- # 'journal_id': account_journal.id,
- # 'company_id': account_move.company_id.id,
- # 'pre_line': True,
- # 'amount': paid_amount,
- # 'period_id': account_move.period_id.id,
- # 'date': account_move.date,
- # 'partner_id': account_move.partner_id.id,
- # 'account_id': account_journal.default_debit_account_id.id,
- # 'currency_id': currency_id,
- # 'line_dr_ids': [[0, False, {
- # 'date_due': l.date_maturity,
- # 'account_id': l.account_id.id,
- # 'date_original': l.invoice.date_invoice,
- # 'move_line_id': l.id,
- # 'amount_original': abs(l.credit or l.debit or 0.0),
- # 'amount_unreconciled': abs(l.amount_residual),
- # 'amount': abs(l.credit) if account_move.date == l.date_maturity else 0.0,
- # 'reconcile': account_move.date == l.date_maturity,
- # 'currency_id': currency_id
- # }] for l in account_move.line_id]
- # })
- # account_voucher.action_move_line_create()
- # return account_voucher
- # '''
- # Close a invoice
- # '''
- # def close_invoice(self, invoice_ids):
- # assert len(invoice_ids) == 1
- # invoice = request.env['account.invoice'].browse(invoice_ids)
- # if invoice.residual == 0:
- # invoice.write({
- # 'state': 'paid'
- # })
- # '''
- # Create account bank statement
- # '''
- # def create_bank_statement(self, account_voucher_id, account_bank_statement_lines, date_today):
- # account_voucher = request.env['account.voucher'].browse(account_voucher_id)
- # account_bank_statement = request.env['account.bank.statement'].search([('journal_id', '=', account_voucher.journal_id.id), ('date', '=', date_today)])
- # account_bank_statement_values = {
- # 'date': date_today,
- # 'user_id': request.env.user.id,
- # 'journal_id': account_voucher.journal_id.id,
- # 'period_id': account_voucher.period_id.id,
- # 'line_ids': account_bank_statement_lines,
- # 'state': 'open' if account_voucher.journal_id.type == 'cash' else 'draft'
- # }
- # if account_bank_statement:
- # size = len(account_bank_statement)
- # if size == 1:
- # account_bank_statement.write(account_bank_statement_values)
- # else:
- # account_bank_statement[size - 1].write(account_bank_statement_values)
- # else:
- # account_bank_statement.create(account_bank_statement_values)
- # return account_bank_statement
- # '''
- # Create account bank statement lines
- # '''
- # def create_bank_statement_lines(self, account_voucher_id, reference=None):
- # account_voucher = request.env['account.voucher'].browse(account_voucher_id)
- # amount = account_voucher.amount
-
- # if account_voucher.type == 'payment':
- # amount = amount * -1
- # return [[0, False, {
- # 'name': account_voucher.reference,
- # 'amount': amount,
- # 'partner_id': account_voucher.partner_id.id,
- # 'voucher_id': account_voucher.id,
- # 'journal_id': account_voucher.journal_id.id,
- # 'account_id': account_voucher.account_id.id,
- # 'journal_entry_id': account_voucher.move_id.id,
- # 'currency_id': account_voucher.currency_id.id,
- # 'ref': 'POS/' + (reference or '')
- # }]]
- # '''
- # Purchase processing resource route
- # '''
- # @http.route('/eiru_purchases/process', type='json', auth='user', methods=['POST'], cors='*')
- # def process_purchase(self, **kw):
- # mode = kw.get('mode')
- # self.make_info_log('Processing {}'.format(mode))
- # # Get date
- # date_now = datetime.now(self.get_timezone()).strftime(DATE_FORMAT)
- # self.make_info_log('Getting date')
- # # Get currency
- # currency_id = self.get_currency_id(kw.get('journalId'))
- # self.make_info_log('Getting currency')
- # # Get pricelist
- # pricelist_id = self.get_pricelist_id(currency_id)
- # self.make_info_log('Product pricelist checked')
- # invoice = None
- # if mode == 'purchase':
- # # Create purchase order
- # purchase_order = self.create_purchase_order(kw.get('supplierId'), kw.get('items'), date_now, currency_id, pricelist_id, kw.get('paymentTermId'))
- # self.make_info_log('Purchase order created')
- # # Confirm purchase
- # self.confirm_purchase_order(purchase_order.id)
- # self.make_info_log('Purchase order confirmed')
-
- # invoice = purchase_order.invoice_ids
- # invoice.write({
- # 'supplier_invoice_number': kw.get('supplierInvoiceNumber', None)
- # })
- # else:
- # invoice = self.create_invoice(kw.get('supplierId'), kw.get('items'), currency_id, kw.get('paymentTermId'), kw.get('supplierInvoiceNumber', None))
- # self.make_info_log('Invoice created')
- # invoice_ids = invoice.mapped(lambda x: x.id)
-
- # # Validate invoice
- # self.prepare_invoice(invoice_ids, currency_id, date_now)
- # self.make_info_log('Invoice prepared')
- # # Create invoice move lines
- # invoice_move_lines = self.create_invoice_move_lines(invoice_ids, float(kw.get('payment')), date_now)
- # self.make_info_log('Invoice move lines created')
- # # Create account move
- # account_move = self.create_account_move(invoice_ids, invoice_move_lines)
- # self.make_info_log('Account move created')
- # # Validate invoice
- # self.validate_invoice(invoice_ids, mode)
- # self.make_info_log('Invoice validated')
- # # Create account voucher
- # account_voucher = self.create_account_voucher(account_move.id, kw.get('journalId'), currency_id, float(kw.get('payment')))
- # self.make_info_log('Account voucher created')
- # # Close invoice
- # self.close_invoice(invoice_ids)
- # self.make_info_log('Attempt close invoice')
- # # Create account bank statement lines
- # account_bank_statement_lines = self.create_bank_statement_lines(account_voucher.id)
- # self.make_info_log('Bank statement lines created')
- # # Create account bank statement
- # self.create_bank_statement(account_voucher.id, account_bank_statement_lines, date_now)
- # self.make_info_log('Bank statement created')
- # return {
- # 'status': 'ok'
- # }
|