changes in kardex report

This commit is contained in:
Elvis 2023-10-28 10:36:36 -05:00
parent 8eddf04b20
commit d3754e27a8
4 changed files with 140 additions and 74 deletions

View File

@ -1 +1 @@
,presik,presik,26.10.2023 15:54,file:///home/presik/.config/libreoffice/4;
,presik,presik,28.10.2023 10:31,file:///home/presik/.config/libreoffice/4;

Binary file not shown.

68
sql_query.sql Normal file
View File

@ -0,0 +1,68 @@
select p.id as pid, p.code, t.name, pb.name as brand, t.reference, s.effective_date, lp.list_price,
SPLIT_PART(s.origin, ',', 1) as model,
SPLIT_PART(s.origin, ',', 2) as line_id,
s.quantity as quantity
from stock_move as s
INNER JOIN product_product as p on p.id = s.product
INNER JOIN product_template as t on p.template = t.id
INNER JOIN product_list_price as lp on t.id = lp.template
INNER JOIN product_brand as pb on pb.id = t.brand
where s.effective_date >= '2023-10-01'
and s.effective_date <= '2023-10-31'
and (s.origin LIKE '%purchase.line%' or s.origin LIKE '%sale.line%')
AND p.id IN (12394);
select t1.pid, t1.model, t1.line_id, slt.number, slt.party,
t1.id AS pid, t1.code, t1.name, t1.name AS brand, t1.reference, t1.effective_date, t1.list_price,
from t1
INNER JOIN (
SELECT line.id, sale.number, sale.party
FROM sale_line AS line
INNER JOIN sale_sale AS sale ON line.sale=sale.id
) AS slt ON slt.id=t1.line_id
WHERE t1.model='sale.line'
INNER JOIN (
SELECT line.id, purchase.number, purchase.party
FROM purchase_line AS line
INNER JOIN purchase_purchase AS purchase ON line.purchase=purchase.id
) AS slt ON slt.id=t1.line_id
WHERE t1.model='purchase.line'
select t1.pid, t1.model, t1.line_id, slt.number, slt.party,
select t1.pid, t1.model, t1.line_id, t1.code, t1.name, t1.name AS brand,
t1.reference, t1.effective_date, t1.quantity,
slt.number as sale_number, slt.unit_price
FROM
(select p.id as pid, p.code, t.name, pb.name as brand, t.reference, s.effective_date,
SPLIT_PART(s.origin, ',', 1) as model,
SPLIT_PART(s.origin, ',', 2) as line_id,
s.quantity as quantity
from stock_move as s
INNER JOIN product_product as p on p.id = s.product
INNER JOIN product_template as t on p.template = t.id
INNER JOIN product_brand as pb on pb.id = t.brand
where s.effective_date >= '2023-10-01'
and s.effective_date <= '2023-10-31'
and (s.origin LIKE '%purchase.line%' or s.origin LIKE '%sale.line%')
AND p.id IN (12394)) as t1
INNER JOIN (
SELECT line.id, line.unit_price, sale.number, sale.party, 'sale.line' AS model
FROM sale_line AS line
INNER JOIN sale_sale AS sale ON line.sale=sale.id
) AS slt ON slt.id=CAST(t1.line_id AS integer)
INNER JOIN (
SELECT line.id, line.unit_price, purchase.number, purchase.party
FROM purchase_line AS line
INNER JOIN purchase_purchase AS purchase ON line.purchase=purchase.id
) AS plt ON plt.id=CAST(t1.line_id AS integer)
WHERE t1.model='purchase.line';
WHERE t1.model='sale.line'

144
stock.py
View File

@ -283,9 +283,7 @@ class MoveByProduct(Report):
dom_products.append(
('product', '=', data['product']),
)
print(dom_products)
moves = Move.search([dom_products])
print(moves, 'moves')
products = {}
Location = pool.get('stock.location')
@ -320,10 +318,8 @@ class MoveByProduct(Report):
template.list_price,
effective_date,
]
print(products[id_dict])
report_context['records'] = products.values()
print(report_context['records'])
report_context['from_location'] = from_location
report_context['to_location'] = location
report_context['customer_location'] = location.type == 'customer'
@ -806,10 +802,7 @@ class WarehouseKardexReport(Report):
Product = pool.get('product.product')
Location = pool.get('stock.location')
warehouses = Location.browse(data['locations'])
# StockMove = pool.get('stock.move')
id_locations = [w.storage_location.id for w in warehouses if w.storage_location]
# input_location = warehouse.input_location.id
# output_location = warehouse.output_location.id
dom_products = [
('active', '=', True),
('template.active', '=', True),
@ -835,7 +828,7 @@ class WarehouseKardexReport(Report):
}
products = {}
products_two = {}
products_detail = []
fields_names = ['code', 'name', 'brand', 'reference', 'quantity']
# products = {p['id']: p for p in products_start}
# type = {'input': f'and from_location != {output_location} and to_location = {id_location}',
@ -849,8 +842,6 @@ class WarehouseKardexReport(Report):
'input': f'and to_location in {tup_locations}',
'output': f'and from_location in {tup_locations}',
}
print(type, 'es type')
# where product in {tuple(products)}
moves = {}
if not data['detail_by_product']:
with Transaction().set_context(stock_context_start):
@ -878,50 +869,30 @@ class WarehouseKardexReport(Report):
for m, v in moves.items():
cls.set_value(v, k, products)
# moves[k] = cls.query_to_dict(query)
# for m, v in moves.items():
# if not m.endswith('dif'):
# continue
#
# for k, value in v.items():
# move = moves[m[0:-4]].get(k, None)
# quantity = 0
# if move:
# quantity = move['quantity']
# moves[m[0:-4]][k] = quantity - value['quantity']
# report_context['products_out'] = moves['output']
# report_context['products_in'] = moves['input']
# report_context['products_start'] = products_start
# report_context['products_end'] = products_end
else:
AverageCost = pool.get('product.average_cost')
Sale = pool.get('sale.line')
Purchase = pool.get('purchase.line')
for k, v in type.items():
clause = v
query = f"""select s.id, p.id as pid, p.code, t.name, pb.name as brand, t.reference, s.effective_date, lp.list_price, s.origin, sum(s.quantity) as quantity
for product in data['products']:
query = f"""select p.id as pid, p.code, t.name, pb.name as brand, t.reference, s.effective_date,
SPLIT_PART(s.origin, ',', 1) as model,
SPLIT_PART(s.origin, ',', 2) as line_id,
s.id as move_id, s.quantity as quantity
from stock_move as s
join product_product as p on p.id = s.product
join product_template as t on p.template = t.id
join product_list_price as lp on t.id = lp.template
join product_brand as pb on pb.id = t.brand
where effective_date >= '{str(data['from_date'])}'
and effective_date <= '{str(data['to_date'])}'
INNER JOIN product_product as p on p.id = s.product
INNER JOIN product_template as t on p.template = t.id
INNER JOIN product_brand as pb on pb.id = t.brand
where s.effective_date >= '2023-10-01'
and s.effective_date <= '2023-10-31'
and (s.origin LIKE '%purchase.line%' or s.origin LIKE '%sale.line%')
AND p.id IN ({', '.join(map(str, data['products']))})
GROUP BY s.id, p.id, p.code, t.name, pb.name, t.reference, s.effective_date, lp.list_price, s.origin"""
# group by s.product, s.effective_date;"""
moves = cls.query_to_dict(query)
av_cost = None
for m, v in moves.items():
# cls.set_value(v, k, products_two)
av_cost = cls.set_value_two(v, k, products_two, id_locations, Product, Sale, Purchase, AverageCost, av_cost)
AND p.id IN ({product})
ORDER BY s.effective_date ASC, p.id ASC
"""
start, end = cls.get_balances(Product, product, data['from_date'], id_locations)
products_detail.append(cls.query_to_dict_detail(query, end))
wh_name = ''
for l in warehouses:
wh_name += (l.name + ', ')
report_context['products'] = products.values()
report_context['products_detail'] = products_detail
report_context['detail_by_product'] = data['detail_by_product']
report_context['products_two'] = products_two.values()
report_context['warehouse'] = wh_name
report_context['company'] = Company(data['company'])
return report_context
@ -948,33 +919,8 @@ class WarehouseKardexReport(Report):
})
@classmethod
def set_value_two(cls, p, key, products, id_locations, Product, Sale, Purchase, AverageCost, average_cost=None):
def set_value_detail(cls, p, key, Product, id_locations):
start, end = cls.get_balances(Product, p['pid'], p['effective_date'], id_locations)
domain_cost = [('product', '=', p['pid']), ('effective_date', '=', p['effective_date'])]
cost_price = AverageCost.search_read(domain_cost, fields_names=['cost_price'])
av_cost = cost_price[0]['cost_price'] if cost_price else average_cost
product_origin = Product(p['pid'])
sale = Sale(int(p['origin'].split(',')[1])) if 'sale.line' in p['origin'] else None
purchase = Purchase(int(p['origin'].split(',')[1])) if 'purchase.line' in p['origin'] else None
products[p['id']] = {
'start': start,
'input': p['quantity'] if 'purchase.line' in p['origin'] else 0,
'output': p['quantity'] if 'sale.line' in p['origin'] else 0,
'end': end,
'code': p['code'],
'name': p['name'],
'party': sale.sale.party.name if sale else purchase.purchase.party.name,
'number_origin': sale.sale.number if sale else purchase.purchase.number,
'reference': p['reference'],
'effective_date': p['effective_date'],
'start': start,
'end': end,
'brand': p['brand'],
'cost_price': av_cost,
'list_price': sale.unit_price if sale else 0,
'id': p['id'],
}
return av_cost
@classmethod
def get_balances(cls, Product, id, effective_data, locations):
@ -998,7 +944,6 @@ class WarehouseKardexReport(Report):
cls.set_value(p, 'start', products)
for p in products_end:
cls.set_value(p, 'end', products)
# print(products)
return products[id]['start'], products[id]['end']
@classmethod
@ -1014,3 +959,56 @@ class WarehouseKardexReport(Report):
row_dict[col.name] = row[i]
res_dict[row[0]] = row_dict
return res_dict
@classmethod
def query_to_dict_detail(cls, query, end):
pool = Pool()
SaleLine = pool.get('sale.line')
PurchaseLine = pool.get('purchase.line')
cursor = Transaction().connection.cursor()
cursor.execute(query)
result = cursor.fetchall()
sale_line_ids = []
purchase_line_ids = []
for row in result:
if row[6] == 'sale.line':
sale_line_ids.append(row[7])
else:
purchase_line_ids.append(row[7])
sale_lines = SaleLine.search_read([('id', 'in', sale_line_ids)], fields_names=['sale.party.name', 'sale.number', 'unit_price'])
sale_lines_dict = {sl['id']: (sl['sale.']['party.']['name'], sl['sale.']['number'], sl['unit_price']) for sl in sale_lines}
purchase_lines = PurchaseLine.search_read([('id', 'in', purchase_line_ids)], fields_names=['purchase.party.name', 'purchase.number', 'unit_price'])
purchase_lines_dict = {pl['id']: (pl['purchase.']['party.']['name'], pl['purchase.']['number'], pl['unit_price']) for pl in purchase_lines}
res = []
balance = end
cost_price = 0
for row in result:
qty_input = 0
qty_output = 0
list_price = 0
party = ''
number = ''
if row[6] == 'sale.line':
qty_output = row[9]
party, number, list_price = sale_lines_dict[int(row[7])]
balance -= qty_output
else:
qty_input = row[9]
party, number, cost_price = purchase_lines_dict[int(row[7])]
balance += qty_input
value = {
'product': row[2],
'code': row[1],
'brand': row[3],
'reference': row[4],
'effective_date': row[5],
'party': party,
'number': number,
'end_balance': balance,
'input': qty_input,
'output': qty_output,
'list_price': list_price,
'cost_price': cost_price,
}
res.append(value)
return res