2022-11-17 18:34:18 +01:00
|
|
|
# This file is part of Tryton. The COPYRIGHT file at the top level of
|
|
|
|
# this repository contains the full copyright notices and license terms.
|
2021-04-08 16:20:38 +02:00
|
|
|
import math
|
|
|
|
from decimal import Decimal
|
2020-04-08 18:23:00 +02:00
|
|
|
from sql import Table
|
|
|
|
from sql.aggregate import Sum
|
2022-11-09 18:02:44 +01:00
|
|
|
from datetime import date, timedelta
|
|
|
|
|
2020-04-08 18:23:00 +02:00
|
|
|
from trytond.pool import Pool, PoolMeta
|
|
|
|
from trytond.transaction import Transaction
|
2022-11-04 05:20:14 +01:00
|
|
|
from trytond.model import fields
|
2022-08-30 01:03:16 +02:00
|
|
|
from trytond.modules.dash.dash import DashAppBase
|
2020-04-08 18:23:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Sale(metaclass=PoolMeta):
|
|
|
|
__name__ = 'sale.sale'
|
|
|
|
|
2022-10-07 22:24:36 +02:00
|
|
|
@classmethod
|
|
|
|
def dash_faster_process(cls, records):
|
|
|
|
for rec in records:
|
|
|
|
cls.faster_process({'id': rec.id})
|
|
|
|
|
2021-03-31 23:17:46 +02:00
|
|
|
@classmethod
|
2022-11-16 23:40:04 +01:00
|
|
|
def dash_quote(cls, args, ctx=None):
|
2021-03-31 23:17:46 +02:00
|
|
|
Shop = Pool().get('sale.shop')
|
2021-04-04 16:56:16 +02:00
|
|
|
Product = Pool().get('product.product')
|
2021-05-12 01:53:38 +02:00
|
|
|
Party = Pool().get('party.party')
|
2021-05-21 02:23:16 +02:00
|
|
|
User = Pool().get('res.user')
|
2022-08-30 01:03:16 +02:00
|
|
|
ctx = Transaction().context
|
2021-05-21 02:23:16 +02:00
|
|
|
if ctx.get('shop'):
|
|
|
|
shop = Shop(ctx['shop'])
|
|
|
|
else:
|
|
|
|
user_id = ctx.get('user')
|
|
|
|
user = User(user_id)
|
|
|
|
shop = user.shop
|
|
|
|
|
2021-04-04 16:56:16 +02:00
|
|
|
for v in args['lines']:
|
2021-05-12 06:30:46 +02:00
|
|
|
if v.get('id'):
|
|
|
|
del v['id']
|
2022-11-16 23:56:49 +01:00
|
|
|
if v.get('amount'):
|
2021-05-12 06:30:46 +02:00
|
|
|
del v['amount']
|
2022-11-16 23:56:49 +01:00
|
|
|
if v.get('unit_price_w_tax'):
|
2022-11-16 23:44:13 +01:00
|
|
|
del v['unit_price_w_tax']
|
2022-11-16 23:56:49 +01:00
|
|
|
if v.get('total_amount'):
|
2022-08-30 01:03:16 +02:00
|
|
|
del v['total_amount']
|
2021-04-04 16:56:16 +02:00
|
|
|
v['type'] = 'line'
|
|
|
|
product = Product(v['product'])
|
2021-06-19 17:01:49 +02:00
|
|
|
|
|
|
|
if v.get('discount') and v['discount'] != '':
|
|
|
|
v['discount'] = Decimal(v['discount'])/100
|
|
|
|
elif v.get('discount'):
|
|
|
|
del v['discount']
|
2022-11-16 23:58:36 +01:00
|
|
|
v['unit'] = product.template.default_uom.id
|
2021-04-27 16:34:52 +02:00
|
|
|
v['description'] = product.name
|
2021-05-22 01:30:35 +02:00
|
|
|
taxes = list(product.account_category.customer_taxes_used)
|
|
|
|
taxes_ids = [t.id for t in taxes]
|
2021-05-22 01:30:44 +02:00
|
|
|
v['taxes'] = [('add', taxes_ids)]
|
2021-05-21 02:23:16 +02:00
|
|
|
|
2022-11-16 23:32:13 +01:00
|
|
|
try:
|
|
|
|
price_list = args['price_list']['id']
|
|
|
|
except:
|
|
|
|
price_list = args.get('price_list', None)
|
2021-05-12 01:53:38 +02:00
|
|
|
|
2022-11-15 16:05:50 +01:00
|
|
|
try:
|
|
|
|
# option kid
|
|
|
|
party = Party(args['party']['id'])
|
|
|
|
except:
|
|
|
|
# option fastkid
|
|
|
|
party = Party(args['party'])
|
2021-05-12 01:53:38 +02:00
|
|
|
if args.get('shipment_address'):
|
2022-11-15 16:05:50 +01:00
|
|
|
try:
|
|
|
|
# option kid
|
|
|
|
shipment_address_id = args.get('shipment_address')['id']
|
|
|
|
except:
|
|
|
|
# option fastkid
|
|
|
|
shipment_address_id = args.get('shipment_address')
|
2021-05-12 01:53:38 +02:00
|
|
|
else:
|
|
|
|
shipment_address_id = party.addresses[0].id
|
|
|
|
|
|
|
|
agent_id = None
|
|
|
|
if args.get('agent'):
|
2022-11-15 16:05:50 +01:00
|
|
|
try:
|
|
|
|
# option kid
|
|
|
|
agent_id = args['agent']['id']
|
|
|
|
except:
|
|
|
|
# option fastkid
|
|
|
|
agent_id = args['agent']
|
2021-05-12 01:53:38 +02:00
|
|
|
|
2021-05-12 06:30:46 +02:00
|
|
|
shipment_date = None
|
|
|
|
if args.get('shipment_date'):
|
|
|
|
shipment_date = args['shipment_date']
|
|
|
|
|
|
|
|
description = args.get('description', '')
|
2021-06-17 16:45:11 +02:00
|
|
|
comment = args.get('comment', '')
|
2022-11-12 15:11:02 +01:00
|
|
|
today = date.today()
|
2021-03-31 23:17:46 +02:00
|
|
|
to_create = {
|
2021-05-21 02:23:16 +02:00
|
|
|
'shop': shop.id,
|
2021-03-31 23:17:46 +02:00
|
|
|
'invoice_type': 'P',
|
|
|
|
'company': shop.company.id,
|
2022-11-15 16:05:50 +01:00
|
|
|
'party': party.id,
|
2022-11-12 15:11:02 +01:00
|
|
|
'sale_date': today,
|
2021-05-12 06:30:46 +02:00
|
|
|
'shipment_date': shipment_date,
|
2021-05-12 01:53:38 +02:00
|
|
|
'shipment_address': shipment_address_id,
|
|
|
|
'invoice_address': shipment_address_id,
|
|
|
|
'agent': agent_id,
|
2021-04-08 16:20:38 +02:00
|
|
|
'price_list': price_list,
|
2021-03-31 23:17:46 +02:00
|
|
|
'payment_term': shop.payment_term.id,
|
|
|
|
'state': 'draft',
|
2021-05-12 06:30:46 +02:00
|
|
|
'description': description,
|
2021-06-17 16:45:11 +02:00
|
|
|
'comment': comment,
|
2021-03-31 23:17:46 +02:00
|
|
|
'lines': [('create', args['lines'])],
|
|
|
|
}
|
2022-11-09 18:02:44 +01:00
|
|
|
|
2021-03-31 23:17:46 +02:00
|
|
|
sale, = cls.create([to_create])
|
2021-06-19 17:01:49 +02:00
|
|
|
for line in sale.lines:
|
|
|
|
if line.discount and line.discount > 0:
|
|
|
|
line.on_change_discount()
|
|
|
|
|
2021-03-31 23:17:46 +02:00
|
|
|
cls.quote([sale])
|
2021-04-12 02:46:53 +02:00
|
|
|
record = args.copy()
|
|
|
|
record.update({
|
|
|
|
'id': sale.id,
|
|
|
|
'state': sale.state,
|
|
|
|
'number': sale.number,
|
|
|
|
'total_amount': sale.total_amount,
|
|
|
|
})
|
2021-03-31 23:17:46 +02:00
|
|
|
res = {
|
2021-04-12 02:46:53 +02:00
|
|
|
'record': record,
|
2021-04-04 16:56:16 +02:00
|
|
|
'msg': 'successful_order',
|
|
|
|
'type': 'success',
|
2021-03-31 23:17:46 +02:00
|
|
|
'open_modal': True,
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
|
2021-04-08 16:20:38 +02:00
|
|
|
@classmethod
|
|
|
|
def dash_get_line(cls, args, ctx):
|
|
|
|
if not args.get('product'):
|
|
|
|
return {}
|
|
|
|
|
|
|
|
Product = Pool().get('product.product')
|
|
|
|
PriceListLine = Pool().get('product.price_list.line')
|
|
|
|
product_id = args['product']['id']
|
|
|
|
product = Product(product_id)
|
|
|
|
|
2021-04-16 06:14:14 +02:00
|
|
|
context = {
|
|
|
|
'company': ctx['company'],
|
|
|
|
'currency': ctx['currency'],
|
|
|
|
}
|
|
|
|
price_list_id = None
|
2021-10-27 21:24:48 +02:00
|
|
|
price_list = args.get('price_list', None)
|
2021-04-16 06:14:14 +02:00
|
|
|
if price_list:
|
|
|
|
price_list_id = price_list['id']
|
|
|
|
context['price_list'] = price_list_id
|
|
|
|
|
|
|
|
with Transaction().set_context(context):
|
2021-04-08 16:20:38 +02:00
|
|
|
unit_price = product.list_price
|
|
|
|
|
|
|
|
if price_list_id:
|
|
|
|
price_lines = PriceListLine.search([
|
|
|
|
('price_list', '=', price_list_id),
|
|
|
|
('product', '=', product_id),
|
|
|
|
])
|
|
|
|
if price_lines:
|
|
|
|
price_line = price_lines[0]
|
|
|
|
unit_price = float(unit_price)
|
|
|
|
unit_price = Decimal(eval(price_line.formula))
|
2021-08-21 00:20:02 +02:00
|
|
|
# percent_commission = price_line.price_list.percent_commission
|
2021-04-08 16:20:38 +02:00
|
|
|
|
2022-11-17 18:34:18 +01:00
|
|
|
# ADD TAXES
|
2021-05-20 22:08:27 +02:00
|
|
|
# taxes_ids = [t.id for t in product.customer_taxes_used]
|
2021-04-08 16:20:38 +02:00
|
|
|
# res = cls.get_price_with_tax([line], ['amount_w_tax', 'unit_price_w_tax'])
|
|
|
|
res = {
|
2021-05-20 22:08:27 +02:00
|
|
|
'unit_price_w_tax': math.ceil(product.sale_price_taxed),
|
2021-04-08 16:20:38 +02:00
|
|
|
# 'amount_w_tax': math.ceil(res['amount_w_tax'][None]),
|
|
|
|
# 'taxes': [[('add'), taxes_ids]],
|
|
|
|
'unit_price': math.ceil(unit_price),
|
|
|
|
'unit': product.template.default_uom.id,
|
|
|
|
'type': 'line',
|
|
|
|
}
|
2021-08-21 00:20:02 +02:00
|
|
|
# if percent_commission:
|
|
|
|
# res['commission_amount'] = round(
|
|
|
|
# (unit_price * quantity * percent_commission), 0
|
|
|
|
# )
|
2021-04-08 16:20:38 +02:00
|
|
|
return res
|
|
|
|
|
2022-04-09 02:03:33 +02:00
|
|
|
@classmethod
|
|
|
|
def mark_commanded(cls, args):
|
|
|
|
"""
|
|
|
|
This method mark as commanded all products in sale, previous
|
|
|
|
positive response of local printer
|
|
|
|
"""
|
|
|
|
sale = cls(args['sale_id'])
|
|
|
|
for line in sale.lines:
|
|
|
|
line.order_sended = True
|
|
|
|
line.save()
|
|
|
|
|
2022-04-09 01:33:37 +02:00
|
|
|
@classmethod
|
2022-04-09 01:44:59 +02:00
|
|
|
def _set_line(cls, val, context):
|
2022-04-09 01:33:37 +02:00
|
|
|
del val['id']
|
|
|
|
Product = Pool().get('product.product')
|
|
|
|
val['type'] = 'line'
|
|
|
|
product = Product(val['product'])
|
2022-04-09 01:44:59 +02:00
|
|
|
with Transaction().set_context(context):
|
|
|
|
unit_price = product.list_price
|
2022-04-09 01:33:37 +02:00
|
|
|
unit_price = unit_price.quantize(Decimal(str(10.0 ** -4)))
|
|
|
|
val['unit_price'] = unit_price
|
|
|
|
val['base_price'] = unit_price
|
|
|
|
val['unit'] = product.template.default_uom.id
|
|
|
|
val['description'] = product.name
|
|
|
|
taxes = list(product.account_category.customer_taxes_used)
|
|
|
|
taxes_ids = [t.id for t in taxes]
|
|
|
|
val['taxes'] = [('add', taxes_ids)]
|
|
|
|
return val
|
|
|
|
|
2022-03-31 15:52:57 +02:00
|
|
|
@classmethod
|
|
|
|
def command(cls, args):
|
|
|
|
Shop = Pool().get('sale.shop')
|
2022-05-02 16:18:12 +02:00
|
|
|
ShopTable = Pool().get('sale.shop.table')
|
2022-03-31 15:52:57 +02:00
|
|
|
User = Pool().get('res.user')
|
|
|
|
context = Transaction().context
|
2022-04-09 01:44:59 +02:00
|
|
|
user_id = context.get('user')
|
|
|
|
user = User(user_id)
|
2022-04-09 01:33:37 +02:00
|
|
|
action = 'create'
|
|
|
|
if args['id'] > 0:
|
|
|
|
action = 'edit'
|
|
|
|
|
2022-05-02 16:18:12 +02:00
|
|
|
table = None
|
|
|
|
if args.get('table_assigned'):
|
2022-11-26 18:51:03 +01:00
|
|
|
try:
|
|
|
|
table_id = args.get('table_assigned')['id']
|
|
|
|
except:
|
|
|
|
table_id = args.get('table_assigned')
|
2022-05-02 16:18:12 +02:00
|
|
|
table = ShopTable(table_id)
|
2022-04-09 01:33:37 +02:00
|
|
|
if action == 'create':
|
|
|
|
if context.get('shop'):
|
|
|
|
shop = Shop(context['shop'])
|
|
|
|
else:
|
|
|
|
shop = user.shop
|
2022-04-09 01:44:59 +02:00
|
|
|
context['price_list'] = shop.price_list
|
2022-04-09 01:33:37 +02:00
|
|
|
for v in args['lines']:
|
2022-04-09 01:44:59 +02:00
|
|
|
cls._set_line(v, context)
|
2022-04-09 01:33:37 +02:00
|
|
|
|
|
|
|
party = shop.party
|
2022-11-12 15:11:02 +01:00
|
|
|
today = date.today()
|
2022-04-09 01:33:37 +02:00
|
|
|
to_create = {
|
|
|
|
'shop': shop.id,
|
|
|
|
'party': party.id,
|
2022-11-04 05:20:14 +01:00
|
|
|
'invoice_type': 'P',
|
2022-11-26 18:51:03 +01:00
|
|
|
'table_assigned': table.id,
|
2022-04-09 01:33:37 +02:00
|
|
|
'shipment_address': party.addresses[0].id,
|
|
|
|
'invoice_address': party.addresses[0].id,
|
|
|
|
'company': shop.company.id,
|
2022-11-12 15:11:02 +01:00
|
|
|
'sale_date': today,
|
|
|
|
'shipment_date': today,
|
2022-04-09 01:44:59 +02:00
|
|
|
'kind': 'to_table',
|
2022-04-09 01:33:37 +02:00
|
|
|
'price_list': shop.price_list,
|
|
|
|
'payment_term': shop.payment_term.id,
|
|
|
|
'state': 'draft',
|
2022-04-09 01:44:59 +02:00
|
|
|
'salesman': user.employee.id if user.employee else None,
|
2022-04-09 01:33:37 +02:00
|
|
|
'order_status': 'commanded',
|
2022-04-09 01:44:59 +02:00
|
|
|
'sale_device': user.sale_device.id,
|
2022-04-09 01:33:37 +02:00
|
|
|
'lines': [('create', args['lines'])],
|
|
|
|
}
|
|
|
|
if args.get('description'):
|
|
|
|
to_create['description'] = args.get('description')
|
|
|
|
try:
|
|
|
|
sale, = cls.create([to_create])
|
|
|
|
sale.set_number([sale])
|
2022-05-02 16:18:12 +02:00
|
|
|
ShopTable.write([table], {
|
|
|
|
'state': 'occupied',
|
|
|
|
'sale': sale.id,
|
|
|
|
})
|
2022-11-26 18:51:03 +01:00
|
|
|
return {'status': 'success', 'record': {'id': sale.id}, 'msg': "Orden Comandada exitosamente!"}
|
2022-04-09 01:33:37 +02:00
|
|
|
except Exception as e:
|
2022-11-09 18:02:44 +01:00
|
|
|
print(e, 'error')
|
2022-11-26 18:51:03 +01:00
|
|
|
return {'status': 'error'}
|
2022-03-31 15:52:57 +02:00
|
|
|
else:
|
2022-04-09 01:33:37 +02:00
|
|
|
sale = cls(args['id'])
|
|
|
|
to_add = []
|
|
|
|
to_write = {}
|
2022-11-26 18:51:03 +01:00
|
|
|
if sale.table_assigned.id != table.id:
|
|
|
|
to_write['table_assigned'] = table.id
|
2022-05-02 16:18:12 +02:00
|
|
|
ShopTable.write([sale.table_assigned], {
|
|
|
|
'state': 'available',
|
|
|
|
'sale': None,
|
|
|
|
})
|
|
|
|
ShopTable.write([table], {
|
|
|
|
'state': 'occupied',
|
|
|
|
'sale': sale.id,
|
|
|
|
})
|
2022-04-09 01:33:37 +02:00
|
|
|
for v in args['lines']:
|
|
|
|
line_id = v.get('id')
|
|
|
|
if line_id < 0:
|
2022-04-09 01:44:59 +02:00
|
|
|
context['price_list'] = sale.shop.price_list
|
|
|
|
to_add.append(cls._set_line(v, context))
|
2022-04-09 01:33:37 +02:00
|
|
|
try:
|
|
|
|
if to_add:
|
|
|
|
to_write['lines'] = [('create', to_add)]
|
|
|
|
|
|
|
|
cls.write([sale], to_write)
|
2022-11-26 18:51:03 +01:00
|
|
|
return {'status': 'success', 'record': {'id': sale.id}, 'msg': "Orden Comandada exitosamente!"}
|
2022-04-09 01:33:37 +02:00
|
|
|
except Exception as e:
|
2022-11-26 18:51:03 +01:00
|
|
|
return {'status': 'error', 'record': {'id': sale.id}, 'msg': "Orden no Comandada"}
|
2022-04-09 01:33:37 +02:00
|
|
|
print(e, 'error')
|
|
|
|
|
2020-04-08 18:23:00 +02:00
|
|
|
@classmethod
|
2022-11-09 18:02:44 +01:00
|
|
|
def dash_get_amount_w_tax(cls, args):
|
|
|
|
Product = Pool().get('product.product')
|
|
|
|
product = Product(args['product'])
|
|
|
|
return product.template.compute_list_price_w_tax(args['list_price'])
|
2020-04-08 18:23:00 +02:00
|
|
|
|
|
|
|
@classmethod
|
2022-11-09 18:02:44 +01:00
|
|
|
def _get_sales_report(cls, dates, currency_id, in_thousands=False):
|
2020-04-08 22:23:43 +02:00
|
|
|
invoice = Table('account_invoice')
|
2022-11-09 18:02:44 +01:00
|
|
|
period = dates.get('period', None)
|
|
|
|
if period:
|
|
|
|
start_date = period.start_date
|
|
|
|
end_date = period.end_date
|
|
|
|
else:
|
|
|
|
start_date = dates.get('start_date')
|
|
|
|
end_date = dates.get('end_date')
|
2020-04-08 22:23:43 +02:00
|
|
|
|
|
|
|
cursor = Transaction().connection.cursor()
|
2022-11-09 18:02:44 +01:00
|
|
|
select = invoice.select(Sum(invoice.untaxed_amount_cache), limit=1)
|
2020-04-08 22:23:43 +02:00
|
|
|
select.where = (
|
|
|
|
invoice.type == 'out') & (
|
|
|
|
invoice.invoice_date >= start_date) & (
|
|
|
|
invoice.currency == currency_id) & (
|
|
|
|
invoice.invoice_date <= end_date) & (
|
2022-05-16 16:51:11 +02:00
|
|
|
invoice.state.in_(['validated', 'posted', 'paid'])
|
2020-04-08 22:23:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
cursor.execute(*select)
|
|
|
|
values = cursor.fetchone()
|
|
|
|
value = 0
|
|
|
|
if values and values[0]:
|
|
|
|
value = int(values[0])
|
2020-04-10 19:14:27 +02:00
|
|
|
if in_thousands:
|
|
|
|
value = int(value / 1000)
|
2020-04-08 22:23:43 +02:00
|
|
|
return value
|
2020-04-08 18:23:00 +02:00
|
|
|
|
|
|
|
@classmethod
|
2022-05-16 16:49:20 +02:00
|
|
|
def report_sales_month(cls, args, ctx=None):
|
2022-11-09 23:57:16 +01:00
|
|
|
# Dash Report
|
2020-04-08 18:23:00 +02:00
|
|
|
pool = Pool()
|
|
|
|
Period = pool.get('account.period')
|
|
|
|
Currency = pool.get('currency.currency')
|
|
|
|
res = {}
|
2022-11-12 15:11:02 +01:00
|
|
|
today = date.today()
|
2023-01-18 15:53:20 +01:00
|
|
|
today = today - timedelta(days=30)
|
2020-04-08 18:23:00 +02:00
|
|
|
periods = Period.search([
|
2022-11-12 15:11:02 +01:00
|
|
|
('start_date', '<=', today),
|
|
|
|
('end_date', '>=', today),
|
2020-04-08 18:23:00 +02:00
|
|
|
('type', '=', 'standard')
|
|
|
|
])
|
|
|
|
if not periods:
|
|
|
|
return res
|
|
|
|
|
|
|
|
period = periods[0]
|
|
|
|
selector_periods = Period.search([
|
|
|
|
('fiscalyear', '=', period.fiscalyear.id),
|
|
|
|
('type', '=', 'standard')
|
|
|
|
])
|
2022-11-09 18:02:44 +01:00
|
|
|
selector = {p.id: p.name for p in selector_periods}
|
|
|
|
|
|
|
|
currency_id = Transaction().context.get('currency')
|
2022-05-16 16:49:20 +02:00
|
|
|
if args.get('currency'):
|
|
|
|
currency_id = ctx.get('currency')
|
|
|
|
currency = Currency(currency_id)
|
2020-04-08 18:23:00 +02:00
|
|
|
|
2022-11-09 18:02:44 +01:00
|
|
|
dates = {'period': period}
|
|
|
|
value = cls._get_sales_report(dates, currency_id, in_thousands=True)
|
2020-04-08 18:23:00 +02:00
|
|
|
|
2022-11-12 15:11:02 +01:00
|
|
|
month_name = today.strftime("%b %Y")
|
2020-04-08 18:23:00 +02:00
|
|
|
res = {
|
|
|
|
'value': value,
|
|
|
|
'selector': selector,
|
2022-11-12 15:11:02 +01:00
|
|
|
'header_meta': month_name,
|
2022-11-09 23:57:16 +01:00
|
|
|
'desc': 'In thousands',
|
|
|
|
'desc_meta': currency.code,
|
2020-04-08 18:23:00 +02:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
|
|
|
|
@classmethod
|
2022-05-16 16:49:20 +02:00
|
|
|
def report_sales_by_month(cls, args, ctx=None):
|
2022-11-09 23:57:16 +01:00
|
|
|
# Dash Report
|
2020-04-08 18:23:00 +02:00
|
|
|
pool = Pool()
|
2020-04-08 22:23:43 +02:00
|
|
|
Fiscalyear = pool.get('account.fiscalyear')
|
2020-04-08 18:23:00 +02:00
|
|
|
Currency = pool.get('currency.currency')
|
2022-11-12 15:11:02 +01:00
|
|
|
today = date.today()
|
2023-01-18 15:53:20 +01:00
|
|
|
today = today - timedelta(days=30)
|
2022-11-09 23:57:16 +01:00
|
|
|
fiscalyear, = Fiscalyear.search([
|
2022-11-12 15:11:02 +01:00
|
|
|
('start_date', '<=', today),
|
|
|
|
('end_date', '>=', today),
|
2022-11-09 23:57:16 +01:00
|
|
|
], limit=1)
|
|
|
|
if not fiscalyear:
|
2020-04-08 22:23:43 +02:00
|
|
|
return {}
|
|
|
|
|
2022-11-09 23:57:16 +01:00
|
|
|
periods = [p for p in fiscalyear.periods if p.type == 'standard']
|
2022-11-09 18:02:44 +01:00
|
|
|
currency_id = Transaction().context.get('currency')
|
2022-05-16 16:49:20 +02:00
|
|
|
if args.get('currency'):
|
|
|
|
currency_id = args.get('currency')
|
|
|
|
|
|
|
|
currency = Currency(currency_id)
|
2020-04-08 22:23:43 +02:00
|
|
|
|
|
|
|
values = []
|
|
|
|
labels = []
|
|
|
|
for p in periods:
|
2022-11-12 17:52:08 +01:00
|
|
|
month = p.start_date.strftime("%b")
|
|
|
|
labels.append(month)
|
2022-11-09 18:02:44 +01:00
|
|
|
dates = {'period': p}
|
|
|
|
val = cls._get_sales_report(dates, currency_id, True)
|
2022-11-17 18:34:18 +01:00
|
|
|
if not val:
|
|
|
|
val = 0
|
|
|
|
values.append(val)
|
2020-04-08 22:23:43 +02:00
|
|
|
|
|
|
|
res = {
|
|
|
|
'values': values,
|
|
|
|
'labels': labels,
|
2022-11-09 23:57:16 +01:00
|
|
|
'header_meta': fiscalyear.name,
|
|
|
|
'desc': 'In thousands',
|
|
|
|
'desc_meta': currency.code,
|
2020-04-08 22:23:43 +02:00
|
|
|
}
|
2020-04-08 18:23:00 +02:00
|
|
|
return res
|
2021-04-14 19:19:41 +02:00
|
|
|
|
2022-08-31 17:12:11 +02:00
|
|
|
@classmethod
|
2022-11-09 18:02:44 +01:00
|
|
|
def report_sales_year(cls, args, ctx=None):
|
2022-11-09 23:57:16 +01:00
|
|
|
# Dash Report
|
2022-11-09 18:02:44 +01:00
|
|
|
pool = Pool()
|
|
|
|
Fiscalyear = pool.get('account.fiscalyear')
|
|
|
|
Currency = pool.get('currency.currency')
|
2022-11-12 15:11:02 +01:00
|
|
|
today = date.today()
|
2023-01-18 15:53:20 +01:00
|
|
|
today = today - timedelta(days=30)
|
2022-11-09 18:02:44 +01:00
|
|
|
fiscalyear, = Fiscalyear.search([
|
2022-11-12 15:11:02 +01:00
|
|
|
('start_date', '<=', today),
|
|
|
|
('end_date', '>=', today),
|
2022-11-09 18:02:44 +01:00
|
|
|
], limit=1)
|
|
|
|
if not fiscalyear:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
selector_fy = Fiscalyear.search([
|
|
|
|
('id', '=', fiscalyear.id),
|
|
|
|
])
|
|
|
|
selector = {p.id: p.name for p in selector_fy}
|
|
|
|
|
|
|
|
periods = [p for p in fiscalyear.periods if p.type == 'standard']
|
|
|
|
currency_id = Transaction().context.get('currency')
|
|
|
|
if args.get('currency'):
|
|
|
|
currency_id = args.get('currency')
|
|
|
|
|
|
|
|
currency = Currency(currency_id)
|
|
|
|
|
|
|
|
values = []
|
|
|
|
labels = []
|
|
|
|
for p in periods:
|
|
|
|
labels.append(p.name)
|
|
|
|
dates = {'period': p}
|
|
|
|
val = cls._get_sales_report(dates, currency_id, True)
|
|
|
|
if val:
|
|
|
|
values.append(val)
|
|
|
|
|
|
|
|
res = {
|
|
|
|
'value': sum(values),
|
|
|
|
'selector': selector,
|
2022-11-12 15:11:02 +01:00
|
|
|
'header_meta': fiscalyear.name,
|
2022-11-09 23:57:16 +01:00
|
|
|
'desc': 'In thousands',
|
|
|
|
'desc_meta': currency.code,
|
2022-11-09 18:02:44 +01:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def report_sales_day(cls, args, ctx=None):
|
2022-11-09 23:57:16 +01:00
|
|
|
# Dash Report
|
2022-11-09 18:02:44 +01:00
|
|
|
pool = Pool()
|
|
|
|
Currency = pool.get('currency.currency')
|
|
|
|
currency_id = Transaction().context.get('currency')
|
|
|
|
if args.get('currency'):
|
|
|
|
currency_id = args.get('currency')
|
|
|
|
|
2022-11-12 15:11:02 +01:00
|
|
|
currency = Currency(currency_id)
|
|
|
|
today = date.today()
|
2022-11-09 18:02:44 +01:00
|
|
|
dates = {
|
2022-11-12 15:11:02 +01:00
|
|
|
'start_date': today,
|
|
|
|
'end_date': today
|
2022-11-09 18:02:44 +01:00
|
|
|
}
|
|
|
|
|
2022-11-12 15:11:02 +01:00
|
|
|
yesterday = str(today - timedelta(days=1))
|
2022-11-09 18:02:44 +01:00
|
|
|
selector = {yesterday: 'yesterday'}
|
|
|
|
values = cls._get_sales_report(dates, currency_id, True)
|
|
|
|
res = {
|
|
|
|
'value': values,
|
|
|
|
'selector': selector,
|
2022-11-12 15:11:02 +01:00
|
|
|
'header_meta': str(today),
|
2022-11-09 23:57:16 +01:00
|
|
|
'desc': 'In thousands',
|
|
|
|
'desc_meta': currency.code,
|
2022-11-09 18:02:44 +01:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def report_fulfillment_goal_year(cls, args, ctx=None):
|
|
|
|
pool = Pool()
|
|
|
|
Fiscalyear = pool.get('account.fiscalyear')
|
2022-11-23 17:39:31 +01:00
|
|
|
Goal = pool.get('goal')
|
2022-11-17 18:34:18 +01:00
|
|
|
# Currency = pool.get('currency.currency')
|
2022-11-12 15:11:02 +01:00
|
|
|
today = date.today()
|
2023-01-18 15:53:20 +01:00
|
|
|
today = today - timedelta(days=30)
|
2022-11-09 18:02:44 +01:00
|
|
|
fiscalyear, = Fiscalyear.search([
|
2022-11-12 15:11:02 +01:00
|
|
|
('start_date', '<=', today),
|
|
|
|
('end_date', '>=', today),
|
2022-11-09 18:02:44 +01:00
|
|
|
], limit=1)
|
|
|
|
if not fiscalyear:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
periods = [p for p in fiscalyear.periods if p.type == 'standard']
|
|
|
|
currency_id = Transaction().context.get('currency')
|
|
|
|
if args.get('currency'):
|
|
|
|
currency_id = args.get('currency')
|
|
|
|
|
2022-11-17 18:34:18 +01:00
|
|
|
# currency = Currency(currency_id)
|
2022-11-09 18:02:44 +01:00
|
|
|
|
|
|
|
values = []
|
|
|
|
labels = []
|
|
|
|
for p in periods:
|
|
|
|
labels.append(p.name)
|
|
|
|
dates = {'period': p}
|
|
|
|
val = cls._get_sales_report(dates, currency_id, True)
|
|
|
|
if val:
|
|
|
|
values.append(val)
|
|
|
|
|
|
|
|
sum_values = sum(values)
|
2022-11-23 17:39:31 +01:00
|
|
|
goals = Goal.search([
|
|
|
|
('fiscalyear', '=', fiscalyear.id),
|
2022-11-28 20:10:11 +01:00
|
|
|
('kind', '=', 'sales'),
|
2022-11-23 17:39:31 +01:00
|
|
|
], limit=1)
|
|
|
|
if not goals:
|
|
|
|
goal_rate = 0
|
|
|
|
missing = 0
|
|
|
|
else:
|
|
|
|
goal = goals[0].total_amount / 1000
|
|
|
|
missing = goal - sum_values
|
|
|
|
goal_rate = int((sum_values / goal) * 100)
|
|
|
|
|
2022-11-09 18:02:44 +01:00
|
|
|
res = {
|
|
|
|
'labels': ["Sales", "Missing"],
|
|
|
|
'values': [sum(values), missing],
|
2022-11-09 23:57:16 +01:00
|
|
|
'center_label': str(goal_rate) + '%',
|
|
|
|
'header_meta': fiscalyear.name,
|
2022-11-09 18:02:44 +01:00
|
|
|
}
|
|
|
|
return res
|
2022-08-31 17:12:11 +02:00
|
|
|
|
2021-04-14 19:19:41 +02:00
|
|
|
|
2022-09-03 17:29:31 +02:00
|
|
|
class AppDelivery(DashAppBase):
|
2021-04-14 22:05:23 +02:00
|
|
|
'App Delivery'
|
2021-04-14 19:19:41 +02:00
|
|
|
__name__ = 'dash.app.delivery'
|
|
|
|
company = fields.Many2One('company.company', 'Company', required=True)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __setup__(cls):
|
2021-04-14 22:05:23 +02:00
|
|
|
super(AppDelivery, cls).__setup__()
|
2021-04-14 19:19:41 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def default_company():
|
|
|
|
return Transaction().context.get('company') or None
|
|
|
|
|
|
|
|
|
2022-09-03 17:29:31 +02:00
|
|
|
class AppTakeOrder(DashAppBase):
|
2022-03-31 15:52:57 +02:00
|
|
|
'App Take Order'
|
|
|
|
__name__ = 'dash.app.take_order'
|
|
|
|
|
|
|
|
|
2022-08-30 01:03:16 +02:00
|
|
|
class AppOrderViewer(DashAppBase):
|
2022-05-02 16:18:12 +02:00
|
|
|
'App Order Viewer'
|
|
|
|
__name__ = 'dash.app.order_viewer'
|
2022-04-01 15:42:18 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def default_company():
|
|
|
|
return Transaction().context.get('company') or None
|
|
|
|
|
|
|
|
|
2022-08-30 01:03:16 +02:00
|
|
|
class AppSaleOrder(DashAppBase):
|
2021-04-14 22:05:23 +02:00
|
|
|
'App Sale Order'
|
2021-04-14 19:19:41 +02:00
|
|
|
__name__ = 'dash.app.sale_order'
|
2021-06-19 17:01:49 +02:00
|
|
|
allow_discount = fields.Boolean('Allow Discount')
|
2022-11-04 05:20:14 +01:00
|
|
|
allow_manual_pricing = fields.Boolean('Allow Manual Pricing',
|
|
|
|
help='Allow manual pricing to user')
|
2021-04-14 19:19:41 +02:00
|
|
|
|
|
|
|
|
2022-08-30 01:03:16 +02:00
|
|
|
class AppSelfServiceSale(DashAppBase):
|
2022-08-07 23:47:26 +02:00
|
|
|
'App Self Service Sale'
|
|
|
|
__name__ = 'dash.app.self_service_sale'
|