| Author |
Topic |
|
Baresi
Starting Member
4 Posts |
Posted - 2004-07-14 : 22:15:20
|
| Hi,Was wondering if anyone could give me a hand on this.I'm developing a quotation system now, and have a problem in the quotation cost calculation.Basically, this is how the tables are like:Product------------IDNameFeature1Feature2Feature3Feature4 Feature4MaxUnitsFeature5 Feature5MaxUnitsFeature6Quotation-------------QuoIDCompanyIDProductIDFeature1Feature2Feature3Feature4 Feature4UnitsFeature5 Feature5UnitsFeature6In Product, Feature1-6 are integer values with the cost of that feature, whilst Feature4Units and 5 are the maximum units of a feature that can be provided on the product.In quotation, Feature1-6 are checkboxes on the interface and hence bit values in the database.What I'm trying to do is create a dynamically calculated field, to be stored alongside in 'Quotation', this field being the total quoted price.So the quoted price will have to retrieve the product record from Product based on ID, then check for each Feature, if it has been selected (check box checked/bit value set?) and where appropriate, the number of units selected by the customer. Simply its the sum of all those individual prices.Now the problem is I'm totally stumped as the prices can change which affects the quoted price, meaning I can't just calculate at my interface and feed the result into the table.Any way to do this in the 'insert quotation' stored procedure and store the field as an equation on database fields?BTW, I'm developing the frontend in asp.net if that matters. |
|
|
timmy
Master Smack Fu Yak Hacker
1242 Posts |
Posted - 2004-07-14 : 23:29:18
|
| SELECT P.ID, P.Name, Q.CompanyID, Convert(int, Q.Feature1) * P.Feature1 as Feature1Price, Convert(int, Q.Feature2) * P.Feature2 as Feature2Price, Convert(int, Q.Feature3) * P.Feature3 as Feature3Price, CASE WHEN Q.Feature4Units > P.Feature4MaxUnits THEN Q.Feature4Units * P.Feature4 ELSE P.Feature4MaxUnits * P.Feature4 END As Feature4 Price, CASE WHEN Q.Feature5Units > P.Feature5MaxUnits THEN Q.Feature5Units * P.Feature5 ELSE P.Feature5MaxUnits * P.Feature5 As Feature5 Price, Convert(int, Q.Feature6) * P.Feature6 as Feature6PriceFROM Product P INNER JOIN Quotation Q on P.ProductID = Q.ProductIDThis will give you an individual price for each feature. To get a total, simply add them all up. btw - Why do you need a Quotation.Feature4 when you've got the Feature4Units? I would have thought that you could just use the latter field, and have it zero if they don't choose anything. |
 |
|
|
lazerath
Constraint Violating Yak Guru
343 Posts |
Posted - 2004-07-15 : 12:05:26
|
| I don't know any specifics about your solution, so I may be off base, but I don't like your approach. Any time you see tables that have fields like Thing1,Thing2,Thing2Blah,Thing3,Thing4, etc... it should strike a cord somewhere within you that something is dreadfully wrong.Consider the below:[CODE]CREATE TABLE #qu_Product ( ProductPK int NOT NULL , Name varchar (50) NOT NULL ) CREATE TABLE #qu_Feature ( FeaturePK int NOT NULL , ProductFK int NOT NULL , Name varchar (50) NOT NULL ) CREATE TABLE #qu_Feature_MaxUnits ( FeatureFK tinyint NOT NULL , MaxUnits int NOT NULL ) CREATE TABLE #qu_Feature_Price ( FeatureFK tinyint NOT NULL , Price smallmoney NULL ) CREATE TABLE #qu_Quotation ( QuotationPK int NOT NULL , CompanyFK int NOT NULL , ProductFK int NOT NULL ) CREATE TABLE #qu_Quotation_Feature ( QuotationFK int NOT NULL , FeatureFK int NOT NULL , Units int NULL )insert #qu_Product (ProductPK,Name)SELECT 1, 'Car' UNION ALLSELECT 2,'Boat' UNION ALLSELECT 3,'House'insert #qu_Feature(FeaturePK,ProductFK,Name)SELECT 1,1,'Engine' UNION ALLSELECT 2,1,'Body' UNION ALLSELECT 3,1,'Frame' UNION ALLSELECT 4,1,'Wheels' UNION ALLSELECT 5,1,'Headlights' UNION ALLSELECT 6,1,'Interior' UNION ALLSELECT 7,2,'Boat Motor' UNION ALLSELECT 8,2,'Deluxe Hull' UNION ALLSELECT 9,2,'Trailer' UNION ALLSELECT 10,2,'Trolling Motor' UNION ALLSELECT 11,3,'Exterior Walls' UNION ALLSELECT 12,3,'Roof' UNION ALLSELECT 13,3,'Furnace' UNION ALLSELECT 14,3,'A/C' UNION ALLSELECT 15,3,'Rooms' UNION ALLSELECT 16,3,'Basement'insert #qu_Feature_MaxUnits (FeatureFK,MaxUnits)SELECT 4,4 UNION ALLSELECT 5,2 UNION ALLSELECT 11,5 UNION ALLSELECT 15,8insert #qu_Feature_Price (FeatureFK,Price)SELECT 1,33.0800 UNION ALLSELECT 2,21.4700 UNION ALLSELECT 3,12.9500 UNION ALLSELECT 4,7.0500 UNION ALLSELECT 5,3.2900 UNION ALLSELECT 6,74.1800 UNION ALLSELECT 7,100.2400 UNION ALLSELECT 8,54.0100 UNION ALLSELECT 9,2.0100 UNION ALLSELECT 10,20.3000 UNION ALLSELECT 11,40.3300 UNION ALLSELECT 12,3.5800 UNION ALLSELECT 13,7.5400 UNION ALLSELECT 14,13.6800 UNION ALLSELECT 15,22.4800 UNION ALLSELECT 16,34.4300insert #qu_Quotation (QuotationPK,CompanyFK,ProductFK)SELECT 1,1,1 UNION ALLSELECT 2,1,2 UNION ALLSELECT 3,1,3 UNION ALLSELECT 4,1,2insert #qu_Quotation_Feature (QuotationFK,FeatureFK,Units)SELECT 1,1,NULL UNION ALLSELECT 1,2,NULL UNION ALLSELECT 1,4,NULL UNION ALLSELECT 2,7,NULL UNION ALLSELECT 2,8,NULL UNION ALLSELECT 2,9,NULL UNION ALLSELECT 3,11,3 UNION ALLSELECT 3,14,NULL UNION ALLSELECT 4,7,2 UNION ALLSELECT 4,10,NULL;SELECT QuotationPK, CompanyFK, p.Name, COUNT(*) FeatureCount, SUM(CASE WHEN ISNULL(mu.MaxUnits,1) < qf.Units THEN ISNULL(mu.MaxUnits,1) ELSE ISNULL(qf.Units,1) END) TotalUnits, SUM(CASE WHEN ISNULL(mu.MaxUnits,1) < qf.Units THEN ISNULL(mu.MaxUnits,1) ELSE ISNULL(qf.Units,1) END * fp.Price) TotalCostFROM #qu_Quotation qJOIN #qu_Product pON q.ProductFK = p.ProductPKJOIN #qu_Quotation_Feature qfON q.QuotationPK = qf.QuotationFKJOIN #qu_Feature_Price fpON fp.FeatureFK = qf.FeatureFKLEFT JOIN #qu_Feature_MaxUnits muON mu.FeatureFK = qf.FeatureFKGROUP BY QuotationPK, CompanyFK, p.Name;DROP TABLE #qu_Product;DROP TABLE #qu_Feature;DROP TABLE #qu_Feature_MaxUnits;DROP TABLE #qu_Feature_Price;DROP TABLE #qu_Quotation;DROP TABLE #qu_Quotation_Feature;[/CODE] |
 |
|
|
|
|
|