def __action_compute(self, cr, uid, ids, context=None): (context, ids) = make_safe(context, ids) os_object = self.browse(cr, uid, ids, context)[0] initial_value = 0 final_value = 0 final_result = [] amount_line_per_account_id_tax = {} for line in os_object.invoice_line: invoice_line = self.pool.get('account.invoice.line').browse(cr, uid, line.id, context) if invoice_line.account_analytic_id: context['analytic_account'] = invoice_line.account_analytic_id.id if not line.is_discount: price = line.price_subtotal for tax in line.invoice_line_tax_id: if tax.price_include: price = line.price_unit * line.quantity break initial_value += price # line.price_unit * line.quantity key = sorted([x.id for x in line.invoice_line_tax_id] + [0]) key = ','.join(map(str, key)) if line.account_id.id not in amount_line_per_account_id_tax: amount_line_per_account_id_tax[line.account_id.id] = {} if key not in amount_line_per_account_id_tax[line.account_id.id]: amount_line_per_account_id_tax[line.account_id.id][key] = price else: amount_line_per_account_id_tax[line.account_id.id][key] += price res = "" (result, value) = self.pool.get('stage_discount.discount').generic_compute(cr, uid, os_object.discount_line_ids, os_object.is_rounded, os_object.currency_id, amount_line_per_account_id_tax, context) final_value += value final_result.extend(result) res = "{0}\n {1}".format(res, self.pool.get('stage_discount.discount').get_message(cr, uid, ids, result, context)) res = "Initial value {0} {1} \n End value {2}".format(initial_value, res, final_value) self.write(cr, uid, ids, {'test_note': res}, context) return final_result
def action_add_discount(self, cr, uid, ids, template_id=False, context=None): ''' Add to this invoice all the lines of a a template discount. These new lines will be a copy of the original ones. ''' (context, ids) = make_safe(context, ids) stage_discount_discount_model = self.pool.get('stage_discount.discount') stage_discount_discount_line_model = self.pool.get('stage_discount.discount_line') if not template_id: if not ids: return [] os_object = self.browse(cr, uid, ids[0], context) if not os_object.discount_template_id: return [] else: template_id = os_object.discount_template_id.id discount_obj = stage_discount_discount_model.browse(cr, uid, template_id, context) discount_line_ids = [] self.write(cr, uid, ids, {'is_rounded': discount_obj.is_rounded}) for line in discount_obj.discount_line_ids: invoice_id = False if ids: invoice_id = ids[0] new_line_id = stage_discount_discount_line_model.copy(cr, uid, line.id, {'discount_id': False, 'invoice_id': invoice_id}, context) discount_line_ids.append(new_line_id) return discount_line_ids
def invoice_validate(self, cr, uid, ids, context=None): self.action_apply_discount(cr, uid, ids, context) result = super(account_invoice_ext, self).invoice_validate(cr, uid, ids, context) (context, ids) = make_safe(context, ids) account_invoice = self.browse(cr, uid, ids[0], context) if account_invoice.amount_total != account_invoice.residual: raise osv.except_osv(_('Usage Error'), _('Please update taxes before continuing.')) else: return result
def action_test_the_value(self, cr, uid, ids, context=None): ''' Test action. Will take the test values and show the results. ''' (context, ids) = make_safe(context, ids) var_id = ids[0] os_object = self.browse(cr, uid, var_id, context) test_tax_ids = [0] + [x.id for x in os_object.test_tax_ids] test_tax_key = ",".join(map(str, test_tax_ids)) (result, final_quantity) = self.compute(cr, uid, var_id, {'0': {test_tax_key: os_object.test_value}}, context) res = "Initial value {0} {1} \n End value {2}".format(os_object.test_value, self.get_message(cr, uid, ids, result, context), final_quantity) self.write(cr, uid, var_id, {'test_note': res}, context) return res
def action_add_template(self, cr, uid, ids, template_id, discount_line, context=None): (context, ids) = make_safe(context, ids) discount_line = self.resolve_2many_commands(cr, uid, 'discount_line_ids', discount_line, context=context) discount_line = [x['id'] for x in discount_line] discount_line_ids = self.action_add_discount(cr, uid, ids, template_id, context) if ids: os_object = self.browse(cr, uid, ids, context)[0] os_object.write({'discount_line_ids': [(2, x) for x in discount_line]}) os_object.refresh() return {'value': { 'discount_template_id': False, 'discount_line_ids': [(4, x.id) for x in os_object.discount_line_ids] }} else: return {'value': { 'discount_template_id': False, 'discount_line_ids': [(4, x) for x in discount_line_ids] + [(2, x) for x in discount_line] }}
def get_discount_template(self, cr, uid, ids, context=None): ''' Returns an integer with the id of the discount template. If the output is -1 then this partner does not have a default discount template. ''' (context, ids) = make_safe(context, ids) partner = self.browse(cr, uid, ids, context)[0] if partner.use_parent_discount: if partner.parent_id: return self.get_discount_template(cr, uid, partner.parent_id.id, context) else: # use_parent_discount flag is set but no parent is defined return -1 else: if partner.discount_template_id: return partner.discount_template_id.id else: return -1
def compute(self, cr, uid, var_id, amount_line_per_tax, context=None): (context, ids) = make_safe(context, var_id) os_object = self.browse(cr, uid, ids, context)[0] user = self.pool.get('res.users').browse(cr, uid, uid, context=context) return self.generic_compute(cr, uid, os_object.discount_line_ids, os_object.is_rounded, user.company_id.currency_id, amount_line_per_tax, context)