Skip to content

Instantly share code, notes, and snippets.

@ianjosephwilson
Created January 26, 2013 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianjosephwilson/4644279 to your computer and use it in GitHub Desktop.
Save ianjosephwilson/4644279 to your computer and use it in GitHub Desktop.
import logging
from decimal import Decimal
from trytond.modules.company import CompanyReport
from trytond.wizard import Wizard, StateAction
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction
__metaclass__ = PoolMeta
log = logging.getLogger(__name__)
class CloseSale(Wizard):
""" Close Sale """
__name__ = 'sale_wizard.close'
start = StateAction('sale_wizard.close_sale_keyword')
@classmethod
def __setup__(cls):
super(CloseSale, cls).__setup__()
cls._error_messages.update({
'shipment_not_assigned': 'The shipment with code %s must be assigned.',
})
def transition_start(self):
"""
Open un-opened invoices.
Pay amount of each invoice.
Mark as paid.
"""
Sale = Pool().get('sale.sale')
ShipmentOut = Pool().get('stock.shipment.out')
Invoice = Pool().get('account.invoice')
Date = Pool().get('ir.date')
Journal = Pool().get('account.journal')
pay_date = Date.today()
pay_cash_journal = Journal.search([('type', '=', 'cash')])[0]
log.error('finalizing sales')
sale_ids = Transaction().context['active_ids']
if not sale_ids:
return action, {}
sales = Sale.browse(sale_ids)
# Finalize the invoices.
log.error('finalizing invoices')
invoices_to_open = []
invoices_to_pay = []
paid_invoices = []
for invoice in [invoice for sale in sales for invoice in sale.invoices]:
if invoice.state in ['done', 'canceled']:
continue
if invoice.state != 'open':
invoices_to_open.append(invoice)
invoices_to_pay.append(invoice)
Invoice.open(invoices_to_open)
for invoice in invoices_to_pay:
log.error('pay invoice')
invoice.pay_invoice(**{
'amount': invoice.amount_to_pay_today or invoice.amount_to_pay,
'journal': pay_cash_journal,
'amount_second_currency': Decimal('0.0'),
'second_currency': None,
'date': pay_date,
'description': invoice.number
})
paid_invoices.append(invoice)
log.error('pay')
Invoice.paid(paid_invoices)
# Finalize the shipments.
log.error('finalizing shipments')
shipments_to_pack = []
shipments_to_do = []
for shipment in [shipment for sale in sales for shipment in \
sale.shipments]:
if shipment.state in ['done', 'canceled']:
continue
if shipment.state in ['draft', 'waiting']:
self.raise_user_error(
'shipment_not_assigned',
error_description='shipment_not_assigned',
error_description_args=shipment.code)
if shipment.state != 'packed':
shipments_to_pack.append(shipment)
shipments_to_do.append(shipment)
log.error('pack')
ShipmentOut.pack(shipments_to_pack)
log.error('done')
ShipmentOut.done(shipments_to_do)
log.error('the end')
return 'end'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment