2018-07-14 21:55:03 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-05-20 08:04:55 +02:00
|
|
|
from trytond.wizard import Wizard, StateAction, StateView, StateTransition, \
|
|
|
|
Button
|
|
|
|
from trytond.pool import Pool, PoolMeta
|
|
|
|
from trytond.transaction import Transaction
|
|
|
|
import logging
|
|
|
|
from trytond.model import (ModelView, ModelSQL, ModelSingleton, ValueMixin,
|
|
|
|
fields)
|
|
|
|
from trytond.modules.company.model import (
|
|
|
|
CompanyMultiValueMixin, CompanyValueMixin)
|
2018-05-20 08:08:38 +02:00
|
|
|
from trytond.model.workflow import Workflow
|
|
|
|
from trytond.modules.account.tax import TaxableMixin
|
2018-05-20 22:12:18 +02:00
|
|
|
from trytond.pyson import Eval
|
2020-06-13 23:49:56 +02:00
|
|
|
from .exceptions import ErroresGenerales
|
|
|
|
from trytond.i18n import gettext
|
|
|
|
|
2018-05-20 08:04:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-06-13 23:49:56 +02:00
|
|
|
__all__ = ['OneClickForPurchase','Configuration','Purchase']
|
2018-05-20 08:04:55 +02:00
|
|
|
|
2018-05-20 08:48:11 +02:00
|
|
|
|
2020-07-25 14:55:40 +02:00
|
|
|
class Configuration(metaclass=PoolMeta):
|
2018-07-14 23:18:08 +02:00
|
|
|
'Purchase Configuration'
|
|
|
|
|
|
|
|
__name__ = 'purchase.configuration'
|
|
|
|
|
|
|
|
one_click_to_location = fields.Many2One('stock.location', 'Destino de Compra',
|
|
|
|
domain=[('active','=',True),('type','=','storage')],
|
|
|
|
help='Destino de Compra para el modulo one_click_for_purchase'
|
|
|
|
)
|
2018-05-20 08:04:55 +02:00
|
|
|
|
2020-07-25 14:55:40 +02:00
|
|
|
class Purchase(metaclass=PoolMeta):
|
2018-07-14 23:18:08 +02:00
|
|
|
'Purchase'
|
|
|
|
|
|
|
|
__name__ = 'purchase.purchase'
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __setup__(cls):
|
|
|
|
super(Purchase, cls).__setup__()
|
|
|
|
cls._buttons.update({
|
|
|
|
'draft_to_done': {
|
|
|
|
'invisible': ~Eval('state').in_(['draft']),
|
|
|
|
'readonly': ~Eval('lines', []),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@ModelView.button
|
|
|
|
def draft_to_done(cls,purchases):
|
|
|
|
'''Lleva las compras (ids) desde el estado borrador hasta finalizados aprobando
|
|
|
|
las facturas y movimientos necesarios.'''
|
|
|
|
|
|
|
|
Invoice = Pool().get('account.invoice')
|
|
|
|
Move = Pool().get('stock.move')
|
|
|
|
Configuration = Pool().get('purchase.configuration')
|
|
|
|
configuration = Configuration(1)
|
2020-05-19 20:09:40 +02:00
|
|
|
Shipment = Pool().get('stock.shipment.in.return')
|
2018-05-20 08:04:55 +02:00
|
|
|
|
2018-07-14 23:18:08 +02:00
|
|
|
if purchases:
|
|
|
|
for purchase in purchases:
|
|
|
|
if purchase.state != 'draft' or len(purchase.lines) < 1:
|
2020-06-13 23:49:56 +02:00
|
|
|
raise ErroresGenerales(gettext('one_click_for_purchase.msg_noborrador_sinlineas').format(s=purchase.number))
|
2018-07-14 23:18:08 +02:00
|
|
|
return
|
2018-07-14 23:21:23 +02:00
|
|
|
if not purchase.invoice_address:
|
2020-06-13 23:49:56 +02:00
|
|
|
raise ErroresGenerales(gettext('one_click_for_purchase.msg_error_direccion').format(p=purchase.id))
|
2018-07-14 23:21:23 +02:00
|
|
|
return
|
2018-07-14 23:18:08 +02:00
|
|
|
else:
|
2020-06-13 23:49:56 +02:00
|
|
|
raise ErroresGenerales(gettext('one_click_for_purchase.msg_error_ventanas'))
|
2018-07-14 23:18:08 +02:00
|
|
|
return
|
2018-05-20 08:04:55 +02:00
|
|
|
|
2018-07-14 23:18:08 +02:00
|
|
|
cls.set_purchase_date(purchases)#pone fecha de compra a los registros que no lo tienen
|
|
|
|
cls.quote(purchases)
|
|
|
|
cls.confirm(purchases)
|
|
|
|
cls.process(purchases)
|
|
|
|
Transaction().commit()#se graba la informacion para poder acceder a las facturas y los movimientos
|
|
|
|
purchases = cls.browse([x.id for x in purchases])#se vuelve a leer para cargar informacion de la base de datos, verificar si es necesario.
|
|
|
|
purchases = [x for x in purchases if (x.state == 'processing' and len(x.lines) > 0) ]
|
|
|
|
if len(purchases) <= 0 :
|
2020-06-13 23:49:56 +02:00
|
|
|
raise ErroresGenerales(gettext('one_click_for_purchase.msg_fallo_finalizacion'))
|
2018-07-14 23:18:08 +02:00
|
|
|
return
|
|
|
|
for purchase in purchases:
|
|
|
|
invoices = purchase.invoices
|
|
|
|
if invoices:
|
|
|
|
for invoice in invoices:
|
|
|
|
if not invoice.invoice_date:
|
|
|
|
invoice.invoice_date = purchase.purchase_date
|
|
|
|
Invoice.save([invoice])
|
|
|
|
Invoice.validate_invoice(invoices)
|
|
|
|
Invoice.post(invoices)
|
|
|
|
moves = purchase.moves
|
|
|
|
if moves:
|
|
|
|
for move in moves:
|
2020-05-19 20:09:40 +02:00
|
|
|
if move.shipment:
|
|
|
|
Shipment(move.shipment.id).to_location = configuration.one_click_to_location
|
|
|
|
Shipment(move.shipment.id).effective_date = purchase.purchase_date
|
|
|
|
Move.save([move])
|
|
|
|
elif move.to_location != configuration.one_click_to_location:
|
2018-07-14 23:18:08 +02:00
|
|
|
move.to_location = configuration.one_click_to_location
|
2018-07-14 23:22:26 +02:00
|
|
|
move.effective_date = purchase.purchase_date
|
2018-07-14 23:18:08 +02:00
|
|
|
Move.save([move])
|
|
|
|
Move.do(moves)
|
2018-05-20 08:08:38 +02:00
|
|
|
|
2020-06-06 21:01:28 +02:00
|
|
|
|
2020-06-13 23:49:56 +02:00
|
|
|
class OneClickForPurchase(Wizard):
|
|
|
|
'''Compra en un click.
|
|
|
|
|
|
|
|
Procesa una compra en estado de borrador llevandola al estado "procesada",
|
|
|
|
creando las Facturas y Movimientos de Stock, además contabiliza la factura
|
|
|
|
y cambia el movimiento de stock de la entrada al almacenamiento y finaliza los movimientos de stock.
|
|
|
|
'''
|
|
|
|
|
|
|
|
__name__ = 'purchase.one_click_for_purchase'
|
|
|
|
|
|
|
|
start = StateTransition()
|
|
|
|
|
|
|
|
def transition_start(self):
|
|
|
|
Purchase = Pool().get('purchase.purchase')
|
|
|
|
purchases = Purchase.browse(Transaction().context['active_ids'])
|
|
|
|
Purchase.draft_to_done(purchases)
|
|
|
|
return 'end'
|