<?php


class DB_PRODUCTS_TRANSACTIONS
{
    public $DB = false;
    public $tablename = 'products_transactions';

    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 NewProductTransactions($productuid, $storeuid, $quantity, $transactiontype, $transactionsessionhash = '', $subtype = '', $createdfor = '', $price = '', $owneruid = '', $createdby = '', $name = '', $description = '', $transactiondata = '', $remarks = '')
    {

        if (!$productuid || !$storeuid || !$transactiontype || !$quantity) {
            return false;
        }

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

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

        $data = [];

        $AddtoDataifNOTEmpty = function ($varname, $varvalue) use (&$data) {
            if (!$varname || !$varvalue) {
                return false;
            }

            if (is_array($varvalue)) {
                $varvalue = tryjsonencode($varvalue);
            }
            $data[$varname] = $varvalue;
        };

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

        if (!$transactionsessionhash) {
            $transactionsessionhash = DBQUERY()->PRODUCTS_TRANSACTIONS_SESSIONS()->NewProductsTransactionSession($storeuid);
        } else {
            if (!DBQUERY()->PRODUCTS_TRANSACTIONS_SESSIONS()->checkifUIDorHashKeyexist($transactionsessionhash)) {
                return false;
            }

        }


        if (!$transactiondata) {
            $transactiondata = [];
        }

        $storeexists = DBQUERY()->STORES()->CheckifUIDorHashKeyExist($storeuid);
        if (!$storeexists) {
            return false;
        }
        $productexists = DBQUERY()->PRODUCTS()->CheckifUIDorHashKeyExist($productuid);
        if (!$productexists) {
            return false;
        }


        $logs = [$date, 'Created New Product Transaction by useruid(' . $createdby . ') at ' . $modified . ' .'];

        $AddtoDataifNOTEmpty('productuid', $productuid);
        $AddtoDataifNOTEmpty('storeuid', $storeuid);
        $AddtoDataifNOTEmpty('quantity', $quantity);
        $AddtoDataifNOTEmpty('transactiontype', $transactiontype);
        $AddtoDataifNOTEmpty('transactionsessionhash', $transactionsessionhash);
        $AddtoDataifNOTEmpty('subtype', $subtype);
        $AddtoDataifNOTEmpty('createdfor', $createdfor);
        $AddtoDataifNOTEmpty('price', $price);
        $AddtoDataifNOTEmpty('owneruid', $owneruid);
        $AddtoDataifNOTEmpty('createdby', $createdby);
        $AddtoDataifNOTEmpty('name', $name);
        $AddtoDataifNOTEmpty('description', $description);
        $AddtoDataifNOTEmpty('description', $remarks);
        $AddtoDataifNOTEmpty('transactiondata', $transactiondata);
        $AddtoDataifNOTEmpty('transactiondata', $logs);
        $AddtoDataifNOTEmpty('created', $created);
        $AddtoDataifNOTEmpty('modified', $modified);

        return $this->InsertIntoDB($data);

    }


    function getTransactionsSessions()
    {

    }

}

class DB_PRODUCTS_TRANSACTIONS_QUICK_MULTIPLESEARCH
{
    use DBClassSearch;
    public $data;

    public $tablename = 'products_transactions';

    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($DB = false)
{
    return new DB_PRODUCTS_TRANSACTIONS($DB);
}

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