Server Action: Loop through each record of model - for-loop

I have a server action to update product costs when selected in tree view:
bom_obj = env["mrp.bom"]
for product in object.browse(context.get('active_ids')):
price = 0
bom = bom_obj._bom_find(product=product)
if bom:
price = product._calc_price(bom)
product.write({'standard_price':price})
But This unfortunately only selects records visible in tree view, not ALL the records in product.product
I tried:
bom_obj = env["mrp.bom"]
product_obj = env["product.product"]
product_ids = product_obj.search(cr, uid, [])
for product in product_ids:
price = 0
bom = bom_obj._bom_find(product=product)
if bom:
price = product._calc_price(bom)
product.write({'standard_price':price})
Could you please tell me how to loop through each record of product.product.
OR
instead, tell me how I would go about update price of record selected in m2o field.
I know how to trigger the code in server action, I just need to know how to get record from m2o field. I will use this when product_id is changed in Sale Order Line, to update the price as it is selected.
Thanks

Related

Laravel eloquent relationship one to many

I am working on three tables: product, delivery and stock. I want that when a product is delivered, its quantity in the stock table increases. So in the controller of the delivery table I wrote this code:
$produit = Produit::find($delivery['produit_id']);
$quantite = $produit->stock->quantite;
$quantite += $delivery['quantite'];
$quantite->save();
but when I make a delivery, the quantity in the stock table does not change.
Simply types in PHP is not by reference, so you have to assign the new value to the Stock object. This is assuming quantite is an integer on the Stock model, which is not totally clear in the code you provided, since you call ->save() on quantite. Remember to save the Stock model instead of quantite.
$stock = $produit->stock;
$stock->quantite = $stock->quantite + $delivery['quantite']
$stock->save();

Qty available by location (again)

I know this was discussed before but it's not really working for me. How can i get qty available for location or warehouse my product is now.
(Most of the answers are in old API and this one not really working for me)
class ProductProduct(models.Model):
_inherit = 'product.template'
available_qty = fields.Integer(
string='Qty By Loc',
compute='product_qty_location_check',
)
def product_qty_location_check(self):
if self:
self.available_qty = self.with_context({'location' : self.source_location.id}).qty_‌​available
AttributeError: 'product.template' object has no attribute 'source_location'
To get the quantity by location you need to search the location with the product_id in stock.quant
Use the below sample in your compute function:
quant_sr = self.env["stock.quant"].search([('location_id','=',self.source_location.id),('product_id','=',self.product_id.id)])
qty = 0.0
for quant in quant_sr:
qty += quant.qty
print qty
First you have to find out your location and/or warehouse. There are enough possibilities to do so:
use a wizard which has a field
search by reference e.g. self.env.ref('my_module.my_location')
use other sources
Now you can use them on product.product's _product_available(). Just call that method on one or more products (recordssets) and evaluate the result of this method. To filter for locations and/or warehouses use the context. For example:
# get a recordset of all products
products = self.env['product.product'].search([])
# get a special location (my_location)
location = self.env.ref('my_module.my_location')
quantities = products.with_context(location=location.id)._product_available()
# print the quantities
for product_id, quantity_dict in quantities.iteritems():
print product_id
print quantity_dict
The same is possible with with_context(warehouse=warehouse.id) or even with list of IDs or names: with_context(location=[1,2,3]), with_context(location="My Location")

How i can show many2one field all records on a form view instead drop down list in odoo 10?

How I can show many2one field all records on a form view instead drop down list in odoo 10.
For example, I have a Product category and Products.
When I select Product category from drop down list then all products belongs to that category shows on form view instead of a drop down list.
Here I assume you have category id like,
category_id = fields.Many2one('product.category', "Product Category")
Add Many2many of product.product field to your model for product records.
product_ids = fields.Many2many('product.product', "Products")
Now you need to define onchange on category_id field like.
#api.onchange('category_id')
def onchange_product_category(self):
if self.category_id:
self.product_ids = [(5,)] # unlink all existing records of product
product_recs = self.env['product.product'].search([('categ_id', '=', self.category_id.id)])
self.product_ids = [(4, x) for x in product_recs] # link the product records of selected category
If you do not want to display your product records in list view you can use widget many2many_tags in the form view like,
<field name="product_ids" widget="many2many_tags"/>
It will work for you.

What is Difference between $_product->setData() & $_product->save()?

I have found some code to update product details
$_product->setData('manage_stock', 1);
$_product->setData('qty', $newQty);
$_product->save();
What is Difference between $_product->setData() & $_product->save() here ?
$_product->setData('qty', 10); is assign the value to the column(qty,etc).
$_product->save(); is save the that value to the DB.
I will show you an example. Suppose we have a product A with quantity 1 by default. Now we need to programmatically change the quatity of the product to 5. So for that we have wrote the following code.
Without save()
$_product = Mage::Registry('current_product');
$qty_before = $_product->getQty();
$_product->setData('qty', 5);
$qty_after = $_product->getQty();
echo "Quantity Available before setting the quantity property =".$qty_before."<br>";
echo "Quantity Available after setting the quantity property =".$qty_after."<br>";
Now it will show the following result
Quantity Available before setting the quantity property = 1.
Quantity Available after setting the quantity property = 5.
Now we have commented out the third line of code and then refreshed our page. Now our result will be look like this.
Quantity Available before setting the quantity property = 1.
Quantity Available after setting the quantity property = 5.
With save()
$_product = Mage::Registry('current_product');
$qty_before = $_product->getQty();
$_product->setData('qty', 5);
$qty_after = $_product->getQty();
$_product->save();
echo "Quantity Available before setting the quantity property =".$qty_before."<br>";
echo "Quantity Available after setting the quantity property =".$qty_after."<br>";
Now it will show the following result
Quantity Available before setting the quantity property = 1.
Quantity Available after setting the quantity property = 5.
Now, again we have commented out the third line of code and then refreshed our page. Now our result will be look like this.
Quantity Available before setting the quantity property = 5.
Quantity Available after setting the quantity property = 5.
So what is the difference? If we are setting a property means, it will update that field value for that time. But actually the value is not saving in database. In order to save the data to the database, we have to use save() method. Otherwise your changes that you made by setData() will be ignored by Magento.
If you load a product and then just use setData for any attribute, the product will show that value temporarily on that page only.
but if you use. $_product->save(); after the setData that value will be permanently saved in database and can be displayed on other pages as well.

Magento: Import product prices for an additional website?

I have a Magento installation, with two websites on it:
Retail (default)
Trade
Currently all the prices have been imported as default and so the prices are set the same on both websites. I now need to import the lower prices just for the trade website.
I know this can be done manually per product, but how do I go about importing these prices (with their SKU's so that they only apply to the trade store?
Any help much appreciated!
I suggest doing a Product Export first so you can see all of the columns that are used. Locate a SKU from your Trade store and see what the values are for that column.
You should see a column called _product_websites. In my installation, this column has "base" in it. On yours it will probably say "base" and "trade" (whatever you specified for your website code). You can sort by this column in Excel or other spreadsheet software and remove all of the rows that just have "base" in it so you're left with "trade". Now you can update your prices, save the sheet and re-import your file.
hth
You can simply follow the following Magento blog post:
http://www.blog.magepsycho.com/updating-product-prices-in-magento-in-easier-faster-way/
Just you need to add store_id filter in following method as:
function _updatePrices($data){
    $connection     = _getConnection('core_write');
    $sku            = $data[0];
    $newPrice       = $data[1];
$storeId = $data[2];
    $productId      = _getIdFromSku($sku);
    $attributeId    = _getAttributeId();
 
    $sql = "UPDATE " . _getTableName('catalog_product_entity_decimal') . " cped
                SET  cped.value = ?
            WHERE  cped.attribute_id = ?
            AND cped.entity_id = ?
AND store_id = ?";
    $connection->query($sql, array($newPrice, $attributeId, $productId, $storeId));
}
Of course you need to use third column of prices.csv for store_id.
Let me know if that helps.

Resources