hughesca Posted July 28, 2011 Posted July 28, 2011 Hello there! I was hoping someone might be able to help me figure out how to extract the data from the orders_total table. I've installed the Apsona Export Orders to CSV addon and it gives me just about everything I need. I've added a few additional fields, but I can't seem to figure out how to extract the orders_total data (subtotal, tax, shipping, total) as they are sorted in a way that I can't figure out how to accurately extract the data. Here is the bit of code I'm modifying: case "orders": $query = 'SELECT orders.orders_id as uuid, orders.customers_id as order__customer_id, orders.customers_name as order__customer_name, orders.customers_company as order__customer_company, orders.customers_street_address as order__customer_street_address, orders.customers_suburb as order__customer_suburb, orders.customers_city as order__customer_city, orders.customers_postcode as order__customer_postcode, orders.customers_state as order__customer_state, orders.customers_country as order__customer_country, orders.customers_telephone as order__customer_telephone, orders.customers_email_address as order__customer_email_address, orders.delivery_name as order__delivery_name, orders.delivery_company as order__delivery_company, orders.delivery_street_address as order__delivery_street_address, orders.delivery_suburb as order__delivery_suburb, orders.delivery_city as order__delivery_city, orders.delivery_postcode as order__delivery_postcode, orders.delivery_state as order__delivery_state, orders.delivery_country as order__delivery_country, orders.billing_name as order__billing_name, orders.billing_company as order__billing_company, orders.billing_street_address as order__billing_street_address, orders.billing_suburb as order__billing_suburb, orders.billing_city as order__billing_city, orders.billing_postcode as order__billing_postcode, orders.billing_state as order__billing_state, orders.billing_country as order__billing_country, orders.payment_method as order__payment_method, orders.last_modified as order__last_modified, orders.date_purchased as order__date_purchased, orders.orders_status as order__order_status, orders_status.orders_status_name as order__order_status_name, orders.orders_date_finished as order__date_finished, orders.currency as order__currency, orders.currency_value as order__currency_value FROM ' . TABLE_ORDERS . ' orders, ' . TABLE_ORDERS_STATUS .' orders_status WHERE orders.orders_status = orders_status.orders_status_id and orders.date_purchased >= '. $dateSince; break; The table orders_total doesn't contain table headings such as subtotal, tax, shipping and total. Instead they are defined in the table headings class and title. I'm not familiar with this structure and can't figure out the syntax to extract the data using the above function. Any help would be greatly appreciated. ;) Peace, Chris
hughesca Posted July 28, 2011 Author Posted July 28, 2011 Well, I've figured out part of it...but I'm still stuck as well, LOL! I changed the bottom of the above code to: orders.currency as order__currency, orders.currency_value as order__currency_value, ot.orders_id as ot__orders_id, ot.value as ot__value, ot.class as ot__class FROM ' . TABLE_ORDERS . ' orders, ' . TABLE_ORDERS_STATUS .' orders_status, ' . TABLE_ORDERS_TOTAL . ' as ot WHERE ot.orders_id = orders_status.orders_status_id and orders.orders_status = orders_status.orders_status_id and orders.date_purchased >= '. $dateSince; break; This gives me the data I need, but the format is still off. I get the table heading ot.class, with values of ot_subtotal, ot_shipping and ot_total under it which results in producing triplicate records in my csv for each one. Somehow I need to turn the values for the ot.class table heading into table headings themselves for the .csv file...Help! Here's the table structure: -- -- Table structure for table `orders_total` -- CREATE TABLE `orders_total` ( `orders_total_id` int(10) unsigned NOT NULL auto_increment, `orders_id` int(11) NOT NULL, `title` varchar(255) NOT NULL, `text` varchar(255) NOT NULL, `value` decimal(15,4) NOT NULL, `class` varchar(32) NOT NULL, `sort_order` int(11) NOT NULL, PRIMARY KEY (`orders_total_id`), KEY `idx_orders_total_orders_id` (`orders_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=13 ; -- -- Dumping data for table `orders_total` -- INSERT INTO `orders_total` VALUES(1, 1, 'Sub-Total:', '$12.95', 12.9500, 'ot_subtotal', 1); INSERT INTO `orders_total` VALUES(2, 1, 'Flat Rate (Best Way):', '$5.00', 5.0000, 'ot_shipping', 2); INSERT INTO `orders_total` VALUES(3, 1, 'Total:', '<strong>$17.95</strong>', 17.9500, 'ot_total', 4); INSERT INTO `orders_total` VALUES(4, 2, 'Sub-Total:', '$27.66', 27.6600, 'ot_subtotal', 1); INSERT INTO `orders_total` VALUES(5, 2, 'Flat Rate (Best Way):', '$5.00', 5.0000, 'ot_shipping', 2); INSERT INTO `orders_total` VALUES(6, 2, 'Total:', '<strong>$32.66</strong>', 32.6600, 'ot_total', 4); INSERT INTO `orders_total` VALUES(7, 3, 'Sub-Total:', '$55.32', 55.3200, 'ot_subtotal', 1); INSERT INTO `orders_total` VALUES(8, 3, 'Flat Rate (Best Way):', '$5.00', 5.0000, 'ot_shipping', 2); INSERT INTO `orders_total` VALUES(9, 3, 'Total:', '<strong>$60.32</strong>', 60.3200, 'ot_total', 4); INSERT INTO `orders_total` VALUES(10, 4, 'Sub-Total:', '$79.59', 79.5900, 'ot_subtotal', 1); INSERT INTO `orders_total` VALUES(11, 4, 'Free Shipping (For orders of $75.00 or more with a maximum package weight of 50 lbs ):', '$0.00', 0.0000, 'ot_shipping', 3); INSERT INTO `orders_total` VALUES(12, 4, 'Total:', '<strong>$79.59</strong>', 79.5900, 'ot_total', 4); Peace, Chris
hughesca Posted July 28, 2011 Author Posted July 28, 2011 Here's a visual example of what the issue is. Below is an example of my current output. order__currency order__currency_value ot__value ot__class USD 1 12.95 ot_subtotal USD 1 5 ot_shipping USD 1 17.95 ot_total USD 1 12.95 ot_subtotal USD 1 5 ot_shipping USD 1 17.95 ot_total USD 1 12.95 ot_subtotal USD 1 5 ot_shipping USD 1 17.95 ot_total USD 1 12.95 ot_subtotal USD 1 5 ot_shipping USD 1 17.95 ot_total This is how I would like it to be: order__currency order__currency_value ot_subtotal ot_shipping ot_total USD 1 12.95 5 17.95 USD 1 12.95 5 17.95 USD 1 12.95 5 17.95 USD 1 12.95 5 17.95
vishalchauhan Posted July 29, 2011 Posted July 29, 2011 Hi Try below query <table border="0" width="100%"> <tr><td>order__currency </td><td>order__currency_value</td><td>ot_subtotal</td><td>ot_shipping</td><td>ot_total</td></tr> <?php $order_q = tep_db_query('select distinct(o.orders_id), o.currency as order__currency, o.currency_value as order__currency_value FROM ' . TABLE_ORDERS . ' o , ' . TABLE_ORDERS_STATUS .' os WHERE o.orders_status = os.orders_status_id'); while($orders = tep_db_fetch_array($order_q)){?> <tr><td><?php echo $orders['order__currency'];?></td> <td><?php echo $orders['order__currency_value'];?></td> <?php $order_total_query = tep_db_query("select ot.orders_id as ot__orders_id, ot.value as ot__value, ot.class as ot__class from ".TABLE_ORDERS_TOTAL." ot where ot.orders_id = ".$orders['orders_id']." order by ot.orders_total_id"); while($order_total = tep_db_fetch_array($order_total_query)){ ?> <td><?php echo $order_total['ot__value']; ?></td> <?php } ?> </tr> <?php } ?> </table> May be it will help you. Please take backup of your files before do changes suggested by me
hughesca Posted July 29, 2011 Author Posted July 29, 2011 Hey there vishalchauhan! Thanks much for the reply, I do appreciate it. ;) While I see what is being done there, I don't quite see a way to implement it into the current function as it's using a loop to build the .csv file. Here is the full file: <?php // // Apsona functions: Needed to export data to Apsona in CSV format // // // Author: [email protected] // Copyright 2009 apsona.com // function apsona_toCSV ($fields) { $delimiter = ','; $enclosure = '"'; $i = 0; $csvline = ''; $escape_char = '\\'; $field_cnt = count($fields); $enc_is_quote = in_array ($enclosure, array('"',"'")); reset($fields); foreach( $fields AS $name => $value ) { if ($i > 0) { $csvline .= $delimiter; } $csvline .= $enclosure; /* enclose a field that contains a delimiter, an enclosure character, or a newline */ if( is_string($value)) { $csvline .= str_replace (array ($enclosure, "\r\n", "\r"), array ($enclosure . $enclosure, "\n", ""), $value); } else { $csvline .= $value; } $csvline .= $enclosure; $i++; } $csvline .= "\n"; return $csvline; }; function apsona_writeCSV ($tableName, $dateSince /* , $dbResource */) { // Do not change the field aliases in the 'as' clauses of the queries below, because they are matched exactly on the // Apsona end. switch ($tableName) { case "categories": $query = 'select c.categories_id as uuid, c.categories_image as category__image, d.categories_name as category__name, c.sort_order as category__sort_order, c.date_added as category__date_added, c.last_modified as category__last_modified from ' . TABLE_CATEGORIES . ' c, ' .TABLE_CATEGORIES_DESCRIPTION . ' d where c.categories_id = d.categories_id and d.language_id = 1 and (c.date_added >= ' . $dateSince . ' or c.last_modified >= ' . $dateSince . ')'; break; case "customers": $query = 'SELECT c.customers_id as uuid, c.customers_gender as customer__gender, c.customers_firstname as customer__first_name, c.customers_lastname as customer__last_name, c.customers_dob as customer__DOB, c.customers_email_address as customer__email_address, c.customers_telephone as customer__telephone, c.customers_fax as customer__fax, c.customers_newsletter as customer__newsletter_ok, o.entry_street_address as customer__street_address, o.entry_suburb as customer__suburb, o.entry_postcode as customer__postcode, o.entry_city as customer__city, z.zone_name as customer__state, t.countries_name as customer__country, ci.customers_info_date_account_created as created, ci.customers_info_date_account_last_modified as modified FROM ' . TABLE_CUSTOMERS . ' c, ' . TABLE_ADDRESS_BOOK . ' o, ' . TABLE_ZONES . ' z, ' . TABLE_COUNTRIES . ' t, ' . TABLE_CUSTOMERS_INFO . ' ci WHERE c.customers_default_address_id = o.address_book_id and o.entry_country_id = t.countries_id and ci.customers_info_id = c.customers_id and z.zone_id = o.entry_zone_id and (ci.customers_info_date_account_created >= ' . $dateSince . ' or ci.customers_info_date_account_last_modified >= ' . $dateSince . ')'; break; case "orders": $query = 'SELECT orders.orders_id as uuid, orders.customers_id as order__customer_id, orders.customers_name as order__customer_name, orders.customers_company as order__customer_company, orders.customers_street_address as order__customer_street_address, orders.customers_suburb as order__customer_suburb, orders.customers_city as order__customer_city, orders.customers_postcode as order__customer_postcode, orders.customers_state as order__customer_state, orders.customers_country as order__customer_country, orders.customers_telephone as order__customer_telephone, orders.customers_email_address as order__customer_email_address, orders.delivery_name as order__delivery_name, orders.delivery_company as order__delivery_company, orders.delivery_street_address as order__delivery_street_address, orders.delivery_suburb as order__delivery_suburb, orders.delivery_city as order__delivery_city, orders.delivery_postcode as order__delivery_postcode, orders.delivery_state as order__delivery_state, orders.delivery_country as order__delivery_country, orders.billing_name as order__billing_name, orders.billing_company as order__billing_company, orders.billing_street_address as order__billing_street_address, orders.billing_suburb as order__billing_suburb, orders.billing_city as order__billing_city, orders.billing_postcode as order__billing_postcode, orders.billing_state as order__billing_state, orders.billing_country as order__billing_country, orders.payment_method as order__payment_method, orders.last_modified as order__last_modified, orders.date_purchased as order__date_purchased, orders.orders_status as order__order_status, orders_status.orders_status_name as order__order_status_name, orders.orders_date_finished as order__date_finished, orders.currency as order__currency, orders.currency_value as order__currency_value, ot.value as ot__value, ot.class as ot__class FROM ' . TABLE_ORDERS . ' orders, ' . TABLE_ORDERS_STATUS .' orders_status, ' . TABLE_ORDERS_TOTAL . ' as ot WHERE ot.orders_id = orders_status.orders_status_id and orders.orders_status = orders_status.orders_status_id and orders.date_purchased >= '. $dateSince; break; case "orders_products_attributes": $query = 'select opa.orders_products_attributes_id as uuid, opa.orders_products_id as opa__order_product_id, opa.products_options as opa__product_option, opa.products_options_values as opa__product_option_value, opa.options_values_price as opa__option_value_price, opa.price_prefix as opa__price_prefix from ' . TABLE_ORDERS_PRODUCTS_ATTRIBUTES . ' opa, ' . TABLE_ORDERS . ' orders where opa.orders_id = orders.orders_id and orders.date_purchased >= '. $dateSince; break; case "orders_products": $query = 'SELECT o.orders_products_id as uuid, o.orders_id as order_product__order_id, o.products_id as order_product__product_id, o.products_model as order_product__product_model, o.products_name as order_product__product_name, o.products_price as order_product__product_price, o.final_price as order_product__final_price, o.products_tax as order_product__product_tax, o.products_quantity as order_product__product_quantity FROM ' . TABLE_ORDERS_PRODUCTS . ' as o, ' . TABLE_ORDERS . ' orders where o.orders_id = orders.orders_id and orders.date_purchased >= '. $dateSince; break; case "orders_status_history": $query = 'SELECT osh.orders_status_history_id as uuid, osh.orders_id as osh__order_id, osh.date_added as osh__date_added, osh.customer_notified as osh__customer_notified, os.orders_status_name as osh__order_status_name from ' . TABLE_ORDERS_STATUS_HISTORY . ' osh, ' . TABLE_ORDERS_STATUS . ' os where osh.orders_status_id = os.orders_status_id'; break; case "products": $query = 'select p.products_id as uuid, d.products_name as product__name, d.products_description as product__description, d.products_url as product__url, d.products_viewed as product__viewed_count, p.products_quantity as product__quantity, p.products_model as product__model, p.products_image as product__image, p.products_price as product__price, p.products_date_added as product__date_added, p.products_last_modified as product__last_modified, p.products_date_available as product__date_available, p.products_weight as product__weight, p.products_status as product__enabled, p.products_tax_class_id as product__taxable, m.manufacturers_name as product__manufacturer, p.products_ordered as product__products_ordered from ' . TABLE_PRODUCTS . ' p left join ' . TABLE_MANUFACTURERS . ' m on p.manufacturers_id = m.manufacturers_id,'. TABLE_PRODUCTS_DESCRIPTION . ' d where p.products_id = d.products_id and d.language_id = 1 and (p.products_date_added >= ' . $dateSince . ' or p.products_last_modified >= ' . $dateSince . ')'; break; }; if (isset ($query)) { $qResult = mysql_query ($query /* , $dbResource */); if ($qResult) { $nFields = mysql_num_fields ($qResult); for ($i = 0; $i < $nFields; $i++) { if ($i > 0) { echo ","; } echo '"' . mysql_field_name ($qResult, $i) . '"'; } echo "\n"; while ($row = mysql_fetch_assoc ($qResult)) { $line = apsona_toCSV ($row); echo $line; }; } } }; function apsona_non_null ($value) { if (is_array($value)) { return (sizeof($value) > 0); } return (is_string($value) || is_int($value)) && ($value != '') && ($value != 'NULL') && (strlen(trim($value)) > 0); } ?> Specifically, I'm modifying the "case "orders":" under the apsona_writeCSV function. Any ideas? Peace, Chris
hughesca Posted July 29, 2011 Author Posted July 29, 2011 A little birdie told me that I need to self join for each type of row I need as a column. Any tips on how to accomplish this? Peace, Chris
hughesca Posted July 29, 2011 Author Posted July 29, 2011 Well, I've been able to figure out how to get two of them to display...but I'm having difficulty getting ot_total into the syntax. SELECT ot1.orders_id, value AS ot_subtotal, ot2.ot_shipping FROM orders_total AS ot1 INNER JOIN ( SELECT orders_id, value AS ot_shipping FROM orders_total WHERE class='ot_subtotal' ) AS ot2 ON ot1.orders_id=ot2.orders_id AND ot1.class='ot_subtotal' Looks like so now: order__currency order__currency_value ot_subtotal ot_shipping USD 1 12.9500 5.0000 USD 1 12.9500 5.0000 USD 1 12.9500 5.0000 USD 1 12.9500 0.0000 Any ideas on how to add ot_total to the mix? Peace, Chris
hughesca Posted July 29, 2011 Author Posted July 29, 2011 Another update. I've finally got a query to format the info correctly, however I'm now having difficulties integrating it into the function. Here is the query that works: SELECT ot1.orders_id, value AS ot_subtotal, ot2.ot_shipping, ot3.ot_total FROM orders_total AS ot1 INNER JOIN ( SELECT orders_id, value AS ot_shipping FROM orders_total WHERE class=ot_shipping ) AS ot2 ON ot1.orders_id=ot2.orders_id AND ot1.class=ot_subtotal INNER JOIN ( SELECT orders_id, value AS ot_total FROM orders_total WHERE class=ot_total ) AS ot3 ON ot1.orders_id=ot3.orders_id AND ot1.class=ot_subtotal It outputs like so: orders_id ot_subtotal ot_shipping ot_total 1 12.9500 5.0000 17.9500 2 27.6600 5.0000 32.6600 3 55.3200 5.0000 60.3200 4 79.5900 0.0000 79.5900 However, when I merge it into the existing function, it outputs a blank page. Here's the updated code: case "orders": $query = 'SELECT orders.orders_id as uuid, orders.customers_id as order__customer_id, orders.customers_name as order__customer_name, orders.customers_company as order__customer_company, orders.customers_street_address as order__customer_street_address, orders.customers_suburb as order__customer_suburb, orders.customers_city as order__customer_city, orders.customers_postcode as order__customer_postcode, orders.customers_state as order__customer_state, orders.customers_country as order__customer_country, orders.customers_telephone as order__customer_telephone, orders.customers_email_address as order__customer_email_address, orders.delivery_name as order__delivery_name, orders.delivery_company as order__delivery_company, orders.delivery_street_address as order__delivery_street_address, orders.delivery_suburb as order__delivery_suburb, orders.delivery_city as order__delivery_city, orders.delivery_postcode as order__delivery_postcode, orders.delivery_state as order__delivery_state, orders.delivery_country as order__delivery_country, orders.billing_name as order__billing_name, orders.billing_company as order__billing_company, orders.billing_street_address as order__billing_street_address, orders.billing_suburb as order__billing_suburb, orders.billing_city as order__billing_city, orders.billing_postcode as order__billing_postcode, orders.billing_state as order__billing_state, orders.billing_country as order__billing_country, orders.payment_method as order__payment_method, orders.last_modified as order__last_modified, orders.date_purchased as order__date_purchased, orders.orders_status as order__order_status, orders_status.orders_status_name as order__order_status_name, orders.orders_date_finished as order__date_finished, orders.currency as order__currency, orders.currency_value as order__currency_value, ot1.orders_id, value AS ot_subtotal, ot2.ot_shipping, ot3.ot_total FROM ' . TABLE_ORDERS . ' orders, ' . TABLE_ORDERS_STATUS .' orders_status, ' . TABLE_ORDERS_TOTAL . ' as ot1 INNER JOIN ( SELECT orders_id, value AS ot_shipping FROM orders_total WHERE class=\'ot_shipping\' ) AS ot2 ON ot1.orders_id=ot2.orders_id AND ot1.class=\'ot_subtotal\' INNER JOIN ( SELECT orders_id, value AS ot_total FROM orders_total WHERE class=\'ot_total\' ) AS ot3 ON ot1.orders_id=ot3.orders_id AND ot1.class=\'ot_subtotal\' WHERE ot.orders_id = orders_status.orders_status_id and orders.orders_status = orders_status.orders_status_id and orders.date_purchased >= '. $dateSince; break; Ideas?
hughesca Posted July 29, 2011 Author Posted July 29, 2011 Ok...one step closer again. ;) This line: WHERE ot.orders_id = orders_status.orders_status_id and orders.orders_status = orders_status.orders_status_id and orders.date_purchased >= '. $dateSince; I removed: ot.orders_id = orders_status.orders_status_id and So now it looks like so: WHERE orders.orders_status = orders_status.orders_status_id and orders.date_purchased >= '. $dateSince; It now gives me a populated .csv file, in the correct format (no more duplicate entries). It contains the table headings orders_id, ot_subtotal, ot_shipping and ot_total as I needed. However, the data under those headings is incorrect, it's populating with all the same data 1, 12.95, 5 and 17.95. I'm close...;)
hughesca Posted July 29, 2011 Author Posted July 29, 2011 Woohoo! This did it. ;) case "orders": $query = 'SELECT orders.orders_id as uuid, orders.customers_id as order__customer_id, orders.customers_name as order__customer_name, orders.customers_company as order__customer_company, orders.customers_street_address as order__customer_street_address, orders.customers_suburb as order__customer_suburb, orders.customers_city as order__customer_city, orders.customers_postcode as order__customer_postcode, orders.customers_state as order__customer_state, orders.customers_country as order__customer_country, orders.customers_telephone as order__customer_telephone, orders.customers_email_address as order__customer_email_address, orders.delivery_name as order__delivery_name, orders.delivery_company as order__delivery_company, orders.delivery_street_address as order__delivery_street_address, orders.delivery_suburb as order__delivery_suburb, orders.delivery_city as order__delivery_city, orders.delivery_postcode as order__delivery_postcode, orders.delivery_state as order__delivery_state, orders.delivery_country as order__delivery_country, orders.billing_name as order__billing_name, orders.billing_company as order__billing_company, orders.billing_street_address as order__billing_street_address, orders.billing_suburb as order__billing_suburb, orders.billing_city as order__billing_city, orders.billing_postcode as order__billing_postcode, orders.billing_state as order__billing_state, orders.billing_country as order__billing_country, orders.payment_method as order__payment_method, orders.last_modified as order__last_modified, orders.date_purchased as order__date_purchased, orders.orders_status as order__order_status, orders_status.orders_status_name as order__order_status_name, orders.orders_date_finished as order__date_finished, orders.currency as order__currency, orders.currency_value as order__currency_value, ot1.orders_id, value AS ot_subtotal, ot2.ot_shipping, ot3.ot_total FROM orders INNER JOIN orders_status ON orders.orders_status = orders_status.orders_status_id INNER JOIN orders_total as ot1 ON orders.orders_id = ot1.orders_id INNER JOIN ( SELECT orders_id, value AS ot_shipping FROM orders_total WHERE class=\'ot_shipping\' ) AS ot2 ON ot1.orders_id=ot2.orders_id INNER JOIN ( SELECT orders_id, value AS ot_total FROM orders_total WHERE class=\'ot_total\' ) AS ot3 ON ot1.orders_id=ot3.orders_id WHERE ot1.class=\'ot_subtotal\' and orders.date_purchased >= '. $dateSince; break;
adam67 Posted August 20, 2011 Posted August 20, 2011 Hi I have zen cart 1.3.9 with the apsona add on, did this final fix actually work for you? Did you have to alter any files other than apsona_functions? Regards Adam
Recommended Posts
Archived
This topic is now archived and is closed to further replies.