Skip to content

Instantly share code, notes, and snippets.

@shofetim
Created April 3, 2014 17:01
Show Gist options
  • Save shofetim/9958484 to your computer and use it in GitHub Desktop.
Save shofetim/9958484 to your computer and use it in GitHub Desktop.
Credits for K2
# What should be done?
"""
If the original order is paid
Deduct the amount of the destroyed order from the debit of the
replacement order, leaving a debit or credit memo.
else
add a credit to the account, for the same value as the debit of the
destroyed order, associate the debit with that order, and have a memo
of "for destroyed order"
"""
from decimal import Decimal
from accounts_receivable.models import PaymentTermType, Payment, Adjustment
from accounts_receivable.constants import (CREDIT_REASON_PAYMENT,
CREDIT_REASON_DAMAGED)
rt1, rt2 = RouteTrip.objects.filter(route__code='K2', cutoff='2014-03-26 16:02'
).order_by('pull')
#all orders on the first trip, excluding the order that was UPS'd
#and orders 2819850, 2817968 that have been adjusted by hand.
for order in rt1.orders.exclude(pk__in=[2817256, 2819850, 2817968]):
print order
answer = raw_input('Next?')
if ans in 'Yy':
pass
else:
break
try:
# if the original order has already been paid
if Adjustment.objects.filter(causal_order=order, credit__isnull=False,
credit__reason=CREDIT_REASON_PAYMENT
).exists():
original_debit = Adjustment.objects.get(causal_order=order,
debit__isnull=False)
customer = order.customer
replacement_order = rt2.orders.get(customer=customer)
replacement_debit = Adjustment.objects.get(
causal_order=replacement_order,
debit__isnull=False)
amount = replacement_debit.amount - original_debit.amount
if amount >= 0: #keep it as a debit, they still owe, or it is zero
replacement_debit.amount = amount
replacement_debit.save()
else: # we owe them
#keep the (now zero'd) debit for a history,
replacement_debit.amount = Decimal(0)
replacement_debit.save()
#give them a credit
customer.add_credit(amount=amount
reason=CREDIT_REASON_DAMAGED,
order=order,
notes='for destroyed order'
)
else:
# original order not yet paid.
customer = order.customer
debit = Adjustment.objects.get(causal_order=order,
debit__isnull=False)
customer.add_credit(amount=adj.amount
reason=CREDIT_REASON_DAMAGED,
order=order,
notes='for destroyed order')
except as e:
print 'Problem with order %s' % order.pk
print e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment