<?php

class Invoice {

    private $id;
    private $amount;
    private $date;
    private $package;
    private $paymentMode;
    private $wallet;
    private $status;
    private $type;
    private $user;
    private $confirmedDate;
    private $roi;
    private $days;
    private $proof;
    private $iType;
    private $via;
    private $count;
    private $cReturns;
    private $wBalance;

    function setId($id) {
        $this->id = $id;
    }

    function getId() {
        return $this->id;
    }

    function setAmount($amount) {
        $this->amount = $amount;
    }

    function getAmount() {
        return $this->amount;
    }

    function setDate($date) {
        $this->date = $date;
    }

    function getDate() {
        return $this->date;
    }

    function setPackage($package) {
        $this->package = $package;
    }

    function getPackage() {
        return $this->package;
    }

    function setPaymentMode($paymentMode) {
        $this->paymentMode = $paymentMode;
    }

    function getPaymentMode() {
        return $this->paymentMode;
    }

    function setWallet($wallet) {
        $this->wallet = $wallet;
    }

    function getWallet() {
        return $this->wallet;
    }

    function setStatus($status) {
        $this->status = $status;
    }

    function getStatus() {
        return $this->status;
    }

    function setType($type) {
        $this->type = $type;
    }

    function getType() {
        return $this->type;
    }

    function setUser($user) {
        $this->user = $user;
    }

    function getUser() {
        return $this->user;
    }

    function setConfirmedDate($confirmedDate) {
        $this->confirmedDate = $confirmedDate;
    }

    function getConfirmedDate() {
        return $this->confirmedDate;
    }

    function setRoi($roi) {
        $this->roi = $roi;
    }

    function getRoi() {
        return $this->roi;
    }

    function setDays($days) {
        $this->days = $days;
    }

    function getDays() {
        return $this->days;
    }

    function setProof($proof) {
        $this->proof = $proof;
    }

    function getProof() {
        return $this->proof;
    }

    function setIType($iType) {
        $this->iType = $iType;
    }

    function getIType() {
        return $this->iType;
    }

    function setVia($via) {
        $this->via = $via;
    }

    function getVia() {
        return $this->via;
    }

    function setCount($count) {
        $this->count = $count;
    }

    function getCount() {
        return $this->count;
    }

    function setCReturns($cReturns) {
        $this->cReturns = $cReturns;
    }

    function getCReturns() {
        return $this->cReturns;
    }

    function setWBalance($wBalance) {
        $this->wBalance = $wBalance;
    }

    function getWBalance() {
        return $this->wBalance;
    }

    /**
     * Get all invoices on the system, if email is provided
     * It fetches the invoice of the particular user.
     *
     * @param mysqli $link
     * @access public
     *
     * @return Invoice Array of invoice or null;
     */
    public static function getInvoices($link, $email = NULL) {
        if (!$link instanceof mysqli) {
            throw new Exception("Bad arguments passed: \n");
        }
        if (is_null($email)) {
            $query = "select * from invoice";
        } else {
            $query = "select * from invoice WHERE `user` = '$email'";
        }
        $invoices = array();
        $result = mysqli_query($link, $query);
        if (mysqli_num_rows($result) === 0) {
            return NULL;
        }
        for ($i = 0; $i < mysqli_num_rows($result); $i++) {
            $invoice = mysqli_fetch_object($result, Invoice::class);
            array_push($invoices, $invoice);
            if (mysqli_more_results($link)) {
                mysqli_next_result($link);
            }
        }
        return $invoices;
    }

    /**
     * Get all current invoices on the system running, if email is provided
     * It fetches the invoice of the particular user.
     *
     * @param mysqli $link
     * @access public
     *
     * @return Invoice Array of invoice or null;
     */
    public static function getCurrentInvoices($link, $email = NULL) {
        if (!$link instanceof mysqli) {
            throw new Exception("Bad arguments passed: \n");
        }
        $status = GROWING;
        $status1 = "Completed";
        $status2 = Paused;
        if (is_null($email)) {
            $query = "select * from invoice WHERE status = '$status' or (status = '$status2' or status = '$status1')";
        } else {
            $query = "select * from invoice WHERE `user` = '$email' and (status = '$status' or (status = '$status2' or status = '$status1'))";
        }
        $invoices = array();
        $result = mysqli_query($link, $query);
        if (mysqli_num_rows($result) === 0) {
            return NULL;
        }
        for ($i = 0; $i < mysqli_num_rows($result); $i++) {
            $invoice = mysqli_fetch_object($result, Invoice::class);
            array_push($invoices, $invoice);
            if (mysqli_more_results($link)) {
                mysqli_next_result($link);
            }
        }
        return $invoices;
    }
    
      /**
     * Get all current invoices on the system running, if email is provided
     * It fetches the invoice of the particular user.
     *
     * @param mysqli $link
     * @access public
     *
     * @return Invoice Array of invoice or null;
     */
    public static function getConfirmedInvoices($link, $email = NULL) {
        if (!$link instanceof mysqli) {
            throw new Exception("Bad arguments passed: \n");
        }
        $status = CONFIRMED;
        if (is_null($email)) {
            $query = "select * from invoice WHERE status = '$status'";
        }
        $invoices = array();
        $result = mysqli_query($link, $query);
        if (mysqli_num_rows($result) === 0) {
            return NULL;
        }
        for ($i = 0; $i < mysqli_num_rows($result); $i++) {
            $invoice = mysqli_fetch_object($result, Invoice::class);
            array_push($invoices, $invoice);
            if (mysqli_more_results($link)) {
                mysqli_next_result($link);
            }
        }
        return $invoices;
    }

 /**
     * Get all current capital on the system running of a particular user.
     *
     * @param mysqli $link
     * @access public
     *
     * @return Invoice Array of invoice or null;
     */
    public static function getUserCapital($link, $email) {
        if (!$link instanceof mysqli) {
            throw new Exception("Bad arguments passed: \n");
        }
        $status = GROWING;
        $status2 = Paused;

        $query = "select sum(amount) as capital from invoice WHERE `user` = '$email' and (status = '$status' or status = '$status2')";
        $result = mysqli_query($link, $query);
        if (mysqli_num_rows($result) === 0) {
            return 0.00;
        } else {
            $capital = mysqli_fetch_assoc($result);
            return $capital['capital'];
        }
    }
    
     /**
     * Get all current profit on the system running of a particular user.
     *
     * @param mysqli $link
     * @access public
     *
     * @return Invoice Array of invoice or null;
     */
    public static function getUserProfit($link, $email) {
        if (!$link instanceof mysqli) {
            throw new Exception("Bad arguments passed: \n");
        }
        $status = GROWING;
        $status2 = Paused;

        $query = "select sum(`cReturns`) as capital from invoice WHERE `user` = '$email' and (status = '$status' or status = '$status2')";
        $result = mysqli_query($link, $query);
        if (mysqli_num_rows($result) === 0) {
            return 0.00;
        } else {
            $capital = mysqli_fetch_assoc($result);
            return $capital['capital'];
        }
    }


    /**
     * Create an invoice on this system
     *
     * @param Invoice $invoice
     * @param mysqli $link
     * @access public
     *
     * @return Invoice invoice or null;
     */
    public static function createInvoice($invoice, $link) {
        $invoice->setStatus(PENDING);

        $stmt = mysqli_prepare($link, "insert into invoice(`iType`, `user`,`date`,amount,status,`paymentMode`,package,roi,days) VALUES(?,?,?,?,?,?,?,?,?)");
        $user = $invoice->getUser();
        $date = $invoice->getDate();
        $amount = $invoice->getAmount();
        $status = $invoice->getStatus();
        $days = $invoice->getDays();
        $paymentMode = $invoice->getPaymentMode();
        $package = $invoice->getPackage();
        $iType = $invoice->getItype();
        $roi = $invoice->getRoi();
        $stmt->bind_param("sssdsssdi", $iType, $user, $date, $amount, $status, $paymentMode, $package, $roi, $days);
        $stmt->execute();
        if ($stmt->affected_rows > 0) {
            $invoice->setId($stmt->insert_id);
            return $invoice;
        } else {
            return null;
        }
    }
 public static function createInvoice1($invoice, $link) {
       
        $stmt = mysqli_prepare($link, "insert into invoice(`iType`, `user`,`date`,amount,status,`paymentMode`,package,roi,days) VALUES(?,?,?,?,?,?,?,?,?)");
        $user = $invoice->getUser();
        $date = $invoice->getDate();
        $amount = $invoice->getAmount();
        $status = $invoice->getStatus();
        $days = $invoice->getDays();
        $paymentMode = $invoice->getPaymentMode();
        $package = $invoice->getPackage();
        $iType = $invoice->getItype();
        $roi = $invoice->getRoi();
        $stmt->bind_param("sssdsssdi", $iType, $user, $date, $amount, $status, $paymentMode, $package, $roi, $days);
        $stmt->execute();
        if ($stmt->affected_rows > 0) {
            $invoice->setId($stmt->insert_id);
            return $invoice;
        } else {
            return null;
        }
    }
    /**
     * Confirm an invoice on this system
     *
     * @param Invoice $invoice
     * @param mysqli $link
     * @param String $email
     * @param String $date
     * @access public
     *
     * @return Invoice invoice or null;
     */
    public static function confirmInvoice($invoiceId, $link, $email, $date) {
        $status = CONFIRMED;
        $response = mysqli_query($link, "update invoice SET status = '$status', `confirmedDate` = '$date', `cReturns` = 0, `count` = 0, `wBalance` = 0 WHERE id = $invoiceId and `user` = '$email' ");
        if (mysqli_affected_rows($link) > 0) {
            $response = true;
        } else {
            $response = FALSE;
        }
        return $response;
    }

}
