[IMP] mrp_procurement, project_mrp: Task ID-574: Changed workflow for service product...
[odoo/odoo.git] / addons / sale / wizard / sale_make_invoice_advance.py
1 ##############################################################################
2 #
3 #    OpenERP, Open Source Management Solution
4 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU Affero General Public License as
8 #    published by the Free Software Foundation, either version 3 of the
9 #    License, or (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU Affero General Public License for more details.
15 #
16 #    You should have received a copy of the GNU Affero General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 ##############################################################################
20
21 from osv import fields, osv
22 from service import web_services
23 from tools.translate import _
24 import ir
25 import netsvc
26 import pooler
27 import wizard
28
29
30 class sale_advance_payment_inv(osv.osv_memory):
31     _name = "sale.advance.payment.inv"
32     _description = "Sale Advance Payment Invoice"
33     _columns = {
34         'product_id': fields.many2one('product.product', 'Product', required=True),
35         'amount': fields.float('Unit Price', size=(16, 2), required=True),
36         'qtty': fields.float('Quantity', size=(16, 2), required=True),
37     }
38     _default = {
39         'qtty' : lambda *a: 1
40                }
41     def create_invoices(self, cr, uid, ids, context={}):
42         """ 
43              To create invoices.
44             
45              @param self: The object pointer.
46              @param cr: A database cursor
47              @param uid: ID of the user currently logged in
48              @param ids: the ID or list of IDs if we want more than one 
49              @param context: A standard dictionary 
50              
51              @return:  
52         
53         """        
54         list_inv = []
55         obj_sale = self.pool.get('sale.order')
56         obj_lines = self.pool.get('account.invoice.line')
57         inv_obj = self.pool.get('account.invoice')
58         
59         for sale_adv_obj in self.browse(cr, uid, ids):
60             for sale in obj_sale.browse(cr, uid, context['active_ids']):
61                 address_contact = False
62                 address_invoice = False
63                 create_ids = []
64                 ids_inv = []
65                 if sale.order_policy == 'postpaid':
66                     raise osv.except_osv(
67                         _('Error'),
68                         _("You cannot make an advance on a sale order \
69                              that is defined as 'Automatic Invoice after delivery'."))
70                 val = obj_lines.product_id_change(cr, uid, [], sale_adv_obj.product_id.id,
71                         uom = False, partner_id = sale.partner_id.id, fposition_id=sale.fiscal_position.id)
72                 line_id =obj_lines.create(cr, uid, {
73                     'name': val['value']['name'],
74                     'account_id': val['value']['account_id'],
75                     'price_unit': sale_adv_obj.amount,
76                     'quantity': sale_adv_obj.qtty,
77                     'discount': False,
78                     'uos_id': val['value']['uos_id'],
79                     'product_id': sale_adv_obj.product_id.id,
80                     'invoice_line_tax_id': [(6, 0, val['value']['invoice_line_tax_id'])],
81                     'account_analytic_id': sale.project_id.id or False,
82                     #'note':'',
83                 })
84                 create_ids.append(line_id)
85                 inv = {
86                     'name': sale.name,
87                     'origin': sale.name,
88                     'type': 'out_invoice',
89                     'reference': False,
90                     'account_id': sale.partner_id.property_account_receivable.id,
91                     'partner_id': sale.partner_id.id,
92                     'address_invoice_id':sale.partner_invoice_id.id,
93                     'address_contact_id':sale.partner_order_id.id,
94                     'invoice_line': [(6, 0, create_ids)],
95                     'currency_id' :sale.pricelist_id.currency_id.id,
96                     'comment': '',
97                     'payment_term':sale.payment_term.id,
98                     'fiscal_position': sale.partner_id.property_account_position.id
99                     }
100                 
101                 inv_id = inv_obj.create(cr, uid, inv)
102
103                 for inv in sale.invoice_ids:
104                     ids_inv.append(inv.id)
105                 ids_inv.append(inv_id)
106                 obj_sale.write(cr, uid, sale.id, {'invoice_ids':[(6, 0, ids_inv)]})
107                 list_inv.append(inv_id)
108         #
109         # If invoice on picking: add the cost on the SO
110         # If not, the advance will be deduced when generating the final invoice
111         #
112                 if sale.order_policy=='picking':
113                     self.pool.get('sale.order.line').create(cr, uid, {
114                         'order_id': sale.id,
115                         'name': val['value']['name'],
116                         'price_unit': -sale_adv_obj.amount,
117                         'product_uom_qty': sale_adv_obj.qtty,
118                         'product_uos_qty': sale_adv_obj.qtty,
119                         'product_uos': val['value']['uos_id'],
120                         'product_uom': val['value']['uos_id'],
121                         'product_id': sale_adv_obj.product_id.id,
122                         'discount': False,
123                         'tax_id': [(6, 0, val['value']['invoice_line_tax_id'])],
124                     }, context)
125
126         return {#'invoice_ids':list_inv,
127                 'name': 'Open Invoice',
128                 'view_type': 'form',
129                 'view_mode': 'form',
130                 'res_model': 'sale.open.invoice',
131                 'type': 'ir.actions.act_window',
132                 'target': 'new',
133                 'context': "{'invoice_ids'=%s}" % (list_inv)
134                 }
135
136 sale_advance_payment_inv()
137
138 class sale_open_invoice(osv.osv_memory):
139     _name = "sale.open.invoice"
140     _description = "Sale Open Invoice"
141     _columns = {
142     }
143
144     def open_invoice(self, cr, uid, ids, context):
145         """ 
146              To open invoice.
147             
148              @param self: The object pointer.
149              @param cr: A database cursor
150              @param uid: ID of the user currently logged in
151              @param ids: the ID or list of IDs if we want more than one 
152              @param context: A standard dictionary 
153              
154              @return:  
155         
156         """        
157         mod_obj = self.pool.get('ir.model.data')
158         invoices = []
159         #TODO: Can not get invoice ids here
160         for advance_pay in self.browse(cr, uid, ids):
161             result = mod_obj._get_id(cr, uid, 'account', 'view_account_invoice_filter')
162             id = mod_obj.read(cr, uid, result, ['res_id'])
163             model_data_ids = mod_obj.search(cr, uid,
164                              [('model', '=', 'ir.ui.view'), ('name', '=', 'invoice_form')])
165             resource_id = mod_obj.read(cr, uid, model_data_ids,
166                                             fields=['res_id'])[0]['res_id']
167         return {
168 #            'domain': "[('id','in', ["+','.join(map(str, invoices))+"])]", # TODO
169             'name': 'Invoices',
170             'view_type': 'form',
171             'view_mode': 'tree,form',
172             'res_model': 'account.invoice',
173             'views': [(False, 'tree'), (resource_id, 'form')],
174             'context': "{'type':'out_invoice'}",
175             'type': 'ir.actions.act_window',
176             'search_view_id': id['res_id']
177          }
178 sale_open_invoice()
179
180 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: