mirror of
https://gitlab.com/hibou-io/hibou-odoo/suite.git
synced 2025-01-20 12:37:31 +02:00
Initial commit of purchase_by_sale_history module for 11.0
This commit is contained in:
1
purchase_by_sale_history/__init__.py
Executable file
1
purchase_by_sale_history/__init__.py
Executable file
@@ -0,0 +1 @@
|
||||
from . import wizard
|
||||
21
purchase_by_sale_history/__manifest__.py
Executable file
21
purchase_by_sale_history/__manifest__.py
Executable file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
'name': 'Purchase by Sale History',
|
||||
'author': 'Hibou Corp. <hello@hibou.io>',
|
||||
'version': '11.0.1.0.0',
|
||||
'category': 'Purchases',
|
||||
'sequence': 95,
|
||||
'summary': 'Fill Purchase Orders by Sales History',
|
||||
'description': """
|
||||
Adds wizard to Purchase Orders that will fill the purchase order with products based on sales history.
|
||||
""",
|
||||
'website': 'https://hibou.io/',
|
||||
'depends': [
|
||||
'sale_stock',
|
||||
'purchase',
|
||||
],
|
||||
'data': [
|
||||
'wizard/purchase_by_sale_history_views.xml',
|
||||
],
|
||||
'installable': True,
|
||||
'application': False,
|
||||
}
|
||||
1
purchase_by_sale_history/tests/__init__.py
Normal file
1
purchase_by_sale_history/tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from . import test_purchase_by_sale_history
|
||||
133
purchase_by_sale_history/tests/test_purchase_by_sale_history.py
Normal file
133
purchase_by_sale_history/tests/test_purchase_by_sale_history.py
Normal file
@@ -0,0 +1,133 @@
|
||||
from odoo import fields
|
||||
from odoo.tests import common
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
|
||||
class TestPurchaseBySaleHistory(common.TransactionCase):
|
||||
|
||||
def test_00_wizard(self):
|
||||
sale_partner = self.env.ref('base.res_partner_2')
|
||||
purchase_partner = self.env['res.partner'].create({
|
||||
'name': 'Purchase Partner',
|
||||
})
|
||||
|
||||
product11 = self.env['product.product'].create({
|
||||
'name': 'Product 1',
|
||||
'type': 'product',
|
||||
})
|
||||
product12 = self.env['product.product'].create({
|
||||
'name': 'Product 1.1',
|
||||
'type': 'product',
|
||||
'product_tmpl_id': product11.product_tmpl_id.id,
|
||||
})
|
||||
product2 = self.env['product.product'].create({
|
||||
'name': 'Product 2',
|
||||
'type': 'product',
|
||||
})
|
||||
|
||||
po1 = self.env['purchase.order'].create({
|
||||
'partner_id': purchase_partner.id,
|
||||
})
|
||||
|
||||
# Create initial wizard, it won't apply to any products because the PO is empty, and the vendor
|
||||
# doesn't supply any products yet.
|
||||
wiz = self.env['purchase.sale.history.make'].create({
|
||||
'purchase_id': po1.id,
|
||||
})
|
||||
|
||||
self.assertEqual(wiz.product_count, 0.0, 'There shouldn\'t be any products for this vendor yet.')
|
||||
|
||||
# Assign vendor to products created earlier.
|
||||
self.env['product.supplierinfo'].create({
|
||||
'name': purchase_partner.id,
|
||||
'product_tmpl_id': product11.product_tmpl_id.id,
|
||||
'product_id': product11.id,
|
||||
})
|
||||
self.env['product.supplierinfo'].create({
|
||||
'name': purchase_partner.id,
|
||||
'product_tmpl_id': product2.product_tmpl_id.id,
|
||||
})
|
||||
# New wizard picks up the correct number of products supplied by this vendor.
|
||||
wiz = self.env['purchase.sale.history.make'].create({
|
||||
'purchase_id': po1.id,
|
||||
})
|
||||
self.assertEqual(wiz.product_count, 2)
|
||||
|
||||
# Make some sales history...
|
||||
sale_date = fields.Datetime.to_string(datetime.now() - timedelta(days=30))
|
||||
self.env['sale.order'].create({
|
||||
'partner_id': sale_partner.id,
|
||||
'date_order': sale_date,
|
||||
'confirmation_date': sale_date,
|
||||
'picking_policy': 'direct',
|
||||
'order_line': [
|
||||
(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': product2.id, 'product_uom_qty': 3.0}),
|
||||
],
|
||||
}).action_confirm()
|
||||
|
||||
days = 60
|
||||
history_start = fields.Date.to_string(datetime.now() - timedelta(days=days))
|
||||
history_end = fields.Date.today()
|
||||
wiz.write({
|
||||
'history_start': history_start,
|
||||
'history_end': history_end,
|
||||
'procure_days': days,
|
||||
})
|
||||
self.assertEqual(wiz.history_days, days)
|
||||
wiz.action_confirm()
|
||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product11).product_qty, 3.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)
|
||||
|
||||
# Make additional sales history...
|
||||
sale_date = fields.Datetime.to_string(datetime.now() - timedelta(days=15))
|
||||
self.env['sale.order'].create({
|
||||
'partner_id': sale_partner.id,
|
||||
'date_order': sale_date,
|
||||
'confirmation_date': sale_date,
|
||||
'picking_policy': 'direct',
|
||||
'order_line': [
|
||||
(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': product2.id, 'product_uom_qty': 3.0}),
|
||||
],
|
||||
}).action_confirm()
|
||||
|
||||
wiz.action_confirm()
|
||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product11).product_qty, 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 == product2).product_qty, 6.0)
|
||||
|
||||
# Make additional sales history that should NOT be counted...
|
||||
sale_date = fields.Datetime.to_string(datetime.now() - timedelta(days=61))
|
||||
self.env['sale.order'].create({
|
||||
'partner_id': sale_partner.id,
|
||||
'date_order': sale_date,
|
||||
'confirmation_date': sale_date,
|
||||
'picking_policy': 'direct',
|
||||
'order_line': [
|
||||
(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': product2.id, 'product_uom_qty': 3.0}),
|
||||
],
|
||||
}).action_confirm()
|
||||
|
||||
wiz.action_confirm()
|
||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product11).product_qty, 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 == product2).product_qty, 6.0)
|
||||
|
||||
# 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()
|
||||
wiz.action_confirm()
|
||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product11).product_qty, 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 == product2).product_qty, 0.0)
|
||||
|
||||
# Plan for 1/2 the days of inventory
|
||||
wiz.procure_days = days / 2.0
|
||||
wiz.action_confirm()
|
||||
self.assertEqual(po1.order_line.filtered(lambda l: l.product_id == product11).product_qty, 6.0 / 2.0)
|
||||
|
||||
1
purchase_by_sale_history/wizard/__init__.py
Normal file
1
purchase_by_sale_history/wizard/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from . import purchase_by_sale_history
|
||||
100
purchase_by_sale_history/wizard/purchase_by_sale_history.py
Normal file
100
purchase_by_sale_history/wizard/purchase_by_sale_history.py
Normal file
@@ -0,0 +1,100 @@
|
||||
from odoo import api, fields, models
|
||||
from math import ceil
|
||||
from datetime import timedelta
|
||||
|
||||
|
||||
class PurchaseBySaleHistory(models.TransientModel):
|
||||
_name = 'purchase.sale.history.make'
|
||||
|
||||
purchase_id = fields.Many2one('purchase.order', string='Purchase Order')
|
||||
history_start = fields.Date(string='Sales History Start', 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_days = fields.Integer(string='Sales History Days', compute='_compute_history_days')
|
||||
procure_days = fields.Integer(string='Days to Procure',
|
||||
default=30,
|
||||
help='History will be computed as an average per day, '
|
||||
'and then multiplied by the days you wish to procure for.')
|
||||
product_count = fields.Integer(string='Product Count', compute='_compute_product_count',
|
||||
help='Products on the PO or that the Vendor provides.')
|
||||
|
||||
@api.multi
|
||||
@api.depends('history_start', 'history_end')
|
||||
def _compute_history_days(self):
|
||||
for wiz in self:
|
||||
if not all((wiz.history_end, wiz.history_start)):
|
||||
wiz.history_days = 0
|
||||
else:
|
||||
delta = fields.Date.from_string(wiz.history_end) - fields.Date.from_string(wiz.history_start)
|
||||
wiz.history_days = delta.days
|
||||
|
||||
@api.multi
|
||||
@api.depends('purchase_id', 'purchase_id.order_line', 'purchase_id.partner_id')
|
||||
def _compute_product_count(self):
|
||||
for wiz in self:
|
||||
if wiz.purchase_id.order_line:
|
||||
wiz.product_count = len(set(wiz.purchase_id.order_line.mapped('product_id.id')))
|
||||
elif wiz.purchase_id.partner_id:
|
||||
self.env.cr.execute("""SELECT COUNT(DISTINCT(psi.product_id)) + COUNT(DISTINCT(p.id))
|
||||
FROM product_supplierinfo psi
|
||||
LEFT JOIN product_product p ON p.product_tmpl_id = psi.product_tmpl_id AND psi.product_id IS NULL
|
||||
WHERE psi.name = %d;"""
|
||||
% (wiz.purchase_id.partner_id.id, ))
|
||||
wiz.product_count = self.env.cr.fetchall()[0][0]
|
||||
|
||||
def _history_product_ids(self):
|
||||
if self.purchase_id.order_line:
|
||||
return self.purchase_id.order_line.mapped('product_id.id')
|
||||
|
||||
self.env.cr.execute("""SELECT DISTINCT(COALESCE(psi.product_id, p.id))
|
||||
FROM product_supplierinfo psi
|
||||
LEFT JOIN product_product p ON p.product_tmpl_id = psi.product_tmpl_id AND psi.product_id IS NULL
|
||||
WHERE psi.name = %d;"""
|
||||
% (self.purchase_id.partner_id.id, ))
|
||||
rows = self.env.cr.fetchall()
|
||||
return [r[0] for r in rows if r[0]]
|
||||
|
||||
def _sale_history(self, product_ids):
|
||||
self.env.cr.execute("""SELECT product_id, sum(product_uom_qty)
|
||||
FROM sale_report
|
||||
WHERE date BETWEEN %s AND %s AND product_id IN %s
|
||||
GROUP BY 1""", (self.history_start, self.history_end, tuple(product_ids)))
|
||||
return self.env.cr.fetchall()
|
||||
|
||||
def _apply_history(self, history):
|
||||
line_model = self.env['purchase.order.line']
|
||||
updated_lines = line_model.browse()
|
||||
for pid, sold_qty in history:
|
||||
# TODO: Should convert from Sale UOM to Purchase UOM
|
||||
qty = ceil(sold_qty * self.procure_days / self.history_days)
|
||||
# Find line that already exists on PO
|
||||
line = self.purchase_id.order_line.filtered(lambda l: l.product_id.id == pid)
|
||||
if line:
|
||||
line.write({'product_qty': qty})
|
||||
line._onchange_quantity()
|
||||
else:
|
||||
# Create new PO line
|
||||
line = line_model.new({
|
||||
'order_id': self.purchase_id.id,
|
||||
'product_id': pid,
|
||||
'product_qty': qty,
|
||||
})
|
||||
line.onchange_product_id()
|
||||
line_vals = line._convert_to_write(line._cache)
|
||||
line_vals['product_qty'] = qty
|
||||
line = line_model.create(line_vals)
|
||||
updated_lines += line
|
||||
|
||||
# Lines not touched should now not be ordered.
|
||||
other_lines = self.purchase_id.order_line - updated_lines
|
||||
other_lines.write({'product_qty': 0.0})
|
||||
for line in other_lines:
|
||||
line._onchange_quantity()
|
||||
|
||||
@api.multi
|
||||
def action_confirm(self):
|
||||
self.ensure_one()
|
||||
history_product_ids = self._history_product_ids()
|
||||
history = self._sale_history(history_product_ids)
|
||||
self._apply_history(history)
|
||||
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<odoo>
|
||||
<record id="purchase_sale_history_make_form" model="ir.ui.view">
|
||||
<field name="name">purchase.sale.history.make.form</field>
|
||||
<field name="model">purchase.sale.history.make</field>
|
||||
<field name="arch" type="xml">
|
||||
<form string="Fill PO From Sales History">
|
||||
<sheet>
|
||||
<field name="id" invisible="1"/>
|
||||
<field name="purchase_id" invisible="1"/>
|
||||
<group>
|
||||
<group>
|
||||
<field name="history_start"/>
|
||||
<field name="history_end"/>
|
||||
<field name="history_days"/>
|
||||
</group>
|
||||
<group>
|
||||
<field name="procure_days"/>
|
||||
<field name="product_count"/>
|
||||
</group>
|
||||
</group>
|
||||
</sheet>
|
||||
<footer>
|
||||
<button string='Run' name="action_confirm" type="object" class="btn-primary"/>
|
||||
<button string="Cancel" class="btn-default" special="cancel"/>
|
||||
</footer>
|
||||
</form>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="purchase_sale_history_make_action" model="ir.actions.act_window">
|
||||
<field name="name">Fill PO From Sales History</field>
|
||||
<field name="res_model">purchase.sale.history.make</field>
|
||||
<field name="view_type">form</field>
|
||||
<field name="view_mode">form</field>
|
||||
<field name="view_id" ref="purchase_sale_history_make_form"/>
|
||||
<field name="target">new</field>
|
||||
<field name="context">{'default_purchase_id': active_id}</field>
|
||||
</record>
|
||||
|
||||
<!-- Button on Purchase Order to launch wizard -->
|
||||
<record id="purchase_order_form_inherit" model="ir.ui.view">
|
||||
<field name="name">purchase.order.form.inherit</field>
|
||||
<field name="model">purchase.order</field>
|
||||
<field name="inherit_id" ref="purchase.purchase_order_form"/>
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//button[@name='button_confirm']" position="after">
|
||||
<button name="%(purchase_by_sale_history.purchase_sale_history_make_action)d" type="action" string="Fill by Sales" attrs="{'invisible': ['|', ('state', 'in', ('purchase', 'done', 'cancel')), ('partner_id', '=', False)]}"/>
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
</odoo>
|
||||
Reference in New Issue
Block a user