[IMP] Changed all module categories, limited number of categories
[odoo/odoo.git] / addons / sale_layout / sale_layout.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU Affero General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from osv import fields, osv
24 import decimal_precision as dp
25
26 class sale_order_line(osv.osv):
27
28     def _amount_line(self, cr, uid, ids, field_name, arg, context=None):
29         res = {}
30         for line in self.browse(cr, uid, ids, context=context):
31             if line.layout_type == 'article':
32                 return super(sale_order_line, self)._amount_line(cr, uid, ids, field_name, arg, context)
33         return res
34
35     def invoice_line_create(self, cr, uid, ids, context=None):
36         new_ids = []
37         list_seq = []
38         for line in self.browse(cr, uid, ids, context=context):
39             if line.layout_type == 'article':
40                 new_ids.append(line.id)
41                 list_seq.append(line.sequence)
42         invoice_line_ids = super(sale_order_line, self).invoice_line_create(cr, uid, new_ids, context)
43         pool_inv_line = self.pool.get('account.invoice.line')
44         seq = 0
45         for obj_inv_line in pool_inv_line.browse(cr, uid, invoice_line_ids, context=context):
46             pool_inv_line.write(cr, uid, [obj_inv_line.id], {'sequence': list_seq[seq]}, context=context)
47             seq += 1
48         return invoice_line_ids
49
50     def onchange_sale_order_line_view(self, cr, uid, id, type, context={}, *args):
51         temp = {}
52         temp['value'] = {}
53         if (not type):
54             return {}
55         if type != 'article':
56             temp = {
57                 'value': {
58                 'product_id': False,
59                 'uos_id': False,
60                 'account_id': False,
61                 'price_unit': 0.0,
62                 'price_subtotal': 0.0,
63                 'quantity': 0,
64                 'discount': 0.0,
65                 'invoice_line_tax_id': False,
66                 'account_analytic_id': False,
67                 'product_uom_qty': 0.0,
68                 },
69             }
70             if type == 'line':
71                 temp['value']['name'] = ' '
72             if type == 'break':
73                 temp['value']['name'] = ' '
74             if type == 'subtotal':
75                 temp['value']['name'] = 'Sub Total'
76             return temp
77         return {}
78
79     def create(self, cr, user, vals, context=None):
80         if vals.has_key('layout_type'):
81             if vals['layout_type'] == 'line':
82                 vals['name'] = ' '
83             if vals['layout_type'] == 'break':
84                 vals['name'] = ' '
85             if vals['layout_type'] != 'article':
86                 vals['product_uom_qty']= 0
87         return super(sale_order_line, self).create(cr, user, vals, context)
88
89     def write(self, cr, user, ids, vals, context=None):
90         if vals.has_key('layout_type'):
91             if vals['layout_type'] == 'line':
92                 vals['name'] = ' '
93             if vals['layout_type'] == 'break':
94                 vals['name'] = ' '
95         return super(sale_order_line, self).write(cr, user, ids, vals, context)
96
97     def copy(self, cr, uid, id, default=None, context=None):
98         if default is None:
99             default = {}
100         default['layout_type'] = self.browse(cr, uid, id, context=context).layout_type
101         return super(sale_order_line, self).copy(cr, uid, id, default, context)
102
103     _order = "order_id, sequence asc"
104     _description = "Sales Order line"
105     _inherit = "sale.order.line"
106     _columns = {
107         'layout_type': fields.selection([
108                 ('article', 'Product'),
109                 ('title', 'Title'),
110                 ('text', 'Note'),
111                 ('subtotal', 'Sub Total'),
112                 ('line', 'Separator Line'),
113                 ('break', 'Page Break'),]
114             ,'Layout Type', select=True, required=True),
115         'sequence': fields.integer('Layout Sequence'),
116         'price_unit': fields.float('Unit Price', required=True, digits_compute= dp.get_precision('Sale Price'), readonly=True, states={'draft': [('readonly', False)]}),
117         'product_uom_qty': fields.float('Quantity (UoM)', digits=(16,2)),
118         'product_uom': fields.many2one('product.uom', 'Product UoM'),
119     }
120
121     _defaults = {
122         'layout_type': 'article',
123     }
124
125 sale_order_line()
126
127 class one2many_mod2(fields.one2many):
128     def get(self, cr, obj, ids, name, user=None, offset=0, context=None, values=None):
129         if not values:
130             values = {}
131         res = {}
132         for id in ids:
133             res[id] = []
134         ids2 = obj.pool.get(self._obj).search(cr, user, [(self._fields_id, 'in', ids), ('layout_type', '=', 'article')], limit=self._limit)
135         for r in obj.pool.get(self._obj)._read_flat(cr, user, ids2, [self._fields_id], context=context, load='_classic_write'):
136             res[r[self._fields_id]].append( r['id'] )
137         return res
138
139
140 class sale_order(osv.osv):
141
142     def copy(self, cr, uid, id, default=None, context=None):
143         if default is None:
144             default = {}
145         default['order_line'] = False
146         return super(sale_order, self).copy(cr, uid, id, default, context)
147
148     _inherit = "sale.order"
149     _columns = {
150         'abstract_line_ids': fields.one2many('sale.order.line', 'order_id', 'Order Lines', readonly=True, states={'draft': [('readonly', False)]}),
151         'order_line': one2many_mod2('sale.order.line', 'order_id', 'Order Lines', readonly=True, states={'draft': [('readonly', False)]}),
152     }
153
154 sale_order()
155
156 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: