Created
January 26, 2013 20:03
-
-
Save ianjosephwilson/4644279 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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