286 lines
11 KiB
Python
286 lines
11 KiB
Python
|
# This file is part of Tryton. The COPYRIGHT file at the top level of
|
||
|
# this repository contains the full copyright notices and license terms.
|
||
|
import time
|
||
|
from decimal import Decimal
|
||
|
import requests
|
||
|
import simplejson as json
|
||
|
from datetime import datetime, date
|
||
|
from trytond.model import ModelView, fields
|
||
|
from trytond.pool import PoolMeta, Pool
|
||
|
from trytond.transaction import Transaction
|
||
|
from trytond.pyson import Bool, Eval, Or, Not
|
||
|
from trytond.wizard import (
|
||
|
Wizard, StateView, StateAction, StateReport, StateTransition, Button)
|
||
|
from trytond.report import Report
|
||
|
|
||
|
__all__ = [
|
||
|
'SaleIncomeDailyStart', 'SaleIncomeDaily',
|
||
|
'SaleIncomeDailyReport',
|
||
|
]
|
||
|
|
||
|
_ZERO = Decimal('0.00')
|
||
|
|
||
|
|
||
|
class Sale(metaclass=PoolMeta):
|
||
|
__name__ = 'sale.sale'
|
||
|
|
||
|
|
||
|
@classmethod
|
||
|
def get_records_income_daily_report(cls, args, ctx):
|
||
|
report_context = {}
|
||
|
pool = Pool()
|
||
|
Report = pool._pool['TODOREPUESTOS']['report']['sale_pos.report_sale_detailed']
|
||
|
# Report = pool._pool['TODOREPUESTOS']['report']['sale_pos.sale_income_daily_report']
|
||
|
report_context = Report.get_context({}, args['data'])
|
||
|
# data = args['data']
|
||
|
# pool = Pool()
|
||
|
# Invoice = pool.get('account.invoice')
|
||
|
# Voucher = pool.get('account.voucher')
|
||
|
# Sale = pool.get('sale.sale')
|
||
|
# Company = pool.get('company.company')
|
||
|
# company = Company(data['company'])
|
||
|
# Statement = pool.get('account.statement')
|
||
|
# Shop = pool.get('sale.shop')
|
||
|
# User = pool.get('res.user')
|
||
|
#
|
||
|
# shop = Shop(data['shop'])
|
||
|
# statements = Statement.search([
|
||
|
# ('date', '=', data['date']),
|
||
|
# # ('create_uid', '=', data['user']),
|
||
|
# ])
|
||
|
# user_id = Transaction().user
|
||
|
# user = User(user_id)
|
||
|
# records = []
|
||
|
# advances = []
|
||
|
# statements_ = []
|
||
|
# total_advances = []
|
||
|
# advances_cash = []
|
||
|
# advances_electronic = []
|
||
|
# total_statements = []
|
||
|
# statement_cash = []
|
||
|
# statement_electronic = []
|
||
|
#
|
||
|
# for statement in statements:
|
||
|
# st_amount = sum(l.amount for l in statement.lines)
|
||
|
# statements_.append({
|
||
|
# 'id': statement.id,
|
||
|
# 'sale_device': statement.sale_device.name,
|
||
|
# 'journal': statement.journal.name,
|
||
|
# 'turn': statement.turn,
|
||
|
# 'total_amount': st_amount,
|
||
|
# 'state': statement.state,
|
||
|
# })
|
||
|
# total_statements.append(st_amount)
|
||
|
#
|
||
|
# for l in statement.lines:
|
||
|
# if l.statement.journal.kind == 'cash':
|
||
|
# statement_cash.append(l.amount)
|
||
|
# else:
|
||
|
# statement_electronic.append(l.amount)
|
||
|
#
|
||
|
# sales = Sale.search([
|
||
|
# ('sale_date', '=', data['date']),
|
||
|
# ('vouchers', '!=', None),
|
||
|
# ])
|
||
|
# advances_voucher = []
|
||
|
# voucher_ids = []
|
||
|
# for sale in sales:
|
||
|
# for v in sale.vouchers:
|
||
|
# cash, electronic = 0,0
|
||
|
# if v.payment_mode.payment_type == 'cash':
|
||
|
# advances_cash.append(v.amount_to_pay)
|
||
|
# cash = v.amount_to_pay
|
||
|
# else:
|
||
|
# electronic = v.amount_to_pay
|
||
|
# advances_electronic.append(v.amount_to_pay)
|
||
|
# voucher_ids.append(v.id)
|
||
|
# advances_voucher.append(v.amount_to_pay)
|
||
|
# advances.append({
|
||
|
# 'number': v.number,
|
||
|
# 'reference': sale.number,
|
||
|
# 'party': v.party.name,
|
||
|
# 'total_amount': v.amount_to_pay,
|
||
|
# 'payment_mode': v.payment_mode.name,
|
||
|
# 'cash': cash,
|
||
|
# 'electronic': electronic,
|
||
|
# })
|
||
|
# total_advances.append(v.amount_to_pay)
|
||
|
#
|
||
|
# dom_vouchers = [
|
||
|
# ('date', '=', data['date']),
|
||
|
# ('state', '!=', 'draft')
|
||
|
# ]
|
||
|
# if voucher_ids:
|
||
|
# dom_vouchers.append(
|
||
|
# ('id', 'not in', voucher_ids)
|
||
|
# )
|
||
|
# vouchers = Voucher.search(dom_vouchers)
|
||
|
#
|
||
|
# for v in vouchers:
|
||
|
# cash = 0
|
||
|
# electronic = 0
|
||
|
# for l in v.lines:
|
||
|
# if v.payment_mode.payment_type == 'cash':
|
||
|
# advances_cash.append(l.amount)
|
||
|
# cash = l.amount
|
||
|
# else:
|
||
|
# advances_electronic.append(l.amount)
|
||
|
# electronic = l.amount
|
||
|
# advances_voucher.append(l.amount)
|
||
|
# advances.append({
|
||
|
# 'number': l.voucher.number,
|
||
|
# 'reference': l.detail,
|
||
|
# 'party': l.party.name if l.party else l.voucher.party.name,
|
||
|
# 'total_amount': l.amount,
|
||
|
# 'payment_mode': l.voucher.payment_mode.name,
|
||
|
# 'cash': cash,
|
||
|
# 'electronic': electronic,
|
||
|
# })
|
||
|
# total_advances.append(l.amount)
|
||
|
#
|
||
|
# dom_ = [
|
||
|
# ('company', '=', data['company']),
|
||
|
# ('invoice_date', '=', data['date']),
|
||
|
# ('shop', '=', data['shop']),
|
||
|
# ('number', '!=', None),
|
||
|
# ('state', 'in', ['posted', 'paid', 'canceled']),
|
||
|
# ]
|
||
|
#
|
||
|
# invoices = Invoice.search(dom_, order=[('number', 'ASC')])
|
||
|
#
|
||
|
# invoices_number = []
|
||
|
# total_invoices_cash = []
|
||
|
# total_invoices_electronic = []
|
||
|
# total_invoices_credit = []
|
||
|
# total_invoices_paid = []
|
||
|
#
|
||
|
# total_invoices_amount = []
|
||
|
# for invoice in invoices:
|
||
|
# invoices_number.append(invoice.number)
|
||
|
# cash = 0
|
||
|
# electronic = 0
|
||
|
# credit = 0
|
||
|
# total_invoices_amount.append(invoice.total_amount)
|
||
|
# sale = invoice.sales[0]
|
||
|
# if not sale.payments:
|
||
|
# total_invoices_credit.append(invoice.amount_to_pay)
|
||
|
# credit = invoice.amount_to_pay
|
||
|
# else:
|
||
|
# for p in sale.payments:
|
||
|
# if p.statement.date == data['date']:
|
||
|
# if p.statement.journal.kind == 'cash':
|
||
|
# cash += p.amount
|
||
|
# total_invoices_cash.append(p.amount)
|
||
|
# else:
|
||
|
# electronic += p.amount
|
||
|
# total_invoices_electronic.append(p.amount)
|
||
|
# total_invoices_paid.append(p.amount)
|
||
|
#
|
||
|
# inv = {
|
||
|
# 'number': invoice.number,
|
||
|
# 'reference': invoice.reference,
|
||
|
# 'party': invoice.party.name,
|
||
|
# 'total_amount': invoice.total_amount,
|
||
|
# 'credit': credit,
|
||
|
# 'cash': cash,
|
||
|
# 'electronic': electronic,
|
||
|
# 'paid': cash + electronic,
|
||
|
# 'state': invoice.state_string,
|
||
|
# }
|
||
|
# records.append(inv)
|
||
|
#
|
||
|
# advances_cash_ = sum(advances_cash)
|
||
|
# advances_electronic_ = sum(advances_electronic)
|
||
|
# statement_cash_ = sum(statement_cash)
|
||
|
# statement_electronic_ = sum(statement_electronic)
|
||
|
#
|
||
|
# report_context['records'] = records
|
||
|
# report_context['advances'] = advances
|
||
|
# report_context['statements'] = statements_
|
||
|
# report_context['date'] = data['date']
|
||
|
# report_context['company'] = company.party.name
|
||
|
# report_context['shop'] = shop.name
|
||
|
# report_context['user'] = user.name
|
||
|
# report_context['print_date'] = datetime.now()
|
||
|
# report_context['statement_cash'] = statement_cash_
|
||
|
# report_context['statement_electronic'] = statement_electronic_
|
||
|
# report_context['total_invoices_amount'] = sum(total_invoices_amount)
|
||
|
# report_context['total_invoices_cash'] = sum(total_invoices_cash)
|
||
|
# report_context['total_invoices_electronic'] = sum(total_invoices_electronic)
|
||
|
# report_context['total_invoices_credit'] = sum(total_invoices_credit)
|
||
|
# report_context['total_invoices_paid'] = sum(total_invoices_paid)
|
||
|
# report_context['total_advances'] = sum(total_advances)
|
||
|
# report_context['advances_cash'] = advances_cash_
|
||
|
# report_context['advances_electronic'] = advances_electronic_
|
||
|
# report_context['advances_voucher'] = sum(advances_voucher)
|
||
|
# report_context['total_statements'] = sum(total_statements)
|
||
|
# report_context['start_invoice'] = min(invoices_number) if invoices_number else ''
|
||
|
# report_context['end_invoice'] = max(invoices_number) if invoices_number else ''
|
||
|
# report_context['total_cash'] = advances_cash_ + statement_cash_
|
||
|
# report_context['total_electronic'] = advances_electronic_ + statement_electronic_
|
||
|
return report_context
|
||
|
|
||
|
|
||
|
class SaleIncomeDailyStart(metaclass=PoolMeta):
|
||
|
__name__ = 'sale_pos.sale_income_daily.start'
|
||
|
|
||
|
sincronize = fields.Boolean('Syncronize')
|
||
|
|
||
|
|
||
|
class SaleIncomeDaily(metaclass=PoolMeta):
|
||
|
__name__ = 'sale_pos.sale_income_daily'
|
||
|
|
||
|
def do_print_(self, action):
|
||
|
action, data = super(SaleIncomeDaily, self).do_print_(action)
|
||
|
data['sincronize'] = self.start.sincronize,
|
||
|
return action, data
|
||
|
|
||
|
|
||
|
class SaleIncomeDailyReport(metaclass=PoolMeta):
|
||
|
__name__ = 'sale_pos.sale_income_daily_report'
|
||
|
|
||
|
@classmethod
|
||
|
def get_context(cls, records, data):
|
||
|
report_context = super(SaleIncomeDailyReport, cls).get_context(records, data)
|
||
|
Company = Pool().get('company.company')
|
||
|
company = Company(data['company'])
|
||
|
result = {}
|
||
|
sync = data['sincronize'][0]
|
||
|
if sync:
|
||
|
date_ = {
|
||
|
'__class__': 'date',
|
||
|
'year': data['date'].year,
|
||
|
'month': data['date'].month,
|
||
|
'day': data['date'].day,
|
||
|
}
|
||
|
data['date'] = date_
|
||
|
data['sincronize'] = [False]
|
||
|
data['shop'] = None
|
||
|
|
||
|
args = {
|
||
|
'records': records,
|
||
|
'data': data,
|
||
|
}
|
||
|
object_ = {
|
||
|
'database': '',
|
||
|
'report_name': 'sale_pos.sale_income_daily_report',
|
||
|
'args': args,
|
||
|
}
|
||
|
for sync in company.connection_companies:
|
||
|
object_['database'] = sync.database
|
||
|
api_ = sync.api_connection + '/'+ sync.database
|
||
|
route = api_ + '/' + 'report_context'
|
||
|
data_ = json.dumps(object_)
|
||
|
res = requests.get(route, data=data_)
|
||
|
result = res.json()
|
||
|
|
||
|
for k in result.keys():
|
||
|
if isinstance(result[k], (int, float, Decimal)):
|
||
|
report_context[k] += int(result[k])
|
||
|
elif isinstance(result[k], list):
|
||
|
report_context[k].extend(result[k])
|
||
|
if k == 'shop':
|
||
|
report_context[k] += result[k] + ', '
|
||
|
|
||
|
return report_context
|