How to include count and sum query sets for models & views - django-models

I am trying to add up the number of tickets (count) and the (sum) of the price of each ticket purchased. The ticket is a fixed price of $25 each. I've used this in my models.py:
class Ticket(models.Model):
venue=models.CharField(max_length=100)
quantity=models.IntegerField(null=True)
price=models.DecimalField(max_digits=10, decimal_places=2)
loop=models.BooleanField(default=True)
purchaser = models.ForeignKey(User, related_name="purchases",
on_delete=models.PROTECT)
created_at=models.DateTimeField(auto_now_add=True)
updated_at=models.DateTimeField(auto_now=True)
I am confused where to do the query sets to get the total number of tickets and the summation of the prices? Would it be
total_price=Ticket.objects.all().aggregate(Sum('price'))
ticket_count = Tickets.objects.count()
Would the above variables (ex: total_price and _ticket_count) be included as columns in the models or it needs to be stated only in the views? Can quantity be the same as count? Thank you very, very much!

More on aggregate
from django.db.models import Sum
# This will give total tickets sold to all the user
total_tickets = Ticket.objects.aggregate(Sum('quantity'))
total_cost = total_tickets * 25

Related

MDX Percentage Row Not Calculating Correctly

I am trying to calculate a percentage row in a table based on a total column derived earlier in the query.
There are two columns then I create a total for that column. In the next row I want a percentage of the total to show. The output should be something like:
Life Non-Life (P&C) SumTotal
Premiums 66,104.44 916,792.51 982,896.95
Percentage 6.73% 93.27% 1
But instead the second line shows zero.
How do I structure the query to calculate correctly?
Here is the mdx code:
with member [LocationSpecificClass].[Hierarchy].[All].&[Insurance Market Statistics].[SumTotal] as SUM([LocationSpecificClass].[Hierarchy].[All].&[Insurance Market Statistics] , [Measures].[Value] )
member [LocationSpecificData].[Data Type].[All].[Percentage] as [LocationSpecificData].[Data Type].[All].[Gross Written Premiums Total - Calculated], [Measures].[Value] ) / ([LocationSpecificClass].[Hierarchy].[All].&[Insurance Market Statistics].[SumTotal], [Measures].[Value] )
Select { [LocationSpecificClass].[Hierarchy].[All].&[Insurance Market Statistics].&[Life], [LocationSpecificClass].[Hierarchy].[All].&[Insurance Market Statistics].&[Non-Life (P&C)], [LocationSpecificClass].[Hierarchy].[All].&[Insurance Market Statistics].[SumTotal] } On Columns ,
{ [LocationSpecificData].[Data Type].[All].[Gross Written Premiums Total - Calculated] , [LocationSpecificData].[Data Type].[All].[Percentage] } On Rows
FROM [CubeDatabase]
Please try the following example, which worked fine in my environment. I've used our date dimension [Datum] with the year attribute [Jahr] to add a second a line by adding [Line2]. The first shown line is the year 2016. Set the calculated measures [M1] and [M2] to your needs and exchange the year dimension accordingly.
WITH
MEMBER [Measures].[M1] AS [Measures].[name of your measure for col 1]
MEMBER [Measures].[M2] AS [Measures].[name of your measure for col 2]
MEMBER [Measures].[Total] AS [Measures].[M1] + [Measures].[M2]
MEMBER [Datum].[Jahr].[Line2] AS
Divide(
([Datum].[Jahr].&[2016], [Measures].CurrentMember),
([Datum].[Jahr].&[2016], [Measures].[Total])
)
SELECT {
[Measures].[M1],
[Measures].[M2],
[Measures].[Total]
} ON 0,
{
[Datum].[Jahr].&[2016],
[Datum].[Jahr].[Line2]
} ON 1
FROM [name of cube]
The result in my environment looks like this:

multiplication two columns in data grid wpf

I have a three columns
productname - mount - price
I need to show a fourth column when I insert mount and price , the fourth column is mount multiplication price as an example
productname - mount - price - Total,
Total= mount* Price
In your model class add another property called as Total and where you assign the other property's there assign the total value, now you can bind the Total property in your total column of data grid.
ex:
productname = aaa
mount = 1
price = 10
Total= mount * Price

Prestashop - how to set custom tax for products of one manufacturer?

My prestashop site has different manufacturers and I want to set 20% TAX/VAT for products of one manufacturer and don't effect the rest of manufacturers. Which way and how could I do it? Let's say the manufacturer identified by it's id - manufacturer_id = 1.
UPDATE ps_product_shop ps
LEFT JOIN
ps_product p ON ps.id_product = p.id_product
SET
ps.id_tax_rules_group = XXX
WHERE
p.id_manufacturer = 1;
replace XXX with actual tax rules id.
You need to repeat this update query for ps_product table to have consistent data.
must be online file upload on your website and call
require_once('config/defines.inc.php');
$id_tax_rules = XXXX; // ID TAX RULES GROUP
$id_manufacturer = 1; // ID MANUFACTURER
Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'product_shop SET id_tax_rules_group = '.$id_tax_rules.' WHERE id_manufacturer = '.$id_manufacturer);
Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'product SET id_tax_rules_group = '.$id_tax_rules.' WHERE id_manufacturer = '.$id_manufacturer);
?>
Regards

filter by last 13 months

I have controller that is using a standardSetController to implement pagination. I want to filter by the trade date for the last 13 months. The date literals don't have a filter by Last_N_Months:N
Is there a way I can filter by the last 13 months?
Here is my current query:
setCtrl = new ApexPages.StandardSetController(Database.getQueryLocator([select TransactionType__c, TradeDate__c, ShareClass__c,
SettlementDate__c, Name, Fund__r.Name, Fund__r.Id, FirstTransaction__c, DCPosition__c, DBR__r.Name, DBR__r.Id, DBR__c,
Amount__c from Transaction__c where DBRPrimaryContact__r.Contact__c =: con.Id ORDER BY TradeDate__c ASC]));
If I can't filter by 13 months, what is the total number of records that can be returned in a query? Is it 2000? There can be a significant number of records for this object and I want to limit the results by 13 months of data. Once I have that result set, I want to add filtering by options.
Thanks for any help.
Try the code below for filtering Date range - you can programatically calculate exact date ranges
DATE d1 = date.today();
Date d2 = d1.addMonths(-13);
Integer d3 = d2.daysBetween(d1);
System.debug('*************' + [SELECT Id FROM Account WHERE CreatedDate >= :d2 AND CreatedDate <=:d1]);
For filtering by date you have LAST_90_DAYS or LAST_N_DAYS:90 like this
SELECT Id FROM Account WHERE CreatedDate = LAST_N_DAYS:90
More info in this link
And yes the total number retrieved in one query is 2000

Trying to avoid Governor limits in no of SOQL queries

i have 2 custom objects appointment_c and TimeDrive_c.
Appointment has fields
startdate__c
contact__c
TimeDrive__c has
Target_date__c
contact__c
CreatedDate
Here is what i need to do
I need to get all get all records with in a specific date range
select id, Target_date__c, Contact__c, Name, CreatedDate
from TimeDrive__c where Target_date__c >=:startdate and
Target_date__c <=:enddate
i need to loop through each record in this list and check if there are appointments for this contact which has startdate fall between targetdate and createddate
Here is the bit i have done till now
timeDriverLst = [select id, Target_date__c, Contact__c, Name, CreatedDate
from TimeDrive__c where Target_date__c >=:startdate and
Target_date__c <=:enddate ];
if(timeDriverLst.size() >0){
for(integer i =0; i < timeDriverLst.size(); i++)
{
mapTime.put(timeDriverLst[i].id, timeDriverLst[i]);
/* appLst = [Select Name, Contact__c from Appointment__c where (StartDate__c > = :timeDriverLst[i].CreatedDate
and StartDateTime__c <=:timeDriverLst[i].Target_date__c) and Contact__c = :timeDriverLst[i].Contact__c ];
*/
}
I know i shouldnt have a SOQL query within a for loop. How can i avoid this and achieve the requirement.
An ugly, but possibly usable solution: You could get all the contact ids from the time driver list and also find the earliest created date. Then you could pull out all the appointments whose contact id is in the contact id list and whose date is between the earliest created date and the target date. Then you would need to do a double loop, checking each appointment against each time driver. (Ordering the appts by contact or by date as you retrieve them might help here).

Resources