2017-02-07 14:51:31 +01:00
|
|
|
# This file is part of sale_confirmed2quotation module for Tryton.
|
|
|
|
# The COPYRIGHT file at the top level of this repository contains
|
|
|
|
# the full copyright notices and license terms.
|
|
|
|
from trytond.model import Workflow, ModelView
|
|
|
|
from trytond.pool import PoolMeta, Pool
|
2017-03-17 11:48:46 +01:00
|
|
|
from trytond.pyson import Eval
|
2017-02-07 14:51:31 +01:00
|
|
|
|
|
|
|
__all__ = ['Sale']
|
|
|
|
|
|
|
|
|
|
|
|
class Sale:
|
|
|
|
__metaclass__ = PoolMeta
|
|
|
|
__name__ = 'sale.sale'
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __setup__(cls):
|
|
|
|
super(Sale, cls).__setup__()
|
|
|
|
cls._transitions.add(
|
|
|
|
('processing', 'draft'),
|
|
|
|
)
|
2017-03-17 11:48:46 +01:00
|
|
|
cls._buttons['draft']['invisible'] = ~Eval('state').in_(
|
|
|
|
['cancel', 'quotation', 'processing'])
|
2017-02-07 14:51:31 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def draft(cls, sales):
|
|
|
|
pool = Pool()
|
|
|
|
Invoice = pool.get('account.invoice')
|
|
|
|
ShipmentOut = pool.get('stock.shipment.out')
|
|
|
|
ShipmentOutReturn = pool.get('stock.shipment.out.return')
|
|
|
|
|
|
|
|
# Check if production module is installed
|
2017-03-17 11:48:46 +01:00
|
|
|
production_installed = False
|
|
|
|
if hasattr(sales[0], 'productions'):
|
|
|
|
production_installed = True
|
2017-02-07 14:51:31 +01:00
|
|
|
Production = pool.get('production')
|
|
|
|
|
2017-03-17 11:48:46 +01:00
|
|
|
to_write = []
|
2017-02-07 14:51:31 +01:00
|
|
|
to_delete_invoices = []
|
|
|
|
to_delete_shipments = []
|
|
|
|
to_delete_shipments_return = []
|
|
|
|
to_delete_productions = []
|
|
|
|
for sale in sales:
|
|
|
|
if sale.state != 'processing':
|
|
|
|
continue
|
|
|
|
if sale.invoices:
|
|
|
|
to_delete_invoices += sale.invoices
|
|
|
|
|
2017-10-06 17:50:40 +02:00
|
|
|
if sale.shipments or sale.shipment_returns:
|
2017-02-07 14:51:31 +01:00
|
|
|
to_delete_shipments += sale.shipments
|
|
|
|
to_delete_shipments_return += sale.shipment_returns
|
|
|
|
|
|
|
|
if production_installed:
|
|
|
|
to_delete_productions += sale.productions
|
|
|
|
to_write.extend(([sale], {'state': 'draft'}))
|
|
|
|
|
|
|
|
if to_write:
|
|
|
|
cls.write(*to_write)
|
2017-03-17 11:48:46 +01:00
|
|
|
if to_delete_invoices:
|
2017-02-07 14:51:31 +01:00
|
|
|
Invoice.delete(to_delete_invoices)
|
2017-03-17 11:48:46 +01:00
|
|
|
if to_delete_shipments:
|
2017-02-07 14:51:31 +01:00
|
|
|
ShipmentOut.delete(to_delete_shipments)
|
2017-03-17 11:48:46 +01:00
|
|
|
if to_delete_shipments_return:
|
2017-02-07 14:51:31 +01:00
|
|
|
ShipmentOutReturn.delete(to_delete_shipments_return)
|
2017-03-17 11:48:46 +01:00
|
|
|
if production_installed and to_delete_productions:
|
|
|
|
Production.delete(to_delete_productions)
|
2017-02-07 14:51:31 +01:00
|
|
|
|
|
|
|
super(Sale, cls).draft(sales)
|