Add check to avoid duplicate quotations in Odoo
- Add the check function by using 'date_order' field in Odoo. - Fix the bug that we use the wrong field name 'order_date'. Change-Id: I001c799e131a25138df28db3f6ae4ef7dfc89674
This commit is contained in:
parent
87797d4aa9
commit
ef277a2f07
|
@ -20,7 +20,6 @@ import ConfigParser
|
|||
import datetime
|
||||
from decimal import Decimal
|
||||
import math
|
||||
import oerplib
|
||||
import os
|
||||
import prettytable
|
||||
import re
|
||||
|
@ -230,6 +229,28 @@ class HelpFormatter(argparse.HelpFormatter):
|
|||
super(HelpFormatter, self).start_section(heading)
|
||||
|
||||
|
||||
def check_odoo_duplicate(shell, partner_id, tenant_id, billing_date):
|
||||
"""Avoid duplicate quotations in odoo.
|
||||
|
||||
The 'date_order' field in 'order' table indicates the billing date of a
|
||||
quotation. Now it's the last day of a month.
|
||||
"""
|
||||
orders = shell.Order.search(
|
||||
[
|
||||
('partner_id', '=', partner_id),
|
||||
('date_order', '=', billing_date),
|
||||
('state', '!=', 'cancel')
|
||||
]
|
||||
)
|
||||
|
||||
if len(orders) >= 1:
|
||||
print('ERROR: order of tenant %s has been already generated. '
|
||||
'Billing date: %s.' % (tenant_id, billing_date))
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
@arg('--tenant-id', type=str, metavar='TENANT_ID',
|
||||
dest='TENANT_ID', required=False,
|
||||
help='The specific tenant which will be quoted.')
|
||||
|
@ -257,6 +278,12 @@ def do_quote(shell, args):
|
|||
|
||||
login_odoo(shell)
|
||||
|
||||
end_timestamp = datetime.datetime.strptime(
|
||||
args.END,
|
||||
'%Y-%m-%dT%H:%M:%S'
|
||||
)
|
||||
billing_date = str((end_timestamp - datetime.timedelta(days=1)).date())
|
||||
|
||||
done = []
|
||||
skip = []
|
||||
|
||||
|
@ -284,21 +311,38 @@ def do_quote(shell, args):
|
|||
print ("Skipping tenant: %s already skipped." % tenant.name)
|
||||
continue
|
||||
|
||||
# Find parter and pricelist of root parter.
|
||||
partner = find_oerp_partner_for_tenant(shell, tenant)
|
||||
if not partner or args.AUDIT:
|
||||
continue
|
||||
root_partner = find_root_partner(shell, partner)
|
||||
pricelist, _ = root_partner['property_product_pricelist']
|
||||
|
||||
# Duplicate quotation check in Odoo.
|
||||
if check_odoo_duplicate(shell, partner['id'], tenant.id,
|
||||
billing_date):
|
||||
continue
|
||||
|
||||
# Get resource usage of tenant.
|
||||
usage = get_tenant_usage(shell, tenant.id, args.START, args.END)
|
||||
if not usage:
|
||||
continue
|
||||
|
||||
pricelist, _ = root_partner['property_product_pricelist']
|
||||
# Pre check, fetch all the products first.
|
||||
try:
|
||||
for m in usage:
|
||||
if not find_oerp_product(shell, m['region'], m['product']):
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(e.info)
|
||||
raise
|
||||
|
||||
try:
|
||||
build_sales_order(shell, args, pricelist, usage, partner,
|
||||
tenant.name, tenant.id)
|
||||
tenant.name, tenant.id, billing_date)
|
||||
except Exception as e:
|
||||
print "Failed to create sales order for tenant: %s" % tenant.name
|
||||
|
||||
with open('failed_tenants.txt', 'a') as f:
|
||||
f.write(tenant.id + "\n")
|
||||
raise e
|
||||
|
@ -373,6 +417,8 @@ def get_tenant_usage(shell, tenant, start, end):
|
|||
'n1.international-out': 0}
|
||||
|
||||
for res_id, res in raw_usage['usage']['resources'].items():
|
||||
name = res.get('name', res.get('ip address', '')) or res_id
|
||||
|
||||
for service_usage in res['services']:
|
||||
if service_usage['volume'] == 'unknown unit conversion':
|
||||
print('WARNING: Bogus unit: %s' % res.get('type'))
|
||||
|
@ -408,10 +454,6 @@ def get_tenant_usage(shell, tenant, start, end):
|
|||
(service_usage,))
|
||||
continue
|
||||
|
||||
name = res.get('name', res.get('ip address', ''))
|
||||
if name == '':
|
||||
name = res_id
|
||||
|
||||
if service_usage['name'] in ('n1.national-in',
|
||||
'n1.national-out',
|
||||
'n1.international-in',
|
||||
|
@ -489,27 +531,16 @@ def get_price(shell, pricelist, product, volume):
|
|||
|
||||
@retry(stop_max_attempt_number=3, wait_fixed=1000)
|
||||
def build_sales_order(shell, args, pricelist, usage, partner, tenant_name,
|
||||
tenant_id):
|
||||
end_timestamp = datetime.datetime.strptime(args.END, '%Y-%m-%dT%H:%M:%S')
|
||||
billing_date = str((end_timestamp - datetime.timedelta(days=1)).date())
|
||||
|
||||
try:
|
||||
# Pre check, fetch all the products first.
|
||||
for m in usage:
|
||||
if not find_oerp_product(shell, m['region'], m['product']):
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(e.info)
|
||||
raise
|
||||
|
||||
tenant_id, billing_date):
|
||||
log(shell.debug, 'Building sale.order')
|
||||
|
||||
try:
|
||||
order_dict = {
|
||||
'partner_id': partner['id'],
|
||||
'pricelist_id': pricelist,
|
||||
'partner_invoice_id': partner['id'],
|
||||
'partner_shipping_id': partner['id'],
|
||||
'order_date': billing_date,
|
||||
'date_order': billing_date,
|
||||
'note': 'Tenant: %s (%s)' % (tenant_name, tenant_id),
|
||||
'section_id': 10,
|
||||
}
|
||||
|
@ -519,7 +550,7 @@ def build_sales_order(shell, args, pricelist, usage, partner, tenant_name,
|
|||
if not args.DRY_RUN:
|
||||
order = shell.Order.create(order_dict)
|
||||
shell.order_id = order
|
||||
print('Order id: %s' % order)
|
||||
print('Order id: %s.' % order)
|
||||
|
||||
# Sort by product
|
||||
usage_dict_list = []
|
||||
|
@ -539,7 +570,10 @@ def build_sales_order(shell, args, pricelist, usage, partner, tenant_name,
|
|||
# Odoo will round the product_uom_qty and if it's under 0.0005
|
||||
# then it would be rounded to 0 and as a result the quoting
|
||||
# will fail.
|
||||
print('%s is too small.' % str(usage_dict['product_uom_qty']))
|
||||
print(
|
||||
'%s is too small for %s.' %
|
||||
(str(usage_dict['product_uom_qty']), m['name'])
|
||||
)
|
||||
continue
|
||||
|
||||
usage_dict_list.append(usage_dict)
|
||||
|
|
Loading…
Reference in New Issue