How can i display the delivery orders automatically in a way that the most recent is the first? - odoo-9

I'm using odoo 9 and i want to display delivery orders in a way that the last one (the most recent) is the first to display automatically . I tried to apply a filter to do this order but there is no solution. The only solution that i found is to press on the field date to make it in order that the last delivery created is shows the first. Is there a solution to do that automatically espacially that the sale orders display in thist way? Any idea for help please ?

Just inherit the stock.picking object and add the "_order" attribute(order by create_date descending)
_order = "create_date desc, priority desc, date asc, id desc"
This will show the recent created Delivery orders first
class Picking(models.Model):
_inherit = "stock.picking"
_description = "Transfer"
_order = "create_date desc, priority desc, date asc, id desc"

Related

Get Total amount & Quantity for same ID

Good Afternoon...
I wanted to get the data from database which will be groupby customerID and base on same data in below format :
customerID
From date
to Date
Total Quantity
Total Amount
Refer attached images for my database.
I able to get data groupby customerID but stuck for futher details.
$test = Dairyincome::get()->groupBy('customerID')->toArray();
dump($test);
Expected result
customerID
From date
to Date
Total Quantity
Total Amount
Cust-01
2022-02-10
2022-02-11
(10+2.3)=12.30
(450+98.90)=548.90
same for other ID
Hope i explained my problem and thanks in Advance
I don't recommend using collection (you use ->groupBy() after ->get()).
You need to know how it can work using SQL first, before using Eloquent or the Query Builder.
I assume your table is dairyincomes :
SELECT
customerID,
MIN(date) as "From date",
MAX(date) as "To Date",
SUM(quantity) as "Total Quantity",
SUM(amount) as "Total Amount"
FROM
dairyincomes
GROUP BY
customerID
The MIN() function returns the minimum value in a set of values.
The MAX() function returns the maximum value in a set of values.
The SUM() function is an aggregate function that allows you to calculate the sum of values in a set.
Eloquent :
Dairyincome::selectRaw('customerID, MIN(date) as "From date", MAX(date) as "To Date", SUM(quantity) as "Total Quantity", SUM(amount) as "Total Amount"')
->groupBy('customerID')
->get();
Also, you can use DB::raw() btw.
Reference :
https://laravel.com/docs/9.x/queries

Get order between two status

I have a problem in getting the sum of time taken between an order
I can able the get the created time by the following code:
$collection = Mage::getResourceModel('sales/order_status_history_collection')
->addAttributeToSelect('created_at')
->addAttributeToFilter('status', array('eq'=>'complete'))
->load()
is there a way to get the sum of time taken between two status like sum(OrderInvoiced+OrderShipped) ?
Any help would be appreciated.
You can use the direct SQL queries method to get the time difference between invoice and shipment created.
Magento Direct SQL Queries
Invoice is saved under sales_flat_invoice and shipment are saved under sales_flat_shipment. There are 2 dates for each created_at and updated_at in both tables.
Here is the SQL query to get the difference between invoice and shipment based on created_at column
SELECT TIMEDIFF(ss.created_at,si.created_at) As TimeTaken
FROM `sales_flat_invoice` si
JOIN `sales_flat_shipment` ss
ON si.order_id=ss.order_id
WHERE si.order_id=1
To get the time difference for all the order just remove WHERE si.order_id=1 from the above query.

Newest items and GROUP By with Eloquent

I have the following prices-table:
shop_id (int)
product_id (int)
price (float)
created (DateTime)
Every hour a cronjob checks the shops and inserts new entries (current prices) into these price-table.
Now I want to display the newest price for a product. I have to GROUP BY the shop_id because I only want one price per shop but I only want the newest entry (created).
Can I solve this with Eloquent Query-Builder or do I have to use raw SQL? Is it possible to pass the result of a raw SQL-query into a model if the columns are the same?
You can try it as:
Price::select('*', DB::raw('MAX(created_at) as max_created_at'))
->groupBy('shop_id')
->get()
Assuming model name is Price
Eloquent (purist) approach:
Price::orderBy('created', 'desc')->groupBy('shop_id')
->get('shop_id', 'price');
References:
https://laravel.com/api/5.3/Illuminate/Database/Query/Builder.html#method_orderBy
https://laravel.com/api/5.3/Illuminate/Database/Query/Builder.html#method_groupBy
https://laravel.com/api/5.3/Illuminate/Database/Query/Builder.html#method_get
*untested though
Q: Is it possible to pass the result of a raw SQL-query into a model if the columns are the same?
A: you could pass it to Model's contructor - but it might need model's field to be fillable - or hydrate a model. Alternatively, just access it like an keyed-array, ie. $something[0]['price'] <-- assuming an array of prices with price column.
I solved the problem without QueryBuilder. Instead I use a raw SQL-statement and generating the models with the hydrateRaw()-function of the Model-class.
$prices = Price::hydrateRaw( 'SELECT p.*
FROM prices p
INNER JOIN (
SELECT shop_id, max(created_at) AS max_ca
FROM prices p1
GROUP BY shop_id
) m ON p.shop_id = m.shop_id AND p.created_at = m.max_ca');

Active Record Join with most recent association object attribute

I have a Contact model which has many Notes. On one page of my app, I show several attributes of a table of contacts (name, email, latest note created_at).
For the note column, I'm trying to write a joins statement that grabs all contacts along with just their latest note (or even just the created_at of it
What I've come up with is incorrect as it limits and orders the contacts, not their notes:
current_user.contacts.joins(:notes).limit(1).order('created_at DESC')
If you just want the created_at value for the most recent note for each contact, you can first create a query to find the max value and then join with that query:
max_times = Note.group(:contact_id).select("contact_id, MAX(created_at) AS note_created_at").to_sql
current_user.contacts.select("contacts.*, note_created_at").joins("LEFT JOIN (#{max_times}) max_times ON contacts.id = max_times.contact_id")
If you want to work with the Note object for the most recent notes, one option would be to select the notes and group them by the contact_id. Then you can read them out of the hash as you work with each Contact.
max_times = Note.group(:contact_id).select("contact_id, MAX(created_at) AS note_created_at").to_sql
max_notes = Note.select("DISTINCT ON (notes.contact_id) notes.*").joins("INNER JOIN (#{max_times}) max_times ON notes.contact_id = max_times.contact_id AND notes.created_at = note_created_at").where(contact_id: current_user.contact_ids)
max_notes.group_by(&:contact_id)
This uses DISTINCT ON to drop dups in case two notes have exactly the same contact_id and created_at values. If you aren't using PostgreSQL you'll need another way to deal with dups.

Distinct by order date and products SQL Server 2008

This is my first post here so if I've been mistaken for my post and English I say sorry :)
I already do research in google and unfortunately until now I can't solve this.
This my problem, show the total orders by date on each product and I want to distinct by products
Here's my query sample
SELECT Orders.OrderDate
,SUM(OrderDetails.OrderDetailQuantity) AS totalOrdered
,Products.ProductId
FROM Orders
INNER JOIN OrderDetails ON Orders.OrderId = OrderDetails.OrderId
INNER JOIN Products ON OrderDetails.ProductId = Products.ProductId
GROUP BY Orders.OrderId
,Orders.OrderDate
,Products.ProductId
HAVING (CONVERT(VARCHAR, Orders.OrderDate, 23) BETWEEN #from AND #to)
Now, I want to distinct by product according to the between OrderDate, I know the big problem here is the DATE but I don't have any idea on how to distinct the date by this query.
Please help me. Thank you.
P.S.: if do you want to solve this in linq expression it would be highly accepted :)
Sample data and desired results would help. When you say "distinct by" I assume you mean group by. Note, in the WHERE clause you dont need to cast Order.OrderDate if you ensure that the time component of your #from & #to params are set correctly (to include each entire day). Its never a good idea to apply a cast operation to the left side of a comparison.
SELECT --cast(Orders.OrderDate as date),
Products.ProductId
SUM(OrderDetails.OrderDetailQuantity) AS totalOrdered,
FROM Orders
INNER JOIN OrderDetails ON Orders.OrderId = OrderDetails.OrderId
INNER JOIN Products ON OrderDetails.ProductId = Products.ProductId
where Orders.OrderDate between cast(#from as date) AND cast(#to as date)
GROUP
BY --cast(Orders.OrderDate as date),
Products.ProductId
-- to illustrate:
declare #From datetime = '1/1/2000 10:30:22',
#To datetime = '1/3/2000 9:11:31'
declare #orders table (i int, dt datetime)
insert into #orders
values(1, '1/1/2000 8:00'),(2, '1/2/2000 10:00'), (3, '1/4/2000 3:00')
-- only returns i=2
select *
from #orders
where dt between #From and #To
-- returns i=1 & i=2
select *
from #orders
where dt between cast(#From as date) and cast(#To as date)

Resources