@@ -22,7 +22,7 @@ class ResCompany(models.Model):
2222
2323 def _set_account_stock_valuation (self , company_string_id ):
2424 self = self .env .ref (company_string_id )
25- value , attach = self ._get_stock_valuation_another ()
25+ value , attach = self ._get_stock_valuation ()
2626 for mfield in (
2727 "account_stock_id" ,
2828 "account_purchase_stock_id" ,
@@ -65,42 +65,34 @@ def _set_account_stock_valuation(self, company_string_id):
6565 )
6666 attach .res_id = move .id
6767
68- def _get_stock_valuation_another (self ):
68+ def _get_stock_valuation (self ):
6969 self .ensure_one ()
70- coef = self .cost_vs_purchase_threshold
7170 base_url = self .env ["ir.config_parameter" ].sudo ().get_param ("web.base.url" )
7271 if tools .config .get ("running_env" ) == "dev" :
7372 base_url = "http://anothercorp.localhost/"
7473 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 (
74+
75+ stock_quant_ids = self .env ["stock.quant" ].search (
7876 [("location_id" , "child_of" , location_ids )],
79- ["product_id" , "warehouse_id" , "quantity" ],
80- ["product_id" , "warehouse_id" ],
81- lazy = False ,
8277 )
83-
84- product_ids = list ({x ["product_id" ][0 ] for x in product_qties })
85-
86- products = self .env ["product.product" ].browse (product_ids )
87- prices = {
88- x : x .variant_seller_ids and x .variant_seller_ids [0 ] or 0 for x in products
89- }
78+ products = self .env ["product.product" ].browse (stock_quant_ids .product_id .ids )
9079 vals = defaultdict (list )
9180
9281 product_dict = {}
93- for product in product_qties :
94- if not product [ " product_id" ][ 0 ] in product_dict :
95- product_dict [product [ " product_id" ][ 0 ] ] = [
96- product [ " warehouse_id" ][ 0 ] ,
97- product [ " quantity" ] ,
82+ for stock_quant in stock_quant_ids :
83+ if stock_quant . product_id . id not in product_dict :
84+ product_dict [stock_quant . product_id . id ] = [
85+ stock_quant . warehouse_id ,
86+ stock_quant . quantity ,
9887 ]
9988 else :
100- product_dict [product ["product_id" ][0 ]] += [
101- product ["warehouse_id" ][0 ],
102- product ["quantity" ],
103- ]
89+ if stock_quant .warehouse_id in product_dict [stock_quant .product_id .id ]:
90+ product_dict [stock_quant .product_id .id ][1 ] += stock_quant .quantity
91+ else :
92+ product_dict [stock_quant .product_id .id ] += [
93+ stock_quant .warehouse_id ,
94+ stock_quant .quantity ,
95+ ]
10496
10597 for product_id , warehouse_quantities in product_dict .items ():
10698 product = products .filtered (lambda s : s .id == product_id )
@@ -111,33 +103,24 @@ def _get_stock_valuation_another(self):
111103 )
112104 vals ["code" ].append (product .default_code )
113105 vals ["designation" ].append (product .name )
106+
114107 for i in range (0 , len (warehouse_quantities ), 2 ):
115108 warehouse_id = warehouse_quantities [i ]
116109 quantity = warehouse_quantities [i + 1 ]
117- warehouse_id = self .env ["stock.warehouse" ].browse (warehouse_id )
118- vals [f"qté_{ warehouse_id .name } " ].append (round (quantity ))
110+ vals [f"qty_{ warehouse_id .code } " ].append (round (quantity ))
119111 if len (warehouse_quantities ) / 2 < len (self .valued_warehouse_ids ):
120112 warehouse_without_qty = self .valued_warehouse_ids .filtered (
121113 lambda r : r .id
122114 not in [
123- warehouse_quantities [i ]
115+ warehouse_quantities [i ]. id
124116 for i in range (0 , len (warehouse_quantities ), 2 )
125117 ]
126118 )
127119 for warehouse in warehouse_without_qty :
128- vals [f"qté_{ warehouse .name } " ].append (0 )
120+ vals [f"qty_{ warehouse .code } " ].append (0 )
121+
122+ vals ["value" ].append (round (product .standard_price ))
129123
130- # TODO quand la valeur est < cost_vs_purchase_threshold % de ce seuil
131- # mettre une colonne 'check' à la valeur 1
132- vals ["valeur" ].append (
133- round (
134- max (
135- product .standard_price ,
136- prices [product ] and prices [product ].price or 0 * coef / 100 ,
137- )
138- * product ["quantity" ]
139- )
140- )
141124 df = pl .from_dict (vals )
142125 mfile = io .BytesIO ()
143126 df .write_excel (workbook = mfile )
0 commit comments