<?php


class DB_PRODUCTS_TRANSACTIONS_SESSIONS
{
    public $DB = false;
    public $tablename = 'products_transactions_sessions';

    use BASICDB;
    public function __construct($DB = false)
    {
        if (!$DB) {
            $DB = DB();
        }
        if (!$DB) {
            return false;
        }
        if ($DB) {
            $this->DB = $DB;
        }
        if (!$this->tablename) {
            return false;
        }

    }


    function NewProductsTransactionSession($storeuid, $status = 'open', $name = '', $description = '', $category = '', $subtype = '', $createdby = '', $createdfor = '', $remarks = '')
    {
        if (!$storeuid) {
            return false;
        }
        $store = DB_STORES()->getDetailsbyUIDorHashkey($storeuid);

        if (!$storeuid) {
            return false;
        }

        $store_owneruid = $store['owneruid'];
        $store_manageruid = $store['manageruid'];

        $date = serverdatetimesql();
        $created = $date;
        $modified = $date;

        global $CurrentUserUID;

        if (!$createdby) {
            $createdby = $CurrentUserUID;
        }
        if (!$createdby) {
            $createdby = CurrentUserUID();
        }

        if (!$createdfor) {
            $createdfor = $createdby;
        }

        $createdby_details = DBQUERY()->USERS()->getDetailsbyUIDorHashkey($createdby);
        if (!$createdby_details) {
            return false;
        }
        if ($createdby !== $createdfor) {
            $createdfor_details = DBQUERY()->USERS()->getDetailsbyUIDorHashkey($createdfor);
        } else {
            $createdfor_details = $createdby_details;
        }

        $ownerdetails = DBQUERY()->USERS()->getDetailsbyUIDorHashkey($store_owneruid);
        if (!$ownerdetails) {
            return false;
        }

        if (!MARKET_MAIN()->isNewTransactionAllowed($createdby_details, $createdfor_details, $ownerdetails)) {
            return false;
        }


        $logs = [$date, 'Created New Transaction by uid(' . $createdby . ')'];

        $hashkey = $this->GenerateNewHash();
        $data = [
            'hashkey' => $hashkey,
            'created' => $date,
            'modified' => $date,
            'name' => $name,
            'description' => $description,
            'category' => $category,
            'subtype' => $subtype,
            'createdby' => $createdby,
            'createdfor' => $createdfor,
            'remarks' => $remarks,
            'logs' => $logs,
            'status' => $status
        ];


        foreach ($data as $key => $value) {
            if (!$value) {
                unset($data[$key]);
            }
            if (is_array($value)) {
                $data[$key] = tryjsonencode($value);
            }
        }

        return $insert = $this->InsertIntoDB($data);



    }
}

class DB_PRODUCTS_TRANSACTIONS_SESSIONS_QUICK_MULTIPLESEARCH
{
    use DBClassSearch;
    public $data;

    public $tablename = 'products_transactions_sessions';

    private $parentidresults = [];


    public function __construct($data = [], $likefields = [], $fieldstoselectarray = '', $orderby = '', $noindex = 0, $whereappend = ' and ', $dateonlyarray = '', $newdata = false)
    {
        return $this->initialize($data, $likefields, $fieldstoselectarray, $orderby, $noindex, $whereappend, $dateonlyarray, $newdata);
    }

    function FindName($uid)
    {
        return $this->Find($fieldname = 'uid', $contenttosearch = $uid, $exact = true)[0]['name'];
    }

}

function DB_PRODUCTS_TRANSACTIONS_SESSIONS($DB = false)
{
    return new DB_PRODUCTS_TRANSACTIONS_SESSIONS($DB);
}
function DB_PRODUCTS_TRANSACTIONS_SESSIONS_QUICK_MULTIPLESEARCH($DB = false)
{
    return new DB_PRODUCTS_TRANSACTIONS_SESSIONS_QUICK_MULTIPLESEARCH($DB);
}