Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD account_stock_situation #308

Open
wants to merge 5 commits into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions account_stock_situation/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
=======================
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]>
* Mathieu DELVA <[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
133 changes: 133 additions & 0 deletions account_stock_situation/models/company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
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()
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(self):
self.ensure_one()
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]
stock_quant_ids = self.env["stock.quant"].search(
[("location_id", "child_of", location_ids)],
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Du coup je ne pense pas kil faut utiliser le read_group vu que c pas top de stocker le warehouse ds les locations.

Tu pourrais modifier la PR en ce sens ?

products = self.env["product.product"].browse(stock_quant_ids.product_id.ids)
vals = defaultdict(list)
product_dict = {}
for stock_quant in stock_quant_ids:
if stock_quant.product_id.id not in product_dict:
product_dict[stock_quant.product_id.id] = [
stock_quant.warehouse_id,
stock_quant.quantity,
]
else:
if (
stock_quant.warehouse_id
in product_dict.get(stock_quant.product_id.id)[0]
):
product_dict[stock_quant.product_id.id][1] += stock_quant.quantity
else:
product_dict[stock_quant.product_id.id] += [
stock_quant.warehouse_id,
stock_quant.quantity,
]
for product_id, warehouse_quantities in product_dict.items():
product = products.filtered(lambda s: s.id == product_id)
vals["link"].append(
f"{base_url}/web#id={product_id}&cids={self.id}&action="
f"{self.env.ref('product.product_normal_action_sell').id}&model="
"product.product&view_type=form"
)
vals["code"].append(product.default_code)
vals["designation"].append(product.name)
for i in range(0, len(warehouse_quantities), 2):
warehouse_id = warehouse_quantities[i]
quantity = warehouse_quantities[i + 1]
vals[f"qty_{warehouse_id.code}"].append(round(quantity))
if len(warehouse_quantities) / 2 < len(self.valued_warehouse_ids):
warehouse_without_qty = self.valued_warehouse_ids.filtered(
lambda r: r.id
not in [
warehouse_quantities[i].id
for i in range(0, len(warehouse_quantities), 2)
]
)
for warehouse in warehouse_without_qty:
vals[f"qty_{warehouse.code}"].append(0)

vals["value"].append(round(product.standard_price, 2))
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["value"]), attach
37 changes: 37 additions & 0 deletions account_stock_situation/models/config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from odoo import fields, models


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

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",
)
account_stock_id = fields.Many2one(
comodel_name="account.account",
readonly=False,
related="company_id.account_stock_id",
)
account_stock_help = fields.Text(compute="_compute_account_stock_help")

def _compute_account_stock_help(self):
for rec in self:
name = ""
if "l10n_fr" in self.env.registry._init_modules:
name = "Compte de stock: '355...'"
name += "Compte de stock d'achat: '603...'"
rec.account_stock_help = name
3 changes: 3 additions & 0 deletions account_stock_situation/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* Akretion
* David BEAL <[email protected]>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Il faut te rajouter ici

* Mathieu DELVA <[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
Loading