~crm-core-editors/openerp-crm/7.0

« back to all changes in this revision

Viewing changes to lettermgmt/res_letter.py

  • Committer: Holger Brunn
  • Author(s): sandy.carter at savoirfairelinux
  • Date: 2014-03-24 11:05:52 UTC
  • Revision ID: hbrunn@therp.nl-20140324110552-ew1jgeb4lq6u7z6h
[ADD] lettermanagement module with automatic hr bindings module

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    Parthiv Pate, Tech Receptives, Open Source For Ideas
 
5
#    Copyright (C) 2009-Today Tech Receptives(http://techreceptives.com).
 
6
#    All Rights Reserved
 
7
#
 
8
#
 
9
#    This program is free software: you can redistribute it and/or modify
 
10
#    it under the terms of the GNU General Public License as published by
 
11
#    the Free Software Foundation, either version 3 of the License, or
 
12
#    (at your option) any later version.
 
13
#
 
14
#    This program is distributed in the hope that it will be useful,
 
15
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
#    GNU General Public License for more details.
 
18
#
 
19
#    You should have received a copy of the GNU General Public License
 
20
#    along with this program.  If not, see http://www.gnu.org/licenses/.
 
21
#
 
22
##############################################################################
 
23
 
 
24
from openerp.osv import fields, orm
 
25
from openerp.tools.translate import _
 
26
import time
 
27
 
 
28
 
 
29
class res_letter(orm.Model):
 
30
    """A register class to log all movements regarding letters"""
 
31
    _name = 'res.letter'
 
32
    _description = _("Log of Letter Movements")
 
33
    _inherit = 'mail.thread'
 
34
 
 
35
    def _get_number(self, cr, uid, context=None):
 
36
        if context is None:
 
37
            context = {}
 
38
        sequence_pool = self.pool.get('ir.sequence')
 
39
        move_type = context.get('move', 'in')
 
40
        return sequence_pool.get(cr, uid, '%s.letter' % move_type, context=context)
 
41
 
 
42
    _columns = {
 
43
        'name': fields.text('Subject', help="Subject of letter."),
 
44
        'folder_id': fields.many2one('letter.folder', string='Folder',
 
45
                                     help='Folder which contains letter.'),
 
46
        'number': fields.char('Number', size=32, help="Auto Generated Number of letter.", required=True),
 
47
        'move': fields.selection([('in', 'IN'), ('out', 'OUT')], 'Move', readonly=True,
 
48
                                 help="Incoming or Outgoing Letter."),
 
49
        'type': fields.many2one('letter.type', 'Type', help="Type of Letter, Depending upon size."),
 
50
        'class': fields.many2one('letter.class', 'Class', help="Classification of Document."),
 
51
        'date': fields.datetime('Letter Date', required=True, help='Created Date of Letter Logging.'),
 
52
        'snd_rec_date': fields.datetime('Sent / Received Date'),
 
53
        'partner_id': fields.many2one('res.partner', string='Follow up by', help='Recipient of letter.'),
 
54
        'recipient_ids': fields.many2many('res.users', string="Send to", help="Persons who will receive Letter."),
 
55
        'send_id': fields.many2one('res.users', "Send By", help="Person who sent Letter."),
 
56
        'send_street': fields.char('Street'),
 
57
        'send_city': fields.char('City'),
 
58
        'send_zip': fields.char('Zip'),
 
59
        'send_country': fields.many2one('res.country', 'Country'),
 
60
        'note': fields.text('Note'),
 
61
        'state': fields.selection([('draft', 'Draft'),
 
62
                                   ('created', 'Created'),
 
63
                                   ('validated', 'Validated'),
 
64
                                   ('rec', 'Received'),
 
65
                                   ('sent', 'Sent'),
 
66
                                   ('rec_bad', 'Received Damage'),
 
67
                                   ('rec_ret', 'Received But Returned'),
 
68
                                   ('cancel', 'Cancelled')],
 
69
                                  'State', readonly=True),
 
70
        'parent_id': fields.many2one('res.letter', 'Parent'),
 
71
        'child_line': fields.one2many('res.letter', 'parent_id', 'Letter Lines'),
 
72
        'channel_id': fields.many2one('letter.channel', 'Sent / Receive Source'),
 
73
        'history_line': fields.one2many('letter.history', 'register_id', 'History'),
 
74
        'orig_ref': fields.char('Original Reference', help="Reference Number at Origin."),
 
75
        'expeditor_ref': fields.char('Expeditor Reference', help="Reference Number used by Expeditor."),
 
76
        'track_ref': fields.char('Tracking Reference', help="Reference Number used for Tracking."),
 
77
        'weight': fields.float('Weight (in KG)'),
 
78
        'size': fields.char('Size', size=64),
 
79
        'reassignment_ids': fields.one2many('letter.reassignment', 'letter_id',
 
80
                                            string='Reassignment lines',
 
81
                                            help='Reassignment users and comments'),
 
82
        'extern_partner_ids': fields.many2many('res.partner', string='Recipients'),
 
83
    }
 
