first commit

This commit is contained in:
root
2026-03-14 09:42:12 +00:00
commit 0adbd20c2c
10991 changed files with 1646955 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.

View File

@@ -0,0 +1,106 @@
========================
Agent Selection Scenario
========================
Imports::
>>> import datetime as dt
>>> from proteus import Model
>>> from trytond.modules.account.tests.tools import create_chart, get_accounts
>>> from trytond.modules.company.tests.tools import create_company, get_company
>>> from trytond.tests.tools import activate_modules, assertEqual, set_user
>>> today = dt.date.today()
>>> yesterday = today - dt.timedelta(days=1)
Activate modules::
>>> config = activate_modules(['commission', 'sale'], create_company, create_chart)
Get company::
>>> company = get_company()
Get accounts::
>>> accounts = get_accounts()
Create employee::
>>> Party = Model.get('party.party')
>>> Employee = Model.get('company.employee')
>>> employee = Employee()
>>> party = Party(name='Employee')
>>> party.save()
>>> employee.party = party
>>> employee.company = company
>>> employee.save()
Create customers::
>>> customer = Party(name='Customer')
>>> customer.save()
>>> other_customer = Party(name='Other Customer')
>>> other_customer.save()
Create agent::
>>> Agent = Model.get('commission.agent')
>>> agent_party = Party(name='Agent')
>>> agent_party.save()
>>> agent = Agent(party=agent_party)
>>> agent.type_ = 'agent'
>>> agent.currency = company.currency
>>> selection = agent.selections.new()
>>> selection.start_date = today
>>> selection.party = customer
>>> selection = agent.selections.new()
>>> selection.employee = employee
>>> agent.save()
Create sale::
>>> Sale = Model.get('sale.sale')
>>> sale = Sale()
>>> sale.party = customer
>>> sale.save()
The agent is assigned on quotation::
>>> sale.agent
>>> sale.click('quote')
>>> sale.state
'quotation'
>>> assertEqual(sale.agent, agent)
Agent is not set for yesterday sales::
>>> Sale = Model.get('sale.sale')
>>> sale = Sale()
>>> sale.sale_date = yesterday
>>> sale.party = customer
>>> sale.click('quote')
>>> sale.state
'quotation'
>>> sale.agent
Link user to employee::
>>> User = Model.get('res.user')
>>> user = User(config.user)
>>> user.employees.append(employee)
>>> user.employee = employee
>>> user.save()
>>> set_user(user.id)
Creating a sale and test agent is assigned::
>>> sale = Sale()
>>> sale.party = other_customer
>>> sale.agent
>>> sale.click('quote')
>>> sale.state
'quotation'
>>> assertEqual(sale.quoted_by, employee)
>>> assertEqual(sale.agent, agent)

View File

