8c9f9ac032dd0b5977e80b41d82c3b12ad76a1ec
[odoo/odoo.git] / addons / point_of_sale / wizard / pos_discount.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19 #
20 ##############################################################################
21
22 import netsvc
23 from osv import osv,fields
24 from tools.translate import _
25
26 class pos_discount(osv.osv_memory):
27     _name = 'pos.discount'
28     _description = 'Add Discount'
29
30     _columns = {
31         'discount': fields.float('Discount ', required=True),
32         'discount_notes': fields.char('Discount Notes',size= 128, required=True),
33     }
34     _defaults = {
35         'discount': lambda *a: 5,
36     }
37     
38     def apply_discount(self, cr, uid, ids, context):
39         """ 
40          To give the discount of  product and check the.
41          
42          @param self: The object pointer.
43          @param cr: A database cursor
44          @param uid: ID of the user currently logged in
45          @param context: A standard dictionary 
46          @return : nothing
47         """        
48         this = self.browse(cr, uid, ids[0], context=context)
49         record_id = context and context.get('active_id', False)
50         if isinstance(record_id, (int, long)):
51             record_id=[record_id]
52         
53         order_ref = self.pool.get('pos.order')
54         order_line_ref =self.pool.get('pos.order.line')
55         
56         for order in order_ref.browse(cr, uid, record_id, context=context):
57             
58             for line in order.lines :
59                 company_discount = order.company_id.company_discount
60                 applied_discount = this.discount
61                 
62                 if applied_discount == 0.00:
63                     notice = 'No Discount'
64                 elif company_discount >=  applied_discount:
65                     notice = 'Minimum Discount'
66                 else:
67                     notice = this.discount_notes
68                 
69                 res_new = {
70                 }
71                 
72                 if this.discount <= company_discount:
73                     res_new = {
74                         'discount': this.discount,
75                         'notice': notice,
76                         'price_ded': line.price_unit * line.qty * (this.discount or 0) * 0.01 or 0.0
77                     }
78                 else:
79                     res_new = {
80                         'discount': this.discount,
81                         'notice': notice,
82                         'price_ded': line.price_unit * line.qty * (this.discount or 0) * 0.01 or 0.0 
83                     }
84                 
85                 order_line_ref.write(cr, uid, [line.id], res_new, context=context)
86         return {}
87
88 #    def check_discount(self, cr, uid, record_id, discount, context):
89 #        """ 
90 #         Check the discount of define by company  .            
91 #         @param self: The object pointer.
92 #         @param cr: A database cursor
93 #         @param uid: ID of the user currently logged in
94 #         @param record_id:Current Order id
95 #         @param discount:Select Discount
96 #         @param context: A standard dictionary 
97 #         @return : retrun  to apply and used the company discount base on condition
98 #        """
99
100 #        order_ref = self.pool.get('pos.order')
101 #        
102 #        for order in order_ref.browse(cr, uid, record_id, context=context):
103 #            company_disc = order.company_id.company_discount
104 #            for line in order.lines :
105 #                prod_disc = discount
106 #                if prod_disc <= company_disc :
107 #                   return 'apply_discount'
108 #                else :
109 #                    return 'disc_discount'    
110 pos_discount()
111 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: