* Portions created by SugarCRM are Copyright (C) 2004-2006 SugarCRM, Inc.;
 * All Rights Reserved.
 * Contributor(s): ______________________________________.
 */
require_once 'XTemplate/xtpl.php';
require_once 'data/Tracker.php';
require_once 'modules/EstimateCalc/EstimateCalc.php';
require_once 'include/time.php';
require_once 'modules/EstimateCalc/Forms.php';
global $timedate;
global $app_strings;
global $app_list_strings;
global $current_language;
global $current_user;
global $sugar_version, $sugar_config;
$focus = new EstimateCalc();
if (!empty($_REQUEST['record'])) {
    $focus->retrieve($_REQUEST['record']);
}
$precalc = null;
if (isset($_REQUEST['precalc']) && !empty($_REQUEST['precalc'])) {
    $precalc = $_REQUEST['precalc'];
}
if (!empty($_REQUEST['estimate_id']) && isset($_REQUEST['estimate_id']) && !is_null($_REQUEST['estimate_id'])) {
    $estimate_id = $_REQUEST['estimate_id'];
    $components_estimate = $focus->componentsEstimate($estimate_id);
} elseif (isset($focus->estimate_id) && !is_null($focus->estimate_id)) {
    $estimate_id = $focus->estimate_id;
    $components_estimate = $focus->componentsEstimate($estimate_id);
} else {
    exit;
 function estimate_update($id, $close = false)
 {
     $estimate_estimate = new EstimateCalc();
     $components_estimate = new ComponentEstimate();
     $component = new EstimateComponents();
     if (close == true) {
         $query = ' UPDATE ' . $estimate_estimate->table_name . ' SET status="closed" WHERE estimate_id="' . $id . '" AND deleted=0 ';
         $this->db->query($query, true, "");
     } else {
         $query = ' SELECT id FROM ' . $estimate_estimate->table_name . ' WHERE estimate_id="' . $id . '" AND deleted=0 ';
         $result = $this->db->query($query, true, "");
         if ($result != false) {
             while ($data = $this->db->fetchByAssoc($result)) {
                 $estimate_estimate->mark_deleted($data['id']);
             }
         }
     }
     $query = ' SELECT id FROM ' . $component->table_name . ' WHERE parent_id="' . $id . '" AND deleted=0 ';
     $result = $this->db->query($query, true, "");
     if ($result != false) {
         if (close == true) {
             $query = ' UPDATE ' . $component->table_name . ' SET status="closed" WHERE parent_id="' . $id . '" AND deleted=0 ';
             $this->db->query($query, true, "");
         }
         while ($data = $this->db->fetchByAssoc($result)) {
             if (close == true) {
                 $query = ' UPDATE ' . $components_estimate->table_name . ' SET status="closed" WHERE component_id="' . $data['id'] . '" AND deleted=0 ';
                 $this->db->query($query, true, "");
             } else {
                 $component->mark_deleted($data['id']);
                 $query = ' SELECT id FROM ' . $components_estimate->table_name . ' WHERE component_id="' . $data['id'] . '" AND deleted=0 ';
                 $result_components_estimate = $this->db->query($query, true, "");
                 if ($result_components_estimate != false) {
                     while ($data = $this->db->fetchByAssoc($result)) {
                         $components_estimate->mark_deleted($data['id']);
                     }
                 }
             }
         }
     }
 }
 function add_quote_estimate($estimate_id, $detailview = false, $pdf = false)
 {
     global $pdfFontSize, $pdfColors;
     if (!is_null($this->id)) {
         $product_name = $this->product_name;
         $product_id = $this->product_id;
         $product_number = $this->product_number;
         $total_paper = $this->total_paper;
         $total_prepress = $this->total_prepress;
         $total_press = $this->total_press;
         $total_operations = $this->total_operations;
         $total_estimate = $this->total_estimate;
         $price = $this->price;
     } else {
     }
     $Estimate = new Estimates();
     $Estimate->retrieve($estimate_id);
     $Product = new Products();
     $Product->retrieve($Estimate->product_id);
     $EstimateCalc = new EstimateCalc();
     $query = ' SELECT id FROM ' . $EstimateCalc->table_name . ' WHERE estimate_id="' . $Estimate->id . '" ';
     $result = $this->db->query($query, true, "Error filling layout fields: ");
     $data = $this->db->fetchByAssoc($result);
     $EstimateCalc->retrieve($data['id']);
     if (!is_null($this->id)) {
         $product_name = $this->product_name;
         $product_id = $this->product_id;
         $product_number = $this->product_number;
         $total_paper = $this->total_paper;
         $total_prepress = $this->total_prepress;
         $total_press = $this->total_press;
         $total_operations = $this->total_operations;
         $total_estimate = $this->total_estimate;
         $price = $this->price;
     } else {
         $product_name = $Product->name;
         $product_id = $Product->id;
         $product_number = $Product->pnum;
         $total_paper = $EstimateCalc->total_paper;
         $total_prepress = $EstimateCalc->total_prepress;
         $total_press = $EstimateCalc->total_press;
         $total_operations = $EstimateCalc->total_operations;
         $total_estimate = $EstimateCalc->total_estimate;
         $price = $EstimateCalc->total_estimate;
     }
     if ($pdf == true) {
         $fSize = $pdfFontSize["default"];
         $fldColor = $pdfColors["field"];
         $tablerow .= "<tr bgcolor={$fldColor}>";
         $tablerow .= "<td><font size={$fSize} >{$product_name}</font></td>";
         $tablerow .= "<td><font size={$fSize}>{$product_number}</font></td>";
         $tablerow .= "<td><font size={$fSize}>{$total_paper}</font></td>";
         $tablerow .= "<td><font size={$fSize}>{$total_prepress}</font></td>";
         $tablerow .= "<td><font size={$fSize}>{$total_press}</font></td>";
         $tablerow .= "<td><font size={$fSize}>{$total_operations}</font></td>";
         $tablerow .= "<td><font size={$fSize}>{$total_estimate}</font></td>";
         $tablerow .= "<td><font size={$fSize}>{$price}</font></td>";
         $tablerow .= "</tr>";
         return $tablerow;
     }
     if ($detailview == false) {
         $tablerow = '';
         $tablerow .= '<TR>';
         $tablerow .= '<TD class=dataField width="10%" ><input style="background:inherit; border-style:none;" type=text readonly value="' . $product_name . '" name="product_name" ><input type=hidden value="' . $product_id . '"  name="product_id" ></TD>';
         $tablerow .= '<TD class=dataField width="10%" ><input style="background:inherit; border-style:none;" type=text readonly value="' . $product_number . '" name="product_number" ></TD>';
         $tablerow .= '<TD class=dataField width="10%" ><input style="background:inherit; border-style:none;" type=text readonly value="' . $total_paper . '" name="total_paper" ></TD>';
         $tablerow .= '<TD class=dataField width="10%" ><input style="background:inherit; border-style:none;" type=text readonly value="' . $total_prepress . '" name="total_prepress" ></TD>';
         $tablerow .= '<TD class=dataField width="10%" ><input style="background:inherit; border-style:none;" type=text readonly value="' . $total_press . '"  name="total_press" ></TD>';
         $tablerow .= '<TD class=dataField width="10%" ><input style="background:inherit; border-style:none;" type=text readonly value="' . $total_operations . '"  name="total_operations" ></TD>';
         $tablerow .= '<TD class=dataField width="10%" ><input style="background:inherit; border-style:none;" type=text readonly value="' . $total_estimate . '"  name="total_estimate" ></TD>';
         $tablerow .= '<TD class=dataField width="30%" ><input type=text size=5 value="' . $price . '"  name="price" ></TD>';
         $tablerow .= '</TR>';
     }
     if ($detailview == true) {
         $tablerow = '';
         $tablerow .= '<TD class=tabDetailViewDF width=10%><a href="index.php?module=Products&action=DetailView&record=' . $product_id . '" class="tabDetailViewDFLink">' . $product_name . '</a></TD>';
         $tablerow .= '<TD class=tabDetailViewDF width=10%>' . $product_number . '</TD>';
         $tablerow .= '<TD class=tabDetailViewDF width=10%>' . $total_paper . '</TD>';
         $tablerow .= '<TD class=tabDetailViewDF width=10%>' . $total_prepress . '</TD>';
         $tablerow .= '<TD class=tabDetailViewDF width=10%>' . $total_press . '</TD>';
         $tablerow .= '<TD class=tabDetailViewDF width=10%>' . $total_operations . '</TD>';
         $tablerow .= '<TD class=tabDetailViewDF width=10%>' . $total_estimate . '</TD>';
         $tablerow .= '<TD class=tabDetailViewDF width=30%>' . $price . '</TD>';
     }
     return $tablerow;
 }
     $componentEstimate->press_rate_a_id = $pressestimate['press_rate'][0]['id'];
     $componentEstimate->press_rate_a_name = $pressestimate['press_rate'][0]['name'];
     $componentEstimate->press_rate_b_id = $pressestimate['press_rate'][1]['id'];
     $componentEstimate->press_rate_b_name = $pressestimate['press_rate'][1]['name'];
     $componentEstimate->press_rate_a_inks = $pressestimate['press_rate'][0]['colors'];
     $componentEstimate->press_rate_a_machine = $pressestimate['press_rate'][0]['machine'];
     $componentEstimate->press_rate_b_inks = $pressestimate['press_rate'][1]['colors'];
     $componentEstimate->press_rate_b_machine = $pressestimate['press_rate'][1]['machine'];
     $componentEstimate->assigned_user_id = $current_user->id;
     $componentEstimate->press_paperwaste_rate_id = $paperestimate['press_paperwaste_rate']['id'];
     $componentEstimate->press_paperwaste_rate_name = $paperestimate['press_paperwaste_rate']['name'];
     $componentEstimate->press_paperwaste_rate_machine = $paperestimate['press_paperwaste_rate']['machine'];
     $componentEstimate->save($GLOBALS['check_notify']);
 }
 ///////////
 $estimateCalc = new EstimateCalc();
 $estimatecalc_id = $focus->get_calc_record($focus->id);
 if (!is_null($estimatecalc_id) && !empty($estimatecalc_id)) {
     $estimateCalc->retrieve($estimatecalc_id);
 }
 $components_estimate = $estimateCalc->componentsEstimate($focus->id);
 $estimateCalc->name = $mod_strings['LBL_EST_NAME_PREFIX'] . "-" . $focus->name;
 $estimateCalc->estimate_name = $focus->name;
 $estimateCalc->estimate_id = $focus->id;
 $estimateCalc->total_paper = $components_estimate['total_paper'];
 $estimateCalc->total_prepress = $components_estimate['total_prepress'];
 $estimateCalc->total_press = $components_estimate['total_press'];
 $estimateCalc->total_operations = $components_estimate['total_operations'];
 $estimateCalc->total_estimate = $components_estimate['total'];
 $estimateCalc->assigned_user_id = $current_user->id;
 $estimateCalc->status = 'uptodate';