several fixes and adapt code from several modules

This commit is contained in:
?ngel ?lvarez 2015-03-07 17:49:54 +01:00
parent 137776f77c
commit 96d1083f17
8 changed files with 354 additions and 26 deletions

View File

@ -7,5 +7,6 @@ from .work import *
def register():
Pool.register(
Project,
ShipmentWork,
ContractLine,
Contract,
module='asset_work_project', type_='model')

View File

@ -61,8 +61,8 @@ setup(name='%s_%s' % (PREFIX, MODULE),
'trytond.modules.%s.tests' % MODULE,
],
package_data={
'trytond.modules.%s' % MODULE: (info.get('xml', [])
+ ['tryton.cfg', 'locale/*.po', 'tests/*.rst']),
'trytond.modules.%s' % MODULE: (info.get('xml', []) +
['tryton.cfg', 'locale/*.po', 'tests/*.rst']),
},
classifiers=[
'Development Status :: 5 - Production/Stable',

View File

@ -0,0 +1,277 @@
=============
Sale Scenario
=============
Imports::
>>> import datetime
>>> from dateutil.relativedelta import relativedelta
>>> from decimal import Decimal
>>> from operator import attrgetter
>>> from proteus import config, Model, Wizard
>>> today = datetime.datetime.combine(datetime.date.today(),
... datetime.datetime.min.time())
>>> tomorrow = datetime.date.today() + relativedelta(days=1)
Create database::
>>> config = config.set_trytond()
>>> config.pool.test = True
Install sale::
>>> Module = Model.get('ir.module.module')
>>> module, = Module.find([('name', '=', 'asset_work_project')])
>>> Module.install([module.id], config.context)
>>> Wizard('ir.module.module.install_upgrade').execute('upgrade')
Create company::
>>> Currency = Model.get('currency.currency')
>>> CurrencyRate = Model.get('currency.currency.rate')
>>> currencies = Currency.find([('code', '=', 'USD')])
>>> if not currencies:
... currency = Currency(name='U.S. Dollar', symbol='$', code='USD',
... rounding=Decimal('0.01'), mon_grouping='[3, 3, 0]',
... mon_decimal_point='.', mon_thousands_sep=',')
... currency.save()
... CurrencyRate(date=today + relativedelta(month=1, day=1),
... rate=Decimal('1.0'), currency=currency).save()
... else:
... currency, = currencies
>>> Company = Model.get('company.company')
>>> Party = Model.get('party.party')
>>> company_config = Wizard('company.company.config')
>>> company_config.execute('company')
>>> company = company_config.form
>>> party = Party(name='Dunder Mifflin')
>>> party.save()
>>> company.party = party
>>> company.currency = currency
>>> company_config.execute('add')
>>> company, = Company.find([])
Reload the context::
>>> User = Model.get('res.user')
>>> config._context = User.get_preferences(True, config.context)
Create fiscal year::
>>> FiscalYear = Model.get('account.fiscalyear')
>>> Sequence = Model.get('ir.sequence')
>>> SequenceStrict = Model.get('ir.sequence.strict')
>>> fiscalyear = FiscalYear(name=str(today.year))
>>> fiscalyear.start_date = today + relativedelta(month=1, day=1)
>>> fiscalyear.end_date = today + relativedelta(month=12, day=31)
>>> fiscalyear.company = company
>>> post_move_seq = Sequence(name=str(today.year), code='account.move',
... company=company)
>>> post_move_seq.save()
>>> fiscalyear.post_move_sequence = post_move_seq
>>> invoice_seq = SequenceStrict(name=str(today.year),
... code='account.invoice', company=company)
>>> invoice_seq.save()
>>> fiscalyear.out_invoice_sequence = invoice_seq
>>> fiscalyear.in_invoice_sequence = invoice_seq
>>> fiscalyear.out_credit_note_sequence = invoice_seq
>>> fiscalyear.in_credit_note_sequence = invoice_seq
>>> fiscalyear.save()
>>> FiscalYear.create_period([fiscalyear.id], config.context)
Create chart of accounts::
>>> AccountTemplate = Model.get('account.account.template')
>>> Account = Model.get('account.account')
>>> Journal = Model.get('account.journal')
>>> account_template, = AccountTemplate.find([('parent', '=', None)])
>>> create_chart = Wizard('account.create_chart')
>>> create_chart.execute('account')
>>> create_chart.form.account_template = account_template
>>> create_chart.form.company = company
>>> create_chart.execute('create_account')
>>> receivable, = Account.find([
... ('kind', '=', 'receivable'),
... ('company', '=', company.id),
... ])
>>> payable, = Account.find([
... ('kind', '=', 'payable'),
... ('company', '=', company.id),
... ])
>>> revenue, = Account.find([
... ('kind', '=', 'revenue'),
... ('company', '=', company.id),
... ])
>>> expense, = Account.find([
... ('kind', '=', 'expense'),
... ('company', '=', company.id),
... ])
>>> create_chart.form.account_receivable = receivable
>>> create_chart.form.account_payable = payable
>>> create_chart.execute('create_properties')
>>> cash, = Account.find([
... ('kind', '=', 'other'),
... ('name', '=', 'Main Cash'),
... ('company', '=', company.id),
... ])
>>> cash_journal, = Journal.find([('type', '=', 'cash')])
>>> cash_journal.credit_account = cash
>>> cash_journal.debit_account = cash
>>> cash_journal.save()
Create parties::
>>> Party = Model.get('party.party')
>>> supplier = Party(name='Supplier')
>>> supplier.save()
>>> customer = Party(name='Customer')
>>> customer.save()
Create category::
>>> ProductCategory = Model.get('product.category')
>>> category = ProductCategory(name='Category')
>>> category.save()
Create product::
>>> ProductUom = Model.get('product.uom')
>>> unit, = ProductUom.find([('name', '=', 'Unit')])
>>> ProductTemplate = Model.get('product.template')
>>> Product = Model.get('product.product')
>>> product = Product()
>>> template = ProductTemplate()
>>> template.name = 'product'
>>> template.category = category
>>> template.default_uom = unit
>>> template.type = 'assets'
>>> template.purchasable = True
>>> template.salable = True
>>> template.list_price = Decimal('10')
>>> template.cost_price = Decimal('8')
>>> template.cost_price_method = 'fixed'
>>> template.account_expense = expense
>>> template.account_revenue = revenue
>>> template.save()
>>> product.template = template
>>> product.save()
>>> service_product = Product()
>>> template = ProductTemplate()
>>> template.name = 'service'
>>> template.default_uom = unit
>>> template.type = 'service'
>>> template.salable = True
>>> template.list_price = Decimal('30')
>>> template.cost_price = Decimal('10')
>>> template.cost_price_method = 'fixed'
>>> template.account_expense = expense
>>> template.account_revenue = revenue
>>> template.save()
>>> service_product.template = template
>>> service_product.save()
Create payment term::
>>> PaymentTerm = Model.get('account.invoice.payment_term')
>>> PaymentTermLine = Model.get('account.invoice.payment_term.line')
>>> payment_term = PaymentTerm(name='Direct')
>>> payment_term_line = PaymentTermLine(type='remainder', days=0)
>>> payment_term.lines.append(payment_term_line)
>>> payment_term.save()
Create an asset::
>>> Asset = Model.get('asset')
>>> asset = Asset()
>>> asset.name = 'Asset'
>>> asset.product = product
>>> asset.owner = customer
>>> asset.save()
Configure shipment work::
>>> StockConfig = Model.get('stock.configuration')
>>> stock_config = StockConfig(1)
>>> shipment_work_sequence, = Sequence.find([
... ('code', '=', 'shipment.work'),
... ])
>>> stock_config.shipment_work_sequence = shipment_work_sequence
>>> stock_config.save()
Create daily service::
>>> Service = Model.get('contract.service')
>>> service = Service()
>>> service.product = service_product
>>> service.freq = 'daily'
>>> service.interval = 1
>>> service.save()
Configure Milestones::
>>> Sequence = Model.get('ir.sequence')
>>> AccountConfiguration = Model.get('account.configuration')
>>> milestone_sequence, = Sequence.find([
... ('code', '=', 'account.invoice.milestone'),
... ], limit=1)
>>> milestone_group_sequence, = Sequence.find([
... ('code', '=', 'account.invoice.milestone.group'),
... ], limit=1)
>>> account_config = AccountConfiguration(1)
>>> account_config.milestone_sequence = milestone_sequence
>>> account_config.milestone_group_sequence = milestone_group_sequence
>>> account_config.save()
Models::
>>> Sequence = Model.get('ir.sequence')
>>> WorkProjectConfig = Model.get('work.project.configuration')
>>> work_project_sequence, = Sequence.find([('code','=','work.project')])
>>> work_project_config = WorkProjectConfig(1)
>>> work_project_config.project_sequence = work_project_sequence
>>> work_project_config.save()
Create a contract::
>>> Contract = Model.get('contract')
>>> contract = Contract()
>>> contract.party = customer
>>> contract.start_date = today
>>> contract.start_period_date = today
>>> contract.freq = 'monthly'
>>> line = contract.lines.new()
>>> line.service = service
>>> line.create_shipment_work = True
>>> line.first_shipment_date = today
>>> line.asset = asset
>>> contract.click('validate_contract')
>>> contract.state
u'validated'
>>> project, = contract.projects
>>> project.asset == asset
True
>>> project.party == customer
True
>>> len(project.work_shipments)
0
>>> project.save()
Create a shipments::
>>> create_shipments = Wizard('contract.create_shipments')
>>> create_shipments.form.date = today + relativedelta(days=+1)
>>> create_shipments.execute('create_shipments')
>>> Shipment = Model.get('shipment.work')
>>> shipments = Shipment.find([])
>>> shipment = shipments[0]
>>> shipment.planned_date == today.date()
True
The asset has a maintenance planned for the same date::
>>> asset.reload()
>>> asset.shipments[0].planned_date == today.date()
True

View File

@ -1,11 +1,10 @@
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
import unittest
# import doctest
import doctest
import trytond.tests.test_tryton
from trytond.tests.test_tryton import test_view, test_depends
# TODO: Remove if no sceneario needed.
# from trytond.tests.test_tryton import doctest_setup, doctest_tearwodn
from trytond.tests.test_tryton import doctest_setup, doctest_teardown
class TestCase(unittest.TestCase):
@ -26,8 +25,7 @@ class TestCase(unittest.TestCase):
def suite():
suite = trytond.tests.test_tryton.suite()
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestCase))
# TODO: remove if no scenario needed.
#suite.addTests(doctest.DocFileSuite('scenario_asset_work_project.rst',
# setUp=doctest_setup, tearDown=doctest_tearown, encoding='utf-8',
# optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
suite.addTests(doctest.DocFileSuite('scenario_asset_work_project.rst',
setUp=doctest_setup, tearDown=doctest_teardown, encoding='utf-8',
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
return suite

View File

@ -1,7 +1,7 @@
[tryton]
version=3.4.0
depends:
asset_work_shipment
asset_contract
work_project
extras_depend:
asset_zone

View File

@ -5,6 +5,8 @@
<xpath expr="/form/label[@name='start_date']" position="before">
<label name="asset"/>
<field name="asset"/>
<label name="contract_line"/>
<field name="contract_line"/>
<newline/>
</xpath>
</data>

View File

@ -4,5 +4,6 @@
<data>
<xpath expr="/tree/field[@name='party']" position="after">
<field name="asset"/>
<field name="contract_line"/>
</xpath>
</data>

79
work.py
View File

@ -3,15 +3,17 @@
from trytond.model import fields
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval
from trytond.transaction import Transaction
__all___ = ['Project', 'ShipmentWork']
__all___ = ['Project', 'ContractLine', 'Contract']
__metaclass__ = PoolMeta
class Project:
__name__ = 'work.project'
asset = fields.Many2One('asset', 'Asset', select=True)
contract_line = fields.Many2One('contract.line', 'Contract Line')
@classmethod
def __setup__(cls):
@ -21,19 +23,66 @@ class Project:
})
class ShipmentWork:
__name__ = 'shipment.work'
class Contract:
__name__ = 'contract'
projects = fields.Function(fields.One2Many('work.project', None,
'Projects'),
'get_projects', searcher='search_projects')
def get_projects(self, name):
return list(set(p.id for l in self.lines for p in l.projects))
@classmethod
def default_employee(cls):
def search_projects(cls, name, clause):
return [('lines.projects',) + tuple(clause[1:])]
@classmethod
def validate_contract(cls, contracts):
super(Contract, cls).validate_contract(contracts)
ContractLine = Pool().get('contract.line')
lines = []
for contract in contracts:
lines += contract.lines
ContractLine.create_projects(lines)
class ContractLine:
__name__ = 'contract.line'
projects = fields.One2Many('work.project', 'contract_line', 'Projects',
domain=[
('asset', '=', Eval('asset')),
('maintenance', '=', True),
],
depends=['asset'])
def get_projects(self):
pool = Pool()
Asset = pool.get('asset')
asset_id = Transaction().context.get('asset')
if asset_id:
asset = Asset(asset_id)
if hasattr(asset, 'zone') and asset.zone and asset.zone.employee:
return asset.zone.employee.id
try:
return super(ShipmentWork, cls).default_employee()
except AttributeError:
return None
Project = pool.get('work.project')
if self.projects or not self.asset:
return
if not self.asset.owner:
self.raise_user_error('no_asset_owner', self.asset.rec_name)
project = Project()
project.company = self.contract.company
project.party = self.asset.owner
project.asset = self.asset
project.maintenance = True
project.start_date = self.contract.start_date
project.end_date = self.contract.end_date if self.contract.end_date \
else None
project.contract_line = self
return [project]
@classmethod
def create_projects(cls, lines):
pool = Pool()
Project = pool.get('work.project')
new_projects = []
for line in lines:
projects = line.get_projects()
if projects:
new_projects.extend(projects)
if new_projects:
Project.create([p._save_values for p in new_projects])