~eduardo-bayardo-bias/openobject-addons/bias_trunk_v8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2012 OpenERP SA (<http://openerp.com>)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import fields, osv
from openerp.tools.translate import _
import openerp.addons.decimal_precision as dp

class crm_lead(osv.Model):
    _inherit = 'crm.lead'

    def _is_asigned(self, cr, uid, ids, name, args, context=None):
        result = {}
        for rec in self.browse(cr, uid, ids, context=context):
            result[rec.id] = not rec.final_close and rec.asigned_by and (rec.asigned_by.id == uid)
        return result


    _columns = {
        ################# SALES #################
        'partner_invoice': fields.char('Invoice Address', size=200),
        'partner_shipping': fields.char('Delivery Address', size=200),
        'delivery_time': fields.char('Delivery time', size=150),
        'operating_conditions': fields.char('Operating Conditions', size=200),
        'main_contact': fields.char('Main Contact', size=150),
        'technical_contact': fields.char('Technical Contact', size=150),
        'invoice_contact': fields.char('Invoice Contact', size=150),
        'payment_contact': fields.char('Payment Contact', size=150),
        'purchase_contact': fields.char('Purchase Contact', size=150),
        'leadime': fields.char('Leadtime', size=150),
        'date_start': fields.date('Date Start'),
        'date_stop': fields.date('Date Stop'),
        'conditions': fields.char('Terms and conditions', size=200),
        'payment_terms': fields.integer('Payment Terms'),
        'advance': fields.char('Advance', size=150),
        'travel_expenses': fields.float('Travel Expenses', digits_compute= dp.get_precision('Account') ),
        'm_o': fields.float('M.O.', digits_compute= dp.get_precision('Account')),
        'other': fields.char('Other', size=200),
        'po_contract_1': fields.binary('Purchase Order / Decision / Contract'),
        'po_contract_2': fields.binary('Purchase Order / Decision / Contract'),
        'quotation': fields.binary('Quotation'),
        'authorization_evidence': fields.char('Authorization Evidence', size=100),
        'evidence': fields.text('Evidence'),
        'sale_observations': fields.text('Sale Observations'),
        'sale_state': fields.selection([('authorized','Sale Autorized'), ('rejected','Sale Rejected')], 'Sale State', track_visibility='onchange'),
        ################# FINANCIAL #################
        'purchase_order': fields.boolean('Purchase Order'),
        'billing': fields.boolean('Billing'),
        'billing_support': fields.boolean('Billing Support'),
        'credit_terms': fields.boolean('Credit Terms'),
        'commertial_terms': fields.boolean('Commertial Terms'),
        'financial_notes': fields.text('Financial Notes'),
        'nt_purchase_order': fields.text('Purchase Order'),
        'nt_billing': fields.text('Billing'),
        'nt_billing_support': fields.text('Billing Support'),
        'nt_credit_terms': fields.text('Credit Terms'),
        'nt_commertial_terms': fields.text('Commertial Terms'),
        'financial_state': fields.selection([('authorized','Financial Autorized'), ('rejected','Financial Rejected')], 'Financial State', track_visibility='onchange'),
        ################# ENGINEERING #################
        'design_by': fields.related('assigned_id', type='many2one', relation='res.users', string='Design by', readonly=True),
        'validated_by': fields.many2one('res.users', 'Validated by'),
        'asigned_by': fields.many2one('res.users', 'Asigned by'),
        'validated_notes': fields.text('Validation Notes'),
        'gang_allocation': fields.char('Gang Allocation', size=300),
        'mo_allocation': fields.char('Amount/Gang/MO Allocation ', size=200),
        'gang_travel_expenses': fields.float('Gang Travel Expenses', digits_compute= dp.get_precision('Account') ),
        'ap_travel_expenses': fields.float('AP Travel Expenses', digits_compute= dp.get_precision('Account') ),
        'vobo': fields.boolean('Vo.Bo.'),
        'nt_change_control': fields.text('Change Control Notes'),
        'change_control': fields.binary('Change Control'),
        'material_chart': fields.binary('Material Chart'),
        'material_return': fields.binary('Material Return'),
        'technical_memory': fields.binary('Technical Memory'),
        'delivery_report': fields.binary('Delivery Report'),
        'blueprints': fields.binary('Blueprints'),
        'photos': fields.binary('Photos'),
        'diagrams': fields.binary('Diagrams'),
        'fluke_test': fields.binary('Fluke Test'),
        'certifications': fields.binary('Certifications'),
        'operative_close': fields.date('Opertive Close'),
        'operative_documentry': fields.date('Operative Documentary'),
        'final_close': fields.date('Final Close'),
        'is_asigned': fields.function(_is_asigned, type='boolean', string="Is Assigned"),
    }   

    def get_obj(self, cr, uid, model, external_id):
        # Return id from Model and External ID
        obj_id = False
        try:
            external_id = external_id.split('.')[1]
        except:
            osv.except_osv(_('Warning!'), _('Such External ID does not Exist!'))
        model_data_pool = self.pool.get('ir.model.data')
        ir_model_data_id = model_data_pool.search(cr, uid, [('model', '=', model), ('name', '=', external_id)])
        if ir_model_data_id:
            ir_model_data_obj = model_data_pool.browse(cr, uid, ir_model_data_id)[0]
            obj = self.pool.get(model).browse(cr, uid, ir_model_data_obj.res_id)
            obj_id = obj and obj.id
        else:
            osv.except_osv(_('Warning!'), _('Such External ID does not Exist!'))
        return obj_id

    def is_manager(self, cr, uid, context=None):
        usr_groups = []
        groups_id = [x.id for x in self.pool.get('res.users').browse(cr, uid, uid).groups_id]
        groups =  [{'external_id':'group_account_manager','rol':'financial'}]
        groups += [{'external_id':'group_sale_manager','rol':'sale'}] 
        groups += [{'external_id':'group_operation_supervisor_user','rol':'operation'}]
        model = 'res.groups'
        for item in groups:
            external_id = item['external_id'] 
            obj_id = self.get_obj(cr, uid, model, external_id)
            if obj_id in groups_id:
                usr_groups.append(item['rol'])
        return usr_groups

    def button_authorize(self, cr, uid, ids, context=None):
        if 'financial' not in self.is_manager(cr, uid, context):
            raise osv.except_osv(_('Error!'), _("Only Financial Manager can authorize this document."))
        oportunity_id = ids[0]
        self.write(cr, uid, oportunity_id, {'financial_state':'authorized'})
        return True
    
    def button_reject(self, cr, uid, ids, context=None):
        if 'financial' not in self.is_manager(cr, uid, context):
            raise osv.except_osv(_('Error!'), _("Only Financial Manager can reject this document."))
        oportunity_id = ids[0]
        self.write(cr, uid, oportunity_id, {'financial_state':'rejected'})
        return True

    def button_authorize_sale(self, cr, uid, ids, context=None):
        if 'sale' not in self.is_manager(cr, uid, context):
            raise osv.except_osv(_('Error!'), _("Only Sale Manager can authorize this document."))
        oportunity_id = ids[0]
        self.write(cr, uid, oportunity_id, {'sale_state':'authorized'})
        return True
    
    def button_reject_sale(self, cr, uid, ids, context=None):
        if 'sale' not in self.is_manager(cr, uid, context):
            raise osv.except_osv(_('Error!'), _("Only Sale Manager can reject this document."))
        oportunity_id = ids[0]
        self.write(cr, uid, oportunity_id, {'sale_state':'rejected'})
        return True

    def button_po(self, cr, uid, ids, context=None):
        return True

    def button_picking_in(self, cr, uid, ids, context=None):
        return True

    def button_supplier_invoice(self, cr, uid, ids, context=None):
        return True

    def button_picking_out(self, cr, uid, ids, context=None):
        return True

    def button_payment(self, cr, uid, ids, context=None):
        return True

    def button_cost_chart(self, cr, uid, ids, context=None):
        return True


    
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: