I have a list of products and I have have included a the Order product: sum_qty field but it seems to count ones that are in checkout. Is there a way I can only count completed orders.

CommentFileSizeAuthor
#5 uc_views_reports.zip3.02 KBmarcus178

Comments

madsph’s picture

No unfortunately there is not. The field is from a database view that simply sums the quantity across all orders with out regard to order status.

I do however, think that you can use some of the standard views features (maybe combined with views_calc) to make your own sums on orders filtered on status.

marcus178’s picture

Well I just spent about 3 hours trying to figure this out, even tried views_calc but not really got anywhere.

I'm sure it must be possible just to have a list of products with how many have been sold i.e the same as the products report that comes with ubercart, and from that I can add the other fields I require.

madsph’s picture

Do you have any experience with coding views code for drupal?

I think that is your best option at this point - I have tried various approaches but none with success. There is a views_groupby module, that in time should solve your problem, but right now they only support COUNT() not SUM().

If you feel like trying, I think it is relatively easy to make your own custom module, with every thing you need.

What you need to do is to:
1. Take a copy of uc_views_marketing.
2. From that copy remove everything except what relates to uc_order_products_qty_vw in all files.
3. Replace uc_order_products_qty_vw to what ever you want in all files (to avoid name clashes with uc_views_marketing)
4. Replace uc_views_marketing to what ever you want in all files and filenames (again to avoid name clashes with uc_views_marketing)
5. In your .install file change the way the db view is created:

db_query("CREATE VIEW {USE WHAT YOU REPLACED WITH IN STEP 3} (nid,order_count,avg_qty,sum_qty,max_qty,min_qty)  AS SELECT `op`.`nid` AS `nid`, COUNT(`op`.`nid`) AS `order_count`,AVG(`op`.`qty`) AS `avg_qty`, SUM(`op`.`qty`) AS `sum_qty`,MAX(`op`.`qty`) AS `max_qty`, MIN(`op`.`qty`) AS `min_qty` FROM {uc_order_products} `op`, {uc_orders} `order` where `order`.`order_id` = `op`.`order_id` and `order`.`order_status` in ('completed') GROUP BY `op`.`nid` ORDER BY `op`.`nid`");
marcus178’s picture

I've done some coding so more than happy to have a bash at this, thanks for your guidance and I'll let you know how I get on.

marcus178’s picture

StatusFileSize
new3.02 KB

That was a lot easier than I thought it would be. I've created a new module called reports that only counts completed orders. I attached it for anyone else that needs a similar function. Thanks for your help.

madsph’s picture

Status: Active » Closed (fixed)

Great glad to help out, and thank you for posting your solution back for others to see.

marcus178’s picture

Status: Closed (fixed) » Active

Sorry to reopen this but I could do with some help as I also need to include another order status in the results but can't seem to work it out, I'm afraid I'm not very god with sql. The status I'm trying to include is pos_completed. I have tried the following.

db_query("CREATE VIEW {uc_order_products_qty_sold} (nid,order_count_sold,avg_qty_sold,sum_qty_sold,max_qty_sold,min_qty_sold) AS SELECT `op`.`nid` AS `nid`, COUNT(`op`.`nid`) AS `order_count_sold`,AVG(`op`.`qty`) AS `avg_qty_sold`, SUM(`op`.`qty`) AS `sum_qty_sold`,MAX(`op`.`qty`) AS `max_qty_sold`, MIN(`op`.`qty`) AS `min_qty_sold` FROM {uc_order_products} `op`, {uc_orders} `order` where `order`.`order_id` = `op`.`order_id` and `order`.`order_status` in ('pos_completed') OR `order`.`order_status` in ('completed') GROUP BY `op`.`nid` ORDER BY `op`.`nid`");

madsph’s picture

I thought you might want to do something like that, so I prepared the SQL to be easy to expand ;-)

The 'in' clause gives you the opportunity to write a list of valid hits for your field, so in stead of making an 'OR' you can simply add 'pos_completed' to the list, like this:

`order_status` in ('pos_completed', 'completed')

j0rd’s picture

I'm going to second this feature. It really only makes sense to create values on the `uc_order_products_qty_vw` for completed orders. Otherwise the numbers are not reflective of actual sales.

I know you can add new order_statuses in Ubercart at will, so there should be a configuration page, where the people can select which order_statuses they want in their query to count towards these sums.

I guess the problem on why you can't do this is because you `CREATE VIEW` in the .install file.

Here's what I did to hack this in:

> DROP VIEW uc_order_products_qty_vw;
> CREATE VIEW uc_order_products_qty_vw (nid,order_count,avg_qty,sum_qty,max_qty,min_qty)  AS SELECT op.nid AS nid, COUNT(op.nid) AS order_count,AVG(op.qty) AS avg_qty, SUM(op.qty) AS sum_qty,MAX(op.qty) AS max_qty, MIN(op.qty) AS min_qty FROM uc_order_products op INNER JOIN uc_orders o USING (order_id) WHERE o.order_status='completed' GROUP BY op.nid ORDER BY op.nid;

You might have to do something like this for the other tables if you require this functionality.

j0rd’s picture

@marcus178 you're module is great for those who only want to get simple order total reports with out all the rest of the functionality of uc_views.

I'd recommend removing the uc_views and views_slideshow dependancies from the .info file and adding this module to drupal.org so people can find it easy.

It would also be a good idea for uc_views to either absorb this module or remove it's dependancies for uc_views_marketting (since I don't think uc_views is actually required).

my two cents.

Decided to go with marcus' module instead, since it does what I need and nothing else ;)

ken hawkins’s picture

Category: support » feature
Status: Active » Needs review

Realize this issue queue is quite old, however the small module by marcus178 in #5 is still perfectly valid and quite usable.

Marking as "needs review" in hopes of getting this considered for inclusion.