@@ -0,0 +1,245 @@
===================
Commission Scenario
===================
Imports::
>>> import datetime as dt
>>> from decimal import Decimal
>>> from proteus import Model, Wizard
>>> from trytond.modules.account.tests.tools import (
... create_chart, create_fiscalyear, get_accounts)
>>> from trytond.modules.account_invoice.tests.tools import (
... create_payment_term, set_fiscalyear_invoice_sequences)
>>> from trytond.modules.company.tests.tools import create_company, get_company
>>> from trytond.tests.tools import activate_modules, assertEqual
>>> today = dt.date.today()
>>> tomorrow = today + dt.timedelta(days=1)
Activate modules::
>>> config = activate_modules('commission', create_company, create_chart)
>>> ReportingAgent = Model.get('commission.reporting.agent')
>>> ReportingAgentTimeseries = Model.get('commission.reporting.agent.time_series')
Get company::
>>> company = get_company()
Create fiscal year::
>>> fiscalyear = set_fiscalyear_invoice_sequences(
... create_fiscalyear(today=(today, tomorrow)))
>>> fiscalyear.click('create_period')
Get accounts::
>>> accounts = get_accounts()
Create customer::
>>> Party = Model.get('party.party')
>>> customer = Party(name='Customer')
>>> customer.save()
Create account category::
>>> ProductCategory = Model.get('product.category')
>>> account_category = ProductCategory(name="Account Category")
>>> account_category.accounting = True
>>> account_category.account_expense = accounts['expense']
>>> account_category.account_revenue = accounts['revenue']
>>> account_category.save()
Create commission product::
>>> Uom = Model.get('product.uom')
>>> Template = Model.get('product.template')
>>> unit, = Uom.find([('name', '=', 'Unit')])
>>> template = Template()
>>> template.name = 'Commission'
>>> template.default_uom = unit
>>> template.type = 'service'
>>> template.list_price = Decimal(0)
>>> template.account_category = account_category
>>> template.save()
>>> commission_product, = template.products
Create commission plan::
>>> Plan = Model.get('commission.plan')
>>> plan = Plan(name='Plan')
>>> plan.commission_product = commission_product
>>> plan.commission_method = 'payment'
>>> line = plan.lines.new()
>>> line.formula = 'amount * 0.1'
>>> plan.save()
Create payment term::
>>> payment_term = create_payment_term()
>>> payment_term.save()
Create payment method::
>>> Journal = Model.get('account.journal')
>>> PaymentMethod = Model.get('account.invoice.payment.method')
>>> Sequence = Model.get('ir.sequence')
>>> journal_cash, = Journal.find([('type', '=', 'cash')])
>>> payment_method = PaymentMethod()
>>> payment_method.name = 'Cash'
>>> payment_method.journal = journal_cash
>>> payment_method.credit_account = accounts['cash']
>>> payment_method.debit_account = accounts['cash']
>>> payment_method.save()
Create agent::
>>> Agent = Model.get('commission.agent')
>>> agent_party = Party(name='Agent')
>>> agent_party.supplier_payment_term = payment_term
>>> agent_party.save()
>>> agent = Agent(party=agent_party)
>>> agent.type_ = 'agent'
>>> agent.plan = plan
>>> agent.currency = company.currency
>>> agent.save()
Create principal::
>>> principal_party = Party(name='Principal')
>>> principal_party.customer_payment_term = payment_term
>>> principal_party.save()
>>> principal = Agent(party=principal_party)
>>> principal.type_ = 'principal'
>>> principal.plan = plan
>>> principal.currency = company.currency
>>> principal.save()
Create product sold::
>>> template = Template()
>>> template.name = 'Product'
>>> template.default_uom = unit
>>> template.type = 'service'
>>> template.list_price = Decimal(100)
>>> template.account_category = account_category
>>> template.principals.append(principal)
>>> template.save()
>>> product, = template.products
Create invoice::
>>> Invoice = Model.get('account.invoice')
>>> invoice = Invoice()
>>> invoice.party = customer
>>> invoice.payment_term = payment_term
>>> invoice.agent = agent
>>> invoice.invoice_date
>>> line = invoice.lines.new()
>>> line.product = product
>>> line.quantity = 1
>>> line.unit_price = Decimal(100)
>>> invoice.save()
Post invoice::
>>> invoice.click('post')
>>> line, = invoice.lines
>>> len(line.commissions)
2
>>> [c.base_amount for c in line.commissions]
[Decimal('100.0000'), Decimal('100.0000')]
>>> [c.amount for c in line.commissions]
[Decimal('10.0000'), Decimal('10.0000')]
>>> [c.invoice_state for c in line.commissions]
['', '']
>>> [c.date for c in line.commissions]
[None, None]
Pending amount for agent::
>>> agent.reload()
>>> agent.pending_amount
Decimal('10.0000')
Pending amount for principal::
>>> principal.reload()
>>> principal.pending_amount
Decimal('10.0000')
Pay invoice::
>>> pay = Wizard('account.invoice.pay', [invoice])
>>> pay.form.payment_method = payment_method
>>> pay.form.date = tomorrow
>>> pay.execute('choice')
>>> pay.state
'end'
>>> Commission = Model.get('commission')
>>> for commission in Commission.find([]):
... assertEqual(commission.date, tomorrow)
Create commission invoices::
>>> create_invoice = Wizard('commission.create_invoice')
>>> create_invoice.form.from_ = None
>>> create_invoice.form.to = None
>>> create_invoice.execute('create_')
>>> invoice, = Invoice.find([
... ('type', '=', 'in'),
... ])
>>> invoice.total_amount
Decimal('10.00')
>>> assertEqual(invoice.party, agent_party)
>>> invoice_line, = invoice.lines
>>> assertEqual(invoice_line.product, commission_product)
>>> invoice, = Invoice.find([
... ('type', '=', 'out'),
... ('party', '=', principal.party.id),
... ])
>>> invoice.total_amount
Decimal('10.00')
>>> commissions = Commission.find([])
>>> [c.invoice_state for c in commissions]
['invoiced', 'invoiced']
Credit invoice::
>>> invoice, = Invoice.find([
... ('type', '=', 'out'),
... ('agent', '=', agent.id),
... ])
>>> credit = Wizard('account.invoice.credit', [invoice])
>>> credit.execute('credit')
>>> credit_note, = credit.actions[0]
>>> assertEqual(credit_note.agent, agent)
Check commission reporting per agent::
>>> with config.set_context(type='out', period='day'):
... reporting_agent, = ReportingAgent.find([])
... reporting_agent_timeseries, = ReportingAgentTimeseries.find([])
>>> reporting_agent.base_amount
Decimal('100.00')
>>> reporting_agent.amount
Decimal('10.0000')
>>> reporting_agent.number
1
>>> assertEqual(reporting_agent_timeseries.date, tomorrow)
>>> reporting_agent_timeseries.base_amount
Decimal('100.00')
>>> reporting_agent_timeseries.amount
Decimal('10.0000')
>>> reporting_agent_timeseries.number
1

View File

@@ -0,0 +1,129 @@
=========================================
Commission Credit Posted Invoice Scenario
=========================================
Imports::
>>> from decimal import Decimal
>>> from proteus import Model, Wizard
>>> from trytond.modules.account.tests.tools import (
... create_chart, create_fiscalyear, get_accounts)
>>> from trytond.modules.account_invoice.tests.tools import (
... set_fiscalyear_invoice_sequences)
>>> from trytond.modules.company.tests.tools import create_company, get_company
>>> from trytond.tests.tools import activate_modules
Activate modules::
>>> config = activate_modules('commission', create_company, create_chart)
>>> Agent = Model.get('commission.agent')
>>> Commission = Model.get('commission')
>>> Invoice = Model.get('account.invoice')
>>> Party = Model.get('party.party')
>>> Plan = Model.get('commission.plan')
>>> ProductCategory = Model.get('product.category')
>>> Template = Model.get('product.template')
>>> Uom = Model.get('product.uom')
Get company::
>>> company = get_company()
Create fiscal year::
>>> fiscalyear = set_fiscalyear_invoice_sequences(
... create_fiscalyear())
>>> fiscalyear.click('create_period')
Get accounts::
>>> accounts = get_accounts()
Create customer::
>>> customer = Party(name='Customer')
>>> customer.save()
Create account category::
>>> account_category = ProductCategory(name="Account Category")
>>> account_category.accounting = True
>>> account_category.account_expense = accounts['expense']
>>> account_category.account_revenue = accounts['revenue']
>>> account_category.save()
Create commission product::
>>> unit, = Uom.find([('name', '=', 'Unit')])
>>> template = Template()
>>> template.name = 'Commission'
>>> template.default_uom = unit
>>> template.type = 'service'
>>> template.list_price = Decimal(0)
>>> template.account_category = account_category
>>> template.save()
>>> commission_product, = template.products
Create commission plan::
>>> plan = Plan(name='Plan')
>>> plan.commission_product = commission_product
>>> plan.commission_method = 'payment'
>>> line = plan.lines.new()
>>> line.formula = 'amount * 0.1'
>>> plan.save()
Create agent::
>>> agent_party = Party(name='Agent')
>>> agent_party.save()
>>> agent = Agent(party=agent_party)
>>> agent.type_ = 'agent'
>>> agent.plan = plan
>>> agent.currency = company.currency
>>> agent.save()
Create product sold::
>>> template = Template()
>>> template.name = 'Product'
>>> template.default_uom = unit
>>> template.type = 'service'
>>> template.list_price = Decimal(100)
>>> template.account_category = account_category
>>> template.save()
>>> product, = template.products
Create invoice and credit it before paying::
>>> invoice = Invoice()
>>> invoice.party = customer
>>> invoice.agent = agent
>>> line = invoice.lines.new()
>>> line.product = product
>>> line.quantity = 1
>>> line.unit_price = Decimal(100)
>>> invoice.click('post')
>>> line, = invoice.lines
>>> commission, = line.commissions
>>> bool(commission.date)
False
>>> commission.amount
Decimal('10.0000')
>>> credit = Wizard('account.invoice.credit', [invoice])
>>> credit.execute('credit')
>>> credit_note, = credit.actions[0]
>>> credit_note.state = 'paid'
>>> credit_line, = credit_note.lines
>>> credit_commission, = Commission.find([
... ('id', '!=', commission.id),
... ])
>>> bool(credit_commission.date)
True
>>> credit_commission.amount
Decimal('-10.0000')
>>> commission.reload()
>>> bool(commission.date)
True

View File

@@ -0,0 +1,210 @@
=========================
Commission Stock Scenario
=========================
Imports::
>>> import datetime as dt
>>> from decimal import Decimal
>>> from proteus import Model
>>> from trytond.modules.account.tests.tools import (
... create_chart, create_fiscalyear, get_accounts)
>>> from trytond.modules.account_invoice.tests.tools import (
... set_fiscalyear_invoice_sequences)
>>> from trytond.modules.company.tests.tools import create_company, get_company
>>> from trytond.tests.tools import activate_modules
>>> today = dt.date.today()
>>> yesterday = today - dt.timedelta(days=1)
Activate modules::
>>> config = activate_modules(
... ['commission', 'sale', 'stock', 'account_invoice_stock'],
... create_company, create_chart)
>>> Agent = Model.get('commission.agent')
>>> Commission = Model.get('commission')
>>> MarginProduct = Model.get('stock.reporting.margin.product')
>>> Party = Model.get('party.party')
>>> Plan = Model.get('commission.plan')
>>> ProductCategory = Model.get('product.category')
>>> Sale = Model.get('sale.sale')
>>> Template = Model.get('product.template')
>>> Uom = Model.get('product.uom')
Get company::
>>> company = get_company()
Create fiscal year::
>>> fiscalyear = set_fiscalyear_invoice_sequences(
... create_fiscalyear(today=(yesterday, today)))
>>> fiscalyear.click('create_period')
Get accounts::
>>> accounts = get_accounts()
Create customer::
>>> customer = Party(name='Customer')
>>> customer.save()
Create account category::
>>> account_category = ProductCategory(name="Account Category")
>>> account_category.accounting = True
>>> account_category.account_expense = accounts['expense']
>>> account_category.account_revenue = accounts['revenue']
>>> account_category.save()
Create commission product::
>>> unit, = Uom.find([('name', '=', 'Unit')])
>>> template = Template()
>>> template.name = 'Commission'
>>> template.default_uom = unit
>>> template.type = 'service'
>>> template.list_price = Decimal(0)
>>> template.account_category = account_category
>>> template.save()
>>> commission_product, = template.products
Create commission plans::
>>> plan1 = Plan(name='Plan 1')
>>> plan1.commission_product = commission_product
>>> plan1.commission_method = 'posting'
>>> line = plan1.lines.new()
>>> line.formula = 'amount * 0.1'
>>> plan1.save()
>>> plan2 = Plan(name='Plan 2')
>>> plan2.commission_product = commission_product
>>> plan2.commission_method = 'posting'
>>> line = plan2.lines.new()
>>> line.formula = 'amount * 0.05'
>>> plan2.save()
Create agent::
>>> agent_party = Party(name='Agent')
>>> agent_party.save()
>>> agent = Agent(party=agent_party)
>>> agent.type_ = 'agent'
>>> agent.plan = plan1
>>> agent.currency = company.currency
>>> agent.save()
Create principal::
>>> principal_party = Party(name='Principal')
>>> principal_party.save()
>>> principal = Agent(party=principal_party)
>>> principal.type_ = 'principal'
>>> principal.plan = plan2
>>> principal.currency = company.currency
>>> principal.save()
Create product sold::
>>> template = Template()
>>> template.name = 'Product'
>>> template.default_uom = unit
>>> template.type = 'goods'
>>> template.salable = True
>>> template.list_price = Decimal('100.0000')
>>> template.account_category = account_category
>>> template.principals.append(principal)
>>> template.save()
>>> product, = template.products
>>> product.cost_price = Decimal('50.0000')
>>> product.save()
Create a sale::
>>> sale = Sale()
>>> sale.party = customer
>>> sale.agent = agent
>>> line = sale.lines.new()
>>> line.product = product
>>> line.quantity = 5
>>> sale.click('quote')
>>> sale.click('confirm')
>>> sale.state
'processing'
Ship in two steps::
>>> shipment, = sale.shipments
>>> move, = shipment.inventory_moves
>>> move.quantity = 3
>>> shipment.click('assign_force')
>>> shipment.click('pick')
>>> shipment.click('pack')
>>> shipment.click('do')
>>> shipment.state
'done'
>>> sale.reload()
>>> _, shipment = sale.shipments
>>> shipment.click('assign_force')
>>> shipment.click('pick')
>>> shipment.click('pack')
>>> shipment.click('do')
>>> shipment.state
'done'
Post the invoice::
>>> invoice, = sale.invoices
>>> invoice.click('post')
>>> invoice.state
'posted'
Check stock move::
>>> shipment, _ = sale.shipments
>>> move, = shipment.outgoing_moves
>>> move.commission_price
Decimal('-5.0000')
Check reporting margin::
>>> context = {
... 'from_date': yesterday,
... 'to_date': today,
... 'period': 'day',
... }
>>> with config.set_context(context=context):
... report, = MarginProduct.find([])
>>> report.cost
Decimal('250.00')
>>> report.revenue
Decimal('500.00')
>>> context['include_commission'] = True
>>> with config.set_context(context=context):
... report, = MarginProduct.find([])
>>> report.cost
Decimal('275.00')
>>> report.revenue
Decimal('500.00')
Update commission amount::
>>> commission, = Commission.find([('agent.type_', '=', 'agent')])
>>> commission.amount = Decimal('60.0000')
Create commission invoice::
>>> commission.click('invoice')
Check stock move::
>>> move.reload()
>>> move.commission_price
Decimal('-7.0000')

View File

@@ -0,0 +1,121 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from decimal import Decimal
from trytond.modules.company.tests import (
CompanyTestMixin, PartyCompanyCheckEraseMixin, create_company, set_company)
from trytond.modules.party.tests import PartyCheckReplaceMixin
from trytond.pool import Pool
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
def create_product(name, list_price, categories=None):
pool = Pool()
Template = pool.get('product.template')
Product = pool.get('product.product')
Uom = pool.get('product.uom')
unit, = Uom.search([('name', '=', 'Unit')])
template = Template(
name=name,
type='service',
list_price=list_price,
default_uom=unit,
products=None,
)
if categories:
template.categories = categories
template.save()
product = Product(template=template)
product.save()
return product
def create_plan(lines):
pool = Pool()
Plan = pool.get('commission.plan')
commission_product = create_product("Commission", Decimal(10), [])
plan, = Plan.create([{
'name': "Commission Plan",
'commission_product': commission_product.id,
'lines': [('create', lines)]
}])
return plan
class CommissionTestCase(
PartyCompanyCheckEraseMixin, PartyCheckReplaceMixin, CompanyTestMixin,
ModuleTestCase):
'Test Commission module'
module = 'commission'
extras = ['sale', 'stock', 'account_invoice_stock']
@with_transaction()
def test_plan_category(self):
"Test plan with category"
pool = Pool()
Category = pool.get('product.category')
category = Category(name="Category")
category.save()
child_category = Category(name="Child Category", parent=category)
child_category.save()
company = create_company()
with set_company(company):
product = create_product("Other", Decimal(10), [category])
plan = create_plan([{
'category': category.id,
'formula': 'amount * 0.8',
}, {
'formula': 'amount',
}])
self.assertEqual(plan.compute(Decimal(1), product), Decimal('0.8'))
template = product.template
template.categories = []
template.save()
self.assertEqual(plan.compute(Decimal(1), product), Decimal(1))
template.categories = [child_category]
template.save()
self.assertEqual(plan.compute(Decimal(1), product), Decimal('0.8'))
@with_transaction()
def test_plan_no_product(self):
"Test plan with no product"
pool = Pool()
Category = pool.get('product.category')
PlanLine = pool.get('commission.plan.line')
category = Category(name="Category")
category.save()
company = create_company()
with set_company(company):
product = create_product("Other", Decimal(10))
plan = create_plan([{
'category': category.id,
'formula': 'amount * 0.8',
}, {
'product': product.id,
'formula': 'amount * 0.7',
}, {
'formula': 'amount',
}])
self.assertEqual(plan.compute(Decimal(1), None), Decimal(1))
PlanLine.delete(plan.lines[1:])
self.assertEqual(plan.compute(Decimal(1), None), None)
del ModuleTestCase

View File

@@ -0,0 +1,8 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from trytond.tests.test_tryton import load_doc_tests
def load_tests(*args, **kwargs):
return load_doc_tests(__name__, __file__, *args, **kwargs)