Skip to content

Commit

Permalink
ADD account_stock_situation
Browse files Browse the repository at this point in the history
  • Loading branch information
bealdav committed Jul 11, 2024
1 parent d104027 commit 6f0e597
Show file tree
Hide file tree
Showing 15 changed files with 790 additions and 0 deletions.
66 changes: 66 additions & 0 deletions account_stock_situation/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
=======================
Account Stock Situation
=======================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:17a4d0785da8b351798bf3c7d2b0463b8a22bf996209b21ecc9043ea00b9bfce
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-akretion%2Fak--odoo--incubator-lightgray.png?logo=github
:target: https://github.com/akretion/ak-odoo-incubator/tree/16.0/account_stock_situation
:alt: akretion/ak-odoo-incubator

|badge1| |badge2| |badge3|

Generate Periodically a miscellaneous account move for stock valuation with an xlsx report attachement

**Table of contents**

.. contents::
:local:

Usage
=====

Consider to check your report before validate your account move and reverse the previous one.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/akretion/ak-odoo-incubator/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/akretion/ak-odoo-incubator/issues/new?body=module:%20account_stock_situation%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Akretion

Contributors
~~~~~~~~~~~~

* Akretion
* David BEAL <[email protected]>

Maintainers
~~~~~~~~~~~

This module is part of the `akretion/ak-odoo-incubator <https://github.com/akretion/ak-odoo-incubator/tree/16.0/account_stock_situation>`_ project on GitHub.

You are welcome to contribute.
1 change: 1 addition & 0 deletions account_stock_situation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
19 changes: 19 additions & 0 deletions account_stock_situation/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2024 David BEAL @ akretion
{
"name": "Account Stock Situation",
"version": "16.0.1.0.0",
"author": "Akretion",
"website": "https://github.com/akretion/ak-odoo-incubator",
"license": "AGPL-3",
"category": "Accounting",
"depends": [
"account",
"stock",
],
"data": [
"views/config_settings.xml",
"views/action.xml",
],
"external_dependencies": {"python": ["polars"]},
"installable": True,
}
2 changes: 2 additions & 0 deletions account_stock_situation/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import company
from . import config_settings
116 changes: 116 additions & 0 deletions account_stock_situation/models/company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import base64
import io
from collections import defaultdict

import polars as pl

from odoo import fields, models, tools
from odoo.exceptions import UserError


class ResCompany(models.Model):
_inherit = "res.company"

valued_warehouse_ids = fields.Many2many(
comodel_name="stock.warehouse",
domain=lambda self: [("company_id", "=", self.env.company.id)],
)
stock_journal_id = fields.Many2one(comodel_name="account.journal")
cost_vs_purchase_threshold = fields.Integer(string="Seuil en %")
account_purchase_stock_id = fields.Many2one(comodel_name="account.account")
account_stock_id = fields.Many2one(comodel_name="account.account")

def _set_account_stock_valuation(self, company_string_id):
self = self.env.ref(company_string_id)
value, attach = self._get_stock_valuation_another()
for mfield in (
"account_stock_id",
"account_purchase_stock_id",
"stock_journal_id",
):
if not self[mfield]:
raise UserError(
f"Le champ '{mfield}' n'est pas défini: vérifiez les "
"paramètres intitulés 'Valorisation de stock'"
)
move = self.env["account.move"].create(
{
"journal_id": self.stock_journal_id.id,
"company_id": self.id,
"to_check": True,
"move_type": "entry",
"line_ids": [
(
0,
0,
{
"account_id": self.account_stock_id.id,
"name": "stock",
"debit": 0,
"credit": value,
},
),
(
0,
0,
{
"account_id": self.account_purchase_stock_id.id,
"name": "stock",
"debit": value,
"credit": 0,
},
),
],
}
)
attach.res_id = move.id

def _get_stock_valuation_another(self):
self.ensure_one()
coef = self.cost_vs_purchase_threshold
base_url = self.env["ir.config_parameter"].sudo().get_param("web.base.url")
if tools.config.get("running_env") == "dev":
base_url = "http://anothercorp.localhost/"
location_ids = [x.lot_stock_id.id for x in self.valued_warehouse_ids]
product_qties = self.env["stock.quant"].read_group(
[("location_id", "child_of", location_ids)],
["product_id", "quantity"],
["product_id"],
)
product_ids = [x["product_id"][0] for x in product_qties]
products = self.env["product.product"].browse(product_ids)
prices = {
x: x.variant_seller_ids and x.variant_seller_ids[0] or 0 for x in products
}
vals = defaultdict(list)
for prd_q in product_qties:
product = products.filtered(lambda s: s.id == prd_q["product_id"][0])
vals["code"].append(product.default_code)
vals["designation"].append(product.name)
vals["qté"].append(round(prd_q["quantity"]))
vals["valeur"].append(
round(
max(
product.standard_price,
prices[product] and prices[product].price or 0 * coef / 100,
)
* prd_q["quantity"]
)
)
vals["lien"].append(
f"{base_url}/web#id={prd_q['product_id'][0]}&cids={self.id}&action="
f"{self.env.ref('product.product_normal_action_sell').id}&model="
"product.product&view_type=form"
)
df = pl.from_dict(vals)
mfile = io.BytesIO()
df.write_excel(workbook=mfile)
attach = self.env["ir.attachment"].create(
{
"name": "Valorisation_stock_jourdain",
"type": "binary",
"res_model": "account.move",
"datas": base64.b64encode(mfile.getvalue()),
}
)
return sum(vals["valeur"]), attach
50 changes: 50 additions & 0 deletions account_stock_situation/models/config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from odoo import fields, models


class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"

def __default_account_stock_domain(self):
return self._account_stock_domain()

def __default_account_purchase_stock_domain(self):
return self._account_stock_domain(kind="purchase")

valued_warehouse_ids = fields.Many2many(
related="company_id.valued_warehouse_ids", readonly=False
)
cost_vs_purchase_threshold = fields.Integer(
related="company_id.cost_vs_purchase_threshold", readonly=False, default=120
)
stock_journal_id = fields.Many2one(
comodel_name="account.journal",
readonly=False,
related="company_id.stock_journal_id",
domain=[("type", "=", "general")],
)
account_purchase_stock_id = fields.Many2one(
comodel_name="account.account",
readonly=False,
related="company_id.account_purchase_stock_id",
domain=[("code", "ilike", lambda s: s.account_purchase_stock_domain)],
)
account_stock_id = fields.Many2one(
comodel_name="account.account",
readonly=False,
related="company_id.account_stock_id",
domain=[("code", "ilike", lambda s: s.account_stock_domain)],
)
account_stock_domain = fields.Char(default=__default_account_stock_domain)
account_purchase_stock_domain = fields.Char(
default=__default_account_purchase_stock_domain
)

def _account_stock_domain(self, kind=None):
"Other localisations maybe completed here"
if "l10n_fr" in self.env.registry:
if not kind:
return "355%00"
elif kind == "purchase":
return "603%"
else:
return "%"
2 changes: 2 additions & 0 deletions account_stock_situation/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Akretion
* David BEAL <[email protected]>
1 change: 1 addition & 0 deletions account_stock_situation/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generate Periodically a miscellaneous account move for stock valuation with an xlsx report attachement
1 change: 1 addition & 0 deletions account_stock_situation/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Consider to check your report before validate your account move and reverse the previous one.
Loading

0 comments on commit 6f0e597

Please sign in to comment.