add option post global charges
This commit is contained in:
parent
965025003f
commit
f938034376
|
@ -9,6 +9,7 @@ from . import invoice
|
|||
from . import api_log
|
||||
from . import product
|
||||
from . import voucher
|
||||
from . import purchase
|
||||
|
||||
|
||||
def register():
|
||||
|
@ -29,6 +30,7 @@ def register():
|
|||
product.Conservation,
|
||||
product.Template,
|
||||
voucher.Voucher,
|
||||
purchase.Purchase,
|
||||
module='laboratory', type_='model')
|
||||
Pool.register(
|
||||
service_order.CreateInvoice,
|
||||
|
|
|
@ -24,6 +24,8 @@ class Configuration(ModelSQL, ModelView):
|
|||
'Cuota Moderadora', domain=[('salable', '=', True)])
|
||||
delivery_product = fields.Many2One('product.product', 'Product Delivery',
|
||||
domain=[('salable', '=', True)])
|
||||
account_discount = fields.Many2One('account.account', "Account Discount")
|
||||
account_courtesy = fields.Many2One('account.account', "Account Courtesy")
|
||||
|
||||
@staticmethod
|
||||
def default_company():
|
||||
|
|
|
@ -6,6 +6,9 @@ this repository contains the full copyright notices and license terms. -->
|
|||
<record model="ir.message" id="msg_missing_payment_term">
|
||||
<field name="text">Missing payment term customer for party with document "%s"!</field>
|
||||
</record>
|
||||
<record model="ir.message" id="missing_account_discount">
|
||||
<field name="text">Missing account discount in configuration</field>
|
||||
</record>
|
||||
|
||||
</data>
|
||||
</tryton>
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
from trytond.pool import PoolMeta
|
||||
from itertools import chain
|
||||
|
||||
|
||||
class Purchase(metaclass=PoolMeta):
|
||||
__name__ = 'purchase.purchase'
|
||||
|
||||
def create_invoice(self):
|
||||
'Create an invoice for the purchase and return it'
|
||||
|
||||
if self.invoice_method == 'manual':
|
||||
return
|
||||
|
||||
goods_to_invoice = []
|
||||
line_goods = []
|
||||
invoice_lines = []
|
||||
for line in self.lines:
|
||||
invoice_line = line.get_invoice_line()
|
||||
if line.product.type == 'goods':
|
||||
line_goods.append(line.id)
|
||||
if invoice_line:
|
||||
goods_to_invoice.append(line.id)
|
||||
invoice_lines.append(invoice_line)
|
||||
invoice_lines = list(chain(*invoice_lines))
|
||||
if not invoice_lines or self.invoice_method == 'shipment' and len(line_goods) > 0 and len(goods_to_invoice) <= 0:
|
||||
return
|
||||
|
||||
invoice = self._get_invoice_purchase()
|
||||
if getattr(invoice, 'lines', None):
|
||||
invoice_lines = list(invoice.lines) + invoice_lines
|
||||
invoice.lines = invoice_lines
|
||||
invoice.save()
|
||||
invoice.update_taxes()
|
||||
self.copy_resources_to(invoice)
|
||||
return invoice
|
|
@ -177,12 +177,12 @@ class Order(Workflow, ModelSQL, ModelView):
|
|||
record = args
|
||||
pool = Pool()
|
||||
Invoice = pool.get('account.invoice')
|
||||
invoice, = Invoice.search(['reference', '=', record.number])
|
||||
if invoice and not record.invoice or not record.copago_invoice:
|
||||
invoices = Invoice.search(['reference', '=', record.number])
|
||||
if invoices and (not record.invoice or not record.copago_invoice):
|
||||
if record.copago:
|
||||
record.copago_invoice = invoice
|
||||
record.copago_invoice = invoices[0]
|
||||
else:
|
||||
record.invoice = invoice
|
||||
record.invoice = invoices[0]
|
||||
record.save()
|
||||
|
||||
if not record.invoice and not record.copago_invoice:
|
||||
|
@ -334,6 +334,8 @@ class Order(Workflow, ModelSQL, ModelView):
|
|||
pool = Pool()
|
||||
Invoice = pool.get('account.invoice')
|
||||
Party = pool.get('party.party')
|
||||
Configuration = pool.get('laboratory.configuration')
|
||||
config = Configuration(1)
|
||||
|
||||
_lines = []
|
||||
if self.copago or self.copago == 0:
|
||||
|
@ -348,29 +350,44 @@ class Order(Workflow, ModelSQL, ModelView):
|
|||
payment_term = self.payment_term.id
|
||||
|
||||
# Discount rate
|
||||
_charges = []
|
||||
disc_rate = None
|
||||
discount_amount = self.discount_amount
|
||||
if discount_amount:
|
||||
disc_rate = 1 - float(discount_amount / self.total_amount)
|
||||
if not config.account_discount:
|
||||
raise UserError('missing_account_discount')
|
||||
account = config.account_discount
|
||||
if discount_amount == self.total_amount:
|
||||
disc_rate = 100
|
||||
else:
|
||||
disc_rate = Decimal(str(round(float(discount_amount / self.total_amount),2)))
|
||||
_charges += [{
|
||||
'base_amount': self.total_amount,
|
||||
'amount': discount_amount,
|
||||
'description': 'Descuento comercial',
|
||||
'charge_percentage': disc_rate,
|
||||
'charge_concept': '00',
|
||||
'account': account
|
||||
}]
|
||||
for line in self.lines:
|
||||
if disc_rate:
|
||||
if len(self.lines) == 1:
|
||||
discount = discount_amount / line.quantity
|
||||
line.unit_price = line.unit_price - discount
|
||||
else:
|
||||
unit_price = int(float(line.unit_price) * disc_rate)
|
||||
amount = (line.unit_price - unit_price) * line.quantity
|
||||
if (discount_amount - amount) <= 1:
|
||||
discount = float(discount_amount) / line.quantity
|
||||
line.unit_price = float(line.unit_price) - discount
|
||||
discount_amount = float(
|
||||
discount_amount) - (discount * line.quantity)
|
||||
else:
|
||||
line.unit_price = unit_price
|
||||
discount_amount = discount_amount - amount
|
||||
|
||||
_line = self.get_line_order(line=line)
|
||||
_lines.append(_line)
|
||||
# if disc_rate:
|
||||
# if len(self.lines) == 1:
|
||||
# discount = discount_amount / line.quantity
|
||||
# line.unit_price = line.unit_price - discount
|
||||
# else:
|
||||
# unit_price = int(float(line.unit_price) * disc_rate)
|
||||
# amount = (line.unit_price - unit_price) * line.quantity
|
||||
# if (discount_amount - amount) <= 1:
|
||||
# discount = float(discount_amount) / line.quantity
|
||||
# line.unit_price = float(line.unit_price) - discount
|
||||
# discount_amount = float(
|
||||
# discount_amount) - (discount * line.quantity)
|
||||
# else:
|
||||
# line.unit_price = unit_price
|
||||
# discount_amount = discount_amount - amount
|
||||
|
||||
|
||||
if self.delivery and self.delivery > 0:
|
||||
_line = self.get_line_order(type='delivery_product')
|
||||
|
@ -396,7 +413,8 @@ class Order(Workflow, ModelSQL, ModelView):
|
|||
'type': 'out',
|
||||
'journal': journal.id,
|
||||
'account': party.account_receivable_used.id,
|
||||
'lines': [('create', _lines)]
|
||||
'lines': [('create', _lines)],
|
||||
'charges': [('create', _charges)]
|
||||
}
|
||||
invoice, = Invoice.create([data])
|
||||
if self.copago or self.copago == 0:
|
||||
|
|
|
@ -14,4 +14,8 @@ this repository contains the full copyright notices and license terms. -->
|
|||
<field name="cuota_moderadora_product"/>
|
||||
<label name="delivery_product"/>
|
||||
<field name="delivery_product"/>
|
||||
<label name="account_discount"/>
|
||||
<field name="account_discount"/>
|
||||
<label name="account_courtesy"/>
|
||||
<field name="account_courtesy"/>
|
||||
</form>
|
||||
|
|
Loading…
Reference in New Issue