<?php
namespace App\Models;

use CodeIgniter\Model;


class PriceList extends Model
{
	protected $table            = 'epos_pricelists';

    private static $collectionListIds = null;

    function get_all()
    {
        $db = \Config\Database::connect();

        $query = $db->table($this->table)
            ->orderBy('priority', 'ASC')
            ->orderBy('id', 'ASC');
        $result = $query->get();

        $data = [];
        foreach($result->getResult() as $pl) {
            // Return all fields as an array
            $data[$pl->id] = (array) $pl;
        }

        return $data;
    }

    public static function getCollectionListIds()
    {
        if (self::$collectionListIds === null) {
            $db = \Config\Database::connect();
            $result = $db->table('epos_pricelists')
                ->select('id')
                ->where('order_types', 'Col')
                ->get();

            self::$collectionListIds = [];
            foreach ($result->getResult() as $row) {
                self::$collectionListIds[] = $row->id;
            }
        }
        return self::$collectionListIds;
    }

    public static function isCollectionList($id)
    {
        return in_array($id, self::getCollectionListIds());
    }

    private static $allCache = null;

    public static function getAllCached()
    {
        if (self::$allCache === null) {
            $model = new self();
            self::$allCache = $model->get_all();
        }
        return self::$allCache;
    }
}