<?php

//
class scale_motex extends scale {

    //to submit one or more products to the scale, even if it is submitted before
    public function register_products($products_arr) {
        return false;
    }

    //to assign specific key with specific product [$key_no is a value from 1 to 54], [$product_plu is of 6 numbers]
    public function assign_product_key($key_no, $product_plu) {
        return false;
    }

    //to reset all keys on the scale
    public function reset_all_keys() {
        return false;
    }

    //to remove products from the scale
    public function unregister_products($products_plu_arr) {
        return false;
    }

    //full register data [products & keys]
    public function full_register_products($data_arr) {
        $db_file = $this->get_scale_property('db_path');
        $db_pass = $this->get_scale_property('db_pass');
        if (!file_exists($db_file)) {
            if ($this->is_debug) {
                echo "<pre>===================== {$this->get_scale_row('scale_name')} =====================<br/>";
                echo 'File Not Exist [' . $db_file . ']<br/><br/>';
            }
            return false;
        }
        try {
            //extension=php_pdo_odbc.dll in php.ini
            $db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ={$db_file}; Uid=; Pwd={$db_pass};");
//            $db->exec('SET NAMES utf8');
//            $db->exec('SET CHARACTER SET utf8');
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            //delete all old data first
            $db->query("delete from Item;");
            $db->query("delete from Preset;");
            //fill products data
            if (count($data_arr['products'])) {
                //pos flag is any 2 digits that the barcode will start with it
                $sql = "INSERT INTO `Item` (item_num, item_name, item_name2, unitprice, pos_flg, pos_code) VALUES (?,?,?,?,20,?)";
                foreach ($data_arr['products'] as $row) {
                    $statement = $db->prepare($sql);
                    $i = 0;
                    $statement->bindValue(++$i, $row['Product_PLU']);
                    $statement->bindValue(++$i, $row['Product_Name']);
                    $statement->bindValue(++$i, $row['Product_Description']);
                    $statement->bindValue(++$i, $row['Product_Price']);
                    $statement->bindValue(++$i, $row['Product_PLU']);
                    $statement->execute();
                }
            }
            //fill products keys data
            if (count($data_arr['keys'])) {
                $sql = "INSERT INTO `Preset` (page_num, num, str) VALUES (1,?,?)";
                foreach ($data_arr['keys'] as $key => $plu) {
                    $statement = $db->prepare($sql);
                    $i = 0;
                    $statement->bindValue(++$i, $key);
                    $statement->bindValue(++$i, $plu);
                    $statement->execute();
                }
            }
        } catch (PDOException $e) {
            if ($this->is_debug) {
                echo "<pre>===================== {$this->get_scale_row('scale_name')} =====================<br/>";
                echo 'PDO Error: ' . $e->getMessage() . '<br/><br/>';
            }
            return false;
        }
        //
        return true;
    }

}
