#!/usr/bin/php
<?php
    define('CRLF',"\r\n");
    ini_set('include_path',ini_get('include_path') . ":/data/sites/www.landscapeonline.com/includes");

    $requiredClasses[] = 'base/vendor_listing.php';
    $requiredClasses[] = 'base/xlist_class.php';

    require('lol_common.inc');

    $t = new vendor();
    $vl = new vendor_login();
    $query = "SELECT v1.id as v1_id, v2.id as v2_id, v1.uid as v1_uid, v2.uid as v2_uid, v1.pass as pri_pass, v2.pass" .
             " FROM vendor_login as v1, vendor_login as v2" .
             " WHERE v1.pass = v2.pass AND v1.id <> v2.id" .
             " ORDER BY v1.pass DESC";

    $dup = $t->db->getAll($query);

    if(DB::isError($duplist)) {
        printf("Error getting duplicate vendor login list: " . $dup->getMessage() . CRLF);
        return(FALSE);
    }

    foreach($dup as $obj) {
        // pass map [ key ] => array(uid list)
        $pp = strtoupper($obj['pri_pass']);
        $pass_map[$pp][$obj['v1_uid']] = $obj['v1_uid'];
        // add to global array list
        $global_id_list[] = $obj['v1_uid'];
    }

    $global_id_list = & array_unique($global_id_list);
    $gstr = implode(',',$global_id_list);
    
    $query = "SELECT v.id from new_vendor as v WHERE find_in_set(v.id, '{$gstr}') AND v.active = 1"; 

    $dup_status = $t->db->getAll($query);
    foreach($dup_status as $n) {
        $ven_status[] = $n['id'];
    }

    foreach($pass_map as $pass => $list) {
        /*
        $idlist = implode(',', $list);
        $zz = array();
        $query = "SELECT v.company_name from new_vendor as v where find_in_set(v.id,'$idlist')";
        $res = $t->db->getAll($query);
        printf("{$pass}: ");
        foreach($res as $nn) {
            $zz[] = $nn['company_name'];
        }
        printf(implode(', ', $zz));
        printf(CRLF);
        */
        printf("{$pass}: ");
        foreach($list as $id) {
            if(in_array($id, $ven_status)) {
                // active
                printf(" [{$id}]");
            } else {
                // not active
                $vl->deleteRow($id,'uid');
                printf(" D_{$id}_D");
                //printf(" {$id}");
            }
        }
        printf(CRLF);
        //printf("{$pass}: {$idlist}" . CRLF);
    }



    // now that we have our list of id's.. we can see if there are any that are either non-existent or non-active.  if so, -we shall delete them-

    
    /*$xl = new xlist();
    $query = "SELECT id, xlist, status FROM new_vendor WHERE active=1";

    $vlist = $xl->db->getAll($query);

    if(DB::isError($vlist)) {
        printf("Error getting vendor list: "  . $vlist->getMessage() . CRLF);
        return(FALSE);
    }


    $vXl = array();
    foreach($vlist as $id => $obj) {
        foreach(explode(',',$obj['xlist']) as $x) {
            $vXl[] = $x;
        }
    }

    $zzz = array_count_values($vXl);

    printf("Total Vendors: " . count($vlist) . CRLF);

    $parents = $xl->fetchXlistParentArray();
    foreach($parents as $p) {
        print("{$p['id']}: {$p['name']}" . CRLF);
        $children = $xl->fetchXlistChildrenArray($p['id']);
        $pCount = 0;
        foreach($children as $c) {
            if(!array_key_exists($c['id'], $zzz)) {
                $zzz[$c['id']] = 0;
            } else {
            }
            $pCount += $zzz[$c['id']];
            print("\t{$c['sub_number']}:: {$c['name']}: {$zzz[$c['id']]}" . CRLF);
        }
        print(CRLF . "*** Total for {$p['name']}: {$pCount} ***" . CRLF . CRLF);
    }
    */
?>
