<?php

require_once "../../config.php";
require_once "../../validation.php";
require_once "../../Helper.php";
require_once "../../ajax/send_email.php";
require_once "check_edit_page_ajax.php";

include_once "../../ichat/admin/route.php";
if ($session->is_logged_in() != true ) {
        redirect_to(WEB_LINK.'/login');
} else {
    $current_user = User::get_specific_id($session->admin_id);
    $current_user->set_online();
    $group = $current_user->prvlg_group;
}


require_once("../../ichat/admin/assets/includes/Library/SessManager.php");
require_once("../../ichat/admin/assets/includes/Library/User.php");
$session = new  SessManager();

require_once '../database1.php';
class convertToInvoice extends database
{
    public function index($checkoutID)
    {
        $this->dbh = $this->connectPDO1();
        $this->dbh->beginTransaction();
        try
        {
            $count = $this->current_count();
            $data['cl_invoice_no'] = $count['cl_prefix'] . '-' . $count['cl_count'];
            $data1['cl_count'] = $count['cl_count'] + 1;
            $where1['cl_id'] = $count['cl_id'];

            $autoID = $this->update1('tb_auto_incre', $data1, $where1);
            if(!$autoID)
            {
                $this->dbh->rollback();
                return 0;
                exit;
            }

            $data['cl_invoice'] = 1;
        	$where['cl_id'] = $checkoutID;
            $res = $this->update('tb_checkout',$data, $where);
            if(!$res)
            {
                $this->dbh->rollback();
                return 0;
                exit;
            }
            $this->dbh->commit();
            return $data['cl_invoice_no'];
            exit;
        }
        catch (PDOException $e) {
            // roll back transaction
            $this->dbh->rollback();
            return 0;
        }
    }

    public function getCheckout($checkoutID)
    {
        return $this->fetchRow('tb_checkout', ['cl_id' => $checkoutID]);
    }

    public function current_count()
    {
        $where['cl_type'] = 'Invoice';
         return $this->fetchRow('tb_auto_incre', $where);
    }

    public function getAllCheckoutItems($checkoutID)
    {
        $sql = " SELECT ci.*, p.cl_name FROM tb_checkout_item ci INNER JOIN tb_product p ON ci.cl_product_id=p.cl_id WHERE ci.cl_deleted_at IS NULL AND ci.cl_checkout_id=? ";
        $where[] = $checkoutID;
        return $this->callQuery($sql, $where);
    }
}

if(!isset($_GET['id']) || !is_numeric($_GET['id']))
{
    http_response_code(400);
    echo json_encode(array('error_text' => 'Order Not Found'));
    exit;
}

$checkoutID = $_GET['id'];

$obj = new convertToInvoice();
$checkout = $obj->getCheckout($checkoutID);
if(!$checkout)
{
    http_response_code(400);
    echo json_encode(array('error_text' => 'Checkout Not Found'));
    exit;
}

$checkoutItems = $obj->getAllCheckoutItems($checkoutID);

$invoiceNo = $obj->index($id);
if(!$invoiceNo)
{
	http_response_code(401);
	echo json_encode(array('error_text' => 'Something Went Wrong'));
	exit;
}


session_start();
$_SESSION['error_message'] = 'Order Converted to Invoice Successfully';
$_SESSION['error_type'] = 'success';

http_response_code(200);
echo json_encode(array('message' => 'Order Converted to Invoice Successfully'));
exit;
