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

« back to all changes in this revision

Viewing changes to dm/campaign.py

  • Committer: Fabien Pinckaers
  • Date: 2008-09-24 19:47:55 UTC
  • mfrom: (3288.1.9 trunk-extra-addons)
  • Revision ID: fp@tinyerp.com-20080924194755-a15pg8qpaxmnhvx2
merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 
11
11
class dm_campaign_group(osv.osv):
12
12
    _name = "dm.campaign.group"
 
13
 
 
14
    def po_generate(self,cr, uid, ids, *args):
 
15
        plines = self.browse(cr, uid ,ids)
 
16
        if not plines:
 
17
            raise  osv.except_osv('Warning', "There's no purchase lines defined for this campaign")
 
18
        for pline in plines:
 
19
            if pline.state == 'pending':
 
20
                if not pline.product_id.seller_ids:
 
21
                    raise  osv.except_osv('Warning', "There's no supplier defined for this product : %s" % (pline.product_id.name,) )
 
22
 
 
23
                # Create a po / supplier
 
24
                for supplier in pline.product_id.seller_ids:
 
25
                    partner_id = supplier.id
 
26
                    partner = supplier.name
 
27
 
 
28
                    address_id = self.pool.get('res.partner').address_get(cr, uid, [partner.id], ['default'])['default']
 
29
                    if not address_id:
 
30
                        raise osv.except_osv('Warning', "There's no delivery address defined for this partner : %s" % (partner.name,) )
 
31
                    pricelist_id = partner.property_product_pricelist_purchase.id
 
32
                    if not pricelist_id:
 
33
                        raise osv.except_osv('Warning', "There's no purchase pricelist defined for this partner : %s" % (partner.name,) )
 
34
                    price = self.pool.get('product.pricelist').price_get(cr, uid, [pricelist_id], pline.product_id.id, pline.quantity, False, {'uom': pline.uom_id.id})[pricelist_id]
 
35
                    print "pline.date_planned : ",pline.date_planned
 
36
                    newdate = DateTime.strptime(pline.date_planned, '%Y-%m-%d %H:%M:%S') - DateTime.RelativeDateTime(days=pline.product_id.product_tmpl_id.seller_delay or 0.0)
 
37
                    print "newdate :",newdate.strftime('%Y-%m-%d %H:%M:%S')
 
38
 
 
39
                    if not pline.campaign_id.offer_id:
 
40
                        raise osv.except_osv('Warning', "There's no offer defined for this campaign : %s" % (pline.campaign_id.name,) )
 
41
                    if not pline.campaign_id.proposition_ids:
 
42
                        raise osv.except_osv('Warning', "There's no proposition defined for this campaign : %s" % (pline.campaign_id.name,) )
 
43
 
 
44
                    # Get constraints
 
45
                    constraints = []
 
46
                    if pline.type == 'manufacturing':
 
47
                        for step in pline.campaign_id.offer_id.step_ids:
 
48
                            for const in step.manufacturing_constraint_ids:
 
49
                                if pline.campaign_id.country_id in const.country_ids or not const.country_ids:
 
50
                                    constraints.append("---------------------------------------------------------------------------")
 
51
                                    constraints.append(const.name)
 
52
                                    constraints.append(const.constraint)
 
53
                    elif pline.type == 'items':
 
54
                        raise osv.except_osv('Warning', "Purchase of items is not yet implemented")
 
55
                        for step in pline.campaign_id.offer_id.step_ids:
 
56
                            for item in step.product_ids:
 
57
                                constraints.append("---------------------------------------------------------------------------")
 
58
                                constraints.append(item.product_id.name)
 
59
                                constraints.append(item.purchase_constraints)
 
60
                    elif pline.type == 'customer_file':
 
61
                        raise osv.except_osv('Warning', "Purchase of customers files is not yet implemented")
 
62
                    elif pline.type == 'translation':
 
63
                        if not pline.campaign_id.lang_id:
 
64
                            raise osv.except_osv('Warning', "There's no language defined for this campaign : %s" % (pline.campaign_id.name,) )
 
65
                        if pline.notes:
 
66
                            constraints.append(pline.notes)
 
67
                        else:
 
68
                            constraints.append(' ')
 
69
                    elif pline.type == False:
 
70
                        if pline.notes:
 
71
                            constraints.append(pline.notes)
 
72
                        else:
 
73
                            constraints.append(' ')
 
74
 
 
75
                    # Create po
 
76
                    purchase_id = self.pool.get('purchase.order').create(cr, uid, {
 
77
                        'origin': pline.campaign_id.name,
 
78
                        'partner_id': partner.id,
 
79
                        'partner_address_id': address_id,
 
80
                        'location_id': 1,
 
81
                        'pricelist_id': pricelist_id,
 
82
                        'notes': "\n".join(constraints),
 
83
                        'dm_campaign_purchase_line': pline.id
 
84
                    })
 
85
 
 
86
                    ''' If Translation Order => Get Number of documents in Offer '''
 
87
                    if pline.type == 'translation':
 
88
                        if pline.quantity == 0:
 
89
                            raise osv.except_osv('Warning',
 
90
                                "There's no documents for this offer: %s" % (pline.campaign_id.offer_id.name,) )
 
91
                        line = self.pool.get('purchase.order.line').create(cr, uid, {
 
92
                           'order_id': purchase_id,
 
93
                           'name': pline.campaign_id.name,
 
94
                           'product_qty': pline.quantity,
 
95
                           'product_id': pline.product_id.id,
 
96
                           'product_uom': pline.uom_id.id,
 
97
                           'price_unit': price,
 
98
                           'date_planned': newdate.strftime('%Y-%m-%d %H:%M:%S'),
 
99
                           'taxes_id': [(6, 0, [x.id for x in pline.product_id.product_tmpl_id.supplier_taxes_id])],
 
100
                           'account_analytic_id': pline.campaign_id.analytic_account_id,
 
101
                        })
 
102
                    else:
 
103
                        ''' Create po lines for each proposition'''
 
104
                        lines = []
 
105
                        for propo in pline.campaign_id.proposition_ids:
 
106
                            quantity = 0
 
107
                            if pline.type == 'customer_file' or pline.type == 'manufacturing' or pline.type == 'items':
 
108
                                if not propo.segment_ids:
 
109
                                    raise osv.except_osv('Warning',
 
110
                                        "There's no segment defined for this commercial proposition: %s" % (propo.name,) )
 
111
                                for segment in propo.segment_ids:
 
112
                                    if segment.quantity_ordered:
 
113
                                        if segment.quantity_ordered != 'AAA':
 
114
                                            quantity += int(segment.quantity_ordered)
 
115
                                        else:
 
116
                                            quantity = 999999
 
117
                                            break
 
118
                            """
 
119
                            elif pline.type == 'manufacturing' or pline.type == 'items':
 
120
                                if not propo.segment_ids:
 
121
                                    raise osv.except_osv('Warning',
 
122
                                        "There's no segment defined for this commercial proposition: %s" % (propo.name,) )
 
123
                                for segment in propo.segment_ids:
 
124
                                    if not segment.quantity_real:
 
125
                                        raise osv.except_osv('Warning',
 
126
                                            "There's no Ordered Quantity defined for the segment %s in proposition %s" % (segment.name,propo.name,) )
 
127
                                    quantity += int(segment.quantity_real)
 
128
                            """
 
129
                            print "DEBUG - Segment Quantity : ",quantity
 
130
 
 
131
                            line = self.pool.get('purchase.order.line').create(cr, uid, {
 
132
                               'order_id': purchase_id,
 
133
                               'name': propo.name,
 
134
                               'product_qty': quantity,
 
135
                               'product_id': pline.product_id.id,
 
136
                               'product_uom': pline.uom_id.id,
 
137
                               'price_unit': price,
 
138
                               'date_planned': newdate.strftime('%Y-%m-%d %H:%M:%S'),
 
139
                               'taxes_id': [(6, 0, [x.id for x in pline.product_id.product_tmpl_id.supplier_taxes_id])],
 
140
                               'account_analytic_id': propo.analytic_account_id.id,
 
141
#                       'move_dest_id': res_id,
 
142
                            })
 
143
#                    self.write(cr, uid, [pline.id], {'state':'requested'})
 
144
 
 
145
                    '''
 
146
                    # Set campaign supervision states
 
147
                    if pline.type == 'translation':
 
148
                        pline.campaign_id.write({'translation_state':'inprogress'})
 
149
                    elif pline.type == 'manufacturing':
 
150
                        pline.campaign_id.write({'manufacturing_state':'inprogress'})
 
151
                    elif pline.type == 'items':
 
152
                        pline.campaign_id.write({'items_state':'inprogress'})
 
153
                    elif pline.type == 'customer_file':
 
154
                        pline.campaign_id.write({'customer_file_state':'inprogress'})
 
155
                    '''
 
156
        return True
 
157
 
13
158
    _columns = {
14
159
        'name': fields.char('Campaign group name', size=64, required=True),
 
160
        'code': fields.char('Code', size=64, required=True),
15
161
        'project_id' : fields.many2one('project.project', 'Project', readonly=True),
16
162
        'campaign_ids': fields.one2many('dm.campaign', 'campaign_group_id', 'Campaigns', domain=[('campaign_group_id','=',False)]),
17
163
    }
36
182
        result ={}
37
183
        for id in ids:
38
184
 
39
 
            overlay = self.browse(cr,uid,[id])[0]
40
 
            trademark_code = overlay.trademark_id.name or ''
 
185
            overlay = self.browse(cr,uid,id)
 
186
            trademark_code = overlay.trademark_id.code or ''
41
187
            dealer_code = overlay.dealer_id.ref or ''
42
 
            country_code = overlay.country_id.code or ''
43
 
 
44
 
            code1='-'.join([trademark_code, dealer_code, country_code])
45
 
            result[id]=code1
 
188
        code1='-'.join([trademark_code, dealer_code])
 
189
        result[id]=code1
46
190
        return result
47
191
 
48
192
    _columns = {
49
 
        'code' : fields.function(_overlay_code,string='Code',type="char",method=True,readonly=True),
 
193
        'code' : fields.function(_overlay_code,string='Code',type='char',method=True,readonly=True),
50
194
        'trademark_id' : fields.many2one('dm.trademark', 'Trademark', required=True),
51
195
        'dealer_id' : fields.many2one('res.partner', 'Dealer',domain=[('category_id','ilike','Dealer')], context={'category':'Dealer'}, required=True),
52
 
        'country_id' : fields.many2one('res.country', 'Country', required=True),
 
196
        'country_ids' : fields.many2many('res.country', 'overlay_country_rel', 'overlay_id', 'country_id', 'Country', required=True),
53
197
        'bank_account_id' : fields.many2one('account.account', 'Account'),
54
198
    }
55
199
dm_overlay()
86
230
        return result
87
231
 
88
232
    def po_check(self,cr, uid, ids, *args):
 
233
        result = {}
89
234
        for pline in self.browse(cr, uid, ids)[0].purchase_line_ids:
90
235
            if pline.state == 'requested':
91
236
                for po in pline.purchase_order_ids:
92
237
                    if po.state == 'confirmed' or po.state == 'approved':
93
 
                        pline.write({'state':'ordered'})
 
238
                        result[po.id]='ordered'
 
239
#                        pline.write({'state':'ordered'})
94
240
            if pline.state == 'ordered':
95
241
                for po in pline.purchase_order_ids:
96
242
                    if po.shipped :
97
 
                        pline.write({'state':'delivered','date_delivery':time.strftime('%Y-%m-%d %H:%M:%S')})
 
243
                        result[po.id]='delivered'
 
244
#                        pline.write({'state':'delivered','date_delivery':time.strftime('%Y-%m-%d %H:%M:%S')})
 
245
                        pline.write({'date_delivery':time.strftime('%Y-%m-%d %H:%M:%S')})
98
246
                        if pline.type == 'translation':
99
247
                            trans_id = self.pool.get('dm.offer.translation').create(cr, uid,
100
248
                                {'offer_id': pline.campaign_id.offer_id.id,
103
251
                                 'translator_id':  po.partner_id.id,
104
252
                                 'notes': pline.notes,
105
253
                                 })
106
 
 
 
254
#       return result
107
255
        return True
108
256
 
109
257
    def _get_campaign_type(self,cr,uid,context={}):
153
301
        'manufacturing_product': fields.many2one('product.product','Manufacturing Product'),
154
302
        'purchase_line_ids': fields.one2many('dm.campaign.purchase_line', 'campaign_id', 'Purchase Lines'),
155
303
        'dtp_dates_ids': fields.one2many('dm.campaign.dtp_dates','campaign_id','DTP Dates'),
156
 
        'overlay_id': fields.many2one('dm.overlay', 'Overlay', ondelete='cascade'),
 
304
        'overlay_id': fields.many2one('dm.overlay', 'Overlay'),
157
305
    }
158
306
 
159
307
    _defaults = {
209
357
            """ In campaign, if no trademark is given, it gets the 'recommended trademark' from offer """
210
358
            if not camp.trademark_id:
211
359
                super(osv.osv, self).write(cr, uid, camp.id, {'trademark_id':offers.recommended_trademark.id})
212
 
        
 
360
 
213
361
        # check if an overlay exists else create it
214
 
        overlay = self.pool.get('dm.overlay').search(cr, uid, [('trademark_id','=',camp.trademark_id.id), ('dealer_id','=',camp.dealer_id.id), ('country_id','=',camp.country_id.id)])
215
 
        if overlay:
216
 
            super(osv.osv, self).write(cr, uid, camp.id, {'overlay_id':overlay[0]}, context)  
 
362
        overlay_country_ids=[]
 
363
        camp1 = self.browse(cr, uid, camp.id)
 
364
        if camp1.trademark_id and camp1.dealer_id and camp1.country_id:
 
365
            overlay = self.pool.get('dm.overlay').search(cr, uid, [('trademark_id','=',camp1.trademark_id.id), ('dealer_id','=',camp1.dealer_id.id)])
 
366
            
 
367
            for o_id in overlay:
 
368
                browse_overlay = self.pool.get('dm.overlay').browse(cr, uid, o_id)
 
369
                overlay_country_ids = [country_ids.id for country_ids in browse_overlay.country_ids]
 
370
            
 
371
            if overlay and (camp1.country_id.id in overlay_country_ids):
 
372
                super(osv.osv, self).write(cr, uid, camp1.id, {'overlay_id':overlay[0]}, context)  
 
373
            elif overlay and not (camp1.country_id.id in overlay_country_ids):
 
374
                overlay_country_ids.append(camp1.country_id.id)
 
375
                self.pool.get('dm.overlay').write(cr, uid, browse_overlay.id, {'country_ids':[[6,0,overlay_country_ids]]}, context)
 
376
                super(osv.osv, self).write(cr, uid, camp1.id, {'overlay_id':overlay[0]}, context)
 
377
            else:
 
378
                overlay_country_ids.append(camp1.country_id.id)
 
379
                overlay_ids1 = self.pool.get('dm.overlay').create(cr, uid, {'trademark_id':camp1.trademark_id.id, 'dealer_id':camp1.dealer_id.id, 'country_ids':[[6,0,overlay_country_ids]]}, context)
 
380
                super(osv.osv, self).write(cr, uid, camp1.id, {'overlay_id':overlay_ids1}, context)
217
381
        else:
218
 
            overlay_ids1 = self.pool.get('dm.overlay').create(cr, uid, {'trademark_id':camp.trademark_id.id, 'dealer_id':camp.dealer_id.id, 'country_id':camp.country_id.id}, context)
219
 
            super(osv.osv, self).write(cr, uid, camp.id, {'overlay_id':overlay_ids1}, context)
220
 
        
 
382
            super(osv.osv, self).write(cr, uid, camp1.id, {'overlay_id':0}, context)
 
383
 
221
384
        return res
222
385
 
223
386
    def create(self,cr,uid,vals,context={}):
251
414
        
252
415
        # check if an overlay exists else create it
253
416
        data_cam1 = self.browse(cr, uid, id_camp)
254
 
        overlay = self.pool.get('dm.overlay').search(cr, uid, [('trademark_id','=',data_cam1.trademark_id.id), ('dealer_id','=',data_cam1.dealer_id.id), ('country_id','=',data_cam1.country_id.id)])
255
 
        if overlay:
256
 
            super(osv.osv, self).write(cr, uid, data_cam1.id, {'overlay_id':overlay[0]}, context)  
257
 
        else:
258
 
            overlay_ids1 = self.pool.get('dm.overlay').create(cr, uid, {'trademark_id':data_cam1.trademark_id.id, 'dealer_id':data_cam1.dealer_id.id, 'country_id':data_cam1.country_id.id}, context)
259
 
            super(osv.osv, self).write(cr, uid, data_cam1.id, {'overlay_id':overlay_ids1}, context)
 
417
        overlay_country_ids = []
 
418
        if data_cam1.trademark_id and data_cam1.dealer_id and data_cam1.country_id:
 
419
            overlay = self.pool.get('dm.overlay').search(cr, uid, [('trademark_id','=',data_cam1.trademark_id.id), ('dealer_id','=',data_cam1.dealer_id.id)])
 
420
            for o_id in overlay:
 
421
                browse_overlay = self.pool.get('dm.overlay').browse(cr, uid, o_id)
 
422
                overlay_country_ids = [country_ids.id for country_ids in browse_overlay.country_ids]
 
423
            if overlay and (data_cam1.country_id.id in overlay_country_ids):
 
424
                super(osv.osv, self).write(cr, uid, data_cam1.id, {'overlay_id':overlay[0]}, context)  
 
425
            elif overlay and not (data_cam1.country_id.id in overlay_country_ids):
 
426
                overlay_country_ids.append(data_cam1.country_id.id)
 
427
                self.pool.get('dm.overlay').write(cr, uid, browse_overlay.id, {'country_ids':[[6,0,overlay_country_ids]]}, context)
 
428
                super(osv.osv, self).write(cr, uid, data_cam1.id, {'overlay_id':overlay[0]}, context)
 
429
            else:
 
430
                overlay_country_ids.append(data_cam1.country_id.id)
 
431
                overlay_ids1 = self.pool.get('dm.overlay').create(cr, uid, {'trademark_id':data_cam1.trademark_id.id, 'dealer_id':data_cam1.dealer_id.id, 'country_ids':[[6,0,overlay_country_ids]]}, context)
 
432
                super(osv.osv, self).write(cr, uid, data_cam1.id, {'overlay_id':overlay_ids1}, context)
260
433
 
261
434
        return id_camp
262
435
 
577
750
        if not plines:
578
751
            raise  osv.except_osv('Warning', "There's no purchase lines defined for this campaign")
579
752
        for pline in plines:
580
 
            if pline.state != 'done':
 
753
            if pline.state == 'pending':
581
754
                if not pline.product_id.seller_ids:
582
755
                    raise  osv.except_osv('Warning', "There's no supplier defined for this product : %s" % (pline.product_id.name,) )
583
756
 
665
838
                        lines = []
666
839
                        for propo in pline.campaign_id.proposition_ids:
667
840
                            quantity = 0
668
 
                            ''' If Customer File Order => Get Ordered Quantity '''
669
 
                            ''' If Manufacturing or Items Order => Get Real Quantity '''
670
 
                            if pline.type == 'customer_file':
 
841
                            if pline.type == 'customer_file' or pline.type == 'manufacturing' or pline.type == 'items':
 
842
                                if not propo.segment_ids:
 
843
                                    raise osv.except_osv('Warning',
 
844
                                        "There's no segment defined for this commercial proposition: %s" % (propo.name,) )
671
845
                                for segment in propo.segment_ids:
672
846
                                    if segment.quantity_ordered:
673
847
                                        if segment.quantity_ordered != 'AAA':
675
849
                                        else:
676
850
                                            quantity = 999999
677
851
                                            break
 
852
                            """
678
853
                            elif pline.type == 'manufacturing' or pline.type == 'items':
679
854
                                if not propo.segment_ids:
680
855
                                    raise osv.except_osv('Warning',
684
859
                                        raise osv.except_osv('Warning',
685
860
                                            "There's no Ordered Quantity defined for the segment %s in proposition %s" % (segment.name,propo.name,) )
686
861
                                    quantity += int(segment.quantity_real)
 
862
                            """
687
863
                            print "DEBUG - Segment Quantity : ",quantity
688
864
 
689
865
                            line = self.pool.get('purchase.order.line').create(cr, uid, {
698
874
                               'account_analytic_id': propo.analytic_account_id.id,
699
875
#                       'move_dest_id': res_id,
700
876
                            })
701
 
                    self.write(cr, uid, [pline.id], {'state':'requested'})
 
877
#                    self.write(cr, uid, [pline.id], {'state':'requested'})
702
878
 
703
879
                    '''
704
880
                    # Set campaign supervision states
722
898
            return newdate.strftime('%Y-%m-%d %H:%M:%S')
723
899
        return []
724
900
 
 
901
 
 
902
    def _state_get(self, cr, uid, ids, name, args, context={}):
 
903
        result = {}
 
904
        for pline in self.browse(cr, uid, ids):
 
905
            print "DEBUG - pline : ", pline
 
906
            delivered=False
 
907
            ordered=False
 
908
            if pline.purchase_order_ids:
 
909
                print "DEBUG - po found"
 
910
                for po in pline.purchase_order_ids:
 
911
                    if delivered:
 
912
                        continue
 
913
                    if po.shipped:
 
914
                        result[pline.id]='delivered'
 
915
                        delivered=True
 
916
#                        self.write(cr, uid, [pline.id],{'date_delivery':time.strftime('%Y-%m-%d %H:%M:%S')})
 
917
                        if pline.type == 'translation':
 
918
                            trans_id = self.pool.get('dm.offer.translation').create(cr, uid,
 
919
                                {'offer_id': pline.campaign_id.offer_id.id,
 
920
                                 'date': pline.date_delivery,
 
921
                                 'language_id': pline.campaign_id.lang_id.id,
 
922
                                 'translator_id':  po.partner_id.id,
 
923
                                 'notes': pline.notes,
 
924
                                 })
 
925
                        continue
 
926
                    if ordered:
 
927
                        continue
 
928
                    if po.state == 'confirmed' or po.state == 'approved':
 
929
                        result[pline.id]='ordered'
 
930
                        ordered=True
 
931
                    if po.state == 'draft':
 
932
                        result[pline.id]='requested'
 
933
            else:
 
934
                result[pline.id] = 'pending'
 
935
            print "DEBUG - Result : ",result[pline.id]
 
936
        return result
 
937
 
725
938
    _columns = {
726
939
        'campaign_id': fields.many2one('dm.campaign', 'Campaign'),
727
940
        'product_id' : fields.many2one('product.product', 'Product', required=True, context={'flag':True}),
734
947
        'type' : fields.selection(PURCHASE_LINE_TYPES, 'Type'),
735
948
        'purchase_order_ids' : fields.one2many('purchase.order','dm_campaign_purchase_line','DM Campaign Purchase Line'),
736
949
        'notes': fields.text('Notes'),
737
 
        'state' : fields.selection(PURCHASE_LINE_STATES, 'State',readonly=True),
 
950
        #'state' : fields.selection(PURCHASE_LINE_STATES, 'State',readonly=True),
 
951
        'state' : fields.function(_state_get, method=True, type='selection', selection=[
 
952
            ('pending','Pending'),
 
953
            ('requested','Quotations Requested'),
 
954
            ('ordered','Ordered'),
 
955
            ('delivered','Delivered'),
 
956
            ], string='State', store=True, readonly=True),
738
957
    }
739
958
 
740
959
    _defaults = {