1 ##############################################################################
3 # OpenERP, Open Source Management Solution
4 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
19 ##############################################################################
21 from osv import fields, osv
22 from service import web_services
23 from tools.translate import _
26 class sale_advance_payment_inv(osv.osv_memory):
27 _name = "sale.advance.payment.inv"
28 _description = "Sale Advance Payment Invoice"
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),
37 def create_invoices(self, cr, uid, ids, context={}):
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
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')
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
61 if sale.order_policy == 'postpaid':
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,
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,
80 create_ids.append(line_id)
84 'type': 'out_invoice',
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,
93 'payment_term':sale.payment_term.id,
94 'fiscal_position': sale.fiscal_position.id or sale.partner_id.property_account_position.id
97 inv_id = inv_obj.create(cr, uid, inv)
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)
105 # If invoice on picking: add the cost on the SO
106 # If not, the advance will be deduced when generating the final invoice
108 if sale.order_policy=='picking':
109 self.pool.get('sale.order.line').create(cr, uid, {
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,
119 'tax_id': [(6, 0, val['value']['invoice_line_tax_id'])],
122 context.update({'invoice_id':list_inv})
124 return {#'invoice_ids':list_inv,
125 'name': 'Open Invoice',
128 'res_model': 'sale.open.invoice',
129 'type': 'ir.actions.act_window',
134 sale_advance_payment_inv()
136 class sale_open_invoice(osv.osv_memory):
137 _name = "sale.open.invoice"
138 _description = "Sale Open Invoice"
142 def open_invoice(self, cr, uid, ids, context):
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
156 record_id = context and context.get('active_id', False) or False
158 mod_obj = self.pool.get('ir.model.data')
159 obj_inv = self.pool.get('account.invoice')
161 invoice = obj_inv.browse(cr, uid, record_id, context=context)
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
171 # 'domain': "[('id','in', ["+','.join(map(str, invoices))+"])]", # TODO
174 'view_mode': 'form,tree',
175 'res_model': 'account.invoice',
176 'res_id': int(context['invoice_id'][0]),
178 'views': [(form_res, 'form'), (tree_res, 'tree')],
179 'context': "{'type':'out_invoice'}",
180 'type': 'ir.actions.act_window',
188 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: