function open_csa_wp_show_total_orders_of_delivery($delivery_id, $producer_id, $page_url)
{
    global $wpdb;
    $producers_map = open_csa_wp_producers_map_array();
    if ($producer_id != null) {
        $producers_involved = $wpdb->get_col($wpdb->prepare("\n\t\t\t\t\t\t\t\tSELECT DISTINCT " . OPEN_CSA_WP_TABLE_PRODUCTS . ".producer \n\t\t\t\t\t\t\t\tFROM " . OPEN_CSA_WP_TABLE_PRODUCTS . " LEFT JOIN " . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . " ON " . OPEN_CSA_WP_TABLE_PRODUCTS . ".id = " . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".product_id\n\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\t" . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".delivery_id = %d AND\n\t\t\t\t\t\t\t\t\t" . OPEN_CSA_WP_TABLE_PRODUCTS . ".producer = %d\n\t\t\t\t\t\t\t", $delivery_id, $producer_id));
    } else {
        $producers_involved = $wpdb->get_col($wpdb->prepare("\n\t\t\t\t\t\t\t\tSELECT DISTINCT " . OPEN_CSA_WP_TABLE_PRODUCTS . ".producer \n\t\t\t\t\t\t\t\tFROM " . OPEN_CSA_WP_TABLE_PRODUCTS . " LEFT JOIN " . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . " ON " . OPEN_CSA_WP_TABLE_PRODUCTS . ".id = " . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".product_id\n\t\t\t\t\t\t\t\tWHERE " . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".delivery_id = %d\n\t\t\t\t\t\t\t\tORDER BY " . OPEN_CSA_WP_TABLE_PRODUCTS . ".producer\n\t\t\t\t\t\t\t", $delivery_id));
    }
    $producers_count = count($producers_involved);
    if ($producer_id != null) {
        echo "<span class='info-text' style='font-size:15px'>Your orders in " . CsaWpPluginGetReadableDeliveryInfo($delivery_id) . " </span>";
    } else {
        echo "<span class='info-text' style='font-size:15px'>Orders of " . CsaWpPluginGetReadableDeliveryInfo($delivery_id) . " </span>";
    }
    if ($producers_count <= 0) {
        if ($producer_id == null) {
            echo "<br/><span class='info-text' style='font-size:15px;color:brown'>" . __('No order has yet been submitted, for this delivery.', OPEN_CSA_WP_DOMAIN) . " </span> <br>";
        } else {
            echo "<br/><span class='info-text' style='font-size:15px;color:brown'>" . __('You have no orders in this delivery.', OPEN_CSA_WP_DOMAIN) . " </span> <br>";
        }
    } else {
        $users_count = $wpdb->get_var($wpdb->prepare("\n\t\t\t\t\t\t\t\t\tSELECT COUNT(DISTINCT user_id)\n\t\t\t\t\t\t\t\t\tFROM " . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . " \n\t\t\t\t\t\t\t\t\tWHERE " . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".delivery_id = %d \n\t\t\t\t\t\t\t\t", $delivery_id));
        $product_details_width = 130;
        $amount_perProduct_width = 30;
        $user_order_width = 58;
        //change also by css (class .left) ???? SHALL WE INCORPORATE THIS? ????
        $product_value_width = 70;
        $main_width = $product_details_width + $amount_perProduct_width + $user_order_width * $users_count + 30;
        // +30 is an offset to accomodate the space of the first orders
        $table_width = $main_width + $product_value_width;
        foreach ($producers_involved as $producer) {
            $revenue = 0;
            //the amount to be paid to this producer
            echo "<p class='panel'> <span style='font-size:14px'> " . __('Producer', OPEN_CSA_WP_DOMAIN) . ": </span> <span class='producer'>" . $producers_map[$producer] . "</span> </p>";
            //Get the products in order for this producer
            $products_in_order = $wpdb->get_results($wpdb->prepare("\n\t\t\t\t\t\t\t\t\t\tSELECT \n\t\t\t\t\t\t\t\t\t\t\t" . OPEN_CSA_WP_TABLE_PRODUCTS . ".name,\n\t\t\t\t\t\t\t\t\t\t\t" . OPEN_CSA_WP_TABLE_PRODUCTS . ".variety,\n\t\t\t\t\t\t\t\t\t\t\t" . OPEN_CSA_WP_TABLE_PRODUCTS . ".current_price_in_euro,\n\t\t\t\t\t\t\t\t\t\t\t" . OPEN_CSA_WP_TABLE_PRODUCTS . ".id,\n\t\t\t\t\t\t\t\t\t\t\t" . OPEN_CSA_WP_TABLE_PRODUCTS . ".measurement_unit, \n\t\t\t\t\t\t\t\t\t\t\tSUM(" . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".quantity) AS total, \n\t\t\t\t\t\t\t\t\t\t\tFORMAT(SUM(" . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".quantity*" . OPEN_CSA_WP_TABLE_PRODUCTS . ".current_price_in_euro),2) as costPerProduct\n\t\t\t\t\t\t\t\t\t\tFROM " . OPEN_CSA_WP_TABLE_PRODUCTS . " LEFT JOIN " . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . " ON " . OPEN_CSA_WP_TABLE_PRODUCTS . ".id = " . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".product_id\n\t\t\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\t\t\t" . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".delivery_id = %d\n\t\t\t\t\t\t\t\t\t\t\tAND " . OPEN_CSA_WP_TABLE_PRODUCTS . ".producer = %d\n\t\t\t\t\t\t\t\t\t\tGROUP BY " . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".product_id \n\t\t\t\t\t\t\t\t\t", $delivery_id, $producer));
            //display product details
            foreach ($products_in_order as $product) {
                $orders_of_product = $wpdb->get_results($wpdb->prepare("\n\t\t\t\t\t\t\t\t\t\t\tSELECT \n\t\t\t\t\t\t\t\t\t\t\t\t" . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".user_id, \n\t\t\t\t\t\t\t\t\t\t\t\t" . OPEN_CSA_WP_TABLE_PRODUCTS . ".name,\n\t\t\t\t\t\t\t\t\t\t\t\t" . OPEN_CSA_WP_TABLE_PRODUCTS . ".variety,\n\t\t\t\t\t\t\t\t\t\t\t\t" . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".quantity\n\t\t\t\t\t\t\t\t\t\t\tFROM " . OPEN_CSA_WP_TABLE_PRODUCTS . " LEFT JOIN " . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . " ON " . OPEN_CSA_WP_TABLE_PRODUCTS . ".id = " . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".product_id\n\t\t\t\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\t\t\t\t" . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".delivery_id = %d AND\n\t\t\t\t\t\t\t\t\t\t\t\t" . OPEN_CSA_WP_TABLE_PRODUCTS . ".id = %d\n\t\t\t\t\t\t\t\t\t\t", $delivery_id, $product->id));
                $total = $product->total;
                if (strpos($total, '.') !== FALSE) {
                    $total = number_format((double) $product->total, 1, '.', '');
                }
                ?>
				<div class='container' style='min-width:100%;width:<?php 
                echo $table_width;
                ?>
px;font-size:14px'>  <!--keeps the quantity and total value divs together-->
					
						<div class='container' style='float:left; width:<?php 
                echo $main_width;
                ?>
px;'> 
							<!--product details-->
							
							<div class='left' style='width:<?php 
                echo $product_details_width;
                ?>
px; background-color:Khaki; display:block; '>
								<div style='display:table-cell; vertical-align:middle; height:70px; padding-left:10px '>
								<?php 
                echo $product->name . " " . $product->variety . "<br/> <span class='info-text'>(" . $product->current_price_in_euro . " € / " . $product->measurement_unit . ")</span>";
                ?>
								</div>
							</div>
							
							<div class='left' style='width:<?php 
                echo $amount_perProduct_width;
                ?>
px;height:70px;line-height:70px;padding-left:10px;font-weight:bold;font-size:16px;background-color:Khaki'>
								<span style='display:inline-block; vertical-align:middle;'><?php 
                echo $total;
                ?>
</span>
							</div>

							<!--order details-->
							<div class="div_tr">
								<?php 
                foreach ($orders_of_product as $product_order) {
                    echo "<div class='left' style='font-weight:bold;'>" . mb_substr(get_user_by('id', $product_order->user_id)->user_login, 0, 7, 'UTF-8') . "</div>";
                }
                ?>
							</div>
								<!--<div style="height:37px;"></div>-->
							<div class="div_tr">
								<?php 
                foreach ($orders_of_product as $preOrder) {
                    echo "<div class='left' >" . $product_order->quantity . "</div>";
                }
                ?>
							</div>
													
						</div>
					
						<!-- price details-->
						<div class='left' style='display:table; height:70px;'>
							<div style='display:table-cell; vertical-align:bottom;'>
							<?php 
                echo "<span class='info-text'>" . $product->costPerProduct . "€</span>";
                ?>
							</div>
						</div>
					
					
				</div>
				
				<?php 
                $revenue += $product->costPerProduct;
            }
            echo "<br/><p class='total' style='margin: -2% 0 3% 0'><span class='emphasis-box'>" . __('Total', OPEN_CSA_WP_DOMAIN) . ": " . $revenue . " € </span></p><br/>";
        }
        ?>
		<table style="margin-top:30px" class='table-straight'> <!-- stucture table (to display the following table next to each other -->
			<tr><td>
				<span style="font-weight:bold;color:green"><?php 
        _e('Cost per consumer', OPEN_CSA_WP_DOMAIN);
        ?>
</span><?php 
        //Show costs in total
        $total_user_costs = $wpdb->get_results($wpdb->prepare("\n\t\t\t\t\t\t\t\t\t\t\t\tSELECT \n\t\t\t\t\t\t\t\t\t\t\t\t\t" . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".user_id,\n\t\t\t\t\t\t\t\t\t\t\t\t\tFORMAT(SUM(" . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".quantity*" . OPEN_CSA_WP_TABLE_PRODUCTS . ".current_price_in_euro),2) AS cost\n\t\t\t\t\t\t\t\t\t\t\t\tFROM " . OPEN_CSA_WP_TABLE_PRODUCTS . " LEFT JOIN " . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . " ON " . OPEN_CSA_WP_TABLE_PRODUCTS . ".id = " . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".product_id\n\t\t\t\t\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\t\t\t\t\t" . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".delivery_id = %d\n\t\t\t\t\t\t\t\t\t\t\t\tGROUP BY " . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".user_id\n\t\t\t\t\t\t\t\t\t\t\t", $delivery_id));
        $total_cost = $wpdb->get_var($wpdb->prepare("\n\t\t\t\t\t\t\t\t\t\t\t\tSELECT FORMAT(SUM(" . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".quantity*" . OPEN_CSA_WP_TABLE_PRODUCTS . ".current_price_in_euro),2)\n\t\t\t\t\t\t\t\t\t\t\t\tFROM " . OPEN_CSA_WP_TABLE_PRODUCTS . " LEFT JOIN " . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . " ON " . OPEN_CSA_WP_TABLE_PRODUCTS . ".id = " . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".product_id\n\t\t\t\t\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\t\t\t\t\t" . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".delivery_id = %d\n\t\t\t\t\t\t\t\t\t\t\t", $delivery_id));
        ?>
				<table style='width:280px;' class='table-bordered'>
					<thead>
						<tr>
							<td><?php 
        _e('Consumer', OPEN_CSA_WP_DOMAIN);
        ?>
</td>
							<td><?php 
        _e('Total Cost', OPEN_CSA_WP_DOMAIN);
        ?>
</td>
						</tr>
					</thead>
					
					<?php 
        foreach ($total_user_costs as $tCost) {
            ?>
						<tr>
							<td style='width:200px;font-weight:bold'><?php 
            echo open_csa_wp_user_readable(get_user_by('id', $tCost->user_id));
            ?>
</td>
							<td><?php 
            echo round($tCost->cost, 1) . " €";
            ?>
</td>
						</tr>
					<?php 
        }
        ?>

					<tr style='background-color:Khaki'>
						<td style="font-weight:bold"><?php 
        _e('Total', OPEN_CSA_WP_DOMAIN);
        ?>
</td>
						<td style="font-weight:bold"><?php 
        echo round($total_cost, 1) . " €";
        ?>
</td>
					</tr>
					</tr>
				</table>
			</td>
			<td> <div style='width:80px'>  </div> </td>
			<td>
				<?php 
        //Show costs per producer
        ?>
<span style="font-weight:bold;color:green;margin-left:5%">Cost per producer</span><?php 
        $producer_costs = $wpdb->get_results($wpdb->prepare("\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tSELECT \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" . OPEN_CSA_WP_TABLE_PRODUCTS . ".producer,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFORMAT(SUM(" . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".quantity*" . OPEN_CSA_WP_TABLE_PRODUCTS . ".current_price_in_euro),2) AS cost\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFROM \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t(" . OPEN_CSA_WP_TABLE_PRODUCTS . " LEFT JOIN " . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . " ON " . OPEN_CSA_WP_TABLE_PRODUCTS . ".id = " . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".product_id)\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" . OPEN_CSA_WP_TABLE_PRODUCT_ORDERS . ".delivery_id = %d\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tGROUP BY " . OPEN_CSA_WP_TABLE_PRODUCTS . ".producer\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t", $delivery_id));
        ?>


				<table style='width:200px;' class='table-bordered'>
					<thead>
						<tr>
							<td><?php 
        _e('Procuder', OPEN_CSA_WP_DOMAIN);
        ?>
</td>
							<td style='width:100px'><?php 
        _e('Income', OPEN_CSA_WP_DOMAIN);
        ?>
</td>
						</tr>
					</thead>
					<?php 
        foreach ($producer_costs as $p_cost) {
            echo "\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<td style='width:200px;font-weight:bold'>" . $producers_map[$p_cost->producer] . "</td>\n\t\t\t\t\t\t\t\t<td>" . round($p_cost->cost, 1) . " €</td>\n\t\t\t\t\t\t\t</tr>";
        }
        ?>

					<tr style='background-color:Khaki'>
						<td style="font-weight:bold"><?php 
        _e('Total', OPEN_CSA_WP_DOMAIN);
        ?>
</td>
						<td style="font-weight:bold"><?php 
        echo round($total_cost, 1) . " €";
        ?>
</td>
					</tr>
				</table>

			</td>
			</tr>
		</table>
		
		<br>
		<table class='table-bordered'>
			<?php 
        $user_orders_comments = $wpdb->get_results($wpdb->prepare("\n\t\t\t\t\t\t\t\t\t\t\tSELECT user_id, comments \n\t\t\t\t\t\t\t\t\t\t\tFROM " . OPEN_CSA_WP_TABLE_USER_ORDERS . " \n\t\t\t\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\t\t\t\tdelivery_id = %d AND\n\t\t\t\t\t\t\t\t\t\t\t\tcomments IS NOT NULL\n\t\t\t\t\t\t\t\t\t\t", $delivery_id));
        if (count($user_orders_comments) > 0) {
            echo "<span style='font-weight:bold;color:green'>Comments:</span>";
            foreach ($user_orders_comments as $user_order_comments) {
                echo "<tr><td style='font-weight:bold;width:200px'>" . open_csa_wp_user_readable_without_login(get_user_by('id', $user_order_comments->user_id)) . "</td><td>" . $user_order_comments->comments . "</td></tr>";
            }
        }
        ?>
		</table>
								
	<?php 
    }
    ?>
	<input 
	type="button"
	class="button button-secondary"
	value='<?php 
    _e('back to orders...', OPEN_CSA_WP_DOMAIN);
    ?>
'
	onclick='window.location.replace(" <?php 
    echo $page_url;
    ?>
 ");'
	/>	

	
<?php 
}
Ejemplo n.º 2
0
function open_csa_wp_producers_map_array()
{
    $producers_map = array();
    $csa_users = get_users();
    foreach ($csa_users as $user) {
        $csa_user_data = get_user_meta($user->ID, 'open-csa-wp_user', true);
        if ($csa_user_data != "" && ($csa_user_data['type'] == "producer" || $csa_user_data['type'] == "both")) {
            $producers_map[$user->ID] = open_csa_wp_user_readable_without_login($user);
        }
    }
    return $producers_map;
}