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 tools.translate import _
23 import decimal_precision as dp
25 class sale_advance_payment_inv(osv.osv_memory):
26 _name = "sale.advance.payment.inv"
27 _description = "Sales Advance Payment Invoice"
30 'advance_payment_method':fields.selection(
31 [('all', 'All'), ('percentage','Percentage'), ('fixed','Fixed Price'),
32 ('lines', 'Some Order Lines')],
33 'Type', required=True,
34 help="""Use All to create the final invoice.
35 Use Percentage to invoice a percentage of the total amount.
36 Use Fixed Price to invoice a specific amound in advance.
37 Use Some Order Lines to invoice a selection of the sale order lines."""),
38 'qtty': fields.float('Quantity', digits=(16, 2), required=True),
39 'product_id': fields.many2one('product.product', 'Advance Product',
40 help="""Select a product of type service which is called 'Advance Product'.
41 You may have to create it and set it as a default value on this field."""),
42 'amount': fields.float('Advance Amount', digits_compute= dp.get_precision('Sale Price'),
43 help="The amount to be invoiced in advance."),
46 def _get_advance_product(self, cr, uid, context=None):
48 product = self.pool.get('ir.model.data').get_object(cr, uid, 'sale', 'advance_product_0')
50 # a ValueError is returned if the xml id given is not found in the table ir_model_data
55 'advance_payment_method': 'all',
57 'product_id': _get_advance_product,
60 def onchange_method(self, cr, uid, ids, advance_payment_method, product_id, context=None):
61 if advance_payment_method == 'percentage':
62 return {'value': {'amount':0, 'product_id':False }}
64 product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
65 return {'value': {'amount': product.list_price}}
66 return {'value': {'amount': 0}}
68 def create_invoices(self, cr, uid, ids, context=None):
69 """ create invoices for the active sale orders """
72 wizard = self.browse(cr, uid, ids[0], context)
73 sale_ids = context.get('active_ids', [])
75 if wizard.advance_payment_method == 'all':
76 # create the final invoices of the active sale orders
77 res = self.pool.get('sale.order').manual_invoice(cr, uid, sale_ids, context)
78 if context.get('open_invoices', False):
80 return {'type': 'ir.actions.act_window_close'}
82 if wizard.advance_payment_method == 'lines':
83 # open the list view of sale order lines to invoice
84 act_window = self.pool.get('ir.actions.act_window')
85 res = act_window.for_xml_id(cr, uid, 'sale', 'action_order_line_tree2', context)
87 'search_default_uninvoiced': 1,
88 'search_default_order_id': sale_ids and sale_ids[0] or False,
92 assert wizard.advance_payment_method in ('fixed', 'percentage')
94 sale_obj = self.pool.get('sale.order')
95 inv_obj = self.pool.get('account.invoice')
96 inv_line_obj = self.pool.get('account.invoice.line')
99 for sale in sale_obj.browse(cr, uid, sale_ids, context=context):
100 if sale.order_policy == 'postpaid':
101 raise osv.except_osv(
103 _("You cannot make an advance on a sales order \
104 that is defined as 'Automatic Invoice after delivery'."))
106 val = inv_line_obj.product_id_change(cr, uid, [], wizard.product_id.id,
107 uom=False, partner_id=sale.partner_id.id, fposition_id=sale.fiscal_position.id)
110 # determine and check income account
111 if not wizard.product_id.id :
112 prop = self.pool.get('ir.property').get(cr, uid,
113 'property_account_income_categ', 'product.category', context=context)
114 prop_id = prop and prop.id or False
115 account_id = self.pool.get('account.fiscal.position').map_account(cr, uid, sale.fiscal_position.id or False, prop_id)
117 raise osv.except_osv(_('Configuration Error !'),
118 _('There is no income account defined as global property.'))
119 res['account_id'] = account_id
120 if not res.get('account_id'):
121 raise osv.except_osv(_('Configuration Error !'),
122 _('There is no income account defined for this product: "%s" (id:%d)') % \
123 (wizard.product_id.name, wizard.product_id.id,))
125 # determine invoice amount
126 if wizard.amount <= 0.00:
127 raise osv.except_osv(_('Incorrect Data'),
128 _('The value of Advance Amount must be positive.'))
129 if wizard.advance_payment_method == 'percentage':
130 inv_amount = sale.amount_total * wizard.amount / 100
131 if not res.get('name'):
132 res['name'] = _("Advance of %s %%") % (wizard.amount)
134 inv_amount = wizard.amount
135 if not res.get('name'):
136 #TODO: should find a way to call formatLang() from rml_parse
137 symbol = sale.pricelist_id.currency_id.symbol
138 if sale.pricelist_id.currency_id.position == 'after':
139 res['name'] = _("Advance of %s %s") % (inv_amount, symbol)
141 res['name'] = _("Advance of %s %s") % (symbol, inv_amount)
144 if res.get('invoice_line_tax_id'):
145 res['invoice_line_tax_id'] = [(6, 0, res.get('invoice_line_tax_id'))]
147 res['invoice_line_tax_id'] = False
151 'name': res.get('name'),
152 'account_id': res['account_id'],
153 'price_unit': inv_amount,
154 'quantity': wizard.qtty or 1.0,
156 'uos_id': res.get('uos_id', False),
157 'product_id': wizard.product_id.id,
158 'invoice_line_tax_id': res.get('invoice_line_tax_id'),
159 'account_analytic_id': sale.project_id.id or False,
162 'name': sale.client_order_ref or sale.name,
164 'type': 'out_invoice',
166 'account_id': sale.partner_id.property_account_receivable.id,
167 'partner_id': sale.partner_id.id,
168 'invoice_line': [(0, 0, inv_line_values)],
169 'currency_id': sale.pricelist_id.currency_id.id,
171 'payment_term': sale.payment_term.id,
172 'fiscal_position': sale.fiscal_position.id or sale.partner_id.property_account_position.id
174 inv_id = inv_obj.create(cr, uid, inv_values, context=context)
175 inv_obj.button_reset_taxes(cr, uid, [inv_id], context=context)
176 inv_ids.append(inv_id)
178 # add the invoice to the sale order's invoices
179 sale.write({'invoice_ids': [(4, inv_id)]})
181 # If invoice on picking: add the cost on the SO
182 # If not, the advance will be deduced when generating the final invoice
183 if sale.order_policy == 'picking':
186 'name': res.get('name'),
187 'price_unit': -inv_amount,
188 'product_uom_qty': wizard.qtty or 1.0,
189 'product_uos_qty': wizard.qtty or 1.0,
190 'product_uos': res.get('uos_id', False),
191 'product_uom': res.get('uom_id', False),
192 'product_id': wizard.product_id.id or False,
194 'tax_id': res.get('invoice_line_tax_id'),
196 self.pool.get('sale.order.line').create(cr, uid, vals, context=context)
198 if context.get('open_invoices', False):
199 return self.open_invoices( cr, uid, ids, inv_ids, context=context)
200 return {'type': 'ir.actions.act_window_close'}
202 def open_invoices(self, cr, uid, ids, invoice_ids, context=None):
203 """ open a view on one of the given invoice_ids """
204 ir_model_data = self.pool.get('ir.model.data')
205 form_res = ir_model_data.get_object_reference(cr, uid, 'account', 'invoice_form')
206 form_id = form_res and form_res[1] or False
207 tree_res = ir_model_data.get_object_reference(cr, uid, 'account', 'invoice_tree')
208 tree_id = tree_res and tree_res[1] or False
211 'name': _('Advance Invoice'),
213 'view_mode': 'form,tree',
214 'res_model': 'account.invoice',
215 'res_id': invoice_ids[0],
217 'views': [(form_id, 'form'), (tree_id, 'tree')],
218 'context': "{'type': 'out_invoice'}",
219 'type': 'ir.actions.act_window',
222 sale_advance_payment_inv()
224 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: