Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
kittiu committed Jun 23, 2017
1 parent bb1ae53 commit c4ae56d
Show file tree
Hide file tree
Showing 28 changed files with 321 additions and 185 deletions.
3 changes: 2 additions & 1 deletion pabi_asset_management/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@
"views/stock_view.xml",
"views/purchase_view.xml",
"views/purchase_master_data_view.xml",
"wizard/asset_action_excel_import.xml",
],
'installable': True,
'active': True,
'auto_install': True,
'auto_install': False,
}

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
4 changes: 2 additions & 2 deletions pabi_asset_management/models/account_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class AccountInvoiceLine(models.Model):
_inherit = 'account.invoice.line'

@api.multi
@api.constrains('asset_category_id', 'asset_id')
@api.constrains('asset_profile_id', 'asset_id')
def _check_asset_id(self):
for rec in self:
if rec.asset_category_id or rec.asset_id:
if rec.asset_profile_id or rec.asset_id:
raise ValidationError(
_('For PABI2, creating asset on invoice is not allowed.'))
4 changes: 2 additions & 2 deletions pabi_asset_management/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AccountMoveLine(models.Model):
help="When account is created by a stock move (anglo saxon).",
)
parent_asset_id = fields.Many2one(
'account.asset.asset',
'account.asset',
string='Parent Asset',
readonly=True,
help="If in purchase order line, parent asset is specified.",
Expand Down Expand Up @@ -68,7 +68,7 @@ def _prepare_asset_vals(self, move_line):
def create(self, vals):
move_line = super(AccountMoveLine, self).create(vals)
if move_line.asset_id and (move_line.asset_id.code or '/') == '/':
if move_line.asset_category_id and move_line.asset_id:
if move_line.asset_profile_id and move_line.asset_id:
vals = self._prepare_asset_vals(move_line)
move_line.asset_id.write(vals)
return move_line
Expand Down
85 changes: 45 additions & 40 deletions pabi_asset_management/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class AccountAssetStatus(models.Model):
)


class AccountAssetAsset(ChartFieldAction, models.Model):
_inherit = 'account.asset.asset'
class AccountAsset(ChartFieldAction, models.Model):
_inherit = 'account.asset'

name = fields.Char(
default='/',
Expand All @@ -59,7 +59,7 @@ class AccountAssetAsset(ChartFieldAction, models.Model):
default=lambda self: self.env.ref('pabi_asset_management.'
'asset_status_cancel'),
domain="[('map_state', '=', state)]",
required=True,
required=False,
index=True,
help="Status vs State\n"
"Draft → ยกเลิก\n"
Expand Down Expand Up @@ -92,7 +92,7 @@ class AccountAssetAsset(ChartFieldAction, models.Model):
product_id = fields.Many2one(
'product.product',
string='Asset Type',
domain=[('asset_category_id', '!=', False)],
domain=[('asset_profile_id', '!=', False)],
readonly=True,
states={'draft': [('readonly', False)]},
help="This asset is created from this product class",
Expand Down Expand Up @@ -130,7 +130,7 @@ class AccountAssetAsset(ChartFieldAction, models.Model):
)
no_depreciation = fields.Boolean(
string='No Depreciation',
related='category_id.no_depreciation',
related='profile_id.no_depreciation',
readonly=True,
)
# Additional Info
Expand Down Expand Up @@ -217,7 +217,7 @@ class AccountAssetAsset(ChartFieldAction, models.Model):
)
# Transfer Asset
target_asset_ids = fields.Many2many(
'account.asset.asset',
'account.asset',
'account_asset_source_target_rel',
'source_asset_id', 'target_asset_id',
string='Transferred to Asset',
Expand All @@ -228,7 +228,7 @@ class AccountAssetAsset(ChartFieldAction, models.Model):
compute='_compute_source_asset_count',
)
source_asset_ids = fields.Many2many(
'account.asset.asset',
'account.asset',
'account_asset_source_target_rel',
'target_asset_id', 'source_asset_id',
string='Source Assets',
Expand Down Expand Up @@ -296,13 +296,12 @@ def write(self, vals):
for asset in self:
if status.map_state != asset.state:
raise ValidationError(_('Invalid change of asset status'))
return super(AccountAssetAsset, self).write(vals)
return super(AccountAsset, self).write(vals)

@api.multi
def open_source_asset(self):
self.ensure_one()
action = self.env.ref('account_asset_management.'
'action_account_asset_asset_form')
action = self.env.ref('account_asset_management.account_asset_action')
result = action.read()[0]
assets = self.with_context(active_test=False).\
search([('target_asset_ids', 'in', [self.id])])
Expand Down Expand Up @@ -336,12 +335,12 @@ def create(self, vals):
raise ValidationError(
_('No asset sequence setup for selected product!'))
vals['code'] = self.env['ir.sequence'].next_by_id(sequence.id)
asset = super(AccountAssetAsset, self).create(vals)
asset = super(AccountAsset, self).create(vals)
asset.update_related_dimension(vals)
# Init Salvage Value from Category
if self._context.get('create_asset_from_move_line', False):
if not asset.category_id.no_depreciation:
asset.salvage_value = asset.category_id.salvage_value
if not asset.profile_id.no_depreciation:
asset.salvage_value = asset.profile_id.salvage_value
return asset

@api.multi
Expand All @@ -358,14 +357,14 @@ def name_get(self):
@api.multi
def compute_depreciation_board(self):
assets = self.filtered(lambda l: not l.no_depreciation)
return super(AccountAssetAsset, assets).compute_depreciation_board()
return super(AccountAsset, assets).compute_depreciation_board()

@api.multi
def onchange_category_id(self, category_id):
res = super(AccountAssetAsset, self).onchange_category_id(category_id)
asset_category = self.env['account.asset.category'].browse(category_id)
if asset_category and not asset_category.no_depreciation:
res['value']['salvage_value'] = asset_category.salvage_value
def onchange_profile_id(self, profile_id):
res = super(AccountAsset, self).onchange_profile_id(profile_id)
asset_profile = self.env['account.asset.profile'].browse(profile_id)
if asset_profile and not asset_profile.no_depreciation:
res['value']['salvage_value'] = asset_profile.salvage_value
return res

# Method used in change owner and transfer
Expand All @@ -375,16 +374,16 @@ def _prepare_asset_reverse_moves(self, assets):
AccountMoveLine = self.env['account.move.line']
default = {'move_id': False,
'parent_asset_id': False,
'asset_category_id': False,
'asset_profile_id': False,
'product_id': False,
'partner_id': False,
'stock_move_id': False,
}
asset_move_lines_dict = []
depre_move_lines_dict = []
for asset in assets:
account_asset_id = asset.category_id.account_asset_id.id
account_depre_id = asset.category_id.account_depreciation_id.id
account_asset_id = asset.profile_id.account_asset_id.id
account_depre_id = asset.profile_id.account_depreciation_id.id
# Getting the origin move_line (1 asset value and 1 depreciation)
# Asset
asset_lines = AccountMoveLine.search([ # Should have 1 line
Expand Down Expand Up @@ -441,8 +440,8 @@ def _prepare_asset_target_move(self, move_lines_dict, new_owner={}):
return move_line_dict


class AccountAssetCategory(models.Model):
_inherit = 'account.asset.category'
class AccountAssetProfile(models.Model):
_inherit = 'account.asset.profile'

product_categ_id = fields.Many2one(
'product.category',
Expand All @@ -466,20 +465,26 @@ class AccountAssetCategory(models.Model):

@api.multi
def write(self, vals):
res = super(AccountAssetCategory, self).write(vals)
res = super(AccountAssetProfile, self).write(vals)
if 'product_categ_id' in vals:
Product = self.env['product.product']
for asset_categ in self:
for asset_profile in self:
products = Product.search([
('asset', '=', True),
('asset_category_id', '=', asset_categ.id)])
products.write({'categ_id': asset_categ.product_categ_id.id})
('asset_profile_id', '=', asset_profile.id)])
products.write({'categ_id': asset_profile.product_categ_id.id})
return res


class AccountAssetDepreciationLine(models.Model):
_inherit = 'account.asset.depreciation.line'
class AccountAssetLine(models.Model):
_inherit = 'account.asset.line'

type = fields.Selection(
[('create', 'Purchase Value'),
('depreciate', 'Depreciation'),
('remove', 'Asset Removal'),
],
)
fiscalyear_id = fields.Many2one(
'account.fiscalyear',
string='Fiscalyear',
Expand All @@ -494,13 +499,13 @@ def _compute_fiscalyear_id(self):
for rec in self:
rec.fiscalyear_id = Fiscal.find(dt=rec.line_date)

def _setup_move_line_data(self, depreciation_line, depreciation_date,
period_id, account_id, type, move_id, context):
move_line_data = super(AccountAssetDepreciationLine, self).\
_setup_move_line_data(depreciation_line, depreciation_date,
period_id, account_id, type,
move_id, context)
asset = depreciation_line.asset_id
@api.multi
def _setup_move_line_data(self, depreciation_date,
period, account, type, move):
move_line_data = super(AccountAssetLine, self).\
_setup_move_line_data(depreciation_date,
period, account, type, move)
asset = self.asset_id
move_line_data.update({'section_id': asset.owner_section_id.id,
'project_id': asset.owner_project_id.id})
return move_line_data
Expand All @@ -510,7 +515,7 @@ class AssetRepairNote(models.Model):
_name = 'asset.repair.note'

asset_id = fields.Many2one(
'account.asset.asset',
'account.asset',
string='Asset',
ondelete='cascade',
index=True,
Expand All @@ -532,7 +537,7 @@ class AccountAssetDepreciationSummary(models.Model):
_order = 'fiscalyear_id'

asset_id = fields.Many2one(
'account.asset.asset',
'account.asset',
string='Asset',
readonly=True,
)
Expand All @@ -551,7 +556,7 @@ def init(self, cr):
_sql = """
select min(id) as id, asset_id, fiscalyear_id,
sum(amount) as amount_depreciate
from account_asset_depreciation_line a
from account_asset_line a
where type = 'depreciate' and fiscalyear_id is not null
group by asset_id, fiscalyear_id
"""
Expand Down
10 changes: 5 additions & 5 deletions pabi_asset_management/models/asset_changeowner.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ class AccountAssetChangeowner(models.Model):
states={'draft': [('readonly', False)]},
)
asset_ids = fields.Many2many(
'account.asset.asset',
'account_asset_asset_changeowner_rel',
'account.asset',
'account_asset_changeowner_rel',
'changeowner_id', 'asset_id',
string='Assets to Change Owner',
domain=[('type', '!=', 'view')],
Expand Down Expand Up @@ -139,7 +139,7 @@ def _changeowner(self):
"""
self.ensure_one()
AccountMove = self.env['account.move']
Asset = self.env['account.asset.asset']
Asset = self.env['account.asset']
Period = self.env['account.period']
period = Period.find()
# New Owner
Expand All @@ -154,7 +154,7 @@ def _changeowner(self):
_('Asset %s change to the same owner!') % (asset.code))
new_owner = {'owner_project_id': project.id,
'owner_section_id': section.id,
'responsible_user': responsible_user.id, }
'responsible_user': responsible_user, }
# Moving of each asset to the new owner
for asset in self.asset_ids:
move_lines = []
Expand All @@ -175,7 +175,7 @@ def _changeowner(self):
move_lines.append(new_depre_move_lines_dict)
# Finalize all moves before create it.
final_move_lines = [(0, 0, x) for x in move_lines]
move_dict = {'journal_id': asset.category_id.journal_id.id,
move_dict = {'journal_id': asset.profile_id.journal_id.id,
'line_id': final_move_lines,
'period_id': period.id,
'date': fields.Date.context_today(self),
Expand Down
40 changes: 22 additions & 18 deletions pabi_asset_management/models/asset_removal.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ class AccountAssetRemoval(models.Model):
readonly=True,
copy=False,
)
target_status = fields.Selection(
[('dispose', u'จำหน่าย'),
('lost', u'สูญหาย'), ],
string='Target Status',
target_status = fields.Many2one(
'account.asset.status',
string='Asset Status',
domain="[('map_state', '=', 'removed')]",
required=True,
readonly=True,
states={'draft': [('readonly', False)]},
Expand All @@ -67,16 +67,17 @@ def default_get(self, field_list):
for asset_id in asset_ids:
Remove = self.env['account.asset.remove'].\
with_context(active_id=asset_id)
vals = Remove._get_sale()
asset_removal_lines.append({
'asset_id': asset_id,
'user_id': user_id,
'target_status': target_status,
'sale_value': vals['sale_value'],
'account_sale_id': vals['account_sale_id'],
'account_plus_value_id': Remove._get_plus_account(),
'account_min_value_id': Remove._get_min_account(),
'account_residual_value_id': Remove._get_residual_account(),
'sale_value': Remove._default_sale_value(),
'account_sale_id': Remove._default_account_sale_id(),
'account_plus_value_id':
Remove._default_account_plus_value_id(),
'account_min_value_id': Remove._default_account_min_value_id(),
'account_residual_value_id':
Remove._default_account_residual_value_id(),
'posting_regime': Remove._get_posting_regime(),
})
res['removal_asset_ids'] = asset_removal_lines
Expand Down Expand Up @@ -135,16 +136,16 @@ class AccountAssetRemovalLine(models.Model):
readonly=True,
)
asset_id = fields.Many2one(
'account.asset.asset',
'account.asset',
string='Asset',
domain=[('type', '=', 'normal'),
('state', '=', 'open')],
required=True,
)
target_status = fields.Selection(
[('dispose', u'จำหน่าย'),
('lost', u'สูญหาย'), ],
string='Target Status',
target_status = fields.Many2one(
'account.asset.status',
string='Asset Status',
domain="[('map_state', '=', 'removed')]",
required=True,
)
_sql_constraints = [
Expand All @@ -161,7 +162,10 @@ def _onchange_asset_id(self):
vals = Remove._get_sale()
self.sale_value = vals['sale_value']
self.account_sale_id = vals['account_sale_id']
self.account_plus_value_id = Remove._get_plus_account()
self.account_min_value_id = Remove._get_min_account()
self.account_residual_value_id = Remove._get_residual_account()
self.account_plus_value_id = \
Remove._default_account_plus_value_id()
self.account_min_value_id = \
Remove._default_account_min_value_id()
self.account_residual_value_id = \
Remove._default_account_residual_value_id()
self.posting_regime = Remove._get_posting_regime()
Loading

0 comments on commit c4ae56d

Please sign in to comment.