|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from openerp import tools |
| 3 | +from openerp import models, fields, api, _ |
| 4 | +from openerp.exceptions import ValidationError |
| 5 | +from .budget_plan_common import BPCommon, BPLMonthCommon |
| 6 | +from openerp.addons.account_budget_activity.models.account_activity \ |
| 7 | + import ActivityCommon |
| 8 | +from openerp.addons.document_status_history.models.document_history import \ |
| 9 | + LogCommon |
| 10 | + |
| 11 | + |
| 12 | +class BudgetPlanUnit(BPCommon, LogCommon, models.Model): |
| 13 | + _name = 'budget.plan.unit' |
| 14 | + _inherit = ['mail.thread'] |
| 15 | + _description = "Unit - Budget Plan" |
| 16 | + |
| 17 | + # COMMON |
| 18 | + plan_line_ids = fields.One2many( |
| 19 | + 'budget.plan.unit.line', |
| 20 | + 'plan_id', |
| 21 | + string='Budget Plan Lines', |
| 22 | + copy=True, |
| 23 | + track_visibility='onchange', |
| 24 | + ) |
| 25 | + plan_revenue_line_ids = fields.One2many( |
| 26 | + 'budget.plan.unit.line', |
| 27 | + 'plan_id', |
| 28 | + string='Revenue Plan Lines', |
| 29 | + copy=True, |
| 30 | + domain=[('budget_method', '=', 'revenue')], # Have domain |
| 31 | + track_visibility='onchange', |
| 32 | + ) |
| 33 | + plan_expense_line_ids = fields.One2many( |
| 34 | + 'budget.plan.unit.line', |
| 35 | + 'plan_id', |
| 36 | + string='Expense Plan Lines', |
| 37 | + copy=True, |
| 38 | + domain=[('budget_method', '=', 'expense')], # Have domain |
| 39 | + track_visibility='onchange', |
| 40 | + ) |
| 41 | + plan_summary_revenue_line_ids = fields.One2many( |
| 42 | + 'budget.plan.unit.summary', |
| 43 | + 'plan_id', |
| 44 | + string='Summary by Activity Group', |
| 45 | + domain=[('budget_method', '=', 'revenue')], |
| 46 | + readonly=True, |
| 47 | + help="Summary by Activity Group View", |
| 48 | + ) |
| 49 | + plan_summary_expense_line_ids = fields.One2many( |
| 50 | + 'budget.plan.unit.summary', |
| 51 | + 'plan_id', |
| 52 | + string='Summary by Activity Group', |
| 53 | + domain=[('budget_method', '=', 'expense')], |
| 54 | + readonly=True, |
| 55 | + help="Summary by Activity Group View", |
| 56 | + ) |
| 57 | + # Select Dimension - Section |
| 58 | + section_id = fields.Many2one( |
| 59 | + 'res.section', |
| 60 | + string='Section', |
| 61 | + required=True, |
| 62 | + ) |
| 63 | + org_id = fields.Many2one( |
| 64 | + 'res.org', |
| 65 | + string='Org', |
| 66 | + related='section_id.org_id', |
| 67 | + readonly=True, |
| 68 | + store=True, |
| 69 | + ) |
| 70 | + division_id = fields.Many2one( |
| 71 | + 'res.division', |
| 72 | + string='Division', |
| 73 | + related='section_id.division_id', |
| 74 | + readonly=True, |
| 75 | + store=True, |
| 76 | + ) |
| 77 | + |
| 78 | + @api.model |
| 79 | + def generate_plans(self, fiscalyear_id=None): |
| 80 | + if not fiscalyear_id: |
| 81 | + raise ValidationError(_('No fiscal year provided!')) |
| 82 | + # Find existing plans, and exclude them |
| 83 | + plans = self.search([('fiscalyear_id', '=', fiscalyear_id)]) |
| 84 | + _ids = plans.mapped('section_id')._ids |
| 85 | + # Find sections |
| 86 | + sections = self.env['res.section'].search([('id', 'not in', _ids)]) |
| 87 | + plan_ids = [] |
| 88 | + for section in sections: |
| 89 | + plan = self.create({'fiscalyear_id': fiscalyear_id, |
| 90 | + 'section_id': section.id, |
| 91 | + 'user_id': False}) |
| 92 | + plan_ids.append(plan.id) |
| 93 | + return plan_ids |
| 94 | + |
| 95 | + |
| 96 | +class BudgetPlanUnitLine(BPLMonthCommon, ActivityCommon, models.Model): |
| 97 | + _name = 'budget.plan.unit.line' |
| 98 | + _description = "Unit - Budget Plan Line" |
| 99 | + |
| 100 | + # COMMON |
| 101 | + chart_view = fields.Selection( |
| 102 | + default='unit_base', # Unit |
| 103 | + ) |
| 104 | + plan_id = fields.Many2one( |
| 105 | + 'budget.plan.unit', |
| 106 | + string='Budget Plan', |
| 107 | + ondelete='cascade', |
| 108 | + index=True, |
| 109 | + required=True, |
| 110 | + ) |
| 111 | + # Extra |
| 112 | + section_id = fields.Many2one( |
| 113 | + related='plan_id.section_id', |
| 114 | + store=True, |
| 115 | + readonly=True, |
| 116 | + ) |
| 117 | + unit = fields.Float( |
| 118 | + string='Unit', |
| 119 | + ) |
| 120 | + activity_unit_price = fields.Float( |
| 121 | + string='Unit Price', |
| 122 | + ) |
| 123 | + activity_unit = fields.Float( |
| 124 | + string='Activity Unit', |
| 125 | + ) |
| 126 | + total_budget = fields.Float( |
| 127 | + string='Total Budget', |
| 128 | + ) |
| 129 | + |
| 130 | + @api.multi |
| 131 | + def _write(self, vals): # Use _write, as it triggered on related field |
| 132 | + res = super(BudgetPlanUnitLine, self)._write(vals) |
| 133 | + print self.section_id |
| 134 | + if not self._context.get('MyModelLoopBreaker', False): |
| 135 | + self.update_related_dimension({'section_id': self.section_id.id}) |
| 136 | + return res |
| 137 | + |
| 138 | + |
| 139 | +class BudgetPlanUnitSummary(models.Model): |
| 140 | + _name = 'budget.plan.unit.summary' |
| 141 | + _auto = False |
| 142 | + _order = 'budget_method desc, activity_group_id' |
| 143 | + |
| 144 | + plan_id = fields.Many2one( |
| 145 | + 'budget.plan.unit', |
| 146 | + string='Budget Plan', |
| 147 | + ) |
| 148 | + budget_method = fields.Selection( |
| 149 | + [('revenue', 'Revenue'), |
| 150 | + ('expense', 'Expense')], |
| 151 | + string='Budget Method', |
| 152 | + ) |
| 153 | + activity_group_id = fields.Many2one( |
| 154 | + 'account.activity.group', |
| 155 | + string='Activity Group', |
| 156 | + ) |
| 157 | + m1 = fields.Float( |
| 158 | + string='Oct', |
| 159 | + ) |
| 160 | + m2 = fields.Float( |
| 161 | + string='Nov', |
| 162 | + ) |
| 163 | + m3 = fields.Float( |
| 164 | + string='Dec', |
| 165 | + ) |
| 166 | + m4 = fields.Float( |
| 167 | + string='Jan', |
| 168 | + ) |
| 169 | + m5 = fields.Float( |
| 170 | + string='Feb', |
| 171 | + ) |
| 172 | + m6 = fields.Float( |
| 173 | + string='Mar', |
| 174 | + ) |
| 175 | + m7 = fields.Float( |
| 176 | + string='Apr', |
| 177 | + ) |
| 178 | + m8 = fields.Float( |
| 179 | + string='May', |
| 180 | + ) |
| 181 | + m9 = fields.Float( |
| 182 | + string='Jun', |
| 183 | + ) |
| 184 | + m10 = fields.Float( |
| 185 | + string='July', |
| 186 | + ) |
| 187 | + m11 = fields.Float( |
| 188 | + string='Aug', |
| 189 | + ) |
| 190 | + m12 = fields.Float( |
| 191 | + string='Sep', |
| 192 | + ) |
| 193 | + planned_amount = fields.Float( |
| 194 | + string='Planned Amount', |
| 195 | + ) |
| 196 | + |
| 197 | + def init(self, cr): |
| 198 | + tools.drop_view_if_exists(cr, self._table) |
| 199 | + cr.execute("""CREATE or REPLACE VIEW %s as ( |
| 200 | + select min(id) id, plan_id, activity_group_id, budget_method, |
| 201 | + sum(m1) m1, sum(m2) m2, sum(m3) m3, sum(m4) m4, |
| 202 | + sum(m5) m5, sum(m6) m6, sum(m7) m7, sum(m8) m8, sum(m9) m9, |
| 203 | + sum(m10) m10, sum(m11) m11, sum(m12) m12, |
| 204 | + sum(planned_amount) planned_amount |
| 205 | + from budget_plan_unit_line l |
| 206 | + group by plan_id, activity_group_id, budget_method |
| 207 | + )""" % (self._table, )) |
0 commit comments