~openerp-commiter/openobject-addons/trunk-extra-addons

« back to all changes in this revision

Viewing changes to cci_mission/cci_mission.py

  • Committer: Jay vora
  • Date: 2008-05-20 11:35:25 UTC
  • Revision ID: jvo@tinyerp.com-03a2449166812d882b00fbed4ab6e013486d0833

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2007 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#
 
5
# WARNING: This program as such is intended to be used by professional
 
6
# programmers who take the whole responsability of assessing all potential
 
7
# consequences resulting from its eventual inadequacies and bugs
 
8
# End users who are looking for a ready-to-use solution with commercial
 
9
# garantees and support are strongly adviced to contract a Free Software
 
10
# Service Company
 
11
#
 
12
# This program is Free Software; you can redistribute it and/or
 
13
# modify it under the terms of the GNU General Public License
 
14
# as published by the Free Software Foundation; either version 2
 
15
# of the License, or (at your option) any later version.
 
16
#
 
17
# This program is distributed in the hope that it will be useful,
 
18
# but WITHOUT ANY WARRANTY; without even the impli(ed warranty of
 
19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
# GNU General Public License for more details.
 
21
#
 
22
# You should have received a copy of the GNU General Public License
 
23
# along with this program; if not, write to the Free Software
 
24
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
25
#
 
26
##############################################################################
 
27
 
 
28
from osv import fields, osv
 
29
import time
 
30
from datetime import date,timedelta
 
31
import datetime
 
32
import pooler
 
33
import netsvc
 
34
 
 
35
STATE = [
 
36
        ('none', 'Non Member'),
 
37
        ('canceled', 'Canceled Member'),
 
38
        ('old', 'Old Member'),
 
39
        ('waiting', 'Waiting Member'),
 
40
        ('invoiced', 'Invoiced Member'),
 
41
        ('associated', 'Associated Member'),
 
42
        ('free', 'Free Member'),
 
43
        ('paid', 'Paid Member'),
 
44
]
 
45
 
 
46
class res_partner(osv.osv):
 
47
        _inherit = 'res.partner'
 
48
        _description = 'res.partner'
 
49
 
 
50
        _columns = {
 
51
                'asker_name': fields.char('Asker Name',size=50),
 
52
                'asker_address': fields.char('Asker Address',size=50),
 
53
                'asker_zip_id': fields.many2one('res.partner.zip','Asker Zip Code'),
 
54
                'sender_name': fields.char('Sender Name',size=50),
 
55
                'insurer_id' : fields.char('Insurer ID',size=50),
 
56
        }
 
57
 
 
58
res_partner()
 
59
 
 
60
class cci_missions_site(osv.osv):
 
61
        _name = 'cci_missions.site'
 
62
        _description = 'cci_missions.site'
 
63
 
 
64
        _columns = {
 
65
                'name' : fields.char('Name of the Site',size=50,required=True),
 
66
                'official_name_1' : fields.char('Official Name of the Site',size=50,required=True),
 
67
                'official_name_2' : fields.char('Official Name of the Site',size=50),
 
68
                'official_name_3' : fields.char('Official Name of the Site',size=50),
 
69
                'official_name_4' : fields.char('Official Name of the Site',size=50),
 
70
                'embassy_sequence_id' : fields.many2one('ir.sequence','Sequence for Embassy Folder'),
 
71
        }
 
72
 
 
73
cci_missions_site()
 
74
 
 
75
class cci_missions_embassy_folder(osv.osv):
 
76
        _name = 'cci_missions.embassy_folder'
 
77
        _description = 'cci_missions.embassy_folder'
 
78
        _inherits = {'crm.case': 'crm_case_id'}
 
79
 
 
80
        def _cci_mission_send(self, cr, uid, ids, *args):
 
81
                self.write(cr, uid, ids, {'state':'pending',})
 
82
                cases = self.browse(cr, uid, ids)
 
83
                self._history(cr, uid, cases, 'Send', history=True,)
 
84
                return True
 
85
 
 
86
        def _cci_mission_got_back(self,cr,uid,ids,*args):
 
87
                self.write(cr, uid, ids, {'state':'open',})
 
88
                cases = self.browse(cr, uid, ids)
 
89
                self._history(cr, uid, cases, 'Got Back', history=True)
 
90
                return True
 
91
 
 
92
        def _cci_mission_done_folder(self,cr,uid,ids,*args):
 
93
                self.write(cr, uid, ids, {'state':'done','invoice_date': time.strftime('%Y-%m-%d %H:%M:%S')})
 
94
                cases = self.browse(cr, uid, ids)
 
95
                self._history(cr, uid, cases, 'Invoiced', history=True)
 
96
                return True
 
97
 
 
98
        def _history(self, cr, uid,ids,keyword, history=False, email=False, context={}):
 
99
                for case in ids:
 
100
                        data = {
 
101
                                'name': keyword,
 
102
                                'som': case.som.id,
 
103
                                'canal_id': case.canal_id.id,
 
104
                                'user_id': uid,
 
105
                                'case_id': case.crm_case_id.id
 
106
                        }
 
107
                        obj = self.pool.get('crm.case.log')
 
108
#                       if history and case.description:
 
109
#                               obj = self.pool.get('crm.case.history')
 
110
#                               data['description'] = case.description
 
111
#                               data['email'] = email or \
 
112
#                               (case.user_id and case.user_id.address_id and \
 
113
#                               case.user_id.address_id.email) or False
 
114
                        obj.create(cr, uid, data, context)
 
115
                return True
 
116
 
 
117
        def create(self, cr, uid, vals, *args, **kwargs):
 
118
#               Overwrite the name field to set next sequence according to the sequence in for the embassy folder related in the site_id
 
119
                if vals['site_id']:
 
120
                        data = self.pool.get('cci_missions.site').browse(cr, uid,vals['site_id'])
 
121
                        seq = self.pool.get('ir.sequence').get(cr, uid,data.embassy_sequence_id.code)
 
122
                        if seq:
 
123
                                vals.update({'name': seq})
 
124
                temp = super(osv.osv,self).create(cr, uid, vals, *args, **kwargs)
 
125
                self._history(cr, uid,self.browse(cr, uid, [temp]), 'Created', history=True)
 
126
                return temp
 
127
 
 
128
 
 
129
 
 
130
        def onchange_partner_id(self, cr, uid, ids, part):
 
131
                if not part:
 
132
                        return {'value':{'partner_address_id': False}}
 
133
                part_obj = self.pool.get('res.partner')
 
134
                data_partner = part_obj.browse(cr,uid,part)
 
135
                if data_partner.alert_legalisations:
 
136
                                raise osv.except_osv('Error!',data_partner.alert_explanation or 'Partner is not valid')
 
137
                addr = part_obj.address_get(cr, uid, [part], ['contact'])
 
138
                data = {'partner_address_id':addr['contact']}
 
139
                return {'value':data}
 
140
 
 
141
        def check_folder_line(self, cr, uid, ids):
 
142
                        #CONSTRAINT: For each embassy Folder, it can only be one embassy_folder_line of each type.
 
143
                data_folder = self.browse(cr,uid,ids)
 
144
                list = []
 
145
                for folder in data_folder:
 
146
                        for line in folder.embassy_folder_line_ids:
 
147
                                if line.type and line.type in list:
 
148
                                        return False
 
149
                                list.append(line.type)
 
150
                return True
 
151
 
 
152
        _columns = {
 
153
                'crm_case_id' : fields.many2one('crm.case','Case'),
 
154
                'member_price' : fields.boolean('Member Price Allowed'),
 
155
                'customer_reference' : fields.char('Folders Reference for the Customer',size=30),
 
156
                'destination_id' : fields.many2one('res.country','Destination Country'),
 
157
                'link_ids': fields.one2many('cci_missions.dossier','embassy_folder_id','Linked Documents'),
 
158
                'internal_note': fields.text('Internal Note'),
 
159
                'invoice_note':fields.text('Note to Display on the Invoice',help='to display as the last embassy_folder_line of this embassy_folder.'),
 
160
                'embassy_folder_line_ids' : fields.one2many('cci_missions.embassy_folder_line','folder_id','Details'),
 
161
                'site_id': fields.many2one('cci_missions.site','Site', required=True),
 
162
                'invoice_date' : fields.datetime('Invoice Date', readonly=True) ,
 
163
                "invoice_id":fields.many2one("account.invoice","Invoice"),
 
164
        }
 
165
 
 
166
        _defaults = {
 
167
                'section_id': lambda obj, cr, uid, context: obj.pool.get('crm.case.section').search(cr, uid, [('name','=','Embassy Folder')])[0],
 
168
                'invoice_date': lambda *a: False,
 
169
                'name': lambda *args: '/',
 
170
                'state' :  lambda *a : 'draft'
 
171
        }
 
172
 
 
173
        _constraints = [(check_folder_line, 'Error: Only One Embassy Folder line allowed for each type!', ['embassy_folder_line_ids'])]
 
174
 
 
175
cci_missions_embassy_folder()
 
176
 
 
177
class cci_missions_embassy_folder_line (osv.osv):
 
178
        _name = 'cci_missions.embassy_folder_line'
 
179
        _description = 'cci_missions.embassy_folder_line '
 
180
 
 
181
 
 
182
        def create(self, cr, uid, vals, *args, **kwargs):
 
183
 
 
184
                prod_name= vals['type'] + str(' Product')
 
185
                cr.execute('select id from product_template where name='"'%s'"''%str(prod_name))
 
186
                prod=cr.fetchone()
 
187
 
 
188
                if prod:
 
189
                        product_id=prod[0]
 
190
                        prod_info = self.pool.get('product.product').browse(cr, uid,product_id)
 
191
                        account =  prod_info.product_tmpl_id.property_account_income.id
 
192
                        if not account:
 
193
                                account = prod_info.categ_id.property_account_income_categ.id
 
194
                        vals['account_id']=account
 
195
                return super(osv.osv,self).create(cr, uid, vals, *args, **kwargs)
 
196
 
 
197
        def write(self, cr, uid, ids,vals, *args, **kwargs):
 
198
 
 
199
                prod_name= vals['type'] + str(' Product')
 
200
                cr.execute('select id from product_template where name='"'%s'"''%str(prod_name))
 
201
                prod=cr.fetchone()
 
202
 
 
203
                if prod:
 
204
                        product_id=prod[0]
 
205
                        prod_info = self.pool.get('product.product').browse(cr, uid,product_id)
 
206
                        account =  prod_info.product_tmpl_id.property_account_income.id
 
207
                        if not account:
 
208
                                account = prod_info.categ_id.property_account_income_categ.id
 
209
                        vals['account_id']=account
 
210
 
 
211
                return super(osv.osv,self).write( cr, uid, ids,vals, *args, **kwargs)
 
212
 
 
213
        def onchange_line_type(self,cr,uid,ids,type):
 
214
                data={}
 
215
                data['courier_cost']=data['customer_amount']=data['account_id']=data['name']=False
 
216
 
 
217
                if not type:
 
218
                        return {'value' : data }
 
219
 
 
220
                data['name']=type
 
221
                prod_name= str(type) + str(' Product')
 
222
                cr.execute('select id from product_template where name='"'%s'"''%str(prod_name))
 
223
                prod=cr.fetchone()
 
224
 
 
225
                if not prod:
 
226
                        return {'value' : data }
 
227
 
 
228
                product_id=prod[0]
 
229
                prod_info = self.pool.get('product.product').browse(cr, uid,product_id)
 
230
                data['courier_cost']=prod_info.standard_price
 
231
                data['customer_amount']=prod_info.list_price
 
232
                account =  prod_info.product_tmpl_id.property_account_income.id
 
233
                if not account:
 
234
                        account = prod_info.categ_id.property_account_income_categ.id
 
235
                data['account_id']=account
 
236
 
 
237
                return {'value' : data }
 
238
 
 
239
        _columns = {
 
240
                'name' : fields.char('Description',size=50,required=True),
 
241
                'folder_id' : fields.many2one('cci_missions.embassy_folder','Related Embassy Folder',required=True),
 
242
                'courier_cost' : fields.float('Couriers Costs'),
 
243
                'customer_amount' : fields.float('Invoiced Amount'),
 
244
                'tax_rate': fields.many2one('account.tax','Tax Rate'),
 
245
                'type' : fields.selection([('CBA','CBA'),('Ministry','Ministry'),('Embassy Consulate','Embassy Consulate'),('Translation','Translation'),('Administrative','Administrative'),('Travel Costs','Travel Costs'),('Others','Others')],'Type'),
 
246
                'account_id' : fields.many2one('account.account', 'Account',required=True),
 
247
        }
 
248
 
 
249
cci_missions_embassy_folder_line()
 
250
 
 
251
class cci_missions_dossier_type(osv.osv):
 
252
        _name = 'cci_missions.dossier_type'
 
253
        _description = 'cci_missions.dossier_type'
 
254
 
 
255
        _columns = {
 
256
                'code' : fields.char('Code',size=3,required=True),
 
257
                'name' : fields.char('Description',size=50,required=True),
 
258
                'original_product_id' : fields.many2one('product.product','Reference for Original Copies',required=True,help='for the association with a pricelist'),
 
259
                'copy_product_id' : fields.many2one('product.product','Reference for Copies',required=True,help='for the association with a pricelist'),
 
260
                'site_id' : fields.many2one('cci_missions.site','Site',required=True),
 
261
                'sequence_id' : fields.many2one('ir.sequence','Sequence',required=True,help='for association with a sequence'),
 
262
                'section' : fields.selection([('certificate','Certificate'),('legalization','Legalization'),('ATA','ATA Carnet')],'Type',required=True),
 
263
                'warranty_product_1': fields.many2one('product.product', 'Warranty product for ATA carnet if Own Risk'),
 
264
                'warranty_product_2': fields.many2one('product.product', 'Warranty product for ATA carnet if not own Risk'),
 
265
        }
 
