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

« back to all changes in this revision

Viewing changes to dm/campaign.py

  • Committer: Fabian Semal
  • Date: 2008-10-15 16:23:11 UTC
  • mto: (3288.4.2 account_india)
  • mto: This revision was merged to the branch mainline in revision 3294.
  • Revision ID: fabian@tinyerp.com-20081015162311-r4zgbkr6f3m9b2it
Added PO for Customes File
Added 'days' in transition delays
Fixed price progression bug

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
class dm_campaign_group(osv.osv):
14
14
    _name = "dm.campaign.group"
15
15
 
 
16
    def _quantity_estimated_total(self, cr, uid, ids, name, args, context={}):
 
17
        result={}
 
18
        groups = self.browse(cr,uid,ids)
 
19
        for group in groups:
 
20
            for campaign in group.campaign_ids:
 
21
                quantity=0
 
22
                numeric=True
 
23
                if campaign.quantity_estimated_total.isdigit():
 
24
                    quantity += int(campaign.quantity_estimated_total)
 
25
                else:
 
26
                    result[group.id]='Check Segments'
 
27
                    numeric=False
 
28
                    break
 
29
            if numeric:
 
30
                result[group.id]=str(quantity)
 
31
        print "Estimated : ",result
 
32
        return result
 
33
 
 
34
    def _quantity_wanted_total(self, cr, uid, ids, name, args, context={}):
 
35
        result={}
 
36
        groups = self.browse(cr,uid,ids)
 
37
        print "ids : ",ids
 
38
        for group in groups:
 
39
            for campaign in group.campaign_ids:
 
40
                quantity=0
 
41
                numeric=True
 
42
                print "wanted total : ", campaign.quantity_wanted_total
 
43
                if campaign.quantity_wanted_total.isdigit():
 
44
                    quantity += int(campaign.quantity_wanted_total)
 
45
                elif campaign.quantity_wanted_total == "AAA for a Segment":
 
46
                    result[group.id]='AAA for a Segment'
 
47
                    numeric=False
 
48
                    break
 
49
                else:
 
50
                    result[group.id]='Check Segments'
 
51
                    numeric=False
 
52
                    break
 
53
            if numeric:
 
54
                result[group.id]=str(quantity)
 
55
        print "Wanted : ",result
 
56
        return result
 
57
 
 
58
    def _quantity_delivered_total(self, cr, uid, ids, name, args, context={}):
 
59
        result={}
 
60
        groups = self.browse(cr,uid,ids)
 
61
        for group in groups:
 
62
            for campaign in group.campaign_ids:
 
63
                quantity=0
 
64
                numeric=True
 
65
                if campaign.quantity_delivered_total.isdigit():
 
66
                    quantity += int(campaign.quantity_delivered_total)
 
67
                else:
 
68
                    result[group.id]='Check Segments'
 
69
                    numeric=False
 
70
                    break
 
71
            if numeric:
 
72
                result[group.id]=str(quantity)
 
73
        print "Delivered : ",result
 
74
        return result
 
75
 
 
76
    def _quantity_usable_total(self, cr, uid, ids, name, args, context={}):
 
77
        result={}
 
78
        groups = self.browse(cr,uid,ids)
 
79
        for group in groups:
 
80
            for campaign in group.campaign_ids:
 
81
                quantity=0
 
82
                numeric=True
 
83
                if campaign.quantity_usable_total.isdigit():
 
84
                    quantity += int(campaign.quantity_usable_total)
 
85
                else:
 
86
                    result[group.id]='Check Segments'
 
87
                    numeric=False
 
88
                    break
 
89
            if numeric:
 
90
                result[group.id]=str(quantity)
 
91
        print "Usable : ",result
 
92
        return result
 
93
 
16
94
    def _camp_group_code(self, cr, uid, ids, name, args, context={}):
17
95
        result ={}
18
96
        offer_code = ''
19
97
        offer_name = ''
20
98
        for id in ids:
21
 
            
 
99
 
22
100
            dt = time.strftime('%Y-%m-%d')
23
101
            date = dt.split('-')
24
102
            year = month = ''
35
113
            code1='-'.join([final_date,offer_code, offer_name])
36
114
            result[id]=code1
37
115
        return result
38
 
    
39
 
    def po_generate(self,cr, uid, ids, *args):
40
 
 
41
 
 
42
 
 
43
 
 
44
 
 
45
 
 
46
 
 
47
 
 
48
 
        return True
49
116
 
50
117
    _columns = {
51
118
        'name': fields.char('Campaign group name', size=64, required=True),
52
119
        'code' : fields.function(_camp_group_code,string='Code',type='char',method=True,readonly=True),
53
120
        'project_id' : fields.many2one('project.project', 'Project', readonly=True),
54
 
        'campaign_ids': fields.one2many('dm.campaign', 'campaign_group_id', 'Campaigns', domain=[('campaign_group_id','=',False)]),
 
121
        'campaign_ids': fields.one2many('dm.campaign', 'campaign_group_id', 'Campaigns', domain=[('campaign_group_id','=',False)], readonly=True),
 
122
        'purchase_line_ids': fields.one2many('dm.campaign.purchase_line', 'campaign_group_id', 'Purchase Lines'),
 
123
        'quantity_estimated_total' : fields.function(_quantity_estimated_total, string='Total Estimated Quantity',type="char",size="64",method=True,readonly=True),
 
124
        'quantity_wanted_total' : fields.function(_quantity_wanted_total, string='Total Wanted Quantity',type="char",size="64",method=True,readonly=True),
 
125
        'quantity_delivered_total' : fields.function(_quantity_delivered_total, string='Total Delivered Quantity',type="char",size="64",method=True,readonly=True),
 
126
        'quantity_usable_total' : fields.function(_quantity_usable_total, string='Total Usable Quantity',type="char",size="64",method=True,readonly=True),
55
127
    }
56
128
dm_campaign_group()
57
129
 
79
151
            if not i in dealer_country_ids:
80
152
                raise  osv.except_osv('Warning', "This country is not allowed for %s" % (data.dealer_id.name,) )
81
153
        return id
82
 
    
 
154
 
83
155
    def _overlay_code(self, cr, uid, ids, name, args, context={}):
84
156
        result ={}
85
157
        for id in ids:
119
191
            elif name[0] == 'm':
120
192
                ids2 = obj.pool.get(self._obj).search(cr, user, [(self._fields_id,'in',project_ids),('type','=','Mailing Manufacturing')], limit=self._limit)
121
193
            elif name[0] == 'c':
122
 
                ids2 = obj.pool.get(self._obj).search(cr, user, [(self._fields_id,'in',project_ids),('type','=','Customer List')], limit=self._limit)
 
194
                ids2 = obj.pool.get(self._obj).search(cr, user, [(self._fields_id,'in',project_ids),('type','=','Customers File')], limit=self._limit)
123
195
            else :
124
196
                ids2 = obj.pool.get(self._obj).search(cr, user, [(self._fields_id,'in',project_ids),('type','=','Mailing Manufacturing')], limit=self._limit)
125
197
            for r in obj.pool.get(self._obj)._read_flat(cr, user, ids2, [self._fields_id], context=context, load='_classic_write'):
173
245
            value['currency_id']=0
174
246
        return {'value':value}
175
247
 
176
 
    def _quantity_theorical_total(self, cr, uid, ids, name, args, context={}):
 
248
    def _quantity_estimated_total(self, cr, uid, ids, name, args, context={}):
177
249
        result={}
178
250
        campaigns = self.browse(cr,uid,ids)
179
251
        for campaign in campaigns:
180
252
            quantity=0
181
253
            numeric=True
182
254
            for propo in campaign.proposition_ids:
183
 
                if propo.quantity_theorical.isdigit():
184
 
                    quantity += int(propo.quantity_theorical)
 
255
                if propo.quantity_estimated.isdigit():
 
256
                    quantity += int(propo.quantity_estimated)
185
257
                else:
186
258
                    result[campaign.id]='Check Segments'
187
259
                    numeric=False
228
300
                result[campaign.id]=str(quantity)
229
301
        return result
230
302
 
231
 
    def _quantity_real_total(self, cr, uid, ids, name, args, context={}):
 
303
    def _quantity_usable_total(self, cr, uid, ids, name, args, context={}):
232
304
        result={}
233
305
        campaigns = self.browse(cr,uid,ids)
234
306
        for campaign in campaigns:
235
307
            quantity=0
236
308
            numeric=True
237
309
            for propo in campaign.proposition_ids:
238
 
                if propo.quantity_real.isdigit():
239
 
                    quantity += int(propo.quantity_real)
 
310
                if propo.quantity_usable.isdigit():
 
311
                    quantity += int(propo.quantity_usable)
240
312
                else:
241
313
                    result[campaign.id]='Check Segments'
242
314
                    numeric=False
273
345
        'dtp_responsible_id' : fields.many2one('res.users','Responsible'),
274
346
        'files_responsible_id' : fields.many2one('res.users','Responsible'),
275
347
        'invoiced_partner_id' : fields.many2one('res.partner','Invoiced Partner'),
 
348
        'files_delivery_address_id' : fields.many2one('res.partner.address','Delivery Address'),
276
349
        'dtp_making_time' : fields.function(dtp_making_time_get, method=True, type='float', string='Making Time'),
277
350
        'deduplicator_id' : fields.many2one('res.partner','Deduplicator',domain=[('category_id','ilike','Deduplicator')], context={'category':'Deduplicator'},
278
351
            help="The deduplicator is a partner responsible to remove identical addresses from the customers list"),
289
362
                                                        domain=[('type','ilike','DTP')], context={'type':'DTP'}),
290
363
        'manufacturing_task_ids': one2many_mod_task('project.task', 'project_id', "Manufacturing tasks",
291
364
                                                        domain=[('type','ilike','Mailing Manufacturing')],context={'type':'Mailing Manufacturing'}),
292
 
        'cust_file_task_ids': one2many_mod_task('project.task', 'project_id', "Customer Lists tasks",
293
 
                                                        domain=[('type','ilike','Customers List')], context={'type':'Customers List'}),
294
 
        'quantity_theorical_total' : fields.function(_quantity_theorical_total, string='Total Theorical Quantity',type="char",size="64",method=True,readonly=True),
 
365
        'cust_file_task_ids': one2many_mod_task('project.task', 'project_id', "Customer Files tasks",
 
366
                                                        domain=[('type','ilike','Customers File')], context={'type':'Customers File'}),
 
367
        'quantity_estimated_total' : fields.function(_quantity_estimated_total, string='Total Estimated Quantity',type="char",size="64",method=True,readonly=True),
295
368
        'quantity_wanted_total' : fields.function(_quantity_wanted_total, string='Total Wanted Quantity',type="char",size="64",method=True,readonly=True),
296
369
        'quantity_delivered_total' : fields.function(_quantity_delivered_total, string='Total Delivered Quantity',type="char",size="64",method=True,readonly=True),
297
 
        'quantity_real_total' : fields.function(_quantity_real_total, string='Total Real Quantity',type="char",size="64",method=True,readonly=True),
 