84
 
 
85
    _defaults = {
 
86
        'number': _get_number,
 
87
        'date': time.strftime('%Y-%m-%d %H:%M:%S'),
 
88
        'move': lambda self, cr, uid, context: context.get('move', 'in'),
 
89
        'state': 'draft',
 
90
    }
 
91
 
 
92
    def history(self, cr, uid, ids, keyword=False, context=None):
 
93
        lh_pool = self.pool.get('letter.history')
 
94
        for id in ids:
 
95
            lh_pool.create(cr, uid, {'name': keyword, 'user_id': uid, 'register_id': id}, context=context)
 
96
        return True
 
97
 
 
98
    def action_received(self, cr, uid, ids, context=None):
 
99
        """Put the state of the letter into Received"""
 
100
        for letter in self.browse(cr, uid, ids, context=context):
 
101
            self.write(cr, uid, [letter.id], {'state': 'rec'}, context=context)
 
102
        return True
 
103
 
 
104
    def action_cancel(self, cr, uid, ids, context=None):
 
105
        """Put the state of the letter into Cancelled"""
 
106
        for letter in self.browse(cr, uid, ids, context=context):
 
107
            self.write(cr, uid, [letter.id], {'state': 'cancel'}, context=context)
 
108
        return True
 
109
 
 
110
    def action_create(self, cr, uid, ids, context=None):
 
111
        """Put the state of the letter into Crated"""
 
112
        for letter in self.browse(cr, uid, ids, context=context):
 
113
            self.write(cr, uid, [letter.id], {'state': 'created'}, context=context)
 
114
        return True
 
115
 
 
116
    def action_validate(self, cr, uid, ids, context=None):
 
117
        """Put the state of the letter into Validated"""
 
118
        for letter in self.browse(cr, uid, ids, context=context):
 
119
            self.write(cr, uid, [letter.id], {'state': 'validated'}, context=context)
 
120
        return True
 
121
 
 
122
    def action_send(self, cr, uid, ids, context=None):
 
123
        """Put the state of the letter into sent"""
 
124
        for letter in self.browse(cr, uid, ids, context=context):
 
125
            self.write(cr, uid, [letter.id], {'state': 'sent'}, context=context)
 
126
        return True
 
127
 
 
128
    def action_rec_ret(self, cr, uid, ids, context=None):
 
129
        """Put the state of the letter into Received but Returned"""
 
130
        for letter in self.browse(cr, uid, ids, context=context):
 
131
            self.write(cr, uid, [letter.id], {'state': 'rec_ret'}, context=context)
 
132
        return True
 
133
 
 
134
    def action_rec_bad(self, cr, uid, ids, context=None):
 
135
        """Put the state of the letter into Received but Damaged"""
 
136
        for letter in self.browse(cr, uid, ids, context=context):
 
137
            self.write(cr, uid, [letter.id], {'state': 'rec_bad'}, context=context)
 
138
        return True
 
139
 
 
140
    def action_set_draft(self, cr, uid, ids, context=None):
 
141
        """Put the state of the letter into draft"""
 
142
        for letter in self.browse(cr, uid, ids, context=context):
 
143
            self.write(cr, uid, [letter.id], {'state': 'draft'}, context=context)
 
144
        return True
 
145
 
 
146
    def onchange_send_id(self, cr, uid, ids, send_id=False, context=None):
 
147
        """Automatically set address from Receiver/Sender"""
 
148
        result = {}
 
149
        if send_id:
 
150
            user = self.pool['res.users'].browse(cr, uid, send_id, context=context)
 
151
            if user:
 
152
                result['value'] = {
 
153
                    'send_street': user.street,
 
154
                    'send_city': user.city,
 
155
                    'send_zip': user.zip,
 
156
                    'send_country': user.country_id.id,
 
157
                }
 
158
        return result
 
159
 
 
160
    def create(self, cr, user, vals, context=None):
 
161
        """Set address from Receiver/Sender on create"""
 
162
        address_vals = self.onchange_send_id(cr, user, [], vals.get('send_id'), context=context)
 
163
        vals = dict(vals.items() + address_vals.get('value', {}).items())
 
164
        return super(res_letter, self).create(cr, user, vals, context=context)
 
165
 
 
166
    def write(self, cr, user, ids, vals, context=None):
 
167
        """Set address from Receiver/Sender on write"""
 
168
        address_vals = self.onchange_send_id(cr, user, ids, vals.get('send_id'), context=context)
 
169
        vals = dict(vals.items() + address_vals.get('value', {}).items())
 
170
        return super(res_letter, self).write(cr, user, ids, vals, context=context)