PHP - MySQL Weekly Sales Report Query Issue

K Ahir

I am preparing weekly sales report for particular seller so he can see after login at any time.

Current week info is collected using below code.

<?php 
$dt_week_start_date = date('Y-m-d 20:00:01',strtotime("last Saturday"));
$dt_week_end_date = date('Y-m-d 20:00:00',strtotime("next Saturday"));
?>

Below query is used to get sold item data for specific seller from table for current week. This query works fine if I simply display sold item listing on page.

<?php
$str_query_select = "SELECT * FROM t_product_purchase ";
$str_query_select .= " WHERE purchasedatetime BETWEEN '".$dt_week_start_date ."' AND '".$dt_week_end_date."'";
$str_query_select .= " AND sellerpkid=1";
$str_query_select .= " ORDER BY purchasedatetime DESC ";
?>

But I need to show sold items order wise like below. I tried to do GROUP BY on purchasedatetime field and tried few other things but it's not working at all. Please help me to solve this isse.

Purchase Date-Time       Product      Price
2014-08-10 14.20.00      Item 010      $50
2014-08-10 14.20.00      Item 016      $20
                         ------------------
                         Total :       $70
                         + Shipping :  $10
                         - Promo Code :$ 5
                         ------------------
                         Sub Total :   $75


2014-08-13 09.08.10      Item 056      $20
2014-08-13 09.08.10      Item 056      $65
2014-08-13 09.08.10      Item 056      $ 5
                         ------------------
                         Total :       $90
                         + Shipping :  $15
                         - Promo Code :$ 5
                         ------------------
                         Sub Total :   $100

Total For week 2014-08-09 to 2014-08-16 : $175

Data are stored in database table in following way. All data are related to single seller only.

  • Buyer 01 buys 2 items together from site where shipping charge is $10 & promo code discount is $5 (so 2 records for 2 items are added in t_product_purchase table along with shipping charge and promo code discount)

  • Buyer 02 buys 3 items together from site where shipping charge is $15 & promo code discount is $5 (so 3 records for 3 items are added in t_product_purchase table along with shipping charge and promo code discount)

Here is database table structure:

CREATE TABLE IF NOT EXISTS `t_product_purchase` (
`purchasepkid` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`sellerpkid` bigint(20) unsigned NOT NULL DEFAULT '0',
`productpkid` bigint(20) unsigned NOT NULL DEFAULT '0',
`prodcatpkid` bigint(20) unsigned NOT NULL DEFAULT '0',
`extendedprice` double(10,2) NOT NULL DEFAULT '0.00',
`shippingvalue` double(10,2) NOT NULL DEFAULT '0.00',
`discountpercentage` float(6,2) NOT NULL DEFAULT '0.00',
`discountamount` float(6,2) NOT NULL DEFAULT '0.00',
`finaldiscountedamount` float(6,2) NOT NULL DEFAULT '0.00',
`quantity` smallint(6) NOT NULL DEFAULT '0',
`color` varchar(255) DEFAULT NULL,
`size` varchar(255) DEFAULT NULL,
`emailid` varchar(255) NOT NULL DEFAULT '',
`firstname` varchar(100) DEFAULT NULL,
`lastname` varchar(100) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
`city` varchar(100) DEFAULT NULL,
`state` varchar(100) DEFAULT NULL,
`country` varchar(100) DEFAULT NULL,
`zipcode` varchar(10) DEFAULT NULL,
`shippingaddress` text,
`specialnote` text,
`ipaddress` varchar(50) DEFAULT NULL,
`purchasedatetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`purchaseday` int(11) DEFAULT '0',
`purchasemonth` int(11) DEFAULT '0',
`purchaseyear` int(11) DEFAULT '0',
PRIMARY KEY (`purchasepkid`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Paullo

You can modify your query to get the Gross price while you do the other additions like the shipment and deductions like the Promo Code from within PHP.

Here is what I mean:

<?php
$str_query_select = "SELECT *, SUM(price_column) as gross_price FROM t_product_purchase ";
$str_query_select .= " WHERE purchasedatetime BETWEEN '".$dt_week_start_date ."' AND '".$dt_week_end_date."'";
$str_query_select .= " AND sellerpkid=1";
$str_query_select .= " GROUP BY purchasedatetime ORDER BY purchasedatetime DESC ";
?>

You can now do your additions and deductions: PLEASE NOTE: You must use the column that is unique for a particular transaction for your grouping if purchasedatetime is not.

Purchase Date-Time                     Price
2014-08-10 14.20.00      Gross Total : $70
                         + Shipping :  $10
                         - Promo Code :$ 5
                         ------------------
                         Sub Total :   $75


2014-08-13 09.08.10      Gross Total : $90
                         + Shipping :  $15
                         - Promo Code :$ 5
                         ------------------
                         Sub Total :   $100

Total For week 2014-08-09 to 2014-08-16 : $175

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related