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 _
30 class sale_advance_payment_inv(osv.osv_memory):
31 _name = "sale.advance.payment.inv"
32 _description = "Sale Advance Payment Invoice"
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),
41 def create_invoices(self, cr, uid, ids, context={}):
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
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')
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
65 if sale.order_policy == 'postpaid':
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,
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,
84 create_ids.append(line_id)
88 'type': 'out_invoice',
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,
97 'payment_term':sale.payment_term.id,
98 'fiscal_position': sale.partner_id.property_account_position.id
101 inv_id = inv_obj.create(cr, uid, inv)
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)
109 # If invoice on picking: add the cost on the SO
110 # If not, the advance will be deduced when generating the final invoice
112 if sale.order_policy=='picking':
113 self.pool.get('sale.order.line').create(cr, uid, {
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,
123 'tax_id': [(6, 0, val['value']['invoice_line_tax_id'])],
126 return {#'invoice_ids':list_inv,
127 'name': 'Open Invoice',
130 'res_model': 'sale.open.invoice',
131 'type': 'ir.actions.act_window',
133 'context': "{'invoice_ids'=%s}" % (list_inv)
136 sale_advance_payment_inv()
138 class sale_open_invoice(osv.osv_memory):
139 _name = "sale.open.invoice"
140 _description = "Sale Open Invoice"
144 def open_invoice(self, cr, uid, ids, context):
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
157 mod_obj = self.pool.get('ir.model.data')
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']
168 # 'domain': "[('id','in', ["+','.join(map(str, invoices))+"])]", # TODO
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']
180 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: