Last active
December 15, 2015 00:29
-
-
Save ianjosephwilson/5173256 to your computer and use it in GitHub Desktop.
This file contains 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
class CloseSale(Wizard): | |
""" Close Sale """ | |
__name__ = 'sale_wizard.close' | |
start = StateTransition() | |
@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. | |
""" | |
from datetime import datetime | |
timers = {} | |
def start_timer(label): | |
timers[label] = datetime.now() | |
def print_timer(label): | |
print '{0} took {1}'.format(label, datetime.now() - timers[label]) | |
start_timer('transition_start') | |
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] | |
sale_ids = Transaction().context['active_ids'] | |
if not sale_ids: | |
return action, {} | |
start_timer('browse sales') | |
sales = Sale.browse(sale_ids) | |
print_timer('browse sales') | |
# Finalize the invoices. | |
start_timer('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) | |
start_timer('open invoice') | |
Invoice.open(invoices_to_open) | |
print_timer('open invoice') | |
for invoice in invoices_to_pay: | |
start_timer('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 | |
}) | |
print_timer('pay invoice') | |
paid_invoices.append(invoice) | |
start_timer('start paid') | |
Invoice.paid(paid_invoices) | |
print_timer('start paid') | |
print_timer('invoices') | |
start_timer('shipments') | |
# Finalize the 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) | |
start_timer('start pack') | |
ShipmentOut.pack(shipments_to_pack) | |
print_timer('start pack') | |
start_timer('start done') | |
ShipmentOut.done(shipments_to_do) | |
print_timer('start done') | |
print_timer('shipments') | |
print_timer('transition_start') | |
return 'end' |
This file contains 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
browse sales took 0:00:00.000104 | |
open invoice took 0:00:08.759467 | |
pay invoice took 0:00:02.109899 | |
start paid took 0:00:02.267797 | |
invoices took 0:00:13.389540 | |
start pack took 0:00:04.304380 | |
start done took 0:00:05.437914 | |
shipments took 0:00:10.026088 | |
transition_start took 0:00:23.417849 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment