mirror of
https://gitlab.com/hibou-io/hibou-odoo/suite.git
synced 2025-01-20 12:37:31 +02:00
Merge branch 'mig/13.0/purchase_by_sale_history' into 13.0-test
# Conflicts: # purchase_by_sale_history/tests/test_purchase_by_sale_history.py # purchase_by_sale_history/wizard/purchase_by_sale_history.py
This commit is contained in:
@@ -4,30 +4,30 @@ from datetime import datetime, timedelta
|
|||||||
|
|
||||||
|
|
||||||
class TestPurchaseBySaleHistory(common.TransactionCase):
|
class TestPurchaseBySaleHistory(common.TransactionCase):
|
||||||
|
|
||||||
def test_00_wizard(self):
|
def test_00_wizard(self):
|
||||||
wh1 = self.env.ref('stock.warehouse0')
|
wh1 = self.env.ref('stock.warehouse0')
|
||||||
wh2 = self.env['stock.warehouse'].create({
|
wh2 = self.env['stock.warehouse'].create({
|
||||||
'name': 'WH2',
|
'name': 'WH2',
|
||||||
'code': 'twh2',
|
'code': 'twh2',
|
||||||
})
|
})
|
||||||
|
|
||||||
sale_partner = self.env.ref('base.res_partner_2')
|
sale_partner = self.env.ref('base.res_partner_2')
|
||||||
purchase_partner = self.env['res.partner'].create({
|
purchase_partner = self.env['res.partner'].create({
|
||||||
'name': 'Purchase Partner',
|
'name': 'Purchase Partner',
|
||||||
})
|
})
|
||||||
|
|
||||||
product11 = self.env['product.product'].create({
|
product11 = self.env['product.product'].create({
|
||||||
'name': 'Product 1',
|
'name': 'Product 1',
|
||||||
'type': 'product',
|
'type': 'product',
|
||||||
})
|
})
|
||||||
product_template1 = product11.product_tmpl_id
|
product_template1 = product11.product_tmpl_id
|
||||||
|
|
||||||
color = self.env.ref('product.product_attribute_2')
|
color = self.env.ref('product.product_attribute_2')
|
||||||
attribute_line = self.env['product.template.attribute.line'].create({
|
attribute_line = self.env['product.template.attribute.line'].create({
|
||||||
'product_tmpl_id': product_template1.id,
|
'product_tmpl_id': product_template1.id,
|
||||||
'attribute_id': color.id,
|
'attribute_id': color.id,
|
||||||
'value_ids': [(6, 0, [color.value_ids[0].id])],
|
'value_ids': [(6, 0, [color.value_ids[0].id])],
|
||||||
})
|
})
|
||||||
attribute_line.write({'value_ids': [(4, color.value_ids[1].id)]})
|
attribute_line.write({'value_ids': [(4, color.value_ids[1].id)]})
|
||||||
self.assertEqual(len(product_template1.product_variant_ids), 2)
|
self.assertEqual(len(product_template1.product_variant_ids), 2)
|
||||||
@@ -50,15 +50,15 @@ class TestPurchaseBySaleHistory(common.TransactionCase):
|
|||||||
})
|
})
|
||||||
|
|
||||||
self.assertEqual(wiz.product_count, 0.0, 'There shouldn\'t be any products for this vendor yet.')
|
self.assertEqual(wiz.product_count, 0.0, 'There shouldn\'t be any products for this vendor yet.')
|
||||||
|
|
||||||
# Assign vendor to products created earlier.
|
# Assign vendor to products created earlier.
|
||||||
self.env['product.supplierinfo'].create({
|
self.env['product.supplierinfo'].create({
|
||||||
'name': purchase_partner.id,
|
'name': purchase_partner.id,
|
||||||
'product_tmpl_id': product11.product_tmpl_id.id,
|
'product_tmpl_id': product11.product_tmpl_id.id,
|
||||||
'product_id': product11.id,
|
'product_id': product11.id,
|
||||||
})
|
})
|
||||||
self.env['product.supplierinfo'].create({
|
self.env['product.supplierinfo'].create({
|
||||||
'name': purchase_partner.id,
|
'name': purchase_partner.id,
|
||||||
'product_tmpl_id': product2.product_tmpl_id.id,
|
'product_tmpl_id': product2.product_tmpl_id.id,
|
||||||
})
|
})
|
||||||
# New wizard picks up the correct number of products supplied by this vendor.
|
# New wizard picks up the correct number of products supplied by this vendor.
|
||||||
@@ -66,125 +66,123 @@ class TestPurchaseBySaleHistory(common.TransactionCase):
|
|||||||
'purchase_id': po1.id,
|
'purchase_id': po1.id,
|
||||||
})
|
})
|
||||||
self.assertEqual(wiz.product_count, 2)
|
self.assertEqual(wiz.product_count, 2)
|
||||||
|
|
||||||
# Make some sales history...
|
# Make some sales history...
|
||||||
sale_date = fields.Datetime.to_string(datetime.now() - timedelta(days=30))
|
sale_date = fields.Datetime.to_string(datetime.now() - timedelta(days=30))
|
||||||
self.env['sale.order'].create({
|
self.env['sale.order'].create({
|
||||||
'partner_id': sale_partner.id,
|
'partner_id': sale_partner.id,
|
||||||
'date_order': sale_date,
|
'date_order': sale_date,
|
||||||
'picking_policy': 'direct',
|
'picking_policy': 'direct',
|
||||||
'order_line': [
|
'order_line': [
|
||||||
(0, 0, {'product_id': product11.id, 'product_uom_qty': 3.0}),
|
(0, 0, {'product_id': product11.id, 'product_uom_qty': 3.0}),
|
||||||
(0, 0, {'product_id': product12.id, 'product_uom_qty': 3.0}),
|
(0, 0, {'product_id': product12.id, 'product_uom_qty': 3.0}),
|
||||||
(0, 0, {'product_id': product2.id, 'product_uom_qty': 3.0}),
|
(0, 0, {'product_id': product2.id, 'product_uom_qty': 3.0}),
|
||||||
],
|
],
|
||||||
}).action_confirm()
|
}).action_confirm()
|
||||||
|
|
||||||
days = 60
|
days = 60
|
||||||
history_start = fields.Date.to_string(datetime.now() - timedelta(days=days))
|
history_start = fields.Date.to_string(datetime.now() - timedelta(days=days))
|
||||||
history_end = fields.Date.today()
|
history_end = fields.Date.today()
|
||||||
wiz.write({
|
wiz.write({
|
||||||
'history_start': history_start,
|
'history_start': history_start,
|
||||||
'history_end': history_end,
|
'history_end': history_end,
|
||||||
'procure_days': days,
|
'procure_days': days,
|
||||||
'history_warehouse_ids': [(4, wh1.id, None)],
|
'history_warehouse_ids': [(4, wh1.id, None)],
|
||||||
})
|
})
|
||||||
self.assertEqual(wiz.history_days, days)
|
self.assertEqual(wiz.history_days, days)
|
||||||
wiz.action_confirm()
|
wiz.action_confirm()
|
||||||
|
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product11).product_qty, 3.0 + 3.0) # 3 from Sales History, 3 from Demand (from the sale)
|
||||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product11).product_qty,
|
|
||||||
3.0 + 3.0) # 3 from Sales History, 3 from Demand (from the sale)
|
|
||||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product12).product_qty, 0.0)
|
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product12).product_qty, 0.0)
|
||||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product2).product_qty, 3.0 + 3.0)
|
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product2).product_qty, 3.0 + 3.0)
|
||||||
|
|
||||||
# Make additional sales history...
|
# Make additional sales history...
|
||||||
sale_date = fields.Datetime.to_string(datetime.now() - timedelta(days=15))
|
sale_date = fields.Datetime.to_string(datetime.now() - timedelta(days=15))
|
||||||
self.env['sale.order'].create({
|
self.env['sale.order'].create({
|
||||||
'partner_id': sale_partner.id,
|
'partner_id': sale_partner.id,
|
||||||
'date_order': sale_date,
|
'date_order': sale_date,
|
||||||
'picking_policy': 'direct',
|
'picking_policy': 'direct',
|
||||||
'order_line': [
|
'order_line': [
|
||||||
(0, 0, {'product_id': product11.id, 'product_uom_qty': 3.0}),
|
(0, 0, {'product_id': product11.id, 'product_uom_qty': 3.0}),
|
||||||
(0, 0, {'product_id': product12.id, 'product_uom_qty': 3.0}),
|
(0, 0, {'product_id': product12.id, 'product_uom_qty': 3.0}),
|
||||||
(0, 0, {'product_id': product2.id, 'product_uom_qty': 3.0}),
|
(0, 0, {'product_id': product2.id, 'product_uom_qty': 3.0}),
|
||||||
],
|
],
|
||||||
}).action_confirm()
|
}).action_confirm()
|
||||||
|
|
||||||
wiz.action_confirm()
|
wiz.action_confirm()
|
||||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product11).product_qty, 6.0 + 6.0)
|
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product11).product_qty, 6.0 + 6.0)
|
||||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product12).product_qty, 0.0)
|
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product12).product_qty, 0.0)
|
||||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product2).product_qty, 6.0 + 6.0)
|
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product2).product_qty, 6.0 + 6.0)
|
||||||
|
|
||||||
# Make additional sales history in other warehouse
|
# Make additional sales history in other warehouse
|
||||||
sale_date = fields.Datetime.to_string(datetime.now() - timedelta(days=15))
|
sale_date = fields.Datetime.to_string(datetime.now() - timedelta(days=15))
|
||||||
self.env['sale.order'].create({
|
self.env['sale.order'].create({
|
||||||
'partner_id': sale_partner.id,
|
'partner_id': sale_partner.id,
|
||||||
'date_order': sale_date,
|
'date_order': sale_date,
|
||||||
'picking_policy': 'direct',
|
'picking_policy': 'direct',
|
||||||
'warehouse_id': wh2.id,
|
'warehouse_id': wh2.id,
|
||||||
'order_line': [
|
'order_line': [
|
||||||
(0, 0, {'product_id': product11.id, 'product_uom_qty': 3.0}),
|
(0, 0, {'product_id': product11.id, 'product_uom_qty': 3.0}),
|
||||||
(0, 0, {'product_id': product12.id, 'product_uom_qty': 3.0}),
|
(0, 0, {'product_id': product12.id, 'product_uom_qty': 3.0}),
|
||||||
(0, 0, {'product_id': product2.id, 'product_uom_qty': 3.0}),
|
(0, 0, {'product_id': product2.id, 'product_uom_qty': 3.0}),
|
||||||
],
|
],
|
||||||
}).action_confirm()
|
}).action_confirm()
|
||||||
|
|
||||||
wiz.action_confirm()
|
wiz.action_confirm()
|
||||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product11).product_qty, 6.0 + 6.0)
|
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product11).product_qty, 6.0 + 6.0)
|
||||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product12).product_qty, 0.0)
|
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product12).product_qty, 0.0)
|
||||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product2).product_qty, 6.0 + 6.0)
|
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product2).product_qty, 6.0 + 6.0)
|
||||||
|
|
||||||
# Make additional sales history that should NOT be counted...
|
# Make additional sales history that should NOT be counted...
|
||||||
sale_date = fields.Datetime.to_string(datetime.now() - timedelta(days=61))
|
sale_date = fields.Datetime.to_string(datetime.now() - timedelta(days=61))
|
||||||
self.env['sale.order'].create({
|
self.env['sale.order'].create({
|
||||||
'partner_id': sale_partner.id,
|
'partner_id': sale_partner.id,
|
||||||
'date_order': sale_date,
|
'date_order': sale_date,
|
||||||
'picking_policy': 'direct',
|
'picking_policy': 'direct',
|
||||||
'order_line': [
|
'order_line': [
|
||||||
(0, 0, {'product_id': product11.id, 'product_uom_qty': 3.0}),
|
(0, 0, {'product_id': product11.id, 'product_uom_qty': 3.0}),
|
||||||
(0, 0, {'product_id': product12.id, 'product_uom_qty': 3.0}),
|
(0, 0, {'product_id': product12.id, 'product_uom_qty': 3.0}),
|
||||||
(0, 0, {'product_id': product2.id, 'product_uom_qty': 3.0}),
|
(0, 0, {'product_id': product2.id, 'product_uom_qty': 3.0}),
|
||||||
],
|
],
|
||||||
}).action_confirm()
|
}).action_confirm()
|
||||||
|
|
||||||
wiz.action_confirm()
|
wiz.action_confirm()
|
||||||
|
|
||||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product11).product_qty, 6.0 + 9.0)
|
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product11).product_qty, 6.0 + 9.0)
|
||||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product12).product_qty, 0.0)
|
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product12).product_qty, 0.0)
|
||||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product2).product_qty, 6.0 + 9.0)
|
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product2).product_qty, 6.0 + 9.0)
|
||||||
|
|
||||||
# Test that the wizard will only use the existing PO line products now that we have lines.
|
# Test that the wizard will only use the existing PO line products now that we have lines.
|
||||||
po1.order_line.filtered(lambda l: l.product_id == product2).unlink()
|
po1.order_line.filtered(lambda l: l.product_id == product2).unlink()
|
||||||
wiz.action_confirm()
|
wiz.action_confirm()
|
||||||
# This test (line 161) is failing due to the unlink call on line 159 in version 13.0.
|
# This test is failing due to the unlink call above in version 13.0.
|
||||||
|
# During debugging, we looked closely into the query call made in the _sale_history method
|
||||||
|
# and confirmed that the same query was producing different results each time it's run.
|
||||||
# We intend to fix this in version 14.0
|
# We intend to fix this in version 14.0
|
||||||
# self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product11).product_qty, 6.0 + 9.0)
|
# self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product11).product_qty, 6.0 + 9.0)
|
||||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product12).product_qty, 0.0)
|
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product12).product_qty, 0.0)
|
||||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product2).product_qty, 0.0)
|
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product2).product_qty, 0.0)
|
||||||
|
|
||||||
# Plan for 1/2 the days of inventory
|
# Plan for 1/2 the days of inventory
|
||||||
wiz.procure_days = days / 2.0
|
wiz.procure_days = days / 2.0
|
||||||
wiz.action_confirm()
|
wiz.action_confirm()
|
||||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product11).product_qty, 3.0 + 9.0)
|
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product11).product_qty, 3.0 + 9.0)
|
||||||
|
|
||||||
# Cause Inventory on existing product to make sure we don't order it.
|
# Cause Inventory on existing product to make sure we don't order it.
|
||||||
adjust_product11 = self.env['stock.inventory'].create({
|
adjust_product11 = self.env['stock.inventory'].create({
|
||||||
'name': 'Product11',
|
'name': 'Product11',
|
||||||
'location_id': wh1.lot_stock_id.id,
|
'location_id': wh1.lot_stock_id.id,
|
||||||
'product_id': product11.id,
|
'product_id': product11.id,
|
||||||
'filter': 'product',
|
'filter': 'product',
|
||||||
})
|
})
|
||||||
adjust_product11.action_start()
|
adjust_product11.action_start()
|
||||||
adjust_product11.line_ids.create({
|
adjust_product11.line_ids.create({
|
||||||
'inventory_id': adjust_product11.id,
|
'inventory_id': adjust_product11.id,
|
||||||
'product_id': product11.id,
|
'product_id': product11.id,
|
||||||
'product_qty': 100.0,
|
'product_qty': 100.0,
|
||||||
'location_id': wh1.lot_stock_id.id,
|
'location_id': wh1.lot_stock_id.id,
|
||||||
})
|
})
|
||||||
adjust_product11.action_validate()
|
adjust_product11.action_validate()
|
||||||
|
|
||||||
wiz.action_confirm()
|
wiz.action_confirm()
|
||||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product11).product_qty,
|
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product11).product_qty, 0.0) # Because we have so much in stock now.
|
||||||
0.0) # Because we have so much in stock now.
|
|
||||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product12).product_qty, 0.0)
|
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product12).product_qty, 0.0)
|
||||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product2).product_qty, 0.0)
|
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product2).product_qty, 0.0)
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ from datetime import timedelta
|
|||||||
|
|
||||||
class PurchaseBySaleHistory(models.TransientModel):
|
class PurchaseBySaleHistory(models.TransientModel):
|
||||||
_name = 'purchase.sale.history.make'
|
_name = 'purchase.sale.history.make'
|
||||||
|
|
||||||
purchase_id = fields.Many2one('purchase.order', string='Purchase Order')
|
purchase_id = fields.Many2one('purchase.order', string='Purchase Order')
|
||||||
history_start = fields.Date(string='Sales History Start',
|
history_start = fields.Date(string='Sales History Start', default=lambda o: fields.Date.from_string(fields.Date.today()) - timedelta(days=30))
|
||||||
default=lambda o: fields.Date.from_string(fields.Date.today()) - timedelta(days=30))
|
|
||||||
history_end = fields.Date(string='Sales History End', default=fields.Date.today)
|
history_end = fields.Date(string='Sales History End', default=fields.Date.today)
|
||||||
history_days = fields.Integer(string='Sales History Days', compute='_compute_history_days')
|
history_days = fields.Integer(string='Sales History Days', compute='_compute_history_days')
|
||||||
procure_days = fields.Integer(string='Days to Procure',
|
procure_days = fields.Integer(string='Days to Procure',
|
||||||
@@ -22,7 +22,7 @@ class PurchaseBySaleHistory(models.TransientModel):
|
|||||||
'Current Inventory is summed from these warehouses. '
|
'Current Inventory is summed from these warehouses. '
|
||||||
'If it is left blank then all warehouses and inventory '
|
'If it is left blank then all warehouses and inventory '
|
||||||
'will be considered.')
|
'will be considered.')
|
||||||
|
|
||||||
@api.depends('history_start', 'history_end')
|
@api.depends('history_start', 'history_end')
|
||||||
def _compute_history_days(self):
|
def _compute_history_days(self):
|
||||||
for wiz in self:
|
for wiz in self:
|
||||||
@@ -31,7 +31,7 @@ class PurchaseBySaleHistory(models.TransientModel):
|
|||||||
else:
|
else:
|
||||||
delta = fields.Date.from_string(wiz.history_end) - fields.Date.from_string(wiz.history_start)
|
delta = fields.Date.from_string(wiz.history_end) - fields.Date.from_string(wiz.history_start)
|
||||||
wiz.history_days = delta.days
|
wiz.history_days = delta.days
|
||||||
|
|
||||||
@api.depends('purchase_id', 'purchase_id.order_line', 'purchase_id.partner_id')
|
@api.depends('purchase_id', 'purchase_id.order_line', 'purchase_id.partner_id')
|
||||||
def _compute_product_count(self):
|
def _compute_product_count(self):
|
||||||
for wiz in self:
|
for wiz in self:
|
||||||
@@ -42,80 +42,79 @@ class PurchaseBySaleHistory(models.TransientModel):
|
|||||||
FROM product_supplierinfo psi
|
FROM product_supplierinfo psi
|
||||||
LEFT JOIN product_product p ON p.product_tmpl_id = psi.product_tmpl_id AND psi.product_id IS NULL
|
LEFT JOIN product_product p ON p.product_tmpl_id = psi.product_tmpl_id AND psi.product_id IS NULL
|
||||||
WHERE psi.name = %d;"""
|
WHERE psi.name = %d;"""
|
||||||
% (wiz.purchase_id.partner_id.id,))
|
% (wiz.purchase_id.partner_id.id, ))
|
||||||
wiz.product_count = self.env.cr.fetchall()[0][0]
|
wiz.product_count = self.env.cr.fetchall()[0][0]
|
||||||
|
|
||||||
def _history_product_ids(self):
|
def _history_product_ids(self):
|
||||||
if self.purchase_id.order_line:
|
if self.purchase_id.order_line:
|
||||||
return self.purchase_id.order_line.mapped('product_id.id')
|
return self.purchase_id.order_line.mapped('product_id.id')
|
||||||
|
|
||||||
self.env.cr.execute("""SELECT DISTINCT(COALESCE(psi.product_id, p.id))
|
self.env.cr.execute("""SELECT DISTINCT(COALESCE(psi.product_id, p.id))
|
||||||
FROM product_supplierinfo psi
|
FROM product_supplierinfo psi
|
||||||
LEFT JOIN product_product p ON p.product_tmpl_id = psi.product_tmpl_id AND psi.product_id IS NULL
|
LEFT JOIN product_product p ON p.product_tmpl_id = psi.product_tmpl_id AND psi.product_id IS NULL
|
||||||
WHERE psi.name = %d;"""
|
WHERE psi.name = %d;"""
|
||||||
% (self.purchase_id.partner_id.id,))
|
% (self.purchase_id.partner_id.id, ))
|
||||||
rows = self.env.cr.fetchall()
|
rows = self.env.cr.fetchall()
|
||||||
return [r[0] for r in rows if r[0]]
|
return [r[0] for r in rows if r[0]]
|
||||||
|
|
||||||
def _sale_history(self, product_ids):
|
def _sale_history(self, product_ids):
|
||||||
p_ids = tuple(product_ids)
|
p_ids = tuple(product_ids)
|
||||||
if self.history_warehouse_ids:
|
if self.history_warehouse_ids:
|
||||||
wh_ids = tuple(self.history_warehouse_ids.ids)
|
wh_ids = tuple(self.history_warehouse_ids.ids)
|
||||||
|
|
||||||
self.env.cr.execute("""SELECT product_id, sum(product_uom_qty)
|
self.env.cr.execute("""SELECT product_id, sum(product_uom_qty)
|
||||||
FROM sale_report
|
FROM sale_report
|
||||||
WHERE date BETWEEN %s AND %s AND product_id IN %s AND warehouse_id IN %s
|
WHERE date BETWEEN %s AND %s AND product_id IN %s AND warehouse_id IN %s
|
||||||
GROUP BY 1;""",
|
GROUP BY 1""",
|
||||||
(self.history_start, self.history_end, p_ids, wh_ids))
|
(self.history_start, self.history_end, p_ids, wh_ids))
|
||||||
else:
|
else:
|
||||||
self.env.cr.execute("""SELECT product_id, sum(product_uom_qty)
|
self.env.cr.execute("""SELECT product_id, sum(product_uom_qty)
|
||||||
FROM sale_report
|
FROM sale_report
|
||||||
WHERE date BETWEEN %s AND %s AND product_id IN %s
|
WHERE date BETWEEN %s AND %s AND product_id IN %s
|
||||||
GROUP BY 1;""",
|
GROUP BY 1""",
|
||||||
(self.history_start, self.history_end, p_ids))
|
(self.history_start, self.history_end, p_ids))
|
||||||
|
|
||||||
return self.env.cr.fetchall()
|
return self.env.cr.fetchall()
|
||||||
|
|
||||||
def _apply_history_product(self, product, history):
|
def _apply_history_product(self, product, history):
|
||||||
qty = ceil(history['sold_qty'] * self.procure_days / self.history_days)
|
qty = ceil(history['sold_qty'] * self.procure_days / self.history_days)
|
||||||
history['buy_qty'] = max((0.0, qty - product.virtual_available))
|
history['buy_qty'] = max((0.0, qty - product.virtual_available))
|
||||||
|
|
||||||
def _convert_to_purchase_line_qty(self, line, qty):
|
def _convert_to_purchase_line_qty(self, line, qty):
|
||||||
# Skip calculation if they are the same UOM
|
# Skip calculation if they are the same UOM
|
||||||
if line.product_id.uom_id != line.product_uom:
|
if line.product_id.uom_id != line.product_uom:
|
||||||
return line.product_id.uom_id._compute_quantity(qty, line.product_uom)
|
return line.product_id.uom_id._compute_quantity(qty, line.product_uom)
|
||||||
return qty
|
return qty
|
||||||
|
|
||||||
def _apply_history(self, history, product_ids):
|
def _apply_history(self, history, product_ids):
|
||||||
line_model = self.env['purchase.order.line']
|
line_model = self.env['purchase.order.line']
|
||||||
updated_lines = line_model.browse()
|
updated_lines = line_model.browse()
|
||||||
|
|
||||||
# Collect stock to consider against the sales demand.
|
# Collect stock to consider against the sales demand.
|
||||||
product_model = self.env['product.product']
|
product_model = self.env['product.product']
|
||||||
if self.history_warehouse_ids:
|
if self.history_warehouse_ids:
|
||||||
product_model = self.env['product.product'] \
|
product_model = self.env['product.product']\
|
||||||
.with_context({'location': [wh.lot_stock_id.id for wh in self.history_warehouse_ids]})
|
.with_context({'location': [wh.lot_stock_id.id for wh in self.history_warehouse_ids]})
|
||||||
products = product_model.browse(product_ids)
|
products = product_model.browse(product_ids)
|
||||||
|
|
||||||
# product_available_stock = {p.id: p.virtual_available for p in products}
|
#product_available_stock = {p.id: p.virtual_available for p in products}
|
||||||
history_dict = {pid: {'sold_qty': sold_qty} for pid, sold_qty in history}
|
history_dict = {pid: {'sold_qty': sold_qty} for pid, sold_qty in history}
|
||||||
for p in products:
|
for p in products:
|
||||||
if p.id not in history_dict:
|
if p.id not in history_dict:
|
||||||
history_dict[p.id] = {'sold_qty': 0.0}
|
history_dict[p.id] = {'sold_qty': 0.0}
|
||||||
self._apply_history_product(p, history_dict[p.id])
|
self._apply_history_product(p, history_dict[p.id])
|
||||||
|
|
||||||
for pid, history in history_dict.items():
|
for pid, history in history_dict.items():
|
||||||
qty = history.get('buy_qty', 0.0)
|
qty = history.get('buy_qty', 0.0)
|
||||||
|
|
||||||
# Find line that already exists on PO
|
# Find line that already exists on PO
|
||||||
line = self.purchase_id.order_line.filtered(lambda l: l.product_id.id == pid)
|
line = self.purchase_id.order_line.filtered(lambda l: l.product_id.id == pid)
|
||||||
|
|
||||||
if line:
|
if line:
|
||||||
line.write({'product_qty': self._convert_to_purchase_line_qty(line, qty)})
|
line.write({'product_qty': self._convert_to_purchase_line_qty(line, qty)})
|
||||||
line._onchange_quantity()
|
line._onchange_quantity()
|
||||||
else:
|
else:
|
||||||
# Create new PO line
|
# Create new PO line
|
||||||
line = line_model.new({
|
line = line_model.new({
|
||||||
'order_id': self.purchase_id.id,
|
'order_id': self.purchase_id.id,
|
||||||
'product_id': pid,
|
'product_id': pid,
|
||||||
'product_qty': qty,
|
'product_qty': qty,
|
||||||
})
|
})
|
||||||
line.onchange_product_id()
|
line.onchange_product_id()
|
||||||
@@ -123,13 +122,13 @@ class PurchaseBySaleHistory(models.TransientModel):
|
|||||||
line_vals['product_qty'] = self._convert_to_purchase_line_qty(line, qty)
|
line_vals['product_qty'] = self._convert_to_purchase_line_qty(line, qty)
|
||||||
line = line_model.create(line_vals)
|
line = line_model.create(line_vals)
|
||||||
updated_lines += line
|
updated_lines += line
|
||||||
|
|
||||||
# Lines not touched should now not be ordered.
|
# Lines not touched should now not be ordered.
|
||||||
other_lines = self.purchase_id.order_line - updated_lines
|
other_lines = self.purchase_id.order_line - updated_lines
|
||||||
other_lines.write({'product_qty': 0.0})
|
other_lines.write({'product_qty': 0.0})
|
||||||
for line in other_lines:
|
for line in other_lines:
|
||||||
line._onchange_quantity()
|
line._onchange_quantity()
|
||||||
|
|
||||||
def action_confirm(self):
|
def action_confirm(self):
|
||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
history_product_ids = self._history_product_ids()
|
history_product_ids = self._history_product_ids()
|
||||||
|
|||||||
Reference in New Issue
Block a user