<?php
class Mupdate extends Model
{
    function getProduct($productId)
    {
        $query = "SELECT * FROM cl_hlusers.o_products WHERE lang = 0 AND id = {$productId}";
        $product = $this->db->query($query)->row();

        if ($product->akcio != '' && $product->akcio != null && date('Y-m-d') <= $product->ackio_date)
            $product->price = $product->akcio;
        else
            $product->price = $product->euro;

        if ($product->type == 8)
            $product->header = 'ARCHLine.XP PROFESSIONAL UPGRADE';
        elseif ($product->type == 4)
            $product->header = 'ARCHLine.XP LT UPGRADE';
        else
            $product->header = 'ARCHLine.XP LIVE UPGRADE';

        if ($productId == 1282 || $productId == 1283)
            $product->header .= ' FOR 2 OR 3 YEARS';

        return $product;
    }

    function GetLanSignFromLanUserNumber($users)
    {
        $lan = 0;

        if ($users == 0) $lan = 0;
        elseif ($users == 1) $lan = 1;
        elseif ($users == 2) $lan = 2;
        elseif ($users == 3) $lan = 3;
        elseif ($users == 4) $lan = 4;
        elseif ($users == 5) $lan = 5;
        elseif ($users == 10) $lan = 6;
        elseif ($users == 15) $lan = 7;
        elseif ($users == 20) $lan = 8;
        elseif ($users <= 49) $lan = 9;

        return $lan;
    }

    function createPassword($key, $verCode, $type, $lan = 0)
    {
        $lanSign = $this->GetLanSignFromLanUserNumber($lan);

        $command    = PATH_CODEGEN . " -Password " . $key . $verCode . $lanSign . $type;
        $ph         = popen($command, "r") or die("A 'codegen' paranccsal nem veheto fel kapcsolat");
        $serial     = fgets($ph, 1024);
        pclose($ph);

        return $serial;
    }

    function getOrderNumber()
    {
        $lastOrder      = $this->db->query("SELECT o.order FROM cl_hlusers.o_orders o WHERE o.country = 0 AND id > 4726 ORDER BY o.id DESC LIMIT 1")->row();
        $orderNumber    = $lastOrder->order;
        $first          = strpos($orderNumber, "-") + 1;
        $last           = strpos($orderNumber, "/") - 2;
        $number         = (int)substr($orderNumber, $first, $last) + 1;

        return "E-" . $number . "/" . date("Y");
    }

    function createLicence($password, $verID, $type, $orderNumber, $prLan, $price, $productID)
    {
        $hlNum = substr($password, 0, 6);

        $updateQuery = "UPDATE cl_hlusers.h_programs
            SET prStat = 1
            WHERE prHlNum = '{$hlNum}'";

        $this->db->query($updateQuery);

        if ($type == 'L')
            $type = 76;

        $prInfo     = $price > 0 ? 'ShareIT' : '';
        $prFizetve  = $productID == 1856 ? '2025-01-30' : '2050-01-01';

        $license                        = new stdClass();
        $license->prTypeID              = $type;
        $license->prVerID               = $verID;
        $license->prLan                 = $prLan;
        $license->prPass                = $password;
        $license->prSellDate            = date("Y-m-d H:i:s");
        $license->prOrdNum              = $orderNumber;
        $license->prActDate             = date("Y-m-d H:i:s");
        $license->prFizetve             = $prFizetve;
        $license->prTime                = date("Y-m-d H:i:s");
        $license->prLastMod             = date("Y-m-d H:i:s");
        $license->prContact             = 0;
        $license->prHlNum               = substr($password, 0, 6);
        $license->prManID               = 36;
        $license->prCtrID               = 44;
        $license->prStat                = 0;
        $license->prActCode             = '';
        $license->prInvoiceDateFrom     = date('Y-m', time());
        $license->old_db                = 'clusers_eng';
        $license->old_prID              = 0;
        $license->prHiType              = 3;
        $license->prPrice               = $price;
        $license->prInfo                = $prInfo;

        $this->db->insert("cl_hlusers.h_programs", $license);

        $history            = new stdClass();
        $history->hiHlNum   = substr($password, 0, 6);
        $history->hiDate    = date("Y-m-d");
        $history->hiManID   = 36;
        $history->hiTypeID  = 3;
        $history->hiTime    = date("Y-m-d H:i:s");
        $history->hiInfo    = "Angol oldal rendelés, Aktiválás érvényessége: " . $prFizetve;

        $this->db->insert("cl_hlusers.h_history", $history);

        if ($productID == 1856) {
            $query = "SELECT hlUser FROM cl_hlusers.hardlock WHERE hlNum = '" . substr($password, 0, 6) . "'";
            $hardlock = $this->db->query($query)->row();

            $query = "SELECT id FROM www_archline_hu.jml_users WHERE nUserID = '" . $hardlock->hlUser . "'";
            $jmlUser = $this->db->query($query)->row();

            $query = "SELECT * FROM www_archline_hu.jml_user_usergroup_map WHERE user_id = '" . $jmlUser->id . "' AND group_id = 17";
            $jmlUserGroup = $this->db->query($query)->row();

            if (empty($jmlUserGroup)) {
                $jmlUserGroupMap            = new stdClass();
                $jmlUserGroupMap->user_id   = $jmlUser->id;
                $jmlUserGroupMap->group_id  = 17;

                $this->db->insert("www_archline_hu.jml_user_usergroup_map", $jmlUserGroupMap);
            }

            if (!empty($hardlock)) {
                $uHistory               = new stdClass();
                $uHistory->nUserID      = $hardlock->hlUser;
                $uHistory->nTopicID     = 135;
                $uHistory->strPlace     = 'Web';
                $uHistory->strInfo      = 'Beta 2025 tesztre jelentkezett';
                $uHistory->nManagerID   = 36;
                $uHistory->insertDate   = date('Y-m-d H:i:s');
                $uHistory->dateEvent    = date('Y-m-d');

                $this->db->insert("cl_hlusers.u_history", $uHistory);
            }
        }
    }

    function createOrder($name, $email, $product, $orderNumber, $oldPassword, $password, $price, $info)
    {
        $ip = $_SERVER['REMOTE_ADDR'];

        $order              = new stdClass();
        $order->ip          = $ip;
        $order->country     = 0;
        $order->date        = date('Y-m-d');
        $order->software    = $product;
        $order->user_name   = $name;
        $order->user_email  = $email;
        $order->order       = $orderNumber;
        $order->input_key   = $oldPassword;
        $order->output_key  = $password;
        $order->price       = $price;
        $order->info        = $info;
        $order->instalments = 'in progress';

        $this->db->insert("cl_hlusers.o_orders", $order);
    }

    function getEmailTemplate($years, $email, $name, $info, $product, $serial, $price, $orderNumber, $version, $productId)
    {
        $template = new stdClass();

        $date = date('d/m/Y');

        $payment = $productId == 1856 ? 'You can download from here: <a href="https://www.archlinexp.com/public/downloads/eng/2025/ARCHLINE_XP2025_64bit_websetup.exe">ARCHLine.XP 2025 Beta</a>' : 'Payment: <b>in progress</b>';
        $facebook = $productId == 1856 ? 'Please provide your feedback on Facebook Beta Tester Group: <a href="https://www.facebook.com/groups/1829716121172036">Facebook Beta Tester Group</a> <br/><br/>' : '';

        $template->subject = $years;
        $template->body = "<html><body style='padding: 10px 30px 10px 30px'>
                <b style='font-size: 200%;'>Thank You for Your Order.</b> <br/><br/>

                We have accepted your online purchase order for the following items. <br/>
                Once processed, invoice will be sent to this e-mail address. <br/>
                Please allow 1 working day for your order to be processed. <br/><br/>

                The following order details were accepted on <b>$date</b> <br/><br/>

                Licensee E-mail: <b>{$email}</b> <br/><br/>

                Licensee name: <b>{$name}</b> <br/><br/>

                Information: <b>{$info}</b> <br/><br/>

                Item: <b>{$product}</b> <br/><br/>

                Serial code {$version}: <b>{$serial}</b> <br/><br/>

                <b>Order Total: € {$price}</b> <br/><br/>

                {$payment} <br/><br/>

                {$facebook}

                Your order number: {$orderNumber} <br/><br/>

                Thank you for your order, <br/><br/>

                CadLine Ltd. <br/><br/>

                This is an automatically generated email – please do not reply to it. <br/>
                If you have any queries regarding your order please email <a href='mailto:office@cadline.hu'>office@cadline.hu</a>.
            </body></html>";

        return $template;
    }
}