370
        'quantity_usable_total' : fields.function(_quantity_usable_total, string='Total Usable Quantity',type="char",size="64",method=True,readonly=True),
298
371
    }
299
372
 
300
373
    _defaults = {
543
616
                result[propo.id]=str(qty)
544
617
        return result
545
618
 
546
 
    def _quantity_theorical_get(self, cr, uid, ids, name, args, context={}):
 
619
    def _quantity_estimated_get(self, cr, uid, ids, name, args, context={}):
547
620
        result ={}
548
621
        for propo in self.browse(cr,uid,ids):
549
622
            if not propo.segment_ids:
551
624
                continue
552
625
            qty = 0
553
626
            for segment in propo.segment_ids:
554
 
                if segment.quantity_theorical == 0:
555
 
                    result[propo.id]='Theorical Quantity missing in a Segment'
 
627
                if segment.quantity_estimated == 0:
 
628
                    result[propo.id]='Estimated Quantity missing in a Segment'
556
629
                    continue
557
 
                qty += segment.quantity_theorical
 
630
                qty += segment.quantity_estimated
558
631
            result[propo.id]=str(qty)
559
632
        return result
560
633
 
573
646
            result[propo.id]=str(qty)
574
647
        return result
575
648
 
576
 
    def _quantity_real_get(self, cr, uid, ids, name, args, context={}):
 
649
    def _quantity_usable_get(self, cr, uid, ids, name, args, context={}):
577
650
        result={}
578
651
        for propo in self.browse(cr,uid,ids):
579
652
            if not propo.segment_ids:
581
654
                continue
582
655
            qty = 0
583
656
            for segment in propo.segment_ids:
584
 
                if segment.quantity_real == 0:
585
 
                    result[propo.id]='Real Quantity missing in a Segment'
 
657
                if segment.quantity_usable == 0:
 
658
                    result[propo.id]='Usable Quantity missing in a Segment'
586
659
                    continue
587
 
                qty += segment.quantity_real
 
660
                qty += segment.quantity_usable
588
661
            result[propo.id]=str(qty)
589
662
        return result
590
663
 
604
677
        'proposition_type' : fields.selection([('init','Initial'),('relaunching','Relauching'),('split','Split')],"Type"),
605
678
        'initial_proposition_id': fields.many2one('dm.campaign.proposition', 'Initial proposition', readonly=True),
606
679
        'segment_ids' : fields.one2many('dm.campaign.proposition.segment','proposition_id','Segment', ondelete='cascade'),
607
 
        'quantity_theorical' : fields.function(_quantity_theorical_get,string='Theorical Quantity',type="char",size="64",method=True,readonly=True,
608
 
                    help='The theorical quantity is an estimation of the real quantity of addresses you  will get after delivery, deduplication and cleaning\n' \
 
680
        'quantity_estimated' : fields.function(_quantity_estimated_get,string='Estimated Quantity',type="char",size="64",method=True,readonly=True,
 
681
                    help='The Estimated quantity is an estimation of the usable quantity of addresses you  will get after delivery, deduplication and cleaning\n' \
609
682
                            'This is usually the quantity used to order the manufacturing of the mailings'),
610
683
        'quantity_wanted' : fields.function(_quantity_wanted_get,string='Wanted Quantity',type="char",size="64",method=True,readonly=True,
611
684
                    help='The wanted quantity is the number of addresses you wish to get for that segment.\n' \
613
686
                            'The wanted quantity could be AAA for All Addresses Available'),
614
687
        'quantity_delivered' : fields.function(_quantity_delivered_get,string='Delivered Quantity',type="char",size="64",method=True,readonly=True,
615
688
                    help='The delivered quantity is the number of addresses you receive from the broker.'),
616
 
        'quantity_real' : fields.function(_quantity_real_get,string='Real Quantity',type="char",size="64",method=True,readonly=True,
617
 
                    help='The real quantity is the number of addresses you have after delivery, deduplication and cleaning.'),
 
689
        'quantity_usable' : fields.function(_quantity_usable_get,string='Usable Quantity',type="char",size="64",method=True,readonly=True,
 
690
                    help='The usable quantity is the number of addresses you have after delivery, deduplication and cleaning.'),
 
691
        'quantity_real': fields.integer('Real Quantity'),
618
692
        'starting_mail_price' : fields.float('Starting Mail Price',digits=(16,2)),
619
693
        'customer_pricelist_id':fields.many2one('product.pricelist','Items Pricelist', required=False),
620
694
        'forwarding_charges' : fields.float('Forwarding Charges', digits=(16,2)),
660
734
    _inherits = {'account.analytic.account': 'analytic_account_id'}
661
735
    _description = "Segment"
662
736
 
663
 
    def _quantity_real_get(self, cr, uid, ids, name, args, context={}):
 
737
    def _quantity_usable_get(self, cr, uid, ids, name, args, context={}):
664
738
        result ={}
665
739
        for segment in self.browse(cr,uid,ids):
666
 
            result[segment.id]=segment.quantity_delivered - segment.quantity_dedup - segment.quantity_cleaned
 
740
            result[segment.id]=segment.quantity_delivered - segment.quantity_dedup_dedup - segment.quantity_cleaned_dedup - segment.quantity_dedup_cleaner- segment.quantity_cleaned_cleaner
667
741
        return result
668
742
 
669
743
    def write(self, cr, uid, ids, vals, context=None):
703
777
        'code1' : fields.function(_segment_code,string='Code',type="char",size="64",method=True,readonly=True),
704
778
        'proposition_id' : fields.many2one('dm.campaign.proposition','Proposition', ondelete='cascade'),
705
779
        'list_id': fields.many2one('dm.customers_list','Customers List',required=True),
706
 
        'quantity_theorical' : fields.integer('Theorical Quantity'),
 
780
        'quantity_estimated' : fields.integer('Estimated Quantity'),
707
781
        'quantity_wanted' : fields.integer('Wanted Quantity'),
708
782
        'quantity_delivered' : fields.integer('Delivered Quantity'),
709
 
        'quantity_dedup' : fields.integer('Deduplication Quantity'),
710
 
        'quantity_cleaned' : fields.integer('Cleaned Quantity'),
711
 
        'quantity_real' : fields.function(_quantity_real_get,string='Real Quantity',type="integer",method=True,readonly=True),
 
783
        'quantity_dedup_dedup' : fields.integer('Deduplication Quantity'),
 
784
        'quantity_dedup_cleaner' : fields.integer('Deduplication Quantity'),
 
785
        'quantity_cleaned_dedup' : fields.integer('Cleaned Quantity'),
 
786
        'quantity_cleaned_cleaner' : fields.integer('Cleaned Quantity'),
 
787
        'quantity_usable' : fields.function(_quantity_usable_get,string='Usable Quantity',type="integer",method=True,readonly=True),
712
788
        'AAA': fields.boolean('All Adresses Available'),
713
789
        'split_id' : fields.many2one('dm.campaign.proposition.segment','Split'),
714
790
        'start_census' :fields.integer('Start Census (days)'),
760
836
]
761
837
 
762
838
QTY_TYPES = [
763
 
    ('quantity_theorical','Theorical Quantity'),
 
839
    ('quantity_estimated','Estimated Quantity'),
764
840
    ('quantity_wanted','Wanted Quantity'),
765
841
    ('quantity_delivered','Delivered Quantity'),
766
 
    ('quantity_real','Real Quantity'),
 
842
    ('quantity_usable','Usable Quantity'),
767
843
    ('quantity_free','Free Quantity'),
768
844
]
769
845
 
780
856
        value = {}
781
857
        if product_id:
782
858
            product = self.pool.get('product.product').browse(cr,uid,[product_id])[0]
783
 
            if product.categ_id.name not in ['Mailing Manufacturing','Customers List','DTP','Item','Translation']:
 
859
            if product.categ_id.name not in ['Mailing Manufacturing','Customers File','DTP','Item','Translation']:
784
860
                raise  osv.except_osv('Warning', 'The category of that product is not valid for a purchase line creation\n' \
785
861
                                            'Product category should be :\n' \
786
862
                                                '* Mailing Manufacturing\n' \
787
 
                                                '* Customers List\n' \
 
863
                                                '* Customers File\n' \
788
864
                                                '* Item\n' \
789
865
                                                '* DTP\n' \
790
866
                                                '* Translation\n' \
805
881
        quantity = 0
806
882
 
807
883
        pline = self.browse(cr, uid, ids)[0]
808
 
        if not pline.campaign_id:
809
 
            raise  osv.except_osv('Warning', "You must first save this Purchase Line and the campaign before using this button")
 
884
#        if not pline.campaign_id:
 
885
#            raise  osv.except_osv('Warning', "You must first save this Purchase Line and the campaign before using this button")
 
886
 
 
887
        if pline.campaign_group_id:
 
888
            obj = pline.campaign_group_id
 
889
        else:
 
890
            obj = pline.campaign_group_id
810
891
 
811
892
        if pline.type_quantity == 'quantity_free':
812
 
            quantity = 0
813
 
        elif pline.type_quantity == 'quantity_theorical':
814
 
            if pline.campaign_id.quantity_theorical_total.isdigit():
815
 
                quantity = pline.campaign_id.quantity_theorical_total
816
 
            else : 
 
893
            if not quantity:
 
894
                quantity = 0
 
895
        elif pline.type_quantity == 'quantity_estimated':
 
896
            if obj.quantity_estimated_total.isdigit():
 
897
                quantity = obj.quantity_estimated_total
 
898
            else :
817
899
                raise osv.except_osv('Warning',
818
900
                    'Cannot get wanted quantity, check prososition segments')
819
901
        elif pline.type_quantity == 'quantity_wanted':
820
 
            if pline.campaign_id.quantity_wanted_total.isdigit():
821
 
                quantity = pline.campaign_id.quantity_wanted_total
822
 
            elif pline.campaign_id.quantity_wanted_total == 'AAA for a Segment':
 
902
            if obj.quantity_wanted_total.isdigit():
 
903
                quantity = obj.quantity_wanted_total
 
904
            elif obj.quantity_wanted_total == 'AAA for a Segment':
823
905
                quantity = 0
824
906
            else :
825
907
                raise osv.except_osv('Warning',
826
908
                    "Cannot get wanted quantity, check prososition segments")
827
909
        elif pline.type_quantity == 'quantity_delivered':
828
 
            if pline.campaign_id.quantity_delivered_total.isdigit():
829
 
                quantity = pline.campaign_id.quantity_delivered_total
830
 
            else : 
 
910
            if obj.quantity_delivered_total.isdigit():
 
911
                quantity = obj.quantity_delivered_total
 
912
            else :
831
913
                raise osv.except_osv('Warning',
832
914
                    "Cannot get delivered quantity, check prososition segments")
833
 
        elif pline.type_quantity == 'quantity_real':
834
 
            if pline.campaign_id.quantity_real_total.isdigit():
835
 
                quantity = pline.campaign_id.quantity_real_total
836
 
            else : 
 
915
        elif pline.type_quantity == 'quantity_usable':
 
916
            if obj.quantity_usable_total.isdigit():
 
917
                quantity = obj.quantity_usable_total
 
918
            else :
837
919
                raise osv.except_osv('Warning',
838
 
                    "Cannot get real quantity, check prososition segments")
 
920
                    "Cannot get usable quantity, check prososition segments")
839
921
        else:
840
922
            raise osv.except_osv('Warning','Error getting quantity')
841
923
 
846
928
 
847
929
    def po_generate(self,cr, uid, ids, *args):
848
930
        plines = self.browse(cr, uid ,ids)
 
931
 
849
932
        if not plines:
850
 
            raise  osv.except_osv('Warning', "There's no purchase lines defined for this campaign")
 
933
            raise  osv.except_osv('Warning', "There's no purchase lines defined")
 
934
 
851
935
        for pline in plines:
852
936
            if pline.state == 'pending':
853
937
 
 
938
                print "Campaign ID : ",pline.campaign_id
 
939
                print "Group ID : ",pline.campaign_group_id
 
940
 
854
941
                if not pline.quantity and pline.type_quantity != 'quantity_free' and pline.type_quantity != 'quantity_wanted':
855
942
                    raise  osv.except_osv('Warning', "There's no quantity defined for this purchase line")
856
943
 
865
952
                    address_id = self.pool.get('res.partner').address_get(cr, uid, [partner.id], ['default'])['default']
866
953
                    if not address_id:
867
954
                        raise osv.except_osv('Warning', "There's no default address defined for this partner : %s" % (partner.name,) )
 
955
                    delivery_address = address_id
868
956
                    pricelist_id = partner.property_product_pricelist_purchase.id
869
957
                    if not pricelist_id:
870
958
                        raise osv.except_osv('Warning', "There's no purchase pricelist defined for this partner : %s" % (partner.name,) )
871
959
                    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]
872
960
                    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)
873
961
 
 
962
                    """
874
963
                    if not pline.campaign_id.offer_id:
875
964
                        raise osv.except_osv('Warning', "There's no offer defined for this campaign : %s" % (pline.campaign_id.name,) )
876
965
                    if not pline.campaign_id.proposition_ids:
877
966
                        raise osv.except_osv('Warning', "There's no proposition defined for this campaign : %s" % (pline.campaign_id.name,) )
 
967
                    """
 
968
 
 
969
                    # if in a group, obj = 1st campaign of the group, if not it's the campaing
 
970
                    if pline.campaign_group_id:
 
971
                        obj = pline.campaign_group_id.campaign_ids[0]
 
972
                        code = pline.campaign_group_id.code
 
973
                        print "First campaign of group : ", obj.name
 
974
                    else:
 
975
                        obj = pline.campaign_id
 
976
                        code = pline.campaign_id.code1
878
977
 
879
978
                    # Get constraints
880
979
                    constraints = []
881
980
                    if pline.type_product == 'Mailing Manufacturing':
882
 
                        for step in pline.campaign_id.offer_id.step_ids:
 
981
                        for step in obj.offer_id.step_ids:
883
982
                            for const in step.manufacturing_constraint_ids:
884
983
                                if not const.country_ids:
