Last active
December 10, 2015 14:18
-
-
Save kevinchampion/4446340 to your computer and use it in GitHub Desktop.
Cron consolidate queries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$last_month = date("n", mktime(0, 0, 0, date("n")-1, 1, date("Y"))); | |
$last_month_year = date("Y", mktime(0, 0, 0, date("n")-1, 1, date("Y"))); | |
// Query for all revenue sources who have an active unit and don't have a | |
// sales tax report for last month. The resulting revenue sources all need | |
// new tax reports created for them for last month. | |
$query = "SELECT | |
n.nid as nid | |
FROM {node} n | |
INNER JOIN {content_field_revenue_unit_ref} r ON r.nid = n.nid | |
INNER JOIN {content_type_tax_reporting_units} u ON u.nid = r.field_revenue_unit_ref_nid | |
INNER JOIN {content_type_tax_sales_report} t ON n.nid = t.field_sales_revenue_ref_nid | |
AND t.field_sales_activity_month_value = %d | |
AND t.field_sales_activity_year_value = %d | |
WHERE n.type = '%s' | |
AND n.status = 1 | |
AND u.field_reporting_unit_status_value = '%s' | |
AND t.nid IS NULL"; | |
/*SELECT | |
n.nid as nid | |
FROM node n | |
INNER JOIN content_field_revenue_unit_ref r ON r.nid = n.nid | |
INNER JOIN content_type_tax_reporting_units u ON u.nid = r.field_revenue_unit_ref_nid | |
LEFT JOIN content_type_tax_sales_report t ON n.nid = t.field_sales_revenue_ref_nid | |
AND t.field_sales_activity_month_value = 12 | |
AND t.field_sales_activity_year_value = 2012 | |
WHERE n.type = 'tax_revenue_source' | |
AND u.field_reporting_unit_status_value = 'A' | |
AND n.status = 1 | |
AND t.nid IS NULL*/ | |
$params = array( | |
$last_month, | |
$last_month_year, | |
"tax_revenue_source", | |
"A" | |
); | |
watchdog('tax_reporting', 'cron query: !period', array('!period' => '<pre>' . print_r($params, TRUE) . '</pre>'), WATCHDOG_NOTICE); | |
$result = db_query($query, $params); | |
// Load each revenue source and create a new report. | |
while ($row = db_fetch_object($result)) { | |
watchdog('tax_reporting', 'cron result: !period', array('!period' => '<pre>' . print_r($row, TRUE) . '</pre>'), WATCHDOG_NOTICE); | |
// TODO: Create queue item using drupal_queue. | |
// Load the node. | |
$node = node_load($row->nid); | |
// Create tax report for this revenue source. | |
$t_node = tax_reporting_create_report_for_source($node, $period); | |
watchdog('tax_reporting', '@title created successfully.', array('@title' => $t_node->title), WATCHDOG_NOTICE); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
n.nid as nid | |
FROM node n | |
INNER JOIN content_field_revenue_unit_ref r ON r.nid = n.nid | |
INNER JOIN content_type_tax_reporting_units u ON u.nid = r.field_revenue_unit_ref_nid | |
LEFT JOIN content_type_tax_sales_report t ON n.nid = t.field_sales_revenue_ref_nid | |
AND t.field_sales_activity_month_value = 12 | |
AND t.field_sales_activity_year_value = 2012 | |
WHERE n.type = 'tax_revenue_source' | |
AND u.field_reporting_unit_status_value = 'A' | |
AND n.status = 1 | |
AND t.nid IS NULL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment