i want to add increment of 6% to the return result from the query - asp.net

<td>
<strong>
Rs #(Model.lstItem.Sum(c => c._product.option != null
? (c._product.option.Price * c.Quantity)
: (c._product.product.Price * c.Quantity))
- (Model.coupon != null ? (int)Model.coupon.Discount : 0))
</strong>
</td>
first of all let me tell you i am totally new in asp.net mvc..
Now my question is i have the above query which is returning total after multiplying price and quantity. now what i want is i want to increment 6% of pricequantity to the pricequanity..
The final result will be like (price * quantity) + 6% of price * quanity..
i hope you guys understand my question

You can just do this:
(price * quantity) + (((price * quantity)/100) * 6))

You shouldn't keep these constants in the View. If you have these calculations in multiple places and you wanted to change the percentage to 7% in the future, you'd have to change everywhere. So, it should either come from your database, or a config file or a constants class.
So, the easiest way to do this is to create a static class called ApplicationConstants in a Common folder or Utility folder.
public static class ApplicationConstants
{
public const int ProfitPercentage = 6;
}
Then in your View,
#using YourAppName.Common
<td>
<strong>
Rs #(
(1 + ApplicationConstants.ProfitPercentage / 100) *
(Model.lstItem.Sum(c => c._product.option != null
? (c._product.option.Price * c.Quantity)
: (c._product.product.Price * c.Quantity)))
- (Model.coupon != null ? (int)Model.coupon.Discount : 0))
</strong>
</td>

Related

Difference between startDate and endDate-Symfony

Can someone assist me with this, I have a student table and I want to ensure that the user enters a date range from now to 3 months. I tried this but it didn't give me the results I wanted. Thinking about using datediff but not sure where I would put it. Is there another way like using a custom validation to validate the date.
* #Assert\Range
* (
* min= "today", max="+3 months"
* )
Error message:
This value should be a valid number.
#Assert\Callback(methods={"validatePayrollPeriod"})
public function validatePayrollPeriod(ExecutionContextInterface $context) {
$days = $this->startdate->diff($this->enddate)->days;
if ($days <= 7 || $days > 14) {
$context->buildViolation('Not a valid payroll period.')
->atPath('enddate')
->addViolation();
}
}
You could try to make a validation in your setter.
Something like
public function setEndDate($endDate)
{
if(your end date - your start date < 3){
$this->endDate= $endDate;
}else{
Throw whatever exepection you cant
}
return $this;
}

Drupal: How to override a function from a contrib module

I am using a module that builds a price table for Drupal Commerce items. There is a function that formats the table headers:
/**
* Helper function that takes care of the quantity displayed in the headers of
* the price table.
*/
function commerce_price_table_display_quantity_headers($item) {
// Set the quantity text to unlimited if it's -1.
$max_qty = $item['max_qty'] == -1 ? t('Unlimited') : $item['max_qty'];
// If max and min qtys are the same, only show one.
if ($item['min_qty'] == $max_qty) {
$quantity_text = $item['min_qty'];
}
else {
$quantity_text = $item['min_qty'] . ' - ' . $max_qty;
}
return $quantity_text;
}
As you can see, this is not a theme function where I can override it in template.php but I can to tweak some of the output.
How can I redefine this function so I can chop and change a few things?
My work so far...
So far, I have tried to create it as a seperate module with a few subtle changes to show if it's working or not, but it's not overriding any of the output.
Info file
; $id$
name = Price Table: Tweaked Display
description = A different layout for the price table as shown on the product display nodes
package = Commerce (contrib)
core = 7.x
dependencies[] = commerce_product
dependencies[] = commerce_price
dependencies[] = commerce_price_table
Module File
/**
* Override of the helper function that takes care of the quantity displayed in the headers of
* the price table.
*/
function commerce_table_tweak_display_quantity_headers($item) {
// Set the quantity text to unlimited if it's -1.
$max_qty = $item['max_qty'] == -1 ? t('Unlimited gnhh') : $item['max_qty'];
// If max and min qtys are the same, only show one.
if ($item['min_qty'] == $max_qty) {
$quantity_text = $item['min_qty'];
}
else {
$quantity_text = $item['min_qty'] . ' - this is working - ' . $max_qty;
}
return $quantity_text;
}
Most drupal modules I believe have a hook_function (or at least ones that are hookable) I don't do this much so I would search the drupal api for how to hook functions and if your module will support it.

Dynamic indentation in devexpress aspxgridview

Are anyone aware of any method to achieve indentation in the ASPXGridView (we are running the 10.x version currently available)
What we got
What we'd like to achieve
Some information about the code-behind.
The grid is populated by an ObjectDataSource and the indentation is stored in a property alongside with the other data. In example the BMI row will have 0 indentation while the GENDER will have 1, and MAN will have 2. Etc...
The indentation is calculated runtime since relations might change.
public void GetItemsRecursive(int? parentId, int level)
{
List<qstFeedbackLine> q;
if (parentId == 0)
q = _db.qstFeedbackLines.Where(x => x.ParentId == null).ToList();
else
q = _db.qstFeedbackLines.Where(x => x.ParentId == parentId).ToList();
foreach (var item in q)
{
// Store the indent
item.Indent = level;
// Add item to List
_items.Add(item);
level++;
// ...and get the children of the current id
GetItemsRecursive(item.FeedBackLineId, level);
}
}
Any advice out there?
Thanks!
Ended up using the ASPxTreeList component instead!
http://demos.devexpress.com/ASPxTreeListDemos/

Database functions in Entity Framework could not return results

I have implemented the same function "distancebetween" as in Nerddinner. I created an airport repository and have these methods:
public IQueryable<AllAirports> ReturnAllAirportWithIn50milesOfAPoint(double lat, double lon)
{
var airports = from d in im.AllAirports
where DistanceBetween(lat, lon, (double)d.Lat, (double)d.Lon) < 1000.00
select d;
return airports;
}
[EdmFunction("AirTravelModel.Store", "DistanceBetween")]
public static double DistanceBetween(double lat1, double long1, double lat2, double long2)
{
throw new NotImplementedException("Only call through LINQ expression");
}
When I tested it, it shows:
base {"The specified method 'Double DistanceBetween(Double, Double, Double, Double)' on the type 'AirTravelMVC3.Models.Repository.AirportRepository' cannot be translated into a LINQ to Entities
store expression because no overload matches the passed arguments."} System.SystemException {System.NotSupportedException}
Do you have any ideas on why this happen? The only different between my work and nerddinner is that I used a POCO plugin in entity framework.
The SQL UDF is as follows, it works very well in the database:
CREATE FUNCTION [dbo].[DistanceBetween](#Lat1 as real,
#Long1 as real, #Lat2 as real, #Long2 as real)
RETURNS real
AS
BEGIN
DECLARE #dLat1InRad as float(53);
SET #dLat1InRad = #Lat1 * (PI()/180.0);
DECLARE #dLong1InRad as float(53);
SET #dLong1InRad = #Long1 * (PI()/180.0);
DECLARE #dLat2InRad as float(53);
SET #dLat2InRad = #Lat2 * (PI()/180.0);
DECLARE #dLong2InRad as float(53);
SET #dLong2InRad = #Long2 * (PI()/180.0);
DECLARE #dLongitude as float(53);
SET #dLongitude = #dLong2InRad - #dLong1InRad;
DECLARE #dLatitude as float(53);
SET #dLatitude = #dLat2InRad - #dLat1InRad;
/* Intermediate result a. */
DECLARE #a as float(53);
SET #a = SQUARE (SIN (#dLatitude / 2.0)) + COS (#dLat1InRad)
* COS (#dLat2InRad)
* SQUARE(SIN (#dLongitude / 2.0));
/* Intermediate result c (great circle distance in Radians). */
DECLARE #c as real;
SET #c = 2.0 * ATN2 (SQRT (#a), SQRT (1.0 - #a));
DECLARE #kEarthRadius as real;
/* SET kEarthRadius = 3956.0 miles */
SET #kEarthRadius = 6376.5; /* kms */
DECLARE #dDistance as real;
SET #dDistance = #kEarthRadius * #c;
return (#dDistance);
END
In order to do this using POCOs with the existing NerdDinner source I had to add this to the DinnerRepository class:
public IQueryable<Dinner> FindByLocation(float latitude, float longitude)
{
List<Dinner> resultList = new List<Dinner>();
var results = db.Database.SqlQuery<Dinner>("SELECT * FROM Dinners WHERE EventDate >= {0} AND dbo.DistanceBetween({1}, {2}, Latitude, Longitude) < 1000", DateTime.Now, latitude, longitude);
foreach (Dinner result in results)
{
resultList.Add(db.Dinners.Where(d => d.DinnerID == result.DinnerID).FirstOrDefault());
}
return resultList.AsQueryable<Dinner>();
}
Assuming DistanceBetween is implemented in c# The issue (as hinted at by #p.campbell) is that the query generator doesn't know how to calculate DistanceBetween
To get your code to work as is, you might need to do something like
public IQueryable<AllAirports> ReturnAllAirportWithIn50milesOfAPoint(double lat, double lon)
{
var airports = from d in im.AllAirports.ToList()
where DistanceBetween(lat, lon, (double)d.Lat, (double)d.Lon) < 1000.00
select d;
return airports;
}
the ToList() will force the AllAirports to evaluate to a list, then it can be evaluated in memory, using your c# function. Obviously this won't scale to a huge number of airports. If that was an issue, you might want to do a rough "box" query where you just do a cheap within-a-square expression to return a small number of airports, ToList that, and then call your distancebetween to refine the results.
I got this same exception after I made several changes to the DB schema and "updated the model from database".
After comparing my EDMX XML with the original from Nerd Dinner, I saw that mine had changed all of the types for the DistanceBetween function to "real", where Nerd Dinner had "float". Changing them back to float resolved the issue.

Use Take() to get a limited number of results but also get the potential total

Given the following...
int maxResults = 25;
string code = "Thailand";
var q = from i in images where i.component_code == code select i;
var images = q.OrderByDescending(x => x.image_rating).Take(maxResults);
if (images.Count() > 0)
{
...
lblResult = string.Format("Viewing {0} Images Of A Possible {1}", images.Count(), ?);
}
How do I get the potential total number of images that would have been returned if Take() had not been used
Can't you use q.Count() for this?

Resources