minor fix in report sales by kind

This commit is contained in:
wilson gomez 2022-02-04 08:57:12 -05:00
parent 13f67ecac2
commit 6afbc5cce7
3 changed files with 66 additions and 11 deletions

69
sale.py
View file

@ -1581,6 +1581,17 @@ class SaleByKindStart(ModelView):
start_time = fields.Time('Start Time')
end_time = fields.Time('End Time')
shop = fields.Many2One('sale.shop', 'Shop')
payment_terms = fields.Many2Many('account.invoice.payment_term', None, None,
'Payment Term')
kind = fields.Selection([
('', ''),
('delivery', 'Delivery'),
('take_away', 'Take Away'),
('to_table', 'To Table'),
('catering', 'Catering'),
], 'Kind Order',)
party = fields.Many2One('party.party', 'Party')
detailed_by_party = fields.Boolean('Detailed By Party')
@staticmethod
def default_company():
@ -1610,6 +1621,10 @@ class SaleByKind(Wizard):
shop_id = None
if self.start.shop:
shop_id = self.start.shop.id
payment_terms = None
if self.start.payment_terms:
payment_terms = [p.id for p in self.start.payment_terms]
report_context = {
'company': self.start.company.id,
'start_date': self.start.start_date,
@ -1617,6 +1632,10 @@ class SaleByKind(Wizard):
'start_time': self.start.start_time,
'end_time': self.start.end_time,
'shop': shop_id,
'payment_terms': payment_terms,
'party': self.start.party,
'kind': self.start.kind,
'detailed': self.start.detailed_by_party,
}
return action, report_context
@ -1635,6 +1654,7 @@ class SaleByKindReport(Report):
Line = pool.get('sale.line')
Shop = pool.get('sale.shop')
Sale = pool.get('sale.sale')
Party = pool.get('party.party')
Company = pool.get('company.company')
company = Company(data['company'])
@ -1648,6 +1668,7 @@ class SaleByKindReport(Report):
line = Line.__table__()
shop = Shop.__table__()
sale = Sale.__table__()
party = Party.__table__()
where = sale.state.in_(['done', 'processing'])
where &= sale.sale_date >= data['start_date']
@ -1662,14 +1683,29 @@ class SaleByKindReport(Report):
where &= sale.create_date >= start_date
if data['shop']:
where &= sale.shop == data['shop']
if data['party']:
where &= sale.party == data['party']
if data['payment_terms']:
where &= sale.payment_term.in_(data['payment_terms'])
if data['kind']:
where &= sale.kind == data['kind']
kinds = ['to_table', 'take_away', 'delivery', 'catering', 'others']
columns1 = [
sale.shop.as_('shop_id'),
shop.name.as_('shop'),
Count(sale.number).as_('sales_total'),
]
group_by = (sale.shop, shop.name)
if data['detailed']:
columns1.extend([
party.id.as_('party_id'),
party.name.as_('party'),
])
group_by += (party.id, party.name)
columns1.extend([Count(sale.number).as_('sales_total')])
for k in kinds:
if k != 'others':
columns_ = [
@ -1680,11 +1716,12 @@ class SaleByKindReport(Report):
Sum(Case((sale.kind == Null or sale.kind == '', 1), else_=0)).as_('sales_'+k),
]
columns1.extend(columns_)
query = sale.join(shop, condition=sale.shop == shop.id).select(
query = sale.join(shop, condition=shop.id == sale.shop
).join(party, condition=party.id == sale.party).select(
*columns1,
where=where,
group_by=(sale.shop, shop.name))
records, totals = cls.get_values(query, {}, {})
group_by=group_by)
records, totals = cls.get_values(query, {}, {}, data['detailed'])
if len(products_exception) > 0:
where &= NotIn(line.product, products_exception)
@ -1692,9 +1729,16 @@ class SaleByKindReport(Report):
columns2 = [
sale.shop.as_('shop_id'),
shop.name.as_('shop'),
Sum(line.quantity*line.unit_price).as_('amount_total'),
]
group_by = (sale.shop, shop.name)
if data['detailed']:
columns2.extend([
party.id.as_('party_id'),
party.name.as_('party'),
])
group_by += (party.id, party.name)
columns2.extend([Sum(line.quantity*line.unit_price).as_('amount_total')])
for k in kinds:
if k != 'others':
columns_ = [
@ -1708,11 +1752,12 @@ class SaleByKindReport(Report):
query = line.join(
sale, condition=line.sale == sale.id
).join(party, condition=sale.party == party.id
).join(shop, condition=sale.shop == shop.id).select(
*columns2,
where=where,
group_by=(sale.shop, shop.name))
records, totals = cls.get_values(query, records, totals)
group_by=group_by)
records, totals = cls.get_values(query, records, totals, data['detailed'])
report_context['records'] = records.values()
report_context['company'] = company.rec_name
@ -1721,13 +1766,15 @@ class SaleByKindReport(Report):
return report_context
@classmethod
def get_values(cls, query, records, totals):
def get_values(cls, query, records, totals, detailed):
cursor = Transaction().connection.cursor()
cursor.execute(*query)
columns = list(cursor.description)
result = cursor.fetchall()
for row in result:
key = row[0]
if detailed:
key = str(row[0]) + '_' + row[3]
row_dict = {}
for i, col in enumerate(columns):
if col.name.startswith('sales') or col.name.startswith('amount'):
@ -1737,9 +1784,9 @@ class SaleByKindReport(Report):
totals[col.name] = row[i]
row_dict[col.name] = row[i]
try:
records[row[0]].update(row_dict)
records[key].update(row_dict)
except:
records[row[0]] = row_dict
records[key] = row_dict
return records, totals

Binary file not shown.

View file

@ -6,6 +6,10 @@ this repository contains the full copyright notices and license terms. -->
<field name="company" widget="selection"/>
<label name="shop"/>
<field name="shop" widget="selection"/>
<label name="kind"/>
<field name="kind"/>
<label name="party"/>
<field name="party"/>
<label name="start_date"/>
<field name="start_date"/>
<label name="end_date"/>
@ -14,4 +18,8 @@ this repository contains the full copyright notices and license terms. -->
<field name="start_time"/>
<label name="end_time"/>
<field name="end_time"/>
<label name="detailed_by_party"/>
<field name="detailed_by_party"/>
<field name="payment_terms" colspan="4"/>
</form>