<?php

$api = new apiPipeDrive();
$data = $api->getAllUsers();
$data = json_decode($data, true);
$user_list_data = array();
$user_list_data = $data["data"];
if (!empty($user_list_data)) {
    unset($fields);
    $fields["is_deleted"] = 1;
    qu("pd_users", $fields, " 1=1 ");
    foreach ($user_list_data as $each_user) {
        d($each_user);
        $user_id = '';
        $user_id = trim($each_user["id"]);
        if ($user_id != '') {
            $check_user_id = array();
            $check_user_id = Call_distribution::CheckMainUserId($user_id);
            echo $check_user_id . "****" . $each_user["name"];
            echo "<br/>";
            unset($fields);
            $fields["name"] = $each_user["name"];
            $fields["email"] = $each_user["email"];
            $fields["phone"] = $each_user["phone"];
            $fields["is_deleted"] = '0';
            if ($each_user['active_flag']) {
                $fields["is_active"] = '1';
            } else {
                $fields["phone"] = '';
                $fields["is_active"] = '0';
            }
<?php

header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$apiPD = new apiPipeDrive();
$deal_source = qs("select pd_source_id from pd_sources where source_name like 'Hot Lead 2'");
$agent_numbers = $apiPD->getAgentByDealSource($deal_source['pd_source_id']);
if (count($agent_numbers) == 0) {
    ?>
    <Response>        
        <Say>Thank you for calling Lysoft dot Com. We are sorry for can't handle your call. Please Try Later.</Say>
    </Response>
<?php 
} else {
    ?>
    <Response>        
        <Say>Thank you for calling Lysoft dot Com.</Say>
        <Say>We are connecting to our agents. Please wait a moment.</Say>
        <Dial>
            <?php 
    foreach ($agent_numbers as $each_agent) {
        ?>
    
                <Number><?php 
        echo $each_agent;
        ?>
</Number>
            <?php 
    }
    ?>
        </Dial>
<?php

//Twilio Website Number: (516) 210-2005
$apiPD = new apiPipeDrive();
$agent_numbers = $apiPD->getAgentByDealSource(37);
$fields['from'] = $_REQUEST['From'];
$fields['CallSid'] = $_REQUEST['CallSid'];
$insertId = qi("website_number_dials", _escapeArray($fields));
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>

    <?php 
if (count($agent_numbers) == 0) {
    ?>
        <Say>Thank you for calling leadpropel.com</Say>
        <Say>Sorry, All agents are busy at the moment.</Say>
        <Say>We will call back later.</Say>
        <Say>Thank you</Say>
    <?php 
} else {
    ?>
        <Dial action="<?php 
    echo _U . "website_number_dial_status?params=" . $insertId . "--00--status";
    ?>
">
            <?php 
    foreach ($agent_numbers as $each) {
        ?>
                <Number url="<?php 
<?php

//_errors_on();
$dealId = "5232";
$apiPD = new apiPipeDrive();
$deal_data = $apiPD->getDealInfo($dealId);
$deal_data = json_decode($deal_data);
$deal_amount = number_format($deal_data->data->value, 2);
$deal_amount = $deal_amount . ' ' . $deal_data->data->currency;
$organization = $deal_data->data->org_name;
$customer_name = $deal_data->data->person_name;
ob_start();
$mail_heading = "Missed Customer Call";
$customer_phone = "919737128291";
include _PATH . "instance/front/tpl/missed_call_mail_template.php";
$content = ob_get_contents();
ob_end_clean();
echo $content;
//_phpmail("*****@*****.**", "Test Mail", "Hi<br><br><br><br>How Are You?");
//_mail("*****@*****.**", "Test Mail", "Hi<br>How Are You?");
echo "Mail Sent";
die;
<?php

$filter_array = array('438' => 'Prospects', '439' => 'Leads', '440' => 'Contact Made', '441' => 'App Out', '442' => 'App In', '443' => 'Submitted', '444' => 'Approved', '445' => 'Contracts Out', '446' => 'Contracts In', '447' => 'Funded');
$start_record_from = 0;
$no_of_records = 100;
$apiPD = new apiPipeDrive();
$source_data = $apiPD->getDealField('12463');
$stage_data = $apiPD->getAllStage();
$source_data = json_decode($source_data, "true");
$stage_data = json_decode($stage_data, "true");
$source = array();
$stage = array();
if (isset($source_data['data']['options'])) {
    foreach ($source_data['data']['options'] as $each_source) {
        $source[$each_source['id']] = $each_source;
        // 'label';
    }
}
if (isset($stage_data['data'])) {
    foreach ($stage_data['data'] as $each_stage) {
        $stage[$each_stage['id']] = $each_stage;
        //'order_nr','name'
    }
}
foreach ($filter_array as $filter_id => $filter_stage_name) {
    $deal_data = $apiPD->getFilterDeals($filter_id, $start_record_from, $no_of_records);
    $deal_data = json_decode($deal_data, "true");
    while ($deal_data['success']) {
        if ($deal_data['data'] && count($deal_data['data']) > 0) {
            foreach ($deal_data['data'] as $each_deal) {
                if ($each_deal['pipeline_id'] != '1') {
<?php

//d($_REQUEST);
$urlArgs = _cg("url_vars");
$agent = qs("select pd_id,name from pd_users where phone = '{$urlArgs[0]}'   ");
$agent_id = $agent['pd_id'];
$agent_name = $agent['name'];
$recording_url = isset($_REQUEST['RecordingUrl']) ? $_REQUEST['RecordingUrl'] : '';
$recording_duration = isset($_REQUEST['RecordingDuration']) ? $_REQUEST['RecordingDuration'] : '0';
//qi('config',array("key"=>$agent_id,"value"=>$urlArgs[1]));
$apiPD = new apiPipeDrive();
//$apiPD->assignDeal($urlArgs[1], $agent_id);
$call_detail_data = q("select * from call_detail where sid='{$_REQUEST['CallSid']}'");
$call_detail_fields['agent_phone'] = $urlArgs[0];
$call_detail_fields['agent_id'] = $agent_id;
$call_detail_fields['agent_name'] = $agent_name;
$call_detail_fields['status'] = isset($_REQUEST['DialCallStatus']) ? $_REQUEST['DialCallStatus'] : '';
$call_detail_fields['recording_duration'] = $recording_duration;
$call_detail_fields['recording_url'] = $recording_url;
$call_detail_fields['customer_phone'] = urlencode($urlArgs[2]);
$call_detail_fields['deal_id'] = $urlArgs[1];
if (count($call_detail_data) > 0) {
    $call_detail_id = $call_detail_data[0]['id'];
    qu('call_detail', $call_detail_fields, "id='{$call_detail_data[0]['id']}'");
} else {
    $call_detail_fields['sid'] = $_REQUEST['CallSid'];
    $call_detail_id = qi('call_detail', $call_detail_fields);
}
$deal_data = json_decode($apiPD->getDealInfo($urlArgs[1]));
//$deal_data = json_decode($apiPD->getDealInfo('4586'));
//$deal_data = json_decode($apiPD->getDealInfo('4586'));
<?php

$call_status = qs("select *,value as call_status from config where `key` = 'CALL_STATUS'");
if (strtolower($call_status['call_status']) != "on") {
    qi("test", array("t" => "call distribution is off."));
    die;
}
_errors_on();
# Get Pipedrive API object
$apiPD = new apiPipeDrive();
$apiCall = new callWebhook();
# Set default timezone
date_default_timezone_set('America/New_York');
# receive the payload
$payload = file_get_contents('php://input');
$data = json_decode(@$payload, true);
//Getting Deal Info and change stage if pipeline id is '1' (i.e. for "Leads")
$deal_info = $apiPD->getDealInfo($data['current']['id']);
$deal_info = json_decode($deal_info, TRUE);
if ($deal_info['data']['pipeline_id'] == "1") {
    $deal_info = $apiPD->modifyDeal($data['current']['id'], array("stage_id" => "1"));
}
// store into the database
//qi("pd_push_notification_log", array("payload" => _escape($payload)));
# now, identify if that is hot lead then get the number of customer and start calling the customer
# c2a6fc3129578b646ae55717ed15f03ce3ee4df0 - this is key for custom attribute/field - "Source"
$deal_source = $data['current']['c2a6fc3129578b646ae55717ed15f03ce3ee4df0'];
if (in_array($deal_source, array('37')) || 1) {
    # Get Person ID
    $person_id = $data['current']['person_id'];
    # Get Deal ID
                 $new_agent_numbers[] = $each_value;
             }
         }
         if (count($new_agent_numbers) > 0) {
             qu("voice_call", array("in_progress" => "1"), "deal_id='" . $each_call['deal_id'] . "'");
             $apiCall = new callWebhook();
             echo "cust: " . $first_voice_call[0]['customer_phone'] . "<br>";
             echo "Agents: <br>";
             d($new_agent_numbers);
             $apiCall->callNow($first_voice_call[0]['customer_phone'], $new_agent_numbers, $each_call['deal_id'], "1", $cate);
         } else {
             qu("voice_call", array("is_aborted" => "2"), "deal_id='" . $each_call['deal_id'] . "'");
             $data = qs("select * From pd_users where is_active='1' and is_default='1' order by name asc");
             $dealId = $each_call['deal_id'];
             if (!empty($data)) {
                 $apiPD = new apiPipeDrive();
                 $agent_id = $data['pd_id'];
                 $deal_data = json_decode($apiPD->getDealInfo($dealId));
                 $person_id = isset($deal_data->data->person_id->value) ? $deal_data->data->person_id->value : '';
                 $org_id = isset($deal_data->data->org_id->value) ? $deal_data->data->org_id->value : '';
                 $apiPD->assignDeal($dealId, $agent_id);
                 $apiPD->assignPerson($person_id, $agent_id);
                 $apiPD->assignOrganization($org_id, $agent_id);
                 die;
             }
         }
         break;
     }
 } else {
     qu("voice_call", array("is_handled" => "1"), "deal_id='" . $each_call['deal_id'] . "'");
 }
function sendNote($deal_id, $message)
{
    $apiPD = new apiPipeDrive();
    $data['deal_id'] = $deal_id;
    $data['content'] = $message;
    $data = $apiPD->createNote($data);
}
$agent_call_dialed = q("select * from agent_call_dialed where is_updated='0' and created_at < DATE_SUB(NOW(), INTERVAL 2 minute) group by deal_id");
if (count($agent_call_dialed) > 0) {
    foreach ($agent_call_dialed as $each_deal) {
        $new_deal = qs("select * from call_detail where deal_id='{$each_deal['deal_id']}'");
        if (isset($new_deal) && !empty($new_deal)) {
            //echo "<br>-" . $each_deal['is_updated'] . "-not-" . $each_deal['deal_id'];
        } else {
            $new = array();
            $new['customer_phone'] = $each_deal['customer_phone'];
            $new['deal_id'] = $each_deal['deal_id'];
            $new['call_handled'] = '0';
            $a = qi("call_detail", $new);
            //echo "<br>-" . $each_deal['is_updated'] . "-added-" . $each_deal['deal_id'];
        }
    }
    $apiPD = new apiPipeDrive();
    $agent_call_dialed = q("select * from agent_call_dialed where is_updated='0'");
    foreach ($agent_call_dialed as $each_deal) {
        $deal_data = $apiPD->getDealInfo($each_deal['deal_id']);
        $deal_data = json_decode($deal_data);
        //d($deal_data);
        $person_id = isset($deal_data->data->person_id->value) ? $deal_data->data->person_id->value : '';
        $org_id = isset($deal_data->data->org_id->value) ? $deal_data->data->org_id->value : '';
        $source_id = isset($deal_data->data->c2a6fc3129578b646ae55717ed15f03ce3ee4df0) ? $deal_data->data->c2a6fc3129578b646ae55717ed15f03ce3ee4df0 : '';
        $cust_name = isset($deal_data->data->person_id->name) ? $deal_data->data->person_id->name : '';
        $add_time = isset($deal_data->data->add_time) ? $deal_data->data->add_time : '';
        $add_time = date("Y-m-d H:i:s", strtotime($add_time) - 3600 * 4);
        $cust_email = isset($deal_data->data->person_id->email[0]->value) ? $deal_data->data->person_id->email[0]->value : '';
        $org_name = isset($deal_data->data->org_name) ? $deal_data->data->org_name : '';
        $created_at = qs("select created_at from agent_call_dialed where deal_id='{$each_deal['deal_id']}' order by id asc limit 0,1");
        $call_detail_fields['source_id'] = $source_id;
<?php

$api = new apiPipeDrive();
$data = $api->getDealField('12463');
$data = json_decode($data, true);
$source_list_arr = array();
$source_list_arr = $data["data"]["options"];
d($source_list_arr);
//Set Flag is_deleted=1 in all the records
unset($fields);
$fields["is_deleted"] = 1;
qu("pd_sources", $fields, " 1=1 ");
if (count($source_list_arr) > 0) {
    foreach ($source_list_arr as $each_source) {
        $source_id = '';
        $source_id = trim($each_source["id"]);
        if ($source_id != '') {
            $check_source_id = array();
            $check_source_id = Call_distribution::CheckMainSourceId($source_id);
            echo $check_source_id . "****" . $each_source["label"];
            echo "<br/>";
            $data_insert = 1;
            if (!empty($check_source_id)) {
                if ($check_source_id["id"] != '') {
                    unset($fields);
                    $fields["source_name"] = trim($each_source["label"]);
                    $fields["is_deleted"] = 0;
                    $fields = _escapeArray($fields);
                    qu("pd_sources", $fields, " id = '{$check_source_id}' ");
                    $data_insert = 0;
                }
<?php

_errors_on();
$apiPD = new apiPipeDrive();
$apiCall = new callWebhook();
# Set default timezone
date_default_timezone_set('America/New_York');
# receive the payload
//Test Mode  - Remove comment on Live
$payload = file_get_contents('php://input');
$data = json_decode(@$payload, true);
//Test Mode - Delete following static code
//$payload1='{"v":1,"matches_filters":null,"meta":{"v":1,"action":"added","object":"activity","id":19126,"company_id":501009,"user_id":990918,"host":"sprout2.pipedrive.com","timestamp":1454411818,"permitted_user_ids":["*"],"trans_pending":false,"is_bulk_update":false},"retry":0,"current":{"id":19126,"company_id":501009,"user_id":990918,"done":false,"type":"text","reference_type":"none","reference_id":null,"due_date":"2016-02-02","due_time":"","duration":"","add_time":"2016-02-0211:16:58","marked_as_done_time":"","subject":"Dave Jay - (918)460-312","deal_id":4586,"org_id":4058,"person_id":4421,"active_flag":true,"update_time":"2016-02-0211:16:58","gcal_event_id":null,"google_calendar_id":null,"google_calendar_etag":null,"person_name":"DaveTest","org_name":"DaveTest","note":"Thisistest","deal_title":"DaveTestdeal","assigned_to_user_id":990918,"created_by_user_id":990918,"owner_name":"DaveJay(Programmer)","person_dropbox_bcc":"*****@*****.**","deal_dropbox_bcc":"*****@*****.**","participants":[],"updates_story_id":128972,"parties":[{"id":3108,"activity_id":19126,"party_id":3108,"active_flag":true,"add_time":"2016-02-0211:16:58","update_time":null,"name":"DaveJay(Programmer)","address":"*****@*****.**","person_id":null,"user_id":990918},{"id":3109,"activity_id":19126,"party_id":3109,"active_flag":true,"add_time":"2016-02-0211:16:58","update_time":null,"name":"DaveTest","address":"","person_id":4421,"user_id":null}]},"previous":null,"event":"added.activity"}';
//$data = json_decode($payload1,true);
if ($data['meta']['host'] == 'api.pipedrive.com' || $data['current']['type'] != 'text' || $data['current']['subject'] == 'SMS - Replied By Customer') {
    die;
}
$activity_id = $data['meta']['id'];
$person_id = $data['current']['person_id'];
$person_data = $apiPD->getPersonInfo($person_id);
$person_data = json_decode($person_data, true);
$message = $data['current']['note'];
$deal_id = $data['current']['deal_id'];
$org_id = $data['current']['org_id'];
$subject = $data['current']['subject'];
$subject = preg_replace("/[^0-9]/", "", $subject);
$activityLogArray = array();
if (strlen($subject) < 10) {
    $phone_value = isset($person_data['data']['phone'][0]['value']) ? $person_data['data']['phone'][0]['value'] : '-1';
} else {
    $phone_value = $subject;
<?php

$agent_numbers = explode(',', $_REQUEST['agent_numbers']);
$dealId = _e($_REQUEST['dealId'], 0);
$phone_value = urlencode($_REQUEST['phone_value']);
$cur_agent = $_REQUEST['cur_agent'];
$agent = qs("select pd_id,name from pd_users where phone = '{$cur_agent}'");
$agent_id = $agent['pd_id'];
$agent_name = $agent['name'];
$apiPD = new apiPipeDrive();
$apiPD->assignDeal($dealId, $agent_id);
$deal_data = json_decode($apiPD->getDealInfo($dealId));
//$deal_data = json_decode($apiPD->getDealInfo('4586'));
$person_id = isset($deal_data->data->person_id->value) ? $deal_data->data->person_id->value : '';
$org_id = isset($deal_data->data->org_id->value) ? $deal_data->data->org_id->value : '';
$source_id = isset($deal_data->data->c2a6fc3129578b646ae55717ed15f03ce3ee4df0) ? $deal_data->data->c2a6fc3129578b646ae55717ed15f03ce3ee4df0 : '';
$cust_name = isset($deal_data->data->person_id->name) ? $deal_data->data->person_id->name : '';
$add_time = isset($deal_data->data->add_time) ? $deal_data->data->add_time : '';
$add_time = date("Y-m-d H:i:s", strtotime($add_time) - 3600 * 4);
$cust_email = isset($deal_data->data->person_id->email[0]->value) ? $deal_data->data->person_id->email[0]->value : '';
$org_name = isset($deal_data->data->org_name) ? $deal_data->data->org_name : '';
$apiPD->assignPerson($person_id, $agent_id);
$apiPD->assignOrganization($org_id, $agent_id);
$call_detail_data = q("select * from call_detail where sid='{$_REQUEST['CallSid']}'");
$call_detail_fields['agent_phone'] = $cur_agent;
$call_detail_fields['agent_id'] = $agent_id;
$call_detail_fields['agent_name'] = $agent_name;
$call_detail_fields['status'] = isset($_REQUEST['CallStatus']) ? $_REQUEST['CallStatus'] : '';
$call_detail_fields['customer_phone'] = $phone_value;
$call_detail_fields['source_id'] = $source_id;
$call_detail_fields['customer_name'] = $cust_name;
<?php

//Test Mode - Live Server Comment following 2 line.
//$_REQUEST['From']="+918460422312";
//$_REQUEST['Body']="Test Static Data";
$apiPD = new apiPipeDrive();
$apiCall = new apiCall();
$phone_value = urldecode($_REQUEST['From']);
$phone_value = last10Char($phone_value);
$payload = file_get_contents('php://input');
$activity_data = qs("select * from activity_log where phone_last10 like '%{$phone_value}%' order by id desc");
if (isset($activity_data)) {
    $fields['subject'] = 'SMS - Replied By Customer';
    $fields['done'] = '1';
    $fields['type'] = 'text';
    $fields['deal_id'] = $activity_data['deal_id'];
    // Test Deal Id - $fields['deal_id'] = '4586';
    $fields['person_id'] = $activity_data['person_id'];
    $fields['org_id'] = $activity_data['org_id'];
    $fields['note'] = _escape(urldecode($_REQUEST['Body']));
    $data = $apiPD->createActivity($fields);
    qi('test', array('payload' => $data));
}
qi('test', array('payload' => $payload, 't' => $_REQUEST['From']));
die;