2020-04-08 18:23:00 +02:00
|
|
|
# This file is part of purchase_discount module for 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
|
|
|
|
from datetime import date
|
|
|
|
from trytond.pool import Pool, PoolMeta
|
|
|
|
from trytond.transaction import Transaction
|
2021-04-14 19:19:41 +02:00
|
|
|
from trytond.model import ModelView, ModelSQL, fields
|
2020-04-08 18:23:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Sale(metaclass=PoolMeta):
|
|
|
|
__name__ = 'sale.sale'
|
|
|
|
|
2021-03-31 23:17:46 +02:00
|
|
|
@classmethod
|
|
|
|
def __setup__(cls):
|
|
|
|
super(Sale, cls).__setup__()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def dash_quote(cls, args, ctx):
|
|
|
|
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')
|
|
|
|
if ctx.get('shop'):
|
|
|
|
shop = Shop(ctx['shop'])
|
|
|
|
else:
|
|
|
|
user_id = ctx.get('user')
|
|
|
|
user = User(user_id)
|
|
|
|
shop = user.shop
|
|
|
|
|
2021-04-08 16:20:38 +02:00
|
|
|
today = date.today()
|
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']
|
|
|
|
if v.get('amount'):
|
|
|
|
del v['amount']
|
2021-05-22 00:25:25 +02:00
|
|
|
if v.get('unit_price_w_tax'):
|
|
|
|
del v['unit_price_w_tax']
|
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
|
|
|
|
unit_price = product.list_price - (product.list_price * Decimal(v['discount']))
|
|
|
|
v['unit_price'] = Decimal(unit_price).quantize(Decimal(str(10.0 ** -4)))
|
|
|
|
elif v.get('discount'):
|
|
|
|
del v['discount']
|
2021-04-04 16:56:16 +02: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
|
|
|
|
2021-04-08 16:20:38 +02:00
|
|
|
price_list = args.get('price_list', None)
|
|
|
|
if price_list:
|
|
|
|
price_list = price_list['id']
|
2021-05-12 01:53:38 +02:00
|
|
|
|
|
|
|
if args.get('shipment_address'):
|
|
|
|
shipment_address_id = args.get('shipment_address')['id']
|
|
|
|
else:
|
|
|
|
party = Party(args['party']['id'])
|
|
|
|
shipment_address_id = party.addresses[0].id
|
|
|
|
|
|
|
|
agent_id = None
|
|
|
|
if args.get('agent'):
|
|
|
|
agent_id = args['agent']['id']
|
|
|
|
|
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', '')
|
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,
|
|
|
|
'party': args['party']['id'],
|
2021-04-08 16:20:38 +02: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'])],
|
|
|
|
}
|
|
|
|
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
|
|
|
|
price_list = args.get('price_list')
|
|
|
|
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 args.get('quantity'):
|
|
|
|
quantity = int(args.get('quantity'))
|
|
|
|
else:
|
|
|
|
quantity = 1
|
|
|
|
|
|
|
|
percent_commission = 0
|
|
|
|
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))
|
|
|
|
percent_commission = price_line.price_list.percent_commission
|
|
|
|
|
|
|
|
#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',
|
|
|
|
}
|
|
|
|
if percent_commission:
|
|
|
|
res['commission_amount'] = round(
|
|
|
|
(unit_price * quantity * percent_commission), 0
|
|
|
|
)
|
|
|
|
return res
|
|
|
|
|
2020-04-08 18:23:00 +02:00
|
|
|
@classmethod
|
|
|
|
def report_sales_day(cls, args, ctx):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
2020-04-10 19:14:27 +02:00
|
|
|
def _get_sales_in_period(cls, period, currency_id, in_thousands=False):
|
2020-04-08 22:23:43 +02:00
|
|
|
invoice = Table('account_invoice')
|
|
|
|
start_date = period.start_date
|
|
|
|
end_date = period.end_date
|
|
|
|
|
|
|
|
cursor = Transaction().connection.cursor()
|
|
|
|
select = invoice.select(
|
|
|
|
Sum(invoice.untaxed_amount_cache),
|
|
|
|
limit=1
|
|
|
|
)
|
|
|
|
select.where = (
|
|
|
|
invoice.type == 'out') & (
|
|
|
|
invoice.invoice_date >= start_date) & (
|
|
|
|
invoice.currency == currency_id) & (
|
|
|
|
invoice.invoice_date <= end_date) & (
|
|
|
|
invoice.state.in_(['posted', 'paid'])
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
def report_sales_month(cls, args, ctx):
|
|
|
|
pool = Pool()
|
|
|
|
Period = pool.get('account.period')
|
|
|
|
Currency = pool.get('currency.currency')
|
|
|
|
res = {}
|
|
|
|
today = date.today()
|
|
|
|
periods = Period.search([
|
|
|
|
('start_date', '<=', today),
|
|
|
|
('end_date', '>=', today),
|
|
|
|
('type', '=', 'standard')
|
|
|
|
])
|
|
|
|
if not periods:
|
|
|
|
return res
|
|
|
|
|
2020-04-10 19:14:27 +02:00
|
|
|
report = args['report']
|
2020-04-08 18:23:00 +02:00
|
|
|
period = periods[0]
|
|
|
|
selector_periods = Period.search([
|
|
|
|
('fiscalyear', '=', period.fiscalyear.id),
|
|
|
|
('type', '=', 'standard')
|
|
|
|
])
|
|
|
|
currency = Currency(ctx['currency'])
|
|
|
|
|
|
|
|
selector = {p.id: p.name for p in selector_periods}
|
|
|
|
|
2020-04-08 22:23:43 +02:00
|
|
|
description = currency.code
|
2020-04-10 19:14:27 +02:00
|
|
|
value = cls._get_sales_in_period(period, ctx['currency'],
|
|
|
|
report.in_thousands)
|
2020-04-08 18:23:00 +02:00
|
|
|
|
|
|
|
res = {
|
|
|
|
'value': value,
|
|
|
|
'description': description,
|
|
|
|
'selector': selector,
|
|
|
|
'default_option': period.id,
|
|
|
|
'meta': period.name,
|
2020-04-10 19:14:27 +02:00
|
|
|
'in_thousands': report.in_thousands
|
2020-04-08 18:23:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def report_sales_by_month(cls, args, ctx):
|
|
|
|
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')
|
2020-04-08 22:23:43 +02:00
|
|
|
|
|
|
|
today = date.today()
|
|
|
|
fiscalyears = Fiscalyear.search([
|
|
|
|
('start_date', '<=', today),
|
|
|
|
('end_date', '>=', today),
|
|
|
|
])
|
|
|
|
if not fiscalyears:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
periods = [p for p in fiscalyears[0].periods if p.type == 'standard']
|
|
|
|
|
|
|
|
values = []
|
|
|
|
labels = []
|
2020-04-10 19:14:27 +02:00
|
|
|
report = args['report']
|
2020-04-08 22:23:43 +02:00
|
|
|
for p in periods:
|
2020-04-10 19:14:27 +02:00
|
|
|
val = cls._get_sales_in_period(p, ctx['currency'], report.in_thousands)
|
2020-04-08 22:23:43 +02:00
|
|
|
if val > 0:
|
|
|
|
values.append(val)
|
|
|
|
labels.append(p.name)
|
|
|
|
|
|
|
|
currency = Currency(ctx['currency'])
|
|
|
|
|
|
|
|
res = {
|
|
|
|
'values': values,
|
|
|
|
'labels': labels,
|
|
|
|
'description': 'report_sales_by_month',
|
|
|
|
'meta': currency.code,
|
2020-04-10 19:14:27 +02:00
|
|
|
'in_thousands': report.in_thousands
|
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
|
|
|
|
|
|
|
|
2021-04-14 22:05:23 +02:00
|
|
|
class AppDelivery(ModelSQL, ModelView):
|
|
|
|
'App Delivery'
|
2021-04-14 19:19:41 +02:00
|
|
|
__name__ = 'dash.app.delivery'
|
|
|
|
company = fields.Many2One('company.company', 'Company', required=True)
|
2021-04-15 06:17:44 +02:00
|
|
|
code = fields.Char('Code')
|
2021-04-14 19:19:41 +02:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
|
2021-04-14 22:05:23 +02:00
|
|
|
class AppSaleOrder(ModelSQL, ModelView):
|
|
|
|
'App Sale Order'
|
2021-04-14 19:19:41 +02:00
|
|
|
__name__ = 'dash.app.sale_order'
|
2021-07-06 20:36:53 +02:00
|
|
|
|
2021-04-14 19:19:41 +02:00
|
|
|
company = fields.Many2One('company.company', 'Company', required=True)
|
2021-06-19 17:01:49 +02:00
|
|
|
allow_discount = fields.Boolean('Allow Discount')
|
2021-04-14 19:19:41 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __setup__(cls):
|
2021-04-14 22:05:23 +02:00
|
|
|
super(AppSaleOrder, cls).__setup__()
|
2021-04-14 19:19:41 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def default_company():
|
|
|
|
return Transaction().context.get('company') or None
|