885
984
                                    constraints.append("---------------------------------------------------------------------------")
886
985
                                    constraints.append(const.name)
887
986
                                    constraints.append(const.constraint)
888
 
                                elif pline.campaign_id.country_id in const.country_ids:
 
987
                                elif obj.country_id in const.country_ids:
889
988
                                    constraints.append("---------------------------------------------------------------------------")
890
 
                                    constraints.append(const.name + ' for country :' +  pline.campaign_id.country_id.name)
 
989
                                    constraints.append(const.name + ' for country :' +  obj.country_id.name)
891
990
                                    constraints.append(const.constraint)
892
991
                            if pline.notes:
893
992
                                constraints.append("---------------------------------------------------------------------------")
896
995
                                constraints.append(' ')
897
996
                    elif pline.type_product == 'Item':
898
997
                        raise osv.except_osv('Warning', "Purchase of items is not yet implemented")
899
 
                        for step in pline.campaign_id.offer_id.step_ids:
 
998
                        for step in obj.offer_id.step_ids:
900
999
                            for item in step.product_ids:
901
1000
                                constraints.append("---------------------------------------------------------------------------")
902
1001
                                constraints.append(item.product_id.name)
903
1002
                                constraints.append(item.purchase_constraints)
904
 
                    elif pline.type_product == 'Customers List':
905
 
                        raise osv.except_osv('Warning', "Purchase of customers files is not yet implemented")
 
1003
                    elif pline.type_product == 'Customers File':
 
1004
                                constraints.append("---------------------------------------------------------------------------")
 
1005
                                constraints.append('Campaign Name : %s' % (obj.name,))
 
1006
                                constraints.append('Campaign Code : %s' % (obj.code1,))
 
1007
                                constraints.append('Deposit Date : %s' % (obj.date_start,))
 
1008
                                constraints.append("---------------------------------------------------------------------------")
 
1009
                                constraints.append('Trademark : %s' % (obj.trademark_id.name,))
 
1010
                                constraints.append('Estimated Quantity : %s' % (obj.quantity_estimated_total,))
 
1011
                                constraints.append('Responsible : %s' % (obj.files_responsible_id.name,))
 
1012
 
 
1013
                                if obj.files_delivery_address_id:
 
1014
                                    delivery_address = obj.files_delivery_address_id
 
1015
 
906
1016
                    elif pline.type == 'Translation':
907
 
                        if not pline.campaign_id.lang_id:
908
 
                            raise osv.except_osv('Warning', "There's no language defined for this campaign : %s" % (pline.campaign_id.name,) )
 
1017
                        if not obj.lang_id:
 
1018
                            raise osv.except_osv('Warning', "There's no language defined for this campaign : %s" % (obj.name,) )
909
1019
                        if pline.notes:
910
1020
                            constraints.append(pline.notes)
911
1021
                        else:
915
1025
                            constraints.append(pline.notes)
916
1026
                        else:
917
1027
                            constraints.append(' ')
 
1028
                    print "DElivery : ", delivery_address
918
1029
 
919
1030
                    # Create po
920
1031
                    purchase_id = self.pool.get('purchase.order').create(cr, uid, {
921
 
                        'origin': pline.campaign_id.code1,
 
1032
                        'origin': code,
922
1033
                        'partner_id': partner.id,
923
1034
                        'partner_address_id': address_id,
 
1035
#                        'dest_address_id': delivery_address.id,
924
1036
                        'location_id': 1,
925
1037
                        'pricelist_id': pricelist_id,
926
1038
                        'notes': "\n".join(constraints),
931
1043
                    if pline.type_product == 'Translation':
932
1044
                        line = self.pool.get('purchase.order.line').create(cr, uid, {
933
1045
                           'order_id': purchase_id,
934
 
                           'name': pline.campaign_id.code1,
 
1046
                           'name': code,
935
1047
                           'product_qty': pline.quantity,
936
1048
                           'product_id': pline.product_id.id,
937
1049
                           'product_uom': pline.uom_id.id,
938
1050
                           'price_unit': price,
939
1051
                           'date_planned': newdate.strftime('%Y-%m-%d %H:%M:%S'),
940
1052
                           'taxes_id': [(6, 0, [x.id for x in pline.product_id.product_tmpl_id.supplier_taxes_id])],
941
 
                           'account_analytic_id': pline.campaign_id.analytic_account_id,
 
1053
#                           'account_analytic_id': obj.analytic_account_id,
942
1054
                        })
943
1055
                    elif pline.type_product == 'Mailing Manufacturing':
944
 
                        ''' Create po lines for each proposition'''
945
 
                        lines = []
946
 
                        for propo in pline.campaign_id.proposition_ids:
947
 
                            line_name = propo.code1 + '-' + propo.type
948
 
 
949
 
                            if pline.type_quantity == 'quantity_free':
950
 
                                line_quantity = pline.quantity
951
 
                            elif pline.type_quantity == 'quantity_theorical':
952
 
                                if propo.quantity_theorical.isdigit():
953
 
                                    quantity = propo.quantity_theorical
954
 
                                else : 
955
 
                                    raise osv.except_osv('Warning',
956
 
                                        'Cannot get theorical quantity, check prososition %s' % (propo.name,)) 
957
 
                            elif pline.type_quantity == 'quantity_wanted':
958
 
                                if propo.quantity_wanted.isdigit():
959
 
                                    quantity = propo.quantity_wanted
960
 
                                elif propo.quantity_wanted == 'AAA for a Segment':
961
 
                                    quantity = 0
962
 
                                    line_name = propo.code1 + '-' + propo.type + '-AAA (All Addresses Available)'
963
 
                                else :
964
 
                                    raise osv.except_osv('Warning',
965
 
                                        'Cannot get wanted quantity, check prososition %s' % (propo.name,)) 
966
 
                            elif pline.type_quantity == 'quantity_delivered':
967
 
                                if propo.quantity_delivered.isdigit():
968
 
                                    quantity = propo.quantity_delivered
969
 
                                else : 
970
 
                                    raise osv.except_osv('Warning',
971
 
                                        'Cannot get delivered quantity, check prososition %s' % (propo.name,)) 
972
 
                            elif pline.type_quantity == 'quantity_real':
973
 
                                if propo.quantity_real.isdigit():
974
 
                                    quantity = propo.quantity_real
975
 
                                else : 
976
 
                                    raise osv.except_osv('Warning',
977
 
                                        'Cannot get delivered quantity, check prososition %s' % (propo.name,)) 
978
 
                            else:
979
 
                                raise osv.except_osv('Warning','Error getting quantity for proposition %s' % (propo.name,))
980
 
 
981
 
                            line = self.pool.get('purchase.order.line').create(cr, uid, {
982
 
                               'order_id': purchase_id,
983
 
                               'name': line_name,
984
 
                               'product_qty': quantity,
985
 
                               'product_id': pline.product_id.id,
986
 
                               'product_uom': pline.uom_id.id,
987
 
                               'price_unit': price,
988
 
                               'date_planned': newdate.strftime('%Y-%m-%d %H:%M:%S'),
989
 
                               'taxes_id': [(6, 0, [x.id for x in pline.product_id.product_tmpl_id.supplier_taxes_id])],
990
 
                               'account_analytic_id': propo.analytic_account_id,
991
 
                            })
 
1056
                        ''' Create po lines for each proposition (of each campaign if group)'''
 
1057
                        lines = []
 
1058
                        if pline.campaign_group_id:
 
1059
                            print "Creating PO line for group"
 
1060
                            for campaign in pline.campaign_group_id.campaign_ids:
 
1061
                                for propo in campaign.proposition_ids:
 
1062
                                    line_name = propo.code1 + '-' + propo.type
 
1063
                                    if pline.type_quantity == 'quantity_free':
 
1064
                                        line_quantity = pline.quantity
 
1065
                                    elif pline.type_quantity == 'quantity_estimated':
 
1066
                                        if propo.quantity_estimated.isdigit():
 
1067
                                            quantity = propo.quantity_estimated
 
1068
                                        else : 
 
1069
                                            raise osv.except_osv('Warning',
 
1070
                                                'Cannot get estimated quantity, check prososition %s' % (propo.name,)) 
 
1071
                                    elif pline.type_quantity == 'quantity_wanted':
 
1072
                                        if propo.quantity_wanted.isdigit():
 
1073
                                            quantity = propo.quantity_wanted
 
1074
                                        elif propo.quantity_wanted == 'AAA for a Segment':
 
1075
                                            quantity = 0
 
1076
                                            line_name = propo.code1 + '-' + propo.type + '- All Addresses Available'
 
1077
                                        else :
 
1078
                                            raise osv.except_osv('Warning',
 
1079
                                                'Cannot get wanted quantity, check prososition %s' % (propo.name,)) 
 
1080
                                    elif pline.type_quantity == 'quantity_delivered':
 
1081
                                        if propo.quantity_delivered.isdigit():
 
1082
                                            quantity = propo.quantity_delivered
 
1083
                                        else : 
 
1084
                                            raise osv.except_osv('Warning',
 
1085
                                                'Cannot get delivered quantity, check prososition %s' % (propo.name,)) 
 
1086
                                    elif pline.type_quantity == 'quantity_usable':
 
1087
                                        if propo.quantity_usable.isdigit():
 
1088
                                            quantity = propo.quantity_usable
 
1089
                                        else : 
 
1090
                                            raise osv.except_osv('Warning',
 
1091
                                                'Cannot get delivered quantity, check prososition %s' % (propo.name,)) 
 
1092
                                    else:
 
1093
                                        raise osv.except_osv('Warning','Error getting quantity for proposition %s' % (propo.name,))
 
1094
 
 
1095
                                    line = self.pool.get('purchase.order.line').create(cr, uid, {
 
1096
                                       'order_id': purchase_id,
 
1097
                                       'name': line_name,
 
1098
                                       'product_qty': quantity,
 
1099
                                       'product_id': pline.product_id.id,
 
1100
                                       'product_uom': pline.uom_id.id,
 
1101
                                       'price_unit': price,
 
1102
                                       'date_planned': newdate.strftime('%Y-%m-%d %H:%M:%S'),
 
1103
                                       'taxes_id': [(6, 0, [x.id for x in pline.product_id.product_tmpl_id.supplier_taxes_id])],
 
1104
                                       'account_analytic_id': propo.analytic_account_id,
 
1105
                                    })
 
1106
                        else:
 
1107
                            for propo in obj.proposition_ids:
 
1108
                                line_name = propo.code1 + '-' + propo.type
 
1109
 
 
1110
                                if pline.type_quantity == 'quantity_free':
 
1111
                                    line_quantity = pline.quantity
 
1112
                                elif pline.type_quantity == 'quantity_estimated':
 
1113
                                    if propo.quantity_estimated.isdigit():
 
1114
                                        quantity = propo.quantity_estimated
 
1115
                                    else : 
 
1116
                                        raise osv.except_osv('Warning',
 
1117
                                            'Cannot get estimated quantity, check prososition %s' % (propo.name,))
 
1118
                                elif pline.type_quantity == 'quantity_wanted':
 
1119
                                    if propo.quantity_wanted.isdigit():
 
1120
                                        quantity = propo.quantity_wanted
 
1121
                                    elif propo.quantity_wanted == 'AAA for a Segment':
 
1122
                                        quantity = 0
 
1123
                                        line_name = propo.code1 + '-' + propo.type + '- All Addresses Available'
 
1124
                                    else :
 
1125
                                        raise osv.except_osv('Warning',
 
1126
                                            'Cannot get wanted quantity, check prososition %s' % (propo.name,))
 
1127
                                elif pline.type_quantity == 'quantity_delivered':
 
1128
                                    if propo.quantity_delivered.isdigit():
 
1129
                                        quantity = propo.quantity_delivered
 
1130
                                    else : 
 
1131
                                        raise osv.except_osv('Warning',
 
1132
                                            'Cannot get delivered quantity, check prososition %s' % (propo.name,))
 
1133
                                elif pline.type_quantity == 'quantity_usable':
 
1134
                                    if propo.quantity_usable.isdigit():
 
1135
                                        quantity = propo.quantity_usable
 
1136
                                    else : 
 
1137
                                        raise osv.except_osv('Warning',
 
1138
                                            'Cannot get delivered quantity, check prososition %s' % (propo.name,))
 
1139
                                else:
 
1140
                                    raise osv.except_osv('Warning','Error getting quantity for proposition %s' % (propo.name,))
 
1141
 
 
1142
                                line = self.pool.get('purchase.order.line').create(cr, uid, {
 
1143
                                   'order_id': purchase_id,
 
1144
                                   'name': line_name,
 
1145
                                   'product_qty': quantity,
 
1146
                                   'product_id': pline.product_id.id,
 
1147
                                   'product_uom': pline.uom_id.id,
 
1148
                                   'price_unit': price,
 
1149
                                   'date_planned': newdate.strftime('%Y-%m-%d %H:%M:%S'),
 
1150
                                   'taxes_id': [(6, 0, [x.id for x in pline.product_id.product_tmpl_id.supplier_taxes_id])],
 
1151
                                   'account_analytic_id': propo.analytic_account_id,
 
1152
                                })
 
1153
                    elif pline.type_product == 'Customers File':
 
1154
                        lines = []
 
1155
                        if pline.campaign_group_id:
 
1156
                            for campaign in pline.campaign_group_id.campaign_ids:
 
1157
                                for propo in campaign.proposition_ids:
 
1158
                                    for segment in propo.segment_ids:
 
1159
                                        line_name = propo.code1 + ' - ' + segment.list_id.name
 
1160
                                        if pline.type_quantity == 'quantity_free':
 
1161
                                            raise osv.except_osv('Warning',
 
1162
                                                'You cannot use a free quantity for a customers file order')
 
1163
                                        elif pline.type_quantity == 'quantity_estimated':
 
1164
                                            quantity = segment.quantity_estimated
 
1165
                                        elif pline.type_quantity == 'quantity_wanted':
 
1166
                                            quantity = segment.quantity_wanted
 
1167
                                            if segment.AAA:
 
1168
                                                quantity = 0
 
1169
                                                line_name = propo.code1 + ' - ' + segment.list_id.name + ' - All Addresses Available'
 
1170
                                        elif pline.type_quantity == 'quantity_delivered':
 
1171
                                            quantity = segment.quantity_delivered
 
1172
                                        elif pline.type_quantity == 'quantity_usable':
 
1173
                                            quantity = segment.quantity_usable
 
1174
                                        else:
 
1175
                                            raise osv.except_osv('Warning','Error getting quantity for proposition %s' % (propo.name,))
 
1176
 
 
1177
                                        line = self.pool.get('purchase.order.line').create(cr, uid, {
 
1178
                                           'order_id': purchase_id,
 
1179
                                           'name': line_name,
 
1180
                                           'product_qty': quantity,
 
1181
                                           'product_id': pline.product_id.id,
 
1182
                                           'product_uom': pline.uom_id.id,
 
1183
                                           'price_unit': price,
 
1184
                                           'date_planned': newdate.strftime('%Y-%m-%d %H:%M:%S'),
 
1185
                                           'taxes_id': [(6, 0, [x.id for x in pline.product_id.product_tmpl_id.supplier_taxes_id])],
 
1186
                                           'account_analytic_id': propo.analytic_account_id,
 
1187
                                        })
 
1188
                        else:
 
1189
                            for propo in obj.proposition_ids:
 
1190
                                for segment in propo.segment_ids:
 
1191
                                    line_name = propo.code1 + ' - ' + segment.list_id.name
 
1192
                                    if pline.type_quantity == 'quantity_free':
 
1193
                                        raise osv.except_osv('Warning',
 
1194
                                            'You cannot use a free quantity for a customers file order')
 
1195
                                    elif pline.type_quantity == 'quantity_estimated':
 
1196
                                        quantity = segment.quantity_estimated
 
1197
                                    elif pline.type_quantity == 'quantity_wanted':
 
1198
                                        quantity = segment.quantity_wanted
 
1199
                                        if segment.AAA:
 
1200
                                            quantity = 0
 
1201
                                            line_name = propo.code1 + ' - ' + segment.list_id.name + ' - All Addresses Available'
 
1202
                                    elif pline.type_quantity == 'quantity_delivered':
 
1203
                                        quantity = propo.quantity_delivered
 
1204
                                    elif pline.type_quantity == 'quantity_usable':
 
1205
                                        quantity = propo.quantity_usable
 
1206
                                    else:
 
1207
                                        raise osv.except_osv('Warning','Error getting quantity for proposition %s' % (propo.name,))
 
1208
 
 
1209
                                    line = self.pool.get('purchase.order.line').create(cr, uid, {
 
1210
                                       'order_id': purchase_id,
 
1211
                                       'name': line_name,
 
1212
                                       'product_qty': quantity,
 
1213
                                       'product_id': pline.product_id.id,
 
1214
                                       'product_uom': pline.uom_id.id,
 
1215
                                       'price_unit': price,
 
1216
                                       'date_planned': newdate.strftime('%Y-%m-%d %H:%M:%S'),
 
1217
                                       'taxes_id': [(6, 0, [x.id for x in pline.product_id.product_tmpl_id.supplier_taxes_id])],
 
1218
                                       'account_analytic_id': propo.analytic_account_id,
 
1219
                                    })
992
1220
 
993
1221
                    if pline.type_document == 'po':
994
1222
                        print "Set as Purchase Order"
1060
1288
 
1061
1289
    _columns = {
1062
1290
        'campaign_id': fields.many2one('dm.campaign', 'Campaign'),
 
1291
        'campaign_group_id': fields.many2one('dm.campaign.group', 'Campaign Group'),
1063
1292
        'product_id' : fields.many2one('product.product', 'Product', required=True, context={'flag':True}),
1064
 
        'quantity' : fields.integer('Quantity', readonly=False, required=True),
 
1293
        'quantity' : fields.integer('Total Quantity', readonly=False, required=True),
1065
1294
        'type_quantity' : fields.selection(QTY_TYPES, 'Quantity Type', size=32),
1066
1295
        'type_document' : fields.selection(DOC_TYPES, 'Document Generated', size=32),
1067
1296
        'type_product' : fields.char('Product Type',size=64, readonly=True),
1071
1300
        'date_delivery': fields.function(_delivery_date_get, method=True, type='datetime', string='Delivery Date', readonly=True),
1072
1301
        'trigger' : fields.selection(PURCHASE_LINE_TRIGGERS, 'Trigger'),
1073
1302
        'type' : fields.selection(PURCHASE_LINE_TYPES, 'Type'),
1074
 
        'purchase_order_ids' : fields.one2many('purchase.order','dm_campaign_purchase_line','DM Campaign Purchase Line'),
 
1303
        'purchase_order_ids' : fields.one2many('purchase.order','dm_campaign_purchase_line','Campaign Purchase Line'),
1075
1304
        'notes': fields.text('Notes'),
 
1305
        'togroup': fields.boolean('Apply to Campaign Group'),
1076
1306
        'state' : fields.function(_state_get, method=True, type='selection', selection=[
1077
1307
            ('pending','Pending'),
1078
1308
            ('requested','Quotations Requested'),