<?php

/**
 * inoERP
 *
 * @copyright   2014 Nishit R. Das
 * @license     https://www.mozilla.org/MPL/2.0/
 * @link        http://inoideas.org
 * @source code https://github.com/inoerp/inoERP
 */

/**
 * org
 * Contains all the org information, such as - org, type, description,  code, etc.
 * 
 */
class org extends dbObject {

 public static $table_name = "org";
 public static $primary_column = "org_id";
 public static $dependent_classes = ['extn_contact'];
 public static $key_column = 'org';
 public static $module = "org"; //same as gl_journal_source
 public static $system_info = [
  'name' => 'Org',
  'number' => '2001',
  'description' => 'Define and mainten Organizations',
  'version' => '0.1.1',
  'db_version' => '1001',
  'mod_version' => '1.1.1',
  'primary_entity_cb' => '',
  'primary_entity_cb' => '',
  'module_name' => 'org',
  'weight' => 1
 ];
 public $field_a = [
  "org_id",
  "org",
  "type",
  "code",
  "description",
  "enterprise_org_id",
  "legal_org_id",
  "business_org_id",
  "inventory_org_id",
  "address_id",
  "ef_id",
  "status",
  "rev_enabled",
  "rev_number",
  "created_by",
  "creation_date",
  "last_update_by",
  "last_update_date"
 ];
 public $initial_search = [
  "org_id",
  "org",
  "code",
  "description",
 ];
 public $column = [
  "org_id",
  "org",
  "type",
  "code",
  "description",
 ];
 public $external = [
  "org_id",
  "org",
  "code",
  "description",
 ];
 public $requiredField = [
  "org",
  "type",
  "code",
  "description",
 ];
 public $search = [
  '_show_update_path' => 1,
  '_update_action_meassge' => 'Update',
  '_show_view_path' => 1,
 ];
 public $pageTitle = " Org - Create & Update Diffent Orgs"; //page Title
 public $option_lists = [
  'org_types' => 'ORG_TYPE'
 ];
 public $org_id;
 public $org;
 public $type;
 public $name;
 public $code;
 public $description;
 public $enterprise_org_id;
 public $legal_org_id;
 public $business_org_id;
 public $inventory_org_id;
 public $address_id;
 public $ef_id;
 public $status;
 public $rev_enabled;
 public $rev_number;
 public $created_by;
 public $creation_date;
 public $last_update_by;
 public $last_update_date;
 private $_parent_org_type;
 private $_parent_org_id;
 private $_child_org_type;
 private $_xxx_org_id;
 private $_xxx_org_type;

 public function _after_save() {
  global $dbc;

  if (!empty($this->type)) {
   $org_type = $this->type;
   switch ($org_type) {
    case "ENTERPRISE":
     if (empty($this->enterprise_org_id)) {
      $sql = " UPDATE `org` SET 
          `enterprise_org_id` = '{$this->org_id}'
          WHERE `org_id` = '{$this->org_id}' ";
     }
     break;

    case "LEGAL_ORG":
     if (empty($this->legal_org_id)) {
      $sql = " UPDATE `org` SET 
          `legal_org_id` = '{$this->org_id}'
          WHERE `org_id` = '{$this->org_id}' ";
     }
     break;

    case "BUSINESS_ORG":
     if (empty($this->business_org_id)) {
      $sql = " UPDATE `org` SET 
          `business_org_id` = '{$this->org_id}'
          WHERE `org_id` = '{$this->org_id}' ";
     }
     break;

    case "INVENTORY_ORG":
     if (empty($this->inventory_org_id)) {
      $sql = " UPDATE `org` SET 
          `inventory_org_id` = '{$this->org_id}'
          WHERE `org_id` = '{$this->org_id}' ";
     }
     break;

    default:
     break;
   }
  }

  if (!empty($sql)) {
   try {
    $dbc->ddlexecute($sql);
   } catch (Exception $e) {
    echo "<br> Failed to update $org_type " . $e->getMessage();
   }
  }
 }
 
 
 private function _create_corresponding_org(){
  
 }

 Public static function org_types() {
  $option_header = option_header::find_by_name('ORG_TYPE');
  $lines = option_line::find_by_option_id($option_header->option_header_id);
  return $lines;
 }

 public function _before_showing() {
  
 }

 public function findBy_orgType() {
  global $db;
  $sql = " SELECT * FROM ";
  $sql .= self::$table_name;
  $sql .= " WHERE type = :type";
  $param_a = ['type'];
  $value_a = ['type' => $this->type];

  $result = $db->findBy_sql($sql, $param_a, $value_a);
  return !empty($result) ? $result : false;
 }

 public static function find_by_orgType($org_type) {
  global $db;
  $db = new dbObject();
  if (empty($_SESSION['user_org_access'])) {
   return false;
  }
  $in_org = "'" . implode("','", array_keys($_SESSION['user_org_access'])) . "'";
  $sql = " SELECT * FROM ";
  $sql .= self::$table_name;
  $sql .= " WHERE type = :type AND org_id IN ({$in_org})";
  $value_a = ['type' => $org_type];

  $result = $db->findBySql($sql, $value_a);
  return !empty($result) ? $result : false;
 }

 public function findAll_enterprise() {
  $this->type = 'ENTERPRISE';
  return $this->findBy_orgType();
 }

 public static function find_all_enterprise() {
  return self::find_by_orgType('ENTERPRISE');
 }

 public function findAll_legal() {
  $this->type = 'LEGAL_ORG';
  return $this->findBy_orgType();
 }

 public static function find_all_legal() {
  return self::find_by_orgType('LEGAL_ORG');
 }

 public function findAll_business() {
  $this->type = 'BUSINESS_ORG';
  return $this->findBy_orgType();
 }

 public static function find_all_business() {
  return self::find_by_orgType('BUSINESS_ORG');
 }

 public function findAll_inventory_o() {
  $this->type = 'INVENTORY_ORG';
  return $this->findBy_orgType();
 }

 public function findAll_inventory() {
  $sql = "  SELECT
 org.org_id,   org.org,   org.type,   org.code,   org.description,
 org.enterprise_org_id,   org.legal_org_id,   org.business_org_id,   org.inventory_org_id,   org.address_id,
 io.type, io.code, io.item_master_cb, io.master_org_id, io.calendar, io.locator_control,
io.allow_negative_balance_cb, io.costing_org, io.costing_method
FROM org LEFT JOIN inventory io ON org.org_id = io.org_id 
WHERE org.type = 'INVENTORY_ORG'
";

  $result = $this->findBy_sql($sql);
  return !empty($result) ? $result : false;
 }

 public static function find_all_inventory() {
  return self::find_by_orgType('INVENTORY_ORG');
 }

 public function findAll_item_master() {
  global $db;
  $sql = " SELECT org.org, org.org_id, org.description, inventory.inventory_id FROM "
   . " org ,inventory "
   . " WHERE inventory.org_id=org.org_id"
   . " AND inventory.item_master_cb = 1"
   . " AND org.type = 'INVENTORY_ORG' ";
  $result = $db->findBy_sql($sql);
  return !empty($result) ? $result : false;
 }

 private function find_childOrgs_ofParentOrg() {
  global $db;
  $sql = " SELECT * FROM ";
  $sql .= self::$table_name;
  $sql .= " WHERE type = :type AND $this->_parent_org_type = :parent_org_id ";
  $param_a = ['type', 'parent_org_id'];
  $value_a = [
   'type' => $this->_child_org_type,
   'parent_org_id' => $this->_parent_org_id
  ];
  $result = $db->findBy_sql($sql, $param_a, $value_a);
  return !empty($result) ? $result : false;
 }

 public function find_legalOrgs_ofEnterprise() {
  $this->_child_org_type = 'LEGAL_ORG';
  $this->_parent_org_type = 'enterprise_org_id';
  $this->_parent_org_id = $this->enterprise_org_id;
  return $this->find_childOrgs_ofParentOrg();
 }

