function queryData($table_type, $sp, $start_date, $end_date)
{
    global $product_categories_name, $class_pb;
    switch ($table_type) {
        case REPORT_TABLE_TYPE_CATEGORY:
            $join_column_alias = $product_categories_name;
            $join_column = 'ptc.categories_id';
            $join_table = ' LEFT JOIN products_to_categories ptc' . ' ON ptc.products_id = rc.products_id';
            $group_column = 'categories_id';
            break;
        case REPORT_TABLE_TYPE_PRICEGROUP:
            $bgst = Product::getOldProductFunction()->retrieveBestGoodSettings();
            $join_column_alias = array();
            $join_column_alias['PG1'] = "{$bgst['pg1_bgn']} - {$bgst['pg1_end']}";
            $join_column_alias['PG2'] = "{$bgst['pg2_bgn']} - {$bgst['pg2_end']}";
            $join_column_alias['PG3'] = "{$bgst['pg3_bgn']} - {$bgst['pg3_end']}";
            $join_column_alias['PG4'] = "{$bgst['pg4_bgn']} - {$bgst['pg4_end']}";
            $join_column_alias['PG5'] = "{$bgst['pg5_bgn']} - {$bgst['pg5_end']}";
            $join_column_alias['PG6'] = "{$bgst['pg6_bgn']} - {$bgst['pg6_end']}";
            $join_column_alias['PG7'] = "{$bgst['pg7_bgn']} - {$bgst['pg7_end']}";
            $join_column_alias['PG8'] = "{$bgst['pg8_bgn']} - {$bgst['pg8_end']}";
            $join_column = " CASE";
            $join_column .= buildQueryPriceGroup('PG1', $bgst['pg1_bgn'], $bgst['pg1_end']);
            $join_column .= buildQueryPriceGroup('PG2', $bgst['pg2_bgn'], $bgst['pg2_end']);
            $join_column .= buildQueryPriceGroup('PG3', $bgst['pg3_bgn'], $bgst['pg3_end']);
            $join_column .= buildQueryPriceGroup('PG4', $bgst['pg4_bgn'], $bgst['pg4_end']);
            $join_column .= buildQueryPriceGroup('PG5', $bgst['pg5_bgn'], $bgst['pg5_end']);
            $join_column .= buildQueryPriceGroup('PG6', $bgst['pg6_bgn'], $bgst['pg6_end']);
            $join_column .= buildQueryPriceGroup('PG7', $bgst['pg7_bgn'], $bgst['pg7_end']);
            $join_column .= buildQueryPriceGroup('PG8', $bgst['pg8_bgn'], $bgst['pg8_end']);
            $join_column .= " END as price_group";
            $join_table = '';
            $group_column = 'price_group';
            break;
        case REPORT_TABLE_TYPE_BRAND:
            $brands = $class_pb->retrieveList();
            $join_column_alias = array();
            foreach ($brands as $bid => $b) {
                $join_column_alias[$bid] = $b['brand_name'];
            }
            $join_column = 'p.products_brand_id';
            $join_table .= ' LEFT JOIN products p' . ' ON p.products_id = rc.products_id';
            $group_column = 'products_brand_id';
            break;
    }
    $sp_filter = $sp == 'allsp' ? ' 1 = 1' : " jng_sp_id = '{$sp}'";
    $start_date = date('Y-m-d 00:00:00', strtotime($start_date));
    $end_date = date('Y-m-d 23:59:59', strtotime($end_date));
    //TODO: Move this as af unction in class ReportCOGS
    $query = "SELECT {$join_column}" . ", COUNT(DISTINCT rc.products_id) AS products_total" . ", SUM(rc.quantity_sold) AS quantity_sold_total" . ", SUM(rc.quantity_return) AS quantity_return_total" . ", SUM(rc.quantity) AS quantity_total" . ", SUM(rc.price_total) AS price_total" . ", SUM(rc.quantity * rc.matexp) AS matexp_total" . ", SUM(rc.quantity * rc.cogs) AS cogs_total" . ", SUM(rc.net_sales_total) AS net_sales_total" . ", SUM(rc.gross_profit_total) AS gross_profit_total" . " FROM report_cogs rc" . " {$join_table}" . " WHERE " . $sp_filter . " AND base_date >= '{$start_date}' AND base_date <= '{$end_date}'" . " GROUP BY {$group_column}";
    $r = tep_db_query($query);
    $total_cogs = 0;
    $total_sold = 0;
    $total_return = 0;
    $total_quantity = 0;
    $total_sales = 0;
    $total_price = 0;
    $total_profit = 0;
    $data = array();
    while ($row = tep_db_fetch_array($r)) {
        $profit = Product::calculateProfit($row['net_sales_total'], $row['cogs_total']);
        $margin = Product::calculateMargin($profit, $row['net_sales_total']);
        $return_rate = $row['quantity_return_total'] / $row['quantity_sold_total'] * 100;
        $data_arr = array();
        $data_arr['row_id'] = $join_column_alias[$row[$group_column]];
        $data_arr['total_sold'] = $row['quantity_sold_total'];
        $data_arr['total_return'] = $row['quantity_return_total'];
        //sold values after returns
        $data_arr['sold_pieces'] = $row['quantity_total'];
        $data_arr['products'] = $row['products_total'];
        $data_arr['total_sales'] = $row['net_sales_total'];
        $data_arr['total_sales_ratio'] = 0;
        $data_arr['total_price'] = $row['price_total'];
        $data_arr['avg_price'] = $row['price_total'] / $row['quantity_total'];
        $data_arr['avg_margin'] = $margin;
        $data_arr['total_profit'] = $row['gross_profit_total'];
        $data_arr['total_profit_ratio'] = 0;
        $data_arr['return_rate'] = $return_rate;
        $data[$row[$group_column]] = $data_arr;
        $total_cogs += $row['cogs_total'];
        $total_sold += $data_arr['total_sold'];
        $total_return += $data_arr['total_return'];
        $total_quantity += $data_arr['sold_pieces'];
        $total_sales += $data_arr['total_sales'];
        $total_price += $data_arr['total_price'];
        $total_profit += $data_arr['total_profit'];
    }
    //Fill in ratio values
    foreach ($data as $key => $array) {
        $data[$key]['total_sales_ratio'] = $array['total_sales'] / $total_sales * 100;
        $data[$key]['total_profit_ratio'] = $array['total_profit'] / $total_profit * 100;
    }
    //Generate total row data
    $margin = Product::calculateMargin($total_profit, $total_sales);
    $return_rate = $total_return / $total_sold * 100;
    $data_arr = array();
    $data_arr['row_id'] = 'TOTAL';
    $data_arr['total_sold'] = $total_sold;
    $data_arr['total_return'] = $total_return;
    $data_arr['sold_pieces'] = $total_quantity;
    $data_arr['products'] = '-';
    $data_arr['total_sales'] = $total_sales;
    $data_arr['total_sales_ratio'] = 100;
    $data_arr['total_price'] = $total_price;
    $data_arr['avg_price'] = $total_price / $total_quantity;
    $data_arr['avg_margin'] = $margin;
    $data_arr['total_profit'] = $total_profit;
    $data_arr['total_profit_ratio'] = 100;
    $data_arr['return_rate'] = $return_rate;
    $data['total'] = $data_arr;
    return $data;
}
     $avpr = $daily_data['total_quantity'] == 0 ? 0 : $daily_data['total_price'] / $daily_data['total_quantity'];
     $margin = Product::calculateMargin($daily_data['total_gross_profit'], $daily_data['total_net_sales']);
     $row = array();
     $row['w080 tac 0'] = $date_display;
     $row['w100 tar 1'] = displayCurrency($currency, $daily_data['total_price']);
     $row['w080 tac 2'] = strval(intval($daily_data['total_quantity']));
     $row['w080 tar 3'] = displayCurrency($currency, $avpr);
     $row['w100 tar 4'] = displayCurrency($currency, $daily_data['total_cogs']);
     $row['w080 tar 5'] = Product::displayMargin($avpr, $margin);
     $row['w100 tar 6'] = displayCurrency($currency, $daily_data['total_gross_profit']);
     $row['w100 tac 7'] = strval($prepayment_count);
     $row['w100 tar 8'] = displayCurrency($currency, $prepayment_value);
     $table[] = $row;
 }
 $avpr = $total_sold == 0 ? 0 : $total_sales / $total_sold;
 $margin = Product::calculateMargin($total_gross_profit, $total_net_sales);
 $total = array();
 $total['w080 tac 0'] = 'TOTAL';
 $total['w100 tar 1'] = displayCurrency($currency, $total_sales);
 $total['w080 tac 2'] = strval($total_sold);
 $total['w080 tar 3'] = displayCurrency($currency, $avpr);
 $total['w100 tar 4'] = displayCurrency($currency, $total_cogs);
 $total['w080 tar 5'] = Product::displayMargin($avpr, $margin);
 $total['w100 tar 6'] = displayCurrency($currency, $total_gross_profit);
 $total['w100 tac 7'] = strval($total_prepayment_count);
 $total['w100 tar 8'] = displayCurrency($currency, $total_prepayment_value);
 $table[] = $total;
 $result = '<div class="red" style="margin-bottom:5px;">* This' . ' table is using the same data from VC Analysis (calculated' . ' daily, based on order date, values are after returns)<br />' . '* Open Prepayment Orders and Value are real time query</div>';
 $result .= tep_draw_table('', $table, false, true);
 echo utf8_encode($result);
 exit;
 /**
  * For handling submitted POST
  * @global array $session_userinfo
  * @param array $post_vars
  */
 public function postProcess($post_vars)
 {
     global $session_userinfo;
     $result = array();
     if ($post_vars['me_action'] == 'SEARCH') {
         $type = $post_vars['type'];
         $jng_sp_id = $post_vars['jng_sp_id'];
         //another strange case, in live datepicker filter is always given as \'datevalue\' instead of 'datevalue'
         //still cant find the root cause (and it's ok in local dev), so for quickwin workaround we simple replace
         //the \' value into ' with below update (sahat 11.02.2014)
         //$post_filters = explode("|", $post_vars['post_filters']);
         $post_filters = explode("|", str_replace('\\\'', "'", $post_vars['post_filters']));
         $post_sorts = explode("|", $post_vars['post_sorts']);
         $filters = array();
         foreach ($post_filters as $f) {
             $f_temp = explode('-', $f);
             $field = $f_temp[0];
             unset($f_temp[0]);
             $value = implode('-', $f_temp);
             //explode to array for input on field $multivalue_fields which allowed multivalue separated by $multivalue_separator
             if (in_array($field, $this->multivalue_fields)) {
                 $mv_temp = explode(current($this->multivalue_separator), $value);
                 if (is_array($mv_temp) && $mv_temp > 0) {
                     foreach ($mv_temp as $mv) {
                         $mv = trim($mv);
                         if (!isset($filters[$field]) || !in_array($mv, $filters[$field])) {
                             $filters[$field][] = $mv;
                         }
                     }
                 } else {
                     $filters[$field][] = $value;
                 }
             } else {
                 $filters[$field][] = $value;
             }
         }
         $sorts = array();
         foreach ($post_sorts as $s) {
             list($field, $value) = explode("-", $s);
             $sorts[$field][] = $value;
         }
         $query = $this->constructQuery($jng_sp_id, $filters, $sorts);
         //die($query);
         $dbq = tep_db_query($query);
         $result = '';
         $products_ids = array();
         $total_stock = 0;
         $total_stock_value = 0;
         $total_margin = 0;
         $total_sold = 0;
         $total_rets = 0;
         $total_sales = 0;
         $total_vc = 0;
         $total_sold_l30d = 0;
         $total_sold_l60d = 0;
         $total_sold_l90d = 0;
         $total_sales_l30d = 0;
         $total_sales_l60d = 0;
         $total_sales_l90d = 0;
         $display_spot = '';
         while ($r = tep_db_fetch_array($dbq)) {
             $products_ids[] = $r['products_id'];
             $total_stock += $r['hh_stock'];
             $total_stock_value += $r['hh_stock_value'];
             $total_margin += $r['margin'];
             $total_sold += $r['total_sold'];
             $total_rets += $r['total_returned'];
             $total_sales += $r['total_sales'];
             $vc += $r['vc_now'];
             //$total_vc += $r['total_vc'];
             $total_vc += $r['vc_total'];
             $total_sold_l30d += $r['sold_monthly_1'];
             $total_sold_l60d += $r['sold_monthly_2'];
             $total_sold_l90d += $r['sold_monthly_3'];
             $total_sales_l30d += $r['sales_monthly_1'];
             $total_sales_l60d += $r['sales_monthly_2'];
             $total_sales_l90d += $r['sales_monthly_3'];
         }
         $jng_nodata = '<span class="notice">N/A&nbsp;for&nbsp;J&amp;G</span>';
         $returned_rate = number_format($total_sold > 0 ? $total_rets / $total_sold * 100 : 0, 1);
         $n_products_ids = count($products_ids);
         $result['type'] = $type;
         $result['spid'] = $jng_sp_id;
         $result['avg_margin'] = ($n_products_ids > 0 ? number_format($total_margin / $n_products_ids, 1) : 0) . '%';
         $result['total'] = $n_products_ids;
         $result['total_stock'] = $total_stock;
         $result['total_stock_value'] = displayCurrency(CURRENCY_DEFAULT, convertCurrency($total_stock_value, CURRENCY_CODE_EURO, CURRENCY_DEFAULT));
         $result['total_sold'] = $jng_sp_id == '0' ? $jng_nodata : $total_sold;
         $result['total_rets'] = $jng_sp_id == '0' ? $jng_nodata : $total_rets . " ({$returned_rate}%)";
         $result['total_sales'] = $jng_sp_id == '0' ? $jng_nodata : displayCurrency(DEFAULT_CURRENCY, $total_sales);
         $result['vc'] = $jng_sp_id == '0' ? $jng_nodata : number_format($vc, 2);
         //$result['total_vc'] = number_format($total_vc, 2);
         $result['vc_total'] = $jng_sp_id == '0' ? $jng_nodata : number_format($total_vc, 2);
         $result['total_sales_l30d'] = displayCurrency(DEFAULT_CURRENCY, $total_sales_l30d);
         $result['total_sales_l60d'] = displayCurrency(DEFAULT_CURRENCY, $total_sales_l60d);
         $result['total_sales_l90d'] = displayCurrency(DEFAULT_CURRENCY, $total_sales_l90d);
         $result['total_sold_l30d'] = $total_sold_l30d;
         $result['total_sold_l60d'] = $total_sold_l60d;
         $result['total_sold_l90d'] = $total_sold_l90d;
         $result['products_ids'] = implode(',', $products_ids);
     } elseif ($post_vars['me_action'] == 'DISPLAY') {
         use_class('products_minierp');
         use_class('minierp_users');
         use_class('Product');
         $class_pm = new products_minierp();
         $class_mu = new minierp_users();
         $jng_sp_id = $post_vars['jng_sp_id'];
         $type = $post_vars['type'];
         $products_id = $post_vars['products_id'];
         $is_killed = $post_vars['is_killed'];
         $is_sp_not_yet_added = $post_vars['is_sp_not_yet_added'];
         $added_post_vars = array('is_killed' => $is_killed, 'is_sp_not_yet_added' => $is_sp_not_yet_added);
         $display_type = $post_vars['display_type'];
         $row_number = $post_vars['row_number'];
         $showing_id = $post_vars['showing_id'];
         $obj_product = new Product($products_id);
         $display_res = $this->retrieveDataForDisplay($jng_sp_id, $products_id, $added_post_vars, $display_type);
         $res = $display_res[0];
         $img_size = $display_type == 'thumbnail' ? IMAGE_SIZE_THUMBNAIL_2 : IMAGE_SIZE_THUMBNAIL_1;
         //SHOW PURE IMAGE FOR SP THAT USED IT AS MAIN IMAGE
         if (in_array($jng_sp_id, $this->sp_use_pure_image)) {
             $obj_product->setDisplayImage(Product::IMAGE_TYPE_ZALANDO);
         } else {
             if (in_array($jng_sp_id, $this->sp_use_clear_image)) {
                 $obj_product->setDisplayImage(Product::IMAGE_TYPE_AMAZON);
             }
         }
         //$products_image = webImageWithDetailLinkStars($image_used, $img_size, $img_size, 'Product '.$products_id, 'img-border', 'View Larger Image', IMAGE_SIZE_BIG_1, '', $res['stars'], 3, '', '', $res['products_brand_id']);
         $products_image = $obj_product->displayImage($img_size, $img_size);
         $position_padding = $display_type == 'thumbnail' ? '2px 0 0 15px;' : '';
         $products_image = webImageAddPosition($row_number, $products_image, $position_padding);
         $products_complexity = productComplexityName($res['complexity']);
         $products_stars = drawStars($res['stars']);
         $products_is_watched = $class_mu->isOnWatchlist($session_userinfo['id'], $products_id);
         $resultsdt = array();
         if ($display_type == 'table' || $type == 'displaytooltip') {
             if ($type == 'thumbnail_tooltip') {
                 //HTML Version result
                 $sales_partner_id = $jng_sp_id == 'allsp' ? '0' : $jng_sp_id;
                 //TODO: seperate B2C and B2b calculation, or even better is if
                 //      we can calculate it by the object directly
                 $price = $obj_product->getPriceSelling($sales_partner_id);
                 $net_price = Product::calculateNetPrice($price, VAT);
                 $cogs = $obj_product->getProductCOGSValue();
                 $profit = Product::calculateProfit($net_price, $cogs);
                 $margin = Product::calculateMargin($profit, $net_price);
                 $jng_nodata = '<span class="notice">N/A for J&amp;G</span>';
                 $infos = array();
                 $infos[] = $obj_product->displayIDAndCode();
                 $infos[] = $res['products_name'];
                 if ($res['hh_stock'] > 0) {
                     $infos[] = '<strong class="green">Depot Stock = ' . $res['hh_stock'] . '</strong>';
                 } else {
                     $infos[] = '<strong>Depot Stock = 0</strong>';
                 }
                 $infos[] = 'Complexity = ' . $products_complexity;
                 if ($res['sample_created'] == '1') {
                     $infos[] = '<span class="green">Sample order created</span>';
                 } else {
                     $infos[] = '<span class="notice">No sample order created</span>';
                 }
                 if ($jng_sp_id == '0') {
                     $total_sold = $jng_nodata;
                     $return_info = $jng_nodata;
                     $qty_sold = $jng_nodata;
                 } else {
                     $returned_rate = number_format($res['returned_rate'], 1);
                     $sales_monthly = number_format($res['sales_monthly'], 1);
                     $sales_weekly = number_format($res['sales_weekly'], 1);
                     $total_sold = $res['total_sold'];
                     $return_info = $res['total_returned'] . ' (' . $returned_rate . '%)';
                     $qty_sold = "per Week: {$sales_weekly} &sdot; per Month: {$sales_monthly}";
                 }
                 $tooltip = '<div class="draw-table"><table border="0" cellpadding="0" cellspacing="0">';
                 $tooltip .= '<tr class="o"><td class="w200 bold">Brand</td><td class="w300 bold">' . $res['brand_name'] . '</td></tr>';
                 $tooltip .= '<tr class="e"><td>Info</td><td>&bull; ' . implode('<br />&bull; ', $infos) . '</td></tr>';
                 $tooltip .= '<tr class="o"><td>Price Preview</td><td>' . $obj_product->displayPricePreview($sales_partner_id) . '</td></tr>';
                 $tooltip .= '<tr class="e"><td>Margin in %</td><td>' . $obj_product->displayMargin($price, $margin) . '</td></tr>';
                 $tooltip .= '<tr class="o"><td>Age JG (finalized)</td><td>' . $res['jng_products_age'] . ' days</td></tr>';
                 $tooltip .= '<tr class="e"><td>Age (selected SP filter)</td><td>' . $res['products_age'] . ' days</td></tr>';
                 $tooltip .= '<tr class="o"><td>Total Sold (excl. JG)</td><td>' . $total_sold . '</td></tr>';
                 $tooltip .= '<tr class="e"><td>Total Returns (excl. JG)</td><td>' . $return_info . '</td></tr>';
                 $tooltip .= '<tr class="o"><td>Pieces Sold</td><td>' . str_replace('align="center"', 'align="left"', $obj_product->displayRecentSoldTable()) . '</div></td></tr>';
                 $tooltip .= '<tr class="e"><td>Qty. Sold (excl. JG)</td><td>' . $qty_sold . '</td></tr>';
                 $tooltip .= '</table></div>';
                 $ajaxResult = array();
                 $ajaxResult['type'] = $type;
                 $ajaxResult['spid'] = $jng_sp_id;
                 $ajaxResult['products_id'] = $products_id;
                 $ajaxResult['tooltip'] = $tooltip;
                 $ajaxResult['rownumber'] = $row_number;
                 $ajaxResult['showingid'] = $showing_id;
                 ajaxReturn($ajaxResult);
             } else {
                 $sales_partner_id = $jng_sp_id == 'allsp' ? '0' : $jng_sp_id;
                 $price = $obj_product->getPriceSelling($sales_partner_id);
                 $net_price = Product::calculateNetPrice($price, VAT);
                 $cogs = $obj_product->getProductCOGSValue();
                 $profit = Product::calculateProfit($net_price, $cogs);
                 $margin = Product::calculateMargin($profit, $net_price);
                 $material_expenses = displayCurrency(DEFAULT_CURRENCY, convertCurrency($res['material_expenses'], 'EUR', DEFAULT_CURRENCY), false);
                 $products_price = displayCurrency(DEFAULT_CURRENCY, $res['products_price'], false);
                 $products_price_old = displayCurrency(DEFAULT_CURRENCY, $res['products_price_old'], false);
                 $products_created = date('d-M-Y', strtotime($res['products_date_added']));
                 $vc_now = number_format($res['vc_now'], 2);
                 $total_vc = number_format($res['vc_total'], 2);
                 $vc_percent = number_format($res['vc_percent'], 1);
                 //$margin = $res['margin'];
                 //$margin = '<span class="'.(($class_pm->priceMargindIsGood($res['products_price'], $margin)) ? 'green' : 'red') . '">' .
                 //    number_format($margin, 1) . '%</span>';
                 $margin = $obj_product->displayMargin($price, $margin);
                 $returned_rate = number_format($res['returned_rate'], 1);
                 $discount = number_format($res['products_discount'], 2);
                 $total_sales = number_format($res['total_sales'], 2);
                 $sales_monthly = number_format($res['sales_monthly'], 1);
                 $sales_weekly = number_format($res['sales_weekly'], 1);
                 $jng_nodata = '<span class="notice">N/A' . ($type == 'displaytooltip' ? '&nbsp;' : '<br />') . 'for&nbsp;J&amp;G</span>';
                 //0-4
                 $resultsdt[] = $products_image;
                 $resultsdt[] = $products_id;
                 $resultsdt[] = $res['products_model'];
                 $resultsdt[] = $res['products_name'];
                 //$resultsdt[] = $class_pm->displayProductName($products_id);
                 $resultsdt[] = $products_price_old;
                 //5-9
                 $resultsdt[] = $products_price;
                 $resultsdt[] = $discount;
                 $resultsdt[] = $jng_sp_id == '0' ? $jng_nodata : $vc_now;
                 $resultsdt[] = $jng_sp_id == '0' ? $jng_nodata : $vc_percent;
                 $resultsdt[] = $margin;
                 //10-14
                 $resultsdt[] = $res['products_age'];
                 $resultsdt[] = $jng_sp_id == '0' ? $jng_nodata : $res['total_sold'];
                 $resultsdt[] = $jng_sp_id == '0' ? $jng_nodata : $total_vc;
                 $resultsdt[] = $res['total_returned'];
                 $resultsdt[] = $jng_sp_id == '0' ? $jng_nodata : $returned_rate;
                 //15-19
                 $resultsdt[] = $jng_sp_id == '0' ? $jng_nodata : $total_sales;
                 $resultsdt[] = $res['sold_monthly_1'];
                 $resultsdt[] = $res['sold_monthly_2'];
                 $resultsdt[] = $res['sold_monthly_3'];
                 $resultsdt[] = $jng_sp_id == '0' ? $jng_nodata : $sales_weekly;
                 //20-24
                 $resultsdt[] = $jng_sp_id == '0' ? $jng_nodata : $sales_monthly;
                 $resultsdt[] = $res['brand_name'];
                 $resultsdt[] = $res['kill_status'];
                 $resultsdt[] = $products_created;
                 $resultsdt[] = $res['hh_stock'];
                 //25-28
                 $resultsdt[] = $products_complexity;
                 $resultsdt[] = $products_stars;
                 $resultsdt[] = $products_is_watched;
                 $resultsdt[] = $res['sample_created'];
                 //29
                 $resultsdt[] = $res['jng_products_age'];
                 $resultsdt[] = $material_expenses;
                 $resultsdt[] = $res['products_length'];
             }
         } elseif ($display_type == 'thumbnail') {
             //0-4
             $resultsdt[] = $products_image;
             $resultsdt[] = $products_id;
             $resultsdt[] = $res['products_model'];
             $resultsdt[] = $products_stars;
             $resultsdt[] = $products_is_watched;
             //5
             $resultsdt[] = $res['sample_created'];
         }
         $result['type'] = $type;
         $result['spid'] = $jng_sp_id;
         $result['result'] = implode($this->result_separator, $resultsdt);
         $result['rownumber'] = $row_number;
         $result['showingid'] = $showing_id;
     }
     ajaxReturn($result);
 }
     $margin = Product::calculateMargin($profit, $net_price);
     $premium = Product::calculatePremium($price_selling, $cogs);
 } else {
     if ($sp_type == 'B2B-ND') {
         $factor = Product::calculateFactor($price_uvp, $price_selling);
         $discount = 0;
         $net_price = $price_selling;
         $profit = Product::calculateProfit($net_price, $cogs);
         $margin = Product::calculateMargin($profit, $net_price);
         $premium = Product::calculatePremium($price_selling, $cogs);
     } else {
         $factor = Product::calculateFactor($price_uvp, $price_selling);
         $discount = Product::calculateDiscount($b2b_price_selling_old, $price_selling);
         $net_price = $price_selling;
         $profit = Product::calculateProfit($net_price, $cogs);
         $margin = Product::calculateMargin($profit, $net_price);
         $premium = Product::calculatePremium($price_selling, $cogs);
     }
 }
 $margin_text = Product::displayMargin($price_selling, $margin);
 $result = array();
 $result['target_id'] = $target_id;
 $result['column_count'] = $column_count;
 $result['factor'] = $factor == 0 ? '-' : $factor;
 $result['discount'] = $discount == 0 ? '-' : $discount . '%';
 $result['cogs'] = $cogs;
 $result['profit'] = $profit;
 $result['margin'] = $margin_text;
 $result['premium'] = $premium;
 ajaxReturn($result);
 exit;
 function updateMargin($products_id)
 {
     use_class('Product');
     $product = new Product($products_id);
     $price = $product->getPriceDefault();
     $net_price = Product::calculateNetPrice($price, VAT);
     $cogs = $product->getProductCOGSValue();
     $profit = Product::calculateProfit($net_price, $cogs);
     $margin = Product::calculateMargin($profit, $net_price);
     $margin_is_good = $this->priceMargindIsGood($price, $margin) ? '1' : '0';
     $sda = array();
     $sda['price_margin'] = $margin;
     $sda['price_margin_is_good'] = $margin_is_good;
     tep_db_perform('products', $sda, 'update', "products_id={$products_id}");
 }