266
 
 
267
cci_missions_dossier_type()
 
268
 
 
269
class cci_missions_dossier(osv.osv):
 
270
        _name = 'cci_missions.dossier'
 
271
        _description = 'cci_missions.dossier'
 
272
 
 
273
        def create(self, cr, uid, vals, *args, **kwargs):
 
274
                #overwrite the create: if the text_on_invoice field is empty then fill it with name + destination_id.name + (quantity_original)
 
275
                if not vals['text_on_invoice']:
 
276
                        invoice_text = vals['name']
 
277
                        if vals['destination_id']:
 
278
                                destination_data = self.pool.get('res.country').browse(cr,uid,vals['destination_id'])
 
279
                                invoice_text = vals['name'] + ' ' + destination_data.name + ' (' + str(vals['quantity_original'])  + ')'
 
280
                        vals.update({'text_on_invoice': invoice_text})
 
281
                return super(osv.osv,self).create(cr, uid, vals, *args, **kwargs)
 
282
 
 
283
        def get_partner_details(self, cr, uid, ids,order_partner_id):
 
284
                result={}
 
285
                asker_name=False
 
286
                sender_name=False
 
287
                if order_partner_id:
 
288
                        partner_info = self.pool.get('res.partner').browse(cr, uid,order_partner_id)
 
289
                        if not partner_info.asker_name:
 
290
                                asker_name=partner_info.name
 
291
                        else:
 
292
                                asker_name=partner_info.asker_name
 
293
                        if not partner_info.sender_name:
 
294
                                sender_name=partner_info.name
 
295
                        else:
 
296
                                sender_name=partner_info.sender_name
 
297
                result = {'value': {
 
298
                        'asker_name': asker_name,
 
299
                        'sender_name': sender_name}
 
300
                }
 
301
                return result
 
302
 
 
303
#       def _amount_total(self, cr, uid, ids, name, args, context=None):
 
304
#               res ={}
 
305
#               data_dosseir = self.browse(cr,uid,ids)
 
306
#               for data in data_dosseir:
 
307
#                       if data.state =='draft':
 
308
#                               res[data.id] = 0.0
 
309
#                       else :
 
310
#                               res[data.id]=data.invoiced_amount
 
311
#               return res
 
312
 
 
313
        def _amount_subtotal(self, cr, uid, ids, name, args, context=None):
 
314
                res={}
 
315
                data_dosseir = self.browse(cr,uid,ids)
 
316
                for data in data_dosseir:
 
317
                        sum=0.00
 
318
                        for product in data.product_ids:
 
319
                                sum += product.price_subtotal
 
320
                        res[data.id]=sum
 
321
                return res
 
322
 
 
323
        _columns = {
 
324
                'id': fields.integer('ID', readonly=True),
 
325
                'name' : fields.char('Reference',size=20,required=True),
 
326
                'type_id' : fields.many2one('cci_missions.dossier_type','Dossier Type',required=True),
 
327
                'date' : fields.date('Creation Date',required=True),
 
328
                'order_partner_id': fields.many2one('res.partner','Billed Customer',required=True),
 
329
                'asker_name':fields.char('Asker Name',size=50),
 
330
                'sender_name':fields.char('Sender Name',size=50),
 
331
                'to_bill':fields.boolean('To Be Billed'),
 
332
                'state':fields.selection([('draft','Confirmed'),('invoiced','Invoiced'),('cancel_customer','Canceled by Customer'),('cancel_cci','Canceled by the CCI')],'State',),
 
333
                'goods':fields.char('Goods Description',size=100),
 
334
                'goods_value':fields.float('Value of the Sold Goods'),#Monetary; must be greater than zero
 
335
                'destination_id':fields.many2one('res.country','Destination Country'),
 
336
                'embassy_folder_id':fields.many2one('cci_missions.embassy_folder','Related Embassy Folder'),
 
337
                'quantity_copies':fields.integer('Number of Copies'),
 
338
                'quantity_original' : fields.integer('Quantity of Originals',required=True),
 
339
                'sub_total':fields.function(_amount_subtotal, method=True, string='Sub Total for Extra Products', store=True),
 
340
                'text_on_invoice':fields.text('Text to Display on the Invoice'),
 
341
                'product_ids': fields.one2many('product.lines', 'dossier_product_line_id', 'Products'),
 
342
                'invoice_id':fields.many2one("account.invoice","Invoice"),
 
343
                'invoiced_amount': fields.float('Total'),
 
344
        }
 
345
 
 
346
        _defaults = {
 
347
                'name': lambda *args: '/',
 
348
                'date': lambda *a: time.strftime('%Y-%m-%d'),
 
349
                'to_bill' : lambda *b : True,
 
350
                'state' : lambda *a : 'draft',
 
351
                'quantity_original' : lambda *a : 1
 
352
        }
 
353
 
 
354
cci_missions_dossier()
 
355
 
 
356
class cci_missions_custom_code(osv.osv):
 
357
        _name= 'cci_missions.custom_code'
 
358
        _desctiption = 'cci_missions.custom_code'
 
359
        _columns = {
 
360
                'name' : fields.char('Name',size=8,required=True),
 
361
                'meaning' : fields.char('Meaning',size=250,required=True),
 
362
                'official' : fields.boolean('Official Code'),
 
363
        }
 
364
 
 
365
        _defaults = {
 
366
                'official': lambda *a: False,
 
367
        }
 
368
 
 
369
cci_missions_custom_code()
 
370
 
 
371
class cci_missions_certificate(osv.osv):
 
372
        _name = 'cci_missions.certificate'
 
373
        _description = 'cci_missions.certificate'
 
374
        _inherits = {'cci_missions.dossier': 'dossier_id' }
 
375
 
 
376
        def _amount_total(self, cr, uid, ids, name, args, context=None):
 
377
                res ={}
 
378
                data_dosseir = self.browse(cr,uid,ids)
 
379
 
 
380
                for data in data_dosseir:
 
381
                        if data.state =='draft':
 
382
                                data_partner = self.pool.get('res.partner').browse(cr,uid,data.order_partner_id.id)
 
383
                                context.update({'partner_id':data_partner})
 
384
                                context.update({'force_member':False})
 
385
                                context.update({'force_non_member':False})
 
386
                                context.update({'date':data.date})
 
387
                                context.update({'value_goods':data.goods_value})
 
388
                                context.update({'pricelist':data_partner.property_product_pricelist.id})
 
389
                                price_org = self.pool.get('product.product')._product_price(cr, uid, [data.type_id.original_product_id.id], False, False, context)
 
390
                                price_copy = self.pool.get('product.product')._product_price(cr, uid, [data.type_id.copy_product_id.id], False, False, context)
 
391
                                cost_org=price_org[data.type_id.original_product_id.id]
 
392
                                cost_copy=price_copy[data.type_id.copy_product_id.id]
 
393
                                qty_org = data.quantity_original
 
394
                                qty_copy = data.quantity_copies
 
395
                                subtotal =  data.sub_total
 
396
                                if qty_org < 0 or qty_copy < 0:
 
397
                                        raise osv.except_osv('Input Error!','No. of Copies and Quantity of Originals should be positive.')
 
398
                                total = ((cost_org * qty_org ) + (cost_copy * qty_copy) + subtotal)
 
399
                                res[data.id] = total
 
400
                        else :
 
401
                                res[data.id]=data.invoiced_amount
 
402
                return res
 
403
 
 
404
        def cci_dossier_cancel_cci(self, cr, uid, ids, *args):
 
405
                data=self.browse(cr,uid,ids[0])
 
406
                if data.invoice_id.state == 'paid':
 
407
                        new_ids = self.pool.get('account.invoice').refund(cr, uid,[data.invoice_id.id])
 
408
                        self.write(cr, uid,ids, {'invoice_id' : new_ids[0]})
 
409
                else:
 
410
                        wf_service = netsvc.LocalService('workflow')
 
411
                        wf_service.trg_validate(uid, 'account.invoice', data.invoice_id.id, 'invoice_cancel', cr)
 
412
                self.write(cr, uid, ids, {'state':'cancel_cci',})
 
413
                return True
 
414
 
 
415
        def get_certification_details(self, cr, uid, ids,order_partner_id):
 
416
                result={}
 
417
                asker_name=False
 
418
                sender_name=False
 
419
                asker_address=False
 
420
                zip=False
 
421
 
 
422
                if order_partner_id:
 
423
                        partner_info = self.pool.get('res.partner').browse(cr, uid,order_partner_id)
 
424
                        if partner_info.alert_legalisations:
 
425
                                raise osv.except_osv('Error!',partner_info.alert_explanation or 'Partner is not valid')
 
426
                        if not partner_info.asker_name:
 
427
                                asker_name=partner_info.name
 
428
                        else:
 
429
                                asker_name=partner_info.asker_name
 
430
                        if not partner_info.sender_name:
 
431
                                sender_name=partner_info.name
 
432
                        else:
 
433
                                sender_name=partner_info.sender_name
 
434
                        if not partner_info.asker_address:
 
435
                                if partner_info.address!=[]:
 
436
                                        for add in partner_info.address:
 
437
                                                if add.type=='default':
 
438
                                                        asker_address=add.street
 
439
                                                        break
 
440
                        else:
 
441
                                asker_address=partner_info.asker_address
 
442
                        if not partner_info.asker_zip_id.id:
 
443
                                if partner_info.address!=[]:
 
444
                                        for add in partner_info.address:
 
445
                                                if add.type=='default':
 
446
                                                        zip=add.zip_id.id
 
447
                                                        break
 
448
                        else:
 
449
                                zip=partner_info.asker_zip_id.id
 
450
 
 
451
                result = {'value': {
 
452
                        'asker_name': asker_name,
 
453
                        'asker_address': asker_address,
 
454
                        'asker_zip_id': zip,
 
455
                        'sender_name': sender_name}
 
456
                }
 
457
                return result
 
458
 
 
459
        def create(self, cr, uid, vals, *args, **kwargs):
 
460
#               Overwrite the name fields to set next sequence according to the sequence in the certification type (type_id)
 
461
                #vals['type_id']=self.pool.get('cci_missions.dossier_type').search(cr, uid, [('name','=','Certificate')])[0]
 
462
                if vals['type_id']:
 
463
                        data = self.pool.get('cci_missions.dossier_type').browse(cr, uid,vals['type_id'])
 
464
                        seq = self.pool.get('ir.sequence').get(cr, uid,data.sequence_id.code)
 
465
                        if seq:
 
466
                                vals.update({'name': seq})
 
467
                return super(osv.osv,self).create(cr, uid, vals, *args, **kwargs)
 
468
 
 
469
        _columns = {
 
470
                'dossier_id' : fields.many2one('cci_missions.dossier','Dossier'),
 
471
                'total':fields.function(_amount_total, method=True, string='Total', store=True),# sum of the price for copies, originals and extra_products
 
472
                'asker_address' : fields.char('Asker Address',size=50),#by default, res.partner->asker_adress or, res_partner.address[default]->street
 
473
                'asker_zip_id' : fields.many2one('res.partner.zip','Asker Zip Code'),#by default, res.partner->asker_zip_id or, res_partner.address[default]->zip_id
 
474
                'special_reason' : fields.selection([('none','None'),('Commercial Reason','Commercial Reason'),('Substitution','Substitution')],'For special cases'),
 
475
                'legalization_ids' : fields.one2many('cci_missions.legalization','certificate_id','Related Legalizations'),
 
476
                'customs_ids' : fields.many2many('cci_missions.custom_code','certificate_custome_code_rel','certificate_id','custom_id','Custom Codes'),
 
477
                'sending_SPF': fields.date('SPF Sending Date',help='Date of the sending of this record to the external database'),
 
478
                'origin_ids' : fields.many2many('res.country','certificate_country_rel','certificate_id','country_id','Origin Countries')
 
479
        }
 
480
 
 
481
        _defaults = {
 
482
                'special_reason': lambda *a: 'none',
 
483
        }
 
484
 
 
485
cci_missions_certificate()
 
486
 
 
487
class cci_missions_legalization(osv.osv):
 
488
        _name = 'cci_missions.legalization'
 
489
        _description = 'cci_missions.legalization'
 
490
        _inherits = {'cci_missions.dossier': 'dossier_id'}
 
491
 
 
492
        def _amount_total(self, cr, uid, ids, name, args, context=None):
 
493
                res ={}
 
494
                data_dosseir = self.browse(cr,uid,ids)
 
495
 
 
496
                for data in data_dosseir:
 
497
                        if data.state =='draft':
 
498
                                data_partner = self.pool.get('res.partner').browse(cr,uid,data.order_partner_id.id)
 
499
 
 
500
                                force_member=force_non_member=False
 
501
                                if data.member_price==1:
 
502
                                        force_member=True
 
503
                                else:
 
504
                                        force_non_member=True
 
505
                                context.update({'partner_id':data_partner})
 
506
                                context.update({'force_member':force_member})
 
507
                                context.update({'force_non_member':force_non_member})
 
508
                                context.update({'date':data.date})
 
509
                                context.update({'value_goods':data.goods_value})
 
510
                                context.update({'pricelist':data_partner.property_product_pricelist.id})
 
511
                                price_org = self.pool.get('product.product')._product_price(cr, uid, [data.type_id.original_product_id.id], False, False, context)
 
512
                                price_copy = self.pool.get('product.product')._product_price(cr, uid, [data.type_id.copy_product_id.id], False, False, context)
 
513
                                cost_org=price_org[data.type_id.original_product_id.id]
 
514
                                cost_copy=price_copy[data.type_id.copy_product_id.id]
 
515
                                qty_org = data.quantity_original
 
516
                                qty_copy = data.quantity_copies
 
517
                                subtotal =  data.sub_total
 
518
 
 
519
                                if qty_org < 0 or qty_copy < 0:
 
520
                                        raise osv.except_osv('Input Error!','No. of Copies and Quantity of Originals should be positive.')
 
521
                                total = ((cost_org * qty_org ) + (cost_copy * qty_copy) + subtotal)
 
522
                                res[data.id] = total
 
523
                        else :
 
524
                                res[data.id]=data.invoiced_amount
 
525
                return res
 
526
 
 
527
        def cci_dossier_cancel_cci(self, cr, uid, ids, *args):
 
528
                data=self.browse(cr,uid,ids[0])
 
529
                if data.invoice_id.state == 'paid':
 
530
                        new_ids = self.pool.get('account.invoice').refund(cr, uid,[data.invoice_id.id])
 
531
                        self.write(cr, uid,ids, {'invoice_id' : new_ids[0]})
 
532
                else:
 
533
                        wf_service = netsvc.LocalService('workflow')
 
534
                        wf_service.trg_validate(uid, 'account.invoice', data.invoice_id.id, 'invoice_cancel', cr)
 
535
                self.write(cr, uid, ids, {'state':'cancel_cci',})
 
536
                return True
 
537
 
 
538
        def get_legalization_details(self, cr, uid, ids,order_partner_id):
 
539
                result={}
 
540
                asker_name=False
 
541
                sender_name=False
 
542
                member_state=False
 
543
                if order_partner_id:
 
544
                        partner_info = self.pool.get('res.partner').browse(cr, uid,order_partner_id)
 
545
                        if partner_info.alert_legalisations:
 
546
                                raise osv.except_osv('Error!',partner_info.alert_explanation or 'Partner is not valid')
 
547
                        if partner_info.membership_state == 'none': #the boolean "Apply the member price" should be set to TRUE or FALSE when the partner is changed in regard of the membership state of him.
 
548
                                member_state = False
 
549
                        else:
 
550
                                member_state = True
 
551
                        if not partner_info.asker_name:
 
552
                                asker_name=partner_info.name
 
553
                        else:
 
554
                                asker_name=partner_info.asker_name
 
555
                        if not partner_info.sender_name:
 
556
                                sender_name=partner_info.name
 
557
                        else:
 
558
                                sender_name=partner_info.sender_name
 
559
                result = {'value': {
 
560
                        'asker_name': asker_name,
 
561
                        'sender_name': sender_name,
 
562
                        'member_price':member_state
 
563
                        }
 
564
                }
 
565
                return result
 
566
 
 
567
        def create(self, cr, uid, vals, *args, **kwargs):
 
568
#               Overwrite the name fields to set next sequence according to the sequence in the legalization type (type_id)
 
569
                #vals['type_id']=self.pool.get('cci_missions.dossier_type').search(cr, uid, [('name','=','Legalization')])[0]
 
570
                if vals['type_id']:
 
571
                        data = self.pool.get('cci_missions.dossier_type').browse(cr, uid,vals['type_id'])
 
572
                        seq = self.pool.get('ir.sequence').get(cr, uid,data.sequence_id.code)
 
573
                        if seq:
 
574
                                vals.update({'name': seq})
 
575
                return super(osv.osv,self).create(cr, uid, vals, *args, **kwargs)
 
576
 
 
577
        def _get_member_state(self, cr, uid, ids, name, args, context=None):
 
578
                res={}
 
579
                leg_ids = self.browse(cr,uid,ids)
 
580
                for p_id in leg_ids:
 
581
                        res[p_id.id]=p_id.dossier_id.order_partner_id.membership_state
 
582
                return res
 
583
 
 
584
        _columns = {
 
585
                'dossier_id' : fields.many2one('cci_missions.dossier','Dossier'),#added for inherits
 
586
                #'quantity_original' : fields.integer('Quantity of Originals',required=True),
 
587
                'total':fields.function(_amount_total, method=True, string='Total', store=True),# sum of the price for copies, originals and extra_products
 
588
                'certificate_id' : fields.many2one('cci_missions.certificate','Related Certificate'),
 
589
                'partner_member_state': fields.function(_get_member_state, method=True,selection=STATE,string='Member State of the Partner',readonly=True,type="selection"),
 
590
                'member_price' : fields.boolean('Apply the Member Price'),
 
591
        }
 
592
 
 
593
cci_missions_legalization()
 
594
 
 
595
class cci_missions_courier_log(osv.osv):
 
596
        _name = 'cci_missions.courier_log'
 
597
        _description = 'cci_missions.courier_log'
 
598
        _columns = {
 
599
                'embassy_folder_id' : fields.many2one('cci_missions.embassy_folder','Related Embassy Folder',required=True),
 
600
                'cba': fields.boolean('CBA'),
 
601
                'ministry' :  fields.boolean('Ministry'),
 
602
                'translation' : fields.boolean('Translation'),
 
603
                'embassy_name' : fields.char('Embassy Name',size=30),
 
604
                'consulate_name' : fields.char('Consulate Name',size=30),
 
605
                'others' : fields.char('Others',size=200),
 
606
                'copy_cba' : fields.boolean('Photocopy Before CBA'),
 
607
                'copy_ministry' : fields.boolean('Photocopy Before Ministry'),
 
608
                'copy_embassy_consulate' : fields.boolean('Photocopy Before Embassy or Consulate'),
 
609
                'documents' : fields.integer('Number of Documents to Legalize'),
 
610
                'documents_certificate' : fields.text('List of Certificates'),
 
611
                'documents_invoice' : fields.text('List of Invoices'),
 
612
                'documents_others' : fields.text('Others'),
 
613
                'message' : fields.text('Message to the Courier'),
 
614
                'return_address' : fields.selection([('A la CCI','A la CCI'),('Au clent','Au client')],'Address of Return',required=True),#onchange
 
615
                'address_name_1' : fields.char('Company Name',size=80),
 
616
                'address_name_2' : fields.char('Contact Name',size=80),
 
617
                'address_street' : fields.char('Street',size=80),
 
618
                'address_city' : fields.char('City',size=80),
 
619
                'qtty_to_print' : fields.integer('Number of Sheets'),
 
620
                'partner_address_id' : fields.many2one('res.partner.address','Courier'),
 
621
        }
 
622
 
 
623
cci_missions_courier_log()
 
624
 
 
625
class cci_missions_area(osv.osv):
 
626
        _name = 'cci_missions.area'
 
627
        _description = 'cci_missions.area'
 
628
        _columns = {
 
629
                'name' : fields.char('Description',size=50,required=True,transtale=True),
 
630
                'country_ids': fields.many2many('res.country','area_country_rel','area','country',"Countries"),
 
631
        }
 
632
 
 
633
cci_missions_area()
 
634
 
 
635
class cci_missions_ata_usage(osv.osv):
 
636
        _name = 'cci_missions.ata_usage'
 
637
        _description = 'cci_missions.ata_usage'
 
638
        _columns = {
 
639
                'name' : fields.char('Usage',size=80,required=True),
 
640
        }
 
641
 
 
642
cci_missions_ata_usage()
 
643
 
 
644
class cci_missions_ata_carnet(osv.osv):
 
645
        _name = 'cci_missions.ata_carnet'
 
646
        _description = 'cci_missions.ata_carnet'
 
647
 
 
648
        def create(self, cr, uid, vals, *args, **kwargs):
 
649
                context = {}
 
650
                data_partner=self.pool.get('res.partner').browse(cr,uid,vals['partner_id'])
 
651
                context.update({'pricelist':data_partner.property_product_pricelist.id})
 
652
 
 
653
                if 'creation_date' in vals:
 
654
                        context.update({'date':vals['creation_date']})
 
655
                        context.update({'emission_date':vals['creation_date']})
 
656
                if 'partner_id' in vals:
 
657
                        context.update({'partner_id':vals['partner_id']})
 
658
                if 'goods_value' in vals:
 
659
                        context.update({'value_goods':vals['goods_value']})
 
660
                if 'double_signature' in vals:
 
661
                        context.update({'double_signature':vals['double_signature']})
 
662
                force_member=force_non_member=False
 
663
                if 'member_price' in vals and vals['member_price']==1:
 
664
                        force_member=True
 
665
                else:
 
666
                        force_non_member=True
 
667
                context.update({'force_member':force_member})
 
668
                context.update({'force_non_member':force_non_member})
 
669
 
 
670
                data = self.pool.get('cci_missions.dossier_type').browse(cr, uid,vals['type_id'])
 
671
                if 'own_risk' in vals and vals['own_risk']:
 
672
                        warranty_product = data.warranty_product_1.id
 
673
                else:
 
674
                        warranty_product = data.warranty_product_2.id
 
675
 
 
676
                warranty= self.pool.get('product.product').price_get(cr,uid,[warranty_product],'list_price', context)[warranty_product]
 
677
                vals.update({'warranty_product_id' : warranty_product, 'warranty': warranty})
 
678
 
 
679
                seq = self.pool.get('ir.sequence').get(cr, uid,data.sequence_id.code)
 
680
                if seq:
 
681
                        vals.update({'name': seq})
 
682
                return super(osv.osv,self).create(cr, uid, vals, *args, **kwargs)
 
683
 
 
684
        def write(self, cr, uid, ids,vals, *args, **kwargs):
 
685
                #super(cci_missions_ata_carnet,self).write(cr, uid, ids,vals, *args, **kwargs)
 
686
                data_carnet = self.browse(cr,uid,ids[0])
 
687
                context = {}
 
688
                if 'creation_date' in vals:
 
689
                        context.update({'date':vals['creation_date']})
 
690
                        context.update({'emission_date':vals['creation_date']})
 
691
                else:
 
692
                        context.update({'date':data_carnet.creation_date})
 
693
                        context.update({'emission_date':data_carnet.creation_date})
 
694
 
 
695
                if 'partner_id' in vals:
 
696
                        context.update({'partner_id':vals['partner_id']})
 
697
                        data_partner=self.pool.get('res.partner').browse(cr,uid,vals['partner_id'])
 
698
                else:
 
699
                        context.update({'partner_id':data_carnet.partner_id.id})
 
700
                        data_partner=self.pool.get('res.partner').browse(cr,uid,data_carnet.partner_id.id)
 
701
 
 
702
                if 'goods_value' in vals:
 
703
                        context.update({'value_goods':vals['goods_value']})
 
704
                else:
 
705
                        context.update({'value_goods':data_carnet.goods_value})
 
706
                if 'double_signature' in vals:
 
707
                        context.update({'double_signature':vals['double_signature']})
 
708
                else:
 
709
                        context.update({'double_signature':data_carnet.double_signature})
 
710
                force_member=force_non_member=False
 
711
 
 
712
                context.update({'pricelist':data_partner.property_product_pricelist.id})
 
713
                if 'member_price' in vals:
 
714
                        if vals['member_price']==1:
 
715
                                force_member=True
 
716
                        else:
 
717
                                force_non_member=True
 
718
                else:
 
719
                        if data_carnet.member_price==1:
 
720
                                force_member=True
 
721
                        else:
 
722
                                force_non_member=True
 
723
                context.update({'force_member':force_member})
 
724
                context.update({'force_non_member':force_non_member})
 
725
 
 
726
                if 'own_risk' in vals:
 
727
                        if vals['own_risk']:
 
728
                                warranty_product = data_carnet.type_id.warranty_product_1.id
 
729
                        else:
 
730
                                warranty_product = data_carnet.type_id.warranty_product_2.id
 
731
                else:
 
732
                        if data_carnet.own_risk:
 
733
                                warranty_product = data_carnet.type_id.warranty_product_1.id
 
734
                        else:
 
735
                                warranty_product = data_carnet.type_id.warranty_product_2.id
 
736
                warranty= self.pool.get('product.product').price_get(cr,uid,[warranty_product],'list_price', context)[warranty_product]
 
737
 
 
738
                vals.update({'warranty_product_id' : warranty_product, 'warranty': warranty})
 
739
                super(cci_missions_ata_carnet,self).write(cr, uid, ids,vals, *args, **kwargs)
 
740
                return True
 
741
 
 
742
        def button_uncertain(self, cr, uid, ids, *args):
 
743
                self.write(cr, uid, ids, {'state':'pending',})
 
744
                return True
 
745
 
 
746
        def button_correct(self, cr, uid, ids, *args):
 
747
                self.write(cr, uid, ids, {'state':'correct','ok_state_date':time.strftime('%Y-%m-%d')})
 
748
                return True
 
749
 
 
750
        def button_dispute(self, cr, uid, ids, *args):
 
751
                self.write(cr, uid, ids, {'state':'dispute',})
 
752
                return True
 
753
 
 
754
        def button_closed(self, cr, uid, ids, *args):
 
755
                self.write(cr, uid, ids, {'state':'closed',})
 
756
                return True
 
757
 
 
758
        def cci_ata_created(self, cr, uid, ids):
 
759
                self.write(cr, uid, ids, {'state':'created','return_date':time.strftime('%Y-%m-%d')})
 
760
                return True
 
761
 
 
762
        def _get_insurer_id(self, cr, uid, ids, name, args, context=None):
 
763
                res={}
 
764
                partner_ids = self.browse(cr,uid,ids)
 
765
                for p_id in partner_ids:
 
766
                        res[p_id.id]=p_id.partner_id.insurer_id
 
767
                return res
 
768
 
 
769
        def onchange_type_carnet(self, cr, uid, ids,type_id,own_risk):
 
770
 
 
771
                data={'warranty_product_id' : False,'warranty':False}
 
772
                if not type_id:
 
773
                        return {'value':data}
 
774
                data_carnet_type = self.pool.get('cci_missions.dossier_type').browse(cr,uid,type_id)
 
775
                if own_risk:
 
776
                        warranty_prod=data_carnet_type.warranty_product_1.id
 
777
                else:
 
778
                        warranty_prod=data_carnet_type.warranty_product_2.id
 
779
                data['warranty_product_id']     =warranty_prod
 
780
                dict1=self.onchange_warranty_product_id(cr,uid,ids,warranty_prod)
 
781
                data.update(dict1['value'])
 
782
                return {'value':data}
 
783
 
 
784
        def onchange_own_risk(self,cr,uid,ids,type_id,own_risk):
 
785
 
 
786
                data={'warranty_product_id' : False,'warranty':False}
 
787
                if not type_id:
 
788
                        return {'value': data}
 
789
                warranty_prod = False
 
790
                data_carnet_type = self.pool.get('cci_missions.dossier_type').browse(cr,uid,type_id)
 
791
                if own_risk:
 
792
                        warranty_prod =data_carnet_type.warranty_product_1.id
 
793
                else:
 
794
                        warranty_prod = data_carnet_type.warranty_product_2.id
 
795
                data['warranty_product_id']     =warranty_prod
 
796
                dict1=self.onchange_warranty_product_id(cr,uid,ids,warranty_prod)
 
797
                data.update(dict1['value'])
 
798
 
 
799
                return {'value':data}
 
800
 
 
801
        def _get_member_state(self, cr, uid, ids, name, args, context=None):
 
802
                res={}
 
803
                partner_ids = self.browse(cr,uid,ids)
 
804
                for p_id in partner_ids:
 
805
                        res[p_id.id]=p_id.partner_id.membership_state
 
806
                return res
 
807
 
 
808
        def check_ata_carnet(self,cr, uid, ids):
 
809
                data_carnet=self.browse(cr, uid, ids)
 
810
                for data in data_carnet:
 
811
                        if (data.own_risk) or (data.insurer_agreement > 0 and data.partner_id.insurer_id > 0):
 
812
                                return True
 
813
                return False
 
814
 
 
815
        def _default_validity_date(self,cr,uid,context={}):
 
816
                creation_date=datetime.datetime.today()
 
817
                year=datetime.date(creation_date.year + 1,creation_date.month,creation_date.day)
 
818
                validity_date= year - timedelta(days=1)
 
819
                return validity_date.strftime('%Y-%m-%d')
 
820
 
 
821
        def _tot_products(self, cr, uid, ids, name, args, context=None):
 
822
                res={}
 
823
                carnet_ids = self.browse(cr,uid,ids)
 
824
                for p_id in carnet_ids:
 
825
                        sum=0.00
 
826
                        for line_id in p_id.product_ids:
 
827
                                sum += line_id.price_subtotal
 
828
                        res[p_id.id]=sum
 
829
                return res
 
830
 
 
831
        def onchange_partner_id(self,cr,uid,ids,partner_id):
 
832
                #the boolean "Apply the member price" should be set to TRUE or FALSE when the partner is changed in regard of the membership state of him.
 
833
                member_state = False
 
834
                if partner_id:
 
835
                        partner_info = self.pool.get('res.partner').browse(cr, uid,partner_id)
 
836
                        if partner_info.alert_legalisations:
 
837
                                raise osv.except_osv('Error!',partner_info.alert_explanation or 'Partner is not valid')
 
838
                        if partner_info.membership_state == 'none':
 
839
                                member_state = False
 
840
                        else:
 
841
                                member_state = True
 
842
                return {'value':{'member_price' : member_state}}
 
843
 
 
844
        def onchange_warranty_product_id(self,cr,uid,ids,prod_id):
 
845
                warranty_price= False
 
846
                if prod_id:
 
847
                        prod_info = self.pool.get('product.product').browse(cr, uid,prod_id)
 
848
                        warranty_price=prod_info.list_price
 
849
                return {'value':{'warranty' : warranty_price}}
 
850
 
 
851
        _columns = {
 
852
                'id': fields.integer('ID', readonly=True),
 
853
                'type_id' : fields.many2one('cci_missions.dossier_type','Related Type of Carnet',required=True),
 
854
                'creation_date' : fields.date('Emission Date',required=True),
 
855
                'validity_date' : fields.date('Validity Date',required=True),
 
856
                'partner_id': fields.many2one('res.partner','Partner',required=True),
 
857
                'holder_name' : fields.char('Holder Name',size=50),
 
858
                'holder_address' : fields.char('Holder Address',size=50),
 
859
                'holder_city' : fields.char('Holder City',size=50),
 
860
                'representer_name' : fields.char('Representer Name',size=50),
 
861
                'representer_address' : fields.char('Representer Address',size=50),
 
862
                'representer_city' : fields.char('Representer City',size=50),
 
863
                'usage_id': fields.many2one('cci_missions.ata_usage','Usage',required=True),
 
864
                'goods': fields.char('Goods',size=80),
 
865
                'area_id': fields.many2one('cci_missions.area','Area',required=True),
 
866
                'insurer_agreement' : fields.char('Insurer Agreement',size=50),
 
867
                'own_risk' : fields.boolean('Own Risks'),
 
868
                'goods_value': fields.float('Goods Value',required=True),
 
869
                'double_signature' : fields.boolean('Double Signature'),
 
870
                'initial_pages' : fields.integer('Initial Number of Pages',required=True),
 
871
                'additional_pages' : fields.integer('Additional Number of Pages'),
 
872
                'warranty':fields.float('Warranty',readonly=True),
 
873
                'warranty_product_id': fields.many2one('product.product','Related Warranty Product',required=True),
 
874
                'return_date' : fields.date('Date of Return'),
 
875
                'state':fields.selection([('draft','Draft'),('created','Created'),('pending','Pending'),('dispute','Dispute'),('correct','Correct'),('closed','Closed')],'State',required=True,readonly=True),
 
876
                'ok_state_date' : fields.date('Date of Closure'),
 
877
                'federation_sending_date' : fields.date('Date of Sending to the Federation', readonly=True),
 
878
                'name' : fields.char('Name',size=50,required=True),
 
879
                'partner_insurer_id': fields.function(_get_insurer_id, method=True,string='Insurer ID of the Partner',readonly=True),
 
880
                'partner_member_state': fields.function(_get_member_state, method=True,selection=STATE,string='Member State of the Partner',readonly=True,type="selection"),
 
881
                'member_price' : fields.boolean('Apply the Member Price'),
 
882
                'product_ids': fields.one2many('product.lines', 'product_line_id', 'Products'),
 
883
                'letter_ids':fields.one2many('cci_missions.letters_log','ata_carnet_id','Letters'),
 
884
                'sub_total': fields.function(_tot_products, method=True, string='Subtotal of Extra Products',type="float"),
 
885
                "invoice_id":fields.many2one("account.invoice","Invoice"),
 
886
        }
 
887
 
 
888
        _defaults = {
 
889
                'own_risk' : lambda *b : False,
 
890
                'double_signature' : lambda *b : False,
 
891
                'state' : lambda *a : 'draft',
 
892
                'validity_date' : _default_validity_date,
 
893
                'name': lambda *args: '/',
 
894
                'creation_date': lambda *a: time.strftime('%Y-%m-%d'),
 
895
        }
 
896
        _constraints = [(check_ata_carnet, 'Error: Please Select (Own Risk) OR ("Insurer Agreement" and "Parnters Insure id" should be greater than Zero)', ['own_risk','insurer_agreement','partner_insurer_id'])]
 
897
 
 
898
cci_missions_ata_carnet()
 
899
 
 
900
class cci_missions_letters_log(osv.osv):
 
901
        _name = 'cci_missions.letters_log'
 
902
        _description = 'cci_missions.letters_log'
 
903
        _rec_name = 'date'
 
904
        _columns = {
 
905
                'ata_carnet_id' : fields.many2one('cci_missions.ata_carnet','Related ATA Carnet',required=True),
 
906
                'letter_type' :  fields.selection([('Rappel avant echeance','Rappel avant echeance'),('Rappel apres echeance','Rappel apres echeance'),('Suite lettre A','Suite lettre A'),('Suite lettre C','Suite lettre C'),('Suite lettre C1','Suite lettre C1'),('Suite lettre I','Suite lettre I'),('Demande de remboursement','Demande de remboursement'),('Rappel a remboursement','Rappel a remboursement'),('Mise en demeure','Mise en demeure')],'Type of Letter',required=True),
 
907
                'date' : fields.date('Date of Sending',required=True),
 
908
        }
 
909
        _defaults = {
 
910
                'date': lambda *args: time.strftime('%Y-%m-%d')
 
911
        }
 
912
 
 
913
cci_missions_letters_log()
 
914
 
 
915
class product_lines(osv.osv):
 
916
        _name = "product.lines"
 
917
        _description = "Product Lines"
 
918
 
 
919
        def create(self, cr, uid, vals, *args, **kwargs):
 
920
                if vals['product_id']:
 
921
                        accnt_dict = {}
 
922
                        data_product = self.pool.get('product.product').browse(cr,uid,vals['product_id'])
 
923
                        a =  data_product.product_tmpl_id.property_account_income.id
 
924
                        if not a:
 
925
                                a = data_product.categ_id.property_account_income_categ.id
 
926
                        accnt_dict['account_id']=a
 
927
                        vals.update(accnt_dict)
 
928
                return super(product_lines,self).create(cr, uid, vals, *args, **kwargs)
 
929
 
 
930
        def write(self, cr, uid, ids,vals, *args, **kwargs):
 
931
                data_product_line= self.pool.get('product.lines').browse(cr,uid,ids[0])
 
932
                if (not data_product_line.product_id.id == vals['product_id']):
 
933
                        accnt_dict = {}
 
934
                        data_product = self.pool.get('product.product').browse(cr,uid,vals['product_id'])
 
935
                        a =  data_product.product_tmpl_id.property_account_income.id
 
936
                        if not a:
 
937
                                a = data_product.categ_id.property_account_income_categ.id
 
938
                        accnt_dict['account_id']=a
 
939
                        vals.update(accnt_dict)
 
940
                return super(product_lines,self).write( cr, uid, ids,vals, *args, **kwargs)
 
941
 
 
942
        def _product_subtotal(self, cr, uid, ids, name, args, context=None):
 
943
                res = {}
 
944
                for line in self.browse(cr, uid, ids):
 
945
                        res[line.id] = round(line.price_unit * line.quantity)
 
946
                return res
 
947
 
 
948
        def product_id_change(self, cr, uid, ids,product_id,):
 
949
                price_unit=uos_id=prod_name=data_partner=False
 
950
                if product_id:
 
951
                        data_product = self.pool.get('product.product').browse(cr,uid,product_id)
 
952
                        uos_id=data_product.uom_id.id
 
953
                        price=self.pool.get('product.product').price_get(cr,uid,[product_id])
 
954
                        price_unit=price[product_id]
 
955
                        prod_name=data_product.name
 
956
                return {'value': {
 
957
                        'uos_id': uos_id,
 
958
                        'price_unit': price_unit,
 
959
                        'name':prod_name,
 
960
                        }
 
961
                }
 
962
 
 
963
        _columns = {
 
964
                'name': fields.char('Description', size=256, required=True),
 
965
                'product_line_id': fields.many2one('cci_missions.ata_carnet', 'Product Ref',select=True),
 
966
                'dossier_product_line_id': fields.many2one('cci_missions.dossier', 'Product Ref',select=True),
 
967
                'uos_id': fields.many2one('product.uom', 'Unit', ondelete='set null'),
 
968
                'product_id': fields.many2one('product.product', 'Product', ondelete='set null',required=True),
 
969
                'price_unit': fields.float('Unit Price', required=True, digits=(16,2)),
 
970
                'price_subtotal': fields.function(_product_subtotal, method=True, string='Subtotal'),
 
971
                'quantity': fields.float('Quantity', required=True),
 
972
                'account_id' : fields.many2one('account.account', 'Account', required=True),
 
973
        }
 
974
        _defaults = {
 
975
                'quantity': lambda *a: 1,
 
976
        }
 
977
product_lines()
 
978
 
 
979
 
 
980
class Product(osv.osv):
 
981
        '''Product'''
 
982
        _inherit = 'product.product'
 
983
 
 
984
        #this function will have to be corrected in order to match the criteria grid of the CCI
 
985
        def price_get(self, cr, uid, ids, ptype='list_price',context={}):
 
986
                #get the price from the pricelist
 
987
                res = super(Product, self).price_get(cr, uid, ids, ptype, context)
 
988
                for product in self.browse(cr, uid, ids, context=context):
 
989
 
 
990
                        #change the price only for ATA originals
 
991
                        if product.name.find('ATA - original') != -1:
 
992
                                if context and ('value_goods' in context):
 
993
                                        if context['value_goods'] < 25000:
 
994
                                                res[product.id] = res[product.id] + context['value_goods']*0.008903875
 
995
                                        elif 25000 <= context['value_goods'] < 75000 :
 
996
                                                res[product.id] = res[product.id] + context['value_goods']*0.006937375
 
997
                                        elif 75000 <= context['value_goods'] < 250000 :
 
998
                                                res[product.id] = res[product.id] + context['value_goods']*0.004446475
 
999
                                        else:
 
1000
                                                res[product.id] = res[product.id] + context['value_goods']*0.002764025
 
1001
                                if context and ('double_signature' in context):
 
1002
                                        if context['double_signature'] == False:
 
1003
                                                res[product.id] = res[product.id] + 5.45
 
1004
 
 
1005
                        #change the price only for warranty own risk on ATA carnet
 
1006
                        if product.name.find('ATA - Own Risk Warranty') != -1:
 
1007
                                if context and ('value_goods' in context):
 
1008
                                        if context['value_goods'] > 15000:
 
1009
                                                res[product.id] = round(context['value_goods']*0.03)
 
1010
 
 
1011
                return res
 
1012
 
 
1013
Product()
 
1014