<?php

ini_set('display_errors', 1);

//global varable
require_once(__DIR__ . '/../../../models/sql/ConnectionProperty.class.php');
// https://github.com/freelanceniek/redbeanx
require_once(__DIR__ . '/../_library/redBean/rb.php');
require_once(__DIR__ . '/../_library/newUplode/uploadfunctions.php');
require_once(__DIR__ . '/../_library/newUplode/uploadImages.php');
require_once(__DIR__ . '/../_library/httpCall/http_call_new.php');


include_once(__DIR__ . '/../_library/php-jwt-master/src/BeforeValidException.php');
include_once(__DIR__ . '/../_library/php-jwt-master/src/ExpiredException.php');
include_once(__DIR__ . '/../_library/php-jwt-master/src/SignatureInvalidException.php');
include_once(__DIR__ . '/../_library/php-jwt-master/src/JWT.php');


use \Firebase\JWT\JWT;
use \Firebase\JWT\ExpiredException;
use \Firebase\JWT\SignatureInvalidException;


$hosturl = get_base_url();
$hosturl_f = get_base_url() . explode('/controllers', $_SERVER['REQUEST_URI'])[0];
define("Host_URL", $hosturl);

$dbhostrb = ConnectionProperty::getHost();
$dbuserrb = ConnectionProperty::getUser();
$dbpasswordrb = ConnectionProperty::getPassword();
$dbnamerb = ConnectionProperty::getDatabase();

R::setup('mysql:host=' . $dbhostrb . ';dbname=' . $dbnamerb . '', '' . $dbuserrb . '', '' . $dbpasswordrb . '');
R::exec('SET NAMES latin1');
//so I can use underscore in table's name
R::ext('xdispense', function ($type) {
    return R::getRedBean()->dispense($type);
});


$rb = R::getRedBean();
if (!isset($rb->idFieldMap)) {
    $rb->idFieldMap = [];
}

$rb->idFieldMap = array_merge($rb->idFieldMap, [
    'accountmovement' => 'accountmovementid',
    'accountmovementkind' => 'accountmovementkindid',
    'assets' => 'assetId',
    'assetscat' => 'assetscatid',
    'assetssale' => 'assetssaleid',
    'bank' => 'bankid',
    'bankaccount' => 'accountid',
    'billname' => 'billnameid',
    'billproperty' => 'billpropertyid',
    'billsettings' => 'billsettingsid',
    'branch' => 'branchId',
    'buyandruternbill' => 'buybillid',
    'buyandruternbilldetail' => 'buybilldetailid',
    'buybill' => 'buybillid',
    'buybilldetail' => 'buybilldetailid',
    'buybilldetailoffer' => 'buybilldetailid',
    'buybilloffer' => 'buybillid',
    'capital' => 'capitalid',
    'cashtransfer' => 'cashtransferid',
    'checkcontroller' => 'checkControllerid',
    'checkdeposit' => 'checkdepositid',
    'checkwithdrawal' => 'checkwithdrawalid',
    'client' => 'clientid',
    'clientdebtchange' => 'clientdebtchangeid',
    'damagedproduct' => 'damagedProductId',
    'datedchecked' => 'datedCheckedID',
    'employee' => 'employeeId',
    'employeepersonnel' => 'employeepersonnelid',
    'expenses' => 'expensesid',
    'expensestype' => 'expensestypeid',
    'faidaproduct' => 'faida_prod_id',
    'goverarea' => 'goverareaid',
    'government' => 'governmetid',
    'impotencetreatmentsuppliersave' => 'impotencetreatmentsuppliersaveid',
    'income' => 'incomeId',
    'incometype' => 'incomeTypeId',
    'm_check' => 'checkId',
    'm_externalmaintenance' => 'externalmaintenanceId',
    'm_finishingpayed' => 'finishingPayedId',
    'm_handedreport' => 'handedReportId',
    'm_initialmaintenance' => 'initialMaintenanceId',
    'm_initialmaintenancedetail' => 'initialMaintenanceDetailId',
    'm_insurance' => 'insuranceId',
    'm_maintenancereceipt' => 'maintenanceReceiptId',
    'm_maintenancereceiptfinish' => 'maintenanceReceiptFinishId',
    'm_producthistory' => 'productHistoryId',
    'm_stageproperity' => 'stageProperityId',
    'manufacuresetting' => 'manufacuresettingid',
    'menuurl' => 'menuurlid',
    'movementmanage' => 'movementmanageid',
    'newdbbackup' => 'backupid',
    'newdbname' => 'newdbnameid',
    'notics' => 'noticesid',
    'onlinetempstorereport' => 'storereportid',
    'partner' => 'partnerid',
    'partnertransferbetween' => 'partnertransferbetweenid',
    'partnerwithdrawal' => 'partnerwithdrawalid',
    'policy' => 'policyId',
    'premiuminstallments' => 'id_installment',
    'product' => 'productId',
    'productcat' => 'productCatId',
    'productcatunit' => 'productcatunitid',
    'productionorder' => 'productionOrderid',
    'productionrate' => 'productionRateId',
    'productionrateproduct' => 'productionRateProductId',
    'productrecieve' => 'productrecieveid',
    'productserial' => 'productserailid',
    'productsetting' => 'productsettingid',
    'producttemp' => 'producttempid',
    'producttempunit' => 'producttempunitid',
    'productunit' => 'productunitid',
    'programsettings' => 'programsettingsid',
    'projectkind' => 'projectkindid',
    'properties' => 'propertyid',
    'relusergroupproperties' => 'relusergrouppropertiesid',
    'representativecompany' => 'representativecompanyId',
    'returnbuybill' => 'returnbuybillid',
    'returnbuybilldetail' => 'returnbuybilldetailid',
    'returnsellbill' => 'returnsellbillid',
    'returnsellbilldetail' => 'returnsellbilldetailid',
    'salaryreport' => 'salaryreportid',
    'save' => 'saveid',
    'saveclose' => 'savecloseid',
    'savedaily' => 'savedailyid',
    'sellandruternbilldetail' => 'sellbilldetailid',
    'sellbill' => 'sellbillid',
    'sellbillandrutern' => 'sellbillid',
    'sellbilldetail' => 'sellbilldetailid',
    'sellbilldetailoffer' => 'sellbilldetailid',
    'sellbilldetailorder' => 'sellbilldetailid',
    'sellbilloffer' => 'sellbillid',
    'sellbillorder' => 'sellbillid',
    'settingoperation' => 'settingOperationID',
    'shippercompanies' => 'companyId',
    'soldserialproduct' => 'soldserialproductid',
    'sparepart' => 'sparepartid',
    'sparepartstore' => 'sparepartstoreId',
    'sparepartstoredetails' => 'sparepartstoredetailid',
    'sparepartstorereport' => 'sparepartstorereportid',
    'stage' => 'stageId',
    'stagestep' => 'stagestepId',
    'store' => 'storeId',
    'storedetail' => 'storedetailid',
    'storemovement' => 'transferproductid',
    'storereport' => 'storereportid',
    'supplier' => 'supplierid',
    'supplierdebtchange' => 'supplierdebtchangeid',
    'supplieropendebtchange' => 'supplieropendebtchangeid',
    'temreturnsellbill' => 'returnsellbillid',
    'temreturnsellbilldetail' => 'returnsellbilldetailid',
    'temsellandruternbilldetail' => 'sellbilldetailid',
    'temsellbill' => 'sellbillid',
    'temsellbillandrutern' => 'sellbillid',
    'temsellbilldetail' => 'sellbilldetailid',
    'transferclientaccounts' => 'transferid',
    'transfermoney' => 'transfermoneyid',
    'typeclient' => 'typeId',
    'typesimpotencetreatment' => 'typesimpotencetreatmentid',
    'typesupplier' => 'typeId',
    'unit' => 'unitId',
    'user' => 'userid',
    'usergroup' => 'usergroupid',
    'workstation' => 'workstationid',
    'workstationassets' => 'workstationassetsId',
    'workstationworker' => 'workstationworkerId',
]);

 // R::getRedBean()->idFieldMap= array('client'=>'clientid');
// R::getRedBean()->fkeyFieldMap = array(
//     'source_tablename' => [
//         'dest_tablename' => 'fkey_fieldname'
//     ]
// );


putenv("api_key=s_vHueL7t@GL~ZvLWj9F;Ztqq{K&NC\w");
putenv("api_user=webApi8");
putenv("api_password=ey8yrw4r");
putenv("erpToken=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6IndlYkFwaTgiLCJwYXNzd29yZCI6ImV5OHlydzRyIn0.UyShdlXVpL0u7zV-QJKYqg67JRO0IrJSgaO67rvi0hY");

function get_base_url() {
    if (isset($_SERVER['HTTPS'])) {
        $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
    } else {
        $protocol = 'http';
    }
    return $protocol . "://" . $_SERVER['HTTP_HOST'];
}
