How to get value from many2one-One2many in (new) product for a calculation in odoo 8? - relationship

I've made a custom field in product.template to select a product quality, based on a Many2one relationship with qualities.
qualities_id = field.Many2one(comodel_name="qualities", string="quality")
In the class: qualities there is a One2Many relationship with the class: quality.supplier.
reseller_ids = fields.One2many('supplierinfo', 'product_qual_id', 'Supplier')
Here we can select a quality type e.g. ( id=16 type=B321 ). This quality type (16) can have more then one supplier. So in supplierinfo we have the field product_qual_id (id = 16) that holds multi suppliers (id = 24, 25, 26)
product_qual_id = fields.Many2one('qualities', 'Quality Template')
Supplierinfo holds suppliers and each supplier holds more then one quantity and price. There is a relationship between supplierinfo and supplier prices using the field suppinfo_id.
pricelist_ids = fields.One2many('supplierprice', 'suppinfo_id', 'Supplier Price')
Because of this relationship, supplierprice: suppinfo, holds more then one quantity and corresponding price based on supplierinfo_id. Field that holds these records:
suppinfo_id = fields.Many2one('supplierinfo', 'Supplier information')
Qualities = ( id=16 type=B321 ). This quality type (16) can have more then one supplier. So in supplierinfo we have the field product_qual_id (id = 16) that holds multi suppliers (id = 24, 25, 26) where Record id: 24 in relation to supplierprice has multi qty and prices:
supplierprice id=30 supplierinfo id=24 qty=200, price=285
supplierprice id=31 supplierinfo id=24 qty=300, price=265
supplierprice id=32 supplierinfo id=24 qty=500, price=248
With additional automation, I need the price from supplierprice, based on (less or more) quantity in the main product. (product.template, price). Can anyone push me in the right direction. How can I get the price from supplierprice into a (new) field in my product.template?
I've added an image of the relationship model.
relationship

Related

SQL - Remove Duplicates

I've inherited data from another provider and have 2 tables to clean up
Rooms
Items
Items are within a Room and are linked via Rooms.RoomID / Items.RoomID
Each time an Item has been inspected it has created a Duplicate of the Room & Item, so I have 6 records for each Room and each Item (Each with a unique RoomID, so 1 to 1)
My aim is to remove the Duplicate Rooms and keep only the latest set of records, then update the Items table with the RoomID (So I have 1 to Many i.e. 1 Room with 6 Item records)
I can GROUP the Rooms and obtain the MAX RoomID but don't know how to do the UPDATE
A kind of suedo would be this:
UPDATE a
SET a.RoomID = MAX(b.RoomID)
FROM [dbo].[tmp_Items] a
inner join [dbo].[tmp_Rooms] b on b.PropertyID = a.PropertyID
and b.FloorLevel = a.FloorLevel
and b.Reference = a.Reference
The combination of PropertyID, FloorLevel, and Reference provides a unique link between Items and Rooms as these columns are in both tables and Reference is unique to each floor of a property (Each Room of each Floor is numbered starting from 1)
Any help or guidance appreciated :)
Use a subquery to aggregate before joining:
UPDATE i
SET i.RoomID = r.max_roomid
FROM [dbo].[tmp_Items] i JOIN
(SELECT PropertyID, FloorLevel, Reference, MAX(r.RoomID) as max_roomid
FROM [dbo].[tmp_Rooms] r
GROUP BY PropertyID, FloorLevel, Reference
) r
ON r.PropertyID = i.PropertyID AND
r.FloorLevel = i.FloorLevel AND
r.Reference = i.Reference;

Use many to many relationship or multiple columns?

I have a product, which can belong to one (or more) of 5 possible categories.
The number/name/structure of categories will not change
Should I be using a many to many relationship and insert 5 records in the category table, add a join table and add foreign keys etc? Or just add 5 fields to the product table? I feel the latter seems more efficient, but goes against the principles of normalization.
i.e.
Product:
- id
- name
- is_cat_a : bool
- is_cat_b : bool
- is_cat_c : bool
- is_cat_d : bool
- is_cat_e : bool
OR
Product
- id
- name
Category
- id
- name
ProductCategories
- product_id
- category_id
As you suggested:
A product, which can belong to one (or more) of 5 possible categories only.
Then, in my opinion, create two tables:
**1. Category**
Id int,
Name varchar,
IsActive bool and other regular columns
**2. Product**
- id int,
- name varchar,
- cat_a : int,
- cat_b : int,
- cat_c : int,
- cat_d : int,
- cat_e : int
Keep all category column's default as zero.

Check checkbox with column from table

Can anyone show me, what i'm doing wrong?
I using c_id from company to give checkbox a value for later use.
I using name from company to write the name of the company next to the checkbox.
I using c_id from artistscompany to print "checked" in the checkboxes, where c_id from artistscompany are equal to c_id from artists.
$slsql = 'SELECT * FROM artistscompany WHERE a_id='.$ad_id;
$blsql = 'SELECT c_id, name FROM company WHERE c_id > 1';
if (($bl_result = mysqli_query($dbh, $blsql))AND($sl_result = mysqli_query($dbh, $slsql))) {
while($bl_row = mysqli_fetch_assoc($bl_result)){
$sl_raekke = mysqli_fetch_assoc($sl_result);
foreach((array)$sl_raekke as $sl_row){}
echo '<input type="checkbox" name="belongs[]" value="'.$bl_row['c_id'].'"'; if($sl_row == $bl_row['c_id']){echo'checked';}ELSE{echo'';} echo'> '.$bl_row['name'].'<br />';
}
mysqli_free_result($bl_result);
mysqli_free_result($sl_result);
}
$query = 'SELECT * FROM artistscompany a, company c WHERE c.c_id > 1 AND
c.c_id=a.c_id AND a_id='.$ad_id;
Now use this query to fetch and display from db.
a and c are used to refer to the fields. You want the c_id > 1 from company table, a_id=$variable and the same company ID in both tables. Correct me if I am wrong.

How to connect entities in core data

I have three entities - Documents, Contragents and Products.
Document has:
id - int64
contragentId - int64
paymentMethod - String
documentDate - Date
documentKind - String
Contragents has
id - int64
name - String
address - String
Products has
id - int64
sku - String
productQuantity - int64
description - String
unitValue - Decimal
totalValue - Decimal
isTemp - Bool
i have implemented Products subclassing the model and totalValue = quantity * unitValue
i want in another table to keep all Documents, but i don't know how to display Contragent.name
i should use that Document.contragentId = Contragent.id
- (IBAction) createDocument:(id)sender
{
//create new document
NSManagedObject *document = [NSEntityDescription
insertNewObjectForEntityForName:#"Documents"
inManagedObjectContext:self.managedObjectContext];
NSNumber *num = [NSNumber numberWithInteger: [_documentNumber integerValue]];
[document setValue: num forKey:#"id"];
[document setValue: [_documentKind stringValue] forKey:#"documentKind"];
[document setValue: [_documentDate dateValue] forKey:#"documentDate"];
[document setValue: [_paymentMethod stringValue] forKey:#"paymentMethod"];
[_documentNumber setIntegerValue:[_documentNumber integerValue]+1];
//get ID
//NSString *contragentId = [[_contragents selectedObjects] valueForKey:#"eik"];
//NSLog(#"The customer ID is: %#", contragentId);
document.contragent = [_contragents selectedObjects];
With Core Data one does not use "foreign keys" to identify rows of another table, but relationships between entities.
So instead of contragentId, you should define a relationship "contragent"
from the "Document" entity to the "Contragent" entity. (I have used the singular form "Contragent" because that seems to be more natural. But I have no idea what a contragent
is.)
You should also define the "inverse relationship" from "Contragent" to "Document".
If a "Contragent" object can be connected to several "Document" objects, the inverse
relationship is "to-many", otherwise "to-one".
Then you make connections just be setting the relationship:
Document *theDocument = ...
Contragent *theContragent = ...
theDocument.contragent = theContragent; // --> connected!
And getting the contragent name for a document:
Document *theDocument = ...
NSString *contrName = theDocument.contragent.name;

Help with Grocery List Database design

I want to create a Grocery List Table that will accept both the Category and the Specific Products.
For Example: I add to my grocery list:
Fruit (Category)
Fuji Apple (Product)
Shampoo (Category)
Dove Energize Shampoo (Product)
I have Product Table w/ Manufacturer(reference table), Category Table w/ SubCategoryId.
I want the user to enter either the Category or Product into the Grocery List, but have a way knowing that item entered is either a Category or Product. Please advise.
Any help is much appreciated.
Bogdan solution is a very good solution and if i have your problem i would implement his solution, but if you insist on one column in one table for your grocery list, i guess you have to use substring
like e.g
CREATE TABLE grocery_list (
[groceryItem] varchar(100)
);
Insert into grocery_list ([groceryItem]) values ('(c)Fruit')
Insert into grocery_list ([groceryItem]) values ('(p)Fuji Apple')
Insert into grocery_list ([groceryItem]) values ('(c)Shampoo')
Insert into grocery_list ([groceryItem]) values ('(c)Dove Energize Shampoo')
and to access your table substring the first 2 chars to check the type of your item
Select [item] = case when substring(groceryItem,2,1) = 'c' then
right(groceryItem,len(groceryItem) - 3) + ' (Category)'
when substring(groceryItem,2,1) = 'p' then
right(groceryItem,len(groceryItem) - 3) + ' (Product)'
end
from grocery_list
This will give you the below result
**item**
Fruit (Category)
Fuji Apple (Product)
Shampoo (Category)
Dove Energize Shampoo (Category)
I would create two separate columns, one for categories and one for products:
CREATE TABLE grocery_list (
'category_id' INTEGER NULL FOREIGN KEY categories 'id',
'product_id' INTEGER NULL FOREIGN KEY products 'id',
...
);
Entries will only put a value in one of the two columns, leaving the other as NULL. Then you can query your grocery list like this:
SELECT * FROM grocery_list, categories, products
WHERE categories.id=grocery_list.category_id
AND products.id=grocery_list.product_id;

Resources