|
| 1 | +import base64 |
| 2 | +import io |
| 3 | +from collections import defaultdict |
| 4 | + |
| 5 | +import polars as pl |
| 6 | + |
| 7 | +from odoo import fields, models, tools |
| 8 | +from odoo.exceptions import UserError |
| 9 | + |
| 10 | + |
| 11 | +class ResCompany(models.Model): |
| 12 | + _inherit = "res.company" |
| 13 | + |
| 14 | + valued_warehouse_ids = fields.Many2many( |
| 15 | + comodel_name="stock.warehouse", |
| 16 | + domain=lambda self: [("company_id", "=", self.env.company.id)], |
| 17 | + ) |
| 18 | + stock_journal_id = fields.Many2one(comodel_name="account.journal") |
| 19 | + cost_vs_purchase_threshold = fields.Integer(string="Seuil en %") |
| 20 | + account_purchase_stock_id = fields.Many2one(comodel_name="account.account") |
| 21 | + account_stock_id = fields.Many2one(comodel_name="account.account") |
| 22 | + |
| 23 | + def _set_account_stock_valuation(self, company_string_id): |
| 24 | + self = self.env.ref(company_string_id) |
| 25 | + value, attach = self._get_stock_valuation_another() |
| 26 | + for mfield in ( |
| 27 | + "account_stock_id", |
| 28 | + "account_purchase_stock_id", |
| 29 | + "stock_journal_id", |
| 30 | + ): |
| 31 | + if not self[mfield]: |
| 32 | + raise UserError( |
| 33 | + f"Le champ '{mfield}' n'est pas défini: vérifiez les " |
| 34 | + "paramètres intitulés 'Valorisation de stock'" |
| 35 | + ) |
| 36 | + move = self.env["account.move"].create( |
| 37 | + { |
| 38 | + "journal_id": self.stock_journal_id.id, |
| 39 | + "company_id": self.id, |
| 40 | + "to_check": True, |
| 41 | + "move_type": "entry", |
| 42 | + "line_ids": [ |
| 43 | + ( |
| 44 | + 0, |
| 45 | + 0, |
| 46 | + { |
| 47 | + "account_id": self.account_stock_id.id, |
| 48 | + "name": "stock", |
| 49 | + "debit": 0, |
| 50 | + "credit": value, |
| 51 | + }, |
| 52 | + ), |
| 53 | + ( |
| 54 | + 0, |
| 55 | + 0, |
| 56 | + { |
| 57 | + "account_id": self.account_purchase_stock_id.id, |
| 58 | + "name": "stock", |
| 59 | + "debit": value, |
| 60 | + "credit": 0, |
| 61 | + }, |
| 62 | + ), |
| 63 | + ], |
| 64 | + } |
| 65 | + ) |
| 66 | + attach.res_id = move.id |
| 67 | + |
| 68 | + def _get_stock_valuation_another(self): |
| 69 | + self.ensure_one() |
| 70 | + coef = self.cost_vs_purchase_threshold |
| 71 | + base_url = self.env["ir.config_parameter"].sudo().get_param("web.base.url") |
| 72 | + if tools.config.get("running_env") == "dev": |
| 73 | + base_url = "http://anothercorp.localhost/" |
| 74 | + location_ids = [x.lot_stock_id.id for x in self.valued_warehouse_ids] |
| 75 | + # TODO conserver un group par emplacement : donc autant de colonnes |
| 76 | + # de nombres de produits que d'entrepots dans l'excel |
| 77 | + product_qties = self.env["stock.quant"].read_group( |
| 78 | + [("location_id", "child_of", location_ids)], |
| 79 | + ["product_id", "quantity"], |
| 80 | + ["product_id"], |
| 81 | + ) |
| 82 | + product_ids = [x["product_id"][0] for x in product_qties] |
| 83 | + products = self.env["product.product"].browse(product_ids) |
| 84 | + prices = { |
| 85 | + x: x.variant_seller_ids and x.variant_seller_ids[0] or 0 for x in products |
| 86 | + } |
| 87 | + vals = defaultdict(list) |
| 88 | + for prd_q in product_qties: |
| 89 | + product = products.filtered(lambda s: s.id == prd_q["product_id"][0]) |
| 90 | + vals["code"].append(product.default_code) |
| 91 | + vals["designation"].append(product.name) |
| 92 | + # TODO mettre une colonne qté par entrepot (x colonnes) |
| 93 | + vals["qté"].append(round(prd_q["quantity"])) |
| 94 | + # TODO quand la valeur est < cost_vs_purchase_threshold % de ce seuil |
| 95 | + # mettre une colonne 'check' à la valeur 1 |
| 96 | + vals["valeur"].append( |
| 97 | + round( |
| 98 | + max( |
| 99 | + product.standard_price, |
| 100 | + prices[product] and prices[product].price or 0 * coef / 100, |
| 101 | + ) |
| 102 | + * prd_q["quantity"] |
| 103 | + ) |
| 104 | + ) |
| 105 | + # TODO mettre l'url en first column |
| 106 | + vals["lien"].append( |
| 107 | + f"{base_url}/web#id={prd_q['product_id'][0]}&cids={self.id}&action=" |
| 108 | + f"{self.env.ref('product.product_normal_action_sell').id}&model=" |
| 109 | + "product.product&view_type=form" |
| 110 | + ) |
| 111 | + df = pl.from_dict(vals) |
| 112 | + mfile = io.BytesIO() |
| 113 | + df.write_excel(workbook=mfile) |
| 114 | + attach = self.env["ir.attachment"].create( |
| 115 | + { |
| 116 | + "name": "Valorisation_stock_jourdain", |
| 117 | + "type": "binary", |
| 118 | + "res_model": "account.move", |
| 119 | + "datas": base64.b64encode(mfile.getvalue()), |
| 120 | + } |
| 121 | + ) |
| 122 | + return sum(vals["valeur"]), attach |
0 commit comments