[merge]
[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
26 class sale_advance_payment_inv(osv.osv_memory):
27     _name = "sale.advance.payment.inv"
28     _description = "Sale Advance Payment Invoice"
29     _columns = {
30         'product_id': fields.many2one('product.product', 'Product', required=True),
31         'amount': fields.float('Unit Price', size=(16, 2), required=True),
32         'qtty': fields.float('Quantity', size=(16, 2), required=True),
33     }
34     _default = {
35         'qtty' : lambda *a: 1
36                }
37     def create_invoices(self, cr, uid, ids, context={}):
38         """
39              To create invoices.
40
41              @param self: The object pointer.
42              @param cr: A database cursor
43              @param uid: ID of the user currently logged in
44              @param ids: the ID or list of IDs if we want more than one
45              @param context: A standard dictionary
46
47              @return:
48
49         """
50         list_inv = []
51         obj_sale = self.pool.get('sale.order')
52         obj_lines = self.pool.get('account.invoice.line')
53         inv_obj = self.pool.get('account.invoice')
54
55         for sale_adv_obj in self.browse(cr, uid, ids):
56             for sale in obj_sale.browse(cr, uid, context['active_ids']):
57                 address_contact = False
58                 address_invoice = False
59                 create_ids = []
60                 ids_inv = []
61                 if sale.order_policy == 'postpaid':
62                     raise osv.except_osv(
63                         _('Error'),
64                         _("You cannot make an advance on a sale order \
65                              that is defined as 'Automatic Invoice after delivery'."))
66                 val = obj_lines.product_id_change(cr, uid, [], sale_adv_obj.product_id.id,
67                         uom = False, partner_id = sale.partner_id.id, fposition_id = sale.fiscal_position.id)
68                 line_id =obj_lines.create(cr, uid, {
69                     'name': val['value']['name'],
70                     'account_id': val['value']['account_id'],
71                     'price_unit': sale_adv_obj.amount,
72                     'quantity': sale_adv_obj.qtty,
73                     'discount': False,
74                     'uos_id': val['value']['uos_id'],
75                     'product_id': sale_adv_obj.product_id.id,
76                     'invoice_line_tax_id': [(6, 0, val['value']['invoice_line_tax_id'])],
77                     'account_analytic_id': sale.project_id.id or False,
78                     #'note':'',
79                 })
80                 create_ids.append(line_id)
81                 inv = {
82                     'name': sale.name,
83                     'origin': sale.name,
84                     'type': 'out_invoice',
85                     'reference': False,
86                     'account_id': sale.partner_id.property_account_receivable.id,
87                     'partner_id': sale.partner_id.id,
88                     'address_invoice_id':sale.partner_invoice_id.id,
89                     'address_contact_id':sale.partner_order_id.id,
90                     'invoice_line': [(6, 0, create_ids)],
91                     'currency_id' :sale.pricelist_id.currency_id.id,
92                     'comment': '',
93                     'payment_term':sale.payment_term.id,
94                     'fiscal_position': sale.fiscal_position.id or sale.partner_id.property_account_position.id
95                     }
96
97                 inv_id = inv_obj.create(cr, uid, inv)
98
99                 for inv in sale.invoice_ids:
100                     ids_inv.append(inv.id)
101                 ids_inv.append(inv_id)
102                 obj_sale.write(cr, uid, sale.id, {'invoice_ids':[(6, 0, ids_inv)]})
103                 list_inv.append(inv_id)
104         #
105         # If invoice on picking: add the cost on the SO
106         # If not, the advance will be deduced when generating the final invoice
107         #
108                 if sale.order_policy=='picking':
109                     self.pool.get('sale.order.line').create(cr, uid, {
110                         'order_id': sale.id,
111                         'name': val['value']['name'],
112                         'price_unit': -sale_adv_obj.amount,
113                         'product_uom_qty': sale_adv_obj.qtty,
114                         'product_uos_qty': sale_adv_obj.qtty,
115                         'product_uos': val['value']['uos_id'],
116                         'product_uom': val['value']['uos_id'],
117                         'product_id': sale_adv_obj.product_id.id,
118                         'discount': False,
119                         'tax_id': [(6, 0, val['value']['invoice_line_tax_id'])],
120                     }, context)
121
122         context.update({'invoice_id':list_inv})
123
124         return {#'invoice_ids':list_inv,
125                 'name': 'Open Invoice',
126                 'view_type': 'form',
127                 'view_mode': 'form',
128                 'res_model': 'sale.open.invoice',
129                 'type': 'ir.actions.act_window',
130                 'target': 'new',
131                 'context':context
132                 }
133
134 sale_advance_payment_inv()
135
136 class sale_open_invoice(osv.osv_memory):
137     _name = "sale.open.invoice"
138     _description = "Sale Open Invoice"
139     _columns = {
140     }
141
142     def open_invoice(self, cr, uid, ids, context):
143
144         """
145              To open invoice.
146
147              @param self: The object pointer.
148              @param cr: A database cursor
149              @param uid: ID of the user currently logged in
150              @param ids: the ID or list of IDs if we want more than one
151              @param context: A standard dictionary
152
153              @return:
154
155         """
156         record_id = context and context.get('active_id', False) or False
157
158         mod_obj = self.pool.get('ir.model.data')
159         obj_inv = self.pool.get('account.invoice')
160         invoices = []
161         invoice = obj_inv.browse(cr, uid, record_id, context=context)
162
163         for advance_pay in self.browse(cr, uid, ids):
164             result = mod_obj._get_id(cr, uid, 'account', 'view_account_invoice_filter')
165             id = mod_obj.read(cr, uid, result, ['res_id'])
166             form_id = mod_obj._get_id(cr, uid, 'account', 'invoice_form')
167             form_res = mod_obj.browse(cr, uid, form_id, context=context).res_id
168             tree_id = mod_obj._get_id(cr, uid, 'account', 'invoice_tree')
169             tree_res = mod_obj.browse(cr, uid, tree_id, context=context).res_id
170         return {
171 #            'domain': "[('id','in', ["+','.join(map(str, invoices))+"])]", # TODO
172             'name': 'Invoices',
173             'view_type': 'form',
174             'view_mode': 'form,tree',
175             'res_model': 'account.invoice',
176             'res_id': int(context['invoice_id'][0]),
177             'view_id': False,
178             'views': [(form_res, 'form'), (tree_res, 'tree')],
179             'context': "{'type':'out_invoice'}",
180             'type': 'ir.actions.act_window',
181          }
182
183
184
185
186 sale_open_invoice()
187
188 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: