Fix credit compute for sales with partial payments

This commit is contained in:
Oscar 2021-09-09 09:33:32 -05:00
parent 28149fc9bb
commit b5ff8a47ed
2 changed files with 45 additions and 24 deletions

View file

@ -15,9 +15,11 @@ from trytond.wizard import (
)
from trytond.report import Report
from trytond.i18n import gettext
from .exceptions import (ProductMissingTaxError, ImportSalesError,
SaleDeleteError, SaleForceDraftError, SaleDeviceError, DraftStatementError,
PartyMissingAccount)
from .exceptions import (
ProductMissingTaxError, ImportSalesError, SaleDeleteError,
SaleForceDraftError, SaleDeviceError, DraftStatementError,
PartyMissingAccount
)
from trytond.modules.sale.exceptions import SaleValidationError
_ZERO = Decimal('0.00')

61
shop.py
View file

@ -177,45 +177,64 @@ class ShopDailySummaryReport(Report):
numbers = []
categories = {}
discounts = {}
payments = {}
_payments = {}
total_discount = []
total_payments = []
for sale in sales:
payments = sale.payments
device_id = None
if sale.sale_device:
device_id = sale.sale_device.id
if sale.total_amount <= 0:
continue
for invoice in sale.invoices:
if not invoice.number or invoice.total_amount <= 0 or not sale.sale_device:
if not invoice.number or invoice.total_amount <= 0 or not device_id:
continue
numbers.append(invoice.number)
devices[sale.sale_device.id]['count_invoices'] += 1
devices[sale.sale_device.id]['untaxed_amount'].append(invoice.untaxed_amount)
devices[sale.sale_device.id]['tax_amount'].append(invoice.tax_amount)
devices[sale.sale_device.id]['total_amount'].append(invoice.total_amount)
devices[device_id]['count_invoices'] += 1
devices[device_id]['untaxed_amount'].append(invoice.untaxed_amount)
devices[device_id]['tax_amount'].append(invoice.tax_amount)
devices[device_id]['total_amount'].append(invoice.total_amount)
untaxed_amount.append(invoice.untaxed_amount)
tax_amount.append(invoice.tax_amount)
total_amount.append(invoice.total_amount)
if sale.payments:
for payment in sale.payments:
if payment.statement.journal.kind and \
payment.statement.journal.kind in ['cash', 'credit', 'electronic']:
kind = payment.statement.journal.kind
else:
if payments:
amount_by_sale = []
for payment in payments:
kind = payment.statement.journal.kind
amount = payment.amount
amount_by_sale.append(amount)
if kind not in ['cash', 'credit', 'electronic']:
kind = 'other'
devices[sale.sale_device.id][kind].append(payment.amount)
payment_modes[kind].append(payment.amount)
devices[device_id][kind].append(amount)
payment_modes[kind].append(amount)
journal = payment.statement.journal
if journal.id not in payments.keys():
payments[journal.id] = {
try:
_payments[journal.id]['amount'].append(payment.amount)
except:
_payments[journal.id] = {
'name': journal.name,
'amount': [payment.amount],
'amount': [amount],
}
else:
payments[journal.id]['amount'].append(payment.amount)
total_payments.append(payment.amount)
amount_to_pay = invoice.amount_to_pay
if amount_to_pay > 0:
print('credito con pagos ...', invoice.number)
# THIS MUST WORKS IN FUTURE WITH ADD PAYMENT INSTATEMENT TO INVOICE
# devices[device_id]['credit'].append(amount_to_pay)
# payment_modes['credit'].append(amount_to_pay)
# FIX TEMPORAL
inv_balance = invoice.total_amount - sum(amount_by_sale)
devices[device_id]['credit'].append(inv_balance)
payment_modes['credit'].append(inv_balance)
else:
devices[sale.sale_device.id]['credit'].append(invoice.total_amount)
payment_modes['credit'].append(invoice.total_amount)
payment_modes['credit'].append(invoice.amount_to_pay)
for line in invoice.lines:
category_id = '0'
@ -291,7 +310,7 @@ class ShopDailySummaryReport(Report):
report_context['sum_total_amount'] = sum(total_amount)
report_context['discounts'] = discounts.values()
report_context['total_discount'] = sum(total_discount)
report_context['payments'] = payments.values()
report_context['payments'] = _payments.values()
report_context['total_payments'] = sum(total_payments)
report_context['sum_cash'] = sum(payment_modes['cash'])
report_context['sum_credit'] = sum(payment_modes['credit'])