REverse bad commit
This commit is contained in:
parent
aafb768494
commit
b2046aff89
199
sale.py
199
sale.py
|
@ -5,12 +5,15 @@ import math
|
|||
from decimal import Decimal
|
||||
from sql import Table
|
||||
from sql.aggregate import Sum
|
||||
from datetime import date
|
||||
from datetime import date, timedelta
|
||||
|
||||
from trytond.pool import Pool, PoolMeta
|
||||
from trytond.transaction import Transaction
|
||||
from trytond.model import fields
|
||||
from trytond.modules.dash.dash import DashAppBase
|
||||
|
||||
TODAY = date.today()
|
||||
|
||||
|
||||
class Sale(metaclass=PoolMeta):
|
||||
__name__ = 'sale.sale'
|
||||
|
@ -34,7 +37,6 @@ class Sale(metaclass=PoolMeta):
|
|||
user = User(user_id)
|
||||
shop = user.shop
|
||||
|
||||
today = date.today()
|
||||
for v in args['lines']:
|
||||
if v.get('id'):
|
||||
del v['id']
|
||||
|
@ -82,7 +84,7 @@ class Sale(metaclass=PoolMeta):
|
|||
'invoice_type': 'P',
|
||||
'company': shop.company.id,
|
||||
'party': args['party']['id'],
|
||||
'sale_date': today,
|
||||
'sale_date': TODAY,
|
||||
'shipment_date': shipment_date,
|
||||
'shipment_address': shipment_address_id,
|
||||
'invoice_address': shipment_address_id,
|
||||
|
@ -94,7 +96,7 @@ class Sale(metaclass=PoolMeta):
|
|||
'comment': comment,
|
||||
'lines': [('create', args['lines'])],
|
||||
}
|
||||
print('sale_to_ create', to_create)
|
||||
|
||||
sale, = cls.create([to_create])
|
||||
for line in sale.lines:
|
||||
if line.discount and line.discount > 0:
|
||||
|
@ -139,10 +141,10 @@ class Sale(metaclass=PoolMeta):
|
|||
with Transaction().set_context(context):
|
||||
unit_price = product.list_price
|
||||
|
||||
if args.get('quantity'):
|
||||
quantity = int(args.get('quantity'))
|
||||
else:
|
||||
quantity = 1
|
||||
# if args.get('quantity'):
|
||||
# quantity = int(args.get('quantity'))
|
||||
# else:
|
||||
# quantity = 1
|
||||
|
||||
# percent_commission = 0
|
||||
if price_list_id:
|
||||
|
@ -210,7 +212,6 @@ class Sale(metaclass=PoolMeta):
|
|||
ShopTable = Pool().get('sale.shop.table')
|
||||
User = Pool().get('res.user')
|
||||
context = Transaction().context
|
||||
today = date.today()
|
||||
user_id = context.get('user')
|
||||
user = User(user_id)
|
||||
action = 'create'
|
||||
|
@ -239,8 +240,8 @@ class Sale(metaclass=PoolMeta):
|
|||
'shipment_address': party.addresses[0].id,
|
||||
'invoice_address': party.addresses[0].id,
|
||||
'company': shop.company.id,
|
||||
'sale_date': today,
|
||||
'shipment_date': today,
|
||||
'sale_date': TODAY,
|
||||
'shipment_date': TODAY,
|
||||
'kind': 'to_table',
|
||||
'price_list': shop.price_list,
|
||||
'payment_term': shop.payment_term.id,
|
||||
|
@ -259,9 +260,9 @@ class Sale(metaclass=PoolMeta):
|
|||
'state': 'occupied',
|
||||
'sale': sale.id,
|
||||
})
|
||||
return {'record': {'id': sale.id}, 'status': 'ok', }
|
||||
return sale.id
|
||||
except Exception as e:
|
||||
return {'status': 'error', 'msg': e}
|
||||
print(e, 'error')
|
||||
else:
|
||||
sale = cls(args['id'])
|
||||
to_add = []
|
||||
|
@ -286,27 +287,29 @@ class Sale(metaclass=PoolMeta):
|
|||
to_write['lines'] = [('create', to_add)]
|
||||
|
||||
cls.write([sale], to_write)
|
||||
value = {'record': {'id': sale.id}, 'status': 'ok', }
|
||||
return value
|
||||
return sale.id
|
||||
except Exception as e:
|
||||
print(e, 'error')
|
||||
return {'status': 'error', 'msg': e}
|
||||
|
||||
@classmethod
|
||||
def report_sales_day(cls, args, ctx):
|
||||
pass
|
||||
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'])
|
||||
|
||||
@classmethod
|
||||
def _get_sales_in_period(cls, period, currency_id, in_thousands=False):
|
||||
def _get_sales_report(cls, dates, currency_id, in_thousands=False):
|
||||
invoice = Table('account_invoice')
|
||||
start_date = period.start_date
|
||||
end_date = period.end_date
|
||||
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')
|
||||
|
||||
cursor = Transaction().connection.cursor()
|
||||
select = invoice.select(
|
||||
Sum(invoice.untaxed_amount_cache),
|
||||
limit=1
|
||||
)
|
||||
select = invoice.select(Sum(invoice.untaxed_amount_cache), limit=1)
|
||||
select.where = (
|
||||
invoice.type == 'out') & (
|
||||
invoice.invoice_date >= start_date) & (
|
||||
|
@ -330,10 +333,9 @@ class Sale(metaclass=PoolMeta):
|
|||
Period = pool.get('account.period')
|
||||
Currency = pool.get('currency.currency')
|
||||
res = {}
|
||||
today = date.today()
|
||||
periods = Period.search([
|
||||
('start_date', '<=', today),
|
||||
('end_date', '>=', today),
|
||||
('start_date', '<=', TODAY),
|
||||
('end_date', '>=', TODAY),
|
||||
('type', '=', 'standard')
|
||||
])
|
||||
if not periods:
|
||||
|
@ -344,18 +346,16 @@ class Sale(metaclass=PoolMeta):
|
|||
('fiscalyear', '=', period.fiscalyear.id),
|
||||
('type', '=', 'standard')
|
||||
])
|
||||
if args.get('currency'):
|
||||
currency_id = ctx.get('currency')
|
||||
else:
|
||||
currency_id = Transaction().context.get('currency')
|
||||
|
||||
currency = Currency(currency_id)
|
||||
|
||||
selector = {p.id: p.name for p in selector_periods}
|
||||
|
||||
currency_id = Transaction().context.get('currency')
|
||||
if args.get('currency'):
|
||||
currency_id = ctx.get('currency')
|
||||
currency = Currency(currency_id)
|
||||
|
||||
description = currency.code
|
||||
value = cls._get_sales_in_period(
|
||||
period, currency_id, in_thousands=True)
|
||||
dates = {'period': period}
|
||||
value = cls._get_sales_report(dates, currency_id, in_thousands=True)
|
||||
|
||||
res = {
|
||||
'value': value,
|
||||
|
@ -373,26 +373,25 @@ class Sale(metaclass=PoolMeta):
|
|||
Fiscalyear = pool.get('account.fiscalyear')
|
||||
Currency = pool.get('currency.currency')
|
||||
|
||||
today = date.today()
|
||||
fiscalyears = Fiscalyear.search([
|
||||
('start_date', '<=', today),
|
||||
('end_date', '>=', today),
|
||||
('start_date', '<=', TODAY),
|
||||
('end_date', '>=', TODAY),
|
||||
])
|
||||
if not fiscalyears:
|
||||
return {}
|
||||
|
||||
periods = [p for p in fiscalyears[0].periods if p.type == 'standard']
|
||||
currency_id = Transaction().context.get('currency')
|
||||
if args.get('currency'):
|
||||
currency_id = args.get('currency')
|
||||
else:
|
||||
currency_id = Transaction().context.get('currency')
|
||||
|
||||
currency = Currency(currency_id)
|
||||
|
||||
values = []
|
||||
labels = []
|
||||
for p in periods:
|
||||
val = cls._get_sales_in_period(p, currency_id, True)
|
||||
dates = {'period': p}
|
||||
val = cls._get_sales_report(dates, currency_id, True)
|
||||
if val > 0:
|
||||
values.append(val)
|
||||
labels.append(p.name)
|
||||
|
@ -407,10 +406,116 @@ class Sale(metaclass=PoolMeta):
|
|||
return res
|
||||
|
||||
@classmethod
|
||||
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'])
|
||||
def report_sales_year(cls, args, ctx=None):
|
||||
pool = Pool()
|
||||
Fiscalyear = pool.get('account.fiscalyear')
|
||||
Currency = pool.get('currency.currency')
|
||||
|
||||
fiscalyear, = Fiscalyear.search([
|
||||
('start_date', '<=', TODAY),
|
||||
('end_date', '>=', TODAY),
|
||||
], 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,
|
||||
'description': currency.code,
|
||||
'meta': fiscalyear.name,
|
||||
'in_thousands': True
|
||||
}
|
||||
return res
|
||||
|
||||
@classmethod
|
||||
def report_sales_day(cls, args, ctx=None):
|
||||
pool = Pool()
|
||||
Currency = pool.get('currency.currency')
|
||||
|
||||
currency_id = Transaction().context.get('currency')
|
||||
if args.get('currency'):
|
||||
currency_id = args.get('currency')
|
||||
currency = Currency(currency_id)
|
||||
|
||||
dates = {
|
||||
'start_date': TODAY,
|
||||
'end_date': TODAY
|
||||
}
|
||||
|
||||
yesterday = str(TODAY - timedelta(days=1))
|
||||
selector = {yesterday: 'yesterday'}
|
||||
values = cls._get_sales_report(dates, currency_id, True)
|
||||
res = {
|
||||
'value': values,
|
||||
'selector': selector,
|
||||
'description': currency.code,
|
||||
'meta': str(TODAY),
|
||||
'in_thousands': True
|
||||
}
|
||||
return res
|
||||
|
||||
@classmethod
|
||||
def report_fulfillment_goal_year(cls, args, ctx=None):
|
||||
pool = Pool()
|
||||
Fiscalyear = pool.get('account.fiscalyear')
|
||||
Currency = pool.get('currency.currency')
|
||||
|
||||
fiscalyear, = Fiscalyear.search([
|
||||
('start_date', '<=', TODAY),
|
||||
('end_date', '>=', TODAY),
|
||||
], 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')
|
||||
|
||||
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)
|
||||
|
||||
sum_values = sum(values)
|
||||
goal = 520000
|
||||
missing = goal - sum_values
|
||||
goal_rate = int(sum_values / goal)
|
||||
res = {
|
||||
'labels': ["Sales", "Missing"],
|
||||
'values': [sum(values), missing],
|
||||
'goal': goal_rate,
|
||||
'meta': fiscalyear.name,
|
||||
'in_thousands': True
|
||||
}
|
||||
return res
|
||||
|
||||
|
||||
class AppDelivery(DashAppBase):
|
||||
|
|
Loading…
Reference in New Issue