Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
2 ตอบกลับ
206 มุมมอง

Hello,


There's a way to add a custom field with Studio to display the available quantity of packaging of the products?


This way I can see how manny stock I have of a product and how manny packages I can count on, based on the formula of the packaging, like:


product: water bottle, unit of measure: units, stock on hand: 60

packaging: water bottle six pack, stock on hand: 6


in the Stock report will look like this:

product | on hand | product package | packages on hand

water bottle | 60 | water bottle six pack | 6


thanks!

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hello Luis
You want the stock report to display the information in a specific format:

product | on hand | product package | packages on hand

Where:

  • product: The name of the product.
  • on hand: The quantity of the product on hand.
  • product package: The name of the packaging (e.g., "water bottle six pack").
  • packages on hand: The calculated number of packages available.

This clarifies that you want to display the name of the packaging as well as the calculated number of packages. This means you'll likely need to use the separate "Packaging" model (Method 2 from the previous response) to store the packaging name.

Here's how to modify the solution to achieve this specific report format:

Follow Steps

  1. Implement the Separate "Packaging" Model (if you haven't already):
    • Create a new model called product.packaging.
      • Name: Packaging
      • Menu: (Optional) Create a menu item to manage packaging records.
      • Fields:
        • Product (Many2one): Link to the product.product or product.template model.
        • Name (Char): The name of the packaging (e.g., "Six-Pack", "Case of 24").
        • Units per Package (Integer): The number of units in the package.
    • Add a One2many field to Product: Add a One2many field to the product.product or product.template model to link to the packaging records.
      • Label: "Packaging"
      • Technical Name: x_packaging_ids
      • Related Model: product.packaging
      • Inverse Field: The field on the product.packaging model that links back to the product.
  2. Modify the Stock Report (stock.quant) Model:
    • Add a Many2one Field for Packaging: You need to explicitly select which packaging you want to use for the calculation in the report. Add a Many2one field to the stock.quant model to link to the product.packaging model.
      • Enter Odoo Studio: Go to Inventory -> Reporting -> Inventory Valuation (or the specific stock report you want to modify).
      • Activate Developer Mode: Make sure you're in developer mode.
      • Find the "Quant" View: Use the "View Form" option in the debugger to find the XML ID of the view that displays the stock report data. It will likely be a tree view on the stock.quant model.
      • Add a "Many2one" Field: Drag a "Many2one" field from the Studio toolbox onto the form.
        • Label: "Product Package"
        • Technical Name: x_product_package_id (Studio will automatically generate this)
        • Related Model: product.packaging
    • Add the Computed Field for Packages on Hand:
      • Add a "Float" field to the stock.quant model.
        • Label: "Packages on Hand"
        • Technical Name: x_packages_on_hand (Studio will automatically generate this)
        • Digits: Set the number of decimal places.
        • Compute Field:
          from odoo import models, fields, api
          
          class StockQuant(models.Model):
              _inherit = 'stock.quant'
          
              x_product_package_id = fields.Many2one('product.packaging', string='Product Package')
              x_packages_on_hand = fields.Float(string='Packages on Hand', compute='_compute_packages_on_hand', store=True)
          
              @api.depends('quantity', 'x_product_package_id.units_per_package')
              def _compute_packages_on_hand(self):
                  for record in self:
                      if record.x_product_package_id and record.x_product_package_id.units_per_package > 0:
                          record.x_packages_on_hand = record.quantity / record.x_product_package_id.units_per_package
                      else:
                          record.x_packages_on_hand = 0.0
          
        • Explanation:
          • x_product_package_id: The Many2one field linking to the product.packaging model.
          • x_packages_on_hand: The computed field for the number of packages.
          • @api.depends('quantity', 'x_product_package_id.units_per_package'): The compute method now depends on the units_per_package field of the selected packaging.
          • The compute method divides the quantity by the units_per_package of the selected packaging.
  3. Modify the Stock Report View:
    • Enter Odoo Studio: Open the stock report view again.
    • Add the Fields to the View: Drag the following fields from the Studio toolbox onto the view, in the order you want them to appear:
      • product_id (or product_tmpl_id if you're using product templates)
      • quantity (On Hand)
      • x_product_package_id (Product Package)
      • x_packages_on_hand (Packages on Hand)

How to Use the Report

  1. Open the Stock Report: Go to Inventory -> Reporting -> Inventory Valuation (or your custom stock report).
  2. Select a Packaging: For each line in the report, you'll need to manually select the appropriate "Product Package" from the dropdown. This tells Odoo which packaging configuration to use for the calculation.
  3. View the Results: The "Packages on Hand" field will then display the calculated number of packages based on the selected packaging and the quantity on hand.

Important Considerations

  • Manual Packaging Selection: This solution requires you to manually select the packaging for each line in the report. This is because Odoo doesn't automatically know which packaging you want to use for the calculation. If you want to automate this, you'll need more complex logic (e.g., a default packaging setting on the product or a way to infer the packaging based on the quantity).
  • Performance: Adding computed fields and manual selection fields can impact performance, especially on large datasets.
  • User Experience: The manual packaging selection might be cumbersome for users. Consider ways to improve the user experience, such as adding a default packaging setting to the product or using a more intuitive interface for selecting the packaging.
  • Filtering and Grouping: You can use Odoo's filtering and grouping features to further analyze the stock report data. For example, you could group the report by product or by packaging.

Alternative: Custom Report (More Flexible, but More Complex)

For more complex reporting requirements, you might consider creating a custom report using Odoo's reporting engine. This would give you more control over the layout and data displayed in the report. However, it would also require more technical expertise.

By following these steps, you should be able to create a stock report that displays the product, quantity on hand, product package, and packages on hand in the format you specified. Remember to adapt the code and configuration to your specific needs and test thoroughly. The key is to use the separate "Packaging" model and the manual packaging selection to provide the necessary information for the calculation.

🚀 Did This Solve Your Problem?

If this answer helped you save time, money, or frustration, consider:

✅ Upvoting (👍) to help others find it faster

✅ Marking as "Best Answer" if it resolved your issue

Your feedback keeps the Odoo community strong! 💪

(Need further customization? Drop a comment—I’m happy to refine the solution!)

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Add a custom Float field (e.g., x_packages_on_hand) to product.product.

Add a field or use existing field for packaging quantity (e.g., 6 for six-pack).

Write a compute method to calculate packages available.

Add the field to the product form or stock report view via XML.

Example compute method:

from odoo import models, fields, api

import math


class ProductProduct(models.Model):

    _inherit = 'product.product'


    packaging_qty = fields.Float(string="Packaging Qty")  

    x_packages_on_hand = fields.Float(string="Packages on Hand", compute="_compute_packages_on_hand")


    @api.depends('qty_available', 'packaging_qty')

    def _compute_packages_on_hand(self):

        for product in self:

            if product.packaging_qty:

                product.x_packages_on_hand = math.floor(product.qty_available / product.packaging_qty)

            else:

                product.x_packages_on_hand = 0


อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
1
พ.ย. 24
624
0
เม.ย. 25
221
2
พ.ค. 25
515
2
เม.ย. 25
2923
1
ต.ค. 24
1028