 public function find_businessOrgs_ofLegal() {
  $this->_child_org_type = 'BUSINESS_ORG';
  $this->_parent_org_type = 'enterprise_org_id';
  $this->_parent_org_id = $this->legal_org_id;
  return $this->find_childOrgs_ofParentOrg();
 }

 public function find_inventoryOrgs_ofBusiness() {
  $this->_child_org_type = 'BUSINESS_ORG';
  $this->_parent_org_type = 'enterprise_org_id';
  $this->_parent_org_id = $this->legal_org_id;
  return $this->find_childOrgs_ofParentOrg();
 }

 private function _find_Orgs_byxxxOrgId() {
  global $db;
  $sql = " SELECT * FROM ";
  $sql .= self::$table_name;
  $sql .= " WHERE type = :type AND $this->_xxx_org_type = :xxx_org_id ";
  $param_a = ['xxx_org_id', 'type'];
  $value_a = ['xxx_org_id' => $this->_xxx_org_id, 'type' => $this->type];
  echo ($sql);
  $result = $db->findBy_sql($sql, $param_a, $value_a);
  pa($result);
  return !empty($result) ? $result : false;
 }

 public function findBy_EnterpriseOrgId() {
  $this->type = 'ENTERPRISE';
  $this->_xxx_org_type = 'enterprise_org_id';
  $this->_xxx_org_id = $this->enterprise_org_id;
  return $this->_find_Orgs_byxxxOrgId();
 }

 public function findBy_LegalOrgId() {
  $this->type = 'LEGAL_ORG';
  $this->_xxx_org_type = 'legal_org_id';
  $this->_xxx_org_id = $this->legal_org_id;
  return $this->_find_Orgs_byxxxOrgId();
 }

 public function findBy_BueinssOrgId() {
  $this->type = 'BUSINESS_ORG';
  $this->_xxx_org_type = 'business_org_id';
  $this->_xxx_org_id = $this->business_org_id;
  return $this->_find_Orgs_byxxxOrgId();
 }

 public function findBy_InventoryOrgId() {
  $this->type = 'INVENTORY_ORG';
  $this->_xxx_org_type = 'inventory_org_id';
  $this->_xxx_org_id = $this->inventory_org_id;
  return $this->_find_Orgs_byxxxOrgId();
 }

 public static function find_financial_details_from_orgId($org_id) {
  $sql = " SELECT org.org_id, org.legal_org_id, legal.ledger_id, 
gl.ledger, gl.description as ledger_description, gl.coa_id, gl.calendar_option_line_code, gl.currency_code, legal.balancing_segments,
org.org, org.code,org.type, org.description as org_description,
CASE
 WHEN type='INVENTORY_ORG' THEN org.business_org_id
 WHEN type='BUSINESS_ORG' THEN org.org_id
END AS business_org_id

FROM org,
legal,
gl_ledger gl
WHERE org.legal_org_id = legal.org_id 
AND gl.gl_ledger_id = legal.ledger_id
AND org.org_id = :org_id
 "
  ;
$sql = ino_perPageSql_i($sql, 1);

	  global $db;
  $value_a = ['org_id' => $org_id];
  $result = $db->findBySql($sql, $value_a);

  return !empty($result) ? array_pop($result) : false;
 }

 public static function print_orgDetails_inLine($org_id) {
  $org = org::find_by_id($org_id);
  $org_details = '<h2>' . $org->description . '</h2>';

  if (!empty($org_id->address_id)) {
   $address = address::find_by_id($org_id->address_id);
   $address_value = address::show_adrees_details_inLine($address);
   $org_details .= $address_value;
  }

  $org_type = $org->type;

  switch ($org_type) {

   case "ENTERPRISE":

    break;

   case "LEGAL_ORG":
    $legal = legal::find_by_orgId($org_id);
    $org_details .= "<br/>Registration Number : $legal->registration_number";
    break;

   case "BUSINESS_ORG":
    $legal = legal::find_by_orgId($org->legal_org_id);
    $org_details .= "<br/>Registration Number : $legal->registration_number";
    break;

   case "INVENTORY_ORG":
    break;

   default:
    break;
  }

  return $org_details;
 }

}

$org = new org();
//end of path class
?>