"""
Creates a Subscription for a customer.
"""
from datetime import datetime
import os
import configparser
# call the function "get_settings_odoo" to get the parameter
# for the logging of the model on the "odoo Webservice API"
try:
from .get_settings_odoo import get_settings_odoo
except ImportError:
from get_settings_odoo import get_settings_odoo
settings = get_settings_odoo()
db = settings[0]
uid = settings[1]
password = settings[3]
models = settings[4]
# ------- get all the parameters from the file "parameter_global.ini"
# Name and full path of the "ini" file
filename = "parameter_global.ini"
path_name = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Go up to repo root
config_path = os.path.join(path_name, 'config')
fullpath = os.path.join(config_path, filename)
config = configparser.ConfigParser()
config.read(fullpath)
# get the default company id
custom_company_id = config.get('general_settings', 'company_id')
# get the default currency of the company
custom_currency_id = config.get('general_settings', 'currency_id')
# the datasets will be stored with the user_id of the Batchuser
custom_create_uid = config.get('general_settings', 'Batchuser')
# set the user_id which store this record
custom_write_uid = custom_create_uid
# ------- get all the inventory parameter from the Parameter File "parameter_inventory.ini" -------
# ------- the section "create_order" of the Parameter File "parameter_inventory.ini" will be used -------
# Name and full path of the "ini" file
filename = "parameter_inventory.ini"
path_name = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Go up to repo root
config_path = os.path.join(path_name, 'config')
fullpath = os.path.join(config_path, filename)
config = configparser.ConfigParser()
config.read(fullpath)
# get the default state of the invoive
custom_account_invoice_state = config.get('create_subscription', 'account_invoice_state')
# get the default Unit of Measure
custom_uom = config.get('create_subscription', 'uom')
# get the default credit account in the general ledger
custom_credit_account = config.get('create_subscription', 'credit_account')
# get the default debit account in the general ledger
custom_debit_account = config.get('create_subscription', 'debit_account')
# get the default Product Quantity
custom_product_qty = config.get('create_subscription', 'product_qty')
# get the default payment term for the Invoice
custom_payment_term_id = config.get('create_subscription', 'payment_term_id')
# ------- get all the parameter from the Parameter File "parameter_sales.ini" -------
# ------- the section "subscription" of the file "parameter_sales.ini" will be used -------
# Name and full path of the "ini" file
filename = "parameter_sales.ini"
path_name = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Go up to repo root
config_path = os.path.join(path_name, 'config')
fullpath = os.path.join(config_path, filename)
config = configparser.ConfigParser()
config.read(fullpath)
# get the default CRM team
custom_crm_team = config.get('create_subscription', 'crm_team')
# get the cron_id number for the scheduled action
custom_cron_id = config.get('create_subscription', 'cron_id')
# get the maximum number of different transfer types
custom_max_number = int(config.get('create_subscription', 'max_number_abo_model'))
# initial the different list variables
custom_short_name = []
custom_full_name = []
custom_pricelist = []
custom_product = []
# these parameters are used to fillup the parameter for the pro-forma invoice
i = 0
while i <= custom_max_number:
custom_string = str(i) + '_short_name'
custom_short_name.append(config.get('create_subscription', custom_string))
custom_string = str(i) + '_full_name'
custom_full_name.append(config.get('create_subscription', custom_string))
custom_string = str(i) + '_pricelist'
custom_pricelist.append(config.get('create_subscription', custom_string))
custom_string = str(i) + '_product'
custom_product.append(config.get('create_subscription', custom_string))
i = i + 1
# ------- end of the initialisation -------
[docs]
def create_subscription(partner_id, abomodel):
"""
Function creates a subscription based on the selected abo model.
First a pro-forma invioce will be created.
Then a subscription will be created based on the pro-forma-invoice.
This subscription will be executed on a regular monthy basis
to create the invoices for the abo (not in this module).
"""
# Initial value as an indicator for the correctness of the value "abomodel"
custom_short = ''
i = 0
while i <= int(custom_max_number):
# if the correct abo model name has been found
# fill up the values with the corresponding parameter from the abo model array
if custom_short_name [i] == abomodel:
custom_short = custom_short_name [i]
custom_name = custom_full_name [i]
custom_pricelist_id = custom_pricelist [i]
custom_product_id = custom_product [i]
i = i + 1
# test if a valid "custom_transfer" value has been given with by the value "custom_name"
if (partner_id != '') and (custom_short != ''):
# get the actual date for some values to store in the odoo database table
now = datetime.now() # current date and time
custom_actual_date = now.strftime("%Y-%m-%d %H:%M:%S")
# find the corresponding product template of the product
custom_product_template_id = '' # default value
if custom_product_id != '':
custom_product_template = models.execute_kw(db,uid, password,
'product.product', 'search_read',
[[['id', '=', custom_product_id]]],
{'fields': ['product_tmpl_id']})
# if the array is not empty
# get the value for 'product_tmpl_id'
if custom_product_template:
for key, value in list(custom_product_template[0].items()):
if key == "product_tmpl_id":
custom_product_template_id = value[0]
# find the corresponding pricelist of the template of the product
# to get the price of the corresponding abo model
# first try: find a pricelist item with an empty date_end
if (custom_product_template_id != '' ) and (custom_pricelist_id != ''):
custom_pricelist_item = models.execute_kw(db,uid, password,
'product.pricelist.item', 'search_read',
[[['pricelist_id', '=', int(custom_pricelist_id)],
['date_start', '<=', custom_actual_date],
['date_end', '=', False],
['product_tmpl_id', '=', int(custom_product_template_id)]]],
{'fields': ['fixed_price','date_end']})
# second try: if no pricelist item found, find a pricelist with date_end later than actual date
if not custom_pricelist_item:
custom_pricelist_item = models.execute_kw(db,uid, password,
'product.pricelist.item', 'search_read',
[[['pricelist_id', '=', int(custom_pricelist_id)],
['date_start', '<=', custom_actual_date],
['date_end', '>=', custom_actual_date],
['product_tmpl_id', '=', int(custom_product_template_id)]]],
{'fields': ['fixed_price','date_end']})
# if the array is not empty
# get the value for 'product_tmpl_id'
if custom_pricelist_item:
for key, value in list(custom_pricelist_item[0].items()):
if key == "fixed_price":
custom_fixed_price = value
# create a new invoice record in the odoo table "account_invoice" with the state "draft"
custom_account_invoice_id = models.execute_kw(db,uid, password,'account.invoice', 'create',
[{'company_id': custom_company_id,
'account_id': custom_credit_account,
'confirmation_date': custom_actual_date,
'create_date': custom_actual_date,
'date_invoice': custom_actual_date,
'create_uid': custom_create_uid,
'currency_id': custom_currency_id,
'partner_id': partner_id,
'partner_shipping_id': partner_id,
'payment_term_id': custom_payment_term_id,
'sent': 0, # == FALSE
'reconciled': 1, # == TRUE
'team_id': custom_crm_team,
'state': 'draft',
'write_uid': custom_write_uid}])
# create a new invoice line record in the odoo table "account_invoice_line"
# relating to the newly created invoice record above (see 'sale_line_ids')
custom_account_invoice_line_id = models.execute_kw(db,uid, password,'account.invoice.line', 'create',
[{'company_id': custom_company_id,
'account_id': custom_debit_account,
'create_date': custom_actual_date,
'create_uid': custom_create_uid,
'currency_id': custom_currency_id,
'name' : custom_name,
'invoice_id': custom_account_invoice_id,
'partner_id': partner_id,
'product_id': custom_product_id,
'price_unit': custom_fixed_price,
'price_subtotal': custom_fixed_price,
'price_subtotal_signed': custom_fixed_price,
'quantity': custom_product_qty,
'uom_id': custom_uom,
'write_uid': custom_write_uid}])
# update the newly created invoice record from the state "draft" to the state "pro-forma"
# by using the method "action_invoice_open"
models.execute_kw(db, uid, password, 'account.invoice', 'action_invoice_proforma2', [custom_account_invoice_id],{})
# update the "account.invoice" record with a meaningful name
models.execute_kw(db,uid, password, 'account.invoice', 'write',
[[custom_account_invoice_id],
{
'number': 'AI' + str(custom_account_invoice_id) + ' ' + str(partner_id) + ' ' + custom_name,
'name': 'AI' + str(custom_account_invoice_id) + ' ' + str(partner_id) + ' ' + custom_name,
}])
# update the "account.invoice.line" record with a meaningful name
models.execute_kw(db,uid, password, 'account.invoice.line', 'write',
[[custom_account_invoice_line_id],
{
'name': 'AIL' + str(custom_account_invoice_line_id) + ' ' + custom_name,
}])
# create the subscription based on the invoice template
custom_subscription_id = models.execute_kw(db,uid, password,'subscription.subscription', 'create',
[{
'active': 1, # == TRUE
'state': 'running',
'create_date': custom_actual_date,
'write_date': custom_actual_date,
'create_uid': custom_create_uid,
'user_id': custom_create_uid,
'write_uid': custom_create_uid,
'partner_id': partner_id,
'cron_id': custom_cron_id,
'name': '',
'date_init': custom_actual_date,
'doc_source': 'account.invoice,' + str(custom_account_invoice_id),
'interval_number': 1, #== Internal Quantity
'interval_type': 'months',
'exec_init': 1 #== Number of Documents
}])
# update the "subscription" record with a meaningful name
models.execute_kw(db,uid, password, 'subscription.subscription', 'write',
[[custom_subscription_id],
{
'name': 'SUB' + str(custom_subscription_id) + ' ' + str(partner_id) + ' ' + custom_name,
'notes': 'Subscription to create the monthly abo model invoices for the dedicated customer based on the pro-forma invoice.'
}])
result = (custom_subscription_id, custom_account_invoice_id)
else:
result = ('', '')
return(result)