<?php
 
class scale_jhma extends scale {

    //important to don't install it inside 'c:\program files\..' as here may not write inside it since need high permission and may 
    //write in something like 'C:\Users\User\AppData\Local\VirtualStore\..' so best thing to install it on 'd:\' or try at 'c:\' at root

    private $scales_key_arr = array(1 => '4D', 2 => '47', 3 => '41', 4 => '3B', 5 => '35', 6 => '2F', 7 => '29', 8 => '23', 9 => '1D', 10 => '4C', 11 => '46', 12 => '40', 13 => '3A', 14 => '34', 15 => '2E', 16 => '28', 17 => '22', 18 => '1C', 19 => '4B', 20 => '45', 21 => '3F', 22 => '39', 23 => '33', 24 => '2D', 25 => '27', 26 => '21', 27 => '1B', 28 => '4A', 29 => '44', 30 => '3E', 31 => '38', 32 => '32', 33 => '2C', 34 => '26', 35 => '20', 36 => '1A', 37 => '49', 38 => '43', 39 => '3D', 40 => '37', 41 => '31', 42 => '2B', 43 => '25', 44 => '1F', 45 => '19', 46 => '48', 47 => '42', 48 => '3C', 49 => '36', 50 => '30', 51 => '2A', 52 => '24', 53 => '1E', 54 => '18');

    //to submit one or more products to the scale, even if it is submitted before
    public function register_products($products_arr) {
        $register_products_cmd = "";
        foreach ($products_arr as $row)
            $register_products_cmd .= $this->build_product_cmd($row);
        return $this->scale_write($register_products_cmd,'pro');
    }

    //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) {
        $scale_ip = $this->get_scale_row('scale_ip');
        $product_plu = str_pad($product_plu, 6, '0', STR_PAD_LEFT);
        $product_key_cmd = $scale_ip."\t"."SCP"."\t"."$key_no"."\t"."$product_plu".PHP_EOL;
        return $product_key_cmd;
    }

    //to reset all keys on the scale
    public function reset_all_keys() {
        $products_keys_reset_cmd = "C014005\nC014013";
        return $this->scale_write($products_keys_reset_cmd);
    }

    //to remove products from the scale
    public function unregister_products($products_plu_arr) {
        $unregister_products_cmd = "";
        foreach ($products_plu_arr as $product_plu)
            $unregister_products_cmd .= "C013005{$product_plu}\n";
        return $this->scale_write($unregister_products_cmd);
    }

    //full register data [products & keys]
    public function full_register_products($data_arr) {
        $is_err = $this->register_products($data_arr['products']);
        $product_key_cmd ='';
        foreach ($data_arr['keys'] as $key => $plu) {
            $product_key_cmd .= $this->assign_product_key($key, $plu);
        }
        if($this->scale_write($product_key_cmd,'shr'))
        $is_err = true;

        return  $is_err ;
    }

    //to delete public messages
    //$delete_public_cmd = "C014018";
    //scale_write($scale, $delete_public_cmd);
    //
    private function scale_write($write_command,$type='') {
        if (empty($write_command))
            return false;
        $current_dir = getcwd();
        //$scale_dir_path = SCALE_PATH;   //$iTegra_kSolutions_DataGate_path = 'D:\scale\kSolutions\DataGate';
        $scale_dir_path = $this->get_scale_property('software_path');
        chdir($scale_dir_path);
        $is_error = false;
        //
        $scale_ip = $this->get_scale_row('scale_ip');
        if (CommonFunctions::check_ping_ip($scale_ip)) {
            if($type=='pro')
            file_put_contents("JHMA_PLU.txt", $write_command);
            //
            if($type=='shr')
            file_put_contents("JHMA_SCP.txt", $write_command);
            file_put_contents("JHMA_SAL.txt", '');

            $output = array();
            $return_var = null;
            if ($this->is_debug) {
                echo "<pre>===================== {$scale_ip} =====================<br/>";
                echo print_r($output, true) . '<br/><br/>';
            }
        } else
            $is_error = true;
        //
        chdir($current_dir);
        return !$is_error;
    }

    //
    private function build_product_cmd($row) {
        $scale_ip = $this->get_scale_row('scale_ip');
        $product_plu = str_pad(mb_substr($row['Product_PLU'], 0, 6), 6, "0", STR_PAD_LEFT);
        //here if arabic character is 15 char. it give error on move to scale for that we substr to half since i think it treat it as 2 characters
        $is_arabic_name = preg_match('/[اأإء-ي]/ui', $row['Product_Name']);
        if ($is_arabic_name)
            $row['Product_Name'] = iconv('utf-8', 'ISO-8859-6', mb_substr($row['Product_Name'], 0, 26));
        $product_name = str_pad(mb_substr($row['Product_Name'], 0, 26), 26, " ", STR_PAD_RIGHT);
        //$product_name = str_pad(mb_substr($row['Product_Name'], 0, (26 / ($is_arabic_name ? 2 : 1))), 26, " ", STR_PAD_RIGHT);
        $is_arabic_desc = preg_match('/[اأإء-ي]/ui', $row['Product_Description']);
        if ($is_arabic_desc)
            $row['Product_Description'] = iconv('utf-8', 'ISO-8859-6', mb_substr($row['Product_Description'], 0, 26));
        $product_desc = str_pad(mb_substr($row['Product_Description'], 0, 26), 26, " ", STR_PAD_RIGHT);
        $product_code = str_pad(mb_substr($row['Product_Code'], 0, 5), 5, "0", STR_PAD_LEFT);
        $plu_type = $row['PLU_Type'];
        $product_price = str_pad(mb_substr($row['Product_Price'], 0, 6), 6, "0", STR_PAD_LEFT);
        /*
         * Command Character [C] # Scale Code [2 digits] # PLU Add/Modify Code [2005] #  PLU Number [6 digits] # Department Code [3 digits]
         * Family Code [3 digits] # Product Name [26 characters] # Product Description [26 characters] # PLU Code [5 digits]
         * PLU Type [1 Character (P = weighable, N = non-weighable)] # Fixed Value [7 digits] # Price [6 digits] # Alternative Price [6 digits]
         * Former Price [6 digits] # Tax 1 [6 digits] # Tax 2 [6 digits] # Pre-packaing Tar [5 digits] # Public Tare [7 digits] # Label Code [2 digits]
         * Recipe Code [4 digits] # Nutritional Code [4 digits] # Packaging Date [1 digit] # Expiry Date [3 digits] # Image Code [4 digits]
         */
        $product_cmd =$scale_ip ."\t". "PLU" ."\t". $product_plu ."\t". $product_code ."\t". "1234567891111" ."\t".
        $product_name ."\t". $product_price ."\t". '1' ."\t". "1"
        ."\t". "0,50" .PHP_EOL;
        return $product_cmd;
    }

}
