Browse Source

Updated

master
cybroodoo 9 years ago
parent
commit
dbdeb4b7be
  1. 76
      sale_discount_total/models/account_invoice.py
  2. 58
      sale_discount_total/models/sale.py
  3. BIN
      sale_discount_total/static/description/Discount_inv_amnt.png
  4. BIN
      sale_discount_total/static/description/Discount_so_perc.png
  5. 10
      sale_discount_total/views/account_invoice_view.xml
  6. 2
      sale_discount_total/views/sale_view.xml

76
sale_discount_total/models/account_invoice.py

@ -9,13 +9,14 @@ class AccountInvoice(models.Model):
@api.one @api.one
@api.depends('invoice_line.price_subtotal', 'tax_line.amount') @api.depends('invoice_line.price_subtotal', 'tax_line.amount')
def _compute_amount(self): def _compute_amount(self):
disc = 0.0
for inv in self:
for line in inv.invoice_line:
disc += (line.quantity * line.price_unit) * line.discount / 100
self.amount_untaxed = sum(line.price_subtotal for line in self.invoice_line) self.amount_untaxed = sum(line.price_subtotal for line in self.invoice_line)
self.amount_tax = sum(line.amount for line in self.tax_line) self.amount_tax = sum(line.amount for line in self.tax_line)
if self.discount_type == 'percent': self.amount_discount = round(disc)
self.amount_discount = ((self.amount_untaxed + self.amount_tax) * self.discount_rate) / 100 self.amount_total = round(self.amount_untaxed + self.amount_tax)
elif self.discount_type == 'amount':
self.amount_discount = self.discount_rate
self.amount_total = self.amount_untaxed + self.amount_tax - self.amount_discount
discount_type = fields.Selection([('percent', 'Percentage'), ('amount', 'Amount')], 'Discount Type', readonly=True, discount_type = fields.Selection([('percent', 'Percentage'), ('amount', 'Amount')], 'Discount Type', readonly=True,
states={'draft': [('readonly', False)]}) states={'draft': [('readonly', False)]})
@ -30,23 +31,38 @@ class AccountInvoice(models.Model):
amount_total = fields.Float(string='Total', digits=dp.get_precision('Account'), amount_total = fields.Float(string='Total', digits=dp.get_precision('Account'),
readonly=True, compute='_compute_amount') readonly=True, compute='_compute_amount')
@api.multi
def compute_discount(self, discount):
print "^^^^^^^^^^^^^^^^^^^^^compute_discount"
for inv in self:
val1 = val2 = 0.0
disc_amnt = 0.0
val2 = sum(line.amount for line in self.tax_line)
for line in inv.invoice_line:
val1 += (line.quantity * line.price_unit)
line.discount = discount
disc_amnt += (line.quantity * line.price_unit) * discount / 100
total = val1 + val2 - disc_amnt
self.amount_discount = round(disc_amnt)
self.amount_tax = round(val2)
self.amount_total = round(total)
@api.onchange('discount_type', 'discount_rate') @api.onchange('discount_type', 'discount_rate')
def compute_discount(self): def supply_rate(self):
for inv in self: for inv in self:
amount = sum(line.price_subtotal for line in self.invoice_line) amount = sum(line.price_subtotal for line in self.invoice_line)
tax = sum(line.amount for line in self.tax_line) tax = sum(line.amount for line in self.tax_line)
if inv.discount_type == 'percent': if inv.discount_type == 'percent':
if inv.discount_rate == 100: self.compute_discount(inv.discount_rate)
disc_amnt = amount + tax
else:
disc_amnt = (amount + tax) * inv.discount_rate / 100
total = amount + tax - disc_amnt
self.amount_discount = disc_amnt
self.amount_total = total
else: else:
total = (amount + tax) - inv.discount_rate total = 0.0
self.amount_discount = inv.discount_rate discount = 0.0
self.amount_total = total for line in inv.invoice_line:
total += (line.quantity * line.price_unit)
if inv.discount_rate != 0:
discount = (inv.discount_rate / total) * 100
self.compute_discount(discount)
@api.model @api.model
def _prepare_refund(self, invoice, date=None, period_id=None, description=None, journal_id=None): def _prepare_refund(self, invoice, date=None, period_id=None, description=None, journal_id=None):
@ -58,31 +74,3 @@ class AccountInvoice(models.Model):
}) })
return res return res
class invoice_line(osv.Model):
_inherit = 'account.invoice.line'
def move_line_get(self, cr, uid, invoice_id, context=None):
res = super(invoice_line, self).move_line_get(cr, uid, invoice_id, context=context)
inv = self.pool.get('account.invoice').browse(cr, uid, invoice_id, context=context)
if inv.type in ('out_invoice', 'out_refund') and inv.discount_type:
prop = self.pool.get('ir.property').get(cr, uid, 'property_account_income_categ', 'product.category',
context=context)
prop_id = prop and prop.id or False
account_id = self.pool.get('account.fiscal.position').map_account(cr, uid, inv.fiscal_position or False,
prop_id)
sign = -1
res.append({
'name': 'Discount',
'price_unit': sign * inv.amount_discount,
'quantity': 1,
'price': sign * inv.amount_discount,
'account_id': account_id,
'product_id': False,
'uos_id': False,
'account_analytic_id': False,
'taxes': False,
})
return res

58
sale_discount_total/models/sale.py

@ -6,9 +6,11 @@ import openerp.addons.decimal_precision as dp
class SaleOrder(osv.Model): class SaleOrder(osv.Model):
_inherit = 'sale.order' _inherit = 'sale.order'
def _amount_all(self, cr, uid, ids, field_name, arg, context=None): def _amount_all(self, cr, uid, ids, field_name, arg, context=None):
cur_obj = self.pool.get('res.currency') cur_obj = self.pool.get('res.currency')
res = {} res = {}
print "_amount_all"
for order in self.browse(cr, uid, ids, context=context): for order in self.browse(cr, uid, ids, context=context):
res[order.id] = { res[order.id] = {
'amount_untaxed': 0.0, 'amount_untaxed': 0.0,
@ -16,21 +18,17 @@ class SaleOrder(osv.Model):
'amount_tax': 0.0, 'amount_tax': 0.0,
'amount_total': 0.0, 'amount_total': 0.0,
} }
val = val1 = val2 = 0.0
cur = order.pricelist_id.currency_id cur = order.pricelist_id.currency_id
val1 = val2 = val3 = 0.0
for line in order.order_line: for line in order.order_line:
val1 += line.price_subtotal val1 += line.price_subtotal
val += self._amount_line_tax(cr, uid, line, context=context) val2 += self._amount_line_tax(cr, uid, line, context=context)
if order.discount_type == 'amount': val3 += (line.product_uom_qty * line.price_unit) * line.discount / 100
val2 = order.discount_rate total = val1 + val2
elif order.discount_type == 'percent': res[order.id]['amount_untaxed'] = round(cur_obj.round(cr, uid, cur, val1))
val2 = ((val1 + val) * order.discount_rate) / 100 res[order.id]['amount_tax'] = round(cur_obj.round(cr, uid, cur, val2))
res[order.id]['amount_tax'] = cur_obj.round(cr, uid, cur, val) res[order.id]['amount_discount'] = round(cur_obj.round(cr, uid, cur, val3))
res[order.id]['amount_untaxed'] = cur_obj.round(cr, uid, cur, val1) res[order.id]['amount_total'] = round(cur_obj.round(cr, uid, cur, total))
res[order.id]['amount_discount'] = cur_obj.round(cr, uid, cur, val2)
res[order.id]['amount_total'] = res[order.id]['amount_untaxed'] + res[order.id]['amount_tax'] - \
res[order.id]['amount_discount']
return res
def _get_order(self, cr, uid, ids, context=None): def _get_order(self, cr, uid, ids, context=None):
result = {} result = {}
@ -60,27 +58,33 @@ class SaleOrder(osv.Model):
'discount_type': 'percent', 'discount_type': 'percent',
} }
@api.onchange('discount_type', 'discount_rate') @api.multi
def compute_discount(self): def compute_discount(self, discount):
for order in self: for order in self:
val1 = val2 = 0.0 val1 = val2 = 0.0
disc_amnt = 0.0
for line in order.order_line: for line in order.order_line:
val1 += line.price_subtotal val1 += (line.product_uom_qty * line.price_unit)
line.discount = discount
val2 += self._amount_line_tax(line) val2 += self._amount_line_tax(line)
disc_amnt += (line.product_uom_qty * line.price_unit * line.discount)/100
total = val1 + val2 - disc_amnt
self.currency_id = order.pricelist_id.currency_id
self.amount_discount = round(disc_amnt)
self.amount_tax = round(val2)
self.amount_total = round(total)
@api.onchange('discount_type', 'discount_rate')
def supply_rate(self):
for order in self:
if order.discount_type == 'percent': if order.discount_type == 'percent':
if order.discount_rate == 100: self.compute_discount(order.discount_rate)
disc_amnt = val1 + val2
else:
disc_amnt = (val1 + val2) * order.discount_rate / 100
total = val1 + val2 - disc_amnt
self.currency_id = order.pricelist_id.currency_id
self.amount_discount = disc_amnt
self.amount_total = total
else: else:
total = (val1 + val2) - order.discount_rate total = 0.0
self.currency_id = order.pricelist_id.currency_id for line in order.order_line:
self.amount_discount = order.discount_rate total += (line.product_uom_qty * line.price_unit)
self.amount_total = total discount = (order.discount_rate / total) * 100
self.compute_discount(discount)
def _prepare_invoice(self, cr, uid, order, lines, context=None): def _prepare_invoice(self, cr, uid, order, lines, context=None):
invoice_vals = super(SaleOrder, self)._prepare_invoice(cr, uid, order, lines, context=context) invoice_vals = super(SaleOrder, self)._prepare_invoice(cr, uid, order, lines, context=context)

BIN
sale_discount_total/static/description/Discount_inv_amnt.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 51 KiB

After

Width:  |  Height:  |  Size: 50 KiB

BIN
sale_discount_total/static/description/Discount_so_perc.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

After

Width:  |  Height:  |  Size: 42 KiB

10
sale_discount_total/views/account_invoice_view.xml

@ -14,14 +14,14 @@
</group> </group>
<group class="oe_subtotal_footer oe_right"> <group class="oe_subtotal_footer oe_right">
<field name="amount_untaxed" widget="monetary" options="{'currency_field': 'currency_id'}"/> <field name="amount_untaxed" widget="monetary" options="{'currency_field': 'currency_id'}"/>
<div> <field name="amount_discount" widget='monetary' options="{'currency_field': 'currency_id'}"/>
<label for="amount_tax"/> <div>
<button name="button_reset_taxes" states="draft,proforma2" <label for="amount_tax"/>
<button name="button_reset_taxes" states="draft,proforma2"
string="(update)" class="oe_link oe_edit_only" string="(update)" class="oe_link oe_edit_only"
type="object" help="Recompute taxes and total"/> type="object" help="Recompute taxes and total"/>
</div> </div>
<field name="amount_tax" nolabel="1" widget="monetary" options="{'currency_field': 'currency_id'}"/> <field name="amount_tax" nolabel="1" widget="monetary" options="{'currency_field': 'currency_id'}"/>
<field name="amount_discount" widget='monetary' options="{'currency_field': 'currency_id'}"/>
<field name="amount_total" class="oe_subtotal_footer_separator" widget="monetary" options="{'currency_field': 'currency_id'}"/> <field name="amount_total" class="oe_subtotal_footer_separator" widget="monetary" options="{'currency_field': 'currency_id'}"/>
<field name="residual" groups="account.group_account_user" widget="monetary" options="{'currency_field': 'currency_id'}"/> <field name="residual" groups="account.group_account_user" widget="monetary" options="{'currency_field': 'currency_id'}"/>
<field name="reconciled" invisible="1"/> <field name="reconciled" invisible="1"/>

2
sale_discount_total/views/sale_view.xml

@ -14,8 +14,8 @@
</group> </group>
<group class="oe_subtotal_footer oe_right" colspan="2" name="sale_total"> <group class="oe_subtotal_footer oe_right" colspan="2" name="sale_total">
<field name="amount_untaxed" widget='monetary' options="{'currency_field': 'currency_id'}"/> <field name="amount_untaxed" widget='monetary' options="{'currency_field': 'currency_id'}"/>
<field name="amount_tax" widget='monetary' options="{'currency_field': 'currency_id'}"/>
<field name="amount_discount" widget='monetary' options="{'currency_field': 'currency_id'}"/> <field name="amount_discount" widget='monetary' options="{'currency_field': 'currency_id'}"/>
<field name="amount_tax" widget='monetary' options="{'currency_field': 'currency_id'}"/>
<div class="oe_subtotal_footer_separator oe_inline"> <div class="oe_subtotal_footer_separator oe_inline">
<label for="amount_total" /> <label for="amount_total" />
<button name="button_dummy" <button name="button_dummy"

Loading…
Cancel
Save