<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/*****************************************************************************
 * The contents of this file are subject to the RECIPROCAL PUBLIC LICENSE
 * Version 1.1 ("License"); You may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * http://opensource.org/licenses/rpl.php. Software distributed under the
 * License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
 * either express or implied.
 *
 * You may:
 * a) Use and distribute this code exactly as you received without payment or
 *    a royalty or other fee.
 * b) Create extensions for this code, provided that you make the extensions
 *    publicly available and document your modifications clearly.
 * c) Charge for a fee for warranty or support or for accepting liability
 *    obligations for your customers.
 *
 * You may NOT:
 * a) Charge for the use of the original code or extensions, including in
 *    electronic distribution models, such as ASP (Application Service
 *    Provider).
 * b) Charge for the original source code or your extensions other than a
 *    nominal fee to cover distribution costs where such distribution
 *    involves PHYSICAL media.
 * c) Modify or delete any pre-existing copyright notices, change notices,
 *    or License text in the Licensed Software
 * d) Assert any patent claims against the Licensor or Contributors, or
 *    which would in any way restrict the ability of any third party to use the
 *    Licensed Software.
 *
 * You must:
 * a) Document any modifications you make to this code including the nature of
 *    the change, the authors of the change, and the date of the change.
 * b) Make the source code for any extensions you deploy available via an
 *    Electronic Distribution Mechanism such as FTP or HTTP download.
 * c) Notify the licensor of the availability of source code to your extensions
 *    and include instructions on how to acquire the source code and updates.
 * d) Grant Licensor a world-wide, non-exclusive, royalty-free license to use,
 *    reproduce, perform, modify, sublicense, and distribute your extensions.
 *
 * The Original Code is: CommuniCore
 *                       Olavo Farias
 *                       2006-04-7 olavo.farias@gmail.com
 *
 * The Initial Developer of the Original Code is CommuniCore.
 * Portions created by CommuniCore are Copyright (C) 2005 CommuniCore Ltda
 * All Rights Reserved.
 ********************************************************************************/
  require_once('data/SugarBean.php');
  require_once('include/utils.php');
  
  // SHOULD INCLUDE SELECTIVELY
  
  class EcmPaymentCondition extends SugarBean {
    
    var $field_name_map = array();
    
    // STANDARD FIELDS
    var $id;
    var $date_entered;
    var $date_modified;
    var $modified_user_id;
    var $assigned_user_id;
    var $name;
    
    // TABLE COLUMNS 
    var $days;
    var $discount;
    var $discount_days;
	var $translations;
	var $payment_method;
	var $weight_from;
	var $weight_to;
	var $price;
    
    // OBJECTS
    
    // RELATED FIELDS
    var $created_by;
    var $created_by_name;
    var $modified_by_name;
    var $assigned_user_name;
    
    // SUBPANELS RELATED
    
    // MODULE OBJECT DETAILS 
    var $module_dir        = 'EcmPaymentConditions';
    var $table_name        = "ecmpaymentconditions";
    var $object_name       = "EcmPaymentCondition";
    
    // RELATED TABLE NAMES
    
    
    // USED TO RETRIEVE RELATED FIELDS FROM FORM POSTS.
    var $additional_column_fields = Array(
      'assigned_user_name', 
      'assigned_user_id', 
      'modified_user_id',
      'created_by',
    );
    
    
    
    // RELATIONSHIP FIELDS 
    var $relationship_fields = Array(
    );


   function EcmPaymentCondition() {
      parent::SugarBean();
      $this->setupCustomFields('EcmPaymentConditions');
      foreach($this->field_defs as $field) {
        $this->field_name_map[$field['name']] = $field;
      }
    }
    
    
    var $new_schema = true;
    
    function get_summary_text(){
      return "$this->name";
    }
    
    function create_list_query($order_by, $where, $show_deleted = 0) {

      // Fill in the assigned_user_name
      $custom_join = $this->custom_fields->getJOIN();
      $query   = "SELECT ";
      $query  .= "ecmpaymentconditions.*
                  ,users.user_name as assigned_user_name";
      if($custom_join) {
        $query .= $custom_join['select'];
      }
      $query   .= " FROM ecmpaymentconditions 
                   LEFT JOIN users
                   ON ecmpaymentconditions.assigned_user_id=users.id";
      $query   .= "  ";
      if($custom_join) {
        $query .= $custom_join['join'];
      }
      $where_auto = '1=1';
      if($show_deleted == 0) {
        $where_auto = " $this->table_name.deleted=0 ";
      } else if($show_deleted == 1) {
        $where_auto = " $this->table_name.deleted=1 "; 
      }
      
      if($where != "")
        $query .= "where $where AND ".$where_auto;
      else
        $query .= "where ".$where_auto;
      if(substr_count($order_by, '.') > 0) {
        $query .= " ORDER BY $order_by";
      }
      else if($order_by != "")
        $query .= " ORDER BY $order_by";
      else
        $query .= " ORDER BY ecmpaymentconditions.name";      
      return $query;
      
    }
  
    function create_export_query($order_by, $where) {    
      $custom_join = $this->custom_fields->getJOIN();
      $query       = "SELECT
                      ecmpaymentconditions.*,
                      users.user_name assigned_user_name";
      if($custom_join) {
        $query .=  $custom_join['select'];
      }
      $query   .= " FROM ecmpaymentconditions ";
      $query   .= " LEFT JOIN users
                  ON ecmpaymentconditions.assigned_user_id=users.id";
      if($custom_join) {
        $query .=  $custom_join['join'];
      }
      $query   .= "";
      $where_auto = " ecmpaymentconditions.deleted=0 
                    ";
      if($where != "")
        $query .= " where $where AND ".$where_auto;
      else
        $query .= " where ".$where_auto;  
      if($order_by != "")
        $query .= " ORDER BY $order_by";
      else
        $query .= " ORDER BY ecmpaymentconditions.name";
      return $query;
    }
  
    function fill_in_additional_list_fields() {
      $this->fill_in_additional_detail_fields(); 
    }
  
    function fill_in_additional_detail_fields() {
      // FILL IN THE ASSIGNED_USER_NAME
      $this->assigned_user_name = get_assigned_user_name($this->assigned_user_id);
      $this->created_by_name    = get_assigned_user_name($this->created_by);
      $this->modified_by_name   = get_assigned_user_name($this->modified_user_id);
	  
	  $this->translations = EcmPaymentCondition::decodeData($this->translations);
	  
/*
      if(!isset($this->currency_id) || $this->currency_id == '') {
        $query = "select `currency_id` from `ecmpaymentconditions` where `id`='$this->id' limit 1";
        $r = $this->db->query($query);
        if($r) {
          $r = $this->db->fetchByAssoc($r);
          $this->currency_id = $r['currency_id'];
        }
      }

      require_once('modules/Currencies/Currency.php');
      $this->currency = new Currency();
      $this->currency->retrieve($this->currency_id);

      if($this->purchase_price == 0) $this->purchase_price = format_number(0.0);
      if($this->selling_price == 0) $this->selling_price = format_number(0.0);
*/

    }
  
    function get_list_view_data(){
      global $current_language;
      $app_list_strings  = return_app_list_strings_language($current_language);
      $mod_strings       = return_module_language($current_language, 'EcmPaymentConditions');
      $the_array         = parent::get_list_view_data();
      
      // THE NEW LISTVIEW CODE ONLY FETCHES COLUMNS THAT WE'RE DISPLAYING AND NOT ALL
      // THE COLUMNS SO WE NEED THESE CHECKS. 
      $the_array['NAME']                    = (($this->name == "") ? "<em>blank</em>" : $this->name);
      $the_array['ENCODED_NAME']            = $this->name;
	  $the_array['DISCOUNT'] = format_number($this->discount).' %';
      return  $the_array;
    }

    /**
    BUILDS A GENERIC SEARCH BASED ON THE QUERY STRING USING OR.
    DO NOT INCLUDE ANY $THIS-> BECAUSE THIS IS CALLED ON WITHOUT HAVING THE CLASS INSTANTIATED.
    */
    function build_generic_where_clause ($the_query_string) { var_dump('lala');
      $where_clauses    = Array();
      $the_query_string = PearDatabase::quote(from_html($the_query_string));
      array_push($where_clauses, "ecmpaymentconditions.name like '$the_query_string%'");  
      $the_where = "";
      foreach($where_clauses as $clause){
        if($the_where != "") $the_where .= " or ";
        $the_where .= $clause;
      }
      return $the_where;
    }
    
    function set_notification_body($xtpl, $simplemodule) {
      global $mod_strings, $app_list_strings;
      $xtpl->assign("NAME",        $simplemodule->name);
      $xtpl->assign("ECMPAYMENTCONDITIONS_DESCRIPTION",    $ecmpaymentconditions->description);
      return $xtpl;
    }
   
    function bean_implements($interface) {
      switch($interface){
        case 'ACL':return true;
      }
      return false;
    }

	static function encodeData($data) {
		return base64_encode(serialize($data));
	}

	static function decodeData($data) {
		return unserialize(base64_decode($data));
	}

    function save($check_notify = FALSE) {
	  $this->translations = EcmPaymentCondition::encodeData($this->translations);
      return parent::save($check_notify);
    }

    function ACLAccess($view,$is_owner='not_set') {
		return parent::ACLAccess($view,$is_owner);
	}
  
}
?>