i stuck in my problem , please give me solution
I have Recursive method like this :
@api.model
def create(self, vals):
dynamic_prefix = self.get_dynamic_prefix()
sequence = self.env['ir.sequence'].next_by_code('sales.forecast') or '/'
roman_month = self.get_roman_month()
current_year = datetime.now().strftime('%Y')
new_code = f"{sequence}/{dynamic_prefix}/{roman_month}/{current_year}"
vals['name'] = new_code
forecast = super(Forecast, self).create(vals)
def process_bom(bom, sales_forecast_id, forecast_name, user_id, company_id, product_line, forecast_product_qty,
parent_product_qty=1, level=0):
for bom_line in bom:
print(f"{' ' * level}BOM: {bom_line.product_id.name} (ID: {bom_line.id})")
for each_line in bom_line.bom_line_ids:
product_id = each_line.product_id.id
uom_id = each_line.product_uom_id.id
product_qty = (forecast_product_qty / bom_line.product_qty) * each_line.product_qty * parent_product_qty
print(f"{' ' * (level + 1)}Product: {each_line.product_id.name} (ID: {product_id}), Quantity: {product_qty}")
child_boms = self.env['mrp.bom'].sudo().search([
('product_tmpl_id', '=', each_line.product_id.product_tmpl_id.id),
('company_id', '=', company_id)
])
if child_boms:
for child_bom in child_boms:
process_bom(child_bom, sales_forecast_id, forecast_name, user_id, company_id, product_line,
product_qty, level=level + 1)
else:
# If no child bom, add the product to product line
product = each_line.product_id
if product.product_tmpl_id.categ_id.id in [14, 12]:
product_line.add((sales_forecast_id, forecast_name, product_qty, product_id,
bom_line.id, user_id, company_id, uom_id))
product_line = set()
# Iterate over each forecast line
for line in forecast.product_forecat_line:
product_qty = line.product_qty
forecast_name = forecast.name
user_id = forecast.user_id.id
company_id = forecast.company_id.id
product_id = line.product_id.id
uom_id = line.uom_id.id
product_line.add(
(line.forecast_id.id, forecast_name, product_qty, product_id, None, user_id, company_id, uom_id))
product = self.env['product.product'].sudo().browse(product_id)
product_template = product.product_tmpl_id
boms = self.env['mrp.bom'].sudo().search([
('product_tmpl_id', '=', product_template.id),
('company_id', '=', company_id)
])
for bom in boms:
process_bom([bom], line.forecast_id.id, forecast_name, user_id, company_id, product_line, product_qty)
# Convert the set to a list and insert into the estimate_forecasted table
product_line_list = list(product_line)
for sales_forecast_id, name, product_qty, product_id, bom_id, user_id, company_id, uom_id in product_line_list:
sql_query = """
INSERT INTO estimate_forecasted (sales_forecast_id, name, product_qty, product_id, bom_id, user_id, company_id, uom_id)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
"""
self.env.cr.execute(sql_query,
(sales_forecast_id, name, product_qty, product_id, bom_id, user_id, company_id, uom_id))
self.env.cr.commit()
return forecast
when i print the result , i get result expected like this :
BOM: False (ID: 47016)
Product: OUTER SHEATHED 4 X 4 MM² (re) - NYRGbY (ID: 70044), Quantity: 1000.0
BOM: False (ID: 50687)
Product: ARMOURED 4 X 4 MM² (re) - NYRGbY (ID: 52150), Quantity: 1000.0
BOM: False (ID: 47013)
Product: INNER SHEATHED 4 X 4 MM² (re) - (NYFGbY/NYRGbY) (ID: 70994), Quantity: 1000.0
BOM: False (ID: 51098)
Product: CABLING 4 X 4 MM² (re) - (NYFGbY/NYRGbY) (ID: 70993), Quantity: 1000.0
BOM: False (ID: 51097)
Product: INSULATED PVC BLUE COPPER WIRE 4 MM² (re) (ID: 46236), Quantity: 1000.0
BOM: False (ID: 34470)
Product: COPPER WIRE 2,19 MM (ID: 45776), Quantity: 1000.0
BOM: False (ID: 34143)
Product: COPPER ROD (ID: 13736), Quantity: 33.69
Product: PVC YJ/A 70 BLUE (YJA-SR701BL) (ID: 55359), Quantity: 15.1
BOM: False (ID: 47528)
Product: RESIN FJ 70 (ID: 50428), Quantity: 7.783986638348764
Product: DOP (ID: 50432), Quantity: 3.5027939872569434
Product: CALCIUM CARBONATE KT 10 C (ID: 50435), Quantity: 2.95791492257253
Product: CLAY CAOLIN (ID: 50436), Quantity: 0.4670391983009258
Product: PIGMENT TITANIUM DIOXIDE RFC 5 (ID: 50498), Quantity: 0.04981751448543209
Product: PIGMENT SUDA FAST BLUE 2745 (ID: 50500), Quantity: 0.015567973276697527
Product: INSULATED PVC BROWN COPPER 4 MM² (re) (ID: 45778), Quantity: 1000.0
BOM: False (ID: 34145)
Product: COPPER WIRE 2,19 MM (ID: 45776), Quantity: 1000.0
BOM: False (ID: 34143)
Product: COPPER ROD (ID: 13736), Quantity: 33.69
Product: PVC YJ/A 70 BROWN (YJA-SR701BR) (ID: 55361), Quantity: 15.1
BOM: False (ID: 47530)
Product: RESIN FJ 70 (ID: 50428), Quantity: 7.770687525730754
Product: DOP (ID: 50432), Quantity: 3.496809386578839
Product: CALCIUM CARBONATE KT 10 C (ID: 50435), Quantity: 2.9528612597776864
Product: CLAY CAOLIN (ID: 50436), Quantity: 0.4662412515438452
Product: PIGMENT YELLOW HR (ID: 50502), Quantity: 0.0195821325648415
Product: PIGMENT SUDAFAST RED 570 (ID: 50446), Quantity: 0.006527377521613833
Product: PIGMENT TITANIUM DIOXIDE RFC 5 (ID: 50498), Quantity: 0.062165500205846026
Product: INSULATED PVC BLACK COPPER 4 MM² (re) (ID: 45779), Quantity: 1000.0
BOM: False (ID: 34146)
Product: COPPER WIRE 2,19 MM (ID: 45776), Quantity: 1000.0
BOM: False (ID: 34143)
Product: COPPER ROD (ID: 13736), Quantity: 33.69
Product: PVC YJ/A 70 BLACK (YJA-SR701BK) (ID: 55357), Quantity: 15.1
BOM: False (ID: 47526)
Product: RESIN FJ 70 (ID: 50428), Quantity: 7.790733670415849
Product: DOP (ID: 50432), Quantity: 3.505830151687132
Product: CALCIUM CARBONATE KT 10 C (ID: 50435), Quantity: 2.960478794758023
Product: CLAY CAOLIN (ID: 50436), Quantity: 0.46744402022495096
Product: PIGMENT CARBON BLACK RAVEN L (ID: 50503), Quantity: 0.05297698895882778
BOM: False (ID: 47986)
Product: PVC YJ/A 70 BLUE (YJA-SR701BL) (ID: 55359), Quantity: 15.048833964520629
BOM: False (ID: 47528)
Product: RESIN FJ 70 (ID: 50428), Quantity: 7.757610761758783
Product: DOP (ID: 50432), Quantity: 3.4909248427914523
Product: CALCIUM CARBONATE KT 10 C (ID: 50435), Quantity: 2.9478920894683376
Product: CLAY CAOLIN (ID: 50436), Quantity: 0.465456645705527
Product: PIGMENT TITANIUM DIOXIDE RFC 5 (ID: 50498), Quantity: 0.049648708875256214
Product: PIGMENT SUDA FAST BLUE 2745 (ID: 50500), Quantity: 0.015515221523517565
Product: PIGMENT CARBON BLACK RAVEN L (ID: 50503), Quantity: 0.05116603547937014
Product: INSULATED PVC GREY COPPER 4 MM² (re) (ID: 45780), Quantity: 1000.0
BOM: False (ID: 34147)
Product: COPPER WIRE 2,19 MM (ID: 45776), Quantity: 1000.0
BOM: False (ID: 34143)
Product: COPPER ROD (ID: 13736), Quantity: 33.69
Product: PVC YJ/A 70 GREY (YJA-SR701GY) (ID: 55362), Quantity: 15.1
BOM: False (ID: 47531)
Product: RESIN FJ 70 (ID: 50428), Quantity: 7.689017435228939
Product: DOP (ID: 50432), Quantity: 3.4600578458530227
Product: CALCIUM CARBONATE KT 10 C (ID: 50435), Quantity: 2.9218266253869967
Product: CLAY CAOLIN (ID: 50436), Quantity: 0.46134104611373633
Product: PIGMENT CARBON BLACK RAVEN L (ID: 50503), Quantity: 0.002768046276682418
Product: PIGMENT TITANIUM DIOXIDE RFC 5 (ID: 50498), Quantity: 0.24604855792732605
Product: PVC FILLER LV BLACK (FI-ILV7001BK) (ID: 55353), Quantity: 107.54
BOM: False (ID: 47522)
Product: RESIN SG710 (ID: 67625), Quantity: 31.920451172454737
Product: DOP (ID: 50432), Quantity: 17.23704363312556
Product: CP 52% (ID: 54816), Quantity: 3.1920451172454736
Product: CALCIUM CARBONATE KT 15 (ID: 50496), Quantity: 53.626357969723955
Product: STABILIZER PVC NON TOXIC (NON-Pb) (ID: 59559), Quantity: 1.1172157910359157
Product: ZB - 20 (ID: 50443), Quantity: 0.09576135351736421
Product: PE WAX POWDER (ID: 54817), Quantity: 0.15960225586227367
Product: PIGMENT CARBON BLACK RAVEN L (ID: 50503), Quantity: 0.19152270703472843
Product: GALVANIZED STEEL WIRE 1,25 MM (FOR ARMOUR) (ID: 48719), Quantity: 245.84
Product: GALVANIZED STEEL TAPE 0,20 X 10 MM (ID: 45508), Quantity: 36.77
Product: PVC YM 1 BLACK (SH601BK) (ID: 55354), Quantity: 143.28
BOM: False (ID: 47523)
Product: RESIN FJ 65 (ID: 50427), Quantity: 65.78512396694215
Product: TOTM (ID: 50429), Quantity: 8.770472727272727
Product: DINP (ID: 50430), Quantity: 22.806386776859505
Product: CALCIUM CARBONATE KT 10 C (ID: 50435), Quantity: 39.47107438016529
Product: PIGMENT CARBON BLACK RAVEN L (ID: 50503), Quantity: 0.6578512396694215
the issue -> product COPPER WIRED print resulted 4, but when insert to table estimate_forecasted
COPPER ROD just insert 1 product,In the provided code, only 1 product is being processed instead of the expected 4 products.
how to solve this ?
Thanks